LLVM 23.0.0git
AlwaysInliner.cpp
Go to the documentation of this file.
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
15#include "llvm/ADT/SetVector.h"
24#include "llvm/IR/Module.h"
28
29using namespace llvm;
30
31#define DEBUG_TYPE "inline"
32
33namespace {
34
35bool AlwaysInlineImpl(
36 Module &M, bool InsertLifetime, ProfileSummaryInfo &PSI,
38 function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
39 function_ref<AAResults &(Function &)> GetAAR,
41 function_ref<const TargetLibraryInfo &(Function &)> GetTLI) {
43 bool Changed = false;
44 SmallVector<Function *, 16> InlinedComdatFunctions;
45
46 auto TryInline = [&](CallBase &CB, Function &Callee,
47 OptimizationRemarkEmitter &ORE, const char *InlineReason,
48 SmallVectorImpl<CallBase *> *NewCallSites =
49 nullptr) -> bool {
50 Function *Caller = CB.getCaller();
51 DebugLoc DLoc = CB.getDebugLoc();
53
54 InlineFunctionInfo IFI(GetAssumptionCache, &PSI);
56 CB, IFI, /*MergeAttributes=*/true, &GetAAR(Callee), InsertLifetime,
57 /*TrackInlineHistory=*/NewCallSites != nullptr);
58 if (!Res.isSuccess()) {
59 ORE.emit([&]() {
60 return OptimizationRemarkMissed(DEBUG_TYPE, "NotInlined", DLoc, Block)
61 << "'" << ore::NV("Callee", &Callee) << "' is not inlined into '"
62 << ore::NV("Caller", Caller)
63 << "': " << ore::NV("Reason", Res.getFailureReason());
64 });
65 return false;
66 }
67
68 emitInlinedIntoBasedOnCost(ORE, DLoc, Block, Callee, *Caller,
69 InlineCost::getAlways(InlineReason),
70 /*ForProfileContext=*/false, DEBUG_TYPE);
71 if (FAM)
72 FAM->invalidate(*Caller, PreservedAnalyses::none());
73 if (NewCallSites)
74 *NewCallSites = std::move(IFI.InlinedCallSites);
75 return true;
76 };
77
78 for (Function &F : M) {
79 if (!F.hasFnAttribute(Attribute::Flatten))
80 continue;
83 SmallVector<CallBase *> NewCallSites;
85
86 // Collect initial calls.
87 for (BasicBlock &BB : F) {
88 for (Instruction &I : BB) {
89 if (auto *CB = dyn_cast<CallBase>(&I)) {
90 Function *Callee = CB->getCalledFunction();
91 if (!Callee || Callee->isDeclaration())
92 continue;
93 Worklist.push_back({CB, -1});
94 }
95 }
96 }
97
98 while (!Worklist.empty()) {
99 auto Item = Worklist.pop_back_val();
100 CallBase *CB = Item.first;
101 int InlineHistoryID = Item.second;
102 Function *Callee = CB->getCalledFunction();
103 if (!Callee)
104 continue;
105
106 // Detect recursion.
107 if (Callee == &F) {
108 ORE.emit([&]() {
109 return OptimizationRemarkMissed("inline", "NotInlined",
110 CB->getDebugLoc(), CB->getParent())
111 << "'" << ore::NV("Callee", Callee)
112 << "' is not inlined into '"
113 << ore::NV("Caller", CB->getCaller())
114 << "': recursive call during flattening";
115 });
116 continue;
117 }
118
119 // Use getAttributeBasedInliningDecision for all attribute-based checks
120 // including TTI/TLI compatibility and isInlineViable.
121 TargetTransformInfo &CalleeTTI = GetTTI(*Callee);
122 auto Decision =
123 getAttributeBasedInliningDecision(*CB, Callee, CalleeTTI, GetTLI);
124 if (!Decision || !Decision->isSuccess())
125 continue;
126
127 if (!TryInline(*CB, *Callee, ORE, "flatten attribute", &NewCallSites))
128 continue;
129
130 Changed = true;
131
132 // Add new call sites from the inlined function to the worklist.
133 if (!NewCallSites.empty()) {
134 int NewHistoryID = InlineHistory.size();
135 InlineHistory.push_back({Callee, InlineHistoryID});
136 for (CallBase *NewCB : NewCallSites) {
137 Function *NewCallee = NewCB->getCalledFunction();
138 if (NewCallee && !NewCallee->isDeclaration())
139 Worklist.push_back({NewCB, NewHistoryID});
140 }
141 }
142 }
143 }
144
145 for (Function &F : make_early_inc_range(M)) {
146 if (F.isPresplitCoroutine())
147 continue;
148
149 if (F.isDeclaration() || !isInlineViable(F).isSuccess())
150 continue;
151
152 Calls.clear();
153
154 for (User *U : F.users())
155 if (auto *CB = dyn_cast<CallBase>(U))
156 if (CB->getCalledFunction() == &F &&
157 CB->hasFnAttr(Attribute::AlwaysInline) &&
158 !CB->getAttributes().hasFnAttr(Attribute::NoInline))
159 Calls.insert(CB);
160
161 for (CallBase *CB : Calls) {
163 Changed |= TryInline(*CB, F, ORE, "always inline attribute");
164 }
165
166 F.removeDeadConstantUsers();
167 if (F.hasFnAttribute(Attribute::AlwaysInline) && F.isDefTriviallyDead()) {
168 if (F.hasComdat()) {
169 InlinedComdatFunctions.push_back(&F);
170 } else {
171 if (FAM)
172 FAM->clear(F, F.getName());
173 M.getFunctionList().erase(F);
174 Changed = true;
175 }
176 }
177 }
178
179 if (!InlinedComdatFunctions.empty()) {
180 // Now we just have the comdat functions. Filter out the ones whose comdats
181 // are not actually dead.
182 filterDeadComdatFunctions(InlinedComdatFunctions);
183 // The remaining functions are actually dead.
184 for (Function *F : InlinedComdatFunctions) {
185 if (FAM)
186 FAM->clear(*F, F->getName());
187 M.getFunctionList().erase(F);
188 Changed = true;
189 }
190 }
191
192 return Changed;
193}
194
195struct AlwaysInlinerLegacyPass : public ModulePass {
196 bool InsertLifetime;
197
198 AlwaysInlinerLegacyPass()
199 : AlwaysInlinerLegacyPass(/*InsertLifetime*/ true) {}
200
201 AlwaysInlinerLegacyPass(bool InsertLifetime)
202 : ModulePass(ID), InsertLifetime(InsertLifetime) {}
203
204 /// Main run interface method.
205 bool runOnModule(Module &M) override {
206
207 auto &PSI = getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
208 auto GetAAR = [&](Function &F) -> AAResults & {
209 return getAnalysis<AAResultsWrapperPass>(F).getAAResults();
210 };
211 auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
212 return getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
213 };
214 auto GetTTI = [&](Function &F) -> TargetTransformInfo & {
215 return getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
216 };
217 auto GetTLI = [&](Function &F) -> const TargetLibraryInfo & {
218 return getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
219 };
220
221 return AlwaysInlineImpl(M, InsertLifetime, PSI, /*FAM=*/nullptr,
222 GetAssumptionCache, GetAAR, GetTTI, GetTLI);
223 }
224
225 static char ID; // Pass identification, replacement for typeid
226
227 void getAnalysisUsage(AnalysisUsage &AU) const override {
233 }
234};
235
236} // namespace
237
238char AlwaysInlinerLegacyPass::ID = 0;
239INITIALIZE_PASS_BEGIN(AlwaysInlinerLegacyPass, "always-inline",
240 "Inliner for always_inline functions", false, false)
246INITIALIZE_PASS_END(AlwaysInlinerLegacyPass, "always-inline",
247 "Inliner for always_inline functions", false, false)
248
250 return new AlwaysInlinerLegacyPass(InsertLifetime);
251}
252
256 MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
257 auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
258 return FAM.getResult<AssumptionAnalysis>(F);
259 };
260 auto GetAAR = [&](Function &F) -> AAResults & {
261 return FAM.getResult<AAManager>(F);
262 };
263 auto GetTTI = [&](Function &F) -> TargetTransformInfo & {
264 return FAM.getResult<TargetIRAnalysis>(F);
265 };
266 auto GetTLI = [&](Function &F) -> const TargetLibraryInfo & {
267 return FAM.getResult<TargetLibraryAnalysis>(F);
268 };
269 auto &PSI = MAM.getResult<ProfileSummaryAnalysis>(M);
270
271 bool Changed = AlwaysInlineImpl(M, InsertLifetime, PSI, &FAM,
272 GetAssumptionCache, GetAAR, GetTTI, GetTLI);
273 if (!Changed)
274 return PreservedAnalyses::all();
275
277 // We have already invalidated all analyses on modified functions.
279 return PA;
280}
Provides passes to inlining "always_inline" functions.
#define DEBUG_TYPE
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file implements a set that has insertion order iteration characteristics.
This pass exposes codegen information to IR-level passes.
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
bool hasFnAttr(Attribute::AttrKind Kind) const
Determine whether this call has the given attribute.
AttributeList getAttributes() const
Return the attributes for this call.
LLVM_ABI Function * getCaller()
Helper to get the caller (the parent function).
A debug info location.
Definition DebugLoc.h:123
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:337
static InlineCost getAlways(const char *Reason, std::optional< CostBenefitPair > CostBenefit=std::nullopt)
Definition InlineCost.h:127
This class captures the data input to the InlineFunction call, and records the auxiliary results prod...
Definition Cloning.h:259
SmallVector< CallBase *, 8 > InlinedCallSites
All of the new call sites inlined into the caller.
Definition Cloning.h:282
InlineResult is basically true or false.
Definition InlineCost.h:181
bool isSuccess() const
Definition InlineCost.h:190
const char * getFailureReason() const
Definition InlineCost.h:191
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
The optimization diagnostic interface.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Diagnostic information for missed-optimization remarks.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
Analysis providing profile information.
void clear()
Completely clear the SetVector.
Definition SetVector.h:267
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:339
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition ilist_node.h:34
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
DiagnosticInfoOptimizationBase::Argument NV
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
LLVM_ABI InlineResult InlineFunction(CallBase &CB, InlineFunctionInfo &IFI, bool MergeAttributes=false, AAResults *CalleeAAR=nullptr, bool InsertLifetime=true, bool TrackInlineHistory=false, Function *ForwardVarArgsTo=nullptr, OptimizationRemarkEmitter *ORE=nullptr)
This function inlines the called function into the basic block of the caller.
LLVM_ABI InlineResult isInlineViable(Function &Callee)
Check if it is mechanically possible to inline the function Callee, based on the contents of the func...
LLVM_ABI void emitInlinedIntoBasedOnCost(OptimizationRemarkEmitter &ORE, DebugLoc DLoc, const BasicBlock *Block, const Function &Callee, const Function &Caller, const InlineCost &IC, bool ForProfileContext=false, const char *PassName=nullptr)
Emit ORE message based in cost (default heuristic).
LLVM_ABI Pass * createAlwaysInlinerLegacyPass(bool InsertLifetime=true)
Create a legacy pass manager instance of a pass to inline and remove functions marked as "always_inli...
LLVM_ABI std::optional< InlineResult > getAttributeBasedInliningDecision(CallBase &Call, Function *Callee, TargetTransformInfo &CalleeTTI, function_ref< const TargetLibraryInfo &(Function &)> GetTLI)
Returns InlineResult::success() if the call site should be always inlined because of user directives,...
LLVM_ABI void filterDeadComdatFunctions(SmallVectorImpl< Function * > &DeadComdatFunctions)
Filter out potentially dead comdat functions where other entries keep the entire comdat group alive.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39