1 //===- InlineAlways.cpp - Code to inline always_inline functions ----------===//
2 //
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 //
10 // This file implements a custom inliner that handles only functions that
11 // are marked as "always inline".
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Transforms/IPO/AlwaysInliner.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/Analysis/AssumptionCache.h"
18 #include "llvm/Analysis/CallGraph.h"
19 #include "llvm/Analysis/InlineCost.h"
20 #include "llvm/Analysis/ProfileSummaryInfo.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/IR/CallSite.h"
23 #include "llvm/IR/CallingConv.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/Transforms/IPO/InlinerPass.h"
30 #include "llvm/Transforms/Utils/Cloning.h"
31 
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "inline"
35 
36 PreservedAnalyses AlwaysInlinerPass::run(Module &M, ModuleAnalysisManager &) {
37   InlineFunctionInfo IFI;
38   SmallSetVector<CallSite, 16> Calls;
39   bool Changed = false;
40   for (Function &F : M)
41     if (!F.isDeclaration() && F.hasFnAttribute(Attribute::AlwaysInline) &&
42         isInlineViable(F)) {
43       Calls.clear();
44 
45       for (User *U : F.users())
46         if (auto CS = CallSite(U))
47           if (CS.getCalledFunction() == &F)
48             Calls.insert(CS);
49 
50       for (CallSite CS : Calls)
51         // FIXME: We really shouldn't be able to fail to inline at this point!
52         // We should do something to log or check the inline failures here.
53         Changed |= InlineFunction(CS, IFI);
54     }
55 
56   return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
57 }
58 
59 namespace {
60 
61 /// Inliner pass which only handles "always inline" functions.
62 ///
63 /// Unlike the \c AlwaysInlinerPass, this uses the more heavyweight \c Inliner
64 /// base class to provide several facilities such as array alloca merging.
65 class AlwaysInlinerLegacyPass : public Inliner {
66 
67 public:
68   AlwaysInlinerLegacyPass() : Inliner(ID, /*InsertLifetime*/ true) {
69     initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry());
70   }
71 
72   AlwaysInlinerLegacyPass(bool InsertLifetime) : Inliner(ID, InsertLifetime) {
73     initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry());
74   }
75 
76   /// Main run interface method.  We override here to avoid calling skipSCC().
77   bool runOnSCC(CallGraphSCC &SCC) override { return inlineCalls(SCC); }
78 
79   static char ID; // Pass identification, replacement for typeid
80 
81   InlineCost getInlineCost(CallSite CS) override;
82 
83   using llvm::Pass::doFinalization;
84   bool doFinalization(CallGraph &CG) override {
85     return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/true);
86   }
87 };
88 }
89 
90 char AlwaysInlinerLegacyPass::ID = 0;
91 INITIALIZE_PASS_BEGIN(AlwaysInlinerLegacyPass, "always-inline",
92                       "Inliner for always_inline functions", false, false)
93 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
94 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
95 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
96 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
97 INITIALIZE_PASS_END(AlwaysInlinerLegacyPass, "always-inline",
98                     "Inliner for always_inline functions", false, false)
99 
100 Pass *llvm::createAlwaysInlinerLegacyPass(bool InsertLifetime) {
101   return new AlwaysInlinerLegacyPass(InsertLifetime);
102 }
103 
104 /// \brief Get the inline cost for the always-inliner.
105 ///
106 /// The always inliner *only* handles functions which are marked with the
107 /// attribute to force inlining. As such, it is dramatically simpler and avoids
108 /// using the powerful (but expensive) inline cost analysis. Instead it uses
109 /// a very simple and boring direct walk of the instructions looking for
110 /// impossible-to-inline constructs.
111 ///
112 /// Note, it would be possible to go to some lengths to cache the information
113 /// computed here, but as we only expect to do this for relatively few and
114 /// small functions which have the explicit attribute to force inlining, it is
115 /// likely not worth it in practice.
116 InlineCost AlwaysInlinerLegacyPass::getInlineCost(CallSite CS) {
117   Function *Callee = CS.getCalledFunction();
118 
119   // Only inline direct calls to functions with always-inline attributes
120   // that are viable for inlining. FIXME: We shouldn't even get here for
121   // declarations.
122   if (Callee && !Callee->isDeclaration() &&
123       CS.hasFnAttr(Attribute::AlwaysInline) && isInlineViable(*Callee))
124     return InlineCost::getAlways();
125 
126   return InlineCost::getNever();
127 }
128