[llvm] [InstCombine] Infer exact division from a known-zero remainder (PR #221509)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 7 18:09:28 PDT 2026


ZERICO2005 wrote:

> You can match the pattern `icmp eq (rem X, Y), 0` in `findValuesAffectedByCondition`. Then use DomConditionCache (Q.DC) here. If you want to leverage assumption information (this pattern is rare), just iterate on AC again.
> 
> I am not sure whether this approach works. In many optimized cases, the rem and div instructions are likely to be placed in the same BB so the dominating condition is visited and added to the DomConditionCache after visiting the div instruction. If it doesn't work, we can set a limit on the number of users to inspect.

This does work. Switched to `DomConditionCache`/`AssumptionCache` lookups, with a new registration in `findValuesAffectedByCondition` for `icmp eq (rem X, Y), 0` so the `div` operands can be indexed.

This also fixed the TODO I had for `udiv_exact_assume_after_division` in `div-exact-from-rem.ll`, where the `rem` was done after `div`. `udiv_exact_assume_after_division_unreachable` however still needs a second `InstCombine` iteration for `exact` to be applied (since it leverages unreachable branches instead of assume).

https://github.com/llvm/llvm-project/pull/221509


More information about the llvm-commits mailing list