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

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 21 19:18:58 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)
----------------
veera-sivarajan wrote:

Thank you for the feedback.

While this fold is similar to the one in this PR, I don't think this PR can fold the provided example because: 

In the example, `C1 = 2`, `C2 = -2`, `C3 = 1` and `BOp = add`.

This PR can fold only when `C3 == BOp C1 C2` and this does not hold true for the example. 


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


More information about the llvm-commits mailing list