How to do demanded bits analysis in instcombine pass?

Context

The relationship between %temp and %or is as follows:

  %temp = zext i8 %y to i32
  %shl1 = shl i32 %x, 8
  %or = or i32 %shl1, %temp
  %shl2 = shl i32 %or, 25

SimplifyDemandedUseBits function in instcombine pass can transform %shl2 = shl i32 %or, 25 to %shl2 = shl i32 %temp, 25.

Question

Is there a way to know that above mentioned transformation can happen without actually modifying IR? I am modifying instcombine pass and want to take some decision based on this analysis.

I know there is a demanded-bits analysis pass but the constructor of the DemandedBits class is defined as:

  DemandedBits(Function &F, AssumptionCache &AC, DominatorTree &DT)

I don’t know how to initialize object for this class, as I only have the instruction I and InstCombinerImpl IC.

You should have the current Function, the AssumptionCache, and the DominatorTree available everywhere in instcombine. For example, you can use InstCombiner::getDominatorTree() to get the domtree. But DemandedBits is not built in a way to make that sort of query cheap; it analyzes the whole function upfront.

Can you integrate your transform into the instcombine simplifydemandedbits transform? Most things related to demanded bits end up there. Not sure all the relevant information is available at that point, but if that works in your case, though. Or maybe you can reformulate your query in terms of computeKnownBits().

1 Like