UnifyFunctionExitNodes.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This pass is used to ensure that functions have at most one return and one
  10. // unreachable instruction in them.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
  14. #include "llvm/IR/BasicBlock.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/Instructions.h"
  17. #include "llvm/IR/Type.h"
  18. #include "llvm/InitializePasses.h"
  19. #include "llvm/Transforms/Utils.h"
  20. using namespace llvm;
  21. char UnifyFunctionExitNodesLegacyPass::ID = 0;
  22. UnifyFunctionExitNodesLegacyPass::UnifyFunctionExitNodesLegacyPass()
  23. : FunctionPass(ID) {
  24. initializeUnifyFunctionExitNodesLegacyPassPass(
  25. *PassRegistry::getPassRegistry());
  26. }
  27. INITIALIZE_PASS(UnifyFunctionExitNodesLegacyPass, "mergereturn",
  28. "Unify function exit nodes", false, false)
  29. Pass *llvm::createUnifyFunctionExitNodesPass() {
  30. return new UnifyFunctionExitNodesLegacyPass();
  31. }
  32. void UnifyFunctionExitNodesLegacyPass::getAnalysisUsage(
  33. AnalysisUsage &AU) const {
  34. // We preserve the non-critical-edgeness property
  35. AU.addPreservedID(BreakCriticalEdgesID);
  36. // This is a cluster of orthogonal Transforms
  37. AU.addPreservedID(LowerSwitchID);
  38. }
  39. namespace {
  40. bool unifyUnreachableBlocks(Function &F) {
  41. std::vector<BasicBlock *> UnreachableBlocks;
  42. for (BasicBlock &I : F)
  43. if (isa<UnreachableInst>(I.getTerminator()))
  44. UnreachableBlocks.push_back(&I);
  45. if (UnreachableBlocks.size() <= 1)
  46. return false;
  47. BasicBlock *UnreachableBlock =
  48. BasicBlock::Create(F.getContext(), "UnifiedUnreachableBlock", &F);
  49. new UnreachableInst(F.getContext(), UnreachableBlock);
  50. for (BasicBlock *BB : UnreachableBlocks) {
  51. BB->back().eraseFromParent(); // Remove the unreachable inst.
  52. BranchInst::Create(UnreachableBlock, BB);
  53. }
  54. return true;
  55. }
  56. bool unifyReturnBlocks(Function &F) {
  57. std::vector<BasicBlock *> ReturningBlocks;
  58. for (BasicBlock &I : F)
  59. if (isa<ReturnInst>(I.getTerminator()))
  60. ReturningBlocks.push_back(&I);
  61. if (ReturningBlocks.size() <= 1)
  62. return false;
  63. // Insert a new basic block into the function, add PHI nodes (if the function
  64. // returns values), and convert all of the return instructions into
  65. // unconditional branches.
  66. BasicBlock *NewRetBlock = BasicBlock::Create(F.getContext(),
  67. "UnifiedReturnBlock", &F);
  68. PHINode *PN = nullptr;
  69. if (F.getReturnType()->isVoidTy()) {
  70. ReturnInst::Create(F.getContext(), nullptr, NewRetBlock);
  71. } else {
  72. // If the function doesn't return void... add a PHI node to the block...
  73. PN = PHINode::Create(F.getReturnType(), ReturningBlocks.size(),
  74. "UnifiedRetVal");
  75. PN->insertInto(NewRetBlock, NewRetBlock->end());
  76. ReturnInst::Create(F.getContext(), PN, NewRetBlock);
  77. }
  78. // Loop over all of the blocks, replacing the return instruction with an
  79. // unconditional branch.
  80. for (BasicBlock *BB : ReturningBlocks) {
  81. // Add an incoming element to the PHI node for every return instruction that
  82. // is merging into this new block...
  83. if (PN)
  84. PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
  85. BB->back().eraseFromParent(); // Remove the return insn
  86. BranchInst::Create(NewRetBlock, BB);
  87. }
  88. return true;
  89. }
  90. } // namespace
  91. // Unify all exit nodes of the CFG by creating a new BasicBlock, and converting
  92. // all returns to unconditional branches to this new basic block. Also, unify
  93. // all unreachable blocks.
  94. bool UnifyFunctionExitNodesLegacyPass::runOnFunction(Function &F) {
  95. bool Changed = false;
  96. Changed |= unifyUnreachableBlocks(F);
  97. Changed |= unifyReturnBlocks(F);
  98. return Changed;
  99. }
  100. PreservedAnalyses UnifyFunctionExitNodesPass::run(Function &F,
  101. FunctionAnalysisManager &AM) {
  102. bool Changed = false;
  103. Changed |= unifyUnreachableBlocks(F);
  104. Changed |= unifyReturnBlocks(F);
  105. return Changed ? PreservedAnalyses() : PreservedAnalyses::all();
  106. }