blob: 12dd5825253368230abd95f0d34f6734719af969 [file] [log] [blame]
Chris Lattnerd99bf492003-02-22 23:57:48 +00001//===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerd99bf492003-02-22 23:57:48 +00009//
10// This pass is a simple pass wrapper around the PromoteMemToReg function call
11// exposed by the Utils library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Transforms/Scalar.h"
16#include "llvm/Transforms/Utils/PromoteMemToReg.h"
17#include "llvm/Analysis/Dominators.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattnerd99bf492003-02-22 23:57:48 +000019#include "llvm/Function.h"
Chris Lattnerfb743a92003-03-03 17:25:18 +000020#include "llvm/Target/TargetData.h"
Chris Lattnerd99bf492003-02-22 23:57:48 +000021#include "Support/Statistic.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattnerd99bf492003-02-22 23:57:48 +000024namespace {
25 Statistic<> NumPromoted("mem2reg", "Number of alloca's promoted");
26
27 struct PromotePass : public FunctionPass {
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
33 // getAnalysisUsage - We need dominance frontiers
34 //
35 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner43f820d2003-10-05 21:20:13 +000036 AU.addRequired<DominatorTree>();
Chris Lattnerd99bf492003-02-22 23:57:48 +000037 AU.addRequired<DominanceFrontier>();
Chris Lattnerfb743a92003-03-03 17:25:18 +000038 AU.addRequired<TargetData>();
Chris Lattnerd99bf492003-02-22 23:57:48 +000039 AU.setPreservesCFG();
40 }
41 };
42
43 RegisterOpt<PromotePass> X("mem2reg", "Promote Memory to Register");
44} // end of anonymous namespace
45
46bool PromotePass::runOnFunction(Function &F) {
47 std::vector<AllocaInst*> Allocas;
Chris Lattnerfb743a92003-03-03 17:25:18 +000048 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattnerd99bf492003-02-22 23:57:48 +000049
Chris Lattner02a3be02003-09-20 14:39:18 +000050 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
Chris Lattnerd99bf492003-02-22 23:57:48 +000051
Chris Lattner83c39d22003-06-25 14:58:56 +000052 bool Changed = false;
Chris Lattner43f820d2003-10-05 21:20:13 +000053
54 DominatorTree &DT = getAnalysis<DominatorTree>();
55 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
Chris Lattner83c39d22003-06-25 14:58:56 +000056
57 while (1) {
58 Allocas.clear();
Chris Lattnerd99bf492003-02-22 23:57:48 +000059
Chris Lattner83c39d22003-06-25 14:58:56 +000060 // Find allocas that are safe to promote, by looking at all instructions in
61 // the entry node
62 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
63 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
64 if (isAllocaPromotable(AI, TD))
65 Allocas.push_back(AI);
66
67 if (Allocas.empty()) break;
68
Chris Lattner43f820d2003-10-05 21:20:13 +000069 PromoteMemToReg(Allocas, DT, DF, TD);
Chris Lattnerd99bf492003-02-22 23:57:48 +000070 NumPromoted += Allocas.size();
Chris Lattner83c39d22003-06-25 14:58:56 +000071 Changed = true;
Chris Lattnerd99bf492003-02-22 23:57:48 +000072 }
Chris Lattner83c39d22003-06-25 14:58:56 +000073
74 return Changed;
Chris Lattnerd99bf492003-02-22 23:57:48 +000075}
76
77// createPromoteMemoryToRegister - Provide an entry point to create this pass.
78//
Chris Lattnerd7456022004-01-09 06:02:20 +000079Pass *llvm::createPromoteMemoryToRegister() {
Chris Lattnerd99bf492003-02-22 23:57:48 +000080 return new PromotePass();
81}
Brian Gaeked0fde302003-11-11 22:41:34 +000082