blob: e7623a96300c5d2446299a1ddf5aa1b7ad484f8b [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"
Chandler Carruth66b31302015-01-04 12:03:27 +000017#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Analysis/CallGraph.h"
19#include "llvm/Analysis/InlineCost.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000020#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000021#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/CallingConv.h"
23#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Module.h"
27#include "llvm/IR/Type.h"
Devang Patel924d9082008-09-03 18:50:53 +000028#include "llvm/Transforms/IPO/InlinerPass.h"
Devang Patel924d9082008-09-03 18:50:53 +000029
30using namespace llvm;
31
Chandler Carruth964daaa2014-04-22 02:55:47 +000032#define DEBUG_TYPE "inline"
33
Devang Patel924d9082008-09-03 18:50:53 +000034namespace {
35
Chandler Carruth7e88e742013-01-21 11:39:16 +000036/// \brief Inliner pass which only handles "always inline" functions.
37class AlwaysInliner : public Inliner {
Matt Beaumont-Gayabfc4462012-12-04 05:41:27 +000038
Chandler Carruth7e88e742013-01-21 11:39:16 +000039public:
Easwaran Ramanf4bb2f02016-01-14 23:16:29 +000040 AlwaysInliner() : Inliner(ID, /*InsertLifetime*/ true) {
Chandler Carruth7e88e742013-01-21 11:39:16 +000041 initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
42 }
Matt Beaumont-Gayabfc4462012-12-04 05:41:27 +000043
Easwaran Ramanf4bb2f02016-01-14 23:16:29 +000044 AlwaysInliner(bool InsertLifetime) : Inliner(ID, InsertLifetime) {
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
52 using llvm::Pass::doFinalization;
Craig Topper3e4c6972014-03-05 09:10:37 +000053 bool doFinalization(CallGraph &CG) override {
Chandler Carruth7e88e742013-01-21 11:39:16 +000054 return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/ true);
55 }
Chandler Carruth7e88e742013-01-21 11:39:16 +000056};
57
Alexander Kornienkof00654e2015-06-23 09:49:53 +000058}
Devang Patel924d9082008-09-03 18:50:53 +000059
60char AlwaysInliner::ID = 0;
Owen Anderson6c18d1a2010-10-19 17:21:58 +000061INITIALIZE_PASS_BEGIN(AlwaysInliner, "always-inline",
62 "Inliner for always_inline functions", false, false)
Chandler Carruth66b31302015-01-04 12:03:27 +000063INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth6378cf52013-11-26 04:19:30 +000064INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +000065INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Owen Anderson6c18d1a2010-10-19 17:21:58 +000066INITIALIZE_PASS_END(AlwaysInliner, "always-inline",
Owen Andersondf7a4f22010-10-07 22:25:06 +000067 "Inliner for always_inline functions", false, false)
Devang Patel924d9082008-09-03 18:50:53 +000068
69Pass *llvm::createAlwaysInlinerPass() { return new AlwaysInliner(); }
70
Chad Rosier07d37bc2012-02-25 02:56:01 +000071Pass *llvm::createAlwaysInlinerPass(bool InsertLifetime) {
72 return new AlwaysInliner(InsertLifetime);
73}
74
Chandler Carrutha88a0fa2012-03-31 13:17:18 +000075/// \brief Get the inline cost for the always-inliner.
76///
77/// The always inliner *only* handles functions which are marked with the
78/// attribute to force inlining. As such, it is dramatically simpler and avoids
79/// using the powerful (but expensive) inline cost analysis. Instead it uses
80/// a very simple and boring direct walk of the instructions looking for
81/// impossible-to-inline constructs.
82///
83/// Note, it would be possible to go to some lengths to cache the information
84/// computed here, but as we only expect to do this for relatively few and
85/// small functions which have the explicit attribute to force inlining, it is
86/// likely not worth it in practice.
87InlineCost AlwaysInliner::getInlineCost(CallSite CS) {
88 Function *Callee = CS.getCalledFunction();
Chandler Carrutha88a0fa2012-03-31 13:17:18 +000089
Bob Wilsona5b0dc82012-11-19 07:04:35 +000090 // Only inline direct calls to functions with always-inline attributes
91 // that are viable for inlining. FIXME: We shouldn't even get here for
92 // declarations.
93 if (Callee && !Callee->isDeclaration() &&
Easwaran Ramanb9f71202015-12-28 20:28:19 +000094 CS.hasFnAttr(Attribute::AlwaysInline) && isInlineViable(*Callee))
Bob Wilsona5b0dc82012-11-19 07:04:35 +000095 return InlineCost::getAlways();
Chandler Carrutha88a0fa2012-03-31 13:17:18 +000096
Bob Wilsona5b0dc82012-11-19 07:04:35 +000097 return InlineCost::getNever();
Chandler Carrutha88a0fa2012-03-31 13:17:18 +000098}