binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

Optimize sign checks on i8, i16 etc.

Open kripken opened this issue 3 years ago • 0 comments

E.g.

(select
 (local.get $0)
 (local.get $1)
 (i32.lt_s
  (i32.extend8_s
   (local.get $2)
  )
  (i32.const 0)
 )
)
 =->
(select
 (local.get $0)
 (local.get $1)
 (i32.and
  (local.get $2)
  (i32.const 128)
 )
)

When in a boolean context, doing an extend and then <0 is the same as checking the sign bit of the size we are extending from.

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

kripken avatar Sep 02 '22 18:09 kripken