1 //===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // 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.
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
21 //===----------------------------------------------------------------------===//
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/ADT/DepthFirstIterator.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/CodeGen/MachineDominators.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineLoopInfo.h"
29 #include "llvm/CodeGen/MachineModuleInfo.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/IR/CFG.h"
32 #include "llvm/IR/Constant.h"
33 #include "llvm/IR/Dominators.h"
34 #include "llvm/IR/Function.h"
35 #include "llvm/IR/Instructions.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/Pass.h"
38 #include "llvm/Target/TargetInstrInfo.h"
42 class UnreachableBlockElim : public FunctionPass {
43 bool runOnFunction(Function &F) override;
45 static char ID; // Pass identification, replacement for typeid
46 UnreachableBlockElim() : FunctionPass(ID) {
47 initializeUnreachableBlockElimPass(*PassRegistry::getPassRegistry());
50 void getAnalysisUsage(AnalysisUsage &AU) const override {
51 AU.addPreserved<DominatorTreeWrapperPass>();
55 char UnreachableBlockElim::ID = 0;
56 INITIALIZE_PASS(UnreachableBlockElim, "unreachableblockelim",
57 "Remove unreachable blocks from the CFG", false, false)
59 FunctionPass *llvm::createUnreachableBlockEliminationPass() {
60 return new UnreachableBlockElim();
63 bool UnreachableBlockElim::runOnFunction(Function &F) {
64 SmallPtrSet<BasicBlock*, 8> Reachable;
66 // Mark all reachable blocks.
67 for (BasicBlock *BB : depth_first_ext(&F, Reachable))
68 (void)BB/* Mark all reachable blocks */;
70 // Loop over all dead blocks, remembering them and deleting all instructions
72 std::vector<BasicBlock*> DeadBlocks;
73 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
74 if (!Reachable.count(I)) {
76 DeadBlocks.push_back(BB);
77 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
78 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
79 BB->getInstList().pop_front();
81 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
82 (*SI)->removePredecessor(BB);
83 BB->dropAllReferences();
86 // Actually remove the blocks now.
87 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
88 DeadBlocks[i]->eraseFromParent();
91 return DeadBlocks.size();
96 class UnreachableMachineBlockElim : public MachineFunctionPass {
97 bool runOnMachineFunction(MachineFunction &F) override;
98 void getAnalysisUsage(AnalysisUsage &AU) const override;
99 MachineModuleInfo *MMI;
101 static char ID; // Pass identification, replacement for typeid
102 UnreachableMachineBlockElim() : MachineFunctionPass(ID) {}
105 char UnreachableMachineBlockElim::ID = 0;
107 INITIALIZE_PASS(UnreachableMachineBlockElim, "unreachable-mbb-elimination",
108 "Remove unreachable machine basic blocks", false, false)
110 char &llvm::UnreachableMachineBlockElimID = UnreachableMachineBlockElim::ID;
112 void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
113 AU.addPreserved<MachineLoopInfo>();
114 AU.addPreserved<MachineDominatorTree>();
115 MachineFunctionPass::getAnalysisUsage(AU);
118 bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
119 SmallPtrSet<MachineBasicBlock*, 8> Reachable;
120 bool ModifiedPHI = false;
122 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
123 MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
124 MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
126 // Mark all reachable blocks.
127 for (MachineBasicBlock *BB : depth_first_ext(&F, Reachable))
128 (void)BB/* Mark all reachable blocks */;
130 // Loop over all dead blocks, remembering them and deleting all instructions
132 std::vector<MachineBasicBlock*> DeadBlocks;
133 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
134 MachineBasicBlock *BB = I;
136 // Test for deadness.
137 if (!Reachable.count(BB)) {
138 DeadBlocks.push_back(BB);
140 // Update dominator and loop info.
141 if (MLI) MLI->removeBlock(BB);
142 if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
144 while (BB->succ_begin() != BB->succ_end()) {
145 MachineBasicBlock* succ = *BB->succ_begin();
147 MachineBasicBlock::iterator start = succ->begin();
148 while (start != succ->end() && start->isPHI()) {
149 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
150 if (start->getOperand(i).isMBB() &&
151 start->getOperand(i).getMBB() == BB) {
152 start->RemoveOperand(i);
153 start->RemoveOperand(i-1);
159 BB->removeSuccessor(BB->succ_begin());
164 // Actually remove the blocks now.
165 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
166 DeadBlocks[i]->eraseFromParent();
168 // Cleanup PHI nodes.
169 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
170 MachineBasicBlock *BB = I;
171 // Prune unneeded PHI entries.
172 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
174 MachineBasicBlock::iterator phi = BB->begin();
175 while (phi != BB->end() && phi->isPHI()) {
176 for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
177 if (!preds.count(phi->getOperand(i).getMBB())) {
178 phi->RemoveOperand(i);
179 phi->RemoveOperand(i-1);
183 if (phi->getNumOperands() == 3) {
184 unsigned Input = phi->getOperand(1).getReg();
185 unsigned Output = phi->getOperand(0).getReg();
187 MachineInstr* temp = phi;
189 temp->eraseFromParent();
192 if (Input != Output) {
193 MachineRegisterInfo &MRI = F.getRegInfo();
194 MRI.constrainRegClass(Input, MRI.getRegClass(Output));
195 MRI.replaceRegWith(Output, Input);
207 return (DeadBlocks.size() || ModifiedPHI);