blob: c3b213cebe9543a95020d13b26a95c215e91661d [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"
Owen Andersonbd3ba462008-08-04 23:54:43 +000029#include "llvm/CodeGen/MachineFunctionPass.h"
Anton Korobeynikoved532ca2008-10-31 20:08:30 +000030#include "llvm/CodeGen/MachineModuleInfo.h"
Owen Anderson5d2f8072008-08-05 21:40:45 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000032#include "llvm/Support/CFG.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000033#include "llvm/Support/Compiler.h"
Owen Andersonbd3ba462008-08-04 23:54:43 +000034#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/ADT/DepthFirstIterator.h"
Owen Andersoneaa009d2008-08-14 21:01:00 +000036#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000037using namespace llvm;
38
39namespace {
Chris Lattner95255282006-06-28 23:17:24 +000040 class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass {
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000041 virtual bool runOnFunction(Function &F);
Devang Patel794fd752007-05-01 21:15:47 +000042 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000043 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000044 UnreachableBlockElim() : FunctionPass(&ID) {}
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000045 };
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000046}
Dan Gohman844731a2008-05-13 00:00:25 +000047char UnreachableBlockElim::ID = 0;
48static RegisterPass<UnreachableBlockElim>
49X("unreachableblockelim", "Remove unreachable blocks from the CFG");
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000050
51FunctionPass *llvm::createUnreachableBlockEliminationPass() {
52 return new UnreachableBlockElim();
53}
54
55bool UnreachableBlockElim::runOnFunction(Function &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +000056 SmallPtrSet<BasicBlock*, 8> Reachable;
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000057
58 // Mark all reachable blocks.
Owen Andersoneaa009d2008-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)
Chris Lattnerfc3c82a2004-07-02 05:46:10 +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)) {
Chris Lattner7f0566c2004-07-06 06:36:11 +000068 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();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000077 }
78
Owen Andersonbd3ba462008-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 Korobeynikoved532ca2008-10-31 20:08:30 +000088 class VISIBILITY_HIDDEN UnreachableMachineBlockElim :
Owen Andersonbd3ba462008-08-04 23:54:43 +000089 public MachineFunctionPass {
90 virtual bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikoved532ca2008-10-31 20:08:30 +000091 MachineModuleInfo *MMI;
Owen Andersonbd3ba462008-08-04 23:54:43 +000092 public:
93 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000094 UnreachableMachineBlockElim() : MachineFunctionPass(&ID) {}
Owen Andersonbd3ba462008-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 Andersoneaa009d2008-08-14 21:01:00 +0000106 SmallPtrSet<MachineBasicBlock*, 8> Reachable;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000107
Duncan Sands1465d612009-01-28 13:14:17 +0000108 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000109
Owen Andersonbd3ba462008-08-04 23:54:43 +0000110 // Mark all reachable blocks.
Owen Andersoneaa009d2008-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 Andersonbd3ba462008-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 Anderson9200e812008-08-06 23:16:52 +0000119 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
120 MachineBasicBlock *BB = I;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000121
Owen Anderson9200e812008-08-06 23:16:52 +0000122 // Test for deadness.
123 if (!Reachable.count(BB)) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000124 DeadBlocks.push_back(BB);
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000125
Owen Andersonbd3ba462008-08-04 23:54:43 +0000126 while (BB->succ_begin() != BB->succ_end()) {
127 MachineBasicBlock* succ = *BB->succ_begin();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000128
Owen Andersonbd3ba462008-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 Gohmand735b802008-10-03 15:45:36 +0000133 if (start->getOperand(i).isMBB() &&
Owen Andersonbd3ba462008-08-04 23:54:43 +0000134 start->getOperand(i).getMBB() == BB) {
135 start->RemoveOperand(i);
136 start->RemoveOperand(i-1);
137 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000138
Owen Anderson9200e812008-08-06 23:16:52 +0000139 start++;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000140 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000141
Owen Andersonbd3ba462008-08-04 23:54:43 +0000142 BB->removeSuccessor(BB->succ_begin());
143 }
144 }
Owen Anderson9200e812008-08-06 23:16:52 +0000145 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000146
147 // Actually remove the blocks now.
Anton Korobeynikoved532ca2008-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 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000162
Owen Anderson9200e812008-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 Korobeynikoved532ca2008-10-31 20:08:30 +0000167 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
Owen Anderson9200e812008-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 Andersonb12ab972008-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 Korobeynikoved532ca2008-10-31 20:08:30 +0000177
Owen Anderson9200e812008-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 Andersonb12ab972008-08-08 18:00:05 +0000188
Owen Anderson9200e812008-08-06 23:16:52 +0000189 continue;
190 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000191
Owen Anderson9200e812008-08-06 23:16:52 +0000192 ++phi;
193 }
194 }
195
Owen Anderson59c0d4f2008-08-05 00:30:10 +0000196 F.RenumberBlocks();
197
Owen Andersonbd3ba462008-08-04 23:54:43 +0000198 return DeadBlocks.size();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000199}