blob: 6cf304003b78b08647b802769506489fb5120f2a [file] [log] [blame]
Devang Patel50c66cd2008-09-03 19:52:17 +00001//===- InlineAlways.cpp - Code to inline always_inline functions ----------===//
Devang Patel924d9082008-09-03 18:50:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Devang Patel50c66cd2008-09-03 19:52:17 +000010// This file implements a custom inliner that handles only functions that
Devang Patela563d242008-09-03 20:25:40 +000011// are marked as "always inline".
Devang Patel924d9082008-09-03 18:50:53 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "inline"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Transforms/IPO.h"
17#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/Analysis/CallGraph.h"
19#include "llvm/Analysis/InlineCost.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000020#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/CallingConv.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Instructions.h"
24#include "llvm/IR/IntrinsicInst.h"
25#include "llvm/IR/Module.h"
26#include "llvm/IR/Type.h"
Devang Patel924d9082008-09-03 18:50:53 +000027#include "llvm/Transforms/IPO/InlinerPass.h"
Devang Patel924d9082008-09-03 18:50:53 +000028
29using namespace llvm;
30
31namespace {
32
Chandler Carruth7e88e742013-01-21 11:39:16 +000033/// \brief Inliner pass which only handles "always inline" functions.
34class AlwaysInliner : public Inliner {
Chandler Carruth4319e292013-01-21 11:39:18 +000035 InlineCostAnalysis *ICA;
Matt Beaumont-Gayabfc4462012-12-04 05:41:27 +000036
Chandler Carruth7e88e742013-01-21 11:39:16 +000037public:
38 // Use extremely low threshold.
Chandler Carruth4319e292013-01-21 11:39:18 +000039 AlwaysInliner() : Inliner(ID, -2000000000, /*InsertLifetime*/ true), ICA(0) {
Chandler Carruth7e88e742013-01-21 11:39:16 +000040 initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
41 }
Matt Beaumont-Gayabfc4462012-12-04 05:41:27 +000042
Chandler Carruth7e88e742013-01-21 11:39:16 +000043 AlwaysInliner(bool InsertLifetime)
Chandler Carruth4319e292013-01-21 11:39:18 +000044 : Inliner(ID, -2000000000, InsertLifetime), ICA(0) {
Chandler Carruth7e88e742013-01-21 11:39:16 +000045 initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
46 }
47
48 static char ID; // Pass identification, replacement for typeid
49
Craig Topper3e4c6972014-03-05 09:10:37 +000050 InlineCost getInlineCost(CallSite CS) override;
Chandler Carruth7e88e742013-01-21 11:39:16 +000051
Craig Topper3e4c6972014-03-05 09:10:37 +000052 void getAnalysisUsage(AnalysisUsage &AU) const override;
53 bool runOnSCC(CallGraphSCC &SCC) override;
Chandler Carruth4319e292013-01-21 11:39:18 +000054
Chandler Carruth7e88e742013-01-21 11:39:16 +000055 using llvm::Pass::doFinalization;
Craig Topper3e4c6972014-03-05 09:10:37 +000056 bool doFinalization(CallGraph &CG) override {
Chandler Carruth7e88e742013-01-21 11:39:16 +000057 return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/ true);
58 }
Chandler Carruth7e88e742013-01-21 11:39:16 +000059};
60
Devang Patel924d9082008-09-03 18:50:53 +000061}
62
63char AlwaysInliner::ID = 0;
Owen Anderson6c18d1a2010-10-19 17:21:58 +000064INITIALIZE_PASS_BEGIN(AlwaysInliner, "always-inline",
65 "Inliner for always_inline functions", false, false)
Chandler Carruth6378cf52013-11-26 04:19:30 +000066INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruth4319e292013-01-21 11:39:18 +000067INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis)
Owen Anderson6c18d1a2010-10-19 17:21:58 +000068INITIALIZE_PASS_END(AlwaysInliner, "always-inline",
Owen Andersondf7a4f22010-10-07 22:25:06 +000069 "Inliner for always_inline functions", false, false)
Devang Patel924d9082008-09-03 18:50:53 +000070
71Pass *llvm::createAlwaysInlinerPass() { return new AlwaysInliner(); }
72
Chad Rosier07d37bc2012-02-25 02:56:01 +000073Pass *llvm::createAlwaysInlinerPass(bool InsertLifetime) {
74 return new AlwaysInliner(InsertLifetime);
75}
76
Chandler Carrutha88a0fa2012-03-31 13:17:18 +000077/// \brief Get the inline cost for the always-inliner.
78///
79/// The always inliner *only* handles functions which are marked with the
80/// attribute to force inlining. As such, it is dramatically simpler and avoids
81/// using the powerful (but expensive) inline cost analysis. Instead it uses
82/// a very simple and boring direct walk of the instructions looking for
83/// impossible-to-inline constructs.
84///
85/// Note, it would be possible to go to some lengths to cache the information
86/// computed here, but as we only expect to do this for relatively few and
87/// small functions which have the explicit attribute to force inlining, it is
88/// likely not worth it in practice.
89InlineCost AlwaysInliner::getInlineCost(CallSite CS) {
90 Function *Callee = CS.getCalledFunction();
Chandler Carrutha88a0fa2012-03-31 13:17:18 +000091
Bob Wilsona5b0dc82012-11-19 07:04:35 +000092 // Only inline direct calls to functions with always-inline attributes
93 // that are viable for inlining. FIXME: We shouldn't even get here for
94 // declarations.
95 if (Callee && !Callee->isDeclaration() &&
Bill Wendling698e84f2012-12-30 10:32:01 +000096 Callee->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
97 Attribute::AlwaysInline) &&
Chandler Carruth4319e292013-01-21 11:39:18 +000098 ICA->isInlineViable(*Callee))
Bob Wilsona5b0dc82012-11-19 07:04:35 +000099 return InlineCost::getAlways();
Chandler Carrutha88a0fa2012-03-31 13:17:18 +0000100
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000101 return InlineCost::getNever();
Chandler Carrutha88a0fa2012-03-31 13:17:18 +0000102}
103
Chandler Carruth4319e292013-01-21 11:39:18 +0000104bool AlwaysInliner::runOnSCC(CallGraphSCC &SCC) {
105 ICA = &getAnalysis<InlineCostAnalysis>();
106 return Inliner::runOnSCC(SCC);
107}
108
109void AlwaysInliner::getAnalysisUsage(AnalysisUsage &AU) const {
110 AU.addRequired<InlineCostAnalysis>();
111 Inliner::getAnalysisUsage(AU);
Devang Patel924d9082008-09-03 18:50:53 +0000112}