LLVM 23.0.0git
ModuleSummaryAnalysis.cpp
Go to the documentation of this file.
1//===- ModuleSummaryAnalysis.cpp - Module summary index builder -----------===//
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 pass builds a ModuleSummaryIndex object for the module, to be written
10// to bitcode or LLVM assembly.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/MapVector.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SetVector.h"
22#include "llvm/ADT/StringRef.h"
32#include "llvm/IR/Attributes.h"
33#include "llvm/IR/BasicBlock.h"
34#include "llvm/IR/Constant.h"
35#include "llvm/IR/Constants.h"
36#include "llvm/IR/Dominators.h"
37#include "llvm/IR/Function.h"
38#include "llvm/IR/GlobalAlias.h"
39#include "llvm/IR/GlobalValue.h"
43#include "llvm/IR/LLVMContext.h"
44#include "llvm/IR/Metadata.h"
45#include "llvm/IR/Module.h"
47#include "llvm/IR/Use.h"
48#include "llvm/IR/User.h"
52#include "llvm/Pass.h"
57#include <cassert>
58#include <cstdint>
59#include <vector>
60
61using namespace llvm;
62using namespace llvm::memprof;
63
64#define DEBUG_TYPE "module-summary-analysis"
65
66// Option to force edges cold which will block importing when the
67// -import-cold-multiplier is set to 0. Useful for debugging.
68namespace llvm {
71
73 "force-summary-edges-cold", cl::Hidden, cl::location(ForceSummaryEdgesCold),
74 cl::desc("Force all edges in the function summary to cold"),
77 "all-non-critical", "All non-critical edges."),
78 clEnumValN(FunctionSummary::FSHT_All, "all", "All edges.")));
79
81 "module-summary-dot-file", cl::Hidden, cl::value_desc("filename"),
82 cl::desc("File to emit dot graph of new summary into"));
83
85 "enable-memprof-indirect-call-support", cl::init(true), cl::Hidden,
87 "Enable MemProf support for summarizing and cloning indirect calls"));
88
89// This can be used to override the number of callees created from VP metadata
90// normally taken from the -icp-max-prom option with a larger amount, if useful
91// for analysis. Use a separate option so that we can control the number of
92// indirect callees for ThinLTO summary based analysis (e.g. for MemProf which
93// needs this information for a correct and not overly-conservative callsite
94// graph analysis, especially because allocation contexts may not be very
95// frequent), without affecting normal ICP.
97 MaxSummaryIndirectEdges("module-summary-max-indirect-edges", cl::init(0),
99 cl::desc("Max number of summary edges added from "
100 "indirect call profile metadata"));
101
103
105
107} // namespace llvm
108
109// Walk through the operands of a given User via worklist iteration and populate
110// the set of GlobalValue references encountered. Invoked either on an
111// Instruction or a GlobalVariable (which walks its initializer).
112// Return true if any of the operands contains blockaddress. This is important
113// to know when computing summary for global var, because if global variable
114// references basic block address we can't import it separately from function
115// containing that basic block. For simplicity we currently don't import such
116// global vars at all. When importing function we aren't interested if any
117// instruction in it takes an address of any basic block, because instruction
118// can only take an address of basic block located in the same function.
119// Set `RefLocalLinkageIFunc` to true if the analyzed value references a
120// local-linkage ifunc.
121static bool
122findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,
125 bool &RefLocalLinkageIFunc) {
126 bool HasBlockAddress = false;
128 if (Visited.insert(CurUser).second)
129 Worklist.push_back(CurUser);
130
131 while (!Worklist.empty()) {
132 const User *U = Worklist.pop_back_val();
133 const auto *CB = dyn_cast<CallBase>(U);
134
135 for (const auto &OI : U->operands()) {
136 const User *Operand = dyn_cast<User>(OI);
137 if (!Operand)
138 continue;
139 if (isa<BlockAddress>(Operand)) {
140 HasBlockAddress = true;
141 continue;
142 }
143 if (auto *GV = dyn_cast<GlobalValue>(Operand)) {
144 // We have a reference to a global value. This should be added to
145 // the reference set unless it is a callee. Callees are handled
146 // specially by WriteFunction and are added to a separate list.
147 if (!(CB && CB->isCallee(&OI))) {
148 // If an ifunc has local linkage, do not add it into ref edges, and
149 // sets `RefLocalLinkageIFunc` to true. The referencer is not eligible
150 // for import. An ifunc doesn't have summary and ThinLTO cannot
151 // promote it; importing the referencer may cause linkage errors.
152 if (auto *GI = dyn_cast_if_present<GlobalIFunc>(GV);
153 GI && GI->hasLocalLinkage()) {
154 RefLocalLinkageIFunc = true;
155 continue;
156 }
157 RefEdges.insert(Index.getOrInsertValueInfo(GV));
158 }
159 continue;
160 }
161 if (Visited.insert(Operand).second)
162 Worklist.push_back(Operand);
163 }
164 }
165
166 const Instruction *I = dyn_cast<Instruction>(CurUser);
167 if (I) {
168 uint64_t TotalCount = 0;
169 // MaxNumVTableAnnotations is the maximum number of vtables annotated on
170 // the instruction.
171 auto ValueDataArray = getValueProfDataFromInst(
172 *I, IPVK_VTableTarget, MaxNumVTableAnnotations, TotalCount);
173
174 for (const auto &V : ValueDataArray)
175 RefEdges.insert(Index.getOrInsertValueInfo(/* VTableGUID = */
176 V.Value));
177 }
178 return HasBlockAddress;
179}
180
181/// Collect globals referenced via !implicit.ref metadata on a function
182/// and add them as reference edges in the module summary. This ensures
183/// ThinLTO liveness analysis treats them as live when the function is
184/// live, and imports them alongside the function during cross-module
185/// importing.
187 ModuleSummaryIndex &Index, const Function &F,
189 if (!F.hasMetadata(LLVMContext::MD_implicit_ref))
190 return;
192 F.getMetadata(LLVMContext::MD_implicit_ref, MDs);
193 for (MDNode *MD : MDs) {
194 for (const MDOperand &Op : MD->operands()) {
195 if (auto *VAM = dyn_cast_or_null<ValueAsMetadata>(Op.get()))
196 if (auto *GV = dyn_cast<GlobalValue>(VAM->getValue()))
197 RefEdges.insert(Index.getOrInsertValueInfo(GV));
198 }
199 }
200}
201
212
213static bool isNonRenamableLocal(const GlobalValue &GV) {
214 return GV.hasSection() && GV.hasLocalLinkage();
215}
216
217/// Determine whether this call has all constant integer arguments (excluding
218/// "this") and summarize it to VCalls or ConstVCalls as appropriate.
219static void addVCallToSet(
221 SetVector<FunctionSummary::VFuncId, std::vector<FunctionSummary::VFuncId>>
222 &VCalls,
224 std::vector<FunctionSummary::ConstVCall>> &ConstVCalls) {
225 std::vector<uint64_t> Args;
226 // Start from the second argument to skip the "this" pointer.
227 for (auto &Arg : drop_begin(Call.CB.args())) {
228 auto *CI = dyn_cast<ConstantInt>(Arg);
229 if (!CI || CI->getBitWidth() > 64) {
230 VCalls.insert({Guid, Call.Offset});
231 return;
232 }
233 Args.push_back(CI->getZExtValue());
234 }
235 ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)});
236}
237
238/// If this intrinsic call requires that we add information to the function
239/// summary, do so via the non-constant reference arguments.
241 const CallInst *CI,
242 SetVector<GlobalValue::GUID, std::vector<GlobalValue::GUID>> &TypeTests,
243 SetVector<FunctionSummary::VFuncId, std::vector<FunctionSummary::VFuncId>>
244 &TypeTestAssumeVCalls,
245 SetVector<FunctionSummary::VFuncId, std::vector<FunctionSummary::VFuncId>>
246 &TypeCheckedLoadVCalls,
248 std::vector<FunctionSummary::ConstVCall>>
249 &TypeTestAssumeConstVCalls,
251 std::vector<FunctionSummary::ConstVCall>>
252 &TypeCheckedLoadConstVCalls,
253 DominatorTree &DT) {
254 switch (CI->getCalledFunction()->getIntrinsicID()) {
255 case Intrinsic::type_test:
256 case Intrinsic::public_type_test: {
257 auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1));
258 auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
259 if (!TypeId)
260 break;
263
264 // Produce a summary from type.test intrinsics. We only summarize type.test
265 // intrinsics that are used other than by an llvm.assume intrinsic.
266 // Intrinsics that are assumed are relevant only to the devirtualization
267 // pass, not the type test lowering pass.
268 bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) {
269 return !isa<AssumeInst>(CIU.getUser());
270 });
271 if (HasNonAssumeUses)
272 TypeTests.insert(Guid);
273
276 findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT);
277 for (auto &Call : DevirtCalls)
278 addVCallToSet(Call, Guid, TypeTestAssumeVCalls,
279 TypeTestAssumeConstVCalls);
280
281 break;
282 }
283
284 case Intrinsic::type_checked_load_relative:
285 case Intrinsic::type_checked_load: {
286 auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2));
287 auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
288 if (!TypeId)
289 break;
292
296 bool HasNonCallUses = false;
297 findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
298 HasNonCallUses, CI, DT);
299 // Any non-call uses of the result of llvm.type.checked.load will
300 // prevent us from optimizing away the llvm.type.test.
301 if (HasNonCallUses)
302 TypeTests.insert(Guid);
303 for (auto &Call : DevirtCalls)
304 addVCallToSet(Call, Guid, TypeCheckedLoadVCalls,
305 TypeCheckedLoadConstVCalls);
306
307 break;
308 }
309 default:
310 break;
311 }
312}
313
314static bool isNonVolatileLoad(const Instruction *I) {
315 if (const auto *LI = dyn_cast<LoadInst>(I))
316 return !LI->isVolatile();
317
318 return false;
319}
320
321static bool isNonVolatileStore(const Instruction *I) {
322 if (const auto *SI = dyn_cast<StoreInst>(I))
323 return !SI->isVolatile();
324
325 return false;
326}
327
328// Returns true if the function definition must be unreachable.
329//
330// Note if this helper function returns true, `F` is guaranteed
331// to be unreachable; if it returns false, `F` might still
332// be unreachable but not covered by this helper function.
334 // A function must be unreachable if its entry block ends with an
335 // 'unreachable'.
336 assert(!F.isDeclaration());
337 return isa<UnreachableInst>(F.getEntryBlock().getTerminator());
338}
339
341 ModuleSummaryIndex &Index, const Module &M, const Function &F,
343 bool HasLocalsInUsedOrAsm, DenseSet<GlobalValue::GUID> &CantBePromoted,
344 bool IsThinLTO,
345 std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) {
346 // Summary not currently supported for anonymous functions, they should
347 // have been named.
348 assert(F.hasName());
349
350 unsigned NumInsts = 0;
351 // Map from callee ValueId to profile count. Used to accumulate profile
352 // counts for all static calls to a given callee.
355 CallGraphEdges;
357 StoreRefEdges;
360 TypeTestAssumeVCalls, TypeCheckedLoadVCalls;
362 std::vector<FunctionSummary::ConstVCall>>
363 TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls;
364 ICallPromotionAnalysis ICallAnalysis;
366
367 // Add personality function, prefix data and prologue data to function's ref
368 // list.
369 bool HasLocalIFuncCallOrRef = false;
370 findRefEdges(Index, &F, RefEdges, Visited, HasLocalIFuncCallOrRef);
371 findImplicitRefEdges(Index, F, RefEdges);
372
373 std::vector<const Instruction *> NonVolatileLoads;
374 std::vector<const Instruction *> NonVolatileStores;
375
376 std::vector<CallsiteInfo> Callsites;
377 std::vector<AllocInfo> Allocs;
378
379#ifndef NDEBUG
380 DenseSet<const CallBase *> CallsThatMayHaveMemprofSummary;
381#endif
382
383 bool HasInlineAsmMaybeReferencingInternal = false;
384 bool HasIndirBranchToBlockAddress = false;
385 bool HasUnknownCall = false;
386 bool MayThrow = false;
387 for (const BasicBlock &BB : F) {
388 // We don't allow inlining of function with indirect branch to blockaddress.
389 // If the blockaddress escapes the function, e.g., via a global variable,
390 // inlining may lead to an invalid cross-function reference. So we shouldn't
391 // import such function either.
392 if (BB.hasAddressTaken()) {
393 for (User *U : BlockAddress::get(const_cast<BasicBlock *>(&BB))->users())
394 if (!isa<CallBrInst>(*U)) {
395 HasIndirBranchToBlockAddress = true;
396 break;
397 }
398 }
399
400 for (const Instruction &I : BB) {
401 if (I.isDebugOrPseudoInst())
402 continue;
403 ++NumInsts;
404
405 // Regular LTO module doesn't participate in ThinLTO import,
406 // so no reference from it can be read/writeonly, since this
407 // would require importing variable as local copy
408 if (IsThinLTO) {
409 if (isNonVolatileLoad(&I)) {
410 // Postpone processing of non-volatile load instructions
411 // See comments below
412 Visited.insert(&I);
413 NonVolatileLoads.push_back(&I);
414 continue;
415 } else if (isNonVolatileStore(&I)) {
416 Visited.insert(&I);
417 NonVolatileStores.push_back(&I);
418 // All references from second operand of store (destination address)
419 // can be considered write-only if they're not referenced by any
420 // non-store instruction. References from first operand of store
421 // (stored value) can't be treated either as read- or as write-only
422 // so we add them to RefEdges as we do with all other instructions
423 // except non-volatile load.
424 Value *Stored = I.getOperand(0);
425 if (auto *GV = dyn_cast<GlobalValue>(Stored))
426 // findRefEdges will try to examine GV operands, so instead
427 // of calling it we should add GV to RefEdges directly.
428 RefEdges.insert(Index.getOrInsertValueInfo(GV));
429 else if (auto *U = dyn_cast<User>(Stored))
430 findRefEdges(Index, U, RefEdges, Visited, HasLocalIFuncCallOrRef);
431 continue;
432 }
433 }
434 findRefEdges(Index, &I, RefEdges, Visited, HasLocalIFuncCallOrRef);
435 const auto *CB = dyn_cast<CallBase>(&I);
436 if (!CB) {
437 if (I.mayThrow())
438 MayThrow = true;
439 continue;
440 }
441
442 const auto *CI = dyn_cast<CallInst>(&I);
443 // Since we don't know exactly which local values are referenced in inline
444 // assembly, conservatively mark the function as possibly referencing
445 // a local value from inline assembly to ensure we don't export a
446 // reference (which would require renaming and promotion of the
447 // referenced value).
448 if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm())
449 HasInlineAsmMaybeReferencingInternal = true;
450
451 // Compute this once per indirect call.
452 uint32_t NumCandidates = 0;
453 uint64_t TotalCount = 0;
454 MutableArrayRef<InstrProfValueData> CandidateProfileData;
455
456 auto *CalledValue = CB->getCalledOperand();
457 auto *CalledFunction = CB->getCalledFunction();
458 if (CalledValue && !CalledFunction) {
459 CalledValue = CalledValue->stripPointerCasts();
460 // Stripping pointer casts can reveal a called function.
461 CalledFunction = dyn_cast<Function>(CalledValue);
462 }
463 // Check if this is an alias to a function. If so, get the
464 // called aliasee for the checks below.
465 if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
466 assert(!CalledFunction && "Expected null called function in callsite for alias");
467 CalledFunction = dyn_cast<Function>(GA->getAliaseeObject());
468 }
469 // Check if this is a direct call to a known function or a known
470 // intrinsic, or an indirect call with profile data.
471 if (CalledFunction) {
472 if (CI && CalledFunction->isIntrinsic()) {
474 CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls,
475 TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls, DT);
476 continue;
477 }
478 // We should have named any anonymous globals
479 assert(CalledFunction->hasName());
480 auto ScaledCount = PSI->getProfileCount(*CB, BFI);
481 auto Hotness = ScaledCount ? getHotness(*ScaledCount, PSI)
485
486 // Use the original CalledValue, in case it was an alias. We want
487 // to record the call edge to the alias in that case. Eventually
488 // an alias summary will be created to associate the alias and
489 // aliasee.
490 auto &ValueInfo = CallGraphEdges[Index.getOrInsertValueInfo(
491 cast<GlobalValue>(CalledValue))];
492 ValueInfo.updateHotness(Hotness);
493 if (CB->isTailCall())
494 ValueInfo.setHasTailCall(true);
495 } else {
496 HasUnknownCall = true;
497 // If F is imported, a local linkage ifunc (e.g. target_clones on a
498 // static function) called by F will be cloned. Since summaries don't
499 // track ifunc, we do not know implementation functions referenced by
500 // the ifunc resolver need to be promoted in the exporter, and we will
501 // get linker errors due to cloned declarations for implementation
502 // functions. As a simple fix, just mark F as not eligible for import.
503 // Non-local ifunc is not cloned and does not have the issue.
504 if (auto *GI = dyn_cast_if_present<GlobalIFunc>(CalledValue))
505 if (GI->hasLocalLinkage())
506 HasLocalIFuncCallOrRef = true;
507 // Skip inline assembly calls.
508 if (CI && CI->isInlineAsm())
509 continue;
510 // Skip direct calls.
511 if (!CalledValue || isa<Constant>(CalledValue))
512 continue;
513
514 // Check if the instruction has a callees metadata. If so, add callees
515 // to CallGraphEdges to reflect the references from the metadata, and
516 // to enable importing for subsequent indirect call promotion and
517 // inlining.
518 if (auto *MD = I.getMetadata(LLVMContext::MD_callees)) {
519 for (const auto &Op : MD->operands()) {
521 if (Callee)
522 CallGraphEdges[Index.getOrInsertValueInfo(Callee)];
523 }
524 }
525
526 CandidateProfileData =
528 &I, TotalCount, NumCandidates, MaxSummaryIndirectEdges);
529 for (const auto &Candidate : CandidateProfileData)
530 CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)]
531 .updateHotness(getHotness(Candidate.Count, PSI));
532 }
533
534 // Summarize memprof related metadata. This is only needed for ThinLTO.
535 if (!IsThinLTO)
536 continue;
537
538 // Skip indirect calls if we haven't enabled memprof ICP.
539 if (!CalledFunction && !EnableMemProfIndirectCallSupport)
540 continue;
541
542 // Ensure we keep this analysis in sync with the handling in the ThinLTO
543 // backend (see MemProfContextDisambiguation::applyImport). Save this call
544 // so that we can skip it in checking the reverse case later.
546#ifndef NDEBUG
547 CallsThatMayHaveMemprofSummary.insert(CB);
548#endif
549
550 // Compute the list of stack ids first (so we can trim them from the stack
551 // ids on any MIBs).
553 I.getMetadata(LLVMContext::MD_callsite));
554 auto *MemProfMD = I.getMetadata(LLVMContext::MD_memprof);
555 if (MemProfMD) {
556 std::vector<MIBInfo> MIBs;
557 std::vector<std::vector<ContextTotalSize>> ContextSizeInfos;
558 bool HasNonZeroContextSizeInfos = false;
559 for (auto &MDOp : MemProfMD->operands()) {
560 auto *MIBMD = cast<const MDNode>(MDOp);
563 SmallVector<unsigned> StackIdIndices;
565 // Collapse out any on the allocation call (inlining).
566 for (auto ContextIter =
567 StackContext.beginAfterSharedPrefix(InstCallsite);
568 ContextIter != StackContext.end(); ++ContextIter) {
569 unsigned StackIdIdx = Index.addOrGetStackIdIndex(*ContextIter);
570 // If this is a direct recursion, simply skip the duplicate
571 // entries. If this is mutual recursion, handling is left to
572 // the LTO link analysis client.
573 if (StackIdIndices.empty() || StackIdIndices.back() != StackIdIdx)
574 StackIdIndices.push_back(StackIdIdx);
575 }
576 // If we have context size information, collect it for inclusion in
577 // the summary.
578 assert(MIBMD->getNumOperands() > 2 ||
580 if (MIBMD->getNumOperands() > 2) {
581 std::vector<ContextTotalSize> ContextSizes;
582 for (unsigned I = 2; I < MIBMD->getNumOperands(); I++) {
583 MDNode *ContextSizePair = dyn_cast<MDNode>(MIBMD->getOperand(I));
584 assert(ContextSizePair->getNumOperands() == 2);
586 ContextSizePair->getOperand(0))
587 ->getZExtValue();
589 ContextSizePair->getOperand(1))
590 ->getZExtValue();
591 ContextSizes.push_back({FullStackId, TS});
592 }
593 // Flag that we need to keep the ContextSizeInfos array for this
594 // alloc as it now contains non-zero context info sizes.
595 HasNonZeroContextSizeInfos = true;
596 ContextSizeInfos.push_back(std::move(ContextSizes));
597 } else {
598 // The ContextSizeInfos must be in the same relative position as the
599 // associated MIB. In some cases we only include a ContextSizeInfo
600 // for a subset of MIBs in an allocation. To handle that, eagerly
601 // fill any MIB entries that don't have context size info metadata
602 // with a pair of 0s. Later on we will only use this array if it
603 // ends up containing any non-zero entries (see where we set
604 // HasNonZeroContextSizeInfos above).
605 ContextSizeInfos.push_back({{0, 0}});
606 }
607 MIBs.push_back(
608 MIBInfo(getMIBAllocType(MIBMD), std::move(StackIdIndices)));
609 }
610 Allocs.push_back(AllocInfo(std::move(MIBs)));
611 assert(HasNonZeroContextSizeInfos ||
613 // We eagerly build the ContextSizeInfos array, but it will be filled
614 // with sub arrays of pairs of 0s if no MIBs on this alloc actually
615 // contained context size info metadata. Only save it if any MIBs had
616 // any such metadata.
617 if (HasNonZeroContextSizeInfos) {
618 assert(Allocs.back().MIBs.size() == ContextSizeInfos.size());
619 Allocs.back().ContextSizeInfos = std::move(ContextSizeInfos);
620 }
621 } else if (!InstCallsite.empty()) {
622 SmallVector<unsigned> StackIdIndices;
623 for (auto StackId : InstCallsite)
624 StackIdIndices.push_back(Index.addOrGetStackIdIndex(StackId));
625 if (CalledFunction) {
626 // Use the original CalledValue, in case it was an alias. We want
627 // to record the call edge to the alias in that case. Eventually
628 // an alias summary will be created to associate the alias and
629 // aliasee.
630 auto CalleeValueInfo =
631 Index.getOrInsertValueInfo(cast<GlobalValue>(CalledValue));
632 Callsites.push_back({CalleeValueInfo, StackIdIndices});
633 } else {
635 // For indirect callsites, create multiple Callsites, one per target.
636 // This enables having a different set of clone versions per target,
637 // and we will apply the cloning decisions while speculatively
638 // devirtualizing in the ThinLTO backends.
639 for (const auto &Candidate : CandidateProfileData) {
640 auto CalleeValueInfo = Index.getOrInsertValueInfo(Candidate.Value);
641 Callsites.push_back({CalleeValueInfo, StackIdIndices});
642 }
643 }
644 }
645 }
646 }
647
649 Index.addBlockCount(F.size());
650
652 if (IsThinLTO) {
653 auto AddRefEdges =
654 [&](const std::vector<const Instruction *> &Instrs,
657 for (const auto *I : Instrs) {
658 Cache.erase(I);
659 findRefEdges(Index, I, Edges, Cache, HasLocalIFuncCallOrRef);
660 }
661 };
662
663 // By now we processed all instructions in a function, except
664 // non-volatile loads and non-volatile value stores. Let's find
665 // ref edges for both of instruction sets
666 AddRefEdges(NonVolatileLoads, LoadRefEdges, Visited);
667 // We can add some values to the Visited set when processing load
668 // instructions which are also used by stores in NonVolatileStores.
669 // For example this can happen if we have following code:
670 //
671 // store %Derived* @foo, %Derived** bitcast (%Base** @bar to %Derived**)
672 // %42 = load %Derived*, %Derived** bitcast (%Base** @bar to %Derived**)
673 //
674 // After processing loads we'll add bitcast to the Visited set, and if
675 // we use the same set while processing stores, we'll never see store
676 // to @bar and @bar will be mistakenly treated as readonly.
678 AddRefEdges(NonVolatileStores, StoreRefEdges, StoreCache);
679
680 // If both load and store instruction reference the same variable
681 // we won't be able to optimize it. Add all such reference edges
682 // to RefEdges set.
683 for (const auto &VI : StoreRefEdges)
684 if (LoadRefEdges.remove(VI))
685 RefEdges.insert(VI);
686
687 unsigned RefCnt = RefEdges.size();
688 // All new reference edges inserted in two loops below are either
689 // read or write only. They will be grouped in the end of RefEdges
690 // vector, so we can use a single integer value to identify them.
691 RefEdges.insert_range(LoadRefEdges);
692
693 unsigned FirstWORef = RefEdges.size();
694 RefEdges.insert_range(StoreRefEdges);
695
696 Refs = RefEdges.takeVector();
697 for (; RefCnt < FirstWORef; ++RefCnt)
698 Refs[RefCnt].setReadOnly();
699
700 for (; RefCnt < Refs.size(); ++RefCnt)
701 Refs[RefCnt].setWriteOnly();
702 } else {
703 Refs = RefEdges.takeVector();
704 }
705 // Explicit add hot edges to enforce importing for designated GUIDs for
706 // sample PGO, to enable the same inlines as the profiled optimized binary.
707 for (auto &I : F.getImportGUIDs())
708 CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness(
712
713#ifndef NDEBUG
714 // Make sure that all calls we decided could not have memprof summaries get a
715 // false value for mayHaveMemprofSummary, to ensure that this handling remains
716 // in sync with the ThinLTO backend handling.
717 if (IsThinLTO) {
718 for (const BasicBlock &BB : F) {
719 for (const Instruction &I : BB) {
720 const auto *CB = dyn_cast<CallBase>(&I);
721 if (!CB)
722 continue;
723 // We already checked these above.
724 if (CallsThatMayHaveMemprofSummary.count(CB))
725 continue;
727 }
728 }
729 }
730#endif
731
732 bool NonRenamableLocal = isNonRenamableLocal(F);
733 bool NotEligibleForImport =
734 NonRenamableLocal || HasInlineAsmMaybeReferencingInternal ||
735 HasIndirBranchToBlockAddress || HasLocalIFuncCallOrRef;
737 F.getLinkage(), F.getVisibility(), NotEligibleForImport,
738 /* Live = */ false, F.isDSOLocal(), F.canBeOmittedFromSymbolTable(),
740 /* NoRenameOnPromotion = */ false);
742 F.doesNotAccessMemory(), F.onlyReadsMemory() && !F.doesNotAccessMemory(),
743 F.hasFnAttribute(Attribute::NoRecurse), F.returnDoesNotAlias(),
744 // FIXME: refactor this to use the same code that inliner is using.
745 // Don't try to import functions with noinline attribute.
746 F.getAttributes().hasFnAttr(Attribute::NoInline),
747 F.hasFnAttribute(Attribute::AlwaysInline),
748 F.hasFnAttribute(Attribute::NoUnwind), MayThrow, HasUnknownCall,
750 std::vector<FunctionSummary::ParamAccess> ParamAccesses;
751 if (auto *SSI = GetSSICallback(F))
752 ParamAccesses = SSI->getParamAccesses(Index);
753 auto FuncSummary = std::make_unique<FunctionSummary>(
754 Flags, NumInsts, FunFlags, std::move(Refs), CallGraphEdges.takeVector(),
755 TypeTests.takeVector(), TypeTestAssumeVCalls.takeVector(),
756 TypeCheckedLoadVCalls.takeVector(),
757 TypeTestAssumeConstVCalls.takeVector(),
758 TypeCheckedLoadConstVCalls.takeVector(), std::move(ParamAccesses),
759 std::move(Callsites), std::move(Allocs));
760 if (NonRenamableLocal)
761 CantBePromoted.insert(F.getGUID());
762 Index.addGlobalValueSummary(F, std::move(FuncSummary));
763}
764
765/// Find function pointers referenced within the given vtable initializer
766/// (or subset of an initializer) \p I. The starting offset of \p I within
767/// the vtable initializer is \p StartingOffset. Any discovered function
768/// pointers are added to \p VTableFuncs along with their cumulative offset
769/// within the initializer.
770static void findFuncPointers(const Constant *I, uint64_t StartingOffset,
771 const Module &M, ModuleSummaryIndex &Index,
772 VTableFuncList &VTableFuncs,
773 const GlobalVariable &OrigGV) {
774 // First check if this is a function pointer.
775 if (I->getType()->isPointerTy()) {
776 auto C = I->stripPointerCasts();
777 auto A = dyn_cast<GlobalAlias>(C);
778 if (isa<Function>(C) || (A && isa<Function>(A->getAliasee()))) {
779 auto GV = dyn_cast<GlobalValue>(C);
780 assert(GV);
781 // We can disregard __cxa_pure_virtual as a possible call target, as
782 // calls to pure virtuals are UB.
783 if (GV && GV->getName() != "__cxa_pure_virtual")
784 VTableFuncs.push_back({Index.getOrInsertValueInfo(GV), StartingOffset});
785 return;
786 }
787 }
788
789 // Walk through the elements in the constant struct or array and recursively
790 // look for virtual function pointers.
791 const DataLayout &DL = M.getDataLayout();
792 if (auto *C = dyn_cast<ConstantStruct>(I)) {
793 StructType *STy = C->getType();
794 assert(STy);
795 const StructLayout *SL = DL.getStructLayout(C->getType());
796
797 for (auto EI : llvm::enumerate(STy->elements())) {
798 auto Offset = SL->getElementOffset(EI.index());
799 unsigned Op = SL->getElementContainingOffset(Offset);
800 findFuncPointers(cast<Constant>(I->getOperand(Op)),
801 StartingOffset + Offset, M, Index, VTableFuncs, OrigGV);
802 }
803 } else if (auto *C = dyn_cast<ConstantArray>(I)) {
804 ArrayType *ATy = C->getType();
805 Type *EltTy = ATy->getElementType();
806 uint64_t EltSize = DL.getTypeAllocSize(EltTy);
807 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
808 findFuncPointers(cast<Constant>(I->getOperand(i)),
809 StartingOffset + i * EltSize, M, Index, VTableFuncs,
810 OrigGV);
811 }
812 } else if (const auto *CE = dyn_cast<ConstantExpr>(I)) {
813 // For relative vtables, the next sub-component should be a trunc.
814 if (CE->getOpcode() != Instruction::Trunc ||
815 !(CE = dyn_cast<ConstantExpr>(CE->getOperand(0))))
816 return;
817
818 // If this constant can be reduced to the offset between a function and a
819 // global, then we know this is a valid virtual function if the RHS is the
820 // original vtable we're scanning through.
821 if (CE->getOpcode() == Instruction::Sub) {
823 APSInt LHSOffset, RHSOffset;
824 if (IsConstantOffsetFromGlobal(CE->getOperand(0), LHS, LHSOffset, DL) &&
825 IsConstantOffsetFromGlobal(CE->getOperand(1), RHS, RHSOffset, DL) &&
826 RHS == &OrigGV &&
827
828 // For relative vtables, this component should point to the callable
829 // function without any offsets.
830 LHSOffset == 0 &&
831
832 // Also, the RHS should always point to somewhere within the vtable.
833 RHSOffset <=
834 static_cast<uint64_t>(DL.getTypeAllocSize(OrigGV.getInitializer()->getType()))) {
835 findFuncPointers(LHS, StartingOffset, M, Index, VTableFuncs, OrigGV);
836 }
837 }
838 }
839}
840
841// Identify the function pointers referenced by vtable definition \p V.
843 const GlobalVariable &V, const Module &M,
844 VTableFuncList &VTableFuncs) {
845 if (!V.isConstant())
846 return;
847
848 findFuncPointers(V.getInitializer(), /*StartingOffset=*/0, M, Index,
849 VTableFuncs, V);
850
851#ifndef NDEBUG
852 // Validate that the VTableFuncs list is ordered by offset.
853 uint64_t PrevOffset = 0;
854 for (auto &P : VTableFuncs) {
855 // The findVFuncPointers traversal should have encountered the
856 // functions in offset order. We need to use ">=" since PrevOffset
857 // starts at 0.
858 assert(P.VTableOffset >= PrevOffset);
859 PrevOffset = P.VTableOffset;
860 }
861#endif
862}
863
864/// Record vtable definition \p V for each type metadata it references.
865static void
867 const GlobalVariable &V,
869 for (MDNode *Type : Types) {
870 auto TypeID = Type->getOperand(1).get();
871
874 cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
875 ->getZExtValue();
876
877 if (auto *TypeId = dyn_cast<MDString>(TypeID))
878 Index.getOrInsertTypeIdCompatibleVtableSummary(TypeId->getString())
879 .push_back({Offset, Index.getOrInsertValueInfo(&V)});
880 }
881}
882
884 const GlobalVariable &V,
885 DenseSet<GlobalValue::GUID> &CantBePromoted,
886 const Module &M,
890 bool RefLocalIFunc = false;
891 bool HasBlockAddress =
892 findRefEdges(Index, &V, RefEdges, Visited, RefLocalIFunc);
893 const bool NotEligibleForImport = (HasBlockAddress || RefLocalIFunc);
894 bool NonRenamableLocal = isNonRenamableLocal(V);
896 V.getLinkage(), V.getVisibility(), NonRenamableLocal,
897 /* Live = */ false, V.isDSOLocal(), V.canBeOmittedFromSymbolTable(),
898 GlobalValueSummary::Definition, /* NoRenameOnPromotion = */ false);
899
900 VTableFuncList VTableFuncs;
901 // If splitting is not enabled, then we compute the summary information
902 // necessary for index-based whole program devirtualization.
903 if (!Index.enableSplitLTOUnit()) {
904 Types.clear();
905 V.getMetadata(LLVMContext::MD_type, Types);
906 if (!Types.empty()) {
907 // Identify the function pointers referenced by this vtable definition.
908 computeVTableFuncs(Index, V, M, VTableFuncs);
909
910 // Record this vtable definition for each type metadata it references.
912 }
913 }
914
915 // Don't mark variables we won't be able to internalize as read/write-only.
916 bool CanBeInternalized =
917 !V.hasComdat() && !V.hasAppendingLinkage() && !V.isInterposable() &&
918 !V.hasAvailableExternallyLinkage() && !V.hasDLLExportStorageClass();
919 bool Constant = V.isConstant();
920 GlobalVarSummary::GVarFlags VarFlags(CanBeInternalized,
921 Constant ? false : CanBeInternalized,
922 Constant, V.getVCallVisibility());
923 auto GVarSummary = std::make_unique<GlobalVarSummary>(Flags, VarFlags,
924 RefEdges.takeVector());
925 if (NonRenamableLocal)
926 CantBePromoted.insert(V.getGUID());
927 if (NotEligibleForImport)
928 GVarSummary->setNotEligibleToImport();
929 if (!VTableFuncs.empty())
930 GVarSummary->setVTableFuncs(VTableFuncs);
931 Index.addGlobalValueSummary(V, std::move(GVarSummary));
932}
933
935 DenseSet<GlobalValue::GUID> &CantBePromoted) {
936 // Skip summary for indirect function aliases as summary for aliasee will not
937 // be emitted.
938 const GlobalObject *Aliasee = A.getAliaseeObject();
939 if (isa<GlobalIFunc>(Aliasee))
940 return;
941 bool NonRenamableLocal = isNonRenamableLocal(A);
943 A.getLinkage(), A.getVisibility(), NonRenamableLocal,
944 /* Live = */ false, A.isDSOLocal(), A.canBeOmittedFromSymbolTable(),
945 GlobalValueSummary::Definition, /* NoRenameOnPromotion = */ false);
946 auto AS = std::make_unique<AliasSummary>(Flags);
947 auto AliaseeVI = Index.getValueInfo(Aliasee->getGUID());
948 assert(AliaseeVI && "Alias expects aliasee summary to be available");
949 assert(AliaseeVI.getSummaryList().size() == 1 &&
950 "Expected a single entry per aliasee in per-module index");
951 AS->setAliasee(AliaseeVI, AliaseeVI.getSummaryList()[0].get());
952 if (NonRenamableLocal)
953 CantBePromoted.insert(A.getGUID());
954 Index.addGlobalValueSummary(A, std::move(AS));
955}
956
957// Set LiveRoot flag on entries matching the given value name.
958static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
959 if (ValueInfo VI =
960 Index.getValueInfo(GlobalValue::getGUIDAssumingExternalLinkage(Name)))
961 for (const auto &Summary : VI.getSummaryList())
962 Summary->setLive(true);
963}
964
966 const Module &M,
967 std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
969 std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) {
970 assert(PSI);
971 bool EnableSplitLTOUnit = false;
972 bool UnifiedLTO = false;
974 M.getModuleFlag("EnableSplitLTOUnit")))
975 EnableSplitLTOUnit = MD->getZExtValue();
976 if (auto *MD =
977 mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("UnifiedLTO")))
978 UnifiedLTO = MD->getZExtValue();
979 ModuleSummaryIndex Index(/*HaveGVs=*/true, EnableSplitLTOUnit, UnifiedLTO);
980
981 // Identify the local values in the llvm.used and llvm.compiler.used sets,
982 // which should not be exported as they would then require renaming and
983 // promotion, but we may have opaque uses e.g. in inline asm. We collect them
984 // here because we use this information to mark functions containing inline
985 // assembly calls as not importable.
988 // First collect those in the llvm.used set.
989 collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/false);
990 // Next collect those in the llvm.compiler.used set.
991 collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/true);
992 DenseSet<GlobalValue::GUID> CantBePromoted;
993 for (auto *V : Used) {
994 if (V->hasLocalLinkage()) {
995 LocalsUsed.insert(V);
996 CantBePromoted.insert(V->getGUID());
997 }
998 }
999
1000 bool HasLocalInlineAsmSymbol = false;
1001 if (!M.getModuleInlineAsm().empty()) {
1002 // Collect the local values defined by module level asm, and set up
1003 // summaries for these symbols so that they can be marked as NoRename,
1004 // to prevent export of any use of them in regular IR that would require
1005 // renaming within the module level asm. Note we don't need to create a
1006 // summary for weak or global defs, as they don't need to be flagged as
1007 // NoRename, and defs in module level asm can't be imported anyway.
1008 // Also, any values used but not defined within module level asm should
1009 // be listed on the llvm.used or llvm.compiler.used global and marked as
1010 // referenced from there.
1012 M, [&](StringRef Name, object::BasicSymbolRef::Flags Flags) {
1013 // Symbols not marked as Weak or Global are local definitions.
1014 if (Flags & (object::BasicSymbolRef::SF_Weak |
1016 return;
1017 HasLocalInlineAsmSymbol = true;
1018 GlobalValue *GV = M.getNamedValue(Name);
1019 if (!GV)
1020 return;
1021 assert(GV->isDeclaration() && "Def in module asm already has definition");
1024 /* NotEligibleToImport = */ true,
1025 /* Live = */ true,
1026 /* Local */ GV->isDSOLocal(), GV->canBeOmittedFromSymbolTable(),
1028 /* NoRenameOnPromotion = */ false);
1029 CantBePromoted.insert(GV->getGUID());
1030 // Create the appropriate summary type.
1031 if (Function *F = dyn_cast<Function>(GV)) {
1032 std::unique_ptr<FunctionSummary> Summary =
1033 std::make_unique<FunctionSummary>(
1034 GVFlags, /*InstCount=*/0,
1036 F->hasFnAttribute(Attribute::ReadNone),
1037 F->hasFnAttribute(Attribute::ReadOnly),
1038 F->hasFnAttribute(Attribute::NoRecurse),
1039 F->returnDoesNotAlias(),
1040 /* NoInline = */ false,
1041 F->hasFnAttribute(Attribute::AlwaysInline),
1042 F->hasFnAttribute(Attribute::NoUnwind),
1043 /* MayThrow */ true,
1044 /* HasUnknownCall */ true,
1045 /* MustBeUnreachable */ false},
1055 Index.addGlobalValueSummary(*GV, std::move(Summary));
1056 } else {
1057 std::unique_ptr<GlobalVarSummary> Summary =
1058 std::make_unique<GlobalVarSummary>(
1059 GVFlags,
1061 false, false, cast<GlobalVariable>(GV)->isConstant(),
1064 Index.addGlobalValueSummary(*GV, std::move(Summary));
1065 }
1066 });
1067 }
1068
1069 bool IsThinLTO = true;
1070 if (auto *MD =
1071 mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
1072 IsThinLTO = MD->getZExtValue();
1073
1074 // Compute summaries for all functions defined in module, and save in the
1075 // index.
1076 for (const auto &F : M) {
1077 if (F.isDeclaration())
1078 continue;
1079
1080 DominatorTree DT(const_cast<Function &>(F));
1081 BlockFrequencyInfo *BFI = nullptr;
1082 std::unique_ptr<BlockFrequencyInfo> BFIPtr;
1083 if (GetBFICallback)
1084 BFI = GetBFICallback(F);
1085 else if (F.hasProfileData()) {
1086 LoopInfo LI{DT};
1087 BranchProbabilityInfo BPI{F, LI};
1088 BFIPtr = std::make_unique<BlockFrequencyInfo>(F, BPI, LI);
1089 BFI = BFIPtr.get();
1090 }
1091
1092 computeFunctionSummary(Index, M, F, BFI, PSI, DT,
1093 !LocalsUsed.empty() || HasLocalInlineAsmSymbol,
1094 CantBePromoted, IsThinLTO, GetSSICallback);
1095 }
1096
1097 // Compute summaries for all variables defined in module, and save in the
1098 // index.
1100 for (const GlobalVariable &G : M.globals()) {
1101 if (G.isDeclaration())
1102 continue;
1103 computeVariableSummary(Index, G, CantBePromoted, M, Types);
1104 }
1105
1106 // Compute summaries for all aliases defined in module, and save in the
1107 // index.
1108 for (const GlobalAlias &A : M.aliases())
1109 computeAliasSummary(Index, A, CantBePromoted);
1110
1111 // Iterate through ifuncs, set their resolvers all alive.
1112 for (const GlobalIFunc &I : M.ifuncs()) {
1113 I.applyAlongResolverPath([&Index](const GlobalValue &GV) {
1114 Index.getGlobalValueSummary(GV)->setLive(true);
1115 });
1116 }
1117
1118 for (auto *V : LocalsUsed) {
1119 auto *Summary = Index.getGlobalValueSummary(*V);
1120 assert(Summary && "Missing summary for global value");
1121 Summary->setNotEligibleToImport();
1122 }
1123
1124 // The linker doesn't know about these LLVM produced values, so we need
1125 // to flag them as live in the index to ensure index-based dead value
1126 // analysis treats them as live roots of the analysis.
1127 setLiveRoot(Index, "llvm.used");
1128 setLiveRoot(Index, "llvm.compiler.used");
1129 setLiveRoot(Index, "llvm.global_ctors");
1130 setLiveRoot(Index, "llvm.global_dtors");
1131 setLiveRoot(Index, "llvm.global.annotations");
1132
1133 for (auto &GlobalList : Index) {
1134 // Ignore entries for references that are undefined in the current module.
1135 if (GlobalList.second.getSummaryList().empty())
1136 continue;
1137
1138 assert(GlobalList.second.getSummaryList().size() == 1 &&
1139 "Expected module's index to have one summary per GUID");
1140 auto &Summary = GlobalList.second.getSummaryList()[0];
1141 if (!IsThinLTO) {
1142 Summary->setNotEligibleToImport();
1143 continue;
1144 }
1145
1146 bool AllRefsCanBeExternallyReferenced =
1147 llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) {
1148 return !CantBePromoted.count(VI.getGUID());
1149 });
1150 if (!AllRefsCanBeExternallyReferenced) {
1151 Summary->setNotEligibleToImport();
1152 continue;
1153 }
1154
1155 if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) {
1156 bool AllCallsCanBeExternallyReferenced = llvm::all_of(
1157 FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
1158 return !CantBePromoted.count(Edge.first.getGUID());
1159 });
1160 if (!AllCallsCanBeExternallyReferenced)
1161 Summary->setNotEligibleToImport();
1162 }
1163 }
1164
1165 if (!ModuleSummaryDotFile.empty()) {
1166 std::error_code EC;
1168 if (EC)
1169 report_fatal_error(Twine("Failed to open dot file ") +
1170 ModuleSummaryDotFile + ": " + EC.message() + "\n");
1171 Index.exportToDot(OSDot, {});
1172 }
1173
1174 return Index;
1175}
1176
1177AnalysisKey ModuleSummaryIndexAnalysis::Key;
1178
1182 auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
1183 bool NeedSSI = needsParamAccessSummary(M);
1185 M,
1186 [&FAM](const Function &F) {
1187 return &FAM.getResult<BlockFrequencyAnalysis>(
1188 *const_cast<Function *>(&F));
1189 },
1190 &PSI,
1191 [&FAM, NeedSSI](const Function &F) -> const StackSafetyInfo * {
1192 return NeedSSI ? &FAM.getResult<StackSafetyAnalysis>(
1193 const_cast<Function &>(F))
1194 : nullptr;
1195 });
1196}
1197
1199
1201 "Module Summary Analysis", false, true)
1206 "Module Summary Analysis", false, true)
1207
1211
1214
1216 auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
1217 bool NeedSSI = needsParamAccessSummary(M);
1218 Index.emplace(buildModuleSummaryIndex(
1219 M,
1220 [this](const Function &F) {
1222 *const_cast<Function *>(&F))
1223 .getBFI());
1224 },
1225 PSI,
1226 [&](const Function &F) -> const StackSafetyInfo * {
1228 const_cast<Function &>(F))
1229 .getResult()
1230 : nullptr;
1231 }));
1232 return false;
1233}
1234
1236 Index.reset();
1237 return false;
1238}
1239
1246
1248
1252
1257
1262
1264 "Module summary info", false, true)
1265
1267 if (!CB)
1268 return false;
1269 if (CB->isDebugOrPseudoInst())
1270 return false;
1271 auto *CI = dyn_cast<CallInst>(CB);
1272 auto *CalledValue = CB->getCalledOperand();
1273 auto *CalledFunction = CB->getCalledFunction();
1274 if (CalledValue && !CalledFunction) {
1275 CalledValue = CalledValue->stripPointerCasts();
1276 // Stripping pointer casts can reveal a called function.
1277 CalledFunction = dyn_cast<Function>(CalledValue);
1278 }
1279 // Check if this is an alias to a function. If so, get the
1280 // called aliasee for the checks below.
1281 if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
1282 assert(!CalledFunction &&
1283 "Expected null called function in callsite for alias");
1284 CalledFunction = dyn_cast<Function>(GA->getAliaseeObject());
1285 }
1286 // Check if this is a direct call to a known function or a known
1287 // intrinsic, or an indirect call with profile data.
1288 if (CalledFunction) {
1289 if (CI && CalledFunction->isIntrinsic())
1290 return false;
1291 } else {
1292 // Skip indirect calls if we haven't enabled memprof ICP.
1294 return false;
1295 // Skip inline assembly calls.
1296 if (CI && CI->isInlineAsm())
1297 return false;
1298 // Skip direct calls via Constant.
1299 if (!CalledValue || isa<Constant>(CalledValue))
1300 return false;
1301 return true;
1302 }
1303 return true;
1304}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
static bool isConstant(const MachineInstr &MI)
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
#define LLVM_ABI
Definition Compiler.h:215
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseSet and SmallDenseSet classes.
Module.h This file contains the declarations for the Module class.
This defines the Use class.
iv users
Definition IVUsers.cpp:48
Interface to identify indirect call promotion candidates.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file implements a map that provides insertion order iteration.
This file contains the declarations for metadata subclasses.
Type::TypeID TypeID
static void addVCallToSet(DevirtCallSite Call, GlobalValue::GUID Guid, SetVector< FunctionSummary::VFuncId, std::vector< FunctionSummary::VFuncId > > &VCalls, SetVector< FunctionSummary::ConstVCall, std::vector< FunctionSummary::ConstVCall > > &ConstVCalls)
Determine whether this call has all constant integer arguments (excluding "this") and summarize it to...
static void computeVTableFuncs(ModuleSummaryIndex &Index, const GlobalVariable &V, const Module &M, VTableFuncList &VTableFuncs)
static void computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A, DenseSet< GlobalValue::GUID > &CantBePromoted)
static void findFuncPointers(const Constant *I, uint64_t StartingOffset, const Module &M, ModuleSummaryIndex &Index, VTableFuncList &VTableFuncs, const GlobalVariable &OrigGV)
Find function pointers referenced within the given vtable initializer (or subset of an initializer) I...
static void computeVariableSummary(ModuleSummaryIndex &Index, const GlobalVariable &V, DenseSet< GlobalValue::GUID > &CantBePromoted, const Module &M, SmallVectorImpl< MDNode * > &Types)
static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name)
static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount, ProfileSummaryInfo *PSI)
static bool isNonVolatileLoad(const Instruction *I)
static void findImplicitRefEdges(ModuleSummaryIndex &Index, const Function &F, SetVector< ValueInfo, SmallVector< ValueInfo, 0 > > &RefEdges)
Collect globals referenced via !implicit.ref metadata on a function and add them as reference edges i...
static bool isNonRenamableLocal(const GlobalValue &GV)
static void computeFunctionSummary(ModuleSummaryIndex &Index, const Module &M, const Function &F, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, DominatorTree &DT, bool HasLocalsInUsedOrAsm, DenseSet< GlobalValue::GUID > &CantBePromoted, bool IsThinLTO, std::function< const StackSafetyInfo *(const Function &F)> GetSSICallback)
static bool mustBeUnreachableFunction(const Function &F)
static bool isNonVolatileStore(const Instruction *I)
static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser, SetVector< ValueInfo, SmallVector< ValueInfo, 0 > > &RefEdges, SmallPtrSet< const User *, 8 > &Visited, bool &RefLocalLinkageIFunc)
static void addIntrinsicToSummary(const CallInst *CI, SetVector< GlobalValue::GUID, std::vector< GlobalValue::GUID > > &TypeTests, SetVector< FunctionSummary::VFuncId, std::vector< FunctionSummary::VFuncId > > &TypeTestAssumeVCalls, SetVector< FunctionSummary::VFuncId, std::vector< FunctionSummary::VFuncId > > &TypeCheckedLoadVCalls, SetVector< FunctionSummary::ConstVCall, std::vector< FunctionSummary::ConstVCall > > &TypeTestAssumeConstVCalls, SetVector< FunctionSummary::ConstVCall, std::vector< FunctionSummary::ConstVCall > > &TypeCheckedLoadConstVCalls, DominatorTree &DT)
If this intrinsic call requires that we add information to the function summary, do so via the non-co...
static void recordTypeIdCompatibleVtableReferences(ModuleSummaryIndex &Index, const GlobalVariable &V, SmallVectorImpl< MDNode * > &Types)
Record vtable definition V for each type metadata it references.
This is the interface to build a ModuleSummaryIndex for a module.
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
#define P(N)
FunctionAnalysisManager FAM
if(PassOpts->AAPipeline)
#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
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
Value * RHS
Value * LHS
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
static LLVM_ABI BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
Analysis pass which computes BlockFrequencyInfo.
Legacy analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Analysis providing branch probability information.
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...
Value * getArgOperand(unsigned i) const
This class represents a function call, abstracting a target machine's calling convention.
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Implements a dense probed hash-table based set.
Definition DenseSet.h:289
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:151
std::pair< ValueInfo, CalleeInfo > EdgeTy
<CalleeValueInfo, CalleeInfo> call edge pair.
ForceSummaryHotnessType
Types for -force-summary-edges-cold debugging option.
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition Function.h:246
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:80
bool isDSOLocal() const
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
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
bool hasLocalLinkage() const
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
@ DefaultVisibility
The GV is visible.
Definition GlobalValue.h:68
bool hasSection() const
LLVM_ABI bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
Definition Globals.cpp:475
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
LLVM_ABI MutableArrayRef< InstrProfValueData > getPromotionCandidatesForInstruction(const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates, unsigned MaxNumValueData=0)
Returns reference to array of InstrProfValueData for the given instruction I.
Legacy wrapper pass to provide the ModuleSummaryIndex object.
ImmutableModuleSummaryIndexWrapperPass(const ModuleSummaryIndex *Index=nullptr)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition Pass.h:285
ImmutablePass(char &pid)
Definition Pass.h:287
Metadata node.
Definition Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1433
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1439
Tracking metadata reference owned by Metadata.
Definition Metadata.h:891
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
VectorType takeVector()
Clear the MapVector and return the underlying vector.
Definition MapVector.h:50
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
ModulePass(char &pid)
Definition Pass.h:257
LLVM_ABI Result run(Module &M, ModuleAnalysisManager &AM)
Legacy wrapper pass to provide the ModuleSummaryIndex object.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
Class to hold module path string table and global value map, and encapsulate methods for operating on...
static LLVM_ABI void CollectAsmSymbols(const Module &M, function_ref< void(StringRef, object::BasicSymbolRef::Flags)> AsmSymbol)
Parse inline ASM and collect the symbols that are defined or referenced in the current module.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
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.
LLVM_ABI std::optional< uint64_t > getProfileCount(const CallBase &CallInst, BlockFrequencyInfo *BFI, bool AllowSynthetic=false) const
Returns the profile count for CallInst.
LLVM_ABI bool isColdCount(uint64_t C) const
Returns true if count C is considered cold.
LLVM_ABI bool hasPartialSampleProfile() const
Returns true if module M has partial-profile sample profile.
LLVM_ABI bool isHotCount(uint64_t C) const
Returns true if count C is considered hot.
A vector that has set insertion semantics.
Definition SetVector.h:57
bool remove(const value_type &X)
Remove an item from the set vector.
Definition SetVector.h:181
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
void insert_range(Range &&R)
Definition SetVector.h:176
Vector takeVector()
Clear the SetVector and return the underlying vector.
Definition SetVector.h:94
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
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.
StackSafetyInfo wrapper for the new pass manager.
StackSafetyInfo wrapper for the legacy pass manager.
Interface to access stack safety analysis results for single function.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:743
LLVM_ABI unsigned getElementContainingOffset(uint64_t FixedOffset) const
Given a valid byte offset into the structure, returns the structure index that contains it.
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:774
Class to represent struct types.
ArrayRef< Type * > elements() const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
iterator_range< use_iterator > uses()
Definition Value.h:380
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:212
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:190
Helper class to iterate through stack ids in both metadata (memprof MIB and callsite) and the corresp...
CallStackIterator beginAfterSharedPrefix(const CallStack &Other)
CallStackIterator end() const
A raw_ostream that writes to a file descriptor.
CallInst * Call
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
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)
LocationClass< Ty > location(Ty &L)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
Definition Metadata.h:683
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
Definition Metadata.h:696
LLVM_ABI bool metadataIncludesAllContextSizeInfo()
Whether the alloc memeprof metadata will include context size info for all MIBs.
LLVM_ABI AllocationType getMIBAllocType(const MDNode *MIB)
Returns the allocation type from an MIB metadata node.
LLVM_ABI MDNode * getMIBStackNode(const MDNode *MIB)
Returns the stack node from an MIB metadata node.
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
Definition FileSystem.h:795
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
@ Offset
Definition DWP.cpp:558
cl::opt< bool > MemProfReportHintedSizes("memprof-report-hinted-sizes", cl::init(false), cl::Hidden, cl::desc("Report total allocation sizes of hinted allocations"))
std::vector< VirtFuncOffset > VTableFuncList
List of functions referenced by a particular vtable definition.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1738
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2553
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool mayHaveMemprofSummary(const CallBase *CB)
Returns true if the instruction could have memprof metadata, used to ensure consistency between summa...
LLVM_ABI bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL, DSOLocalEquivalent **DSOEquiv=nullptr)
If this constant is a constant offset from a global, return the global and the constant.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold
LLVM_ABI bool needsParamAccessSummary(const Module &M)
static cl::opt< std::string > ModuleSummaryDotFile("module-summary-dot-file", cl::Hidden, cl::value_desc("filename"), cl::desc("File to emit dot graph of new summary into"))
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
LLVM_ABI ModuleSummaryIndex buildModuleSummaryIndex(const Module &M, std::function< BlockFrequencyInfo *(const Function &F)> GetBFICallback, ProfileSummaryInfo *PSI, std::function< const StackSafetyInfo *(const Function &F)> GetSSICallback=[](const Function &F) -> const StackSafetyInfo *{ return nullptr;})
Direct function to compute a ModuleSummaryIndex from a given module.
cl::opt< unsigned > MaxNumVTableAnnotations("icp-max-num-vtables", cl::init(6), cl::Hidden, cl::desc("Max number of vtables annotated for a vtable load instruction."))
LLVM_ABI cl::opt< bool > ScalePartialSampleProfileWorkingSetSize
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
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:1745
cl::opt< unsigned > MaxSummaryIndirectEdges("module-summary-max-indirect-edges", cl::init(0), cl::Hidden, cl::desc("Max number of summary edges added from " "indirect call profile metadata"))
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
LLVM_ABI void findDevirtualizableCallsForTypeCheckedLoad(SmallVectorImpl< DevirtCallSite > &DevirtCalls, SmallVectorImpl< Instruction * > &LoadedPtrs, SmallVectorImpl< Instruction * > &Preds, bool &HasNonCallUses, const CallInst *CI, DominatorTree &DT)
Given a call to the intrinsic @llvm.type.checked.load, find all devirtualizable call sites based on t...
Function::ProfileCount ProfileCount
LLVM_ABI SmallVector< InstrProfValueData, 4 > getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind, uint32_t MaxNumValueData, uint64_t &TotalC, bool GetNoICPValue=false)
Extract the value profile data from Inst and returns them if Inst is annotated with value profile dat...
LLVM_ABI ModulePass * createModuleSummaryIndexWrapperPass()
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
DWARFExpression::Operation Op
LLVM_ABI ImmutablePass * createImmutableModuleSummaryIndexWrapperPass(const ModuleSummaryIndex *Index)
static cl::opt< FunctionSummary::ForceSummaryHotnessType, true > FSEC("force-summary-edges-cold", cl::Hidden, cl::location(ForceSummaryEdgesCold), cl::desc("Force all edges in the function summary to cold"), cl::values(clEnumValN(FunctionSummary::FSHT_None, "none", "None."), clEnumValN(FunctionSummary::FSHT_AllNonCritical, "all-non-critical", "All non-critical edges."), clEnumValN(FunctionSummary::FSHT_All, "all", "All edges.")))
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
static cl::opt< bool > EnableMemProfIndirectCallSupport("enable-memprof-indirect-call-support", cl::init(true), cl::Hidden, cl::desc("Enable MemProf support for summarizing and cloning indirect calls"))
LLVM_ABI void findDevirtualizableCallsForTypeTest(SmallVectorImpl< DevirtCallSite > &DevirtCalls, SmallVectorImpl< CallInst * > &Assumes, const CallInst *CI, DominatorTree &DT)
Given a call to the intrinsic @llvm.type.test, find all devirtualizable call sites based on the call ...
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
LLVM_ABI GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition Module.cpp:898
Summary of memprof metadata on allocations.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
A call site that could be devirtualized.
A specification for a virtual function call with all constant integer arguments.
Flags specific to function summaries.
An "identifier" for a virtual function.
Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
Summary of a single MIB in a memprof metadata on allocations.
Struct that holds a reference to a particular GUID in a global value summary.