I am trying to understand the demanded-bits pass. The example in the source code (below) seems quite explicit. In the example, only 16 bits from a 32 bit variable in IR are demanded and therefore the variable is truncated to a 16 bit variable.
%1 = add i32 %x, %y
%2 = trunc i32 %1 to i16
However I was wondering if for example, the addition demanded, say 18 bits, would this pass generate a i18 integer variable? I have not done the experiment, but I would like to know beforehand if this is something that this pass can do.
The demanded bits pass is an analysis pass. By itself it won’t make any changes to the IR. It is used by other transformation passes that care about this type of information.
The main place I know where we look to see if we can perform an operation truncated is in InstCombine. See canEvaluateTruncated in InstCombineCasts.cpp. But I believe we have to be able to completely remove the trunc before we will move it. So we’ll need to find something like an earlier sign extend or zero extend that extended from the same type we’d be truncatng to.
We also use DemandedBits for a similar purpose during loop vectorisation (see computeMinimalBitwidths in VectorUtils.cpp).