[llvm] [CodeGenPrepare] Folding `urem` with loop invariant value as remainder (PR #96625)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 25 05:18:06 PDT 2024


goldsteinn wrote:

The goal is the extend this in the near future. I was hoping to get feedback on the approach. Mostly is CGP a reasonable place to put this?

AFAICT LLVM doesn't have much infrastructure for generic strength reduction. The other places I considered where:
LoopStrengthReduction and IndVar.

LoopStrengthReduction, however, seems to only be geared towards strength reduction for the sake of address calculation, and IndVar seems geared towards reduction in the number of IVs, not the other way around.

My rationale putting it here are
1) its late in the pipeline so its arguably less canonical form won't interfere with other folds.
2) at this point we can be pretty certain the remainder isn't a power of 2 (or at least we won't be able to prove that it is)
3) this seemed like the only place in LLVM I could find where we both have access to loop constructs / IR but also are willing to essentially further complicate the code (other than vectorization stuff).

If this patch looks good (or at least is headed in the right direction) my plan is to add support for non-zero `Start`/`IncrLoopInvariant` and add special support for its usage in an `icmp` / and `(br (icmp...))` as both of those cases show up a lot in code and we can piggy back on the existing `icmp`/`br` to optimize the transform result.

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


More information about the llvm-commits mailing list