binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

Optimize checks for higher bits using a low bit mask

Open kripken opened this issue 3 years ago • 1 comments

E.g.

(i32.ne
 (i32.and
  (local.get $0)
  (i32.const 268435455) ;; 0x0fffffff
 )
 (local.get $0)
)
 =->
(i32.ge_u
 (local.get $0)
 (i32.const 268435456)
)

(x & low-bit-mask) != x implies that x has some bit set that is larger than the bitmask, so we can just look for those bits.

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

kripken avatar Sep 02 '22 19:09 kripken

The same thing can be done with a high bit mask, e.g.

(i32.eqz
 (i32.and
  (local.get $0)
  (i32.const -8) ;; 0xfffffff8
 )
)
 =->
(i32.lt_u
 (local.get $0)
 (i32.const 8)
)

(rule #16)

kripken avatar Sep 02 '22 19:09 kripken