[llvm] [LSR] Reuse identical casts and instructions across fixups (PR #201839)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 11 08:16:43 PDT 2026


nikic wrote:

Okay, having looked closer at the motivating case, this way of fixing it is really unfortunate :(

What we have is three conditions that are really equivalent, but non-obviously so. The LSR rewrite bases these on a common IV, which makes their equivalence obvious. The changes here then do poor man's CSE and implied condition reasoning to fold one branch to false, which presumably gets eliminated later.

This is not good, because LSR is a late backend pass -- it's only job is to rewrite IV uses to be more efficient for the target. It's not supposed to do any simplification. Simplification should happen earlier so that other middle-end passes can still take advantage of it. It also depends on LSR picking a specific rewrite as the most profitable (and not, for example, deciding that keeping separate IVs is better).

I'm trying to figure out if there is some part earlier in the optimization pipeline where this would fit naturally. An annoying thing about this test case is that the exit is not latch dominating. The structure here is something like this:
```
// All the comparisons here are actually equivalent
loop {
  if (p1 == end1) {
    if (p2 == end2 && p3 == end3)
      break;
  }
}
```
We don't really try to reason about this kind of non-dominating exit.

Rewriting things to use a canonical IV in IndVars would solve this, but that would be a really big change.

cc @fhahn in case you have ideas.

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


More information about the llvm-commits mailing list