blob: 8286239096a072de4502752cb75451f5f125caf6 [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"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000030#include "llvm/Support/CFG.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000031#include "llvm/Support/Compiler.h"
Owen Andersonbd3ba462008-08-04 23:54:43 +000032#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/ADT/DepthFirstIterator.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000034using namespace llvm;
35
36namespace {
Chris Lattner95255282006-06-28 23:17:24 +000037 class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass {
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000038 virtual bool runOnFunction(Function &F);
Devang Patel794fd752007-05-01 21:15:47 +000039 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000040 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000041 UnreachableBlockElim() : FunctionPass((intptr_t)&ID) {}
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000042 };
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000043}
Dan Gohman844731a2008-05-13 00:00:25 +000044char UnreachableBlockElim::ID = 0;
45static RegisterPass<UnreachableBlockElim>
46X("unreachableblockelim", "Remove unreachable blocks from the CFG");
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000047
48FunctionPass *llvm::createUnreachableBlockEliminationPass() {
49 return new UnreachableBlockElim();
50}
51
52bool UnreachableBlockElim::runOnFunction(Function &F) {
53 std::set<BasicBlock*> Reachable;
54
55 // Mark all reachable blocks.
56 for (df_ext_iterator<Function*> I = df_ext_begin(&F, Reachable),
57 E = df_ext_end(&F, Reachable); I != E; ++I)
58 /* Mark all reachable blocks */;
59
60 // Loop over all dead blocks, remembering them and deleting all instructions
61 // in them.
62 std::vector<BasicBlock*> DeadBlocks;
63 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
64 if (!Reachable.count(I)) {
Chris Lattner7f0566c2004-07-06 06:36:11 +000065 BasicBlock *BB = I;
66 DeadBlocks.push_back(BB);
67 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
68 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
69 BB->getInstList().pop_front();
70 }
71 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
72 (*SI)->removePredecessor(BB);
73 BB->dropAllReferences();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000074 }
75
Owen Andersonbd3ba462008-08-04 23:54:43 +000076 // Actually remove the blocks now.
77 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
78 DeadBlocks[i]->eraseFromParent();
79
80 return DeadBlocks.size();
81}
82
83
84namespace {
85 class VISIBILITY_HIDDEN UnreachableMachineBlockElim :
86 public MachineFunctionPass {
87 virtual bool runOnMachineFunction(MachineFunction &F);
88 bool iterateOnFunction(MachineFunction& F);
89
90 public:
91 static char ID; // Pass identification, replacement for typeid
92 UnreachableMachineBlockElim() : MachineFunctionPass((intptr_t)&ID) {}
93 };
94}
95char UnreachableMachineBlockElim::ID = 0;
96
97static RegisterPass<UnreachableMachineBlockElim>
98Y("unreachable-mbb-elimination",
99 "Remove unreachable machine basic blocks");
100
101const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y;
102
103bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
104 bool changed = true;
105 bool result = false;
106
107 while (changed) {
108 changed = iterateOnFunction(F);
109 result |= changed;
110 }
111
112 if (result)
113 F.RenumberBlocks();
114
115 return result;
116}
117
118bool UnreachableMachineBlockElim::iterateOnFunction(MachineFunction &F) {
119 std::set<MachineBasicBlock*> Reachable;
120
121 // Mark all reachable blocks.
122 for (df_ext_iterator<MachineFunction*> I = df_ext_begin(&F, Reachable),
123 E = df_ext_end(&F, Reachable); I != E; ++I)
124 /* Mark all reachable blocks */;
125
126 // Loop over all dead blocks, remembering them and deleting all instructions
127 // in them.
128 std::vector<MachineBasicBlock*> DeadBlocks;
129 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I)
130 if (!Reachable.count(I)) {
131 MachineBasicBlock *BB = I;
132 DeadBlocks.push_back(BB);
133
134 while (BB->succ_begin() != BB->succ_end()) {
135 MachineBasicBlock* succ = *BB->succ_begin();
136
137 MachineBasicBlock::iterator start = succ->begin();
138 while (start != succ->end() &&
139 start->getOpcode() == TargetInstrInfo::PHI) {
140 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
141 if (start->getOperand(i).isMBB() &&
142 start->getOperand(i).getMBB() == BB) {
143 start->RemoveOperand(i);
144 start->RemoveOperand(i-1);
145 }
146
147 if (start->getNumOperands() == 1) {
148 MachineInstr* phi = start;
149 start++;
150 phi->eraseFromParent();
151 } else
152 start++;
153 }
154
155 BB->removeSuccessor(BB->succ_begin());
156 }
157 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000158
159 // Actually remove the blocks now.
160 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
Owen Andersonbd3ba462008-08-04 23:54:43 +0000161 DeadBlocks[i]->eraseFromParent();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000162
Owen Andersonbd3ba462008-08-04 23:54:43 +0000163 return DeadBlocks.size();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000164}
Owen Andersonbd3ba462008-08-04 23:54:43 +0000165