blob: 6ab5db2edaacb9e52006cbdd876df549e0b6e9f9 [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"
Owen Andersonbd3ba462008-08-04 23:54:43 +000036#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000037#include "llvm/ADT/DepthFirstIterator.h"
Owen Andersoneaa009d2008-08-14 21:01:00 +000038#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000039using namespace llvm;
40
41namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000042 class UnreachableBlockElim : public FunctionPass {
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000043 virtual bool runOnFunction(Function &F);
Devang Patel794fd752007-05-01 21:15:47 +000044 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000045 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000046 UnreachableBlockElim() : FunctionPass(&ID) {}
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000047
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 AU.addPreserved<ProfileInfo>();
50 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000051 };
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000052}
Dan Gohman844731a2008-05-13 00:00:25 +000053char UnreachableBlockElim::ID = 0;
54static RegisterPass<UnreachableBlockElim>
55X("unreachableblockelim", "Remove unreachable blocks from the CFG");
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000056
57FunctionPass *llvm::createUnreachableBlockEliminationPass() {
58 return new UnreachableBlockElim();
59}
60
61bool UnreachableBlockElim::runOnFunction(Function &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +000062 SmallPtrSet<BasicBlock*, 8> Reachable;
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000063
64 // Mark all reachable blocks.
Owen Andersoneaa009d2008-08-14 21:01:00 +000065 for (df_ext_iterator<Function*, SmallPtrSet<BasicBlock*, 8> > I =
66 df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); I != E; ++I)
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000067 /* Mark all reachable blocks */;
68
69 // Loop over all dead blocks, remembering them and deleting all instructions
70 // in them.
71 std::vector<BasicBlock*> DeadBlocks;
72 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
73 if (!Reachable.count(I)) {
Chris Lattner7f0566c2004-07-06 06:36:11 +000074 BasicBlock *BB = I;
75 DeadBlocks.push_back(BB);
76 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
Owen Andersona7235ea2009-07-31 20:28:14 +000077 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
Chris Lattner7f0566c2004-07-06 06:36:11 +000078 BB->getInstList().pop_front();
79 }
80 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
81 (*SI)->removePredecessor(BB);
82 BB->dropAllReferences();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000083 }
84
Owen Andersonbd3ba462008-08-04 23:54:43 +000085 // Actually remove the blocks now.
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000086 ProfileInfo *PI = getAnalysisIfAvailable<ProfileInfo>();
87 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
88 if (PI) PI->removeBlock(DeadBlocks[i]);
Owen Andersonbd3ba462008-08-04 23:54:43 +000089 DeadBlocks[i]->eraseFromParent();
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000090 }
Owen Andersonbd3ba462008-08-04 23:54:43 +000091
92 return DeadBlocks.size();
93}
94
95
96namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000097 class UnreachableMachineBlockElim : public MachineFunctionPass {
Owen Andersonbd3ba462008-08-04 23:54:43 +000098 virtual bool runOnMachineFunction(MachineFunction &F);
Dan Gohman4f3cfba2009-08-01 00:34:30 +000099 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000100 MachineModuleInfo *MMI;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000101 public:
102 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000103 UnreachableMachineBlockElim() : MachineFunctionPass(&ID) {}
Owen Andersonbd3ba462008-08-04 23:54:43 +0000104 };
105}
106char UnreachableMachineBlockElim::ID = 0;
107
108static RegisterPass<UnreachableMachineBlockElim>
109Y("unreachable-mbb-elimination",
110 "Remove unreachable machine basic blocks");
111
112const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y;
113
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000114void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
115 AU.addPreserved<MachineLoopInfo>();
116 AU.addPreserved<MachineDominatorTree>();
117 MachineFunctionPass::getAnalysisUsage(AU);
118}
119
Owen Andersonbd3ba462008-08-04 23:54:43 +0000120bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +0000121 SmallPtrSet<MachineBasicBlock*, 8> Reachable;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000122
Duncan Sands1465d612009-01-28 13:14:17 +0000123 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000124 MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
125 MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000126
Owen Andersonbd3ba462008-08-04 23:54:43 +0000127 // Mark all reachable blocks.
Owen Andersoneaa009d2008-08-14 21:01:00 +0000128 for (df_ext_iterator<MachineFunction*, SmallPtrSet<MachineBasicBlock*, 8> >
129 I = df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable);
130 I != E; ++I)
Owen Andersonbd3ba462008-08-04 23:54:43 +0000131 /* Mark all reachable blocks */;
132
133 // Loop over all dead blocks, remembering them and deleting all instructions
134 // in them.
135 std::vector<MachineBasicBlock*> DeadBlocks;
Owen Anderson9200e812008-08-06 23:16:52 +0000136 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
137 MachineBasicBlock *BB = I;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000138
Owen Anderson9200e812008-08-06 23:16:52 +0000139 // Test for deadness.
140 if (!Reachable.count(BB)) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000141 DeadBlocks.push_back(BB);
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000142
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000143 // Update dominator and loop info.
144 if (MLI) MLI->removeBlock(BB);
145 if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
146
Owen Andersonbd3ba462008-08-04 23:54:43 +0000147 while (BB->succ_begin() != BB->succ_end()) {
148 MachineBasicBlock* succ = *BB->succ_begin();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000149
Owen Andersonbd3ba462008-08-04 23:54:43 +0000150 MachineBasicBlock::iterator start = succ->begin();
151 while (start != succ->end() &&
152 start->getOpcode() == TargetInstrInfo::PHI) {
153 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
Dan Gohmand735b802008-10-03 15:45:36 +0000154 if (start->getOperand(i).isMBB() &&
Owen Andersonbd3ba462008-08-04 23:54:43 +0000155 start->getOperand(i).getMBB() == BB) {
156 start->RemoveOperand(i);
157 start->RemoveOperand(i-1);
158 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000159
Owen Anderson9200e812008-08-06 23:16:52 +0000160 start++;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000161 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000162
Owen Andersonbd3ba462008-08-04 23:54:43 +0000163 BB->removeSuccessor(BB->succ_begin());
164 }
165 }
Owen Anderson9200e812008-08-06 23:16:52 +0000166 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000167
168 // Actually remove the blocks now.
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000169 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
170 MachineBasicBlock *MBB = DeadBlocks[i];
171 // If there are any labels in the basic block, unregister them from
172 // MachineModuleInfo.
173 if (MMI && !MBB->empty()) {
174 for (MachineBasicBlock::iterator I = MBB->begin(),
175 E = MBB->end(); I != E; ++I) {
176 if (I->isLabel())
177 // The label ID # is always operand #0, an immediate.
178 MMI->InvalidateLabel(I->getOperand(0).getImm());
179 }
180 }
181 MBB->eraseFromParent();
182 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000183
Owen Anderson9200e812008-08-06 23:16:52 +0000184 // Cleanup PHI nodes.
185 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
186 MachineBasicBlock *BB = I;
187 // Prune unneeded PHI entries.
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000188 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
Owen Anderson9200e812008-08-06 23:16:52 +0000189 BB->pred_end());
190 MachineBasicBlock::iterator phi = BB->begin();
191 while (phi != BB->end() &&
192 phi->getOpcode() == TargetInstrInfo::PHI) {
Owen Andersonb12ab972008-08-08 18:00:05 +0000193 for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
194 if (!preds.count(phi->getOperand(i).getMBB())) {
195 phi->RemoveOperand(i);
196 phi->RemoveOperand(i-1);
197 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000198
Owen Anderson9200e812008-08-06 23:16:52 +0000199 if (phi->getNumOperands() == 3) {
200 unsigned Input = phi->getOperand(1).getReg();
201 unsigned Output = phi->getOperand(0).getReg();
202
203 MachineInstr* temp = phi;
204 ++phi;
205 temp->eraseFromParent();
206
207 if (Input != Output)
208 F.getRegInfo().replaceRegWith(Output, Input);
Owen Andersonb12ab972008-08-08 18:00:05 +0000209
Owen Anderson9200e812008-08-06 23:16:52 +0000210 continue;
211 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000212
Owen Anderson9200e812008-08-06 23:16:52 +0000213 ++phi;
214 }
215 }
216
Owen Anderson59c0d4f2008-08-05 00:30:10 +0000217 F.RenumberBlocks();
218
Owen Andersonbd3ba462008-08-04 23:54:43 +0000219 return DeadBlocks.size();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000220}