diff options
author | Sanjay Patel <spatel@rotateright.com> | 2018-07-02 14:43:40 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2018-07-02 14:43:40 +0000 |
commit | 284ba0c18fc0a8c946ff5198ad77e2142c2b8d51 (patch) | |
tree | 4c0938b41440d44826070ac47b0412054dccfb90 | |
parent | d414c6c131aa4df775bcfa7dbd5540f05b2d39c2 (diff) | |
download | bcm5719-llvm-284ba0c18fc0a8c946ff5198ad77e2142c2b8d51.tar.gz bcm5719-llvm-284ba0c18fc0a8c946ff5198ad77e2142c2b8d51.zip |
[ValueTracking] allow undef elements when matching vector abs
llvm-svn: 336111
-rw-r--r-- | llvm/lib/Analysis/ValueTracking.cpp | 59 | ||||
-rw-r--r-- | llvm/test/Transforms/InstCombine/abs-1.ll | 8 |
2 files changed, 31 insertions, 36 deletions
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 16a3c7c6175..d212cf32c10 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/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 <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_SLT && - (C1->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 <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_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/llvm/test/Transforms/InstCombine/abs-1.ll b/llvm/test/Transforms/InstCombine/abs-1.ll index 72124e30a57..17b06909ac8 100644 --- a/llvm/test/Transforms/InstCombine/abs-1.ll +++ b/llvm/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:%.*]], <i8 undef, i8 -1> +; 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, <i8 undef, i8 -1> @@ -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:%.*]], <i8 -1, i8 undef> +; 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, <i8 -1, i8 undef> |