blob: 4fdb22882471a67211e27a09f0df3ecf01e8ed91 [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"
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000029#include "llvm/Analysis/ProfileInfo.h"
Dan Gohman4f3cfba2009-08-01 00:34:30 +000030#include "llvm/CodeGen/MachineDominators.h"
Owen Andersonbd3ba462008-08-04 23:54:43 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Anton Korobeynikoved532ca2008-10-31 20:08:30 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman4f3cfba2009-08-01 00:34:30 +000033#include "llvm/CodeGen/MachineLoopInfo.h"
Owen Anderson5d2f8072008-08-05 21:40:45 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000035#include "llvm/Support/CFG.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 {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000042 class 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
Owen Anderson081c34b2010-10-19 17:21:58 +000046 UnreachableBlockElim() : FunctionPass(ID) {
47 initializeUnreachableBlockElimPass(*PassRegistry::getPassRegistry());
48 }
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000049
50 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
51 AU.addPreserved<ProfileInfo>();
52 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000053 };
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000054}
Dan Gohman844731a2008-05-13 00:00:25 +000055char UnreachableBlockElim::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000056INITIALIZE_PASS(UnreachableBlockElim, "unreachableblockelim",
Owen Andersonce665bd2010-10-07 22:25:06 +000057 "Remove unreachable blocks from the CFG", false, false)
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000058
59FunctionPass *llvm::createUnreachableBlockEliminationPass() {
60 return new UnreachableBlockElim();
61}
62
63bool UnreachableBlockElim::runOnFunction(Function &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +000064 SmallPtrSet<BasicBlock*, 8> Reachable;
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000065
66 // Mark all reachable blocks.
Chris Lattner88698f52010-03-15 18:42:01 +000067 for (df_ext_iterator<Function*, SmallPtrSet<BasicBlock*, 8> > I =
68 df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); I != E; ++I)
69 /* Mark all reachable blocks */;
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000070
71 // Loop over all dead blocks, remembering them and deleting all instructions
72 // in them.
73 std::vector<BasicBlock*> DeadBlocks;
74 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
75 if (!Reachable.count(I)) {
Chris Lattner7f0566c2004-07-06 06:36:11 +000076 BasicBlock *BB = I;
77 DeadBlocks.push_back(BB);
78 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
Owen Andersona7235ea2009-07-31 20:28:14 +000079 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
Chris Lattner7f0566c2004-07-06 06:36:11 +000080 BB->getInstList().pop_front();
81 }
82 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
83 (*SI)->removePredecessor(BB);
84 BB->dropAllReferences();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000085 }
86
Owen Andersonbd3ba462008-08-04 23:54:43 +000087 // Actually remove the blocks now.
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000088 ProfileInfo *PI = getAnalysisIfAvailable<ProfileInfo>();
89 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
90 if (PI) PI->removeBlock(DeadBlocks[i]);
Owen Andersonbd3ba462008-08-04 23:54:43 +000091 DeadBlocks[i]->eraseFromParent();
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000092 }
Owen Andersonbd3ba462008-08-04 23:54:43 +000093
94 return DeadBlocks.size();
95}
96
97
98namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000099 class UnreachableMachineBlockElim : public MachineFunctionPass {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000100 virtual bool runOnMachineFunction(MachineFunction &F);
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000101 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000102 MachineModuleInfo *MMI;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000103 public:
104 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +0000105 UnreachableMachineBlockElim() : MachineFunctionPass(ID) {}
Owen Andersonbd3ba462008-08-04 23:54:43 +0000106 };
107}
108char UnreachableMachineBlockElim::ID = 0;
109
Owen Anderson02dd53e2010-08-23 17:52:01 +0000110INITIALIZE_PASS(UnreachableMachineBlockElim, "unreachable-mbb-elimination",
Owen Andersonce665bd2010-10-07 22:25:06 +0000111 "Remove unreachable machine basic blocks", false, false)
Owen Andersonbd3ba462008-08-04 23:54:43 +0000112
Owen Anderson90c579d2010-08-06 18:33:48 +0000113char &llvm::UnreachableMachineBlockElimID = UnreachableMachineBlockElim::ID;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000114
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000115void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
116 AU.addPreserved<MachineLoopInfo>();
117 AU.addPreserved<MachineDominatorTree>();
118 MachineFunctionPass::getAnalysisUsage(AU);
119}
120
Owen Andersonbd3ba462008-08-04 23:54:43 +0000121bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +0000122 SmallPtrSet<MachineBasicBlock*, 8> Reachable;
Owen Andersonb0725312010-09-29 20:57:19 +0000123 bool ModifiedPHI = false;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000124
Duncan Sands1465d612009-01-28 13:14:17 +0000125 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000126 MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
127 MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000128
Owen Andersonbd3ba462008-08-04 23:54:43 +0000129 // Mark all reachable blocks.
Owen Andersoneaa009d2008-08-14 21:01:00 +0000130 for (df_ext_iterator<MachineFunction*, SmallPtrSet<MachineBasicBlock*, 8> >
131 I = df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable);
132 I != E; ++I)
Owen Andersonbd3ba462008-08-04 23:54:43 +0000133 /* Mark all reachable blocks */;
134
135 // Loop over all dead blocks, remembering them and deleting all instructions
136 // in them.
137 std::vector<MachineBasicBlock*> DeadBlocks;
Owen Anderson9200e812008-08-06 23:16:52 +0000138 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
139 MachineBasicBlock *BB = I;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000140
Owen Anderson9200e812008-08-06 23:16:52 +0000141 // Test for deadness.
142 if (!Reachable.count(BB)) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000143 DeadBlocks.push_back(BB);
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000144
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000145 // Update dominator and loop info.
146 if (MLI) MLI->removeBlock(BB);
147 if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
148
Owen Andersonbd3ba462008-08-04 23:54:43 +0000149 while (BB->succ_begin() != BB->succ_end()) {
150 MachineBasicBlock* succ = *BB->succ_begin();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000151
Owen Andersonbd3ba462008-08-04 23:54:43 +0000152 MachineBasicBlock::iterator start = succ->begin();
Chris Lattner518bb532010-02-09 19:54:29 +0000153 while (start != succ->end() && start->isPHI()) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000154 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
Dan Gohmand735b802008-10-03 15:45:36 +0000155 if (start->getOperand(i).isMBB() &&
Owen Andersonbd3ba462008-08-04 23:54:43 +0000156 start->getOperand(i).getMBB() == BB) {
157 start->RemoveOperand(i);
158 start->RemoveOperand(i-1);
159 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000160
Owen Anderson9200e812008-08-06 23:16:52 +0000161 start++;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000162 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000163
Owen Andersonbd3ba462008-08-04 23:54:43 +0000164 BB->removeSuccessor(BB->succ_begin());
165 }
166 }
Owen Anderson9200e812008-08-06 23:16:52 +0000167 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000168
169 // Actually remove the blocks now.
Chris Lattner18589de2010-03-14 02:24:55 +0000170 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
171 DeadBlocks[i]->eraseFromParent();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000172
Owen Anderson9200e812008-08-06 23:16:52 +0000173 // Cleanup PHI nodes.
174 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
175 MachineBasicBlock *BB = I;
176 // Prune unneeded PHI entries.
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000177 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
Owen Anderson9200e812008-08-06 23:16:52 +0000178 BB->pred_end());
179 MachineBasicBlock::iterator phi = BB->begin();
Chris Lattner518bb532010-02-09 19:54:29 +0000180 while (phi != BB->end() && phi->isPHI()) {
Owen Andersonb12ab972008-08-08 18:00:05 +0000181 for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
182 if (!preds.count(phi->getOperand(i).getMBB())) {
183 phi->RemoveOperand(i);
184 phi->RemoveOperand(i-1);
Owen Andersonb0725312010-09-29 20:57:19 +0000185 ModifiedPHI = true;
Owen Andersonb12ab972008-08-08 18:00:05 +0000186 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000187
Owen Anderson9200e812008-08-06 23:16:52 +0000188 if (phi->getNumOperands() == 3) {
189 unsigned Input = phi->getOperand(1).getReg();
190 unsigned Output = phi->getOperand(0).getReg();
191
192 MachineInstr* temp = phi;
193 ++phi;
194 temp->eraseFromParent();
Owen Andersonb0725312010-09-29 20:57:19 +0000195 ModifiedPHI = true;
Owen Anderson9200e812008-08-06 23:16:52 +0000196
197 if (Input != Output)
198 F.getRegInfo().replaceRegWith(Output, Input);
Owen Andersonb12ab972008-08-08 18:00:05 +0000199
Owen Anderson9200e812008-08-06 23:16:52 +0000200 continue;
201 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000202
Owen Anderson9200e812008-08-06 23:16:52 +0000203 ++phi;
204 }
205 }
206
Owen Anderson59c0d4f2008-08-05 00:30:10 +0000207 F.RenumberBlocks();
208
Owen Andersonb0725312010-09-29 20:57:19 +0000209 return (DeadBlocks.size() || ModifiedPHI);
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000210}