llvm.org GIT mirror llvm / 69e98e2
teach reassociate to factor x+x+x -> x*3. While I'm at it, fix RemoveDeadBinaryOp to actually do something. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92368 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 9 years ago
2 changed file(s) with 105 addition(s) and 35 deletion(s). Raw diff Collapse all Expand all
8787 private:
8888 void BuildRankMap(Function &F);
8989 unsigned getRank(Value *V);
90 void ReassociateExpression(BinaryOperator *I);
90 Value *ReassociateExpression(BinaryOperator *I);
9191 void RewriteExprTree(BinaryOperator *I, SmallVectorImpl &Ops,
9292 unsigned Idx = 0);
9393 Value *OptimizeExpression(BinaryOperator *I,
110110
111111 void Reassociate::RemoveDeadBinaryOp(Value *V) {
112112 Instruction *Op = dyn_cast(V);
113 if (!Op || !isa(Op) || !isa(Op) || !Op->use_empty())
113 if (!Op || !isa(Op) || !Op->use_empty())
114114 return;
115115
116116 Value *LHS = Op->getOperand(0), *RHS = Op->getOperand(1);
117
118 ValueRankMap.erase(Op);
119 Op->eraseFromParent();
117120 RemoveDeadBinaryOp(LHS);
118121 RemoveDeadBinaryOp(RHS);
119122 }
601604 /// is returned, otherwise the Ops list is mutated as necessary.
602605 Value *Reassociate::OptimizeAdd(Instruction *I,
603606 SmallVectorImpl &Ops) {
607 SmallPtrSet OperandsSeen;
608
609 Restart:
610 OperandsSeen.clear();
611
604612 // Scan the operand lists looking for X and -X pairs. If we find any, we
605 // can simplify the expression. X+-X == 0.
613 // can simplify the expression. X+-X == 0. While we're at it, scan for any
614 // duplicates. We want to canonicalize Y+Y+Y+Z -> 3*Y+Z.
606615 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
607 assert(i < Ops.size());
616 Value *TheOp = Ops[i].Op;
617 // Check to see if we've seen this operand before. If so, we factor all
618 // instances of the operand together.
619 if (!OperandsSeen.insert(TheOp)) {
620 // Rescan the list, removing all instances of this operand from the expr.
621 unsigned NumFound = 0;
622 for (unsigned j = 0, je = Ops.size(); j != je; ++j) {
623 if (Ops[j].Op != TheOp) continue;
624 ++NumFound;
625 Ops.erase(Ops.begin()+j);
626 --j; --je;
627 }
628
629 /*DEBUG*/(errs() << "\nFACTORING [" << NumFound << "]: " << *TheOp << '\n');
630 ++NumFactor;
631
632
633 // Insert a new multiply.
634 Value *Mul = ConstantInt::get(cast(I->getType()), NumFound);
635 Mul = BinaryOperator::CreateMul(TheOp, Mul, "factor", I);
636
637 // Now that we have inserted a multiply, optimize it. This allows us to
638 // handle cases that require multiple factoring steps, such as this:
639 // (X*2) + (X*2) + (X*2) -> (X*2)*3 -> X*6
640 Mul = ReassociateExpression(cast(Mul));
641
642 // If every add operand was a duplicate, return the multiply.
643 if (Ops.empty())
644 return Mul;
645
646 // Otherwise, we had some input that didn't have the dupe, such as
647 // "A + A + B" -> "A*2 + B". Add the new multiply to the list of
648 // things being added by this operation.
649 Ops.insert(Ops.begin(), ValueEntry(getRank(Mul), Mul));
650 goto Restart;
651 }
652
608653 // Check for X and -X in the operand list.
609 if (!BinaryOperator::isNeg(Ops[i].Op))
654 if (!BinaryOperator::isNeg(TheOp))
610655 continue;
611656
612 Value *X = BinaryOperator::getNegArgument(Ops[i].Op);
657 Value *X = BinaryOperator::getNegArgument(TheOp);
613658 unsigned FoundX = FindInOperandList(Ops, i, X);
614659 if (FoundX == i)
615660 continue;
638683
639684 // Keep track of each multiply we see, to avoid triggering on (X*4)+(X*4)
640685 // where they are actually the same multiply.
641 SmallPtrSet Multiplies;
642686 unsigned MaxOcc = 0;
643687 Value *MaxOccVal = 0;
644688 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
645689 BinaryOperator *BOp = dyn_cast(Ops[i].Op);
646690 if (BOp == 0 || BOp->getOpcode() != Instruction::Mul || !BOp->use_empty())
647691 continue;
648
649 // If we've already seen this multiply, don't revisit it.
650 if (!Multiplies.insert(BOp)) continue;
651692
652693 // Compute all of the factors of this added value.
653694 SmallVector Factors;
675716
676717 // If any factor occurred more than one time, we can pull it out.
677718 if (MaxOcc > 1) {
678 DEBUG(errs() << "\nFACTORING [" << MaxOcc << "]: " << *MaxOccVal << "\n");
719 DEBUG(errs() << "\nFACTORING [" << MaxOcc << "]: " << *MaxOccVal << '\n');
679720 ++NumFactor;
680721
681722 // Create a new instruction that uses the MaxOccVal twice. If we don't do
697738
698739 unsigned NumAddedValues = NewMulOps.size();
699740 Value *V = EmitAddTreeOfValues(I, NewMulOps);
700 Value *V2 = BinaryOperator::CreateMul(V, MaxOccVal, "tmp", I);
701
702 // Now that we have inserted V and its sole use, optimize it. This allows
703 // us to handle cases that require multiple factoring steps, such as this:
741
742 // Now that we have inserted the add tree, optimize it. This allows us to
743 // handle cases that require multiple factoring steps, such as this:
704744 // A*A*B + A*A*C --> A*(A*B+A*C) --> A*(A*(B+C))
705745 assert(NumAddedValues > 1 && "Each occurrence should contribute a value");
706 ReassociateExpression(cast(V));
746 V = ReassociateExpression(cast(V));
747
748 // Create the multiply.
749 Value *V2 = BinaryOperator::CreateMul(V, MaxOccVal, "tmp", I);
750
751 // FIXME: Should rerun 'ReassociateExpression' on the mul too??
707752
708753 // If every add operand included the factor (e.g. "A*B + A*C"), then the
709754 // entire result expression is just the multiply "A*(B+C)".
851896 }
852897 }
853898
854 void Reassociate::ReassociateExpression(BinaryOperator *I) {
855
856 // First, walk the expression tree, linearizing the tree, collecting
899 Value *Reassociate::ReassociateExpression(BinaryOperator *I) {
900
901 // First, walk the expression tree, linearizing the tree, collecting the
902 // operand information.
857903 SmallVector Ops;
858904 LinearizeExprTree(I, Ops);
859905
876922 I->replaceAllUsesWith(V);
877923 RemoveDeadBinaryOp(I);
878924 ++NumAnnihil;
879 return;
925 return V;
880926 }
881927
882928 // We want to sink immediates as deeply as possible except in the case where
898944 // eliminate it.
899945 I->replaceAllUsesWith(Ops[0].Op);
900946 RemoveDeadBinaryOp(I);
901 } else {
902 // Now that we ordered and optimized the expressions, splat them back into
903 // the expression tree, removing any unneeded nodes.
904 RewriteExprTree(I, Ops);
905 }
947 return Ops[0].Op;
948 }
949
950 // Now that we ordered and optimized the expressions, splat them back into
951 // the expression tree, removing any unneeded nodes.
952 RewriteExprTree(I, Ops);
953 return I;
906954 }
907955
908956
8787 }
8888
8989 define i32 @test6() {
90 %tmp.0 = load i32* @a ; [#uses=2]
91 %tmp.1 = load i32* @b ; [#uses=2]
90 %tmp.0 = load i32* @a
91 %tmp.1 = load i32* @b
9292 ; (a+b)
93 %tmp.2 = add i32 %tmp.0, %tmp.1 ; [#uses=1]
94 %tmp.4 = load i32* @c ; [#uses=2]
93 %tmp.2 = add i32 %tmp.0, %tmp.1
94 %tmp.4 = load i32* @c
9595 ; (a+b)+c
96 %tmp.5 = add i32 %tmp.2, %tmp.4 ; [#uses=1]
96 %tmp.5 = add i32 %tmp.2, %tmp.4
9797 ; (a+c)
98 %tmp.8 = add i32 %tmp.0, %tmp.4 ; [#uses=1]
98 %tmp.8 = add i32 %tmp.0, %tmp.4
9999 ; (a+c)+b
100 %tmp.11 = add i32 %tmp.8, %tmp.1 ; [#uses=1]
100 %tmp.11 = add i32 %tmp.8, %tmp.1
101101 ; X ^ X = 0
102 %RV = xor i32 %tmp.5, %tmp.11 ; [#uses=1]
102 %RV = xor i32 %tmp.5, %tmp.11
103103 ret i32 %RV
104104 ; CHECK: @test6
105105 ; CHECK: ret i32 0
107107
108108 ; This should be one add and two multiplies.
109109 define i32 @test7(i32 %A, i32 %B, i32 %C) {
110 ; A*A*B + A*C*A
110111 %aa = mul i32 %A, %A
111112 %aab = mul i32 %aa, %B
112113 %ac = mul i32 %A, %C
140141 %Z = add i32 %Y, %Y
141142 ret i32 %Z
142143 ; CHECK: @test9
143 ; CHECK-NEXT: %Z = mul i32 %X, 94
144 ; CHECK-NEXT: ret i32 %Z
144 ; CHECK-NEXT: mul i32 %X, 94
145 ; CHECK-NEXT: ret i32
145146 }
147
148 define i32 @test10(i32 %X) {
149 %Y = add i32 %X ,%X
150 %Z = add i32 %Y, %X
151 ret i32 %Z
152 ; CHECK: @test10
153 ; CHECK-NEXT: mul i32 %X, 3
154 ; CHECK-NEXT: ret i32
155 }
156
157 define i32 @test11(i32 %W) {
158 %X = mul i32 %W, 127
159 %Y = add i32 %X ,%X
160 %Z = add i32 %Y, %X
161 ret i32 %Z
162 ; CHECK: @test11
163 ; CHECK-NEXT: mul i32 %W, 381
164 ; CHECK-NEXT: ret i32
165 }
166
167