Mem2Reg.cpp 2.9 KB

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