blob: 617ad66d37def16c0a91b1c984b32001eed8b786 [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//
Chris Lattnerf3ebc3f2007-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 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
Davide Italianocccf4f02016-06-14 03:22:22 +000015#include "llvm/Transforms/Utils/Mem2Reg.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Statistic.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000017#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Function.h"
19#include "llvm/IR/Instructions.h"
Davide Italianocccf4f02016-06-14 03:22:22 +000020#include "llvm/Transforms/Scalar.h"
Chris Lattner26f15902003-02-22 23:57:48 +000021#include "llvm/Transforms/Utils/PromoteMemToReg.h"
Chris Lattner4fe87d62006-05-09 04:13:41 +000022#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattner49525f82004-01-09 06:02:20 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Chandler Carruth964daaa2014-04-22 02:55:47 +000025#define DEBUG_TYPE "mem2reg"
26
Chris Lattner45f966d2006-12-19 22:17:40 +000027STATISTIC(NumPromoted, "Number of alloca's promoted");
Chris Lattner26f15902003-02-22 23:57:48 +000028
Hal Finkel3ca4a6b2016-12-15 03:02:15 +000029static bool promoteMemoryToRegister(Function &F, DominatorTree &DT) {
Davide Italianocccf4f02016-06-14 03:22:22 +000030 std::vector<AllocaInst *> Allocas;
31 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
32 bool Changed = false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000033
Chris Lattnerb396afd2003-06-25 14:58:56 +000034 while (1) {
35 Allocas.clear();
Chris Lattner26f15902003-02-22 23:57:48 +000036
Chris Lattnerb396afd2003-06-25 14:58:56 +000037 // Find allocas that are safe to promote, by looking at all instructions in
38 // the entry node
39 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
Davide Italianocccf4f02016-06-14 03:22:22 +000040 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
Nick Lewyckyc7776f72013-08-13 22:51:58 +000041 if (isAllocaPromotable(AI))
Chris Lattnerb396afd2003-06-25 14:58:56 +000042 Allocas.push_back(AI);
43
Davide Italianocccf4f02016-06-14 03:22:22 +000044 if (Allocas.empty())
45 break;
Chris Lattnerb396afd2003-06-25 14:58:56 +000046
Hal Finkel3ca4a6b2016-12-15 03:02:15 +000047 PromoteMemToReg(Allocas, DT, nullptr);
Chris Lattner26f15902003-02-22 23:57:48 +000048 NumPromoted += Allocas.size();
Chris Lattnerb396afd2003-06-25 14:58:56 +000049 Changed = true;
Chris Lattner26f15902003-02-22 23:57:48 +000050 }
Chris Lattnerb396afd2003-06-25 14:58:56 +000051 return Changed;
Chris Lattner26f15902003-02-22 23:57:48 +000052}
53
Sean Silva36e0d012016-08-09 00:28:15 +000054PreservedAnalyses PromotePass::run(Function &F, FunctionAnalysisManager &AM) {
Davide Italianocccf4f02016-06-14 03:22:22 +000055 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
Hal Finkel3ca4a6b2016-12-15 03:02:15 +000056 if (!promoteMemoryToRegister(F, DT))
Davide Italianocccf4f02016-06-14 03:22:22 +000057 return PreservedAnalyses::all();
58
Michael Kuperstein835facd2016-06-28 00:54:12 +000059 // FIXME: This should also 'preserve the CFG'.
Davide Italianocccf4f02016-06-14 03:22:22 +000060 return PreservedAnalyses::none();
61}
62
63namespace {
64struct PromoteLegacyPass : public FunctionPass {
65 static char ID; // Pass identification, replacement for typeid
66 PromoteLegacyPass() : FunctionPass(ID) {
67 initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry());
68 }
69
70 // runOnFunction - To run this pass, first we calculate the alloca
71 // instructions that are safe for promotion, then we promote each one.
72 //
73 bool runOnFunction(Function &F) override {
74 if (skipFunction(F))
75 return false;
76
77 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Hal Finkel3ca4a6b2016-12-15 03:02:15 +000078 return promoteMemoryToRegister(F, DT);
Davide Italianocccf4f02016-06-14 03:22:22 +000079 }
80
81 void getAnalysisUsage(AnalysisUsage &AU) const override {
Davide Italianocccf4f02016-06-14 03:22:22 +000082 AU.addRequired<DominatorTreeWrapperPass>();
83 AU.setPreservesCFG();
Davide Italianocccf4f02016-06-14 03:22:22 +000084 }
85 };
86} // end of anonymous namespace
87
88char PromoteLegacyPass::ID = 0;
89INITIALIZE_PASS_BEGIN(PromoteLegacyPass, "mem2reg", "Promote Memory to "
90 "Register",
91 false, false)
Davide Italianocccf4f02016-06-14 03:22:22 +000092INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
93INITIALIZE_PASS_END(PromoteLegacyPass, "mem2reg", "Promote Memory to Register",
94 false, false)
95
Chris Lattner26f15902003-02-22 23:57:48 +000096// createPromoteMemoryToRegister - Provide an entry point to create this pass.
97//
Alkis Evlogimenos9ead0d72005-03-28 02:01:12 +000098FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
Davide Italianocccf4f02016-06-14 03:22:22 +000099 return new PromoteLegacyPass();
Chris Lattner26f15902003-02-22 23:57:48 +0000100}