blob: 41d07ae38df483e90dc7d14b36d1f2dde5e14449 [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 {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000043 class 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 {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000098 class UnreachableMachineBlockElim : public MachineFunctionPass {
Owen Andersonbd3ba462008-08-04 23:54:43 +000099 virtual bool runOnMachineFunction(MachineFunction &F);
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000100 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000101 MachineModuleInfo *MMI;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000102 public:
103 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000104 UnreachableMachineBlockElim() : MachineFunctionPass(&ID) {}
Owen Andersonbd3ba462008-08-04 23:54:43 +0000105 };
106}
107char UnreachableMachineBlockElim::ID = 0;
108
109static RegisterPass<UnreachableMachineBlockElim>
110Y("unreachable-mbb-elimination",
111 "Remove unreachable machine basic blocks");
112
113const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y;
114
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000115void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
116 AU.addPreserved<MachineLoopInfo>();
117 AU.addPreserved<MachineDominatorTree>();
118 MachineFunctionPass::getAnalysisUsage(AU);
119}
120
Owen Andersonbd3ba462008-08-04 23:54:43 +0000121bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +0000122 SmallPtrSet<MachineBasicBlock*, 8> Reachable;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000123
Duncan Sands1465d612009-01-28 13:14:17 +0000124 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000125 MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
126 MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000127
Owen Andersonbd3ba462008-08-04 23:54:43 +0000128 // Mark all reachable blocks.
Owen Andersoneaa009d2008-08-14 21:01:00 +0000129 for (df_ext_iterator<MachineFunction*, SmallPtrSet<MachineBasicBlock*, 8> >
130 I = df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable);
131 I != E; ++I)
Owen Andersonbd3ba462008-08-04 23:54:43 +0000132 /* Mark all reachable blocks */;
133
134 // Loop over all dead blocks, remembering them and deleting all instructions
135 // in them.
136 std::vector<MachineBasicBlock*> DeadBlocks;
Owen Anderson9200e812008-08-06 23:16:52 +0000137 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
138 MachineBasicBlock *BB = I;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000139
Owen Anderson9200e812008-08-06 23:16:52 +0000140 // Test for deadness.
141 if (!Reachable.count(BB)) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000142 DeadBlocks.push_back(BB);
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000143
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000144 // Update dominator and loop info.
145 if (MLI) MLI->removeBlock(BB);
146 if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
147
Owen Andersonbd3ba462008-08-04 23:54:43 +0000148 while (BB->succ_begin() != BB->succ_end()) {
149 MachineBasicBlock* succ = *BB->succ_begin();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000150
Owen Andersonbd3ba462008-08-04 23:54:43 +0000151 MachineBasicBlock::iterator start = succ->begin();
152 while (start != succ->end() &&
153 start->getOpcode() == TargetInstrInfo::PHI) {
154 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
Dan Gohmand735b802008-10-03 15:45:36 +0000155 if (start->getOperand(i).isMBB() &&
Owen Andersonbd3ba462008-08-04 23:54:43 +0000156 start->getOperand(i).getMBB() == BB) {
157 start->RemoveOperand(i);
158 start->RemoveOperand(i-1);
159 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000160
Owen Anderson9200e812008-08-06 23:16:52 +0000161 start++;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000162 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000163
Owen Andersonbd3ba462008-08-04 23:54:43 +0000164 BB->removeSuccessor(BB->succ_begin());
165 }
166 }
Owen Anderson9200e812008-08-06 23:16:52 +0000167 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000168
169 // Actually remove the blocks now.
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000170 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
171 MachineBasicBlock *MBB = DeadBlocks[i];
172 // If there are any labels in the basic block, unregister them from
173 // MachineModuleInfo.
174 if (MMI && !MBB->empty()) {
175 for (MachineBasicBlock::iterator I = MBB->begin(),
176 E = MBB->end(); I != E; ++I) {
177 if (I->isLabel())
178 // The label ID # is always operand #0, an immediate.
179 MMI->InvalidateLabel(I->getOperand(0).getImm());
180 }
181 }
182 MBB->eraseFromParent();
183 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000184
Owen Anderson9200e812008-08-06 23:16:52 +0000185 // Cleanup PHI nodes.
186 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
187 MachineBasicBlock *BB = I;
188 // Prune unneeded PHI entries.
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000189 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
Owen Anderson9200e812008-08-06 23:16:52 +0000190 BB->pred_end());
191 MachineBasicBlock::iterator phi = BB->begin();
192 while (phi != BB->end() &&
193 phi->getOpcode() == TargetInstrInfo::PHI) {
Owen Andersonb12ab972008-08-08 18:00:05 +0000194 for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
195 if (!preds.count(phi->getOperand(i).getMBB())) {
196 phi->RemoveOperand(i);
197 phi->RemoveOperand(i-1);
198 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000199
Owen Anderson9200e812008-08-06 23:16:52 +0000200 if (phi->getNumOperands() == 3) {
201 unsigned Input = phi->getOperand(1).getReg();
202 unsigned Output = phi->getOperand(0).getReg();
203
204 MachineInstr* temp = phi;
205 ++phi;
206 temp->eraseFromParent();
207
208 if (Input != Output)
209 F.getRegInfo().replaceRegWith(Output, Input);
Owen Andersonb12ab972008-08-08 18:00:05 +0000210
Owen Anderson9200e812008-08-06 23:16:52 +0000211 continue;
212 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000213
Owen Anderson9200e812008-08-06 23:16:52 +0000214 ++phi;
215 }
216 }
217
Owen Anderson59c0d4f2008-08-05 00:30:10 +0000218 F.RenumberBlocks();
219
Owen Andersonbd3ba462008-08-04 23:54:43 +0000220 return DeadBlocks.size();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000221}