blob: 24b3b12930ac5f05165fe3fbdbf3326c415753ca [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 Carruth66b31302015-01-04 12:03:27 +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
Davide Italianocccf4f02016-06-14 03:22:22 +000030static bool promoteMemoryToRegister(Function &F, DominatorTree &DT,
31 AssumptionCache &AC) {
32 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
Chandler Carruth66b31302015-01-04 12:03:27 +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);
58 auto &AC = AM.getResult<AssumptionAnalysis>(F);
59 if (!promoteMemoryToRegister(F, DT, AC))
60 return PreservedAnalyses::all();
61
Michael Kuperstein835facd2016-06-28 00:54:12 +000062 // FIXME: This should also 'preserve the CFG'.
Davide Italianocccf4f02016-06-14 03:22:22 +000063 return PreservedAnalyses::none();
64}
65
66namespace {
67struct PromoteLegacyPass : public FunctionPass {
68 static char ID; // Pass identification, replacement for typeid
69 PromoteLegacyPass() : FunctionPass(ID) {
70 initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry());
71 }
72
73 // runOnFunction - To run this pass, first we calculate the alloca
74 // instructions that are safe for promotion, then we promote each one.
75 //
76 bool runOnFunction(Function &F) override {
77 if (skipFunction(F))
78 return false;
79
80 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
81 AssumptionCache &AC =
82 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
83 return promoteMemoryToRegister(F, DT, AC);
84 }
85
86 void getAnalysisUsage(AnalysisUsage &AU) const override {
87 AU.addRequired<AssumptionCacheTracker>();
88 AU.addRequired<DominatorTreeWrapperPass>();
89 AU.setPreservesCFG();
Davide Italianocccf4f02016-06-14 03:22:22 +000090 }
91 };
92} // end of anonymous namespace
93
94char PromoteLegacyPass::ID = 0;
95INITIALIZE_PASS_BEGIN(PromoteLegacyPass, "mem2reg", "Promote Memory to "
96 "Register",
97 false, false)
98INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
99INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
100INITIALIZE_PASS_END(PromoteLegacyPass, "mem2reg", "Promote Memory to Register",
101 false, false)
102
Chris Lattner26f15902003-02-22 23:57:48 +0000103// createPromoteMemoryToRegister - Provide an entry point to create this pass.
104//
Alkis Evlogimenos9ead0d72005-03-28 02:01:12 +0000105FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
Davide Italianocccf4f02016-06-14 03:22:22 +0000106 return new PromoteLegacyPass();
Chris Lattner26f15902003-02-22 23:57:48 +0000107}