From cdbffdd9b54532ab0ce3565d8e6fc79046977e1c Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Mon, 2 Jul 2018 14:43:40 +0000 Subject: [PATCH] [ValueTracking] allow undef elements when matching vector abs git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@336111 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ValueTracking.cpp | 59 +++++++++++++++++------------------- test/Transforms/InstCombine/abs-1.ll | 8 ++--- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index 16a3c7c6175..d212cf32c10 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -4605,39 +4605,34 @@ static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, } } - const APInt *C1; - if (match(CmpRHS, m_APInt(C1))) { - // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can - // match against either LHS or sext(LHS). - auto MaybeSExtLHS = m_CombineOr(m_Specific(CmpLHS), - m_SExt(m_Specific(CmpLHS))); - if ((match(TrueVal, MaybeSExtLHS) && - match(FalseVal, m_Neg(m_Specific(TrueVal)))) || - (match(FalseVal, MaybeSExtLHS) && - match(TrueVal, m_Neg(m_Specific(FalseVal))))) { - // Set LHS and RHS so that RHS is the negated operand of the select - if (match(TrueVal, MaybeSExtLHS)) { - LHS = TrueVal; - RHS = FalseVal; - } else { - LHS = FalseVal; - RHS = TrueVal; - } - - // ABS(X) ==> (X >s 0) ? X : -X and (X >s -1) ? X : -X - // NABS(X) ==> (X >s 0) ? -X : X and (X >s -1) ? -X : X - if (Pred == ICmpInst::ICMP_SGT && - (C1->isNullValue() || C1->isAllOnesValue())) { - return {(LHS == TrueVal) ? SPF_ABS : SPF_NABS, SPNB_NA, false}; - } - - // ABS(X) ==> (X (X isNullValue() || C1->isOneValue())) { - return {(LHS == FalseVal) ? SPF_ABS : SPF_NABS, SPNB_NA, false}; - } + // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can + // match against either LHS or sext(LHS). + auto MaybeSExtLHS = m_CombineOr(m_Specific(CmpLHS), + m_SExt(m_Specific(CmpLHS))); + if ((match(TrueVal, MaybeSExtLHS) && + match(FalseVal, m_Neg(m_Specific(TrueVal)))) || + (match(FalseVal, MaybeSExtLHS) && + match(TrueVal, m_Neg(m_Specific(FalseVal))))) { + // Set LHS and RHS so that RHS is the negated operand of the select + if (match(TrueVal, MaybeSExtLHS)) { + LHS = TrueVal; + RHS = FalseVal; + } else { + LHS = FalseVal; + RHS = TrueVal; } + + // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X) + // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X) + if (Pred == ICmpInst::ICMP_SGT && + match(CmpRHS, m_CombineOr(m_ZeroInt(), m_AllOnes()))) + return {(LHS == TrueVal) ? SPF_ABS : SPF_NABS, SPNB_NA, false}; + + // (X ABS(X) + // (X NABS(X) + if (Pred == ICmpInst::ICMP_SLT && + match(CmpRHS, m_CombineOr(m_ZeroInt(), m_One()))) + return {(LHS == FalseVal) ? SPF_ABS : SPF_NABS, SPNB_NA, false}; } if (CmpInst::isIntPredicate(Pred)) diff --git a/test/Transforms/InstCombine/abs-1.ll b/test/Transforms/InstCombine/abs-1.ll index 72124e30a57..17b06909ac8 100644 --- a/test/Transforms/InstCombine/abs-1.ll +++ b/test/Transforms/InstCombine/abs-1.ll @@ -77,9 +77,9 @@ define <2 x i8> @abs_canonical_2(<2 x i8> %x) { define <2 x i8> @abs_canonical_2_vec_undef_elts(<2 x i8> %x) { ; CHECK-LABEL: @abs_canonical_2_vec_undef_elts( -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i8> [[X:%.*]], +; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i8> [[X:%.*]], zeroinitializer ; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[X]] -; CHECK-NEXT: [[ABS:%.*]] = select <2 x i1> [[CMP]], <2 x i8> [[X]], <2 x i8> [[NEG]] +; CHECK-NEXT: [[ABS:%.*]] = select <2 x i1> [[CMP]], <2 x i8> [[NEG]], <2 x i8> [[X]] ; CHECK-NEXT: ret <2 x i8> [[ABS]] ; %cmp = icmp sgt <2 x i8> %x, @@ -165,9 +165,9 @@ define <2 x i8> @nabs_canonical_2(<2 x i8> %x) { define <2 x i8> @nabs_canonical_2_vec_undef_elts(<2 x i8> %x) { ; CHECK-LABEL: @nabs_canonical_2_vec_undef_elts( -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i8> [[X:%.*]], +; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i8> [[X:%.*]], zeroinitializer ; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[X]] -; CHECK-NEXT: [[ABS:%.*]] = select <2 x i1> [[CMP]], <2 x i8> [[NEG]], <2 x i8> [[X]] +; CHECK-NEXT: [[ABS:%.*]] = select <2 x i1> [[CMP]], <2 x i8> [[X]], <2 x i8> [[NEG]] ; CHECK-NEXT: ret <2 x i8> [[ABS]] ; %cmp = icmp sgt <2 x i8> %x, -- 2.11.0