1 //===- InlineAdvisor.cpp - analysis pass implementation -------------------===//
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 InlineAdvisorAnalysis and DefaultInlineAdvisor, and
10 // related types.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Analysis/InlineAdvisor.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/Analysis/InlineCost.h"
17 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
18 #include "llvm/Analysis/ProfileSummaryInfo.h"
19 #include "llvm/Analysis/TargetLibraryInfo.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 #include <sstream>
25 
26 using namespace llvm;
27 #define DEBUG_TYPE "inline"
28 
29 // This weirdly named statistic tracks the number of times that, when attempting
30 // to inline a function A into B, we analyze the callers of B in order to see
31 // if those would be more profitable and blocked inline steps.
32 STATISTIC(NumCallerCallersAnalyzed, "Number of caller-callers analyzed");
33 
34 /// Flag to add inline messages as callsite attributes 'inline-remark'.
35 static cl::opt<bool>
36     InlineRemarkAttribute("inline-remark-attribute", cl::init(false),
37                           cl::Hidden,
38                           cl::desc("Enable adding inline-remark attribute to"
39                                    " callsites processed by inliner but decided"
40                                    " to be not inlined"));
41 
42 // An integer used to limit the cost of inline deferral.  The default negative
43 // number tells shouldBeDeferred to only take the secondary cost into account.
44 static cl::opt<int>
45     InlineDeferralScale("inline-deferral-scale",
46                         cl::desc("Scale to limit the cost of inline deferral"),
47                         cl::init(2), cl::Hidden);
48 
49 namespace {
50 class DefaultInlineAdvice : public InlineAdvice {
51 public:
52   DefaultInlineAdvice(DefaultInlineAdvisor *Advisor, CallBase &CB,
53                       Optional<InlineCost> OIC, OptimizationRemarkEmitter &ORE)
54       : InlineAdvice(Advisor, CB, ORE, OIC.hasValue()), OriginalCB(&CB),
55         OIC(OIC) {}
56 
57 private:
58   void recordUnsuccessfulInliningImpl(const InlineResult &Result) override {
59     using namespace ore;
60     llvm::setInlineRemark(*OriginalCB, std::string(Result.getFailureReason()) +
61                                            "; " + inlineCostStr(*OIC));
62     ORE.emit([&]() {
63       return OptimizationRemarkMissed(DEBUG_TYPE, "NotInlined", DLoc, Block)
64              << NV("Callee", Callee) << " will not be inlined into "
65              << NV("Caller", Caller) << ": "
66              << NV("Reason", Result.getFailureReason());
67     });
68   }
69 
70   void recordInliningWithCalleeDeletedImpl() override {
71     emitInlinedInto(ORE, DLoc, Block, *Callee, *Caller, *OIC);
72   }
73 
74   void recordInliningImpl() override {
75     emitInlinedInto(ORE, DLoc, Block, *Callee, *Caller, *OIC);
76   }
77 
78 private:
79   CallBase *const OriginalCB;
80   Optional<InlineCost> OIC;
81 };
82 
83 } // namespace
84 
85 std::unique_ptr<InlineAdvice> DefaultInlineAdvisor::getAdvice(CallBase &CB) {
86   Function &Caller = *CB.getCaller();
87   ProfileSummaryInfo *PSI =
88       FAM.getResult<ModuleAnalysisManagerFunctionProxy>(Caller)
89           .getCachedResult<ProfileSummaryAnalysis>(
90               *CB.getParent()->getParent()->getParent());
91 
92   auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
93   auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
94     return FAM.getResult<AssumptionAnalysis>(F);
95   };
96   auto GetBFI = [&](Function &F) -> BlockFrequencyInfo & {
97     return FAM.getResult<BlockFrequencyAnalysis>(F);
98   };
99   auto GetTLI = [&](Function &F) -> const TargetLibraryInfo & {
100     return FAM.getResult<TargetLibraryAnalysis>(F);
101   };
102 
103   auto GetInlineCost = [&](CallBase &CB) {
104     Function &Callee = *CB.getCalledFunction();
105     auto &CalleeTTI = FAM.getResult<TargetIRAnalysis>(Callee);
106     bool RemarksEnabled =
107         Callee.getContext().getDiagHandlerPtr()->isMissedOptRemarkEnabled(
108             DEBUG_TYPE);
109     return getInlineCost(CB, Params, CalleeTTI, GetAssumptionCache, GetTLI,
110                          GetBFI, PSI, RemarksEnabled ? &ORE : nullptr);
111   };
112   auto OIC = llvm::shouldInline(CB, GetInlineCost, ORE,
113                                 Params.EnableDeferral.hasValue() &&
114                                     Params.EnableDeferral.getValue());
115   return std::make_unique<DefaultInlineAdvice>(this, CB, OIC, ORE);
116 }
117 
118 InlineAdvice::InlineAdvice(InlineAdvisor *Advisor, CallBase &CB,
119                            OptimizationRemarkEmitter &ORE,
120                            bool IsInliningRecommended)
121     : Advisor(Advisor), Caller(CB.getCaller()), Callee(CB.getCalledFunction()),
122       DLoc(CB.getDebugLoc()), Block(CB.getParent()), ORE(ORE),
123       IsInliningRecommended(IsInliningRecommended) {}
124 
125 void InlineAdvisor::markFunctionAsDeleted(Function *F) {
126   assert((!DeletedFunctions.count(F)) &&
127          "Cannot put cause a function to become dead twice!");
128   DeletedFunctions.insert(F);
129 }
130 
131 void InlineAdvisor::freeDeletedFunctions() {
132   for (auto *F : DeletedFunctions)
133     delete F;
134   DeletedFunctions.clear();
135 }
136 
137 void InlineAdvice::recordInliningWithCalleeDeleted() {
138   markRecorded();
139   Advisor->markFunctionAsDeleted(Callee);
140   recordInliningWithCalleeDeletedImpl();
141 }
142 
143 AnalysisKey InlineAdvisorAnalysis::Key;
144 
145 bool InlineAdvisorAnalysis::Result::tryCreate(InlineParams Params,
146                                               InliningAdvisorMode Mode) {
147   auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
148   switch (Mode) {
149   case InliningAdvisorMode::Default:
150     Advisor.reset(new DefaultInlineAdvisor(FAM, Params));
151     break;
152   case InliningAdvisorMode::Development:
153     // To be added subsequently under conditional compilation.
154     break;
155   case InliningAdvisorMode::Release:
156     // To be added subsequently under conditional compilation.
157     break;
158   }
159   return !!Advisor;
160 }
161 
162 /// Return true if inlining of CB can block the caller from being
163 /// inlined which is proved to be more beneficial. \p IC is the
164 /// estimated inline cost associated with callsite \p CB.
165 /// \p TotalSecondaryCost will be set to the estimated cost of inlining the
166 /// caller if \p CB is suppressed for inlining.
167 static bool
168 shouldBeDeferred(Function *Caller, InlineCost IC, int &TotalSecondaryCost,
169                  function_ref<InlineCost(CallBase &CB)> GetInlineCost) {
170   // For now we only handle local or inline functions.
171   if (!Caller->hasLocalLinkage() && !Caller->hasLinkOnceODRLinkage())
172     return false;
173   // If the cost of inlining CB is non-positive, it is not going to prevent the
174   // caller from being inlined into its callers and hence we don't need to
175   // defer.
176   if (IC.getCost() <= 0)
177     return false;
178   // Try to detect the case where the current inlining candidate caller (call
179   // it B) is a static or linkonce-ODR function and is an inlining candidate
180   // elsewhere, and the current candidate callee (call it C) is large enough
181   // that inlining it into B would make B too big to inline later. In these
182   // circumstances it may be best not to inline C into B, but to inline B into
183   // its callers.
184   //
185   // This only applies to static and linkonce-ODR functions because those are
186   // expected to be available for inlining in the translation units where they
187   // are used. Thus we will always have the opportunity to make local inlining
188   // decisions. Importantly the linkonce-ODR linkage covers inline functions
189   // and templates in C++.
190   //
191   // FIXME: All of this logic should be sunk into getInlineCost. It relies on
192   // the internal implementation of the inline cost metrics rather than
193   // treating them as truly abstract units etc.
194   TotalSecondaryCost = 0;
195   // The candidate cost to be imposed upon the current function.
196   int CandidateCost = IC.getCost() - 1;
197   // If the caller has local linkage and can be inlined to all its callers, we
198   // can apply a huge negative bonus to TotalSecondaryCost.
199   bool ApplyLastCallBonus = Caller->hasLocalLinkage() && !Caller->hasOneUse();
200   // This bool tracks what happens if we DO inline C into B.
201   bool InliningPreventsSomeOuterInline = false;
202   unsigned NumCallerUsers = 0;
203   for (User *U : Caller->users()) {
204     CallBase *CS2 = dyn_cast<CallBase>(U);
205 
206     // If this isn't a call to Caller (it could be some other sort
207     // of reference) skip it.  Such references will prevent the caller
208     // from being removed.
209     if (!CS2 || CS2->getCalledFunction() != Caller) {
210       ApplyLastCallBonus = false;
211       continue;
212     }
213 
214     InlineCost IC2 = GetInlineCost(*CS2);
215     ++NumCallerCallersAnalyzed;
216     if (!IC2) {
217       ApplyLastCallBonus = false;
218       continue;
219     }
220     if (IC2.isAlways())
221       continue;
222 
223     // See if inlining of the original callsite would erase the cost delta of
224     // this callsite. We subtract off the penalty for the call instruction,
225     // which we would be deleting.
226     if (IC2.getCostDelta() <= CandidateCost) {
227       InliningPreventsSomeOuterInline = true;
228       TotalSecondaryCost += IC2.getCost();
229       NumCallerUsers++;
230     }
231   }
232 
233   if (!InliningPreventsSomeOuterInline)
234     return false;
235 
236   // If all outer calls to Caller would get inlined, the cost for the last
237   // one is set very low by getInlineCost, in anticipation that Caller will
238   // be removed entirely.  We did not account for this above unless there
239   // is only one caller of Caller.
240   if (ApplyLastCallBonus)
241     TotalSecondaryCost -= InlineConstants::LastCallToStaticBonus;
242 
243   // If InlineDeferralScale is negative, then ignore the cost of primary
244   // inlining -- IC.getCost() multiplied by the number of callers to Caller.
245   if (InlineDeferralScale < 0)
246     return TotalSecondaryCost < IC.getCost();
247 
248   int TotalCost = TotalSecondaryCost + IC.getCost() * NumCallerUsers;
249   int Allowance = IC.getCost() * InlineDeferralScale;
250   return TotalCost < Allowance;
251 }
252 
253 namespace llvm {
254 static std::basic_ostream<char> &operator<<(std::basic_ostream<char> &R,
255                                             const ore::NV &Arg) {
256   return R << Arg.Val;
257 }
258 
259 template <class RemarkT>
260 RemarkT &operator<<(RemarkT &&R, const InlineCost &IC) {
261   using namespace ore;
262   if (IC.isAlways()) {
263     R << "(cost=always)";
264   } else if (IC.isNever()) {
265     R << "(cost=never)";
266   } else {
267     R << "(cost=" << ore::NV("Cost", IC.getCost())
268       << ", threshold=" << ore::NV("Threshold", IC.getThreshold()) << ")";
269   }
270   if (const char *Reason = IC.getReason())
271     R << ": " << ore::NV("Reason", Reason);
272   return R;
273 }
274 } // namespace llvm
275 
276 std::string llvm::inlineCostStr(const InlineCost &IC) {
277   std::stringstream Remark;
278   Remark << IC;
279   return Remark.str();
280 }
281 
282 void llvm::setInlineRemark(CallBase &CB, StringRef Message) {
283   if (!InlineRemarkAttribute)
284     return;
285 
286   Attribute Attr = Attribute::get(CB.getContext(), "inline-remark", Message);
287   CB.addAttribute(AttributeList::FunctionIndex, Attr);
288 }
289 
290 /// Return the cost only if the inliner should attempt to inline at the given
291 /// CallSite. If we return the cost, we will emit an optimisation remark later
292 /// using that cost, so we won't do so from this function. Return None if
293 /// inlining should not be attempted.
294 Optional<InlineCost>
295 llvm::shouldInline(CallBase &CB,
296                    function_ref<InlineCost(CallBase &CB)> GetInlineCost,
297                    OptimizationRemarkEmitter &ORE, bool EnableDeferral) {
298   using namespace ore;
299 
300   InlineCost IC = GetInlineCost(CB);
301   Instruction *Call = &CB;
302   Function *Callee = CB.getCalledFunction();
303   Function *Caller = CB.getCaller();
304 
305   if (IC.isAlways()) {
306     LLVM_DEBUG(dbgs() << "    Inlining " << inlineCostStr(IC)
307                       << ", Call: " << CB << "\n");
308     return IC;
309   }
310 
311   if (!IC) {
312     LLVM_DEBUG(dbgs() << "    NOT Inlining " << inlineCostStr(IC)
313                       << ", Call: " << CB << "\n");
314     if (IC.isNever()) {
315       ORE.emit([&]() {
316         return OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline", Call)
317                << NV("Callee", Callee) << " not inlined into "
318                << NV("Caller", Caller) << " because it should never be inlined "
319                << IC;
320       });
321     } else {
322       ORE.emit([&]() {
323         return OptimizationRemarkMissed(DEBUG_TYPE, "TooCostly", Call)
324                << NV("Callee", Callee) << " not inlined into "
325                << NV("Caller", Caller) << " because too costly to inline "
326                << IC;
327       });
328     }
329     setInlineRemark(CB, inlineCostStr(IC));
330     return None;
331   }
332 
333   int TotalSecondaryCost = 0;
334   if (EnableDeferral &&
335       shouldBeDeferred(Caller, IC, TotalSecondaryCost, GetInlineCost)) {
336     LLVM_DEBUG(dbgs() << "    NOT Inlining: " << CB
337                       << " Cost = " << IC.getCost()
338                       << ", outer Cost = " << TotalSecondaryCost << '\n');
339     ORE.emit([&]() {
340       return OptimizationRemarkMissed(DEBUG_TYPE, "IncreaseCostInOtherContexts",
341                                       Call)
342              << "Not inlining. Cost of inlining " << NV("Callee", Callee)
343              << " increases the cost of inlining " << NV("Caller", Caller)
344              << " in other contexts";
345     });
346     setInlineRemark(CB, "deferred");
347     // IC does not bool() to false, so get an InlineCost that will.
348     // This will not be inspected to make an error message.
349     return None;
350   }
351 
352   LLVM_DEBUG(dbgs() << "    Inlining " << inlineCostStr(IC) << ", Call: " << CB
353                     << '\n');
354   return IC;
355 }
356 
357 void llvm::emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
358                            const BasicBlock *Block, const Function &Callee,
359                            const Function &Caller, const InlineCost &IC) {
360   ORE.emit([&]() {
361     bool AlwaysInline = IC.isAlways();
362     StringRef RemarkName = AlwaysInline ? "AlwaysInline" : "Inlined";
363     return OptimizationRemark(DEBUG_TYPE, RemarkName, DLoc, Block)
364            << ore::NV("Callee", &Callee) << " inlined into "
365            << ore::NV("Caller", &Caller) << " with " << IC;
366   });
367 }
368