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