1 //===- AlwaysInliner.cpp - Code to inline always_inline functions ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a custom inliner that handles only functions that
10 // are marked as "always inline".
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/IPO/AlwaysInliner.h"
15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/Analysis/AliasAnalysis.h"
17 #include "llvm/Analysis/AssumptionCache.h"
18 #include "llvm/Analysis/InlineCost.h"
19 #include "llvm/Analysis/ProfileSummaryInfo.h"
20 #include "llvm/Analysis/TargetLibraryInfo.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/InitializePasses.h"
27 #include "llvm/Transforms/IPO.h"
28 #include "llvm/Transforms/IPO/Inliner.h"
29 #include "llvm/Transforms/Utils/Cloning.h"
30 #include "llvm/Transforms/Utils/ModuleUtils.h"
31 
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "inline"
35 
36 PreservedAnalyses AlwaysInlinerPass::run(Module &M,
37                                          ModuleAnalysisManager &MAM) {
38   // Add inline assumptions during code generation.
39   FunctionAnalysisManager &FAM =
40       MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
41   auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
42     return FAM.getResult<AssumptionAnalysis>(F);
43   };
44   auto &PSI = MAM.getResult<ProfileSummaryAnalysis>(M);
45 
46   SmallSetVector<CallBase *, 16> Calls;
47   bool Changed = false;
48   SmallVector<Function *, 16> InlinedFunctions;
49   for (Function &F : M) {
50     // When callee coroutine function is inlined into caller coroutine function
51     // before coro-split pass,
52     // coro-early pass can not handle this quiet well.
53     // So we won't inline the coroutine function if it have not been unsplited
54     if (F.isPresplitCoroutine())
55       continue;
56 
57     if (!F.isDeclaration() && isInlineViable(F).isSuccess()) {
58       Calls.clear();
59 
60       for (User *U : F.users())
61         if (auto *CB = dyn_cast<CallBase>(U))
62           if (CB->getCalledFunction() == &F) {
63             if (F.hasFnAttribute(Attribute::AlwaysInline)) {
64               // Avoid inlining if noinline call site attribute.
65               if (!CB->isNoInline())
66                 Calls.insert(CB);
67             } else if (CB->hasFnAttr(Attribute::AlwaysInline)) {
68               // Ok, alwaysinline call site attribute.
69               Calls.insert(CB);
70             }
71           }
72 
73       for (CallBase *CB : Calls) {
74         Function *Caller = CB->getCaller();
75         OptimizationRemarkEmitter ORE(Caller);
76         auto OIC = shouldInline(
77             *CB,
78             [&](CallBase &CB) {
79               return InlineCost::getAlways("always inline attribute");
80             },
81             ORE);
82         assert(OIC);
83         emitInlinedIntoBasedOnCost(ORE, CB->getDebugLoc(), CB->getParent(), F,
84                                    *Caller, *OIC, false, DEBUG_TYPE);
85 
86         InlineFunctionInfo IFI(
87             /*cg=*/nullptr, GetAssumptionCache, &PSI,
88             &FAM.getResult<BlockFrequencyAnalysis>(*(CB->getCaller())),
89             &FAM.getResult<BlockFrequencyAnalysis>(F));
90 
91         InlineResult Res = InlineFunction(
92             *CB, IFI, &FAM.getResult<AAManager>(F), InsertLifetime);
93         assert(Res.isSuccess() && "unexpected failure to inline");
94         (void)Res;
95 
96         // Merge the attributes based on the inlining.
97         AttributeFuncs::mergeAttributesForInlining(*Caller, F);
98 
99         Changed = true;
100       }
101 
102       if (F.hasFnAttribute(Attribute::AlwaysInline)) {
103         // Remember to try and delete this function afterward. This both avoids
104         // re-walking the rest of the module and avoids dealing with any
105         // iterator invalidation issues while deleting functions.
106         InlinedFunctions.push_back(&F);
107       }
108     }
109   }
110 
111   // Remove any live functions.
112   erase_if(InlinedFunctions, [&](Function *F) {
113     F->removeDeadConstantUsers();
114     return !F->isDefTriviallyDead();
115   });
116 
117   // Delete the non-comdat ones from the module and also from our vector.
118   auto NonComdatBegin = partition(
119       InlinedFunctions, [&](Function *F) { return F->hasComdat(); });
120   for (Function *F : make_range(NonComdatBegin, InlinedFunctions.end())) {
121     M.getFunctionList().erase(F);
122     Changed = true;
123   }
124   InlinedFunctions.erase(NonComdatBegin, InlinedFunctions.end());
125 
126   if (!InlinedFunctions.empty()) {
127     // Now we just have the comdat functions. Filter out the ones whose comdats
128     // are not actually dead.
129     filterDeadComdatFunctions(InlinedFunctions);
130     // The remaining functions are actually dead.
131     for (Function *F : InlinedFunctions) {
132       M.getFunctionList().erase(F);
133       Changed = true;
134     }
135   }
136 
137   return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
138 }
139 
140 namespace {
141 
142 /// Inliner pass which only handles "always inline" functions.
143 ///
144 /// Unlike the \c AlwaysInlinerPass, this uses the more heavyweight \c Inliner
145 /// base class to provide several facilities such as array alloca merging.
146 class AlwaysInlinerLegacyPass : public LegacyInlinerBase {
147 
148 public:
149   AlwaysInlinerLegacyPass() : LegacyInlinerBase(ID, /*InsertLifetime*/ true) {
150     initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry());
151   }
152 
153   AlwaysInlinerLegacyPass(bool InsertLifetime)
154       : LegacyInlinerBase(ID, InsertLifetime) {
155     initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry());
156   }
157 
158   /// Main run interface method.  We override here to avoid calling skipSCC().
159   bool runOnSCC(CallGraphSCC &SCC) override { return inlineCalls(SCC); }
160 
161   static char ID; // Pass identification, replacement for typeid
162 
163   InlineCost getInlineCost(CallBase &CB) override;
164 
165   using llvm::Pass::doFinalization;
166   bool doFinalization(CallGraph &CG) override {
167     return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/true);
168   }
169 };
170 }
171 
172 char AlwaysInlinerLegacyPass::ID = 0;
173 INITIALIZE_PASS_BEGIN(AlwaysInlinerLegacyPass, "always-inline",
174                       "Inliner for always_inline functions", false, false)
175 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
176 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
177 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
178 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
179 INITIALIZE_PASS_END(AlwaysInlinerLegacyPass, "always-inline",
180                     "Inliner for always_inline functions", false, false)
181 
182 Pass *llvm::createAlwaysInlinerLegacyPass(bool InsertLifetime) {
183   return new AlwaysInlinerLegacyPass(InsertLifetime);
184 }
185 
186 /// Get the inline cost for the always-inliner.
187 ///
188 /// The always inliner *only* handles functions which are marked with the
189 /// attribute to force inlining. As such, it is dramatically simpler and avoids
190 /// using the powerful (but expensive) inline cost analysis. Instead it uses
191 /// a very simple and boring direct walk of the instructions looking for
192 /// impossible-to-inline constructs.
193 ///
194 /// Note, it would be possible to go to some lengths to cache the information
195 /// computed here, but as we only expect to do this for relatively few and
196 /// small functions which have the explicit attribute to force inlining, it is
197 /// likely not worth it in practice.
198 InlineCost AlwaysInlinerLegacyPass::getInlineCost(CallBase &CB) {
199   Function *Callee = CB.getCalledFunction();
200 
201   // Only inline direct calls to functions with always-inline attributes
202   // that are viable for inlining.
203   if (!Callee)
204     return InlineCost::getNever("indirect call");
205 
206   // When callee coroutine function is inlined into caller coroutine function
207   // before coro-split pass,
208   // coro-early pass can not handle this quiet well.
209   // So we won't inline the coroutine function if it have not been unsplited
210   if (Callee->isPresplitCoroutine())
211     return InlineCost::getNever("unsplited coroutine call");
212 
213   // FIXME: We shouldn't even get here for declarations.
214   if (Callee->isDeclaration())
215     return InlineCost::getNever("no definition");
216 
217   if (!CB.hasFnAttr(Attribute::AlwaysInline))
218     return InlineCost::getNever("no alwaysinline attribute");
219 
220   if (Callee->hasFnAttribute(Attribute::AlwaysInline) && CB.isNoInline())
221     return InlineCost::getNever("noinline call site attribute");
222 
223   auto IsViable = isInlineViable(*Callee);
224   if (!IsViable.isSuccess())
225     return InlineCost::getNever(IsViable.getFailureReason());
226 
227   return InlineCost::getAlways("always inliner");
228 }
229