[llvm] r354384 - [InstCombine] rearrange saturated add folds; NFC
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 19 13:46:13 PST 2019
Author: spatel
Date: Tue Feb 19 13:46:13 2019
New Revision: 354384
URL: http://llvm.org/viewvc/llvm-project?rev=354384&view=rev
Log:
[InstCombine] rearrange saturated add folds; NFC
This is no-functional-change-intended, but that was also
true when it was part of rL354276, and I managed to lose
2 predicates for the fold with constant...causing much bot
distress. So this time I'm adding a couple of negative tests
to avoid that.
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp
llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp?rev=354384&r1=354383&r2=354384&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp Tue Feb 19 13:46:13 2019
@@ -680,10 +680,22 @@ static Value *canonicalizeSaturatedAdd(I
if (!Cmp->hasOneUse())
return nullptr;
- // Canonicalize to 'ULT' to simplify matching below.
+ // Match unsigned saturated add with constant.
Value *Cmp0 = Cmp->getOperand(0);
Value *Cmp1 = Cmp->getOperand(1);
ICmpInst::Predicate Pred = Cmp->getPredicate();
+ Value *X;
+ const APInt *C, *CmpC;
+ if (Pred == ICmpInst::ICMP_ULT &&
+ match(TVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 &&
+ match(FVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) && *CmpC == ~*C) {
+ // Commute compare predicate and select operands:
+ // (X u< ~C) ? (X + C) : -1 --> (X u> ~C) ? -1 : (X + C)
+ Value *NewCmp = Builder.CreateICmp(ICmpInst::ICMP_UGT, X, Cmp1);
+ return Builder.CreateSelect(NewCmp, FVal, TVal);
+ }
+
+ // Canonicalize to 'ULT' to simplify matching below.
if (Pred == ICmpInst::ICMP_UGT) {
Pred = ICmpInst::ICMP_ULT;
std::swap(Cmp0, Cmp1);
@@ -694,7 +706,7 @@ static Value *canonicalizeSaturatedAdd(I
// Match unsigned saturated add of 2 variables with an unnecessary 'not'.
// TODO: There are more variations of this pattern.
- Value *X, *Y;
+ Value *Y;
if (match(TVal, m_AllOnes()) && match(Cmp0, m_Not(m_Value(X))) &&
match(FVal, m_c_Add(m_Specific(X), m_Value(Y))) && Y == Cmp1) {
// Change the comparison to use the sum (false value of the select). That is
@@ -706,16 +718,6 @@ static Value *canonicalizeSaturatedAdd(I
return Builder.CreateSelect(NewCmp, TVal, FVal);
}
- // Match unsigned saturated add with constant.
- const APInt *C, *CmpC;
- if (match(TVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 &&
- match(FVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) && *CmpC == ~*C) {
- // Commute compare predicate and select operands:
- // (X u< ~C) ? (X + C) : -1 --> (X u> ~C) ? -1 : (X + C)
- Value *NewCmp = Builder.CreateICmp(ICmpInst::ICMP_UGT, X, Cmp1);
- return Builder.CreateSelect(NewCmp, FVal, TVal);
- }
-
return nullptr;
}
Modified: llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll?rev=354384&r1=354383&r2=354384&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll Tue Feb 19 13:46:13 2019
@@ -771,6 +771,36 @@ define i32 @uadd_sat_commute_select_ugt_
ret i32 %r
}
+; Negative test - make sure we have a -1 in the select.
+
+define i32 @not_uadd_sat(i32 %x, i32 %y) {
+; CHECK-LABEL: @not_uadd_sat(
+; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], -2
+; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[X]], 1
+; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 [[A]], i32 [[Y:%.*]]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %a = add i32 %x, -2
+ %c = icmp ugt i32 %x, 1
+ %r = select i1 %c, i32 %a, i32 %y
+ ret i32 %r
+}
+
+; Negative test - make sure the predicate is 'ult'.
+
+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]]
+;
+ %a = add i32 %x, -2
+ %c = icmp ugt i32 %x, 1
+ %r = select i1 %c, i32 %a, i32 -1
+ ret i32 %r
+}
+
define i32 @uadd_sat_constant(i32 %x) {
; CHECK-LABEL: @uadd_sat_constant(
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], 42
More information about the llvm-commits
mailing list