Chris Lattner | 693244f | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 1 | //===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===// |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 693244f | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 693244f | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 693244f | 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 | |
Wei Mi | 90d195a | 2016-07-08 03:32:49 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/UnreachableBlockElim.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DepthFirstIterator.h" |
| 25 | #include "llvm/ADT/SmallPtrSet.h" |
Dan Gohman | ac31be1 | 2009-08-01 00:34:30 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineDominators.h" |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Dan Gohman | ac31be1 | 2009-08-01 00:34:30 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Owen Anderson | bdaed55 | 2008-08-05 21:40:45 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Wei Mi | 90d195a | 2016-07-08 03:32:49 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 32 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Constant.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Function.h" |
| 36 | #include "llvm/IR/Instructions.h" |
| 37 | #include "llvm/IR/Type.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 38 | #include "llvm/Pass.h" |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | 693244f | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
Wei Mi | 90d195a | 2016-07-08 03:32:49 +0000 | [diff] [blame] | 42 | static bool eliminateUnreachableBlock(Function &F) { |
David Callahan | c1051ab | 2016-10-05 21:36:16 +0000 | [diff] [blame] | 43 | df_iterator_default_set<BasicBlock*> Reachable; |
Chris Lattner | 693244f | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 44 | |
| 45 | // Mark all reachable blocks. |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 46 | for (BasicBlock *BB : depth_first_ext(&F, Reachable)) |
| 47 | (void)BB/* Mark all reachable blocks */; |
Chris Lattner | 693244f | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 48 | |
| 49 | // Loop over all dead blocks, remembering them and deleting all instructions |
| 50 | // in them. |
| 51 | std::vector<BasicBlock*> DeadBlocks; |
| 52 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 53 | if (!Reachable.count(&*I)) { |
| 54 | BasicBlock *BB = &*I; |
Chris Lattner | 520df84 | 2004-07-06 06:36:11 +0000 | [diff] [blame] | 55 | DeadBlocks.push_back(BB); |
| 56 | while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 57 | PN->replaceAllUsesWith(Constant::getNullValue(PN->getType())); |
Chris Lattner | 520df84 | 2004-07-06 06:36:11 +0000 | [diff] [blame] | 58 | BB->getInstList().pop_front(); |
| 59 | } |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 60 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 61 | (*SI)->removePredecessor(BB); |
Chris Lattner | 520df84 | 2004-07-06 06:36:11 +0000 | [diff] [blame] | 62 | BB->dropAllReferences(); |
Chris Lattner | 693244f | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 65 | // Actually remove the blocks now. |
Andreas Neustifter | 4c0b284 | 2009-09-09 17:53:39 +0000 | [diff] [blame] | 66 | for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) { |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 67 | DeadBlocks[i]->eraseFromParent(); |
Andreas Neustifter | 4c0b284 | 2009-09-09 17:53:39 +0000 | [diff] [blame] | 68 | } |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 69 | |
Alexander Kornienko | 8c0809c | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 70 | return !DeadBlocks.empty(); |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Wei Mi | 90d195a | 2016-07-08 03:32:49 +0000 | [diff] [blame] | 73 | namespace { |
| 74 | class UnreachableBlockElimLegacyPass : public FunctionPass { |
| 75 | bool runOnFunction(Function &F) override { |
| 76 | return eliminateUnreachableBlock(F); |
| 77 | } |
| 78 | |
| 79 | public: |
| 80 | static char ID; // Pass identification, replacement for typeid |
| 81 | UnreachableBlockElimLegacyPass() : FunctionPass(ID) { |
| 82 | initializeUnreachableBlockElimLegacyPassPass( |
| 83 | *PassRegistry::getPassRegistry()); |
| 84 | } |
| 85 | |
| 86 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 87 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| 88 | } |
| 89 | }; |
| 90 | } |
| 91 | char UnreachableBlockElimLegacyPass::ID = 0; |
| 92 | INITIALIZE_PASS(UnreachableBlockElimLegacyPass, "unreachableblockelim", |
| 93 | "Remove unreachable blocks from the CFG", false, false) |
| 94 | |
| 95 | FunctionPass *llvm::createUnreachableBlockEliminationPass() { |
| 96 | return new UnreachableBlockElimLegacyPass(); |
| 97 | } |
| 98 | |
| 99 | PreservedAnalyses UnreachableBlockElimPass::run(Function &F, |
| 100 | FunctionAnalysisManager &AM) { |
| 101 | bool Changed = eliminateUnreachableBlock(F); |
| 102 | if (!Changed) |
| 103 | return PreservedAnalyses::all(); |
| 104 | PreservedAnalyses PA; |
| 105 | PA.preserve<DominatorTreeAnalysis>(); |
| 106 | return PA; |
| 107 | } |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 108 | |
| 109 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 110 | class UnreachableMachineBlockElim : public MachineFunctionPass { |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 111 | bool runOnMachineFunction(MachineFunction &F) override; |
| 112 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Anton Korobeynikov | 7c5a01f | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 113 | MachineModuleInfo *MMI; |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 114 | public: |
| 115 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 116 | UnreachableMachineBlockElim() : MachineFunctionPass(ID) {} |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 117 | }; |
| 118 | } |
| 119 | char UnreachableMachineBlockElim::ID = 0; |
| 120 | |
Owen Anderson | d31d82d | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 121 | INITIALIZE_PASS(UnreachableMachineBlockElim, "unreachable-mbb-elimination", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 122 | "Remove unreachable machine basic blocks", false, false) |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 123 | |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 124 | char &llvm::UnreachableMachineBlockElimID = UnreachableMachineBlockElim::ID; |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 125 | |
Dan Gohman | ac31be1 | 2009-08-01 00:34:30 +0000 | [diff] [blame] | 126 | void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const { |
| 127 | AU.addPreserved<MachineLoopInfo>(); |
| 128 | AU.addPreserved<MachineDominatorTree>(); |
| 129 | MachineFunctionPass::getAnalysisUsage(AU); |
| 130 | } |
| 131 | |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 132 | bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) { |
David Callahan | c1051ab | 2016-10-05 21:36:16 +0000 | [diff] [blame] | 133 | df_iterator_default_set<MachineBasicBlock*> Reachable; |
Owen Anderson | 0cd5224 | 2010-09-29 20:57:19 +0000 | [diff] [blame] | 134 | bool ModifiedPHI = false; |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 135 | |
Duncan Sands | 5a913d6 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 136 | MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
Dan Gohman | ac31be1 | 2009-08-01 00:34:30 +0000 | [diff] [blame] | 137 | MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>(); |
| 138 | MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>(); |
Anton Korobeynikov | 7c5a01f | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 139 | |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 140 | // Mark all reachable blocks. |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 141 | for (MachineBasicBlock *BB : depth_first_ext(&F, Reachable)) |
| 142 | (void)BB/* Mark all reachable blocks */; |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 143 | |
| 144 | // Loop over all dead blocks, remembering them and deleting all instructions |
| 145 | // in them. |
| 146 | std::vector<MachineBasicBlock*> DeadBlocks; |
Owen Anderson | c6d5270 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 147 | for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 148 | MachineBasicBlock *BB = &*I; |
Anton Korobeynikov | 7c5a01f | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 149 | |
Owen Anderson | c6d5270 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 150 | // Test for deadness. |
| 151 | if (!Reachable.count(BB)) { |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 152 | DeadBlocks.push_back(BB); |
Anton Korobeynikov | 7c5a01f | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 153 | |
Dan Gohman | ac31be1 | 2009-08-01 00:34:30 +0000 | [diff] [blame] | 154 | // Update dominator and loop info. |
| 155 | if (MLI) MLI->removeBlock(BB); |
| 156 | if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB); |
| 157 | |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 158 | while (BB->succ_begin() != BB->succ_end()) { |
| 159 | MachineBasicBlock* succ = *BB->succ_begin(); |
Anton Korobeynikov | 7c5a01f | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 160 | |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 161 | MachineBasicBlock::iterator start = succ->begin(); |
Chris Lattner | b06015a | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 162 | while (start != succ->end() && start->isPHI()) { |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 163 | for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2) |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 164 | if (start->getOperand(i).isMBB() && |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 165 | start->getOperand(i).getMBB() == BB) { |
| 166 | start->RemoveOperand(i); |
| 167 | start->RemoveOperand(i-1); |
| 168 | } |
Anton Korobeynikov | 7c5a01f | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 169 | |
Owen Anderson | c6d5270 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 170 | start++; |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 171 | } |
Anton Korobeynikov | 7c5a01f | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 172 | |
Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 173 | BB->removeSuccessor(BB->succ_begin()); |
| 174 | } |
| 175 | } |
Owen Anderson | c6d5270 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 176 | } |
Chris Lattner | 693244f | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 177 | |
| 178 | // Actually remove the blocks now. |
Chris Lattner | 07293da | 2010-03-14 02:24:55 +0000 | [diff] [blame] | 179 | for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) |
| 180 | DeadBlocks[i]->eraseFromParent(); |
Chris Lattner | 693244f | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 181 | |
Owen Anderson | c6d5270 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 182 | // Cleanup PHI nodes. |
| 183 | for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 184 | MachineBasicBlock *BB = &*I; |
Owen Anderson | c6d5270 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 185 | // Prune unneeded PHI entries. |
Anton Korobeynikov | 7c5a01f | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 186 | SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(), |
Owen Anderson | c6d5270 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 187 | BB->pred_end()); |
| 188 | MachineBasicBlock::iterator phi = BB->begin(); |
Chris Lattner | b06015a | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 189 | while (phi != BB->end() && phi->isPHI()) { |
Owen Anderson | dfb0b69 | 2008-08-08 18:00:05 +0000 | [diff] [blame] | 190 | for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2) |
| 191 | if (!preds.count(phi->getOperand(i).getMBB())) { |
| 192 | phi->RemoveOperand(i); |
| 193 | phi->RemoveOperand(i-1); |
Owen Anderson | 0cd5224 | 2010-09-29 20:57:19 +0000 | [diff] [blame] | 194 | ModifiedPHI = true; |
Owen Anderson | dfb0b69 | 2008-08-08 18:00:05 +0000 | [diff] [blame] | 195 | } |
Anton Korobeynikov | 7c5a01f | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 196 | |
Owen Anderson | c6d5270 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 197 | if (phi->getNumOperands() == 3) { |
| 198 | unsigned Input = phi->getOperand(1).getReg(); |
| 199 | unsigned Output = phi->getOperand(0).getReg(); |
| 200 | |
Duncan P. N. Exon Smith | eda8f5d | 2016-07-01 15:13:09 +0000 | [diff] [blame] | 201 | phi++->eraseFromParent(); |
Owen Anderson | 0cd5224 | 2010-09-29 20:57:19 +0000 | [diff] [blame] | 202 | ModifiedPHI = true; |
Owen Anderson | c6d5270 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 203 | |
Cameron Zwarich | 34ef49d | 2011-05-27 05:04:51 +0000 | [diff] [blame] | 204 | if (Input != Output) { |
| 205 | MachineRegisterInfo &MRI = F.getRegInfo(); |
| 206 | MRI.constrainRegClass(Input, MRI.getRegClass(Output)); |
| 207 | MRI.replaceRegWith(Output, Input); |
| 208 | } |
Owen Anderson | dfb0b69 | 2008-08-08 18:00:05 +0000 | [diff] [blame] | 209 | |
Owen Anderson | c6d5270 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 210 | continue; |
| 211 | } |
Anton Korobeynikov | 7c5a01f | 2008-10-31 20:08:30 +0000 | [diff] [blame] | 212 | |
Owen Anderson | c6d5270 | 2008-08-06 23:16:52 +0000 | [diff] [blame] | 213 | ++phi; |
| 214 | } |
| 215 | } |
| 216 | |
Owen Anderson | 9f51539 | 2008-08-05 00:30:10 +0000 | [diff] [blame] | 217 | F.RenumberBlocks(); |
| 218 | |
Alexander Kornienko | 8c0809c | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 219 | return (!DeadBlocks.empty() || ModifiedPHI); |
Chris Lattner | 693244f | 2004-07-02 05:46:10 +0000 | [diff] [blame] | 220 | } |