blob: 8fe155434253d9b9829bdea2243e85f72765232e [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"
Dan Gohman4f3cfba2009-08-01 00:34:30 +000029#include "llvm/CodeGen/MachineDominators.h"
Owen Andersonbd3ba462008-08-04 23:54:43 +000030#include "llvm/CodeGen/MachineFunctionPass.h"
Anton Korobeynikoved532ca2008-10-31 20:08:30 +000031#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman4f3cfba2009-08-01 00:34:30 +000032#include "llvm/CodeGen/MachineLoopInfo.h"
Owen Anderson5d2f8072008-08-05 21:40:45 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000034#include "llvm/Support/CFG.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000035#include "llvm/Support/Compiler.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 {
Chris Lattner95255282006-06-28 23:17:24 +000042 class VISIBILITY_HIDDEN 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) {}
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000047 };
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000048}
Dan Gohman844731a2008-05-13 00:00:25 +000049char UnreachableBlockElim::ID = 0;
50static RegisterPass<UnreachableBlockElim>
51X("unreachableblockelim", "Remove unreachable blocks from the CFG");
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000052
53FunctionPass *llvm::createUnreachableBlockEliminationPass() {
54 return new UnreachableBlockElim();
55}
56
57bool UnreachableBlockElim::runOnFunction(Function &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +000058 SmallPtrSet<BasicBlock*, 8> Reachable;
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000059
60 // Mark all reachable blocks.
Owen Andersoneaa009d2008-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)
Chris Lattnerfc3c82a2004-07-02 05:46:10 +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)) {
Chris Lattner7f0566c2004-07-06 06:36:11 +000070 BasicBlock *BB = I;
71 DeadBlocks.push_back(BB);
72 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
Owen Andersona7235ea2009-07-31 20:28:14 +000073 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
Chris Lattner7f0566c2004-07-06 06:36:11 +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();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000079 }
80
Owen Andersonbd3ba462008-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 Korobeynikoved532ca2008-10-31 20:08:30 +000090 class VISIBILITY_HIDDEN UnreachableMachineBlockElim :
Owen Andersonbd3ba462008-08-04 23:54:43 +000091 public MachineFunctionPass {
92 virtual bool runOnMachineFunction(MachineFunction &F);
Dan Gohman4f3cfba2009-08-01 00:34:30 +000093 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +000094 MachineModuleInfo *MMI;
Owen Andersonbd3ba462008-08-04 23:54:43 +000095 public:
96 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000097 UnreachableMachineBlockElim() : MachineFunctionPass(&ID) {}
Owen Andersonbd3ba462008-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 Gohman4f3cfba2009-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 Andersonbd3ba462008-08-04 23:54:43 +0000114bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +0000115 SmallPtrSet<MachineBasicBlock*, 8> Reachable;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000116
Duncan Sands1465d612009-01-28 13:14:17 +0000117 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000118 MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
119 MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000120
Owen Andersonbd3ba462008-08-04 23:54:43 +0000121 // Mark all reachable blocks.
Owen Andersoneaa009d2008-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 Andersonbd3ba462008-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 Anderson9200e812008-08-06 23:16:52 +0000130 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
131 MachineBasicBlock *BB = I;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000132
Owen Anderson9200e812008-08-06 23:16:52 +0000133 // Test for deadness.
134 if (!Reachable.count(BB)) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000135 DeadBlocks.push_back(BB);
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000136
Dan Gohman4f3cfba2009-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 Andersonbd3ba462008-08-04 23:54:43 +0000141 while (BB->succ_begin() != BB->succ_end()) {
142 MachineBasicBlock* succ = *BB->succ_begin();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000143
Owen Andersonbd3ba462008-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 Gohmand735b802008-10-03 15:45:36 +0000148 if (start->getOperand(i).isMBB() &&
Owen Andersonbd3ba462008-08-04 23:54:43 +0000149 start->getOperand(i).getMBB() == BB) {
150 start->RemoveOperand(i);
151 start->RemoveOperand(i-1);
152 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000153
Owen Anderson9200e812008-08-06 23:16:52 +0000154 start++;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000155 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000156
Owen Andersonbd3ba462008-08-04 23:54:43 +0000157 BB->removeSuccessor(BB->succ_begin());
158 }
159 }
Owen Anderson9200e812008-08-06 23:16:52 +0000160 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000161
162 // Actually remove the blocks now.
Anton Korobeynikoved532ca2008-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 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000177
Owen Anderson9200e812008-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 Korobeynikoved532ca2008-10-31 20:08:30 +0000182 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
Owen Anderson9200e812008-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 Andersonb12ab972008-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 Korobeynikoved532ca2008-10-31 20:08:30 +0000192
Owen Anderson9200e812008-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 Andersonb12ab972008-08-08 18:00:05 +0000203
Owen Anderson9200e812008-08-06 23:16:52 +0000204 continue;
205 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000206
Owen Anderson9200e812008-08-06 23:16:52 +0000207 ++phi;
208 }
209 }
210
Owen Anderson59c0d4f2008-08-05 00:30:10 +0000211 F.RenumberBlocks();
212
Owen Andersonbd3ba462008-08-04 23:54:43 +0000213 return DeadBlocks.size();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000214}