blob: 19341ee72159ae6b224b4209270d2cef2134c312 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass is an extremely simple version of the SimplifyCFG pass. Its sole
11// job is to delete LLVM basic blocks that are not reachable from the entry
12// node. To do this, it performs a simple depth first traversal of the CFG,
13// then deletes any unvisited nodes.
14//
15// Note that this pass is really a hack. In particular, the instruction
16// selectors for various targets should just not generate code for unreachable
17// blocks. Until LLVM has a more systematic way of defining instruction
18// selectors, however, we cannot really expect them to handle additional
19// complexity.
20//
21//===----------------------------------------------------------------------===//
22
23#include "llvm/CodeGen/Passes.h"
24#include "llvm/Constant.h"
25#include "llvm/Instructions.h"
26#include "llvm/Function.h"
27#include "llvm/Pass.h"
28#include "llvm/Type.h"
Owen Andersonfb6914f2008-08-04 23:54:43 +000029#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/CFG.h"
31#include "llvm/Support/Compiler.h"
Owen Andersonfb6914f2008-08-04 23:54:43 +000032#include "llvm/Target/TargetInstrInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/ADT/DepthFirstIterator.h"
34using namespace llvm;
35
36namespace {
37 class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass {
38 virtual bool runOnFunction(Function &F);
39 public:
40 static char ID; // Pass identification, replacement for typeid
41 UnreachableBlockElim() : FunctionPass((intptr_t)&ID) {}
42 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043}
Dan Gohman089efff2008-05-13 00:00:25 +000044char UnreachableBlockElim::ID = 0;
45static RegisterPass<UnreachableBlockElim>
46X("unreachableblockelim", "Remove unreachable blocks from the CFG");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
48FunctionPass *llvm::createUnreachableBlockEliminationPass() {
49 return new UnreachableBlockElim();
50}
51
52bool UnreachableBlockElim::runOnFunction(Function &F) {
53 std::set<BasicBlock*> Reachable;
54
55 // Mark all reachable blocks.
56 for (df_ext_iterator<Function*> I = df_ext_begin(&F, Reachable),
57 E = df_ext_end(&F, Reachable); I != E; ++I)
58 /* Mark all reachable blocks */;
59
60 // Loop over all dead blocks, remembering them and deleting all instructions
61 // in them.
62 std::vector<BasicBlock*> DeadBlocks;
63 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
64 if (!Reachable.count(I)) {
65 BasicBlock *BB = I;
66 DeadBlocks.push_back(BB);
67 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
68 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
69 BB->getInstList().pop_front();
70 }
71 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
72 (*SI)->removePredecessor(BB);
73 BB->dropAllReferences();
74 }
75
Owen Andersonfb6914f2008-08-04 23:54:43 +000076 // Actually remove the blocks now.
77 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
78 DeadBlocks[i]->eraseFromParent();
79
80 return DeadBlocks.size();
81}
82
83
84namespace {
85 class VISIBILITY_HIDDEN UnreachableMachineBlockElim :
86 public MachineFunctionPass {
87 virtual bool runOnMachineFunction(MachineFunction &F);
Owen Andersonfb6914f2008-08-04 23:54:43 +000088
89 public:
90 static char ID; // Pass identification, replacement for typeid
91 UnreachableMachineBlockElim() : MachineFunctionPass((intptr_t)&ID) {}
92 };
93}
94char UnreachableMachineBlockElim::ID = 0;
95
96static RegisterPass<UnreachableMachineBlockElim>
97Y("unreachable-mbb-elimination",
98 "Remove unreachable machine basic blocks");
99
100const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y;
101
102bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
Owen Andersonfb6914f2008-08-04 23:54:43 +0000103 std::set<MachineBasicBlock*> Reachable;
104
105 // Mark all reachable blocks.
106 for (df_ext_iterator<MachineFunction*> I = df_ext_begin(&F, Reachable),
107 E = df_ext_end(&F, Reachable); I != E; ++I)
108 /* Mark all reachable blocks */;
109
110 // Loop over all dead blocks, remembering them and deleting all instructions
111 // in them.
112 std::vector<MachineBasicBlock*> DeadBlocks;
113 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I)
114 if (!Reachable.count(I)) {
115 MachineBasicBlock *BB = I;
116 DeadBlocks.push_back(BB);
117
118 while (BB->succ_begin() != BB->succ_end()) {
119 MachineBasicBlock* succ = *BB->succ_begin();
120
121 MachineBasicBlock::iterator start = succ->begin();
122 while (start != succ->end() &&
123 start->getOpcode() == TargetInstrInfo::PHI) {
124 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
125 if (start->getOperand(i).isMBB() &&
126 start->getOperand(i).getMBB() == BB) {
127 start->RemoveOperand(i);
128 start->RemoveOperand(i-1);
129 }
130
131 if (start->getNumOperands() == 1) {
132 MachineInstr* phi = start;
133 start++;
134 phi->eraseFromParent();
135 } else
136 start++;
137 }
138
139 BB->removeSuccessor(BB->succ_begin());
140 }
141 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142
143 // Actually remove the blocks now.
144 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
Owen Andersonfb6914f2008-08-04 23:54:43 +0000145 DeadBlocks[i]->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
Owen Anderson7a5d9142008-08-05 00:30:10 +0000147 F.RenumberBlocks();
148
Owen Andersonfb6914f2008-08-04 23:54:43 +0000149 return DeadBlocks.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150}
Owen Andersonfb6914f2008-08-04 23:54:43 +0000151