6fbb43f6c3f318fc729dd0a6a5a99d7643564cf4
[oota-llvm.git] / lib / Transforms / Utils / Mem2Reg.cpp
1 //===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===//
2 //
3 // This pass is a simple pass wrapper around the PromoteMemToReg function call
4 // exposed by the Utils library.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Transforms/Scalar.h"
9 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
10 #include "llvm/Analysis/Dominators.h"
11 #include "llvm/iMemory.h"
12 #include "llvm/Function.h"
13 #include "Support/Statistic.h"
14
15 namespace {
16   Statistic<> NumPromoted("mem2reg", "Number of alloca's promoted");
17
18   struct PromotePass : public FunctionPass {
19     // runOnFunction - To run this pass, first we calculate the alloca
20     // instructions that are safe for promotion, then we promote each one.
21     //
22     virtual bool runOnFunction(Function &F);
23
24     // getAnalysisUsage - We need dominance frontiers
25     //
26     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
27       AU.addRequired<DominanceFrontier>();
28       AU.setPreservesCFG();
29     }
30   };
31
32   RegisterOpt<PromotePass> X("mem2reg", "Promote Memory to Register");
33 }  // end of anonymous namespace
34
35 bool PromotePass::runOnFunction(Function &F) {
36   std::vector<AllocaInst*> Allocas;
37
38   BasicBlock &BB = F.getEntryNode();  // Get the entry node for the function
39
40   // Find allocas that are safe to promote, by looking at all instructions in
41   // the entry node
42   for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
43     if (AllocaInst *AI = dyn_cast<AllocaInst>(&*I))       // Is it an alloca?
44       if (isAllocaPromotable(AI))
45         Allocas.push_back(AI);
46
47   if (!Allocas.empty()) {
48     PromoteMemToReg(Allocas, getAnalysis<DominanceFrontier>());
49     NumPromoted += Allocas.size();
50     return true;
51   }
52   return false;
53 }
54
55 // createPromoteMemoryToRegister - Provide an entry point to create this pass.
56 //
57 Pass *createPromoteMemoryToRegister() {
58   return new PromotePass();
59 }