UnreachableBlockElim.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/Constant.h"
  24. #include "llvm/Instructions.h"
  25. #include "llvm/Function.h"
  26. #include "llvm/Pass.h"
  27. #include "llvm/Type.h"
  28. #include "llvm/CodeGen/MachineFunctionPass.h"
  29. #include "llvm/CodeGen/MachineRegisterInfo.h"
  30. #include "llvm/Support/CFG.h"
  31. #include "llvm/Support/Compiler.h"
  32. #include "llvm/Target/TargetInstrInfo.h"
  33. #include "llvm/ADT/DepthFirstIterator.h"
  34. #include "llvm/ADT/SmallPtrSet.h"
  35. using namespace llvm;
  36. namespace {
  37. class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass {
  38. virtual bool runOnFunction(Function &F);
  39. public:
  40. static char ID; // Pass identification, replacement for typeid
  41. UnreachableBlockElim() : FunctionPass(&ID) {}
  42. };
  43. }
  44. char UnreachableBlockElim::ID = 0;
  45. static RegisterPass<UnreachableBlockElim>
  46. X("unreachableblockelim", "Remove unreachable blocks from the CFG");
  47. FunctionPass *llvm::createUnreachableBlockEliminationPass() {
  48. return new UnreachableBlockElim();
  49. }
  50. bool UnreachableBlockElim::runOnFunction(Function &F) {
  51. SmallPtrSet<BasicBlock*, 8> Reachable;
  52. // Mark all reachable blocks.
  53. for (df_ext_iterator<Function*, SmallPtrSet<BasicBlock*, 8> > I =
  54. df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); I != E; ++I)
  55. /* Mark all reachable blocks */;
  56. // Loop over all dead blocks, remembering them and deleting all instructions
  57. // in them.
  58. std::vector<BasicBlock*> DeadBlocks;
  59. for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
  60. if (!Reachable.count(I)) {
  61. BasicBlock *BB = I;
  62. DeadBlocks.push_back(BB);
  63. while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
  64. PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
  65. BB->getInstList().pop_front();
  66. }
  67. for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
  68. (*SI)->removePredecessor(BB);
  69. BB->dropAllReferences();
  70. }
  71. // Actually remove the blocks now.
  72. for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
  73. DeadBlocks[i]->eraseFromParent();
  74. return DeadBlocks.size();
  75. }
  76. namespace {
  77. class VISIBILITY_HIDDEN UnreachableMachineBlockElim :
  78. public MachineFunctionPass {
  79. virtual bool runOnMachineFunction(MachineFunction &F);
  80. public:
  81. static char ID; // Pass identification, replacement for typeid
  82. UnreachableMachineBlockElim() : MachineFunctionPass(&ID) {}
  83. };
  84. }
  85. char UnreachableMachineBlockElim::ID = 0;
  86. static RegisterPass<UnreachableMachineBlockElim>
  87. Y("unreachable-mbb-elimination",
  88. "Remove unreachable machine basic blocks");
  89. const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y;
  90. bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
  91. SmallPtrSet<MachineBasicBlock*, 8> Reachable;
  92. // Mark all reachable blocks.
  93. for (df_ext_iterator<MachineFunction*, SmallPtrSet<MachineBasicBlock*, 8> >
  94. I = df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable);
  95. I != E; ++I)
  96. /* Mark all reachable blocks */;
  97. // Loop over all dead blocks, remembering them and deleting all instructions
  98. // in them.
  99. std::vector<MachineBasicBlock*> DeadBlocks;
  100. for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
  101. MachineBasicBlock *BB = I;
  102. // Test for deadness.
  103. if (!Reachable.count(BB)) {
  104. DeadBlocks.push_back(BB);
  105. while (BB->succ_begin() != BB->succ_end()) {
  106. MachineBasicBlock* succ = *BB->succ_begin();
  107. MachineBasicBlock::iterator start = succ->begin();
  108. while (start != succ->end() &&
  109. start->getOpcode() == TargetInstrInfo::PHI) {
  110. for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
  111. if (start->getOperand(i).isMBB() &&
  112. start->getOperand(i).getMBB() == BB) {
  113. start->RemoveOperand(i);
  114. start->RemoveOperand(i-1);
  115. }
  116. start++;
  117. }
  118. BB->removeSuccessor(BB->succ_begin());
  119. }
  120. }
  121. }
  122. // Actually remove the blocks now.
  123. for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
  124. DeadBlocks[i]->eraseFromParent();
  125. // Cleanup PHI nodes.
  126. for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
  127. MachineBasicBlock *BB = I;
  128. // Prune unneeded PHI entries.
  129. SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
  130. BB->pred_end());
  131. MachineBasicBlock::iterator phi = BB->begin();
  132. while (phi != BB->end() &&
  133. phi->getOpcode() == TargetInstrInfo::PHI) {
  134. for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
  135. if (!preds.count(phi->getOperand(i).getMBB())) {
  136. phi->RemoveOperand(i);
  137. phi->RemoveOperand(i-1);
  138. }
  139. if (phi->getNumOperands() == 3) {
  140. unsigned Input = phi->getOperand(1).getReg();
  141. unsigned Output = phi->getOperand(0).getReg();
  142. MachineInstr* temp = phi;
  143. ++phi;
  144. temp->eraseFromParent();
  145. if (Input != Output)
  146. F.getRegInfo().replaceRegWith(Output, Input);
  147. continue;
  148. }
  149. ++phi;
  150. }
  151. }
  152. F.RenumberBlocks();
  153. return DeadBlocks.size();
  154. }