blob: 0f45c5f95aadf26f13d97a694e79e4057428d032 [file] [log] [blame]
Chris Lattnerd075cc22003-08-31 19:10:30 +00001//===- InlineSimple.cpp - Code to perform simple function inlining --------===//
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 Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner530d4bf2003-05-29 15:11:31 +000010// This file implements bottom-up inlining of functions into callees.
Chris Lattner1c8d3f82002-04-18 18:52:03 +000011//
Chris Lattner2f7c9632001-06-06 20:29:01 +000012//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/Transforms/IPO.h"
Chandler Carruth66b31302015-01-04 12:03:27 +000015#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Analysis/CallGraph.h"
17#include "llvm/Analysis/InlineCost.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000018#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000019#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/CallingConv.h"
21#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/IR/IntrinsicInst.h"
24#include "llvm/IR/Module.h"
25#include "llvm/IR/Type.h"
Tanya Lattnerab11b1c2007-06-19 22:29:50 +000026#include "llvm/Transforms/IPO/InlinerPass.h"
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000027
Chris Lattner51c28a52003-11-21 21:46:09 +000028using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000029
Chandler Carruth964daaa2014-04-22 02:55:47 +000030#define DEBUG_TYPE "inline"
31
Chris Lattner04805fa2002-02-26 21:46:54 +000032namespace {
Chris Lattner6dc0ae22003-10-06 15:52:43 +000033
David Majnemer927df852013-11-03 11:09:39 +000034/// \brief Actual inliner pass implementation.
Chandler Carruth0df3e532013-01-21 11:39:14 +000035///
36/// The common implementation of the inlining logic is shared between this
37/// inliner pass and the always inliner pass. The two passes use different cost
38/// analyses to determine when to inline.
39class SimpleInliner : public Inliner {
Chandler Carruth4319e292013-01-21 11:39:18 +000040 InlineCostAnalysis *ICA;
Chandler Carruth0df3e532013-01-21 11:39:14 +000041
42public:
Craig Topperf40110f2014-04-25 05:29:35 +000043 SimpleInliner() : Inliner(ID), ICA(nullptr) {
Chandler Carruth0df3e532013-01-21 11:39:14 +000044 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
45 }
46
47 SimpleInliner(int Threshold)
Craig Topperf40110f2014-04-25 05:29:35 +000048 : Inliner(ID, Threshold, /*InsertLifetime*/ true), ICA(nullptr) {
Chandler Carruth0df3e532013-01-21 11:39:14 +000049 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
50 }
51
52 static char ID; // Pass identification, replacement for typeid
53
Craig Topper3e4c6972014-03-05 09:10:37 +000054 InlineCost getInlineCost(CallSite CS) override {
Chandler Carruth4319e292013-01-21 11:39:18 +000055 return ICA->getInlineCost(CS, getInlineThreshold(CS));
Chandler Carruth0df3e532013-01-21 11:39:14 +000056 }
57
Craig Topper3e4c6972014-03-05 09:10:37 +000058 bool runOnSCC(CallGraphSCC &SCC) override;
59 void getAnalysisUsage(AnalysisUsage &AU) const override;
Chandler Carruth0df3e532013-01-21 11:39:14 +000060};
61
Eli Bendersky49f65652014-03-12 16:12:36 +000062static int computeThresholdFromOptLevels(unsigned OptLevel,
63 unsigned SizeOptLevel) {
64 if (OptLevel > 2)
65 return 275;
Eli Bendersky95b540f2014-03-12 16:44:17 +000066 if (SizeOptLevel == 1) // -Os
Eli Bendersky49f65652014-03-12 16:12:36 +000067 return 75;
Eli Bendersky95b540f2014-03-12 16:44:17 +000068 if (SizeOptLevel == 2) // -Oz
Eli Bendersky49f65652014-03-12 16:12:36 +000069 return 25;
70 return 225;
71}
72
Chandler Carruth0df3e532013-01-21 11:39:14 +000073} // end anonymous namespace
Chris Lattner04805fa2002-02-26 21:46:54 +000074
Dan Gohmand78c4002008-05-13 00:00:25 +000075char SimpleInliner::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +000076INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
77 "Function Integration/Inlining", false, false)
Chandler Carruth66b31302015-01-04 12:03:27 +000078INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth6378cf52013-11-26 04:19:30 +000079INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruth4319e292013-01-21 11:39:18 +000080INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis)
Chandler Carruth7b560d42015-09-09 17:55:00 +000081INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Owen Anderson071cee02010-10-13 22:00:45 +000082INITIALIZE_PASS_END(SimpleInliner, "inline",
Owen Andersondf7a4f22010-10-07 22:25:06 +000083 "Function Integration/Inlining", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000084
Devang Patel13058a52007-01-26 00:47:38 +000085Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
Chris Lattner51c28a52003-11-21 21:46:09 +000086
Andrew Trickcaa500b2011-10-01 01:27:56 +000087Pass *llvm::createFunctionInliningPass(int Threshold) {
Chris Lattner22ad7ab2008-01-12 06:49:13 +000088 return new SimpleInliner(Threshold);
89}
90
Eli Bendersky49f65652014-03-12 16:12:36 +000091Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
92 unsigned SizeOptLevel) {
93 return new SimpleInliner(
94 computeThresholdFromOptLevels(OptLevel, SizeOptLevel));
95}
96
Chandler Carruth4319e292013-01-21 11:39:18 +000097bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
98 ICA = &getAnalysis<InlineCostAnalysis>();
99 return Inliner::runOnSCC(SCC);
Tanya Lattnercb90f1d2007-06-06 21:59:26 +0000100}
Devang Patel33227112007-07-25 18:00:25 +0000101
Chandler Carruth4319e292013-01-21 11:39:18 +0000102void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
103 AU.addRequired<InlineCostAnalysis>();
104 Inliner::getAnalysisUsage(AU);
105}