diff options
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | 45 |
1 files changed, 28 insertions, 17 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index 3ee418feb0e..9fb77abad66 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -680,25 +680,46 @@ static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal, if (!Cmp->hasOneUse()) return nullptr; - // Canonicalize to 'ULT' to simplify matching below. Value *Cmp0 = Cmp->getOperand(0); Value *Cmp1 = Cmp->getOperand(1); + + // Match unsigned saturated add with constant. + Value *X; + const APInt *C, *CmpC; + if (match(TVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 && + match(Cmp1, m_APInt(CmpC)) && *CmpC == ~*C) { + // Commute compare predicate and select operands. The backend is expecting + // this form (-1 is true value). If this changes, the backend must be + // updated too: + // (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); + } + + // Match unsigned saturated add of 2 variables with an unnecessary 'not'. + // There are 8 commuted variants. + // Canonicalize -1 (saturated result) to true value of the select. + if (match(FVal, m_AllOnes())) { + std::swap(TVal, FVal); + std::swap(Cmp0, Cmp1); + } + if (!match(TVal, m_AllOnes())) + return nullptr; + + // Canonicalize predicate to 'ULT'. ICmpInst::Predicate Pred = Cmp->getPredicate(); if (Pred == ICmpInst::ICMP_UGT) { Pred = ICmpInst::ICMP_ULT; std::swap(Cmp0, Cmp1); } - if (Pred != ICmpInst::ICMP_ULT) return nullptr; - // Match unsigned saturated add of 2 variables with an unnecessary 'not'. - // TODO: There are more variations of this pattern. - Value *X, *Y; - if (match(TVal, m_AllOnes()) && match(Cmp0, m_Not(m_Value(X))) && + Value *Y; + if (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 - // the canonical pattern match form for uadd.with.overflow and eliminates a + // a canonical pattern match form for uadd.with.overflow and eliminates a // use of the 'not' op: // (~X u< Y) ? -1 : (X + Y) --> ((X + Y) u< Y) ? -1 : (X + Y) // (~X u< Y) ? -1 : (Y + X) --> ((Y + X) u< Y) ? -1 : (Y + X) @@ -706,16 +727,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; } |