blob: 8fe155434253d9b9829bdea2243e85f72765232e [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"
Dan Gohmande104bd2009-08-01 00:34:30 +000029#include "llvm/CodeGen/MachineDominators.h"
Owen Andersonfb6914f2008-08-04 23:54:43 +000030#include "llvm/CodeGen/MachineFunctionPass.h"
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +000031#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmande104bd2009-08-01 00:34:30 +000032#include "llvm/CodeGen/MachineLoopInfo.h"
Owen Andersonb12a5b02008-08-05 21:40:45 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/Support/CFG.h"
35#include "llvm/Support/Compiler.h"
Owen Andersonfb6914f2008-08-04 23:54:43 +000036#include "llvm/Target/TargetInstrInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/ADT/DepthFirstIterator.h"
Owen Anderson43ada1d2008-08-14 21:01:00 +000038#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039using namespace llvm;
40
41namespace {
42 class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass {
43 virtual bool runOnFunction(Function &F);
44 public:
45 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000046 UnreachableBlockElim() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048}
Dan Gohman089efff2008-05-13 00:00:25 +000049char UnreachableBlockElim::ID = 0;
50static RegisterPass<UnreachableBlockElim>
51X("unreachableblockelim", "Remove unreachable blocks from the CFG");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052
53FunctionPass *llvm::createUnreachableBlockEliminationPass() {
54 return new UnreachableBlockElim();
55}
56
57bool UnreachableBlockElim::runOnFunction(Function &F) {
Owen Anderson43ada1d2008-08-14 21:01:00 +000058 SmallPtrSet<BasicBlock*, 8> Reachable;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
60 // Mark all reachable blocks.
Owen Anderson43ada1d2008-08-14 21:01:00 +000061 for (df_ext_iterator<Function*, SmallPtrSet<BasicBlock*, 8> > I =
62 df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); I != E; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 /* Mark all reachable blocks */;
64
65 // Loop over all dead blocks, remembering them and deleting all instructions
66 // in them.
67 std::vector<BasicBlock*> DeadBlocks;
68 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
69 if (!Reachable.count(I)) {
70 BasicBlock *BB = I;
71 DeadBlocks.push_back(BB);
72 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
Owen Andersonaac28372009-07-31 20:28:14 +000073 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 BB->getInstList().pop_front();
75 }
76 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
77 (*SI)->removePredecessor(BB);
78 BB->dropAllReferences();
79 }
80
Owen Andersonfb6914f2008-08-04 23:54:43 +000081 // Actually remove the blocks now.
82 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
83 DeadBlocks[i]->eraseFromParent();
84
85 return DeadBlocks.size();
86}
87
88
89namespace {
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +000090 class VISIBILITY_HIDDEN UnreachableMachineBlockElim :
Owen Andersonfb6914f2008-08-04 23:54:43 +000091 public MachineFunctionPass {
92 virtual bool runOnMachineFunction(MachineFunction &F);
Dan Gohmande104bd2009-08-01 00:34:30 +000093 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +000094 MachineModuleInfo *MMI;
Owen Andersonfb6914f2008-08-04 23:54:43 +000095 public:
96 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000097 UnreachableMachineBlockElim() : MachineFunctionPass(&ID) {}
Owen Andersonfb6914f2008-08-04 23:54:43 +000098 };
99}
100char UnreachableMachineBlockElim::ID = 0;
101
102static RegisterPass<UnreachableMachineBlockElim>
103Y("unreachable-mbb-elimination",
104 "Remove unreachable machine basic blocks");
105
106const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y;
107
Dan Gohmande104bd2009-08-01 00:34:30 +0000108void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
109 AU.addPreserved<MachineLoopInfo>();
110 AU.addPreserved<MachineDominatorTree>();
111 MachineFunctionPass::getAnalysisUsage(AU);
112}
113
Owen Andersonfb6914f2008-08-04 23:54:43 +0000114bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
Owen Anderson43ada1d2008-08-14 21:01:00 +0000115 SmallPtrSet<MachineBasicBlock*, 8> Reachable;
Owen Andersonfb6914f2008-08-04 23:54:43 +0000116
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000117 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Dan Gohmande104bd2009-08-01 00:34:30 +0000118 MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
119 MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000120
Owen Andersonfb6914f2008-08-04 23:54:43 +0000121 // Mark all reachable blocks.
Owen Anderson43ada1d2008-08-14 21:01:00 +0000122 for (df_ext_iterator<MachineFunction*, SmallPtrSet<MachineBasicBlock*, 8> >
123 I = df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable);
124 I != E; ++I)
Owen Andersonfb6914f2008-08-04 23:54:43 +0000125 /* Mark all reachable blocks */;
126
127 // Loop over all dead blocks, remembering them and deleting all instructions
128 // in them.
129 std::vector<MachineBasicBlock*> DeadBlocks;
Owen Andersonc11ae742008-08-06 23:16:52 +0000130 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
131 MachineBasicBlock *BB = I;
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000132
Owen Andersonc11ae742008-08-06 23:16:52 +0000133 // Test for deadness.
134 if (!Reachable.count(BB)) {
Owen Andersonfb6914f2008-08-04 23:54:43 +0000135 DeadBlocks.push_back(BB);
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000136
Dan Gohmande104bd2009-08-01 00:34:30 +0000137 // Update dominator and loop info.
138 if (MLI) MLI->removeBlock(BB);
139 if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
140
Owen Andersonfb6914f2008-08-04 23:54:43 +0000141 while (BB->succ_begin() != BB->succ_end()) {
142 MachineBasicBlock* succ = *BB->succ_begin();
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000143
Owen Andersonfb6914f2008-08-04 23:54:43 +0000144 MachineBasicBlock::iterator start = succ->begin();
145 while (start != succ->end() &&
146 start->getOpcode() == TargetInstrInfo::PHI) {
147 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000148 if (start->getOperand(i).isMBB() &&
Owen Andersonfb6914f2008-08-04 23:54:43 +0000149 start->getOperand(i).getMBB() == BB) {
150 start->RemoveOperand(i);
151 start->RemoveOperand(i-1);
152 }
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000153
Owen Andersonc11ae742008-08-06 23:16:52 +0000154 start++;
Owen Andersonfb6914f2008-08-04 23:54:43 +0000155 }
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000156
Owen Andersonfb6914f2008-08-04 23:54:43 +0000157 BB->removeSuccessor(BB->succ_begin());
158 }
159 }
Owen Andersonc11ae742008-08-06 23:16:52 +0000160 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161
162 // Actually remove the blocks now.
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000163 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
164 MachineBasicBlock *MBB = DeadBlocks[i];
165 // If there are any labels in the basic block, unregister them from
166 // MachineModuleInfo.
167 if (MMI && !MBB->empty()) {
168 for (MachineBasicBlock::iterator I = MBB->begin(),
169 E = MBB->end(); I != E; ++I) {
170 if (I->isLabel())
171 // The label ID # is always operand #0, an immediate.
172 MMI->InvalidateLabel(I->getOperand(0).getImm());
173 }
174 }
175 MBB->eraseFromParent();
176 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
Owen Andersonc11ae742008-08-06 23:16:52 +0000178 // Cleanup PHI nodes.
179 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
180 MachineBasicBlock *BB = I;
181 // Prune unneeded PHI entries.
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000182 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
Owen Andersonc11ae742008-08-06 23:16:52 +0000183 BB->pred_end());
184 MachineBasicBlock::iterator phi = BB->begin();
185 while (phi != BB->end() &&
186 phi->getOpcode() == TargetInstrInfo::PHI) {
Owen Anderson45d4b482008-08-08 18:00:05 +0000187 for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
188 if (!preds.count(phi->getOperand(i).getMBB())) {
189 phi->RemoveOperand(i);
190 phi->RemoveOperand(i-1);
191 }
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000192
Owen Andersonc11ae742008-08-06 23:16:52 +0000193 if (phi->getNumOperands() == 3) {
194 unsigned Input = phi->getOperand(1).getReg();
195 unsigned Output = phi->getOperand(0).getReg();
196
197 MachineInstr* temp = phi;
198 ++phi;
199 temp->eraseFromParent();
200
201 if (Input != Output)
202 F.getRegInfo().replaceRegWith(Output, Input);
Owen Anderson45d4b482008-08-08 18:00:05 +0000203
Owen Andersonc11ae742008-08-06 23:16:52 +0000204 continue;
205 }
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000206
Owen Andersonc11ae742008-08-06 23:16:52 +0000207 ++phi;
208 }
209 }
210
Owen Anderson7a5d9142008-08-05 00:30:10 +0000211 F.RenumberBlocks();
212
Owen Andersonfb6914f2008-08-04 23:54:43 +0000213 return DeadBlocks.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214}