[llvm] r324825 - [InstCombine] Add constant vector support for ~(C >> Y) --> ~C >> Y

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 10 13:46:09 PST 2018


Author: rksimon
Date: Sat Feb 10 13:46:09 2018
New Revision: 324825

URL: http://llvm.org/viewvc/llvm-project?rev=324825&view=rev
Log:
[InstCombine] Add constant vector support for ~(C >> Y) --> ~C >> Y

Includes adding m_NonNegative constant pattern matcher

Modified:
    llvm/trunk/include/llvm/IR/PatternMatch.h
    llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
    llvm/trunk/test/Transforms/InstCombine/vector-xor.ll

Modified: llvm/trunk/include/llvm/IR/PatternMatch.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/PatternMatch.h?rev=324825&r1=324824&r2=324825&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/PatternMatch.h (original)
+++ llvm/trunk/include/llvm/IR/PatternMatch.h Sat Feb 10 13:46:09 2018
@@ -347,6 +347,14 @@ struct is_negative {
 inline cst_pred_ty<is_negative> m_Negative() { return cst_pred_ty<is_negative>(); }
 inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
 
+struct is_nonnegative {
+  bool isValue(const APInt &C) { return C.isNonNegative(); }
+};
+
+/// \brief Match an integer or vector of nonnegative values.
+inline cst_pred_ty<is_nonnegative> m_NonNegative() { return cst_pred_ty<is_nonnegative>(); }
+inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
+
 struct is_power2 {
   bool isValue(const APInt &C) { return C.isPowerOf2(); }
 };

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp?rev=324825&r1=324824&r2=324825&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp Sat Feb 10 13:46:09 2018
@@ -2284,16 +2284,18 @@ Instruction *InstCombiner::visitXor(Bina
     // the 'not' by inverting the constant and using the opposite shift type.
     // Canonicalization rules ensure that only a negative constant uses 'ashr',
     // but we must check that in case that transform has not fired yet.
-    const APInt *C;
-    if (match(NotVal, m_AShr(m_APInt(C), m_Value(Y))) && C->isNegative()) {
+    Constant *C;
+    if (match(NotVal, m_AShr(m_Constant(C), m_Value(Y))) &&
+        match(C, m_Negative())) {
       // ~(C >>s Y) --> ~C >>u Y (when inverting the replicated sign bits)
-      Constant *NotC = ConstantInt::get(I.getType(), ~(*C));
+      Constant *NotC = ConstantExpr::getNot(C);
       return BinaryOperator::CreateLShr(NotC, Y);
     }
 
-    if (match(NotVal, m_LShr(m_APInt(C), m_Value(Y))) && C->isNonNegative()) {
+    if (match(NotVal, m_LShr(m_Constant(C), m_Value(Y))) &&
+        match(C, m_NonNegative())) {
       // ~(C >>u Y) --> ~C >>s Y (when inverting the replicated sign bits)
-      Constant *NotC = ConstantInt::get(I.getType(), ~(*C));
+      Constant *NotC = ConstantExpr::getNot(C);
       return BinaryOperator::CreateAShr(NotC, Y);
     }
   }

Modified: llvm/trunk/test/Transforms/InstCombine/vector-xor.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/vector-xor.ll?rev=324825&r1=324824&r2=324825&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/vector-xor.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/vector-xor.ll Sat Feb 10 13:46:09 2018
@@ -132,9 +132,8 @@ define <4 x i32> @test_v4i32_not_ashr_ne
 
 define <4 x i32> @test_v4i32_not_ashr_negative_const(<4 x i32> %a0) {
 ; CHECK-LABEL: @test_v4i32_not_ashr_negative_const(
-; CHECK-NEXT:    [[TMP1:%.*]] = ashr <4 x i32> <i32 -3, i32 -5, i32 -7, i32 -9>, [[A0:%.*]]
-; CHECK-NEXT:    [[TMP2:%.*]] = xor <4 x i32> [[TMP1]], <i32 -1, i32 -1, i32 -1, i32 -1>
-; CHECK-NEXT:    ret <4 x i32> [[TMP2]]
+; CHECK-NEXT:    [[TMP1:%.*]] = lshr <4 x i32> <i32 2, i32 4, i32 6, i32 8>, [[A0:%.*]]
+; CHECK-NEXT:    ret <4 x i32> [[TMP1]]
 ;
   %1 = ashr <4 x i32> <i32 -3, i32 -5, i32 -7, i32 -9>, %a0
   %2 = xor  <4 x i32> <i32 -1, i32 -1, i32 -1, i32 -1>, %1
@@ -166,9 +165,8 @@ define <4 x i32> @test_v4i32_not_lshr_no
 
 define <4 x i32> @test_v4i32_not_lshr_nonnegative_const(<4 x i32> %a0) {
 ; CHECK-LABEL: @test_v4i32_not_lshr_nonnegative_const(
-; CHECK-NEXT:    [[TMP1:%.*]] = lshr <4 x i32> <i32 3, i32 5, i32 7, i32 9>, [[A0:%.*]]
-; CHECK-NEXT:    [[TMP2:%.*]] = xor <4 x i32> [[TMP1]], <i32 -1, i32 -1, i32 -1, i32 -1>
-; CHECK-NEXT:    ret <4 x i32> [[TMP2]]
+; CHECK-NEXT:    [[TMP1:%.*]] = ashr <4 x i32> <i32 -4, i32 -6, i32 -8, i32 -10>, [[A0:%.*]]
+; CHECK-NEXT:    ret <4 x i32> [[TMP1]]
 ;
   %1 = lshr <4 x i32> <i32  3, i32  5, i32  7, i32  9>, %a0
   %2 = xor  <4 x i32> <i32 -1, i32 -1, i32 -1, i32 -1>, %1




More information about the llvm-commits mailing list