[llvm] [LICM][MustExec] Make must-exec logic for IV condition commutative (PR #93150)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat May 25 13:45:52 PDT 2024


nikic wrote:

> In the simplest case, we have a loop with a single exit, that we can prove is finite some other way (mustprogress etc.). Then we can just report N+1, because the overflow case would be undefined.

We already do this (the "exit controls finite loop" case).

> The given testcase is a bit more complicated: you have two exits. But still, we know the loop is finite. The backedge-taken count is something like `umin(zext(OtherExit), zext(N)+1)`. But that's equivalent to `trunc(umin(zext(OtherExit), zext(N)+1))`. And we can simplify that to `umin(OtherExit, umax(N, N+1))`.

Yes, this formulation using `umax(N, N+1)` to handle the potential overflow would work. But I think it would be quite hard to make use of this in this context. I don't think we would be able to directly report `umax(N, N+1)` as the exit count (after all, if N=-1 it doesn't actually exit at -1 backedges, as this would imply), and as the logic here is based around comparing exit counts to the backedge taken count, even if the BECount is truncated to i64 using this method, we'd still have to compare it to the i128 exit count, and would get the extension back in that way.

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


More information about the llvm-commits mailing list