OSDN Git Service

[ValueTracking] allow undef elements when matching vector abs
authorSanjay Patel <spatel@rotateright.com>
Mon, 2 Jul 2018 14:43:40 +0000 (14:43 +0000)
committerSanjay Patel <spatel@rotateright.com>
Mon, 2 Jul 2018 14:43:40 +0000 (14:43 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@336111 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ValueTracking.cpp
test/Transforms/InstCombine/abs-1.ll

index 16a3c7c..d212cf3 100644 (file)
@@ -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))
index 72124e3..17b0690 100644 (file)
@@ -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>