summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2005-07-07 20:40:38 +0000
committerChris Lattner <sabre@nondot.org>2005-07-07 20:40:38 +0000
commit4ed40f7c6f13bcb1ef0487392413c6d78dd6790d (patch)
tree519c9be33899e500f0be9b51e2046d6903b12383
parent2d0f468cd29a5817719fa63f3ff70c27ae46db66 (diff)
downloadbcm5719-llvm-4ed40f7c6f13bcb1ef0487392413c6d78dd6790d.tar.gz
bcm5719-llvm-4ed40f7c6f13bcb1ef0487392413c6d78dd6790d.zip
Fix a problem that instcombine would hit when dealing with unreachable code.
Because the instcombine has to scan the entire function when it starts up to begin with, we might as well do it in DFO so we can nuke unreachable code. This fixes: Transforms/InstCombine/2005-07-07-DeadPHILoop.ll llvm-svn: 22348
-rw-r--r--llvm/lib/Transforms/Scalar/InstructionCombining.cpp34
1 files changed, 29 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp
index 55dd09846e6..e2ef04ea1df 100644
--- a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -45,9 +45,9 @@
#include "llvm/Support/CallSite.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/GetElementPtrTypeIterator.h"
-#include "llvm/Support/InstIterator.h"
#include "llvm/Support/InstVisitor.h"
#include "llvm/Support/PatternMatch.h"
+#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/STLExtras.h"
#include <algorithm>
@@ -4400,7 +4400,8 @@ Instruction *InstCombiner::visitPHINode(PHINode &PN) {
// However, because we don't have dom info, we can't do a perfect job.
if (Instruction *I = dyn_cast<Instruction>(V)) {
// We know that the instruction dominates the PHI if there are no undef
- // values coming in.
+ // values coming in. If the instruction is defined in the entry block,
+ // and is not an invoke, we know it is ok.
if (I->getParent() != &I->getParent()->getParent()->front() ||
isa<InvokeInst>(I))
for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
@@ -5226,9 +5227,32 @@ bool InstCombiner::runOnFunction(Function &F) {
bool Changed = false;
TD = &getAnalysis<TargetData>();
- for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
- WorkList.push_back(&*i);
-
+ {
+ // Populate the worklist with the reachable instructions.
+ std::set<BasicBlock*> Visited;
+ for (df_ext_iterator<BasicBlock*> BB = df_ext_begin(&F.front(), Visited),
+ E = df_ext_end(&F.front(), Visited); BB != E; ++BB)
+ for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
+ WorkList.push_back(I);
+
+ // Do a quick scan over the function. If we find any blocks that are
+ // unreachable, remove any instructions inside of them. This prevents
+ // the instcombine code from having to deal with some bad special cases.
+ for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
+ if (!Visited.count(BB)) {
+ Instruction *Term = BB->getTerminator();
+ while (Term != BB->begin()) { // Remove instrs bottom-up
+ BasicBlock::iterator I = Term; --I;
+
+ DEBUG(std::cerr << "IC: DCE: " << *I);
+ ++NumDeadInst;
+
+ if (!I->use_empty())
+ I->replaceAllUsesWith(UndefValue::get(I->getType()));
+ I->eraseFromParent();
+ }
+ }
+ }
while (!WorkList.empty()) {
Instruction *I = WorkList.back(); // Get an instruction from the worklist
OpenPOWER on IntegriCloud