blob: e7c34129268e50a8cf363573c6ea49c86b16c6cd [file] [log] [blame]
Chris Lattnerfc3c82a2004-07-02 05:46:10 +00001//===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattnerfc3c82a2004-07-02 05:46:10 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattnerfc3c82a2004-07-02 05:46:10 +00008//===----------------------------------------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00009//
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000010// 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"
Chris Lattner7f0566c2004-07-06 06:36:11 +000024#include "llvm/Constant.h"
Chris Lattner879313a2004-07-29 17:23:00 +000025#include "llvm/Instructions.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000026#include "llvm/Function.h"
27#include "llvm/Pass.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000028#include "llvm/Type.h"
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000029#include "llvm/Analysis/ProfileInfo.h"
Dan Gohman4f3cfba2009-08-01 00:34:30 +000030#include "llvm/CodeGen/MachineDominators.h"
Owen Andersonbd3ba462008-08-04 23:54:43 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Anton Korobeynikoved532ca2008-10-31 20:08:30 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman4f3cfba2009-08-01 00:34:30 +000033#include "llvm/CodeGen/MachineLoopInfo.h"
Owen Anderson5d2f8072008-08-05 21:40:45 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000035#include "llvm/Support/CFG.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000036#include "llvm/Support/Compiler.h"
Owen Andersonbd3ba462008-08-04 23:54:43 +000037#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000038#include "llvm/ADT/DepthFirstIterator.h"
Owen Andersoneaa009d2008-08-14 21:01:00 +000039#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000040using namespace llvm;
41
42namespace {
Chris Lattner95255282006-06-28 23:17:24 +000043 class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass {
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000044 virtual bool runOnFunction(Function &F);
Devang Patel794fd752007-05-01 21:15:47 +000045 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000046 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000047 UnreachableBlockElim() : FunctionPass(&ID) {}
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000048
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50 AU.addPreserved<ProfileInfo>();
51 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000052 };
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000053}
Dan Gohman844731a2008-05-13 00:00:25 +000054char UnreachableBlockElim::ID = 0;
55static RegisterPass<UnreachableBlockElim>
56X("unreachableblockelim", "Remove unreachable blocks from the CFG");
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000057
58FunctionPass *llvm::createUnreachableBlockEliminationPass() {
59 return new UnreachableBlockElim();
60}
61
62bool UnreachableBlockElim::runOnFunction(Function &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +000063 SmallPtrSet<BasicBlock*, 8> Reachable;
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000064
65 // Mark all reachable blocks.
Owen Andersoneaa009d2008-08-14 21:01:00 +000066 for (df_ext_iterator<Function*, SmallPtrSet<BasicBlock*, 8> > I =
67 df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); I != E; ++I)
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000068 /* Mark all reachable blocks */;
69
70 // Loop over all dead blocks, remembering them and deleting all instructions
71 // in them.
72 std::vector<BasicBlock*> DeadBlocks;
73 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
74 if (!Reachable.count(I)) {
Chris Lattner7f0566c2004-07-06 06:36:11 +000075 BasicBlock *BB = I;
76 DeadBlocks.push_back(BB);
77 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
Owen Andersona7235ea2009-07-31 20:28:14 +000078 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
Chris Lattner7f0566c2004-07-06 06:36:11 +000079 BB->getInstList().pop_front();
80 }
81 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
82 (*SI)->removePredecessor(BB);
83 BB->dropAllReferences();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000084 }
85
Owen Andersonbd3ba462008-08-04 23:54:43 +000086 // Actually remove the blocks now.
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000087 ProfileInfo *PI = getAnalysisIfAvailable<ProfileInfo>();
88 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
89 if (PI) PI->removeBlock(DeadBlocks[i]);
Owen Andersonbd3ba462008-08-04 23:54:43 +000090 DeadBlocks[i]->eraseFromParent();
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000091 }
Owen Andersonbd3ba462008-08-04 23:54:43 +000092
93 return DeadBlocks.size();
94}
95
96
97namespace {
Anton Korobeynikoved532ca2008-10-31 20:08:30 +000098 class VISIBILITY_HIDDEN UnreachableMachineBlockElim :
Owen Andersonbd3ba462008-08-04 23:54:43 +000099 public MachineFunctionPass {
100 virtual bool runOnMachineFunction(MachineFunction &F);
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000101 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000102 MachineModuleInfo *MMI;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000103 public:
104 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000105 UnreachableMachineBlockElim() : MachineFunctionPass(&ID) {}
Owen Andersonbd3ba462008-08-04 23:54:43 +0000106 };
107}
108char UnreachableMachineBlockElim::ID = 0;
109
110static RegisterPass<UnreachableMachineBlockElim>
111Y("unreachable-mbb-elimination",
112 "Remove unreachable machine basic blocks");
113
114const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y;
115
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000116void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
117 AU.addPreserved<MachineLoopInfo>();
118 AU.addPreserved<MachineDominatorTree>();
119 MachineFunctionPass::getAnalysisUsage(AU);
120}
121
Owen Andersonbd3ba462008-08-04 23:54:43 +0000122bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +0000123 SmallPtrSet<MachineBasicBlock*, 8> Reachable;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000124
Duncan Sands1465d612009-01-28 13:14:17 +0000125 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000126 MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
127 MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000128
Owen Andersonbd3ba462008-08-04 23:54:43 +0000129 // Mark all reachable blocks.
Owen Andersoneaa009d2008-08-14 21:01:00 +0000130 for (df_ext_iterator<MachineFunction*, SmallPtrSet<MachineBasicBlock*, 8> >
131 I = df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable);
132 I != E; ++I)
Owen Andersonbd3ba462008-08-04 23:54:43 +0000133 /* Mark all reachable blocks */;
134
135 // Loop over all dead blocks, remembering them and deleting all instructions
136 // in them.
137 std::vector<MachineBasicBlock*> DeadBlocks;
Owen Anderson9200e812008-08-06 23:16:52 +0000138 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
139 MachineBasicBlock *BB = I;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000140
Owen Anderson9200e812008-08-06 23:16:52 +0000141 // Test for deadness.
142 if (!Reachable.count(BB)) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000143 DeadBlocks.push_back(BB);
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000144
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000145 // Update dominator and loop info.
146 if (MLI) MLI->removeBlock(BB);
147 if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
148
Owen Andersonbd3ba462008-08-04 23:54:43 +0000149 while (BB->succ_begin() != BB->succ_end()) {
150 MachineBasicBlock* succ = *BB->succ_begin();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000151
Owen Andersonbd3ba462008-08-04 23:54:43 +0000152 MachineBasicBlock::iterator start = succ->begin();
153 while (start != succ->end() &&
154 start->getOpcode() == TargetInstrInfo::PHI) {
155 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
Dan Gohmand735b802008-10-03 15:45:36 +0000156 if (start->getOperand(i).isMBB() &&
Owen Andersonbd3ba462008-08-04 23:54:43 +0000157 start->getOperand(i).getMBB() == BB) {
158 start->RemoveOperand(i);
159 start->RemoveOperand(i-1);
160 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000161
Owen Anderson9200e812008-08-06 23:16:52 +0000162 start++;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000163 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000164
Owen Andersonbd3ba462008-08-04 23:54:43 +0000165 BB->removeSuccessor(BB->succ_begin());
166 }
167 }
Owen Anderson9200e812008-08-06 23:16:52 +0000168 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000169
170 // Actually remove the blocks now.
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000171 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
172 MachineBasicBlock *MBB = DeadBlocks[i];
173 // If there are any labels in the basic block, unregister them from
174 // MachineModuleInfo.
175 if (MMI && !MBB->empty()) {
176 for (MachineBasicBlock::iterator I = MBB->begin(),
177 E = MBB->end(); I != E; ++I) {
178 if (I->isLabel())
179 // The label ID # is always operand #0, an immediate.
180 MMI->InvalidateLabel(I->getOperand(0).getImm());
181 }
182 }
183 MBB->eraseFromParent();
184 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000185
Owen Anderson9200e812008-08-06 23:16:52 +0000186 // Cleanup PHI nodes.
187 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
188 MachineBasicBlock *BB = I;
189 // Prune unneeded PHI entries.
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000190 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
Owen Anderson9200e812008-08-06 23:16:52 +0000191 BB->pred_end());
192 MachineBasicBlock::iterator phi = BB->begin();
193 while (phi != BB->end() &&
194 phi->getOpcode() == TargetInstrInfo::PHI) {
Owen Andersonb12ab972008-08-08 18:00:05 +0000195 for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
196 if (!preds.count(phi->getOperand(i).getMBB())) {
197 phi->RemoveOperand(i);
198 phi->RemoveOperand(i-1);
199 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000200
Owen Anderson9200e812008-08-06 23:16:52 +0000201 if (phi->getNumOperands() == 3) {
202 unsigned Input = phi->getOperand(1).getReg();
203 unsigned Output = phi->getOperand(0).getReg();
204
205 MachineInstr* temp = phi;
206 ++phi;
207 temp->eraseFromParent();
208
209 if (Input != Output)
210 F.getRegInfo().replaceRegWith(Output, Input);
Owen Andersonb12ab972008-08-08 18:00:05 +0000211
Owen Anderson9200e812008-08-06 23:16:52 +0000212 continue;
213 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000214
Owen Anderson9200e812008-08-06 23:16:52 +0000215 ++phi;
216 }
217 }
218
Owen Anderson59c0d4f2008-08-05 00:30:10 +0000219 F.RenumberBlocks();
220
Owen Andersonbd3ba462008-08-04 23:54:43 +0000221 return DeadBlocks.size();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000222}