llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[InstSimplify] Simplify nonzero checks of `X - (X urem Y)`

Open ChrisLee02 opened this issue 3 months ago • 0 comments

InstSimplify can use the following equivalence:

icmp uge (sub X, (urem X, Y)), 1

is equivalent to:

icmp uge X, Y

For a defined urem, let Q = X udiv Y and R = X urem Y. Then:

X - R = Q * Y

X - R >= 1
<=> Q != 0
<=> X >= Y

This would allow InstSimplify to use dominating conditions involving X and Y to simplify the original comparison. For example, when X u>= Y is known on a path:

%rem = urem i64 %x, %y
%sub = sub i64 %x, %rem
%max = call i64 @llvm.umax.i64(i64 %sub, i64 1)

the umax can be simplified to %sub.

This was exposed by #214480. After (X urem Y) == X is folded to X u< Y, the false edge provides X u>= Y instead of the direct X != (X urem Y) relationship. InstSimplify currently cannot use the new condition to eliminate the umax above.

Alive2: https://alive2.llvm.org/ce/z/pYsL4L

ChrisLee02 avatar Aug 13 '26 14:08 ChrisLee02