blob: 152f2f84e8d72c57664edf13ac0700222b3d86c6 [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"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000035using namespace llvm;
36
37namespace {
Chris Lattner95255282006-06-28 23:17:24 +000038 class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass {
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000039 virtual bool runOnFunction(Function &F);
Devang Patel794fd752007-05-01 21:15:47 +000040 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000041 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000042 UnreachableBlockElim() : FunctionPass((intptr_t)&ID) {}
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000043 };
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000044}
Dan Gohman844731a2008-05-13 00:00:25 +000045char UnreachableBlockElim::ID = 0;
46static RegisterPass<UnreachableBlockElim>
47X("unreachableblockelim", "Remove unreachable blocks from the CFG");
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000048
49FunctionPass *llvm::createUnreachableBlockEliminationPass() {
50 return new UnreachableBlockElim();
51}
52
53bool UnreachableBlockElim::runOnFunction(Function &F) {
54 std::set<BasicBlock*> Reachable;
55
56 // Mark all reachable blocks.
57 for (df_ext_iterator<Function*> I = df_ext_begin(&F, Reachable),
58 E = df_ext_end(&F, Reachable); I != E; ++I)
59 /* Mark all reachable blocks */;
60
61 // Loop over all dead blocks, remembering them and deleting all instructions
62 // in them.
63 std::vector<BasicBlock*> DeadBlocks;
64 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
65 if (!Reachable.count(I)) {
Chris Lattner7f0566c2004-07-06 06:36:11 +000066 BasicBlock *BB = I;
67 DeadBlocks.push_back(BB);
68 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
69 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
70 BB->getInstList().pop_front();
71 }
72 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
73 (*SI)->removePredecessor(BB);
74 BB->dropAllReferences();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000075 }
76
Owen Andersonbd3ba462008-08-04 23:54:43 +000077 // Actually remove the blocks now.
78 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
79 DeadBlocks[i]->eraseFromParent();
80
81 return DeadBlocks.size();
82}
83
84
85namespace {
86 class VISIBILITY_HIDDEN UnreachableMachineBlockElim :
87 public MachineFunctionPass {
88 virtual bool runOnMachineFunction(MachineFunction &F);
Owen Andersonbd3ba462008-08-04 23:54:43 +000089
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) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000104 std::set<MachineBasicBlock*> Reachable;
105
106 // Mark all reachable blocks.
107 for (df_ext_iterator<MachineFunction*> I = df_ext_begin(&F, Reachable),
108 E = df_ext_end(&F, Reachable); I != E; ++I)
109 /* Mark all reachable blocks */;
110
111 // Loop over all dead blocks, remembering them and deleting all instructions
112 // in them.
113 std::vector<MachineBasicBlock*> DeadBlocks;
Owen Anderson9200e812008-08-06 23:16:52 +0000114 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
115 MachineBasicBlock *BB = I;
116
117 // Test for deadness.
118 if (!Reachable.count(BB)) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000119 DeadBlocks.push_back(BB);
120
121 while (BB->succ_begin() != BB->succ_end()) {
122 MachineBasicBlock* succ = *BB->succ_begin();
123
124 MachineBasicBlock::iterator start = succ->begin();
125 while (start != succ->end() &&
126 start->getOpcode() == TargetInstrInfo::PHI) {
127 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
128 if (start->getOperand(i).isMBB() &&
129 start->getOperand(i).getMBB() == BB) {
130 start->RemoveOperand(i);
131 start->RemoveOperand(i-1);
132 }
133
Owen Anderson9200e812008-08-06 23:16:52 +0000134 start++;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000135 }
136
137 BB->removeSuccessor(BB->succ_begin());
138 }
139 }
Owen Anderson9200e812008-08-06 23:16:52 +0000140 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000141
142 // Actually remove the blocks now.
143 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
Owen Andersonbd3ba462008-08-04 23:54:43 +0000144 DeadBlocks[i]->eraseFromParent();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000145
Owen Anderson9200e812008-08-06 23:16:52 +0000146 // Cleanup PHI nodes.
147 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
148 MachineBasicBlock *BB = I;
149 // Prune unneeded PHI entries.
150 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
151 BB->pred_end());
152 MachineBasicBlock::iterator phi = BB->begin();
153 while (phi != BB->end() &&
154 phi->getOpcode() == TargetInstrInfo::PHI) {
Owen Andersonb12ab972008-08-08 18:00:05 +0000155 for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
156 if (!preds.count(phi->getOperand(i).getMBB())) {
157 phi->RemoveOperand(i);
158 phi->RemoveOperand(i-1);
159 }
160
Owen Anderson9200e812008-08-06 23:16:52 +0000161 if (phi->getNumOperands() == 3) {
162 unsigned Input = phi->getOperand(1).getReg();
163 unsigned Output = phi->getOperand(0).getReg();
164
165 MachineInstr* temp = phi;
166 ++phi;
167 temp->eraseFromParent();
168
169 if (Input != Output)
170 F.getRegInfo().replaceRegWith(Output, Input);
Owen Andersonb12ab972008-08-08 18:00:05 +0000171
Owen Anderson9200e812008-08-06 23:16:52 +0000172 continue;
173 }
Owen Anderson9200e812008-08-06 23:16:52 +0000174
175 ++phi;
176 }
177 }
178
Owen Anderson59c0d4f2008-08-05 00:30:10 +0000179 F.RenumberBlocks();
180
Owen Andersonbd3ba462008-08-04 23:54:43 +0000181 return DeadBlocks.size();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000182}
Owen Andersonbd3ba462008-08-04 23:54:43 +0000183