binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

[proposal] getMinBits or getRangeBits?

Open MaxGraey opened this issue 4 years ago • 2 comments

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

MaxGraey avatar Jun 06 '20 11:06 MaxGraey

In general this sounds good, but the question is how useful this would be, and if it is worth adding.

kripken avatar Jun 08 '20 23:06 kripken

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.

MaxGraey avatar Jun 09 '20 06:06 MaxGraey