llvm.org GIT mirror llvm / 9506c93
When factoring multiply expressions across adds, factor both positive and negative forms of constants together. This allows us to compile: int foo(int x, int y) { return (x-y) + (x-y) + (x-y); } into: _foo: ## @foo subl %esi, %edi leal (%rdi,%rdi,2), %eax ret instead of (where the 3 and -3 were not factored): _foo: imull $-3, 8(%esp), %ecx imull $3, 4(%esp), %eax addl %ecx, %eax ret this started out as: movl 12(%ebp), %ecx imull $3, 8(%ebp), %eax subl %ecx, %eax subl %ecx, %eax subl %ecx, %eax ret This comes from PR5359. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92381 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 9 years ago
2 changed file(s) with 64 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
509509 }
510510
511511 // Scan backwards and forwards among values with the same rank as element i to
512 // see if X exists. If X does not exist, return i.
512 // see if X exists. If X does not exist, return i. This is useful when
513 // scanning for 'x' when we see '-x' because they both get the same rank.
513514 static unsigned FindInOperandList(SmallVectorImpl &Ops, unsigned i,
514515 Value *X) {
515516 unsigned XRank = Ops[i].Rank;
517518 for (unsigned j = i+1; j != e && Ops[j].Rank == XRank; ++j)
518519 if (Ops[j].Op == X)
519520 return j;
520 // Scan backwards
521 // Scan backwards.
521522 for (unsigned j = i-1; j != ~0U && Ops[j].Rank == XRank; --j)
522523 if (Ops[j].Op == X)
523524 return j;
546547 LinearizeExprTree(BO, Factors);
547548
548549 bool FoundFactor = false;
549 for (unsigned i = 0, e = Factors.size(); i != e; ++i)
550 bool NeedsNegate = false;
551 for (unsigned i = 0, e = Factors.size(); i != e; ++i) {
550552 if (Factors[i].Op == Factor) {
551553 FoundFactor = true;
552554 Factors.erase(Factors.begin()+i);
553555 break;
554556 }
557
558 // If this is a negative version of this factor, remove it.
559 if (ConstantInt *FC1 = dyn_cast(Factor))
560 if (ConstantInt *FC2 = dyn_cast(Factors[i].Op))
561 if (FC1->getValue() == -FC2->getValue()) {
562 FoundFactor = NeedsNegate = true;
563 Factors.erase(Factors.begin()+i);
564 break;
565 }
566 }
567
555568 if (!FoundFactor) {
556569 // Make sure to restore the operands to the expression tree.
557570 RewriteExprTree(BO, Factors);
558571 return 0;
559572 }
573
574 BasicBlock::iterator InsertPt = BO; ++InsertPt;
560575
561576 // If this was just a single multiply, remove the multiply and return the only
562577 // remaining operand.
563578 if (Factors.size() == 1) {
564579 ValueRankMap.erase(BO);
565580 BO->eraseFromParent();
566 return Factors[0].Op;
567 }
568
569 RewriteExprTree(BO, Factors);
570 return BO;
581 V = Factors[0].Op;
582 } else {
583 RewriteExprTree(BO, Factors);
584 V = BO;
585 }
586
587 if (NeedsNegate)
588 V = BinaryOperator::CreateNeg(V, "neg", InsertPt);
589
590 return V;
571591 }
572592
573593 /// FindSingleUseMultiplyFactors - If V is a single-use multiply, recursively
644664 // Scan the operand lists looking for X and -X pairs. If we find any, we
645665 // can simplify the expression. X+-X == 0. While we're at it, scan for any
646666 // duplicates. We want to canonicalize Y+Y+Y+Z -> 3*Y+Z.
667 //
668 // TODO: We could handle "X + ~X" -> "-1" if we wanted, since "-X = ~X+1".
669 //
647670 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
648671 Value *TheOp = Ops[i].Op;
649672 // Check to see if we've seen this operand before. If so, we factor all
729752 assert(Factors.size() > 1 && "Bad linearize!");
730753
731754 // Add one to FactorOccurrences for each unique factor in this op.
732 if (Factors.size() == 2) {
733 unsigned Occ = ++FactorOccurrences[Factors[0]];
734 if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factors[0]; }
735 if (Factors[0] != Factors[1]) { // Don't double count A*A.
736 Occ = ++FactorOccurrences[Factors[1]];
737 if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factors[1]; }
738 }
739 } else {
740 SmallPtrSet Duplicates;
741 for (unsigned i = 0, e = Factors.size(); i != e; ++i) {
742 if (!Duplicates.insert(Factors[i])) continue;
743
744 unsigned Occ = ++FactorOccurrences[Factors[i]];
745 if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factors[i]; }
746 }
755 SmallPtrSet Duplicates;
756 for (unsigned i = 0, e = Factors.size(); i != e; ++i) {
757 Value *Factor = Factors[i];
758 if (!Duplicates.insert(Factor)) continue;
759
760 unsigned Occ = ++FactorOccurrences[Factor];
761 if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factor; }
762
763 // If Factor is a negative constant, add the negated value as a factor
764 // because we can percolate the negate out. Watch for minint, which
765 // cannot be positivified.
766 if (ConstantInt *CI = dyn_cast(Factor))
767 if (CI->getValue().isNegative() && !CI->getValue().isMinSignedValue()) {
768 Factor = ConstantInt::get(CI->getContext(), -CI->getValue());
769 assert(!Duplicates.count(Factor) &&
770 "Shouldn't have two constant factors, missed a canonicalize");
771
772 unsigned Occ = ++FactorOccurrences[Factor];
773 if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factor; }
774 }
747775 }
748776 }
749777
190190 ; CHECK-NEXT: ret i32
191191 }
192192
193 ; PR5359
194 define i32 @test14(i32 %X1, i32 %X2) {
195 %B = mul i32 %X1, 47 ; X1*47
196 %C = mul i32 %X2, -47 ; X2*-47
197 %D = add i32 %B, %C ; X1*47 + X2*-47 -> 47*(X1-X2)
198 ret i32 %D
199 ; CHECK: @test14
200 ; CHECK-NEXT: sub i32 %X1, %X2
201 ; CHECK-NEXT: mul i32 {{.*}}, 47
202 ; CHECK-NEXT: ret i32
203 }
204
205