xref: /aosp_15_r20/external/llvm/lib/Transforms/IPO/InlineAlways.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- InlineAlways.cpp - Code to inline always_inline functions ----------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements a custom inliner that handles only functions that
11*9880d681SAndroid Build Coastguard Worker // are marked as "always inline".
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker 
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/IPO.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AssumptionCache.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CallGraph.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/InlineCost.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ProfileSummaryInfo.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetLibraryInfo.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallSite.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallingConv.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/IPO/InlinerPass.h"
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker using namespace llvm;
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "inline"
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker namespace {
36*9880d681SAndroid Build Coastguard Worker 
37*9880d681SAndroid Build Coastguard Worker /// \brief Inliner pass which only handles "always inline" functions.
38*9880d681SAndroid Build Coastguard Worker class AlwaysInliner : public Inliner {
39*9880d681SAndroid Build Coastguard Worker 
40*9880d681SAndroid Build Coastguard Worker public:
AlwaysInliner()41*9880d681SAndroid Build Coastguard Worker   AlwaysInliner() : Inliner(ID, /*InsertLifetime*/ true) {
42*9880d681SAndroid Build Coastguard Worker     initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
43*9880d681SAndroid Build Coastguard Worker   }
44*9880d681SAndroid Build Coastguard Worker 
AlwaysInliner(bool InsertLifetime)45*9880d681SAndroid Build Coastguard Worker   AlwaysInliner(bool InsertLifetime) : Inliner(ID, InsertLifetime) {
46*9880d681SAndroid Build Coastguard Worker     initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
47*9880d681SAndroid Build Coastguard Worker   }
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker   /// Main run interface method.  We override here to avoid calling skipSCC().
runOnSCC(CallGraphSCC & SCC)50*9880d681SAndroid Build Coastguard Worker   bool runOnSCC(CallGraphSCC &SCC) override { return inlineCalls(SCC); }
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker   static char ID; // Pass identification, replacement for typeid
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker   InlineCost getInlineCost(CallSite CS) override;
55*9880d681SAndroid Build Coastguard Worker 
56*9880d681SAndroid Build Coastguard Worker   using llvm::Pass::doFinalization;
doFinalization(CallGraph & CG)57*9880d681SAndroid Build Coastguard Worker   bool doFinalization(CallGraph &CG) override {
58*9880d681SAndroid Build Coastguard Worker     return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/ true);
59*9880d681SAndroid Build Coastguard Worker   }
60*9880d681SAndroid Build Coastguard Worker };
61*9880d681SAndroid Build Coastguard Worker 
62*9880d681SAndroid Build Coastguard Worker }
63*9880d681SAndroid Build Coastguard Worker 
64*9880d681SAndroid Build Coastguard Worker char AlwaysInliner::ID = 0;
65*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(AlwaysInliner, "always-inline",
66*9880d681SAndroid Build Coastguard Worker                 "Inliner for always_inline functions", false, false)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)67*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
68*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
69*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
70*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
71*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(AlwaysInliner, "always-inline",
72*9880d681SAndroid Build Coastguard Worker                 "Inliner for always_inline functions", false, false)
73*9880d681SAndroid Build Coastguard Worker 
74*9880d681SAndroid Build Coastguard Worker Pass *llvm::createAlwaysInlinerPass() { return new AlwaysInliner(); }
75*9880d681SAndroid Build Coastguard Worker 
createAlwaysInlinerPass(bool InsertLifetime)76*9880d681SAndroid Build Coastguard Worker Pass *llvm::createAlwaysInlinerPass(bool InsertLifetime) {
77*9880d681SAndroid Build Coastguard Worker   return new AlwaysInliner(InsertLifetime);
78*9880d681SAndroid Build Coastguard Worker }
79*9880d681SAndroid Build Coastguard Worker 
80*9880d681SAndroid Build Coastguard Worker /// \brief Get the inline cost for the always-inliner.
81*9880d681SAndroid Build Coastguard Worker ///
82*9880d681SAndroid Build Coastguard Worker /// The always inliner *only* handles functions which are marked with the
83*9880d681SAndroid Build Coastguard Worker /// attribute to force inlining. As such, it is dramatically simpler and avoids
84*9880d681SAndroid Build Coastguard Worker /// using the powerful (but expensive) inline cost analysis. Instead it uses
85*9880d681SAndroid Build Coastguard Worker /// a very simple and boring direct walk of the instructions looking for
86*9880d681SAndroid Build Coastguard Worker /// impossible-to-inline constructs.
87*9880d681SAndroid Build Coastguard Worker ///
88*9880d681SAndroid Build Coastguard Worker /// Note, it would be possible to go to some lengths to cache the information
89*9880d681SAndroid Build Coastguard Worker /// computed here, but as we only expect to do this for relatively few and
90*9880d681SAndroid Build Coastguard Worker /// small functions which have the explicit attribute to force inlining, it is
91*9880d681SAndroid Build Coastguard Worker /// likely not worth it in practice.
getInlineCost(CallSite CS)92*9880d681SAndroid Build Coastguard Worker InlineCost AlwaysInliner::getInlineCost(CallSite CS) {
93*9880d681SAndroid Build Coastguard Worker   Function *Callee = CS.getCalledFunction();
94*9880d681SAndroid Build Coastguard Worker 
95*9880d681SAndroid Build Coastguard Worker   // Only inline direct calls to functions with always-inline attributes
96*9880d681SAndroid Build Coastguard Worker   // that are viable for inlining. FIXME: We shouldn't even get here for
97*9880d681SAndroid Build Coastguard Worker   // declarations.
98*9880d681SAndroid Build Coastguard Worker   if (Callee && !Callee->isDeclaration() &&
99*9880d681SAndroid Build Coastguard Worker       CS.hasFnAttr(Attribute::AlwaysInline) && isInlineViable(*Callee))
100*9880d681SAndroid Build Coastguard Worker     return InlineCost::getAlways();
101*9880d681SAndroid Build Coastguard Worker 
102*9880d681SAndroid Build Coastguard Worker   return InlineCost::getNever();
103*9880d681SAndroid Build Coastguard Worker }
104