This is related to the discussion in D140665. I was looking over the demanded bits implementation in IR and noticed that we just bail out of a potential fold if a udiv is exact:
DemandedBits source link
Also, see tests added with 7f0c11509e8f.
Then, I saw that we could lose a fold to poison if we zap the exact with that transform, so this patch tries to catch that as a preliminary step.
Alive2 proofs:
https://alive2.llvm.org/ce/z/zCjKM7
https://alive2.llvm.org/ce/z/-tz_RK (trailing zeros must be "less-than")
https://alive2.llvm.org/ce/z/c9CMsJ (general proof and specific example)
This is not correct, Q.CxtI can be an unrelated instruction. You need to pass the exact flag down into the function.