blob: e0f79dd36c0959151cea522fa94e505fe18e63fb [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 Lattner3d27be12006-08-27 12:54:02 +000023#include "llvm/Support/Compiler.h"
Chris Lattner49525f82004-01-09 06:02:20 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner26f15902003-02-22 23:57:48 +000026namespace {
Chris Lattner700b8732006-12-06 17:46:33 +000027 Statistic NumPromoted("mem2reg", "Number of alloca's promoted");
Chris Lattner26f15902003-02-22 23:57:48 +000028
Chris Lattner4a4c7fe2006-06-28 22:08:15 +000029 struct VISIBILITY_HIDDEN PromotePass : public FunctionPass {
Chris Lattner26f15902003-02-22 23:57:48 +000030 // runOnFunction - To run this pass, first we calculate the alloca
31 // instructions that are safe for promotion, then we promote each one.
32 //
33 virtual bool runOnFunction(Function &F);
34
35 // getAnalysisUsage - We need dominance frontiers
36 //
37 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnera906bac2003-10-05 21:20:13 +000038 AU.addRequired<DominatorTree>();
Chris Lattner26f15902003-02-22 23:57:48 +000039 AU.addRequired<DominanceFrontier>();
Chris Lattnere27406e2003-03-03 17:25:18 +000040 AU.addRequired<TargetData>();
Chris Lattner26f15902003-02-22 23:57:48 +000041 AU.setPreservesCFG();
Chris Lattnere4cb4762006-05-17 21:05:27 +000042 // This is a cluster of orthogonal Transforms
Chris Lattner4fe87d62006-05-09 04:13:41 +000043 AU.addPreserved<UnifyFunctionExitNodes>();
44 AU.addPreservedID(LowerSelectID);
45 AU.addPreservedID(LowerSwitchID);
Chris Lattnere4cb4762006-05-17 21:05:27 +000046 AU.addPreservedID(LowerInvokePassID);
47 AU.addPreservedID(LowerAllocationsID);
Chris Lattner26f15902003-02-22 23:57:48 +000048 }
49 };
50
Chris Lattnerc2d3d312006-08-27 22:42:52 +000051 RegisterPass<PromotePass> X("mem2reg", "Promote Memory to Register");
Chris Lattner26f15902003-02-22 23:57:48 +000052} // end of anonymous namespace
53
54bool PromotePass::runOnFunction(Function &F) {
55 std::vector<AllocaInst*> Allocas;
Chris Lattnere27406e2003-03-03 17:25:18 +000056 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattner26f15902003-02-22 23:57:48 +000057
Chris Lattner5dac64f2003-09-20 14:39:18 +000058 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
Chris Lattner26f15902003-02-22 23:57:48 +000059
Chris Lattnerb396afd2003-06-25 14:58:56 +000060 bool Changed = false;
Chris Lattnera906bac2003-10-05 21:20:13 +000061
62 DominatorTree &DT = getAnalysis<DominatorTree>();
63 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
Misha Brukmanb1c93172005-04-21 23:48:37 +000064
Chris Lattnerb396afd2003-06-25 14:58:56 +000065 while (1) {
66 Allocas.clear();
Chris Lattner26f15902003-02-22 23:57:48 +000067
Chris Lattnerb396afd2003-06-25 14:58:56 +000068 // Find allocas that are safe to promote, by looking at all instructions in
69 // the entry node
70 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
71 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
72 if (isAllocaPromotable(AI, TD))
73 Allocas.push_back(AI);
74
75 if (Allocas.empty()) break;
76
Chris Lattnera906bac2003-10-05 21:20:13 +000077 PromoteMemToReg(Allocas, DT, DF, TD);
Chris Lattner26f15902003-02-22 23:57:48 +000078 NumPromoted += Allocas.size();
Chris Lattnerb396afd2003-06-25 14:58:56 +000079 Changed = true;
Chris Lattner26f15902003-02-22 23:57:48 +000080 }
Chris Lattnerb396afd2003-06-25 14:58:56 +000081
82 return Changed;
Chris Lattner26f15902003-02-22 23:57:48 +000083}
84
Chris Lattner2d3a0272006-05-02 04:24:36 +000085// Publically exposed interface to pass...
86const PassInfo *llvm::PromoteMemoryToRegisterID = X.getPassInfo();
Chris Lattner26f15902003-02-22 23:57:48 +000087// createPromoteMemoryToRegister - Provide an entry point to create this pass.
88//
Alkis Evlogimenos9ead0d72005-03-28 02:01:12 +000089FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
Chris Lattner26f15902003-02-22 23:57:48 +000090 return new PromotePass();
91}