blob: 9ca2c8300da918ea03cbdbb14ddfdbd4aa8b658d [file] [log] [blame]
Chris Lattner26f15902003-02-22 23:57:48 +00001//===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner26f15902003-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"
Chris Lattner4fe87d62006-05-09 04:13:41 +000017#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattner26f15902003-02-22 23:57:48 +000018#include "llvm/Analysis/Dominators.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Chris Lattner26f15902003-02-22 23:57:48 +000020#include "llvm/Function.h"
Chris Lattnere27406e2003-03-03 17:25:18 +000021#include "llvm/Target/TargetData.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/ADT/Statistic.h"
Chris Lattner49525f82004-01-09 06:02:20 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Chris Lattner26f15902003-02-22 23:57:48 +000025namespace {
26 Statistic<> NumPromoted("mem2reg", "Number of alloca's promoted");
27
28 struct PromotePass : public FunctionPass {
29 // runOnFunction - To run this pass, first we calculate the alloca
30 // instructions that are safe for promotion, then we promote each one.
31 //
32 virtual bool runOnFunction(Function &F);
33
34 // getAnalysisUsage - We need dominance frontiers
35 //
36 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnera906bac2003-10-05 21:20:13 +000037 AU.addRequired<DominatorTree>();
Chris Lattner26f15902003-02-22 23:57:48 +000038 AU.addRequired<DominanceFrontier>();
Chris Lattnere27406e2003-03-03 17:25:18 +000039 AU.addRequired<TargetData>();
Chris Lattner26f15902003-02-22 23:57:48 +000040 AU.setPreservesCFG();
Chris Lattner4fe87d62006-05-09 04:13:41 +000041 // This is a cluster of orthogonal Transforms
42 AU.addPreserved<UnifyFunctionExitNodes>();
43 AU.addPreservedID(LowerSelectID);
44 AU.addPreservedID(LowerSwitchID);
Chris Lattner26f15902003-02-22 23:57:48 +000045 }
46 };
47
48 RegisterOpt<PromotePass> X("mem2reg", "Promote Memory to Register");
49} // end of anonymous namespace
50
51bool PromotePass::runOnFunction(Function &F) {
52 std::vector<AllocaInst*> Allocas;
Chris Lattnere27406e2003-03-03 17:25:18 +000053 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattner26f15902003-02-22 23:57:48 +000054
Chris Lattner5dac64f2003-09-20 14:39:18 +000055 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
Chris Lattner26f15902003-02-22 23:57:48 +000056
Chris Lattnerb396afd2003-06-25 14:58:56 +000057 bool Changed = false;
Chris Lattnera906bac2003-10-05 21:20:13 +000058
59 DominatorTree &DT = getAnalysis<DominatorTree>();
60 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
Misha Brukmanb1c93172005-04-21 23:48:37 +000061
Chris Lattnerb396afd2003-06-25 14:58:56 +000062 while (1) {
63 Allocas.clear();
Chris Lattner26f15902003-02-22 23:57:48 +000064
Chris Lattnerb396afd2003-06-25 14:58:56 +000065 // Find allocas that are safe to promote, by looking at all instructions in
66 // the entry node
67 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
68 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
69 if (isAllocaPromotable(AI, TD))
70 Allocas.push_back(AI);
71
72 if (Allocas.empty()) break;
73
Chris Lattnera906bac2003-10-05 21:20:13 +000074 PromoteMemToReg(Allocas, DT, DF, TD);
Chris Lattner26f15902003-02-22 23:57:48 +000075 NumPromoted += Allocas.size();
Chris Lattnerb396afd2003-06-25 14:58:56 +000076 Changed = true;
Chris Lattner26f15902003-02-22 23:57:48 +000077 }
Chris Lattnerb396afd2003-06-25 14:58:56 +000078
79 return Changed;
Chris Lattner26f15902003-02-22 23:57:48 +000080}
81
Chris Lattner2d3a0272006-05-02 04:24:36 +000082// Publically exposed interface to pass...
83const PassInfo *llvm::PromoteMemoryToRegisterID = X.getPassInfo();
Chris Lattner26f15902003-02-22 23:57:48 +000084// createPromoteMemoryToRegister - Provide an entry point to create this pass.
85//
Alkis Evlogimenos9ead0d72005-03-28 02:01:12 +000086FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
Chris Lattner26f15902003-02-22 23:57:48 +000087 return new PromotePass();
88}