blob: 48d8ab1658da5912076b98fd6fed7d6a7cece3fb [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"
Cameron Zwarich80f6a502011-01-08 17:01:52 +000029#include "llvm/Analysis/Dominators.h"
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000030#include "llvm/Analysis/ProfileInfo.h"
Dan Gohman4f3cfba2009-08-01 00:34:30 +000031#include "llvm/CodeGen/MachineDominators.h"
Owen Andersonbd3ba462008-08-04 23:54:43 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Anton Korobeynikoved532ca2008-10-31 20:08:30 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman4f3cfba2009-08-01 00:34:30 +000034#include "llvm/CodeGen/MachineLoopInfo.h"
Owen Anderson5d2f8072008-08-05 21:40:45 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000036#include "llvm/Support/CFG.h"
Owen Andersonbd3ba462008-08-04 23:54:43 +000037#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000038#include "llvm/ADT/DepthFirstIterator.h"
Owen Andersoneaa009d2008-08-14 21:01:00 +000039#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000040using namespace llvm;
41
42namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000043 class UnreachableBlockElim : public FunctionPass {
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000044 virtual bool runOnFunction(Function &F);
Devang Patel794fd752007-05-01 21:15:47 +000045 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000046 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000047 UnreachableBlockElim() : FunctionPass(ID) {
48 initializeUnreachableBlockElimPass(*PassRegistry::getPassRegistry());
49 }
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000050
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Cameron Zwarich80f6a502011-01-08 17:01:52 +000052 AU.addPreserved<DominatorTree>();
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000053 AU.addPreserved<ProfileInfo>();
54 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000055 };
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000056}
Dan Gohman844731a2008-05-13 00:00:25 +000057char UnreachableBlockElim::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000058INITIALIZE_PASS(UnreachableBlockElim, "unreachableblockelim",
Owen Andersonce665bd2010-10-07 22:25:06 +000059 "Remove unreachable blocks from the CFG", false, false)
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000060
61FunctionPass *llvm::createUnreachableBlockEliminationPass() {
62 return new UnreachableBlockElim();
63}
64
65bool UnreachableBlockElim::runOnFunction(Function &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +000066 SmallPtrSet<BasicBlock*, 8> Reachable;
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000067
68 // Mark all reachable blocks.
Chris Lattner88698f52010-03-15 18:42:01 +000069 for (df_ext_iterator<Function*, SmallPtrSet<BasicBlock*, 8> > I =
70 df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); I != E; ++I)
71 /* Mark all reachable blocks */;
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000072
73 // Loop over all dead blocks, remembering them and deleting all instructions
74 // in them.
75 std::vector<BasicBlock*> DeadBlocks;
76 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
77 if (!Reachable.count(I)) {
Chris Lattner7f0566c2004-07-06 06:36:11 +000078 BasicBlock *BB = I;
79 DeadBlocks.push_back(BB);
80 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
Owen Andersona7235ea2009-07-31 20:28:14 +000081 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
Chris Lattner7f0566c2004-07-06 06:36:11 +000082 BB->getInstList().pop_front();
83 }
84 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
85 (*SI)->removePredecessor(BB);
86 BB->dropAllReferences();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000087 }
88
Owen Andersonbd3ba462008-08-04 23:54:43 +000089 // Actually remove the blocks now.
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000090 ProfileInfo *PI = getAnalysisIfAvailable<ProfileInfo>();
91 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
92 if (PI) PI->removeBlock(DeadBlocks[i]);
Owen Andersonbd3ba462008-08-04 23:54:43 +000093 DeadBlocks[i]->eraseFromParent();
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000094 }
Owen Andersonbd3ba462008-08-04 23:54:43 +000095
96 return DeadBlocks.size();
97}
98
99
100namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000101 class UnreachableMachineBlockElim : public MachineFunctionPass {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000102 virtual bool runOnMachineFunction(MachineFunction &F);
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000103 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000104 MachineModuleInfo *MMI;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000105 public:
106 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +0000107 UnreachableMachineBlockElim() : MachineFunctionPass(ID) {}
Owen Andersonbd3ba462008-08-04 23:54:43 +0000108 };
109}
110char UnreachableMachineBlockElim::ID = 0;
111
Owen Anderson02dd53e2010-08-23 17:52:01 +0000112INITIALIZE_PASS(UnreachableMachineBlockElim, "unreachable-mbb-elimination",
Owen Andersonce665bd2010-10-07 22:25:06 +0000113 "Remove unreachable machine basic blocks", false, false)
Owen Andersonbd3ba462008-08-04 23:54:43 +0000114
Owen Anderson90c579d2010-08-06 18:33:48 +0000115char &llvm::UnreachableMachineBlockElimID = UnreachableMachineBlockElim::ID;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000116
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000117void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
118 AU.addPreserved<MachineLoopInfo>();
119 AU.addPreserved<MachineDominatorTree>();
120 MachineFunctionPass::getAnalysisUsage(AU);
121}
122
Owen Andersonbd3ba462008-08-04 23:54:43 +0000123bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
Owen Andersoneaa009d2008-08-14 21:01:00 +0000124 SmallPtrSet<MachineBasicBlock*, 8> Reachable;
Owen Andersonb0725312010-09-29 20:57:19 +0000125 bool ModifiedPHI = false;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000126
Duncan Sands1465d612009-01-28 13:14:17 +0000127 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000128 MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
129 MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000130
Owen Andersonbd3ba462008-08-04 23:54:43 +0000131 // Mark all reachable blocks.
Owen Andersoneaa009d2008-08-14 21:01:00 +0000132 for (df_ext_iterator<MachineFunction*, SmallPtrSet<MachineBasicBlock*, 8> >
133 I = df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable);
134 I != E; ++I)
Owen Andersonbd3ba462008-08-04 23:54:43 +0000135 /* Mark all reachable blocks */;
136
137 // Loop over all dead blocks, remembering them and deleting all instructions
138 // in them.
139 std::vector<MachineBasicBlock*> DeadBlocks;
Owen Anderson9200e812008-08-06 23:16:52 +0000140 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
141 MachineBasicBlock *BB = I;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000142
Owen Anderson9200e812008-08-06 23:16:52 +0000143 // Test for deadness.
144 if (!Reachable.count(BB)) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000145 DeadBlocks.push_back(BB);
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000146
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000147 // Update dominator and loop info.
148 if (MLI) MLI->removeBlock(BB);
149 if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
150
Owen Andersonbd3ba462008-08-04 23:54:43 +0000151 while (BB->succ_begin() != BB->succ_end()) {
152 MachineBasicBlock* succ = *BB->succ_begin();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000153
Owen Andersonbd3ba462008-08-04 23:54:43 +0000154 MachineBasicBlock::iterator start = succ->begin();
Chris Lattner518bb532010-02-09 19:54:29 +0000155 while (start != succ->end() && start->isPHI()) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000156 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
Dan Gohmand735b802008-10-03 15:45:36 +0000157 if (start->getOperand(i).isMBB() &&
Owen Andersonbd3ba462008-08-04 23:54:43 +0000158 start->getOperand(i).getMBB() == BB) {
159 start->RemoveOperand(i);
160 start->RemoveOperand(i-1);
161 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000162
Owen Anderson9200e812008-08-06 23:16:52 +0000163 start++;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000164 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000165
Owen Andersonbd3ba462008-08-04 23:54:43 +0000166 BB->removeSuccessor(BB->succ_begin());
167 }
168 }
Owen Anderson9200e812008-08-06 23:16:52 +0000169 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000170
171 // Actually remove the blocks now.
Chris Lattner18589de2010-03-14 02:24:55 +0000172 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
173 DeadBlocks[i]->eraseFromParent();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000174
Owen Anderson9200e812008-08-06 23:16:52 +0000175 // Cleanup PHI nodes.
176 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
177 MachineBasicBlock *BB = I;
178 // Prune unneeded PHI entries.
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000179 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
Owen Anderson9200e812008-08-06 23:16:52 +0000180 BB->pred_end());
181 MachineBasicBlock::iterator phi = BB->begin();
Chris Lattner518bb532010-02-09 19:54:29 +0000182 while (phi != BB->end() && phi->isPHI()) {
Owen Andersonb12ab972008-08-08 18:00:05 +0000183 for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
184 if (!preds.count(phi->getOperand(i).getMBB())) {
185 phi->RemoveOperand(i);
186 phi->RemoveOperand(i-1);
Owen Andersonb0725312010-09-29 20:57:19 +0000187 ModifiedPHI = true;
Owen Andersonb12ab972008-08-08 18:00:05 +0000188 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000189
Owen Anderson9200e812008-08-06 23:16:52 +0000190 if (phi->getNumOperands() == 3) {
191 unsigned Input = phi->getOperand(1).getReg();
192 unsigned Output = phi->getOperand(0).getReg();
193
194 MachineInstr* temp = phi;
195 ++phi;
196 temp->eraseFromParent();
Owen Andersonb0725312010-09-29 20:57:19 +0000197 ModifiedPHI = true;
Owen Anderson9200e812008-08-06 23:16:52 +0000198
199 if (Input != Output)
200 F.getRegInfo().replaceRegWith(Output, Input);
Owen Andersonb12ab972008-08-08 18:00:05 +0000201
Owen Anderson9200e812008-08-06 23:16:52 +0000202 continue;
203 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000204
Owen Anderson9200e812008-08-06 23:16:52 +0000205 ++phi;
206 }
207 }
208
Owen Anderson59c0d4f2008-08-05 00:30:10 +0000209 F.RenumberBlocks();
210
Owen Andersonb0725312010-09-29 20:57:19 +0000211 return (DeadBlocks.size() || ModifiedPHI);
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000212}