llvm.org GIT mirror llvm / 1a72ab0
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@345042 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjay Patel 10 months ago
3 changed file(s) with 19 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
204204 for (unsigned i = 0, e = I->getNumOperands(); i != e && Rank != MaxRank; ++i)
205205 Rank = std::max(Rank, getRank(I->getOperand(i)));
206206
207 // If this is a not or neg instruction, do not count it for rank. This
207 // If this is a 'not' or 'neg' instruction, do not count it for rank. This
208208 // assures us that X and ~X will have the same rank.
209 if (!BinaryOperator::isNot(I) && !BinaryOperator::isNeg(I) &&
209 if (!match(I, m_Not(m_Value())) && !match(I, m_Neg(m_Value())) &&
210210 !BinaryOperator::isFNeg(I))
211211 ++Rank;
212212
573573 // If this is a multiply expression, turn any internal negations into
574574 // multiplies by -1 so they can be reassociated.
575575 if (BinaryOperator *BO = dyn_cast(Op))
576 if ((Opcode == Instruction::Mul && BinaryOperator::isNeg(BO)) ||
576 if ((Opcode == Instruction::Mul && match(BO, m_Neg(m_Value()))) ||
577577 (Opcode == Instruction::FMul && BinaryOperator::isFNeg(BO))) {
578578 LLVM_DEBUG(dbgs()
579579 << "MORPH LEAF: " << *Op << " (" << Weight << ") TO ");
854854 // Okay, we need to materialize a negated version of V with an instruction.
855855 // Scan the use lists of V to see if we have one already.
856856 for (User *U : V->users()) {
857 if (!BinaryOperator::isNeg(U) && !BinaryOperator::isFNeg(U))
857 if (!match(U, m_Neg(m_Value())) && !BinaryOperator::isFNeg(U))
858858 continue;
859859
860860 // We found one! Now we have to make sure that the definition dominates
899899 /// Return true if we should break up this subtract of X-Y into (X + -Y).
900900 static bool ShouldBreakUpSubtract(Instruction *Sub) {
901901 // If this is a negation, we can't split it up!
902 if (BinaryOperator::isNeg(Sub) || BinaryOperator::isFNeg(Sub))
902 if (match(Sub, m_Neg(m_Value())) || BinaryOperator::isFNeg(Sub))
903903 return false;
904904
905905 // Don't breakup X - undef.
11131113 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
11141114 // First, check for X and ~X in the operand list.
11151115 assert(i < Ops.size());
1116 if (BinaryOperator::isNot(Ops[i].Op)) { // Cannot occur for ^.
1117 Value *X = BinaryOperator::getNotArgument(Ops[i].Op);
1116 Value *X;
1117 if (match(Ops[i].Op, m_Not(m_Value(X)))) { // Cannot occur for ^.
11181118 unsigned FoundX = FindInOperandList(Ops, i, X);
11191119 if (FoundX != i) {
11201120 if (Opcode == Instruction::And) // ...&X&~X = 0
14601460 }
14611461
14621462 // Check for X and -X or X and ~X in the operand list.
1463 if (!BinaryOperator::isNeg(TheOp) && !BinaryOperator::isFNeg(TheOp) &&
1464 !BinaryOperator::isNot(TheOp))
1463 Value *X;
1464 if (!match(TheOp, m_Neg(m_Value(X))) && !match(TheOp, m_Not(m_Value(X))) &&
1465 !BinaryOperator::isFNeg(TheOp))
14651466 continue;
14661467
1467 Value *X = nullptr;
1468 if (BinaryOperator::isNeg(TheOp) || BinaryOperator::isFNeg(TheOp))
1469 X = BinaryOperator::getNegArgument(TheOp);
1470 else if (BinaryOperator::isNot(TheOp))
1471 X = BinaryOperator::getNotArgument(TheOp);
1468 if (BinaryOperator::isFNeg(TheOp))
1469 X = BinaryOperator::getFNegArgument(TheOp);
14721470
14731471 unsigned FoundX = FindInOperandList(Ops, i, X);
14741472 if (FoundX == i)
14761474
14771475 // Remove X and -X from the operand list.
14781476 if (Ops.size() == 2 &&
1479 (BinaryOperator::isNeg(TheOp) || BinaryOperator::isFNeg(TheOp)))
1477 (match(TheOp, m_Neg(m_Value())) || BinaryOperator::isFNeg(TheOp)))
14801478 return Constant::getNullValue(X->getType());
14811479
14821480 // Remove X and ~X from the operand list.
1483 if (Ops.size() == 2 && BinaryOperator::isNot(TheOp))
1481 if (Ops.size() == 2 && match(TheOp, m_Not(m_Value())))
14841482 return Constant::getAllOnesValue(X->getType());
14851483
14861484 Ops.erase(Ops.begin()+i);
14941492 e -= 2; // Removed two elements.
14951493
14961494 // if X and ~X we append -1 to the operand list.
1497 if (BinaryOperator::isNot(TheOp)) {
1495 if (match(TheOp, m_Not(m_Value()))) {
14981496 Value *V = Constant::getAllOnesValue(X->getType());
14991497 Ops.insert(Ops.end(), ValueEntry(getRank(V), V));
15001498 e += 1;
20582056 RedoInsts.insert(I);
20592057 MadeChange = true;
20602058 I = NI;
2061 } else if (BinaryOperator::isNeg(I)) {
2059 } else if (match(I, m_Neg(m_Value()))) {
20622060 // Otherwise, this is a negation. See if the operand is a multiply tree
20632061 // and if this is not an inner node of a multiply tree.
20642062 if (isReassociableOp(I->getOperand(1), Instruction::Mul) &&
1313
1414 define <2 x i32> @not_op_vec_undef(<2 x i32> %a, <2 x i32> %b) {
1515 ; CHECK-LABEL: @not_op_vec_undef(
16 ; CHECK-NEXT: [[T2:%.*]] = and <2 x i32> [[B:%.*]], [[A:%.*]]
17 ; CHECK-NEXT: [[T4:%.*]] = xor <2 x i32> [[A]],
18 ; CHECK-NEXT: [[T5:%.*]] = and <2 x i32> [[T2]], [[T4]]
19 ; CHECK-NEXT: ret <2 x i32> [[T5]]
16 ; CHECK-NEXT: ret <2 x i32> zeroinitializer
2017 ;
2118 %t2 = and <2 x i32> %b, %a
2219 %t4 = xor <2 x i32> %a,
3232
3333 define <2 x i32> @negate_vec_undefs(<2 x i32> %a, <2 x i32> %b, <2 x i32> %z) {
3434 ; CHECK-LABEL: @negate_vec_undefs(
35 ; CHECK-NEXT: [[TMP1:%.*]] = mul <2 x i32> [[Z:%.*]],
36 ; CHECK-NEXT: [[E:%.*]] = mul <2 x i32> [[TMP1]], [[A:%.*]]
37 ; CHECK-NEXT: [[F:%.*]] = sub <2 x i32> , [[E]]
35 ; CHECK-NEXT: [[E:%.*]] = mul <2 x i32> [[A:%.*]],
36 ; CHECK-NEXT: [[F:%.*]] = mul <2 x i32> [[E]], [[Z:%.*]]
3837 ; CHECK-NEXT: ret <2 x i32> [[F]]
3938 ;
4039 %d = mul <2 x i32> %z,