blob: aa1e35ddba024f458e3d2241c65b31d1fe361c80 [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
15#include "llvm/Transforms/Scalar.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"
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
Chris Lattner45f966d2006-12-19 22:17:40 +000029namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000030 struct PromotePass : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000031 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000032 PromotePass() : FunctionPass(ID) {
33 initializePromotePassPass(*PassRegistry::getPassRegistry());
34 }
Devang Patel09f162c2007-05-01 21:15:47 +000035
Chris Lattner26f15902003-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 //
Craig Topper3e4c6972014-03-05 09:10:37 +000039 bool runOnFunction(Function &F) override;
Chris Lattner26f15902003-02-22 23:57:48 +000040
Craig Topper3e4c6972014-03-05 09:10:37 +000041 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth66b31302015-01-04 12:03:27 +000042 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruth73523022014-01-13 13:07:17 +000043 AU.addRequired<DominatorTreeWrapperPass>();
Chris Lattner26f15902003-02-22 23:57:48 +000044 AU.setPreservesCFG();
Anton Korobeynikovfb801512007-04-16 18:10:23 +000045 // This is a cluster of orthogonal Transforms
Chris Lattner4fe87d62006-05-09 04:13:41 +000046 AU.addPreserved<UnifyFunctionExitNodes>();
Chris Lattner4fe87d62006-05-09 04:13:41 +000047 AU.addPreservedID(LowerSwitchID);
Chris Lattnere4cb4762006-05-17 21:05:27 +000048 AU.addPreservedID(LowerInvokePassID);
Chris Lattner26f15902003-02-22 23:57:48 +000049 }
50 };
Chris Lattner26f15902003-02-22 23:57:48 +000051} // end of anonymous namespace
52
Dan Gohmand78c4002008-05-13 00:00:25 +000053char PromotePass::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000054INITIALIZE_PASS_BEGIN(PromotePass, "mem2reg", "Promote Memory to Register",
55 false, false)
Chandler Carruth66b31302015-01-04 12:03:27 +000056INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth73523022014-01-13 13:07:17 +000057INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +000058INITIALIZE_PASS_END(PromotePass, "mem2reg", "Promote Memory to Register",
Owen Andersondf7a4f22010-10-07 22:25:06 +000059 false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000060
Chris Lattner26f15902003-02-22 23:57:48 +000061bool PromotePass::runOnFunction(Function &F) {
62 std::vector<AllocaInst*> Allocas;
63
Chris Lattner5dac64f2003-09-20 14:39:18 +000064 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
Chris Lattner26f15902003-02-22 23:57:48 +000065
Vedant Kumar6013f452016-04-22 06:51:37 +000066 if (F.hasFnAttribute(Attribute::OptimizeNone))
67 return false;
68
Chris Lattnerb396afd2003-06-25 14:58:56 +000069 bool Changed = false;
Chris Lattnera906bac2003-10-05 21:20:13 +000070
Chandler Carruth73523022014-01-13 13:07:17 +000071 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Chandler Carruth66b31302015-01-04 12:03:27 +000072 AssumptionCache &AC =
73 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Misha Brukmanb1c93172005-04-21 23:48:37 +000074
Chris Lattnerb396afd2003-06-25 14:58:56 +000075 while (1) {
76 Allocas.clear();
Chris Lattner26f15902003-02-22 23:57:48 +000077
Chris Lattnerb396afd2003-06-25 14:58:56 +000078 // Find allocas that are safe to promote, by looking at all instructions in
79 // the entry node
80 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
81 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
Nick Lewyckyc7776f72013-08-13 22:51:58 +000082 if (isAllocaPromotable(AI))
Chris Lattnerb396afd2003-06-25 14:58:56 +000083 Allocas.push_back(AI);
84
85 if (Allocas.empty()) break;
86
Chandler Carruth66b31302015-01-04 12:03:27 +000087 PromoteMemToReg(Allocas, DT, nullptr, &AC);
Chris Lattner26f15902003-02-22 23:57:48 +000088 NumPromoted += Allocas.size();
Chris Lattnerb396afd2003-06-25 14:58:56 +000089 Changed = true;
Chris Lattner26f15902003-02-22 23:57:48 +000090 }
Chris Lattnerb396afd2003-06-25 14:58:56 +000091
92 return Changed;
Chris Lattner26f15902003-02-22 23:57:48 +000093}
94
95// createPromoteMemoryToRegister - Provide an entry point to create this pass.
96//
Alkis Evlogimenos9ead0d72005-03-28 02:01:12 +000097FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
Chris Lattner26f15902003-02-22 23:57:48 +000098 return new PromotePass();
99}