diff options
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index 3ee418feb0e..0e9d6f05275 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -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; } |