Mem2Reg.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===//
  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 a simple pass wrapper around the PromoteMemToReg function call
  10. // exposed by the Utils library.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Utils/Mem2Reg.h"
  14. #include "llvm/ADT/Statistic.h"
  15. #include "llvm/Analysis/AssumptionCache.h"
  16. #include "llvm/IR/BasicBlock.h"
  17. #include "llvm/IR/Dominators.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include "llvm/IR/PassManager.h"
  21. #include "llvm/InitializePasses.h"
  22. #include "llvm/Pass.h"
  23. #include "llvm/Support/Casting.h"
  24. #include "llvm/Transforms/Utils.h"
  25. #include "llvm/Transforms/Utils/PromoteMemToReg.h"
  26. #include <vector>
  27. using namespace llvm;
  28. #define DEBUG_TYPE "mem2reg"
  29. STATISTIC(NumPromoted, "Number of alloca's promoted");
  30. static bool promoteMemoryToRegister(Function &F, DominatorTree &DT,
  31. AssumptionCache &AC) {
  32. std::vector<AllocaInst *> Allocas;
  33. BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
  34. bool Changed = false;
  35. while (true) {
  36. Allocas.clear();
  37. // Find allocas that are safe to promote, by looking at all instructions in
  38. // the entry node
  39. for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
  40. if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
  41. if (isAllocaPromotable(AI))
  42. Allocas.push_back(AI);
  43. if (Allocas.empty())
  44. break;
  45. PromoteMemToReg(Allocas, DT, &AC);
  46. NumPromoted += Allocas.size();
  47. Changed = true;
  48. }
  49. return Changed;
  50. }
  51. PreservedAnalyses PromotePass::run(Function &F, FunctionAnalysisManager &AM) {
  52. auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
  53. auto &AC = AM.getResult<AssumptionAnalysis>(F);
  54. if (!promoteMemoryToRegister(F, DT, AC))
  55. return PreservedAnalyses::all();
  56. PreservedAnalyses PA;
  57. PA.preserveSet<CFGAnalyses>();
  58. return PA;
  59. }
  60. namespace {
  61. struct PromoteLegacyPass : public FunctionPass {
  62. // Pass identification, replacement for typeid
  63. static char ID;
  64. PromoteLegacyPass() : FunctionPass(ID) {
  65. initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry());
  66. }
  67. // runOnFunction - To run this pass, first we calculate the alloca
  68. // instructions that are safe for promotion, then we promote each one.
  69. bool runOnFunction(Function &F) override {
  70. if (skipFunction(F))
  71. return false;
  72. DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  73. AssumptionCache &AC =
  74. getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  75. return promoteMemoryToRegister(F, DT, AC);
  76. }
  77. void getAnalysisUsage(AnalysisUsage &AU) const override {
  78. AU.addRequired<AssumptionCacheTracker>();
  79. AU.addRequired<DominatorTreeWrapperPass>();
  80. AU.setPreservesCFG();
  81. }
  82. };
  83. } // end anonymous namespace
  84. char PromoteLegacyPass::ID = 0;
  85. INITIALIZE_PASS_BEGIN(PromoteLegacyPass, "mem2reg", "Promote Memory to "
  86. "Register",
  87. false, false)
  88. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  89. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  90. INITIALIZE_PASS_END(PromoteLegacyPass, "mem2reg", "Promote Memory to Register",
  91. false, false)
  92. // createPromoteMemoryToRegister - Provide an entry point to create this pass.
  93. FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
  94. return new PromoteLegacyPass();
  95. }