binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

Remove masks that are shifted out later

Open kripken opened this issue 3 years ago • 1 comments

E.g.

(i32.shl
 (i32.and
  (local.get $0)
  (i32.const 1073741823) ;; 0x3fffffff
 )
 (i32.const 2)
)
 =->
(i32.shl
 (local.get $0)
 (i32.const 2)
)

Only the top two bits are missing in the mask, and we shift left, so any effect the and had is removed.

Found by the superoptimizer https://github.com/WebAssembly/binaryen/pull/4994 (for comparison to other findings: rule #23, benefit 12936).

kripken avatar Sep 02 '22 19:09 kripken

A related example, rule #27

(i32.and
 (i32.or
  (local.get $0)
  (i32.shl
   (local.get $1)
   (i32.const 16)
  )
 )
 (i32.const 4)
)
 =->
(i32.and
 (local.get $0)
 (i32.const 4)
)

$1 is shifted left so much that it can't contribute to the final and.

kripken avatar Sep 02 '22 20:09 kripken