1 //===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===//
3 // This pass is a simple pass wrapper around the PromoteMemToReg function call
4 // exposed by the Utils library.
6 //===----------------------------------------------------------------------===//
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 "llvm/Target/TargetData.h"
14 #include "Support/Statistic.h"
17 Statistic<> NumPromoted("mem2reg", "Number of alloca's promoted");
19 struct PromotePass : public FunctionPass {
20 // runOnFunction - To run this pass, first we calculate the alloca
21 // instructions that are safe for promotion, then we promote each one.
23 virtual bool runOnFunction(Function &F);
25 // getAnalysisUsage - We need dominance frontiers
27 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
28 AU.addRequired<DominatorTree>();
29 AU.addRequired<DominanceFrontier>();
30 AU.addRequired<TargetData>();
35 RegisterOpt<PromotePass> X("mem2reg", "Promote Memory to Register");
36 } // end of anonymous namespace
38 bool PromotePass::runOnFunction(Function &F) {
39 std::vector<AllocaInst*> Allocas;
40 const TargetData &TD = getAnalysis<TargetData>();
42 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
46 DominatorTree &DT = getAnalysis<DominatorTree>();
47 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
52 // Find allocas that are safe to promote, by looking at all instructions in
54 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
55 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
56 if (isAllocaPromotable(AI, TD))
57 Allocas.push_back(AI);
59 if (Allocas.empty()) break;
61 PromoteMemToReg(Allocas, DT, DF, TD);
62 NumPromoted += Allocas.size();
69 // createPromoteMemoryToRegister - Provide an entry point to create this pass.
71 Pass *createPromoteMemoryToRegister() {
72 return new PromotePass();