UnifyFunctionExitNodes.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
  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 used to ensure that functions have at most one return
  11. // instruction in them. Additionally, it keeps track of which node is the new
  12. // exit node of the CFG. If there are no exit nodes in the CFG, the getExitNode
  13. // method will return a null pointer.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
  17. #include "llvm/Transforms/Scalar.h"
  18. #include "llvm/BasicBlock.h"
  19. #include "llvm/Function.h"
  20. #include "llvm/Instructions.h"
  21. #include "llvm/Type.h"
  22. #include "llvm/ADT/StringExtras.h"
  23. using namespace llvm;
  24. char UnifyFunctionExitNodes::ID = 0;
  25. static RegisterPass<UnifyFunctionExitNodes>
  26. X("mergereturn", "Unify function exit nodes");
  27. Pass *llvm::createUnifyFunctionExitNodesPass() {
  28. return new UnifyFunctionExitNodes();
  29. }
  30. void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
  31. // We preserve the non-critical-edgeness property
  32. AU.addPreservedID(BreakCriticalEdgesID);
  33. // This is a cluster of orthogonal Transforms
  34. AU.addPreservedID(PromoteMemoryToRegisterID);
  35. AU.addPreservedID(LowerSwitchID);
  36. }
  37. // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
  38. // BasicBlock, and converting all returns to unconditional branches to this
  39. // new basic block. The singular exit node is returned.
  40. //
  41. // If there are no return stmts in the Function, a null pointer is returned.
  42. //
  43. bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
  44. // Loop over all of the blocks in a function, tracking all of the blocks that
  45. // return.
  46. //
  47. std::vector<BasicBlock*> ReturningBlocks;
  48. std::vector<BasicBlock*> UnwindingBlocks;
  49. std::vector<BasicBlock*> UnreachableBlocks;
  50. for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
  51. if (isa<ReturnInst>(I->getTerminator()))
  52. ReturningBlocks.push_back(I);
  53. else if (isa<UnwindInst>(I->getTerminator()))
  54. UnwindingBlocks.push_back(I);
  55. else if (isa<UnreachableInst>(I->getTerminator()))
  56. UnreachableBlocks.push_back(I);
  57. // Handle unwinding blocks first.
  58. if (UnwindingBlocks.empty()) {
  59. UnwindBlock = 0;
  60. } else if (UnwindingBlocks.size() == 1) {
  61. UnwindBlock = UnwindingBlocks.front();
  62. } else {
  63. UnwindBlock = BasicBlock::Create("UnifiedUnwindBlock", &F);
  64. new UnwindInst(UnwindBlock);
  65. for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
  66. E = UnwindingBlocks.end(); I != E; ++I) {
  67. BasicBlock *BB = *I;
  68. BB->getInstList().pop_back(); // Remove the unwind insn
  69. BranchInst::Create(UnwindBlock, BB);
  70. }
  71. }
  72. // Then unreachable blocks.
  73. if (UnreachableBlocks.empty()) {
  74. UnreachableBlock = 0;
  75. } else if (UnreachableBlocks.size() == 1) {
  76. UnreachableBlock = UnreachableBlocks.front();
  77. } else {
  78. UnreachableBlock = BasicBlock::Create("UnifiedUnreachableBlock", &F);
  79. new UnreachableInst(UnreachableBlock);
  80. for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
  81. E = UnreachableBlocks.end(); I != E; ++I) {
  82. BasicBlock *BB = *I;
  83. BB->getInstList().pop_back(); // Remove the unreachable inst.
  84. BranchInst::Create(UnreachableBlock, BB);
  85. }
  86. }
  87. // Now handle return blocks.
  88. if (ReturningBlocks.empty()) {
  89. ReturnBlock = 0;
  90. return false; // No blocks return
  91. } else if (ReturningBlocks.size() == 1) {
  92. ReturnBlock = ReturningBlocks.front(); // Already has a single return block
  93. return false;
  94. }
  95. // Otherwise, we need to insert a new basic block into the function, add a PHI
  96. // nodes (if the function returns values), and convert all of the return
  97. // instructions into unconditional branches.
  98. //
  99. BasicBlock *NewRetBlock = BasicBlock::Create("UnifiedReturnBlock", &F);
  100. PHINode *PN = 0;
  101. if (F.getReturnType() == Type::VoidTy) {
  102. ReturnInst::Create(NULL, NewRetBlock);
  103. } else {
  104. // If the function doesn't return void... add a PHI node to the block...
  105. PN = PHINode::Create(F.getReturnType(), "UnifiedRetVal");
  106. NewRetBlock->getInstList().push_back(PN);
  107. ReturnInst::Create(PN, NewRetBlock);
  108. }
  109. // Loop over all of the blocks, replacing the return instruction with an
  110. // unconditional branch.
  111. //
  112. for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
  113. E = ReturningBlocks.end(); I != E; ++I) {
  114. BasicBlock *BB = *I;
  115. // Add an incoming element to the PHI node for every return instruction that
  116. // is merging into this new block...
  117. if (PN)
  118. PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
  119. BB->getInstList().pop_back(); // Remove the return insn
  120. BranchInst::Create(NewRetBlock, BB);
  121. }
  122. ReturnBlock = NewRetBlock;
  123. return true;
  124. }