UnifyFunctionExitNodes.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/ADT/StringExtras.h"
  18. #include "llvm/IR/BasicBlock.h"
  19. #include "llvm/IR/Function.h"
  20. #include "llvm/IR/Instructions.h"
  21. #include "llvm/IR/Type.h"
  22. #include "llvm/Transforms/Scalar.h"
  23. using namespace llvm;
  24. char UnifyFunctionExitNodes::ID = 0;
  25. INITIALIZE_PASS(UnifyFunctionExitNodes, "mergereturn",
  26. "Unify function exit nodes", false, false)
  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(LowerSwitchID);
  35. }
  36. // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
  37. // BasicBlock, and converting all returns to unconditional branches to this
  38. // new basic block. The singular exit node is returned.
  39. //
  40. // If there are no return stmts in the Function, a null pointer is returned.
  41. //
  42. bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
  43. // Loop over all of the blocks in a function, tracking all of the blocks that
  44. // return.
  45. //
  46. std::vector<BasicBlock*> ReturningBlocks;
  47. std::vector<BasicBlock*> UnreachableBlocks;
  48. for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
  49. if (isa<ReturnInst>(I->getTerminator()))
  50. ReturningBlocks.push_back(I);
  51. else if (isa<UnreachableInst>(I->getTerminator()))
  52. UnreachableBlocks.push_back(I);
  53. // Then unreachable blocks.
  54. if (UnreachableBlocks.empty()) {
  55. UnreachableBlock = nullptr;
  56. } else if (UnreachableBlocks.size() == 1) {
  57. UnreachableBlock = UnreachableBlocks.front();
  58. } else {
  59. UnreachableBlock = BasicBlock::Create(F.getContext(),
  60. "UnifiedUnreachableBlock", &F);
  61. new UnreachableInst(F.getContext(), UnreachableBlock);
  62. for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
  63. E = UnreachableBlocks.end(); I != E; ++I) {
  64. BasicBlock *BB = *I;
  65. BB->getInstList().pop_back(); // Remove the unreachable inst.
  66. BranchInst::Create(UnreachableBlock, BB);
  67. }
  68. }
  69. // Now handle return blocks.
  70. if (ReturningBlocks.empty()) {
  71. ReturnBlock = nullptr;
  72. return false; // No blocks return
  73. } else if (ReturningBlocks.size() == 1) {
  74. ReturnBlock = ReturningBlocks.front(); // Already has a single return block
  75. return false;
  76. }
  77. // Otherwise, we need to insert a new basic block into the function, add a PHI
  78. // nodes (if the function returns values), and convert all of the return
  79. // instructions into unconditional branches.
  80. //
  81. BasicBlock *NewRetBlock = BasicBlock::Create(F.getContext(),
  82. "UnifiedReturnBlock", &F);
  83. PHINode *PN = nullptr;
  84. if (F.getReturnType()->isVoidTy()) {
  85. ReturnInst::Create(F.getContext(), nullptr, NewRetBlock);
  86. } else {
  87. // If the function doesn't return void... add a PHI node to the block...
  88. PN = PHINode::Create(F.getReturnType(), ReturningBlocks.size(),
  89. "UnifiedRetVal");
  90. NewRetBlock->getInstList().push_back(PN);
  91. ReturnInst::Create(F.getContext(), PN, NewRetBlock);
  92. }
  93. // Loop over all of the blocks, replacing the return instruction with an
  94. // unconditional branch.
  95. //
  96. for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
  97. E = ReturningBlocks.end(); I != E; ++I) {
  98. BasicBlock *BB = *I;
  99. // Add an incoming element to the PHI node for every return instruction that
  100. // is merging into this new block...
  101. if (PN)
  102. PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
  103. BB->getInstList().pop_back(); // Remove the return insn
  104. BranchInst::Create(NewRetBlock, BB);
  105. }
  106. ReturnBlock = NewRetBlock;
  107. return true;
  108. }