[llvm] r345042 - [Reassociate] replace fake binop queries with 'match' API

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 23 08:55:06 PDT 2018


Author: spatel
Date: Tue Oct 23 08:55:06 2018
New Revision: 345042

URL: http://llvm.org/viewvc/llvm-project?rev=345042&view=rev
Log:
[Reassociate] replace fake binop queries with 'match' API

We need to update this code before introducing an 'fneg' instruction in IR,
so we might as well kill off the integer neg/not queries too.

This is no-functional-change-intended for scalar code and most vector code. 
For vectors, we can see that the 'match' API allows for undef elements in 
constants, so we optimize those cases better.

Ideally, there would be a test for each code diff, but I don't see evidence
of that for the existing code, so I didn't try very hard to come up with new 
vector tests for each code change.

Differential Revision: https://reviews.llvm.org/D53533

Modified:
    llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp
    llvm/trunk/test/Transforms/Reassociate/inverses.ll
    llvm/trunk/test/Transforms/Reassociate/negation.ll

Modified: llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp?rev=345042&r1=345041&r2=345042&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp Tue Oct 23 08:55:06 2018
@@ -205,9 +205,9 @@ unsigned ReassociatePass::getRank(Value
   for (unsigned i = 0, e = I->getNumOperands(); i != e && Rank != MaxRank; ++i)
     Rank = std::max(Rank, getRank(I->getOperand(i)));
 
-  // If this is a not or neg instruction, do not count it for rank.  This
+  // If this is a 'not' or 'neg' instruction, do not count it for rank. This
   // assures us that X and ~X will have the same rank.
-  if (!BinaryOperator::isNot(I) && !BinaryOperator::isNeg(I) &&
+  if (!match(I, m_Not(m_Value())) && !match(I, m_Neg(m_Value())) &&
       !BinaryOperator::isFNeg(I))
     ++Rank;
 
@@ -574,7 +574,7 @@ static bool LinearizeExprTree(BinaryOper
       // If this is a multiply expression, turn any internal negations into
       // multiplies by -1 so they can be reassociated.
       if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op))
-        if ((Opcode == Instruction::Mul && BinaryOperator::isNeg(BO)) ||
+        if ((Opcode == Instruction::Mul && match(BO, m_Neg(m_Value()))) ||
             (Opcode == Instruction::FMul && BinaryOperator::isFNeg(BO))) {
           LLVM_DEBUG(dbgs()
                      << "MORPH LEAF: " << *Op << " (" << Weight << ") TO ");
@@ -855,7 +855,7 @@ static Value *NegateValue(Value *V, Inst
   // Okay, we need to materialize a negated version of V with an instruction.
   // Scan the use lists of V to see if we have one already.
   for (User *U : V->users()) {
-    if (!BinaryOperator::isNeg(U) && !BinaryOperator::isFNeg(U))
+    if (!match(U, m_Neg(m_Value())) && !BinaryOperator::isFNeg(U))
       continue;
 
     // We found one!  Now we have to make sure that the definition dominates
@@ -900,7 +900,7 @@ static Value *NegateValue(Value *V, Inst
 /// Return true if we should break up this subtract of X-Y into (X + -Y).
 static bool ShouldBreakUpSubtract(Instruction *Sub) {
   // If this is a negation, we can't split it up!
-  if (BinaryOperator::isNeg(Sub) || BinaryOperator::isFNeg(Sub))
+  if (match(Sub, m_Neg(m_Value())) || BinaryOperator::isFNeg(Sub))
     return false;
 
   // Don't breakup X - undef.
@@ -1114,8 +1114,8 @@ static Value *OptimizeAndOrXor(unsigned
   for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
     // First, check for X and ~X in the operand list.
     assert(i < Ops.size());
-    if (BinaryOperator::isNot(Ops[i].Op)) {    // Cannot occur for ^.
-      Value *X = BinaryOperator::getNotArgument(Ops[i].Op);
+    Value *X;
+    if (match(Ops[i].Op, m_Not(m_Value(X)))) {    // Cannot occur for ^.
       unsigned FoundX = FindInOperandList(Ops, i, X);
       if (FoundX != i) {
         if (Opcode == Instruction::And)   // ...&X&~X = 0
@@ -1461,15 +1461,13 @@ Value *ReassociatePass::OptimizeAdd(Inst
     }
 
     // Check for X and -X or X and ~X in the operand list.
-    if (!BinaryOperator::isNeg(TheOp) && !BinaryOperator::isFNeg(TheOp) &&
-        !BinaryOperator::isNot(TheOp))
+    Value *X;
+    if (!match(TheOp, m_Neg(m_Value(X))) && !match(TheOp, m_Not(m_Value(X))) &&
+        !BinaryOperator::isFNeg(TheOp))
       continue;
 
-    Value *X = nullptr;
-    if (BinaryOperator::isNeg(TheOp) || BinaryOperator::isFNeg(TheOp))
-      X = BinaryOperator::getNegArgument(TheOp);
-    else if (BinaryOperator::isNot(TheOp))
-      X = BinaryOperator::getNotArgument(TheOp);
+    if (BinaryOperator::isFNeg(TheOp))
+      X = BinaryOperator::getFNegArgument(TheOp);
 
     unsigned FoundX = FindInOperandList(Ops, i, X);
     if (FoundX == i)
@@ -1477,11 +1475,11 @@ Value *ReassociatePass::OptimizeAdd(Inst
 
     // Remove X and -X from the operand list.
     if (Ops.size() == 2 &&
-        (BinaryOperator::isNeg(TheOp) || BinaryOperator::isFNeg(TheOp)))
+        (match(TheOp, m_Neg(m_Value())) || BinaryOperator::isFNeg(TheOp)))
       return Constant::getNullValue(X->getType());
 
     // Remove X and ~X from the operand list.
-    if (Ops.size() == 2 && BinaryOperator::isNot(TheOp))
+    if (Ops.size() == 2 && match(TheOp, m_Not(m_Value())))
       return Constant::getAllOnesValue(X->getType());
 
     Ops.erase(Ops.begin()+i);
@@ -1495,7 +1493,7 @@ Value *ReassociatePass::OptimizeAdd(Inst
     e -= 2;  // Removed two elements.
 
     // if X and ~X we append -1 to the operand list.
-    if (BinaryOperator::isNot(TheOp)) {
+    if (match(TheOp, m_Not(m_Value()))) {
       Value *V = Constant::getAllOnesValue(X->getType());
       Ops.insert(Ops.end(), ValueEntry(getRank(V), V));
       e += 1;
@@ -2059,7 +2057,7 @@ void ReassociatePass::OptimizeInst(Instr
       RedoInsts.insert(I);
       MadeChange = true;
       I = NI;
-    } else if (BinaryOperator::isNeg(I)) {
+    } else if (match(I, m_Neg(m_Value()))) {
       // Otherwise, this is a negation.  See if the operand is a multiply tree
       // and if this is not an inner node of a multiply tree.
       if (isReassociableOp(I->getOperand(1), Instruction::Mul) &&

Modified: llvm/trunk/test/Transforms/Reassociate/inverses.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Reassociate/inverses.ll?rev=345042&r1=345041&r2=345042&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/Reassociate/inverses.ll (original)
+++ llvm/trunk/test/Transforms/Reassociate/inverses.ll Tue Oct 23 08:55:06 2018
@@ -14,10 +14,7 @@ define i32 @test1(i32 %a, i32 %b) {
 
 define <2 x i32> @not_op_vec_undef(<2 x i32> %a, <2 x i32> %b) {
 ; CHECK-LABEL: @not_op_vec_undef(
-; CHECK-NEXT:    [[T2:%.*]] = and <2 x i32> [[B:%.*]], [[A:%.*]]
-; CHECK-NEXT:    [[T4:%.*]] = xor <2 x i32> [[A]], <i32 -1, i32 undef>
-; CHECK-NEXT:    [[T5:%.*]] = and <2 x i32> [[T2]], [[T4]]
-; CHECK-NEXT:    ret <2 x i32> [[T5]]
+; CHECK-NEXT:    ret <2 x i32> zeroinitializer
 ;
   %t2 = and <2 x i32> %b, %a
   %t4 = xor <2 x i32> %a, <i32 -1, i32 undef>

Modified: llvm/trunk/test/Transforms/Reassociate/negation.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Reassociate/negation.ll?rev=345042&r1=345041&r2=345042&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/Reassociate/negation.ll (original)
+++ llvm/trunk/test/Transforms/Reassociate/negation.ll Tue Oct 23 08:55:06 2018
@@ -33,9 +33,8 @@ define i32 @test2(i32 %a, i32 %b, i32 %z
 
 define <2 x i32> @negate_vec_undefs(<2 x i32> %a, <2 x i32> %b, <2 x i32> %z) {
 ; CHECK-LABEL: @negate_vec_undefs(
-; CHECK-NEXT:    [[TMP1:%.*]] = mul <2 x i32> [[Z:%.*]], <i32 -40, i32 -40>
-; CHECK-NEXT:    [[E:%.*]] = mul <2 x i32> [[TMP1]], [[A:%.*]]
-; CHECK-NEXT:    [[F:%.*]] = sub <2 x i32> <i32 0, i32 undef>, [[E]]
+; CHECK-NEXT:    [[E:%.*]] = mul <2 x i32> [[A:%.*]], <i32 40, i32 40>
+; CHECK-NEXT:    [[F:%.*]] = mul <2 x i32> [[E]], [[Z:%.*]]
 ; CHECK-NEXT:    ret <2 x i32> [[F]]
 ;
   %d = mul <2 x i32> %z, <i32 40, i32 40>




More information about the llvm-commits mailing list