blob: c597e5fd7db0ffedde3a737d384e7719643b77bc [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"
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +000030#include "llvm/CodeGen/MachineModuleInfo.h"
Owen Andersonb12a5b02008-08-05 21:40:45 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Support/CFG.h"
33#include "llvm/Support/Compiler.h"
Owen Andersonfb6914f2008-08-04 23:54:43 +000034#include "llvm/Target/TargetInstrInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/ADT/DepthFirstIterator.h"
Owen Anderson43ada1d2008-08-14 21:01:00 +000036#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037using namespace llvm;
38
39namespace {
40 class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass {
41 virtual bool runOnFunction(Function &F);
42 public:
43 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000044 UnreachableBlockElim() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046}
Dan Gohman089efff2008-05-13 00:00:25 +000047char UnreachableBlockElim::ID = 0;
48static RegisterPass<UnreachableBlockElim>
49X("unreachableblockelim", "Remove unreachable blocks from the CFG");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050
51FunctionPass *llvm::createUnreachableBlockEliminationPass() {
52 return new UnreachableBlockElim();
53}
54
55bool UnreachableBlockElim::runOnFunction(Function &F) {
Owen Anderson43ada1d2008-08-14 21:01:00 +000056 SmallPtrSet<BasicBlock*, 8> Reachable;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
58 // Mark all reachable blocks.
Owen Anderson43ada1d2008-08-14 21:01:00 +000059 for (df_ext_iterator<Function*, SmallPtrSet<BasicBlock*, 8> > I =
60 df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); I != E; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 /* Mark all reachable blocks */;
62
63 // Loop over all dead blocks, remembering them and deleting all instructions
64 // in them.
65 std::vector<BasicBlock*> DeadBlocks;
66 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
67 if (!Reachable.count(I)) {
68 BasicBlock *BB = I;
69 DeadBlocks.push_back(BB);
70 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
71 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
72 BB->getInstList().pop_front();
73 }
74 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
75 (*SI)->removePredecessor(BB);
76 BB->dropAllReferences();
77 }
78
Owen Andersonfb6914f2008-08-04 23:54:43 +000079 // Actually remove the blocks now.
80 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
81 DeadBlocks[i]->eraseFromParent();
82
83 return DeadBlocks.size();
84}
85
86
87namespace {
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +000088 class VISIBILITY_HIDDEN UnreachableMachineBlockElim :
Owen Andersonfb6914f2008-08-04 23:54:43 +000089 public MachineFunctionPass {
90 virtual bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +000091 MachineModuleInfo *MMI;
Owen Andersonfb6914f2008-08-04 23:54:43 +000092 public:
93 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000094 UnreachableMachineBlockElim() : MachineFunctionPass(&ID) {}
Owen Andersonfb6914f2008-08-04 23:54:43 +000095 };
96}
97char UnreachableMachineBlockElim::ID = 0;
98
99static RegisterPass<UnreachableMachineBlockElim>
100Y("unreachable-mbb-elimination",
101 "Remove unreachable machine basic blocks");
102
103const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y;
104
105bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
Owen Anderson43ada1d2008-08-14 21:01:00 +0000106 SmallPtrSet<MachineBasicBlock*, 8> Reachable;
Owen Andersonfb6914f2008-08-04 23:54:43 +0000107
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000108 MMI = getAnalysisToUpdate<MachineModuleInfo>();
109
Owen Andersonfb6914f2008-08-04 23:54:43 +0000110 // Mark all reachable blocks.
Owen Anderson43ada1d2008-08-14 21:01:00 +0000111 for (df_ext_iterator<MachineFunction*, SmallPtrSet<MachineBasicBlock*, 8> >
112 I = df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable);
113 I != E; ++I)
Owen Andersonfb6914f2008-08-04 23:54:43 +0000114 /* Mark all reachable blocks */;
115
116 // Loop over all dead blocks, remembering them and deleting all instructions
117 // in them.
118 std::vector<MachineBasicBlock*> DeadBlocks;
Owen Andersonc11ae742008-08-06 23:16:52 +0000119 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
120 MachineBasicBlock *BB = I;
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000121
Owen Andersonc11ae742008-08-06 23:16:52 +0000122 // Test for deadness.
123 if (!Reachable.count(BB)) {
Owen Andersonfb6914f2008-08-04 23:54:43 +0000124 DeadBlocks.push_back(BB);
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000125
Owen Andersonfb6914f2008-08-04 23:54:43 +0000126 while (BB->succ_begin() != BB->succ_end()) {
127 MachineBasicBlock* succ = *BB->succ_begin();
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000128
Owen Andersonfb6914f2008-08-04 23:54:43 +0000129 MachineBasicBlock::iterator start = succ->begin();
130 while (start != succ->end() &&
131 start->getOpcode() == TargetInstrInfo::PHI) {
132 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000133 if (start->getOperand(i).isMBB() &&
Owen Andersonfb6914f2008-08-04 23:54:43 +0000134 start->getOperand(i).getMBB() == BB) {
135 start->RemoveOperand(i);
136 start->RemoveOperand(i-1);
137 }
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000138
Owen Andersonc11ae742008-08-06 23:16:52 +0000139 start++;
Owen Andersonfb6914f2008-08-04 23:54:43 +0000140 }
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000141
Owen Andersonfb6914f2008-08-04 23:54:43 +0000142 BB->removeSuccessor(BB->succ_begin());
143 }
144 }
Owen Andersonc11ae742008-08-06 23:16:52 +0000145 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
147 // Actually remove the blocks now.
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000148 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
149 MachineBasicBlock *MBB = DeadBlocks[i];
150 // If there are any labels in the basic block, unregister them from
151 // MachineModuleInfo.
152 if (MMI && !MBB->empty()) {
153 for (MachineBasicBlock::iterator I = MBB->begin(),
154 E = MBB->end(); I != E; ++I) {
155 if (I->isLabel())
156 // The label ID # is always operand #0, an immediate.
157 MMI->InvalidateLabel(I->getOperand(0).getImm());
158 }
159 }
160 MBB->eraseFromParent();
161 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162
Owen Andersonc11ae742008-08-06 23:16:52 +0000163 // Cleanup PHI nodes.
164 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
165 MachineBasicBlock *BB = I;
166 // Prune unneeded PHI entries.
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000167 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
Owen Andersonc11ae742008-08-06 23:16:52 +0000168 BB->pred_end());
169 MachineBasicBlock::iterator phi = BB->begin();
170 while (phi != BB->end() &&
171 phi->getOpcode() == TargetInstrInfo::PHI) {
Owen Anderson45d4b482008-08-08 18:00:05 +0000172 for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
173 if (!preds.count(phi->getOperand(i).getMBB())) {
174 phi->RemoveOperand(i);
175 phi->RemoveOperand(i-1);
176 }
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000177
Owen Andersonc11ae742008-08-06 23:16:52 +0000178 if (phi->getNumOperands() == 3) {
179 unsigned Input = phi->getOperand(1).getReg();
180 unsigned Output = phi->getOperand(0).getReg();
181
182 MachineInstr* temp = phi;
183 ++phi;
184 temp->eraseFromParent();
185
186 if (Input != Output)
187 F.getRegInfo().replaceRegWith(Output, Input);
Owen Anderson45d4b482008-08-08 18:00:05 +0000188
Owen Andersonc11ae742008-08-06 23:16:52 +0000189 continue;
190 }
Anton Korobeynikov2c56ce02008-10-31 20:08:30 +0000191
Owen Andersonc11ae742008-08-06 23:16:52 +0000192 ++phi;
193 }
194 }
195
Owen Anderson7a5d9142008-08-05 00:30:10 +0000196 F.RenumberBlocks();
197
Owen Andersonfb6914f2008-08-04 23:54:43 +0000198 return DeadBlocks.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199}