blob: 3c3fca51b66f27931e640b39b7f0f87d2572a16a [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"
Owen Anderson5d2f8072008-08-05 21:40:45 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000031#include "llvm/Support/CFG.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000032#include "llvm/Support/Compiler.h"
Owen Andersonbd3ba462008-08-04 23:54:43 +000033#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000034#include "llvm/ADT/DepthFirstIterator.h"
Owen Andersoneaa009d2008-08-14 21:01:00 +000035#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000036using namespace llvm;
37
38namespace {
Chris Lattner95255282006-06-28 23:17:24 +000039 class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass {
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000040 virtual bool runOnFunction(Function &F);
Devang Patel794fd752007-05-01 21:15:47 +000041 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000042 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000043 UnreachableBlockElim() : FunctionPass(&ID) {}
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000044 };
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000045}
Dan Gohman844731a2008-05-13 00:00:25 +000046char UnreachableBlockElim::ID = 0;
47static RegisterPass<UnreachableBlockElim>
48X("unreachableblockelim", "Remove unreachable blocks from the CFG");
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000049
50FunctionPass *llvm::createUnreachableBlockEliminationPass() {
51 return new UnreachableBlockElim();
52}
53
54bool UnreachableBlockElim::runOnFunction(Function &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +000055 SmallPtrSet<BasicBlock*, 8> Reachable;
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000056
57 // Mark all reachable blocks.
Owen Andersoneaa009d2008-08-14 21:01:00 +000058 for (df_ext_iterator<Function*, SmallPtrSet<BasicBlock*, 8> > I =
59 df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); I != E; ++I)
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000060 /* Mark all reachable blocks */;
61
62 // Loop over all dead blocks, remembering them and deleting all instructions
63 // in them.
64 std::vector<BasicBlock*> DeadBlocks;
65 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
66 if (!Reachable.count(I)) {
Chris Lattner7f0566c2004-07-06 06:36:11 +000067 BasicBlock *BB = I;
68 DeadBlocks.push_back(BB);
69 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
70 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
71 BB->getInstList().pop_front();
72 }
73 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
74 (*SI)->removePredecessor(BB);
75 BB->dropAllReferences();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000076 }
77
Owen Andersonbd3ba462008-08-04 23:54:43 +000078 // Actually remove the blocks now.
79 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
80 DeadBlocks[i]->eraseFromParent();
81
82 return DeadBlocks.size();
83}
84
85
86namespace {
87 class VISIBILITY_HIDDEN UnreachableMachineBlockElim :
88 public MachineFunctionPass {
89 virtual bool runOnMachineFunction(MachineFunction &F);
Owen Andersonbd3ba462008-08-04 23:54:43 +000090
91 public:
92 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000093 UnreachableMachineBlockElim() : MachineFunctionPass(&ID) {}
Owen Andersonbd3ba462008-08-04 23:54:43 +000094 };
95}
96char UnreachableMachineBlockElim::ID = 0;
97
98static RegisterPass<UnreachableMachineBlockElim>
99Y("unreachable-mbb-elimination",
100 "Remove unreachable machine basic blocks");
101
102const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y;
103
104bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +0000105 SmallPtrSet<MachineBasicBlock*, 8> Reachable;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000106
107 // Mark all reachable blocks.
Owen Andersoneaa009d2008-08-14 21:01:00 +0000108 for (df_ext_iterator<MachineFunction*, SmallPtrSet<MachineBasicBlock*, 8> >
109 I = df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable);
110 I != E; ++I)
Owen Andersonbd3ba462008-08-04 23:54:43 +0000111 /* Mark all reachable blocks */;
112
113 // Loop over all dead blocks, remembering them and deleting all instructions
114 // in them.
115 std::vector<MachineBasicBlock*> DeadBlocks;
Owen Anderson9200e812008-08-06 23:16:52 +0000116 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
117 MachineBasicBlock *BB = I;
118
119 // Test for deadness.
120 if (!Reachable.count(BB)) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000121 DeadBlocks.push_back(BB);
122
123 while (BB->succ_begin() != BB->succ_end()) {
124 MachineBasicBlock* succ = *BB->succ_begin();
125
126 MachineBasicBlock::iterator start = succ->begin();
127 while (start != succ->end() &&
128 start->getOpcode() == TargetInstrInfo::PHI) {
129 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
130 if (start->getOperand(i).isMBB() &&
131 start->getOperand(i).getMBB() == BB) {
132 start->RemoveOperand(i);
133 start->RemoveOperand(i-1);
134 }
135
Owen Anderson9200e812008-08-06 23:16:52 +0000136 start++;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000137 }
138
139 BB->removeSuccessor(BB->succ_begin());
140 }
141 }
Owen Anderson9200e812008-08-06 23:16:52 +0000142 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000143
144 // Actually remove the blocks now.
145 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
Owen Andersonbd3ba462008-08-04 23:54:43 +0000146 DeadBlocks[i]->eraseFromParent();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000147
Owen Anderson9200e812008-08-06 23:16:52 +0000148 // Cleanup PHI nodes.
149 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
150 MachineBasicBlock *BB = I;
151 // Prune unneeded PHI entries.
152 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
153 BB->pred_end());
154 MachineBasicBlock::iterator phi = BB->begin();
155 while (phi != BB->end() &&
156 phi->getOpcode() == TargetInstrInfo::PHI) {
Owen Andersonb12ab972008-08-08 18:00:05 +0000157 for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
158 if (!preds.count(phi->getOperand(i).getMBB())) {
159 phi->RemoveOperand(i);
160 phi->RemoveOperand(i-1);
161 }
162
Owen Anderson9200e812008-08-06 23:16:52 +0000163 if (phi->getNumOperands() == 3) {
164 unsigned Input = phi->getOperand(1).getReg();
165 unsigned Output = phi->getOperand(0).getReg();
166
167 MachineInstr* temp = phi;
168 ++phi;
169 temp->eraseFromParent();
170
171 if (Input != Output)
172 F.getRegInfo().replaceRegWith(Output, Input);
Owen Andersonb12ab972008-08-08 18:00:05 +0000173
Owen Anderson9200e812008-08-06 23:16:52 +0000174 continue;
175 }
Owen Anderson9200e812008-08-06 23:16:52 +0000176
177 ++phi;
178 }
179 }
180
Owen Anderson59c0d4f2008-08-05 00:30:10 +0000181 F.RenumberBlocks();
182
Owen Andersonbd3ba462008-08-04 23:54:43 +0000183 return DeadBlocks.size();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000184}
Owen Andersonbd3ba462008-08-04 23:54:43 +0000185