• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/InlineCost.h"
19 #include "llvm/Analysis/TargetLibraryInfo.h"
20 #include "llvm/IR/CallSite.h"
21 #include "llvm/IR/CallingConv.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/Type.h"
26 #include "llvm/Transforms/IPO.h"
27 #include "llvm/Transforms/IPO/Inliner.h"
28 #include "llvm/Transforms/Utils/Cloning.h"
29 #include "llvm/Transforms/Utils/ModuleUtils.h"
30 
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "inline"
34 
run(Module & M,ModuleAnalysisManager &)35 PreservedAnalyses AlwaysInlinerPass::run(Module &M, ModuleAnalysisManager &) {
36   InlineFunctionInfo IFI;
37   SmallSetVector<CallSite, 16> Calls;
38   bool Changed = false;
39   SmallVector<Function *, 16> InlinedFunctions;
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 |=
54             InlineFunction(CS, IFI, /*CalleeAAR=*/nullptr, InsertLifetime);
55 
56       // Remember to try and delete this function afterward. This both avoids
57       // re-walking the rest of the module and avoids dealing with any iterator
58       // invalidation issues while deleting functions.
59       InlinedFunctions.push_back(&F);
60     }
61 
62   // Remove any live functions.
63   erase_if(InlinedFunctions, [&](Function *F) {
64     F->removeDeadConstantUsers();
65     return !F->isDefTriviallyDead();
66   });
67 
68   // Delete the non-comdat ones from the module and also from our vector.
69   auto NonComdatBegin = partition(
70       InlinedFunctions, [&](Function *F) { return F->hasComdat(); });
71   for (Function *F : make_range(NonComdatBegin, InlinedFunctions.end()))
72     M.getFunctionList().erase(F);
73   InlinedFunctions.erase(NonComdatBegin, InlinedFunctions.end());
74 
75   if (!InlinedFunctions.empty()) {
76     // Now we just have the comdat functions. Filter out the ones whose comdats
77     // are not actually dead.
78     filterDeadComdatFunctions(M, InlinedFunctions);
79     // The remaining functions are actually dead.
80     for (Function *F : InlinedFunctions)
81       M.getFunctionList().erase(F);
82   }
83 
84   return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
85 }
86 
87 namespace {
88 
89 /// Inliner pass which only handles "always inline" functions.
90 ///
91 /// Unlike the \c AlwaysInlinerPass, this uses the more heavyweight \c Inliner
92 /// base class to provide several facilities such as array alloca merging.
93 class AlwaysInlinerLegacyPass : public LegacyInlinerBase {
94 
95 public:
AlwaysInlinerLegacyPass()96   AlwaysInlinerLegacyPass() : LegacyInlinerBase(ID, /*InsertLifetime*/ true) {
97     initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry());
98   }
99 
AlwaysInlinerLegacyPass(bool InsertLifetime)100   AlwaysInlinerLegacyPass(bool InsertLifetime)
101       : LegacyInlinerBase(ID, InsertLifetime) {
102     initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry());
103   }
104 
105   /// Main run interface method.  We override here to avoid calling skipSCC().
runOnSCC(CallGraphSCC & SCC)106   bool runOnSCC(CallGraphSCC &SCC) override { return inlineCalls(SCC); }
107 
108   static char ID; // Pass identification, replacement for typeid
109 
110   InlineCost getInlineCost(CallSite CS) override;
111 
112   using llvm::Pass::doFinalization;
doFinalization(CallGraph & CG)113   bool doFinalization(CallGraph &CG) override {
114     return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/true);
115   }
116 };
117 }
118 
119 char AlwaysInlinerLegacyPass::ID = 0;
120 INITIALIZE_PASS_BEGIN(AlwaysInlinerLegacyPass, "always-inline",
121                       "Inliner for always_inline functions", false, false)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)122 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
123 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
124 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
125 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
126 INITIALIZE_PASS_END(AlwaysInlinerLegacyPass, "always-inline",
127                     "Inliner for always_inline functions", false, false)
128 
129 Pass *llvm::createAlwaysInlinerLegacyPass(bool InsertLifetime) {
130   return new AlwaysInlinerLegacyPass(InsertLifetime);
131 }
132 
133 /// Get the inline cost for the always-inliner.
134 ///
135 /// The always inliner *only* handles functions which are marked with the
136 /// attribute to force inlining. As such, it is dramatically simpler and avoids
137 /// using the powerful (but expensive) inline cost analysis. Instead it uses
138 /// a very simple and boring direct walk of the instructions looking for
139 /// impossible-to-inline constructs.
140 ///
141 /// Note, it would be possible to go to some lengths to cache the information
142 /// computed here, but as we only expect to do this for relatively few and
143 /// small functions which have the explicit attribute to force inlining, it is
144 /// likely not worth it in practice.
getInlineCost(CallSite CS)145 InlineCost AlwaysInlinerLegacyPass::getInlineCost(CallSite CS) {
146   Function *Callee = CS.getCalledFunction();
147 
148   // Only inline direct calls to functions with always-inline attributes
149   // that are viable for inlining. FIXME: We shouldn't even get here for
150   // declarations.
151   if (Callee && !Callee->isDeclaration() &&
152       CS.hasFnAttr(Attribute::AlwaysInline) && isInlineViable(*Callee))
153     return InlineCost::getAlways();
154 
155   return InlineCost::getNever();
156 }
157