blob: 4984c3cc2a72e96c1dfd20a02b57499dac401465 [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"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000017#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000018#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Function.h"
20#include "llvm/IR/Instructions.h"
Davide Italianocccf4f02016-06-14 03:22:22 +000021#include "llvm/Transforms/Scalar.h"
Chris Lattner26f15902003-02-22 23:57:48 +000022#include "llvm/Transforms/Utils/PromoteMemToReg.h"
Chris Lattner4fe87d62006-05-09 04:13:41 +000023#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattner49525f82004-01-09 06:02:20 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chandler Carruth964daaa2014-04-22 02:55:47 +000026#define DEBUG_TYPE "mem2reg"
27
Chris Lattner45f966d2006-12-19 22:17:40 +000028STATISTIC(NumPromoted, "Number of alloca's promoted");
Chris Lattner26f15902003-02-22 23:57:48 +000029
Daniel Jasperaec2fa32016-12-19 08:22:17 +000030static bool promoteMemoryToRegister(Function &F, DominatorTree &DT,
31 AssumptionCache &AC) {
Davide Italianocccf4f02016-06-14 03:22:22 +000032 std::vector<AllocaInst *> Allocas;
33 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
34 bool Changed = false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000035
Chris Lattnerb396afd2003-06-25 14:58:56 +000036 while (1) {
37 Allocas.clear();
Chris Lattner26f15902003-02-22 23:57:48 +000038
Chris Lattnerb396afd2003-06-25 14:58:56 +000039 // Find allocas that are safe to promote, by looking at all instructions in
40 // the entry node
41 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
Davide Italianocccf4f02016-06-14 03:22:22 +000042 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
Nick Lewyckyc7776f72013-08-13 22:51:58 +000043 if (isAllocaPromotable(AI))
Chris Lattnerb396afd2003-06-25 14:58:56 +000044 Allocas.push_back(AI);
45
Davide Italianocccf4f02016-06-14 03:22:22 +000046 if (Allocas.empty())
47 break;
Chris Lattnerb396afd2003-06-25 14:58:56 +000048
Daniel Jasperaec2fa32016-12-19 08:22:17 +000049 PromoteMemToReg(Allocas, DT, nullptr, &AC);
Chris Lattner26f15902003-02-22 23:57:48 +000050 NumPromoted += Allocas.size();
Chris Lattnerb396afd2003-06-25 14:58:56 +000051 Changed = true;
Chris Lattner26f15902003-02-22 23:57:48 +000052 }
Chris Lattnerb396afd2003-06-25 14:58:56 +000053 return Changed;
Chris Lattner26f15902003-02-22 23:57:48 +000054}
55
Sean Silva36e0d012016-08-09 00:28:15 +000056PreservedAnalyses PromotePass::run(Function &F, FunctionAnalysisManager &AM) {
Davide Italianocccf4f02016-06-14 03:22:22 +000057 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
Daniel Jasperaec2fa32016-12-19 08:22:17 +000058 auto &AC = AM.getResult<AssumptionAnalysis>(F);
59 if (!promoteMemoryToRegister(F, DT, AC))
Davide Italianocccf4f02016-06-14 03:22:22 +000060 return PreservedAnalyses::all();
61
Chandler Carruthca68a3e2017-01-15 06:32:49 +000062 PreservedAnalyses PA;
63 PA.preserveSet<CFGAnalyses>();
64 return PA;
Davide Italianocccf4f02016-06-14 03:22:22 +000065}
66
67namespace {
68struct PromoteLegacyPass : public FunctionPass {
69 static char ID; // Pass identification, replacement for typeid
70 PromoteLegacyPass() : FunctionPass(ID) {
71 initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry());
72 }
73
74 // runOnFunction - To run this pass, first we calculate the alloca
75 // instructions that are safe for promotion, then we promote each one.
76 //
77 bool runOnFunction(Function &F) override {
78 if (skipFunction(F))
79 return false;
80
81 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Daniel Jasperaec2fa32016-12-19 08:22:17 +000082 AssumptionCache &AC =
83 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
84 return promoteMemoryToRegister(F, DT, AC);
Davide Italianocccf4f02016-06-14 03:22:22 +000085 }
86
87 void getAnalysisUsage(AnalysisUsage &AU) const override {
Daniel Jasperaec2fa32016-12-19 08:22:17 +000088 AU.addRequired<AssumptionCacheTracker>();
Davide Italianocccf4f02016-06-14 03:22:22 +000089 AU.addRequired<DominatorTreeWrapperPass>();
90 AU.setPreservesCFG();
Davide Italianocccf4f02016-06-14 03:22:22 +000091 }
92 };
93} // end of anonymous namespace
94
95char PromoteLegacyPass::ID = 0;
96INITIALIZE_PASS_BEGIN(PromoteLegacyPass, "mem2reg", "Promote Memory to "
97 "Register",
98 false, false)
Daniel Jasperaec2fa32016-12-19 08:22:17 +000099INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Davide Italianocccf4f02016-06-14 03:22:22 +0000100INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
101INITIALIZE_PASS_END(PromoteLegacyPass, "mem2reg", "Promote Memory to Register",
102 false, false)
103
Chris Lattner26f15902003-02-22 23:57:48 +0000104// createPromoteMemoryToRegister - Provide an entry point to create this pass.
105//
Alkis Evlogimenos9ead0d72005-03-28 02:01:12 +0000106FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
Davide Italianocccf4f02016-06-14 03:22:22 +0000107 return new PromoteLegacyPass();
Chris Lattner26f15902003-02-22 23:57:48 +0000108}