]> git.proxmox.com Git - rustc.git/blob - src/llvm/lib/CodeGen/UnreachableBlockElim.cpp
Imported Upstream version 0.6
[rustc.git] / src / llvm / lib / CodeGen / UnreachableBlockElim.cpp
1 //===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
29 #include "llvm/Analysis/Dominators.h"
30 #include "llvm/Analysis/ProfileInfo.h"
31 #include "llvm/CodeGen/MachineDominators.h"
32 #include "llvm/CodeGen/MachineFunctionPass.h"
33 #include "llvm/CodeGen/MachineModuleInfo.h"
34 #include "llvm/CodeGen/MachineLoopInfo.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/Support/CFG.h"
37 #include "llvm/Target/TargetInstrInfo.h"
38 #include "llvm/ADT/DepthFirstIterator.h"
39 #include "llvm/ADT/SmallPtrSet.h"
40 using namespace llvm;
41
42 namespace {
43 class UnreachableBlockElim : public FunctionPass {
44 virtual bool runOnFunction(Function &F);
45 public:
46 static char ID; // Pass identification, replacement for typeid
47 UnreachableBlockElim() : FunctionPass(ID) {
48 initializeUnreachableBlockElimPass(*PassRegistry::getPassRegistry());
49 }
50
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.addPreserved<DominatorTree>();
53 AU.addPreserved<ProfileInfo>();
54 }
55 };
56 }
57 char UnreachableBlockElim::ID = 0;
58 INITIALIZE_PASS(UnreachableBlockElim, "unreachableblockelim",
59 "Remove unreachable blocks from the CFG", false, false)
60
61 FunctionPass *llvm::createUnreachableBlockEliminationPass() {
62 return new UnreachableBlockElim();
63 }
64
65 bool UnreachableBlockElim::runOnFunction(Function &F) {
66 SmallPtrSet<BasicBlock*, 8> Reachable;
67
68 // Mark all reachable blocks.
69 for (df_ext_iterator<Function*, SmallPtrSet<BasicBlock*, 8> > I =
70 df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); I != E; ++I)
71 /* Mark all reachable blocks */;
72
73 // Loop over all dead blocks, remembering them and deleting all instructions
74 // in them.
75 std::vector<BasicBlock*> DeadBlocks;
76 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
77 if (!Reachable.count(I)) {
78 BasicBlock *BB = I;
79 DeadBlocks.push_back(BB);
80 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
81 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
82 BB->getInstList().pop_front();
83 }
84 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
85 (*SI)->removePredecessor(BB);
86 BB->dropAllReferences();
87 }
88
89 // Actually remove the blocks now.
90 ProfileInfo *PI = getAnalysisIfAvailable<ProfileInfo>();
91 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
92 if (PI) PI->removeBlock(DeadBlocks[i]);
93 DeadBlocks[i]->eraseFromParent();
94 }
95
96 return DeadBlocks.size();
97 }
98
99
100 namespace {
101 class UnreachableMachineBlockElim : public MachineFunctionPass {
102 virtual bool runOnMachineFunction(MachineFunction &F);
103 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
104 MachineModuleInfo *MMI;
105 public:
106 static char ID; // Pass identification, replacement for typeid
107 UnreachableMachineBlockElim() : MachineFunctionPass(ID) {}
108 };
109 }
110 char UnreachableMachineBlockElim::ID = 0;
111
112 INITIALIZE_PASS(UnreachableMachineBlockElim, "unreachable-mbb-elimination",
113 "Remove unreachable machine basic blocks", false, false)
114
115 char &llvm::UnreachableMachineBlockElimID = UnreachableMachineBlockElim::ID;
116
117 void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
118 AU.addPreserved<MachineLoopInfo>();
119 AU.addPreserved<MachineDominatorTree>();
120 MachineFunctionPass::getAnalysisUsage(AU);
121 }
122
123 bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
124 SmallPtrSet<MachineBasicBlock*, 8> Reachable;
125 bool ModifiedPHI = false;
126
127 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
128 MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
129 MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
130
131 // Mark all reachable blocks.
132 for (df_ext_iterator<MachineFunction*, SmallPtrSet<MachineBasicBlock*, 8> >
133 I = df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable);
134 I != E; ++I)
135 /* Mark all reachable blocks */;
136
137 // Loop over all dead blocks, remembering them and deleting all instructions
138 // in them.
139 std::vector<MachineBasicBlock*> DeadBlocks;
140 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
141 MachineBasicBlock *BB = I;
142
143 // Test for deadness.
144 if (!Reachable.count(BB)) {
145 DeadBlocks.push_back(BB);
146
147 // Update dominator and loop info.
148 if (MLI) MLI->removeBlock(BB);
149 if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
150
151 while (BB->succ_begin() != BB->succ_end()) {
152 MachineBasicBlock* succ = *BB->succ_begin();
153
154 MachineBasicBlock::iterator start = succ->begin();
155 while (start != succ->end() && start->isPHI()) {
156 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
157 if (start->getOperand(i).isMBB() &&
158 start->getOperand(i).getMBB() == BB) {
159 start->RemoveOperand(i);
160 start->RemoveOperand(i-1);
161 }
162
163 start++;
164 }
165
166 BB->removeSuccessor(BB->succ_begin());
167 }
168 }
169 }
170
171 // Actually remove the blocks now.
172 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
173 DeadBlocks[i]->eraseFromParent();
174
175 // Cleanup PHI nodes.
176 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
177 MachineBasicBlock *BB = I;
178 // Prune unneeded PHI entries.
179 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
180 BB->pred_end());
181 MachineBasicBlock::iterator phi = BB->begin();
182 while (phi != BB->end() && phi->isPHI()) {
183 for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
184 if (!preds.count(phi->getOperand(i).getMBB())) {
185 phi->RemoveOperand(i);
186 phi->RemoveOperand(i-1);
187 ModifiedPHI = true;
188 }
189
190 if (phi->getNumOperands() == 3) {
191 unsigned Input = phi->getOperand(1).getReg();
192 unsigned Output = phi->getOperand(0).getReg();
193
194 MachineInstr* temp = phi;
195 ++phi;
196 temp->eraseFromParent();
197 ModifiedPHI = true;
198
199 if (Input != Output) {
200 MachineRegisterInfo &MRI = F.getRegInfo();
201 MRI.constrainRegClass(Input, MRI.getRegClass(Output));
202 MRI.replaceRegWith(Output, Input);
203 }
204
205 continue;
206 }
207
208 ++phi;
209 }
210 }
211
212 F.RenumberBlocks();
213
214 return (DeadBlocks.size() || ModifiedPHI);
215 }