blob: ebd7db6210caecea445d143cd86c353acd2a1b77 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Chris Lattnerd216e8b2006-12-19 22:17:40 +000015#define DEBUG_TYPE "mem2reg"
Chris Lattnerd99bf492003-02-22 23:57:48 +000016#include "llvm/Transforms/Scalar.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/ADT/Statistic.h"
18#include "llvm/Analysis/Dominators.h"
Chandler Carruth6c3a95d2013-07-28 06:43:11 +000019#include "llvm/IR/DataLayout.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Function.h"
21#include "llvm/IR/Instructions.h"
Chris Lattnerd99bf492003-02-22 23:57:48 +000022#include "llvm/Transforms/Utils/PromoteMemToReg.h"
Chris Lattner8d89e7b2006-05-09 04:13:41 +000023#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnerd216e8b2006-12-19 22:17:40 +000026STATISTIC(NumPromoted, "Number of alloca's promoted");
Chris Lattnerd99bf492003-02-22 23:57:48 +000027
Chris Lattnerd216e8b2006-12-19 22:17:40 +000028namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000029 struct PromotePass : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000030 static char ID; // Pass identification, replacement for typeid
Chandler Carruth6c3a95d2013-07-28 06:43:11 +000031
Owen Anderson081c34b2010-10-19 17:21:58 +000032 PromotePass() : FunctionPass(ID) {
33 initializePromotePassPass(*PassRegistry::getPassRegistry());
34 }
Devang Patel794fd752007-05-01 21:15:47 +000035
Chris Lattnerd99bf492003-02-22 23:57:48 +000036 // runOnFunction - To run this pass, first we calculate the alloca
37 // instructions that are safe for promotion, then we promote each one.
38 //
39 virtual bool runOnFunction(Function &F);
40
Chris Lattnerd99bf492003-02-22 23:57:48 +000041 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Devang Patel326821e2007-06-07 21:57:03 +000042 AU.addRequired<DominatorTree>();
Chris Lattnerd99bf492003-02-22 23:57:48 +000043 AU.setPreservesCFG();
Anton Korobeynikovbed29462007-04-16 18:10:23 +000044 // This is a cluster of orthogonal Transforms
Chris Lattner8d89e7b2006-05-09 04:13:41 +000045 AU.addPreserved<UnifyFunctionExitNodes>();
Chris Lattner8d89e7b2006-05-09 04:13:41 +000046 AU.addPreservedID(LowerSwitchID);
Chris Lattnered96fe82006-05-17 21:05:27 +000047 AU.addPreservedID(LowerInvokePassID);
Chris Lattnerd99bf492003-02-22 23:57:48 +000048 }
49 };
Chris Lattnerd99bf492003-02-22 23:57:48 +000050} // end of anonymous namespace
51
Dan Gohman844731a2008-05-13 00:00:25 +000052char PromotePass::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000053INITIALIZE_PASS_BEGIN(PromotePass, "mem2reg", "Promote Memory to Register",
54 false, false)
55INITIALIZE_PASS_DEPENDENCY(DominatorTree)
Owen Anderson2ab36d32010-10-12 19:48:12 +000056INITIALIZE_PASS_END(PromotePass, "mem2reg", "Promote Memory to Register",
Owen Andersonce665bd2010-10-07 22:25:06 +000057 false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000058
Chris Lattnerd99bf492003-02-22 23:57:48 +000059bool PromotePass::runOnFunction(Function &F) {
60 std::vector<AllocaInst*> Allocas;
61
Chris Lattner02a3be02003-09-20 14:39:18 +000062 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
Chris Lattnerd99bf492003-02-22 23:57:48 +000063
Chris Lattner83c39d22003-06-25 14:58:56 +000064 bool Changed = false;
Chris Lattner43f820d2003-10-05 21:20:13 +000065
Devang Patel326821e2007-06-07 21:57:03 +000066 DominatorTree &DT = getAnalysis<DominatorTree>();
Chandler Carruth6c3a95d2013-07-28 06:43:11 +000067 const DataLayout *DL = getAnalysisIfAvailable<DataLayout>();
Misha Brukmanfd939082005-04-21 23:48:37 +000068
Chris Lattner83c39d22003-06-25 14:58:56 +000069 while (1) {
70 Allocas.clear();
Chris Lattnerd99bf492003-02-22 23:57:48 +000071
Chris Lattner83c39d22003-06-25 14:58:56 +000072 // Find allocas that are safe to promote, by looking at all instructions in
73 // the entry node
74 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
75 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
Chandler Carruth6c3a95d2013-07-28 06:43:11 +000076 if (isAllocaPromotable(AI, DL))
Chris Lattner83c39d22003-06-25 14:58:56 +000077 Allocas.push_back(AI);
78
79 if (Allocas.empty()) break;
80
Chandler Carruth6c3a95d2013-07-28 06:43:11 +000081 PromoteMemToReg(Allocas, DT, DL);
Chris Lattnerd99bf492003-02-22 23:57:48 +000082 NumPromoted += Allocas.size();
Chris Lattner83c39d22003-06-25 14:58:56 +000083 Changed = true;
Chris Lattnerd99bf492003-02-22 23:57:48 +000084 }
Chris Lattner83c39d22003-06-25 14:58:56 +000085
86 return Changed;
Chris Lattnerd99bf492003-02-22 23:57:48 +000087}
88
89// createPromoteMemoryToRegister - Provide an entry point to create this pass.
90//
Alkis Evlogimenosab7ada32005-03-28 02:01:12 +000091FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
Chris Lattnerd99bf492003-02-22 23:57:48 +000092 return new PromotePass();
93}