blob: bc29c9f2d607e0e3664fc140462468fc5d761c8d [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
Chris Lattner45f966d2006-12-19 22:17:40 +000015#define DEBUG_TYPE "mem2reg"
Chris Lattner26f15902003-02-22 23:57:48 +000016#include "llvm/Transforms/Scalar.h"
17#include "llvm/Transforms/Utils/PromoteMemToReg.h"
Chris Lattner4fe87d62006-05-09 04:13:41 +000018#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattner26f15902003-02-22 23:57:48 +000019#include "llvm/Analysis/Dominators.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000020#include "llvm/Instructions.h"
Chris Lattner26f15902003-02-22 23:57:48 +000021#include "llvm/Function.h"
Chris Lattnere27406e2003-03-03 17:25:18 +000022#include "llvm/Target/TargetData.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/ADT/Statistic.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000024#include "llvm/Support/Compiler.h"
Chris Lattner49525f82004-01-09 06:02:20 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chris Lattner45f966d2006-12-19 22:17:40 +000027STATISTIC(NumPromoted, "Number of alloca's promoted");
Chris Lattner26f15902003-02-22 23:57:48 +000028
Chris Lattner45f966d2006-12-19 22:17:40 +000029namespace {
Chris Lattner4a4c7fe2006-06-28 22:08:15 +000030 struct VISIBILITY_HIDDEN PromotePass : public FunctionPass {
Chris Lattner26f15902003-02-22 23:57:48 +000031 // runOnFunction - To run this pass, first we calculate the alloca
32 // instructions that are safe for promotion, then we promote each one.
33 //
34 virtual bool runOnFunction(Function &F);
35
36 // getAnalysisUsage - We need dominance frontiers
37 //
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson2da606c2007-04-20 06:27:13 +000039 AU.addRequired<ETForest>();
Chris Lattner26f15902003-02-22 23:57:48 +000040 AU.addRequired<DominanceFrontier>();
Chris Lattnere27406e2003-03-03 17:25:18 +000041 AU.addRequired<TargetData>();
Chris Lattner26f15902003-02-22 23:57:48 +000042 AU.setPreservesCFG();
Anton Korobeynikovfb801512007-04-16 18:10:23 +000043 // This is a cluster of orthogonal Transforms
Chris Lattner4fe87d62006-05-09 04:13:41 +000044 AU.addPreserved<UnifyFunctionExitNodes>();
45 AU.addPreservedID(LowerSelectID);
46 AU.addPreservedID(LowerSwitchID);
Chris Lattnere4cb4762006-05-17 21:05:27 +000047 AU.addPreservedID(LowerInvokePassID);
48 AU.addPreservedID(LowerAllocationsID);
Chris Lattner26f15902003-02-22 23:57:48 +000049 }
50 };
51
Chris Lattnerc2d3d312006-08-27 22:42:52 +000052 RegisterPass<PromotePass> X("mem2reg", "Promote Memory to Register");
Chris Lattner26f15902003-02-22 23:57:48 +000053} // end of anonymous namespace
54
55bool PromotePass::runOnFunction(Function &F) {
56 std::vector<AllocaInst*> Allocas;
Chris Lattnere27406e2003-03-03 17:25:18 +000057 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattner26f15902003-02-22 23:57:48 +000058
Chris Lattner5dac64f2003-09-20 14:39:18 +000059 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
Chris Lattner26f15902003-02-22 23:57:48 +000060
Chris Lattnerb396afd2003-06-25 14:58:56 +000061 bool Changed = false;
Chris Lattnera906bac2003-10-05 21:20:13 +000062
Owen Anderson2da606c2007-04-20 06:27:13 +000063 ETForest &ET = getAnalysis<ETForest>();
Chris Lattnera906bac2003-10-05 21:20:13 +000064 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
Misha Brukmanb1c93172005-04-21 23:48:37 +000065
Chris Lattnerb396afd2003-06-25 14:58:56 +000066 while (1) {
67 Allocas.clear();
Chris Lattner26f15902003-02-22 23:57:48 +000068
Chris Lattnerb396afd2003-06-25 14:58:56 +000069 // Find allocas that are safe to promote, by looking at all instructions in
70 // the entry node
71 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
72 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
Devang Patel073be552007-04-25 17:15:20 +000073 if (isAllocaPromotable(AI))
Chris Lattnerb396afd2003-06-25 14:58:56 +000074 Allocas.push_back(AI);
75
76 if (Allocas.empty()) break;
77
Owen Anderson2da606c2007-04-20 06:27:13 +000078 PromoteMemToReg(Allocas, ET, DF, TD);
Chris Lattner26f15902003-02-22 23:57:48 +000079 NumPromoted += Allocas.size();
Chris Lattnerb396afd2003-06-25 14:58:56 +000080 Changed = true;
Chris Lattner26f15902003-02-22 23:57:48 +000081 }
Chris Lattnerb396afd2003-06-25 14:58:56 +000082
83 return Changed;
Chris Lattner26f15902003-02-22 23:57:48 +000084}
85
Chris Lattner2d3a0272006-05-02 04:24:36 +000086// Publically exposed interface to pass...
87const PassInfo *llvm::PromoteMemoryToRegisterID = X.getPassInfo();
Chris Lattner26f15902003-02-22 23:57:48 +000088// createPromoteMemoryToRegister - Provide an entry point to create this pass.
89//
Alkis Evlogimenos9ead0d72005-03-28 02:01:12 +000090FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
Chris Lattner26f15902003-02-22 23:57:48 +000091 return new PromotePass();
92}