[llvm] r336111 - [ValueTracking] allow undef elements when matching vector abs

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 2 07:43:40 PDT 2018


Author: spatel
Date: Mon Jul  2 07:43:40 2018
New Revision: 336111

URL: http://llvm.org/viewvc/llvm-project?rev=336111&view=rev
Log:
[ValueTracking] allow undef elements when matching vector abs

Modified:
    llvm/trunk/lib/Analysis/ValueTracking.cpp
    llvm/trunk/test/Transforms/InstCombine/abs-1.ll

Modified: llvm/trunk/lib/Analysis/ValueTracking.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ValueTracking.cpp?rev=336111&r1=336110&r2=336111&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ValueTracking.cpp (original)
+++ llvm/trunk/lib/Analysis/ValueTracking.cpp Mon Jul  2 07:43:40 2018
@@ -4605,39 +4605,34 @@ static SelectPatternResult matchSelectPa
     }
   }
 
-  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;
-      }
+  // 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};
-      }
+    // (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};
 
-      // 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};
-      }
-    }
+    // (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))

Modified: llvm/trunk/test/Transforms/InstCombine/abs-1.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/abs-1.ll?rev=336111&r1=336110&r2=336111&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/abs-1.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/abs-1.ll Mon Jul  2 07:43:40 2018
@@ -77,9 +77,9 @@ define <2 x i8> @abs_canonical_2(<2 x i8
 
 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 i
 
 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>




More information about the llvm-commits mailing list