blob: d1b2c50796eec0cebfd2a19fbbf4f2ef44a202c2 [file] [log] [blame]
Chris Lattnerd99bf492003-02-22 23:57:48 +00001//===- Mem2Reg.cpp - The -mem2reg pass, a wrapper around the Utils lib ----===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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"
Chris Lattner8d89e7b2006-05-09 04:13:41 +000017#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattnerd99bf492003-02-22 23:57:48 +000018#include "llvm/Analysis/Dominators.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Chris Lattnerd99bf492003-02-22 23:57:48 +000020#include "llvm/Function.h"
Chris Lattnerfb743a92003-03-03 17:25:18 +000021#include "llvm/Target/TargetData.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/ADT/Statistic.h"
Chris Lattnerf4b54612006-06-28 22:08:15 +000023#include "llvm/Support/Visibility.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnerd99bf492003-02-22 23:57:48 +000026namespace {
27 Statistic<> NumPromoted("mem2reg", "Number of alloca's promoted");
28
Chris Lattnerf4b54612006-06-28 22:08:15 +000029 struct VISIBILITY_HIDDEN PromotePass : public FunctionPass {
Chris Lattnerd99bf492003-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 Lattner43f820d2003-10-05 21:20:13 +000038 AU.addRequired<DominatorTree>();
Chris Lattnerd99bf492003-02-22 23:57:48 +000039 AU.addRequired<DominanceFrontier>();
Chris Lattnerfb743a92003-03-03 17:25:18 +000040 AU.addRequired<TargetData>();
Chris Lattnerd99bf492003-02-22 23:57:48 +000041 AU.setPreservesCFG();
Chris Lattnered96fe82006-05-17 21:05:27 +000042 // This is a cluster of orthogonal Transforms
Chris Lattner8d89e7b2006-05-09 04:13:41 +000043 AU.addPreserved<UnifyFunctionExitNodes>();
44 AU.addPreservedID(LowerSelectID);
45 AU.addPreservedID(LowerSwitchID);
Chris Lattnered96fe82006-05-17 21:05:27 +000046 AU.addPreservedID(LowerInvokePassID);
47 AU.addPreservedID(LowerAllocationsID);
Chris Lattnerd99bf492003-02-22 23:57:48 +000048 }
49 };
50
51 RegisterOpt<PromotePass> X("mem2reg", "Promote Memory to Register");
52} // end of anonymous namespace
53
54bool PromotePass::runOnFunction(Function &F) {
55 std::vector<AllocaInst*> Allocas;
Chris Lattnerfb743a92003-03-03 17:25:18 +000056 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattnerd99bf492003-02-22 23:57:48 +000057
Chris Lattner02a3be02003-09-20 14:39:18 +000058 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
Chris Lattnerd99bf492003-02-22 23:57:48 +000059
Chris Lattner83c39d22003-06-25 14:58:56 +000060 bool Changed = false;
Chris Lattner43f820d2003-10-05 21:20:13 +000061
62 DominatorTree &DT = getAnalysis<DominatorTree>();
63 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
Misha Brukmanfd939082005-04-21 23:48:37 +000064
Chris Lattner83c39d22003-06-25 14:58:56 +000065 while (1) {
66 Allocas.clear();
Chris Lattnerd99bf492003-02-22 23:57:48 +000067
Chris Lattner83c39d22003-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 Lattner43f820d2003-10-05 21:20:13 +000077 PromoteMemToReg(Allocas, DT, DF, TD);
Chris Lattnerd99bf492003-02-22 23:57:48 +000078 NumPromoted += Allocas.size();
Chris Lattner83c39d22003-06-25 14:58:56 +000079 Changed = true;
Chris Lattnerd99bf492003-02-22 23:57:48 +000080 }
Chris Lattner83c39d22003-06-25 14:58:56 +000081
82 return Changed;
Chris Lattnerd99bf492003-02-22 23:57:48 +000083}
84
Chris Lattnerb3674e42006-05-02 04:24:36 +000085// Publically exposed interface to pass...
86const PassInfo *llvm::PromoteMemoryToRegisterID = X.getPassInfo();
Chris Lattnerd99bf492003-02-22 23:57:48 +000087// createPromoteMemoryToRegister - Provide an entry point to create this pass.
88//
Alkis Evlogimenosab7ada32005-03-28 02:01:12 +000089FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
Chris Lattnerd99bf492003-02-22 23:57:48 +000090 return new PromotePass();
91}