Mem2Reg.cpp 3.1 KB

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