Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 1 | //===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 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. |
| 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 Lattner | 7f0566c | 2004-07-06 06:36:11 +0000 | [diff] [blame] | 24 | #include "llvm/Constant.h" |
Chris Lattner | 879313a | 2004-07-29 17:23:00 +0000 | [diff] [blame] | 25 | #include "llvm/Instructions.h" |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 26 | #include "llvm/Function.h" |
| 27 | #include "llvm/Pass.h" |
Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 28 | #include "llvm/Type.h" |
Andreas Neustifter | ff5dfdf | 2009-09-09 17:53:39 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/ProfileInfo.h" |
Dan Gohman | 4f3cfba | 2009-08-01 00:34:30 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineDominators.h" |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Anton Korobeynikov | ed532ca | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Dan Gohman | 4f3cfba | 2009-08-01 00:34:30 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Owen Anderson | 5d2f807 | 2008-08-05 21:40:45 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CFG.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Compiler.h" |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetInstrInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/DepthFirstIterator.h" |
Owen Anderson | eaa009d | 2008-08-14 21:01:00 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
| 42 | namespace { |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 43 | class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass { |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 44 | virtual bool runOnFunction(Function &F); |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 45 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 46 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 47 | UnreachableBlockElim() : FunctionPass(&ID) {} |
Andreas Neustifter | ff5dfdf | 2009-09-09 17:53:39 +0000 | [diff] [blame] | 48 | |
| 49 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 50 | AU.addPreserved<ProfileInfo>(); |
| 51 | } |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 52 | }; |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 53 | } |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 54 | char UnreachableBlockElim::ID = 0; |
| 55 | static RegisterPass<UnreachableBlockElim> |
| 56 | X("unreachableblockelim", "Remove unreachable blocks from the CFG"); |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 57 | |
| 58 | FunctionPass *llvm::createUnreachableBlockEliminationPass() { |
| 59 | return new UnreachableBlockElim(); |
| 60 | } |
| 61 | |
| 62 | bool UnreachableBlockElim::runOnFunction(Function &F) { |
Owen Anderson | eaa009d | 2008-08-14 21:01:00 +0000 | [diff] [blame] | 63 | SmallPtrSet<BasicBlock*, 8> Reachable; |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 64 | |
| 65 | // Mark all reachable blocks. |
Owen Anderson | eaa009d | 2008-08-14 21:01:00 +0000 | [diff] [blame] | 66 | for (df_ext_iterator<Function*, SmallPtrSet<BasicBlock*, 8> > I = |
| 67 | df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); I != E; ++I) |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 68 | /* Mark all reachable blocks */; |
| 69 | |
| 70 | // Loop over all dead blocks, remembering them and deleting all instructions |
| 71 | // in them. |
| 72 | std::vector<BasicBlock*> DeadBlocks; |
| 73 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
| 74 | if (!Reachable.count(I)) { |
Chris Lattner | 7f0566c | 2004-07-06 06:36:11 +0000 | [diff] [blame] | 75 | BasicBlock *BB = I; |
| 76 | DeadBlocks.push_back(BB); |
| 77 | while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 78 | PN->replaceAllUsesWith(Constant::getNullValue(PN->getType())); |
Chris Lattner | 7f0566c | 2004-07-06 06:36:11 +0000 | [diff] [blame] | 79 | BB->getInstList().pop_front(); |
| 80 | } |
| 81 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 82 | (*SI)->removePredecessor(BB); |
| 83 | BB->dropAllReferences(); |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 86 | // Actually remove the blocks now. |
Andreas Neustifter | ff5dfdf | 2009-09-09 17:53:39 +0000 | [diff] [blame] | 87 | ProfileInfo *PI = getAnalysisIfAvailable<ProfileInfo>(); |
| 88 | for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) { |
| 89 | if (PI) PI->removeBlock(DeadBlocks[i]); |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 90 | DeadBlocks[i]->eraseFromParent(); |
Andreas Neustifter | ff5dfdf | 2009-09-09 17:53:39 +0000 | [diff] [blame] | 91 | } |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 92 | |
| 93 | return DeadBlocks.size(); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | namespace { |
Anton Korobeynikov | ed532ca | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 98 | class VISIBILITY_HIDDEN UnreachableMachineBlockElim : |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 99 | public MachineFunctionPass { |
| 100 | virtual bool runOnMachineFunction(MachineFunction &F); |
Dan Gohman | 4f3cfba | 2009-08-01 00:34:30 +0000 | [diff] [blame] | 101 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
Anton Korobeynikov | ed532ca | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 102 | MachineModuleInfo *MMI; |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 103 | public: |
| 104 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 105 | UnreachableMachineBlockElim() : MachineFunctionPass(&ID) {} |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 106 | }; |
| 107 | } |
| 108 | char UnreachableMachineBlockElim::ID = 0; |
| 109 | |
| 110 | static RegisterPass<UnreachableMachineBlockElim> |
| 111 | Y("unreachable-mbb-elimination", |
| 112 | "Remove unreachable machine basic blocks"); |
| 113 | |
| 114 | const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y; |
| 115 | |
Dan Gohman | 4f3cfba | 2009-08-01 00:34:30 +0000 | [diff] [blame] | 116 | void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const { |
| 117 | AU.addPreserved<MachineLoopInfo>(); |
| 118 | AU.addPreserved<MachineDominatorTree>(); |
| 119 | MachineFunctionPass::getAnalysisUsage(AU); |
| 120 | } |
| 121 | |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 122 | bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) { |
Owen Anderson | eaa009d | 2008-08-14 21:01:00 +0000 | [diff] [blame] | 123 | SmallPtrSet<MachineBasicBlock*, 8> Reachable; |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 124 | |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 125 | MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
Dan Gohman | 4f3cfba | 2009-08-01 00:34:30 +0000 | [diff] [blame] | 126 | MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>(); |
| 127 | MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>(); |
Anton Korobeynikov | ed532ca | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 128 | |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 129 | // Mark all reachable blocks. |
Owen Anderson | eaa009d | 2008-08-14 21:01:00 +0000 | [diff] [blame] | 130 | 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 Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 133 | /* 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 Anderson | 9200e81 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 138 | for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 139 | MachineBasicBlock *BB = I; |
Anton Korobeynikov | ed532ca | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 140 | |
Owen Anderson | 9200e81 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 141 | // Test for deadness. |
| 142 | if (!Reachable.count(BB)) { |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 143 | DeadBlocks.push_back(BB); |
Anton Korobeynikov | ed532ca | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 144 | |
Dan Gohman | 4f3cfba | 2009-08-01 00:34:30 +0000 | [diff] [blame] | 145 | // Update dominator and loop info. |
| 146 | if (MLI) MLI->removeBlock(BB); |
| 147 | if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB); |
| 148 | |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 149 | while (BB->succ_begin() != BB->succ_end()) { |
| 150 | MachineBasicBlock* succ = *BB->succ_begin(); |
Anton Korobeynikov | ed532ca | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 151 | |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 152 | MachineBasicBlock::iterator start = succ->begin(); |
| 153 | while (start != succ->end() && |
| 154 | start->getOpcode() == TargetInstrInfo::PHI) { |
| 155 | for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2) |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 156 | if (start->getOperand(i).isMBB() && |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 157 | start->getOperand(i).getMBB() == BB) { |
| 158 | start->RemoveOperand(i); |
| 159 | start->RemoveOperand(i-1); |
| 160 | } |
Anton Korobeynikov | ed532ca | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 161 | |
Owen Anderson | 9200e81 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 162 | start++; |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 163 | } |
Anton Korobeynikov | ed532ca | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 164 | |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 165 | BB->removeSuccessor(BB->succ_begin()); |
| 166 | } |
| 167 | } |
Owen Anderson | 9200e81 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 168 | } |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 169 | |
| 170 | // Actually remove the blocks now. |
Anton Korobeynikov | ed532ca | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 171 | for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) { |
| 172 | MachineBasicBlock *MBB = DeadBlocks[i]; |
| 173 | // If there are any labels in the basic block, unregister them from |
| 174 | // MachineModuleInfo. |
| 175 | if (MMI && !MBB->empty()) { |
| 176 | for (MachineBasicBlock::iterator I = MBB->begin(), |
| 177 | E = MBB->end(); I != E; ++I) { |
| 178 | if (I->isLabel()) |
| 179 | // The label ID # is always operand #0, an immediate. |
| 180 | MMI->InvalidateLabel(I->getOperand(0).getImm()); |
| 181 | } |
| 182 | } |
| 183 | MBB->eraseFromParent(); |
| 184 | } |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 185 | |
Owen Anderson | 9200e81 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 186 | // Cleanup PHI nodes. |
| 187 | for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 188 | MachineBasicBlock *BB = I; |
| 189 | // Prune unneeded PHI entries. |
Anton Korobeynikov | ed532ca | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 190 | SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(), |
Owen Anderson | 9200e81 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 191 | BB->pred_end()); |
| 192 | MachineBasicBlock::iterator phi = BB->begin(); |
| 193 | while (phi != BB->end() && |
| 194 | phi->getOpcode() == TargetInstrInfo::PHI) { |
Owen Anderson | b12ab97 | 2008-08-08 18:00:05 +0000 | [diff] [blame] | 195 | for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2) |
| 196 | if (!preds.count(phi->getOperand(i).getMBB())) { |
| 197 | phi->RemoveOperand(i); |
| 198 | phi->RemoveOperand(i-1); |
| 199 | } |
Anton Korobeynikov | ed532ca | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 200 | |
Owen Anderson | 9200e81 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 201 | if (phi->getNumOperands() == 3) { |
| 202 | unsigned Input = phi->getOperand(1).getReg(); |
| 203 | unsigned Output = phi->getOperand(0).getReg(); |
| 204 | |
| 205 | MachineInstr* temp = phi; |
| 206 | ++phi; |
| 207 | temp->eraseFromParent(); |
| 208 | |
| 209 | if (Input != Output) |
| 210 | F.getRegInfo().replaceRegWith(Output, Input); |
Owen Anderson | b12ab97 | 2008-08-08 18:00:05 +0000 | [diff] [blame] | 211 | |
Owen Anderson | 9200e81 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 212 | continue; |
| 213 | } |
Anton Korobeynikov | ed532ca | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 214 | |
Owen Anderson | 9200e81 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 215 | ++phi; |
| 216 | } |
| 217 | } |
| 218 | |
Owen Anderson | 59c0d4f | 2008-08-05 00:30:10 +0000 | [diff] [blame] | 219 | F.RenumberBlocks(); |
| 220 | |
Owen Anderson | bd3ba46 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 221 | return DeadBlocks.size(); |
Chris Lattner | fc3c82a | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 222 | } |