binaryen
binaryen copied to clipboard
[proposal] getMinBits or getRangeBits?
Binaryen already has getMaxBits
which check max possible bits for expressions and allow some optimizations without pattern matching. I propose calculate also "min possible bits" which open opportunities for simplifications like this: (x << 1) & 1
==> 0
, popcnt((x >> 5) & 1)
==> (x >> 5) & 1
without pattern matching. It could be compute parallel with "max possible bits" in this case it probably better use new routine called getRangeBits
which calculate touple of minBits
and maxBits
. Calculation of min possible bits
also pretty trivial. We will use count trailing zeros
for constants and for multiplication just use minBits = min(maxBits(a), maxBits(b))
and etc.
[0 0 1 1 0 1 0 0]
|----------| maxBits
|----| minBits
|-----| range = maxBits - minBits
also we could calc other set operations like unions and etc
In general this sounds good, but the question is how useful this would be, and if it is worth adding.
I think just need build some prototype and test on real samples. But sometimes even a little peephole optimization can open the way for far more cascaded changes for next passes. I really hope it will be so this time) Otherwise, you can simply not accept such PR.