blob: 52033940997747b87f285e659750e9caab6e301b [file] [log] [blame]
Chris Lattnerd99bf492003-02-22 23:57:48 +00001//===- 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"
Chris Lattnerfb743a92003-03-03 17:25:18 +000013#include "llvm/Target/TargetData.h"
Chris Lattnerd99bf492003-02-22 23:57:48 +000014#include "Support/Statistic.h"
15
16namespace {
17 Statistic<> NumPromoted("mem2reg", "Number of alloca's promoted");
18
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.
22 //
23 virtual bool runOnFunction(Function &F);
24
25 // getAnalysisUsage - We need dominance frontiers
26 //
27 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner43f820d2003-10-05 21:20:13 +000028 AU.addRequired<DominatorTree>();
Chris Lattnerd99bf492003-02-22 23:57:48 +000029 AU.addRequired<DominanceFrontier>();
Chris Lattnerfb743a92003-03-03 17:25:18 +000030 AU.addRequired<TargetData>();
Chris Lattnerd99bf492003-02-22 23:57:48 +000031 AU.setPreservesCFG();
32 }
33 };
34
35 RegisterOpt<PromotePass> X("mem2reg", "Promote Memory to Register");
36} // end of anonymous namespace
37
38bool PromotePass::runOnFunction(Function &F) {
39 std::vector<AllocaInst*> Allocas;
Chris Lattnerfb743a92003-03-03 17:25:18 +000040 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattnerd99bf492003-02-22 23:57:48 +000041
Chris Lattner02a3be02003-09-20 14:39:18 +000042 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
Chris Lattnerd99bf492003-02-22 23:57:48 +000043
Chris Lattner83c39d22003-06-25 14:58:56 +000044 bool Changed = false;
Chris Lattner43f820d2003-10-05 21:20:13 +000045
46 DominatorTree &DT = getAnalysis<DominatorTree>();
47 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
Chris Lattner83c39d22003-06-25 14:58:56 +000048
49 while (1) {
50 Allocas.clear();
Chris Lattnerd99bf492003-02-22 23:57:48 +000051
Chris Lattner83c39d22003-06-25 14:58:56 +000052 // Find allocas that are safe to promote, by looking at all instructions in
53 // the entry node
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);
58
59 if (Allocas.empty()) break;
60
Chris Lattner43f820d2003-10-05 21:20:13 +000061 PromoteMemToReg(Allocas, DT, DF, TD);
Chris Lattnerd99bf492003-02-22 23:57:48 +000062 NumPromoted += Allocas.size();
Chris Lattner83c39d22003-06-25 14:58:56 +000063 Changed = true;
Chris Lattnerd99bf492003-02-22 23:57:48 +000064 }
Chris Lattner83c39d22003-06-25 14:58:56 +000065
66 return Changed;
Chris Lattnerd99bf492003-02-22 23:57:48 +000067}
68
69// createPromoteMemoryToRegister - Provide an entry point to create this pass.
70//
71Pass *createPromoteMemoryToRegister() {
72 return new PromotePass();
73}