blob: 189caa7d145a58612691e4de072742c1f348c26f [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
15#include "llvm/Transforms/Scalar.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/Statistic.h"
Stephen Hines36b56882014-04-23 16:57:46 -070017#include "llvm/IR/Dominators.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/Function.h"
19#include "llvm/IR/Instructions.h"
Chris Lattnerd99bf492003-02-22 23:57:48 +000020#include "llvm/Transforms/Utils/PromoteMemToReg.h"
Chris Lattner8d89e7b2006-05-09 04:13:41 +000021#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Stephen Hinesdce4a402014-05-29 02:49:00 -070024#define DEBUG_TYPE "mem2reg"
25
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
Owen Anderson081c34b2010-10-19 17:21:58 +000031 PromotePass() : FunctionPass(ID) {
32 initializePromotePassPass(*PassRegistry::getPassRegistry());
33 }
Devang Patel794fd752007-05-01 21:15:47 +000034
Chris Lattnerd99bf492003-02-22 23:57:48 +000035 // runOnFunction - To run this pass, first we calculate the alloca
36 // instructions that are safe for promotion, then we promote each one.
37 //
Stephen Hines36b56882014-04-23 16:57:46 -070038 bool runOnFunction(Function &F) override;
Chris Lattnerd99bf492003-02-22 23:57:48 +000039
Stephen Hines36b56882014-04-23 16:57:46 -070040 void getAnalysisUsage(AnalysisUsage &AU) const override {
41 AU.addRequired<DominatorTreeWrapperPass>();
Chris Lattnerd99bf492003-02-22 23:57:48 +000042 AU.setPreservesCFG();
Anton Korobeynikovbed29462007-04-16 18:10:23 +000043 // This is a cluster of orthogonal Transforms
Chris Lattner8d89e7b2006-05-09 04:13:41 +000044 AU.addPreserved<UnifyFunctionExitNodes>();
Chris Lattner8d89e7b2006-05-09 04:13:41 +000045 AU.addPreservedID(LowerSwitchID);
Chris Lattnered96fe82006-05-17 21:05:27 +000046 AU.addPreservedID(LowerInvokePassID);
Chris Lattnerd99bf492003-02-22 23:57:48 +000047 }
48 };
Chris Lattnerd99bf492003-02-22 23:57:48 +000049} // end of anonymous namespace
50
Dan Gohman844731a2008-05-13 00:00:25 +000051char PromotePass::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000052INITIALIZE_PASS_BEGIN(PromotePass, "mem2reg", "Promote Memory to Register",
53 false, false)
Stephen Hines36b56882014-04-23 16:57:46 -070054INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Owen Anderson2ab36d32010-10-12 19:48:12 +000055INITIALIZE_PASS_END(PromotePass, "mem2reg", "Promote Memory to Register",
Owen Andersonce665bd2010-10-07 22:25:06 +000056 false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000057
Chris Lattnerd99bf492003-02-22 23:57:48 +000058bool PromotePass::runOnFunction(Function &F) {
59 std::vector<AllocaInst*> Allocas;
60
Chris Lattner02a3be02003-09-20 14:39:18 +000061 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
Chris Lattnerd99bf492003-02-22 23:57:48 +000062
Chris Lattner83c39d22003-06-25 14:58:56 +000063 bool Changed = false;
Chris Lattner43f820d2003-10-05 21:20:13 +000064
Stephen Hines36b56882014-04-23 16:57:46 -070065 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Misha Brukmanfd939082005-04-21 23:48:37 +000066
Chris Lattner83c39d22003-06-25 14:58:56 +000067 while (1) {
68 Allocas.clear();
Chris Lattnerd99bf492003-02-22 23:57:48 +000069
Chris Lattner83c39d22003-06-25 14:58:56 +000070 // Find allocas that are safe to promote, by looking at all instructions in
71 // the entry node
72 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
73 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
Nick Lewycky6c1fa7c2013-08-13 22:51:58 +000074 if (isAllocaPromotable(AI))
Chris Lattner83c39d22003-06-25 14:58:56 +000075 Allocas.push_back(AI);
76
77 if (Allocas.empty()) break;
78
Nick Lewycky6c1fa7c2013-08-13 22:51:58 +000079 PromoteMemToReg(Allocas, DT);
Chris Lattnerd99bf492003-02-22 23:57:48 +000080 NumPromoted += Allocas.size();
Chris Lattner83c39d22003-06-25 14:58:56 +000081 Changed = true;
Chris Lattnerd99bf492003-02-22 23:57:48 +000082 }
Chris Lattner83c39d22003-06-25 14:58:56 +000083
84 return Changed;
Chris Lattnerd99bf492003-02-22 23:57:48 +000085}
86
87// 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}