[llvm] [InstCombine] Fold `X > C2 ? X + C1 : C2 + C1` to `max(X, C2) + C1` (PR #116888)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 19 17:14:08 PST 2024


================
@@ -1793,10 +1793,9 @@ define i32 @not_uadd_sat(i32 %x, i32 %y) {
 
 define i32 @not_uadd_sat2(i32 %x, i32 %y) {
 ; CHECK-LABEL: @not_uadd_sat2(
-; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], -2
-; CHECK-NEXT:    [[C:%.*]] = icmp ugt i32 [[X]], 1
-; CHECK-NEXT:    [[R:%.*]] = select i1 [[C]], i32 [[A]], i32 -1
-; CHECK-NEXT:    ret i32 [[R]]
+; CHECK-NEXT:    [[X:%.*]] = call i32 @llvm.umax.i32(i32 [[X1:%.*]], i32 1)
----------------
dtcxzyw wrote:

Missing fold: https://alive2.llvm.org/ce/z/LnDhkk
```
define i32 @src(i32 %x, i32 %y) {
  %a = add i32 %x, -2
  %c = icmp ult i32 %x, 2
  %r = select i1 %c, i32 -1, i32 %a
  ret i32 %r
}

define i32 @tgt(i32 %x) {
  %a = call i32 @llvm.umax(i32 %x, i32 1)
  %r = add i32 %a, -2
  ret i32 %r
}
```


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


More information about the llvm-commits mailing list