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