2
0

UnreachableBlockElim.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  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. #include "llvm/CodeGen/Passes.h"
  23. #include "llvm/ADT/DepthFirstIterator.h"
  24. #include "llvm/ADT/SmallPtrSet.h"
  25. #include "llvm/CodeGen/MachineDominators.h"
  26. #include "llvm/CodeGen/MachineFunctionPass.h"
  27. #include "llvm/CodeGen/MachineLoopInfo.h"
  28. #include "llvm/CodeGen/MachineModuleInfo.h"
  29. #include "llvm/CodeGen/MachineRegisterInfo.h"
  30. #include "llvm/IR/CFG.h"
  31. #include "llvm/IR/Constant.h"
  32. #include "llvm/IR/Dominators.h"
  33. #include "llvm/IR/Function.h"
  34. #include "llvm/IR/Instructions.h"
  35. #include "llvm/IR/Type.h"
  36. #include "llvm/Pass.h"
  37. #include "llvm/Target/TargetInstrInfo.h"
  38. using namespace llvm;
  39. namespace {
  40. class UnreachableBlockElim : public FunctionPass {
  41. bool runOnFunction(Function &F) override;
  42. public:
  43. static char ID; // Pass identification, replacement for typeid
  44. UnreachableBlockElim() : FunctionPass(ID) {
  45. initializeUnreachableBlockElimPass(*PassRegistry::getPassRegistry());
  46. }
  47. void getAnalysisUsage(AnalysisUsage &AU) const override {
  48. AU.addPreserved<DominatorTreeWrapperPass>();
  49. }
  50. };
  51. }
  52. char UnreachableBlockElim::ID = 0;
  53. INITIALIZE_PASS(UnreachableBlockElim, "unreachableblockelim",
  54. "Remove unreachable blocks from the CFG", false, false)
  55. FunctionPass *llvm::createUnreachableBlockEliminationPass() {
  56. return new UnreachableBlockElim();
  57. }
  58. bool UnreachableBlockElim::runOnFunction(Function &F) {
  59. SmallPtrSet<BasicBlock*, 8> Reachable;
  60. // Mark all reachable blocks.
  61. for (BasicBlock *BB : depth_first_ext(&F, Reachable))
  62. (void)BB/* Mark all reachable blocks */;
  63. // Loop over all dead blocks, remembering them and deleting all instructions
  64. // in them.
  65. std::vector<BasicBlock*> DeadBlocks;
  66. for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
  67. if (!Reachable.count(I)) {
  68. BasicBlock *BB = I;
  69. DeadBlocks.push_back(BB);
  70. while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
  71. PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
  72. BB->getInstList().pop_front();
  73. }
  74. for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
  75. (*SI)->removePredecessor(BB);
  76. BB->dropAllReferences();
  77. }
  78. // Actually remove the blocks now.
  79. for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
  80. DeadBlocks[i]->eraseFromParent();
  81. }
  82. return !DeadBlocks.empty();
  83. }
  84. namespace {
  85. class UnreachableMachineBlockElim : public MachineFunctionPass {
  86. bool runOnMachineFunction(MachineFunction &F) override;
  87. void getAnalysisUsage(AnalysisUsage &AU) const override;
  88. MachineModuleInfo *MMI;
  89. public:
  90. static char ID; // Pass identification, replacement for typeid
  91. UnreachableMachineBlockElim() : MachineFunctionPass(ID) {}
  92. };
  93. }
  94. char UnreachableMachineBlockElim::ID = 0;
  95. INITIALIZE_PASS(UnreachableMachineBlockElim, "unreachable-mbb-elimination",
  96. "Remove unreachable machine basic blocks", false, false)
  97. char &llvm::UnreachableMachineBlockElimID = UnreachableMachineBlockElim::ID;
  98. void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
  99. AU.addPreserved<MachineLoopInfo>();
  100. AU.addPreserved<MachineDominatorTree>();
  101. MachineFunctionPass::getAnalysisUsage(AU);
  102. }
  103. bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
  104. SmallPtrSet<MachineBasicBlock*, 8> Reachable;
  105. bool ModifiedPHI = false;
  106. MMI = getAnalysisIfAvailable<MachineModuleInfo>();
  107. MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
  108. MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
  109. // Mark all reachable blocks.
  110. for (MachineBasicBlock *BB : depth_first_ext(&F, Reachable))
  111. (void)BB/* Mark all reachable blocks */;
  112. // Loop over all dead blocks, remembering them and deleting all instructions
  113. // in them.
  114. std::vector<MachineBasicBlock*> DeadBlocks;
  115. for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
  116. MachineBasicBlock *BB = I;
  117. // Test for deadness.
  118. if (!Reachable.count(BB)) {
  119. DeadBlocks.push_back(BB);
  120. // Update dominator and loop info.
  121. if (MLI) MLI->removeBlock(BB);
  122. if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
  123. while (BB->succ_begin() != BB->succ_end()) {
  124. MachineBasicBlock* succ = *BB->succ_begin();
  125. MachineBasicBlock::iterator start = succ->begin();
  126. while (start != succ->end() && start->isPHI()) {
  127. for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
  128. if (start->getOperand(i).isMBB() &&
  129. start->getOperand(i).getMBB() == BB) {
  130. start->RemoveOperand(i);
  131. start->RemoveOperand(i-1);
  132. }
  133. start++;
  134. }
  135. BB->removeSuccessor(BB->succ_begin());
  136. }
  137. }
  138. }
  139. // Actually remove the blocks now.
  140. for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
  141. DeadBlocks[i]->eraseFromParent();
  142. // Cleanup PHI nodes.
  143. for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
  144. MachineBasicBlock *BB = I;
  145. // Prune unneeded PHI entries.
  146. SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
  147. BB->pred_end());
  148. MachineBasicBlock::iterator phi = BB->begin();
  149. while (phi != BB->end() && phi->isPHI()) {
  150. for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
  151. if (!preds.count(phi->getOperand(i).getMBB())) {
  152. phi->RemoveOperand(i);
  153. phi->RemoveOperand(i-1);
  154. ModifiedPHI = true;
  155. }
  156. if (phi->getNumOperands() == 3) {
  157. unsigned Input = phi->getOperand(1).getReg();
  158. unsigned Output = phi->getOperand(0).getReg();
  159. MachineInstr* temp = phi;
  160. ++phi;
  161. temp->eraseFromParent();
  162. ModifiedPHI = true;
  163. if (Input != Output) {
  164. MachineRegisterInfo &MRI = F.getRegInfo();
  165. MRI.constrainRegClass(Input, MRI.getRegClass(Output));
  166. MRI.replaceRegWith(Output, Input);
  167. }
  168. continue;
  169. }
  170. ++phi;
  171. }
  172. }
  173. F.RenumberBlocks();
  174. return (!DeadBlocks.empty() || ModifiedPHI);
  175. }