/3rd_party/llvm/lib/Transforms/Utils/Mem2Reg.cpp

https://code.google.com/p/softart/ · C++ · 90 lines · 53 code · 16 blank · 21 comment · 6 complexity · c4cbc8771306e0fa79e9ccf59413103d MD5 · raw file

  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/ADT/Statistic.h"
  17. #include "llvm/Analysis/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. 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. initializePromotePassPass(*PassRegistry::getPassRegistry());
  29. }
  30. // runOnFunction - To run this pass, first we calculate the alloca
  31. // instructions that are safe for promotion, then we promote each one.
  32. //
  33. virtual bool runOnFunction(Function &F);
  34. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  35. AU.addRequired<DominatorTree>();
  36. AU.setPreservesCFG();
  37. // This is a cluster of orthogonal Transforms
  38. AU.addPreserved<UnifyFunctionExitNodes>();
  39. AU.addPreservedID(LowerSwitchID);
  40. AU.addPreservedID(LowerInvokePassID);
  41. }
  42. };
  43. } // end of anonymous namespace
  44. char PromotePass::ID = 0;
  45. INITIALIZE_PASS_BEGIN(PromotePass, "mem2reg", "Promote Memory to Register",
  46. false, false)
  47. INITIALIZE_PASS_DEPENDENCY(DominatorTree)
  48. INITIALIZE_PASS_END(PromotePass, "mem2reg", "Promote Memory to Register",
  49. false, false)
  50. bool PromotePass::runOnFunction(Function &F) {
  51. std::vector<AllocaInst*> Allocas;
  52. BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
  53. bool Changed = false;
  54. DominatorTree &DT = getAnalysis<DominatorTree>();
  55. while (1) {
  56. Allocas.clear();
  57. // Find allocas that are safe to promote, by looking at all instructions in
  58. // the entry node
  59. for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
  60. if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
  61. if (isAllocaPromotable(AI))
  62. Allocas.push_back(AI);
  63. if (Allocas.empty()) break;
  64. PromoteMemToReg(Allocas, DT);
  65. NumPromoted += Allocas.size();
  66. Changed = true;
  67. }
  68. return Changed;
  69. }
  70. // createPromoteMemoryToRegister - Provide an entry point to create this pass.
  71. //
  72. FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
  73. return new PromotePass();
  74. }