LLVM 23.0.0git
Inliner.cpp
Go to the documentation of this file.
1//===- Inliner.cpp - Code common to all inliners --------------------------===//
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 the mechanics required to implement inlining without
10// missing any calls and updating the call graph. The decisions of which calls
11// are profitable to inline are implemented elsewhere.
12//
13//===----------------------------------------------------------------------===//
14
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/ScopeExit.h"
19#include "llvm/ADT/SetVector.h"
22#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/StringRef.h"
37#include "llvm/IR/Attributes.h"
38#include "llvm/IR/BasicBlock.h"
39#include "llvm/IR/DebugLoc.h"
42#include "llvm/IR/Function.h"
44#include "llvm/IR/Instruction.h"
47#include "llvm/IR/Metadata.h"
48#include "llvm/IR/Module.h"
49#include "llvm/IR/PassManager.h"
50#include "llvm/IR/Value.h"
51#include "llvm/Pass.h"
54#include "llvm/Support/Debug.h"
60#include <algorithm>
61#include <cassert>
62#include <utility>
63
64using namespace llvm;
65
66#define DEBUG_TYPE "inline"
67
68STATISTIC(NumInlined, "Number of functions inlined");
69STATISTIC(NumDeleted, "Number of functions deleted because all callers found");
70
72 "intra-scc-cost-multiplier", cl::init(2), cl::Hidden,
74 "Cost multiplier to multiply onto inlined call sites where the "
75 "new call was previously an intra-SCC call (not relevant when the "
76 "original call was already intra-SCC). This can accumulate over "
77 "multiple inlinings (e.g. if a call site already had a cost "
78 "multiplier and one of its inlined calls was also subject to "
79 "this, the inlined call would have the original multiplier "
80 "multiplied by intra-scc-cost-multiplier). This is to prevent tons of "
81 "inlining through a child SCC which can cause terrible compile times"));
82
83/// A flag for test, so we can print the content of the advisor when running it
84/// as part of the default (e.g. -O3) pipeline.
85static cl::opt<bool> KeepAdvisorForPrinting("keep-inline-advisor-for-printing",
86 cl::init(false), cl::Hidden);
87
88/// Allows printing the contents of the advisor after each SCC inliner pass.
89static cl::opt<bool>
90 EnablePostSCCAdvisorPrinting("enable-scc-inline-advisor-printing",
91 cl::init(false), cl::Hidden);
92
93
95 "cgscc-inline-replay", cl::init(""), cl::value_desc("filename"),
97 "Optimization remarks file containing inline remarks to be replayed "
98 "by cgscc inlining."),
100
102 "cgscc-inline-replay-scope",
105 "Replay on functions that have remarks associated "
106 "with them (default)"),
108 "Replay on the entire module")),
109 cl::desc("Whether inline replay should be applied to the entire "
110 "Module or just the Functions (default) that are present as "
111 "callers in remarks during cgscc inlining."),
112 cl::Hidden);
113
115 "cgscc-inline-replay-fallback",
120 "All decisions not in replay send to original advisor (default)"),
122 "AlwaysInline", "All decisions not in replay are inlined"),
124 "All decisions not in replay are not inlined")),
125 cl::desc(
126 "How cgscc inline replay treats sites that don't come from the replay. "
127 "Original: defers to original advisor, AlwaysInline: inline all sites "
128 "not in replay, NeverInline: inline no sites not in replay"),
129 cl::Hidden);
130
132 "cgscc-inline-replay-format",
135 clEnumValN(CallSiteFormat::Format::Line, "Line", "<Line Number>"),
137 "<Line Number>:<Column Number>"),
139 "LineDiscriminator", "<Line Number>.<Discriminator>"),
141 "LineColumnDiscriminator",
142 "<Line Number>:<Column Number>.<Discriminator> (default)")),
143 cl::desc("How cgscc inline replay file is formatted"), cl::Hidden);
144
146InlinerPass::getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM,
148 if (OwnedAdvisor)
149 return *OwnedAdvisor;
150
151 auto *IAA = MAM.getCachedResult<InlineAdvisorAnalysis>(M);
152 if (!IAA) {
153 // It should still be possible to run the inliner as a stand-alone SCC pass,
154 // for test scenarios. In that case, we default to the
155 // DefaultInlineAdvisor, which doesn't need to keep state between SCC pass
156 // runs. It also uses just the default InlineParams.
157 // In this case, we need to use the provided FAM, which is valid for the
158 // duration of the inliner pass, and thus the lifetime of the owned advisor.
159 // The one we would get from the MAM can be invalidated as a result of the
160 // inliner's activity.
161 OwnedAdvisor = std::make_unique<DefaultInlineAdvisor>(
162 M, FAM, getInlineParams(),
164
165 if (!CGSCCInlineReplayFile.empty())
166 OwnedAdvisor = getReplayInlineAdvisor(
167 M, FAM, M.getContext(), std::move(OwnedAdvisor),
168 ReplayInlinerSettings{CGSCCInlineReplayFile,
169 CGSCCInlineReplayScope,
170 CGSCCInlineReplayFallback,
171 {CGSCCInlineReplayFormat}},
172 /*EmitRemarks=*/true,
173 InlineContext{LTOPhase, InlinePass::ReplayCGSCCInliner});
174
175 return *OwnedAdvisor;
176 }
177 assert(IAA->getAdvisor() &&
178 "Expected a present InlineAdvisorAnalysis also have an "
179 "InlineAdvisor initialized");
180 return *IAA->getAdvisor();
181}
182
184 F.dropAllReferences();
186 BB.eraseFromParent();
187 BasicBlock *BB = BasicBlock::Create(F.getContext(), "", &F);
188 new UnreachableInst(F.getContext(), BB);
189}
190
193 CGSCCUpdateResult &UR) {
194 const auto &MAMProxy =
196 bool Changed = false;
197
198 assert(InitialC.size() > 0 && "Cannot handle an empty SCC!");
199 Module &M = *InitialC.begin()->getFunction().getParent();
200 ProfileSummaryInfo *PSI = MAMProxy.getCachedResult<ProfileSummaryAnalysis>(M);
201
204 .getManager();
205
206 InlineAdvisor &Advisor = getAdvisor(MAMProxy, FAM, M);
207 Advisor.onPassEntry(&InitialC);
208
209 // We use a single common worklist for calls across the entire SCC. We
210 // process these in-order and append new calls introduced during inlining to
211 // the end. The PriorityInlineOrder is optional here, in which the smaller
212 // callee would have a higher priority to inline.
213 //
214 // Note that this particular order of processing is actually critical to
215 // avoid very bad behaviors. Consider *highly connected* call graphs where
216 // each function contains a small amount of code and a couple of calls to
217 // other functions. Because the LLVM inliner is fundamentally a bottom-up
218 // inliner, it can handle gracefully the fact that these all appear to be
219 // reasonable inlining candidates as it will flatten things until they become
220 // too big to inline, and then move on and flatten another batch.
221 //
222 // However, when processing call edges *within* an SCC we cannot rely on this
223 // bottom-up behavior. As a consequence, with heavily connected *SCCs* of
224 // functions we can end up incrementally inlining N calls into each of
225 // N functions because each incremental inlining decision looks good and we
226 // don't have a topological ordering to prevent explosions.
227 //
228 // To compensate for this, we don't process transitive edges made immediate
229 // by inlining until we've done one pass of inlining across the entire SCC.
230 // Large, highly connected SCCs still lead to some amount of code bloat in
231 // this model, but it is uniformly spread across all the functions in the SCC
232 // and eventually they all become too large to inline, rather than
233 // incrementally maknig a single function grow in a super linear fashion.
235
236 // Populate the initial list of calls in this SCC.
237 for (auto &N : InitialC) {
238 auto &ORE =
239 FAM.getResult<OptimizationRemarkEmitterAnalysis>(N.getFunction());
240 // We want to generally process call sites top-down in order for
241 // simplifications stemming from replacing the call with the returned value
242 // after inlining to be visible to subsequent inlining decisions.
243 // FIXME: Using instructions sequence is a really bad way to do this.
244 // Instead we should do an actual RPO walk of the function body.
245 for (Instruction &I : instructions(N.getFunction()))
246 if (auto *CB = dyn_cast<CallBase>(&I))
247 if (Function *Callee = CB->getCalledFunction()) {
248 if (!Callee->isDeclaration())
249 Calls.push_back({CB, -1});
250 else if (!isa<IntrinsicInst>(I)) {
251 using namespace ore;
252 setInlineRemark(*CB, "unavailable definition");
253 ORE.emit([&]() {
254 return OptimizationRemarkMissed(DEBUG_TYPE, "NoDefinition", &I)
255 << NV("Callee", Callee) << " will not be inlined into "
256 << NV("Caller", CB->getCaller())
257 << " because its definition is unavailable"
258 << setIsVerbose();
259 });
260 }
261 }
262 }
263
264 // Capture updatable variable for the current SCC.
265 auto *C = &InitialC;
266
267 llvm::scope_exit AdvisorOnExit([&] { Advisor.onPassExit(C); });
268
269 if (Calls.empty())
270 return PreservedAnalyses::all();
271
272 // When inlining a callee produces new call sites, we want to keep track of
273 // the fact that they were inlined from the callee. This allows us to avoid
274 // infinite inlining in some obscure cases. To represent this, we use an
275 // index into the InlineHistory vector.
277
278 // Track a set vector of inlined callees so that we can augment the caller
279 // with all of their edges in the call graph before pruning out the ones that
280 // got simplified away.
281 SmallSetVector<Function *, 4> InlinedCallees;
282
283 // Track the dead functions to delete once finished with inlining calls. We
284 // defer deleting these to make it easier to handle the call graph updates.
285 SmallVector<Function *, 4> DeadFunctions;
286
287 // Track potentially dead non-local functions with comdats to see if they can
288 // be deleted as a batch after inlining.
289 SmallVector<Function *, 4> DeadFunctionsInComdats;
290
291 // Loop forward over all of the calls. Note that we cannot cache the size as
292 // inlining can introduce new calls that need to be processed.
293 for (int I = 0; I < (int)Calls.size(); ++I) {
294 // We expect the calls to typically be batched with sequences of calls that
295 // have the same caller, so we first set up some shared infrastructure for
296 // this caller. We also do any pruning we can at this layer on the caller
297 // alone.
298 Function &F = *Calls[I].first->getCaller();
300 if (CG.lookupSCC(N) != C)
301 continue;
302
303 LLVM_DEBUG(dbgs() << "Inlining calls in: " << F.getName() << "\n"
304 << " Function size: " << F.getInstructionCount()
305 << "\n");
306
307 auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
308 return FAM.getResult<AssumptionAnalysis>(F);
309 };
310
311 // Now process as many calls as we have within this caller in the sequence.
312 // We bail out as soon as the caller has to change so we can update the
313 // call graph and prepare the context of that new caller.
314 bool DidInline = false;
315 for (; I < (int)Calls.size() && Calls[I].first->getCaller() == &F; ++I) {
316 auto &P = Calls[I];
317 CallBase *CB = P.first;
318 const int InlineHistoryID = P.second;
319 Function &Callee = *CB->getCalledFunction();
320
321 if (InlineHistoryID != -1 &&
322 inlineHistoryIncludes(&Callee, InlineHistoryID, InlineHistory)) {
323 LLVM_DEBUG(dbgs() << "Skipping inlining due to history: " << F.getName()
324 << " -> " << Callee.getName() << "\n");
325 setInlineRemark(*CB, "recursive");
326 // Set noinline so that we don't forget this decision across CGSCC
327 // iterations.
328 CB->setIsNoInline();
329 continue;
330 }
331
332 // Check if this inlining may repeat breaking an SCC apart that has
333 // already been split once before. In that case, inlining here may
334 // trigger infinite inlining, much like is prevented within the inliner
335 // itself by the InlineHistory above, but spread across CGSCC iterations
336 // and thus hidden from the full inline history.
337 LazyCallGraph::SCC *CalleeSCC = CG.lookupSCC(*CG.lookup(Callee));
338 if (CalleeSCC == C && UR.InlinedInternalEdges.count({&N, C})) {
339 LLVM_DEBUG(dbgs() << "Skipping inlining internal SCC edge from a node "
340 "previously split out of this SCC by inlining: "
341 << F.getName() << " -> " << Callee.getName() << "\n");
342 setInlineRemark(*CB, "recursive SCC split");
343 continue;
344 }
345
346 std::unique_ptr<InlineAdvice> Advice =
347 Advisor.getAdvice(*CB, OnlyMandatory);
348
349 // Check whether we want to inline this callsite.
350 if (!Advice)
351 continue;
352
353 if (!Advice->isInliningRecommended()) {
354 Advice->recordUnattemptedInlining();
355 continue;
356 }
357
358 int CBCostMult =
361 .value_or(1);
362
363 // Setup the data structure used to plumb customization into the
364 // `InlineFunction` routine.
366 GetAssumptionCache, PSI,
367 &FAM.getResult<BlockFrequencyAnalysis>(*(CB->getCaller())),
368 &FAM.getResult<BlockFrequencyAnalysis>(Callee));
369
371 *CB, IFI, /*MergeAttributes=*/true,
372 &FAM.getResult<AAManager>(*CB->getCaller()), true, nullptr,
374 if (!IR.isSuccess()) {
375 Advice->recordUnsuccessfulInlining(IR);
376 continue;
377 }
378 // TODO: Shouldn't we be invalidating all analyses on F here?
379 // The caller was modified, so invalidate Ephemeral Values.
380 FAM.getResult<EphemeralValuesAnalysis>(F).clear();
381
382 DidInline = true;
383 InlinedCallees.insert(&Callee);
384 ++NumInlined;
385
386 LLVM_DEBUG(dbgs() << " Size after inlining: "
387 << F.getInstructionCount() << "\n");
388
389 // Add any new callsites to defined functions to the worklist.
390 if (!IFI.InlinedCallSites.empty()) {
391 int NewHistoryID = InlineHistory.size();
392 InlineHistory.push_back({&Callee, InlineHistoryID});
393
394 for (CallBase *ICB : reverse(IFI.InlinedCallSites)) {
395 Function *NewCallee = ICB->getCalledFunction();
396 assert(!(NewCallee && NewCallee->isIntrinsic()) &&
397 "Intrinsic calls should not be tracked.");
398 if (!NewCallee) {
399 // Try to promote an indirect (virtual) call without waiting for
400 // the post-inline cleanup and the next DevirtSCCRepeatedPass
401 // iteration because the next iteration may not happen and we may
402 // miss inlining it.
403 if (tryPromoteCall(*ICB))
404 NewCallee = ICB->getCalledFunction();
405 }
406 if (NewCallee) {
407 if (!NewCallee->isDeclaration()) {
408 Calls.push_back({ICB, NewHistoryID});
409 // Continually inlining through an SCC can result in huge compile
410 // times and bloated code since we arbitrarily stop at some point
411 // when the inliner decides it's not profitable to inline anymore.
412 // We attempt to mitigate this by making these calls exponentially
413 // more expensive.
414 // This doesn't apply to calls in the same SCC since if we do
415 // inline through the SCC the function will end up being
416 // self-recursive which the inliner bails out on, and inlining
417 // within an SCC is necessary for performance.
418 if (CalleeSCC != C &&
419 CalleeSCC == CG.lookupSCC(CG.get(*NewCallee))) {
420 Attribute NewCBCostMult = Attribute::get(
421 M.getContext(),
423 itostr(CBCostMult * IntraSCCCostMultiplier));
424 ICB->addFnAttr(NewCBCostMult);
425 }
426 }
427 }
428 }
429 }
430
431 // For local functions or discardable functions without comdats, check
432 // whether this makes the callee trivially dead. In that case, we can drop
433 // the body of the function eagerly which may reduce the number of callers
434 // of other functions to one, changing inline cost thresholds. Non-local
435 // discardable functions with comdats are checked later on.
436 bool CalleeWasDeleted = false;
437 if (Callee.isDiscardableIfUnused() && Callee.hasZeroLiveUses() &&
438 !CG.isLibFunction(Callee)) {
439 if (Callee.hasLocalLinkage() || !Callee.hasComdat()) {
440 Calls.erase(
441 std::remove_if(Calls.begin() + I + 1, Calls.end(),
442 [&](const std::pair<CallBase *, int> &Call) {
443 return Call.first->getCaller() == &Callee;
444 }),
445 Calls.end());
446
447 // Report inlining decision BEFORE deleting function contents, so we
448 // can still access e.g. the DebugLoc
449 Advice->recordInliningWithCalleeDeleted();
450 // Clear the body and queue the function itself for call graph
451 // updating when we finish inlining.
453 assert(!is_contained(DeadFunctions, &Callee) &&
454 "Cannot put cause a function to become dead twice!");
455 DeadFunctions.push_back(&Callee);
456 CalleeWasDeleted = true;
457 } else {
458 DeadFunctionsInComdats.push_back(&Callee);
459 }
460 }
461 if (!CalleeWasDeleted)
462 Advice->recordInlining();
463 }
464
465 // Back the call index up by one to put us in a good position to go around
466 // the outer loop.
467 --I;
468
469 if (!DidInline)
470 continue;
471 Changed = true;
472
473 // At this point, since we have made changes we have at least removed
474 // a call instruction. However, in the process we do some incremental
475 // simplification of the surrounding code. This simplification can
476 // essentially do all of the same things as a function pass and we can
477 // re-use the exact same logic for updating the call graph to reflect the
478 // change.
479
480 // Inside the update, we also update the FunctionAnalysisManager in the
481 // proxy for this particular SCC. We do this as the SCC may have changed and
482 // as we're going to mutate this particular function we want to make sure
483 // the proxy is in place to forward any invalidation events.
484 LazyCallGraph::SCC *OldC = C;
486 LLVM_DEBUG(dbgs() << "Updated inlining SCC: " << *C << "\n");
487
488 // If this causes an SCC to split apart into multiple smaller SCCs, there
489 // is a subtle risk we need to prepare for. Other transformations may
490 // expose an "infinite inlining" opportunity later, and because of the SCC
491 // mutation, we will revisit this function and potentially re-inline. If we
492 // do, and that re-inlining also has the potentially to mutate the SCC
493 // structure, the infinite inlining problem can manifest through infinite
494 // SCC splits and merges. To avoid this, we capture the originating caller
495 // node and the SCC containing the call edge. This is a slight over
496 // approximation of the possible inlining decisions that must be avoided,
497 // but is relatively efficient to store. We use C != OldC to know when
498 // a new SCC is generated and the original SCC may be generated via merge
499 // in later iterations.
500 //
501 // It is also possible that even if no new SCC is generated
502 // (i.e., C == OldC), the original SCC could be split and then merged
503 // into the same one as itself. and the original SCC will be added into
504 // UR.CWorklist again, we want to catch such cases too.
505 //
506 // FIXME: This seems like a very heavyweight way of retaining the inline
507 // history, we should look for a more efficient way of tracking it.
508 if ((C != OldC || UR.CWorklist.count(OldC)) &&
509 llvm::any_of(InlinedCallees, [&](Function *Callee) {
510 return CG.lookupSCC(*CG.lookup(*Callee)) == OldC;
511 })) {
512 LLVM_DEBUG(dbgs() << "Inlined an internal call edge and split an SCC, "
513 "retaining this to avoid infinite inlining.\n");
514 UR.InlinedInternalEdges.insert({&N, OldC});
515 }
516 InlinedCallees.clear();
517
518 // Invalidate analyses for this function now so that we don't have to
519 // invalidate analyses for all functions in this SCC later.
520 FAM.invalidate(F, PreservedAnalyses::none());
521 }
522
523 // We must ensure that we only delete functions with comdats if every function
524 // in the comdat is going to be deleted.
525 if (!DeadFunctionsInComdats.empty()) {
526 filterDeadComdatFunctions(DeadFunctionsInComdats);
527 for (auto *Callee : DeadFunctionsInComdats)
529 DeadFunctions.append(DeadFunctionsInComdats);
530 }
531
532 // Now that we've finished inlining all of the calls across this SCC, delete
533 // all of the trivially dead functions, updating the call graph and the CGSCC
534 // pass manager in the process.
535 //
536 // Note that this walks a pointer set which has non-deterministic order but
537 // that is OK as all we do is delete things and add pointers to unordered
538 // sets.
539 for (Function *DeadF : DeadFunctions) {
540 CG.markDeadFunction(*DeadF);
541 // Get the necessary information out of the call graph and nuke the
542 // function there. Also, clear out any cached analyses.
543 auto &DeadC = *CG.lookupSCC(*CG.lookup(*DeadF));
544 FAM.clear(*DeadF, DeadF->getName());
545 AM.clear(DeadC, DeadC.getName());
546
547 // Mark the relevant parts of the call graph as invalid so we don't visit
548 // them.
549 UR.InvalidatedSCCs.insert(&DeadC);
550
551 UR.DeadFunctions.push_back(DeadF);
552
553 ++NumDeleted;
554 }
555
556 if (!Changed)
557 return PreservedAnalyses::all();
558
560 // Even if we change the IR, we update the core CGSCC data structures and so
561 // can preserve the proxy to the function analysis manager.
563 // We have already invalidated all analyses on modified functions.
565 return PA;
566}
567
569 bool MandatoryFirst,
570 InlineContext IC,
572 unsigned MaxDevirtIterations)
573 : Params(Params), IC(IC), Mode(Mode),
574 MaxDevirtIterations(MaxDevirtIterations) {
575 // Run the inliner first. The theory is that we are walking bottom-up and so
576 // the callees have already been fully optimized, and we want to inline them
577 // into the callers so that our optimizations can reflect that.
578 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
579 // because it makes profile annotation in the backend inaccurate.
580 if (MandatoryFirst) {
581 PM.addPass(InlinerPass(/*OnlyMandatory*/ true));
584 }
585 PM.addPass(InlinerPass());
588}
589
592 auto &IAA = MAM.getResult<InlineAdvisorAnalysis>(M);
593 if (!IAA.tryCreate(Params, Mode,
594 {CGSCCInlineReplayFile,
595 CGSCCInlineReplayScope,
596 CGSCCInlineReplayFallback,
597 {CGSCCInlineReplayFormat}},
598 IC)) {
599 M.getContext().emitError(
600 "Could not setup Inlining Advisor for the requested "
601 "mode and/or options");
602 return PreservedAnalyses::all();
603 }
604
605 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
606 // to detect when we devirtualize indirect calls and iterate the SCC passes
607 // in that case to try and catch knock-on inlining or function attrs
608 // opportunities. Then we add it to the module pipeline by walking the SCCs
609 // in postorder (or bottom-up).
610 // If MaxDevirtIterations is 0, we just don't use the devirtualization
611 // wrapper.
612 if (MaxDevirtIterations == 0)
613 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(PM)));
614 else
617
618 MPM.addPass(std::move(AfterCGMPM));
619 MPM.run(M, MAM);
620
621 // Discard the InlineAdvisor, a subsequent inlining session should construct
622 // its own.
623 auto PA = PreservedAnalyses::all();
625 PA.abandon<InlineAdvisorAnalysis>();
626 return PA;
627}
628
630 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
631 static_cast<PassInfoMixin<InlinerPass> *>(this)->printPipeline(
632 OS, MapClassName2PassName);
633 if (OnlyMandatory)
634 OS << "<only-mandatory>";
635}
636
638 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
639 // Print some info about passes added to the wrapper. This is however
640 // incomplete as InlineAdvisorAnalysis part isn't included (which also depends
641 // on Params and Mode).
642 if (!MPM.isEmpty()) {
643 MPM.printPipeline(OS, MapClassName2PassName);
644 OS << ',';
645 }
646 OS << "cgscc(";
647 if (MaxDevirtIterations != 0)
648 OS << "devirt<" << MaxDevirtIterations << ">(";
649 PM.printPipeline(OS, MapClassName2PassName);
650 if (MaxDevirtIterations != 0)
651 OS << ')';
652 OS << ')';
653}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Expand Atomic instructions
This file contains the simple types necessary to represent the attributes associated with functions a...
This is the interface for LLVM's primary stateless and local alias analysis.
This header provides classes for managing passes over SCCs of the call graph.
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
#define DEBUG_TYPE
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
static cl::opt< ReplayInlinerSettings::Scope > CGSCCInlineReplayScope("cgscc-inline-replay-scope", cl::init(ReplayInlinerSettings::Scope::Function), cl::values(clEnumValN(ReplayInlinerSettings::Scope::Function, "Function", "Replay on functions that have remarks associated " "with them (default)"), clEnumValN(ReplayInlinerSettings::Scope::Module, "Module", "Replay on the entire module")), cl::desc("Whether inline replay should be applied to the entire " "Module or just the Functions (default) that are present as " "callers in remarks during cgscc inlining."), cl::Hidden)
static cl::opt< bool > KeepAdvisorForPrinting("keep-inline-advisor-for-printing", cl::init(false), cl::Hidden)
A flag for test, so we can print the content of the advisor when running it as part of the default (e...
static cl::opt< std::string > CGSCCInlineReplayFile("cgscc-inline-replay", cl::init(""), cl::value_desc("filename"), cl::desc("Optimization remarks file containing inline remarks to be replayed " "by cgscc inlining."), cl::Hidden)
static cl::opt< bool > EnablePostSCCAdvisorPrinting("enable-scc-inline-advisor-printing", cl::init(false), cl::Hidden)
Allows printing the contents of the advisor after each SCC inliner pass.
static cl::opt< int > IntraSCCCostMultiplier("intra-scc-cost-multiplier", cl::init(2), cl::Hidden, cl::desc("Cost multiplier to multiply onto inlined call sites where the " "new call was previously an intra-SCC call (not relevant when the " "original call was already intra-SCC). This can accumulate over " "multiple inlinings (e.g. if a call site already had a cost " "multiplier and one of its inlined calls was also subject to " "this, the inlined call would have the original multiplier " "multiplied by intra-scc-cost-multiplier). This is to prevent tons of " "inlining through a child SCC which can cause terrible compile times"))
static cl::opt< CallSiteFormat::Format > CGSCCInlineReplayFormat("cgscc-inline-replay-format", cl::init(CallSiteFormat::Format::LineColumnDiscriminator), cl::values(clEnumValN(CallSiteFormat::Format::Line, "Line", "<Line Number>"), clEnumValN(CallSiteFormat::Format::LineColumn, "LineColumn", "<Line Number>:<Column Number>"), clEnumValN(CallSiteFormat::Format::LineDiscriminator, "LineDiscriminator", "<Line Number>.<Discriminator>"), clEnumValN(CallSiteFormat::Format::LineColumnDiscriminator, "LineColumnDiscriminator", "<Line Number>:<Column Number>.<Discriminator> (default)")), cl::desc("How cgscc inline replay file is formatted"), cl::Hidden)
void makeFunctionBodyUnreachable(Function &F)
Definition Inliner.cpp:183
static cl::opt< ReplayInlinerSettings::Fallback > CGSCCInlineReplayFallback("cgscc-inline-replay-fallback", cl::init(ReplayInlinerSettings::Fallback::Original), cl::values(clEnumValN(ReplayInlinerSettings::Fallback::Original, "Original", "All decisions not in replay send to original advisor (default)"), clEnumValN(ReplayInlinerSettings::Fallback::AlwaysInline, "AlwaysInline", "All decisions not in replay are inlined"), clEnumValN(ReplayInlinerSettings::Fallback::NeverInline, "NeverInline", "All decisions not in replay are not inlined")), cl::desc("How cgscc inline replay treats sites that don't come from the replay. " "Original: defers to original advisor, AlwaysInline: inline all sites " "not in replay, NeverInline: inline no sites not in replay"), cl::Hidden)
Implements a lazy call graph analysis and related passes for the new pass manager.
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:81
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declarations for metadata subclasses.
#define P(N)
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
This file provides a priority worklist.
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:114
A manager for alias analyses.
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
void clear(IRUnitT &IR, llvm::StringRef Name)
Clear any cached analysis results for a single unit of IR.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:105
static LLVM_ABI Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
Analysis pass which computes BlockFrequencyInfo.
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...
void setIsNoInline()
LLVM_ABI Function * getCaller()
Helper to get the caller (the parent function).
A proxy from a FunctionAnalysisManager to an SCC.
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition Function.h:251
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
Printer pass for the InlineAdvisorAnalysis results.
The InlineAdvisorAnalysis is a module pass because the InlineAdvisor needs to capture state right bef...
Result run(Module &M, ModuleAnalysisManager &MAM)
Interface for deciding whether to inline a call site or not.
virtual void onPassEntry(LazyCallGraph::SCC *SCC=nullptr)
This must be called when the Inliner pass is entered, to allow the InlineAdvisor update internal stat...
virtual void onPassExit(LazyCallGraph::SCC *SCC=nullptr)
This must be called when the Inliner pass is exited, as function passes may be run subsequently.
std::unique_ptr< InlineAdvice > getAdvice(CallBase &CB, bool MandatoryOnly=false)
Get an InlineAdvice containing a recommendation on whether to inline or not.
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
The inliner pass for the new pass manager.
Definition Inliner.h:36
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition Inliner.cpp:629
LLVM_ABI PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, LazyCallGraph &CG, CGSCCUpdateResult &UR)
Definition Inliner.cpp:191
A node in the call graph.
An SCC of the call graph.
A lazily constructed view of the call graph of a module.
bool isLibFunction(Function &F) const
Test whether a function is a known and defined library function tracked by the call graph.
LLVM_ABI void markDeadFunction(Function &F)
Mark a function as dead to be removed later by removeDeadFunctions().
Node & get(Function &F)
Get a graph node for a given function, scanning it to populate the graph data as necessary.
SCC * lookupSCC(Node &N) const
Lookup a function's SCC in the graph.
Node * lookup(const Function &F) const
Lookup a function in the graph which has already been scanned and added.
LLVM_ABI PreservedAnalyses run(Module &, ModuleAnalysisManager &)
Definition Inliner.cpp:590
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition Inliner.cpp:637
LLVM_ABI ModuleInlinerWrapperPass(InlineParams Params=getInlineParams(), bool MandatoryFirst=true, InlineContext IC={}, InliningAdvisorMode Mode=InliningAdvisorMode::Default, unsigned MaxDevirtIterations=0)
Definition Inliner.cpp:568
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Diagnostic information for missed-optimization remarks.
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
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
An analysis pass based on the new PM 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
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This function has undefined behavior.
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
CallInst * Call
Changed
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
const char FunctionInlineCostMultiplierAttributeName[]
Definition InlineCost.h:60
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
Add a small namespace to avoid name clashes with the classes used in the streaming interface.
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
InliningAdvisorMode
There are 4 scenarios we can use the InlineAdvisor:
LLVM_ABI std::optional< int > getStringFnAttrAsInt(CallBase &CB, StringRef AttrKind)
DevirtSCCRepeatedPass createDevirtSCCRepeatedPass(CGSCCPassT &&Pass, int MaxIterations)
A function to deduce a function pass type and wrap it in the templated adaptor.
LLVM_ABI LazyCallGraph::SCC & updateCGAndAnalysisManagerForCGSCCPass(LazyCallGraph &G, LazyCallGraph::SCC &C, LazyCallGraph::Node &N, CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, FunctionAnalysisManager &FAM)
Helper to update the call graph after running a CGSCC pass.
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
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 void setInlineRemark(CallBase &CB, StringRef Message)
Set the inline-remark attribute.
AnalysisManager< LazyCallGraph::SCC, LazyCallGraph & > CGSCCAnalysisManager
The CGSCC analysis manager.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
ModuleToPostOrderCGSCCPassAdaptor createModuleToPostOrderCGSCCPassAdaptor(CGSCCPassT &&Pass)
A function to deduce a function pass type and wrap it in the templated adaptor.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
std::unique_ptr< InlineAdvisor > getReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM, LLVMContext &Context, std::unique_ptr< InlineAdvisor > OriginalAdvisor, const ReplayInlinerSettings &ReplaySettings, bool EmitRemarks, InlineContext IC)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
OuterAnalysisManagerProxy< ModuleAnalysisManager, LazyCallGraph::SCC, LazyCallGraph & > ModuleAnalysisManagerCGSCCProxy
A proxy from a ModuleAnalysisManager to an SCC.
LLVM_ABI InlineParams getInlineParams()
Generate the parameters to tune the inline cost analysis based only on the commandline options.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
LLVM_ABI void filterDeadComdatFunctions(SmallVectorImpl< Function * > &DeadComdatFunctions)
Filter out potentially dead comdat functions where other entries keep the entire comdat group alive.
LLVM_ABI bool tryPromoteCall(CallBase &CB)
Try to promote (devirtualize) a virtual call on an Alloca.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
cl::opt< unsigned > MaxDevirtIterations("max-devirt-iterations", cl::ReallyHidden, cl::init(4))
std::string itostr(int64_t X)
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
#define N
Support structure for SCC passes to communicate updates the call graph back to the CGSCC pass manager...
SmallPriorityWorklist< LazyCallGraph::SCC *, 1 > & CWorklist
Worklist of the SCCs queued for processing.
SmallDenseSet< std::pair< LazyCallGraph::Node *, LazyCallGraph::SCC * >, 4 > & InlinedInternalEdges
A hacky area where the inliner can retain history about inlining decisions that mutated the call grap...
SmallVector< Function *, 4 > & DeadFunctions
Functions that a pass has considered to be dead to be removed at the end of the call graph walk in ba...
SmallPtrSetImpl< LazyCallGraph::SCC * > & InvalidatedSCCs
The set of invalidated SCCs which should be skipped if they are found in CWorklist.
Provides context on when an inline advisor is constructed in the pipeline (e.g., link phase,...
Thresholds to tune inline cost analysis.
Definition InlineCost.h:207
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:70