forked from OSchip/llvm-project
[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. llvm-svn: 354384
This commit is contained in:
parent
9845da93e1
commit
dcb93c0dda
|
@ -680,10 +680,22 @@ static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
|
|||
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(ICmpInst *Cmp, Value *TVal, Value *FVal,
|
|||
|
||||
// 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(ICmpInst *Cmp, Value *TVal, Value *FVal,
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -771,6 +771,36 @@ define i32 @uadd_sat_commute_select_ugt_commute_add(i32 %xp, i32 %y) {
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue