[llvm] [InstSimplify] Fold `X < Y ? (X + zext(X < Y)) <= Y : false` to `X < Y` (PR #118579)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 3 22:35:02 PST 2024


https://github.com/dtcxzyw commented:

Two main issues:
1. Logical and/or expressions are always canonicalized into bitwise versions. These pattern should be simplified by `simplifyAndInst/simplifyOrInst`: https://alive2.llvm.org/ce/z/kedJdF
2. We already implemented similar folds in `simplifyAndInst/simplifyOrInst`: https://github.com/llvm/llvm-project/blob/7be3326200ef382705d8e6b2d7dc5378af96b34a/llvm/lib/Analysis/InstructionSimplify.cpp#L2212-L2229. Therefore, we should support this pattern in `isImpliedCondICmps`.

Some suggestions:
1. Add more tests.
   + `X >= Y ? true : (X + zext(X >= Y)) > Y`: https://alive2.llvm.org/ce/z/QWa954
   + `X < Y & (X + zext(X < Y) <= Y)`
   + `X >= Y & (X + zext(X >= Y) > Y)`
2. Make sure that `isImpliedPoison((X + zext(X < Y)) <= Y, X < Y)` returns true
3. Add support for this pattern in `isImpliedCondICmps`.

As an alternative, we can replace the inner `X < Y` with `true` in InstCombine (via `replaceInInstruction`). Then InstCombine will fold this expression into `X < Y`:
```
X < Y ? (X +nuw zext(X < Y)) <= Y : false -->
X < Y ? (X +nuw 1) <= Y : false -->
X < Y ? X < Y : false -->
X < Y
```



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


More information about the llvm-commits mailing list