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 SmallVector<Function *, 4> NeedFlattening;
46
47 auto TryInline = [&](CallBase &CB, Function &Callee,
48 OptimizationRemarkEmitter &ORE, const char *InlineReason,
49 SmallVectorImpl<CallBase *> *NewCallSites =
50 nullptr) -> bool {
51 Function *Caller = CB.getCaller();
52 DebugLoc DLoc = CB.getDebugLoc();
54
55 InlineFunctionInfo IFI(GetAssumptionCache, &PSI);
56 InlineResult Res = InlineFunction(CB, IFI, /*MergeAttributes=*/true,
57 &GetAAR(Callee), InsertLifetime);
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 : make_early_inc_range(M)) {
79 if (F.hasFnAttribute(Attribute::Flatten))
80 NeedFlattening.push_back(&F);
81
82 if (F.isPresplitCoroutine())
83 continue;
84
85 if (F.isDeclaration() || !isInlineViable(F).isSuccess())
86 continue;
87
88 Calls.clear();
89
90 for (User *U : F.users())
91 if (auto *CB = dyn_cast<CallBase>(U))
92 if (CB->getCalledFunction() == &F &&
93 CB->hasFnAttr(Attribute::AlwaysInline) &&
94 !CB->getAttributes().hasFnAttr(Attribute::NoInline))
95 Calls.insert(CB);
96
97 for (CallBase *CB : Calls) {
99 Changed |= TryInline(*CB, F, ORE, "always inline attribute");
100 }
101
102 F.removeDeadConstantUsers();
103 if (F.hasFnAttribute(Attribute::AlwaysInline) && F.isDefTriviallyDead()) {
104 if (F.hasComdat()) {
105 InlinedComdatFunctions.push_back(&F);
106 } else {
107 if (FAM)
108 FAM->clear(F, F.getName());
109 M.getFunctionList().erase(F);
110 Changed = true;
111 }
112 }
113 }
114
115 // Flatten functions with the flatten attribute using a local worklist.
116 for (Function *F : NeedFlattening) {
119 SmallVector<CallBase *> NewCallSites;
121
122 // Collect initial calls.
123 for (BasicBlock &BB : *F) {
124 for (Instruction &I : BB) {
125 if (auto *CB = dyn_cast<CallBase>(&I)) {
126 Function *Callee = CB->getCalledFunction();
127 if (!Callee || Callee->isDeclaration())
128 continue;
129 Worklist.push_back({CB, -1});
130 }
131 }
132 }
133
134 while (!Worklist.empty()) {
135 auto Item = Worklist.pop_back_val();
136 CallBase *CB = Item.first;
137 int InlineHistoryID = Item.second;
138 Function *Callee = CB->getCalledFunction();
139 if (!Callee)
140 continue;
141
142 // Detect recursion.
143 if (Callee == F ||
144 inlineHistoryIncludes(Callee, InlineHistoryID, InlineHistory)) {
145 ORE.emit([&]() {
146 return OptimizationRemarkMissed("inline", "NotInlined",
147 CB->getDebugLoc(), CB->getParent())
148 << "'" << ore::NV("Callee", Callee)
149 << "' is not inlined into '"
150 << ore::NV("Caller", CB->getCaller())
151 << "': recursive call during flattening";
152 });
153 continue;
154 }
155
156 // Use getAttributeBasedInliningDecision for all attribute-based checks
157 // including TTI/TLI compatibility and isInlineViable.
158 TargetTransformInfo &CalleeTTI = GetTTI(*Callee);
159 auto Decision =
160 getAttributeBasedInliningDecision(*CB, Callee, CalleeTTI, GetTLI);
161 if (!Decision || !Decision->isSuccess())
162 continue;
163
164 if (!TryInline(*CB, *Callee, ORE, "flatten attribute", &NewCallSites))
165 continue;
166
167 Changed = true;
168
169 // Add new call sites from the inlined function to the worklist.
170 if (!NewCallSites.empty()) {
171 int NewHistoryID = InlineHistory.size();
172 InlineHistory.push_back({Callee, InlineHistoryID});
173 for (CallBase *NewCB : NewCallSites) {
174 Function *NewCallee = NewCB->getCalledFunction();
175 if (NewCallee && !NewCallee->isDeclaration())
176 Worklist.push_back({NewCB, NewHistoryID});
177 }
178 }
179 }
180 }
181
182 if (!InlinedComdatFunctions.empty()) {
183 // Now we just have the comdat functions. Filter out the ones whose comdats
184 // are not actually dead.
185 filterDeadComdatFunctions(InlinedComdatFunctions);
186 // The remaining functions are actually dead.
187 for (Function *F : InlinedComdatFunctions) {
188 if (FAM)
189 FAM->clear(*F, F->getName());
190 M.getFunctionList().erase(F);
191 Changed = true;
192 }
193 }
194
195 return Changed;
196}
197
198struct AlwaysInlinerLegacyPass : public ModulePass {
199 bool InsertLifetime;
200
201 AlwaysInlinerLegacyPass()
202 : AlwaysInlinerLegacyPass(/*InsertLifetime*/ true) {}
203
204 AlwaysInlinerLegacyPass(bool InsertLifetime)
205 : ModulePass(ID), InsertLifetime(InsertLifetime) {}
206
207 /// Main run interface method.
208 bool runOnModule(Module &M) override {
209
210 auto &PSI = getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
211 auto GetAAR = [&](Function &F) -> AAResults & {
212 return getAnalysis<AAResultsWrapperPass>(F).getAAResults();
213 };
214 auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
215 return getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
216 };
217 auto GetTTI = [&](Function &F) -> TargetTransformInfo & {
218 return getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
219 };
220 auto GetTLI = [&](Function &F) -> const TargetLibraryInfo & {
221 return getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
222 };
223
224 return AlwaysInlineImpl(M, InsertLifetime, PSI, /*FAM=*/nullptr,
225 GetAssumptionCache, GetAAR, GetTTI, GetTLI);
226 }
227
228 static char ID; // Pass identification, replacement for typeid
229
230 void getAnalysisUsage(AnalysisUsage &AU) const override {
236 }
237};
238
239} // namespace
240
241char AlwaysInlinerLegacyPass::ID = 0;
242INITIALIZE_PASS_BEGIN(AlwaysInlinerLegacyPass, "always-inline",
243 "Inliner for always_inline functions", false, false)
249INITIALIZE_PASS_END(AlwaysInlinerLegacyPass, "always-inline",
250 "Inliner for always_inline functions", false, false)
251
253 return new AlwaysInlinerLegacyPass(InsertLifetime);
254}
255
259 MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
260 auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
261 return FAM.getResult<AssumptionAnalysis>(F);
262 };
263 auto GetAAR = [&](Function &F) -> AAResults & {
264 return FAM.getResult<AAManager>(F);
265 };
266 auto GetTTI = [&](Function &F) -> TargetTransformInfo & {
267 return FAM.getResult<TargetIRAnalysis>(F);
268 };
269 auto GetTLI = [&](Function &F) -> const TargetLibraryInfo & {
270 return FAM.getResult<TargetLibraryAnalysis>(F);
271 };
272 auto &PSI = MAM.getResult<ProfileSummaryAnalysis>(M);
273
274 bool Changed = AlwaysInlineImpl(M, InsertLifetime, PSI, &FAM,
275 GetAssumptionCache, GetAAR, GetTTI, GetTLI);
276 if (!Changed)
277 return PreservedAnalyses::all();
278
280 // We have already invalidated all analyses on modified functions.
282 return PA;
283}
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:329
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:252
SmallVector< CallBase *, 8 > InlinedCallSites
All of the new call sites inlined into the caller.
Definition Cloning.h:275
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.
Definition Types.h:26
LLVM_ABI InlineResult InlineFunction(CallBase &CB, InlineFunctionInfo &IFI, bool MergeAttributes=false, AAResults *CalleeAAR=nullptr, bool InsertLifetime=true, Function *ForwardVarArgsTo=nullptr, OptimizationRemarkEmitter *ORE=nullptr)
This function inlines the called function into the basic block of the caller.
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:634
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
LLVM_ABI bool inlineHistoryIncludes(Function *F, int InlineHistoryID, ArrayRef< std::pair< Function *, int > > InlineHistory)
Check if Function F appears in the inline history chain.
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