blob: 18c2540910fa2bef725bca61e712c295b66eee56 [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/Transforms/IPO.h"
16#include "llvm/ADT/SmallPtrSet.h"
Hal Finkel0c083022014-09-01 09:01:39 +000017#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#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
Chandler Carruth964daaa2014-04-22 02:55:47 +000031#define DEBUG_TYPE "inline"
32
Devang Patel924d9082008-09-03 18:50:53 +000033namespace {
34
Chandler Carruth7e88e742013-01-21 11:39:16 +000035/// \brief Inliner pass which only handles "always inline" functions.
36class AlwaysInliner : public Inliner {
Chandler Carruth4319e292013-01-21 11:39:18 +000037 InlineCostAnalysis *ICA;
Matt Beaumont-Gayabfc4462012-12-04 05:41:27 +000038
Chandler Carruth7e88e742013-01-21 11:39:16 +000039public:
40 // Use extremely low threshold.
Craig Topperf40110f2014-04-25 05:29:35 +000041 AlwaysInliner() : Inliner(ID, -2000000000, /*InsertLifetime*/ true),
42 ICA(nullptr) {
Chandler Carruth7e88e742013-01-21 11:39:16 +000043 initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
44 }
Matt Beaumont-Gayabfc4462012-12-04 05:41:27 +000045
Chandler Carruth7e88e742013-01-21 11:39:16 +000046 AlwaysInliner(bool InsertLifetime)
Craig Topperf40110f2014-04-25 05:29:35 +000047 : Inliner(ID, -2000000000, InsertLifetime), ICA(nullptr) {
Chandler Carruth7e88e742013-01-21 11:39:16 +000048 initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
49 }
50
51 static char ID; // Pass identification, replacement for typeid
52
Craig Topper3e4c6972014-03-05 09:10:37 +000053 InlineCost getInlineCost(CallSite CS) override;
Chandler Carruth7e88e742013-01-21 11:39:16 +000054
Craig Topper3e4c6972014-03-05 09:10:37 +000055 void getAnalysisUsage(AnalysisUsage &AU) const override;
56 bool runOnSCC(CallGraphSCC &SCC) override;
Chandler Carruth4319e292013-01-21 11:39:18 +000057
Chandler Carruth7e88e742013-01-21 11:39:16 +000058 using llvm::Pass::doFinalization;
Craig Topper3e4c6972014-03-05 09:10:37 +000059 bool doFinalization(CallGraph &CG) override {
Chandler Carruth7e88e742013-01-21 11:39:16 +000060 return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/ true);
61 }
Chandler Carruth7e88e742013-01-21 11:39:16 +000062};
63
Devang Patel924d9082008-09-03 18:50:53 +000064}
65
66char AlwaysInliner::ID = 0;
Owen Anderson6c18d1a2010-10-19 17:21:58 +000067INITIALIZE_PASS_BEGIN(AlwaysInliner, "always-inline",
68 "Inliner for always_inline functions", false, false)
Hal Finkel0c083022014-09-01 09:01:39 +000069INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
Chandler Carruth6378cf52013-11-26 04:19:30 +000070INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruth4319e292013-01-21 11:39:18 +000071INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis)
Owen Anderson6c18d1a2010-10-19 17:21:58 +000072INITIALIZE_PASS_END(AlwaysInliner, "always-inline",
Owen Andersondf7a4f22010-10-07 22:25:06 +000073 "Inliner for always_inline functions", false, false)
Devang Patel924d9082008-09-03 18:50:53 +000074
75Pass *llvm::createAlwaysInlinerPass() { return new AlwaysInliner(); }
76
Chad Rosier07d37bc2012-02-25 02:56:01 +000077Pass *llvm::createAlwaysInlinerPass(bool InsertLifetime) {
78 return new AlwaysInliner(InsertLifetime);
79}
80
Chandler Carrutha88a0fa2012-03-31 13:17:18 +000081/// \brief Get the inline cost for the always-inliner.
82///
83/// The always inliner *only* handles functions which are marked with the
84/// attribute to force inlining. As such, it is dramatically simpler and avoids
85/// using the powerful (but expensive) inline cost analysis. Instead it uses
86/// a very simple and boring direct walk of the instructions looking for
87/// impossible-to-inline constructs.
88///
89/// Note, it would be possible to go to some lengths to cache the information
90/// computed here, but as we only expect to do this for relatively few and
91/// small functions which have the explicit attribute to force inlining, it is
92/// likely not worth it in practice.
93InlineCost AlwaysInliner::getInlineCost(CallSite CS) {
94 Function *Callee = CS.getCalledFunction();
Chandler Carrutha88a0fa2012-03-31 13:17:18 +000095
Bob Wilsona5b0dc82012-11-19 07:04:35 +000096 // Only inline direct calls to functions with always-inline attributes
97 // that are viable for inlining. FIXME: We shouldn't even get here for
98 // declarations.
99 if (Callee && !Callee->isDeclaration() &&
Peter Collingbourne68a88972014-05-19 18:25:54 +0000100 CS.hasFnAttr(Attribute::AlwaysInline) &&
Chandler Carruth4319e292013-01-21 11:39:18 +0000101 ICA->isInlineViable(*Callee))
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000102 return InlineCost::getAlways();
Chandler Carrutha88a0fa2012-03-31 13:17:18 +0000103
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000104 return InlineCost::getNever();
Chandler Carrutha88a0fa2012-03-31 13:17:18 +0000105}
106
Chandler Carruth4319e292013-01-21 11:39:18 +0000107bool AlwaysInliner::runOnSCC(CallGraphSCC &SCC) {
108 ICA = &getAnalysis<InlineCostAnalysis>();
109 return Inliner::runOnSCC(SCC);
110}
111
112void AlwaysInliner::getAnalysisUsage(AnalysisUsage &AU) const {
113 AU.addRequired<InlineCostAnalysis>();
114 Inliner::getAnalysisUsage(AU);
Devang Patel924d9082008-09-03 18:50:53 +0000115}