aboutsummaryrefslogtreecommitdiff
path: root/lib/Transforms/InstCombine/InstructionCombining.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2011-01-16 05:28:59 +0000
committerChris Lattner <sabre@nondot.org>2011-01-16 05:28:59 +0000
commit192228edb1c08ca11da2df959072bcaa99eacd63 (patch)
treecdc7988daef8b656c8d663b006be839b60c8b18e /lib/Transforms/InstCombine/InstructionCombining.cpp
parentdf55fea807835bc23a2879fbf7f7a8bd53de8256 (diff)
enhance FoldOpIntoPhi in instcombine to try harder when a phi has
multiple uses. In some cases, all the uses are the same operation, so instcombine can go ahead and promote the phi. In the testcase this pushes an add out of the loop. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123568 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/InstCombine/InstructionCombining.cpp')
-rw-r--r--lib/Transforms/InstCombine/InstructionCombining.cpp22
1 files changed, 19 insertions, 3 deletions
diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp
index 6d05466b9b..23e76ee499 100644
--- a/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -519,9 +519,17 @@ Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) {
return 0;
// We normally only transform phis with a single use, unless we're trying
- // hard to make jump threading happen.
- if (!PN->hasOneUse())
- return 0;
+ // hard to make jump threading happen. However, if a PHI has multiple uses
+ // and they are all the same operation, we can fold *all* of the uses into the
+ // PHI.
+ if (!PN->hasOneUse()) {
+ // Walk the use list for the instruction, comparing them to I.
+ for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end();
+ UI != E; ++UI)
+ if (!I.isIdenticalTo(cast<Instruction>(*UI)))
+ return 0;
+ // Otherwise, we can replace *all* users with the new PHI we form.
+ }
// Check to see if all of the operands of the PHI are simple constants
// (constantint/constantfp/undef). If there is one non-constant value,
@@ -628,6 +636,14 @@ Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) {
NewPN->addIncoming(InV, PN->getIncomingBlock(i));
}
}
+
+ for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end();
+ UI != E; ) {
+ Instruction *User = cast<Instruction>(*UI++);
+ if (User == &I) continue;
+ ReplaceInstUsesWith(*User, NewPN);
+ EraseInstFromFunction(*User);
+ }
return ReplaceInstUsesWith(I, NewPN);
}