aboutsummaryrefslogtreecommitdiff
path: root/lib/Transforms/Scalar/Reassociate.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2006-03-14 07:11:11 +0000
committerChris Lattner <sabre@nondot.org>2006-03-14 07:11:11 +0000
commit895b392269cad07c34d59110d68dc86708c53adb (patch)
tree39bd8a79780bf59251f9bc59adb45f4d967cd4e3 /lib/Transforms/Scalar/Reassociate.cpp
parentad5a3a0265f4dfe175cc598913b025b69d132afb (diff)
extract some code into a method, no functionality change
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26755 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Scalar/Reassociate.cpp')
-rw-r--r--lib/Transforms/Scalar/Reassociate.cpp106
1 files changed, 56 insertions, 50 deletions
diff --git a/lib/Transforms/Scalar/Reassociate.cpp b/lib/Transforms/Scalar/Reassociate.cpp
index 4a484c3584..dc44ad593f 100644
--- a/lib/Transforms/Scalar/Reassociate.cpp
+++ b/lib/Transforms/Scalar/Reassociate.cpp
@@ -78,6 +78,7 @@ namespace {
private:
void BuildRankMap(Function &F);
unsigned getRank(Value *V);
+ void ReassociateExpression(BinaryOperator *I);
void RewriteExprTree(BinaryOperator *I, unsigned Idx,
std::vector<ValueEntry> &Ops);
Value *OptimizeExpression(BinaryOperator *I, std::vector<ValueEntry> &Ops);
@@ -752,57 +753,62 @@ void Reassociate::ReassociateBB(BasicBlock *BB) {
cast<Instruction>(I->use_back())->getOpcode() == Instruction::Sub)
continue;
- // First, walk the expression tree, linearizing the tree, collecting
- std::vector<ValueEntry> Ops;
- LinearizeExprTree(I, Ops);
-
- DEBUG(std::cerr << "RAIn:\t"; PrintOps(I, Ops);
- std::cerr << "\n");
-
- // Now that we have linearized the tree to a list and have gathered all of
- // the operands and their ranks, sort the operands by their rank. Use a
- // stable_sort so that values with equal ranks will have their relative
- // positions maintained (and so the compiler is deterministic). Note that
- // this sorts so that the highest ranking values end up at the beginning of
- // the vector.
- std::stable_sort(Ops.begin(), Ops.end());
-
- // OptimizeExpression - Now that we have the expression tree in a convenient
- // sorted form, optimize it globally if possible.
- if (Value *V = OptimizeExpression(I, Ops)) {
- // This expression tree simplified to something that isn't a tree,
- // eliminate it.
- DEBUG(std::cerr << "Reassoc to scalar: " << *V << "\n");
- I->replaceAllUsesWith(V);
- RemoveDeadBinaryOp(I);
- continue;
- }
-
- // We want to sink immediates as deeply as possible except in the case where
- // this is a multiply tree used only by an add, and the immediate is a -1.
- // In this case we reassociate to put the negation on the outside so that we
- // can fold the negation into the add: (-X)*Y + Z -> Z-X*Y
- if (I->getOpcode() == Instruction::Mul && I->hasOneUse() &&
- cast<Instruction>(I->use_back())->getOpcode() == Instruction::Add &&
- isa<ConstantInt>(Ops.back().Op) &&
- cast<ConstantInt>(Ops.back().Op)->isAllOnesValue()) {
- Ops.insert(Ops.begin(), Ops.back());
- Ops.pop_back();
- }
-
- DEBUG(std::cerr << "RAOut:\t"; PrintOps(I, Ops);
- std::cerr << "\n");
+ ReassociateExpression(I);
+ }
+}
- if (Ops.size() == 1) {
- // This expression tree simplified to something that isn't a tree,
- // eliminate it.
- I->replaceAllUsesWith(Ops[0].Op);
- RemoveDeadBinaryOp(I);
- } else {
- // Now that we ordered and optimized the expressions, splat them back into
- // the expression tree, removing any unneeded nodes.
- RewriteExprTree(I, 0, Ops);
- }
+void Reassociate::ReassociateExpression(BinaryOperator *I) {
+
+ // First, walk the expression tree, linearizing the tree, collecting
+ std::vector<ValueEntry> Ops;
+ LinearizeExprTree(I, Ops);
+
+ DEBUG(std::cerr << "RAIn:\t"; PrintOps(I, Ops);
+ std::cerr << "\n");
+
+ // Now that we have linearized the tree to a list and have gathered all of
+ // the operands and their ranks, sort the operands by their rank. Use a
+ // stable_sort so that values with equal ranks will have their relative
+ // positions maintained (and so the compiler is deterministic). Note that
+ // this sorts so that the highest ranking values end up at the beginning of
+ // the vector.
+ std::stable_sort(Ops.begin(), Ops.end());
+
+ // OptimizeExpression - Now that we have the expression tree in a convenient
+ // sorted form, optimize it globally if possible.
+ if (Value *V = OptimizeExpression(I, Ops)) {
+ // This expression tree simplified to something that isn't a tree,
+ // eliminate it.
+ DEBUG(std::cerr << "Reassoc to scalar: " << *V << "\n");
+ I->replaceAllUsesWith(V);
+ RemoveDeadBinaryOp(I);
+ return;
+ }
+
+ // We want to sink immediates as deeply as possible except in the case where
+ // this is a multiply tree used only by an add, and the immediate is a -1.
+ // In this case we reassociate to put the negation on the outside so that we
+ // can fold the negation into the add: (-X)*Y + Z -> Z-X*Y
+ if (I->getOpcode() == Instruction::Mul && I->hasOneUse() &&
+ cast<Instruction>(I->use_back())->getOpcode() == Instruction::Add &&
+ isa<ConstantInt>(Ops.back().Op) &&
+ cast<ConstantInt>(Ops.back().Op)->isAllOnesValue()) {
+ Ops.insert(Ops.begin(), Ops.back());
+ Ops.pop_back();
+ }
+
+ DEBUG(std::cerr << "RAOut:\t"; PrintOps(I, Ops);
+ std::cerr << "\n");
+
+ if (Ops.size() == 1) {
+ // This expression tree simplified to something that isn't a tree,
+ // eliminate it.
+ I->replaceAllUsesWith(Ops[0].Op);
+ RemoveDeadBinaryOp(I);
+ } else {
+ // Now that we ordered and optimized the expressions, splat them back into
+ // the expression tree, removing any unneeded nodes.
+ RewriteExprTree(I, 0, Ops);
}
}