-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
performance: Peephole optimise (128 : Word8 & x == 0) resp. (128 : Word8 >= x) #177
Comments
To give a concrete example, this Motoko func upperMask(a : Word8) : Bool = a & (0x80 : Word8) != (0 : Word8); compiles currently to this Wasm: (func $upperMask (type 5) (param $clos i32) (param $a i32) (result i32)
local.get $a
i32.const -2147483648 // 0x80000000
i32.and
i32.eqz
i32.eqz
i32.const 1
i32.shl) We could have (unverified) (func $upperMask (type 5) (param $clos i32) (param $a i32) (result i32)
local.get $a
i32.const 31
i32.shr
i32.const 1
i32.shl) |
Something like func upperBitGe(a : Word8) : Bool = a >= (0x80 : Word8);
func upperBitGt(a : Word8) : Bool = a > (0x7F : Word8); with (func $upperBitGt (type 5) (param $clos i32) (param $a i32) (result i32)
local.get $a
i32.const 2130706432 // 0x7f000000
i32.gt_u
i32.const 1
i32.shl)
(func $upperBitGe (type 5) (param $clos i32) (param $a i32) (result i32)
local.get $a
i32.const -2147483648 // 0x80000000
i32.ge_u
i32.const 1
i32.shl) unless we insert redundant hint instructions into the Wasm: (func $upperBitGt (type 5) (param $clos i32) (param $a i32) (result i32)
local.get $a
hint (i32.const 0xFF000000; i32.and) -- adds some info about the TOS: 24 lower bits are zeroed
i32.const 2130706432 // 0x7f000000
i32.gt_u
i32.const 1
i32.shl) With such hints we can perform the I envision the |
|
Also note that after a 1-bit masking |
See |
Another reduction path: block (result i32) ;; label = @1
i32.const 1
end
drop
|
to
bitShiftRight 31
These happen frequently in the
Random.mo
code.Note: this issue should be in
motoko
proper, but it ended up here by mistake. I cannot move it there.The text was updated successfully, but these errors were encountered: