xref: /llvm-project/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp (revision 98eb8abff6ad5c2e2f95fd1189b35a24f2800b32)
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 
14 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
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"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Analysis/BlockFrequencyInfo.h"
24 #include "llvm/Analysis/BranchProbabilityInfo.h"
25 #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
26 #include "llvm/Analysis/LoopInfo.h"
27 #include "llvm/Analysis/MemoryProfileInfo.h"
28 #include "llvm/Analysis/ProfileSummaryInfo.h"
29 #include "llvm/Analysis/StackSafetyAnalysis.h"
30 #include "llvm/Analysis/TypeMetadataUtils.h"
31 #include "llvm/IR/Attributes.h"
32 #include "llvm/IR/BasicBlock.h"
33 #include "llvm/IR/Constant.h"
34 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/Dominators.h"
36 #include "llvm/IR/Function.h"
37 #include "llvm/IR/GlobalAlias.h"
38 #include "llvm/IR/GlobalValue.h"
39 #include "llvm/IR/GlobalVariable.h"
40 #include "llvm/IR/Instructions.h"
41 #include "llvm/IR/IntrinsicInst.h"
42 #include "llvm/IR/Metadata.h"
43 #include "llvm/IR/Module.h"
44 #include "llvm/IR/ModuleSummaryIndex.h"
45 #include "llvm/IR/Use.h"
46 #include "llvm/IR/User.h"
47 #include "llvm/InitializePasses.h"
48 #include "llvm/Object/ModuleSymbolTable.h"
49 #include "llvm/Object/SymbolicFile.h"
50 #include "llvm/Pass.h"
51 #include "llvm/Support/Casting.h"
52 #include "llvm/Support/CommandLine.h"
53 #include "llvm/Support/FileSystem.h"
54 #include <algorithm>
55 #include <cassert>
56 #include <cstdint>
57 #include <vector>
58 
59 using namespace llvm;
60 using namespace llvm::memprof;
61 
62 #define DEBUG_TYPE "module-summary-analysis"
63 
64 // Option to force edges cold which will block importing when the
65 // -import-cold-multiplier is set to 0. Useful for debugging.
66 namespace llvm {
67 FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold =
68     FunctionSummary::FSHT_None;
69 } // namespace llvm
70 
71 static cl::opt<FunctionSummary::ForceSummaryHotnessType, true> FSEC(
72     "force-summary-edges-cold", cl::Hidden, cl::location(ForceSummaryEdgesCold),
73     cl::desc("Force all edges in the function summary to cold"),
74     cl::values(clEnumValN(FunctionSummary::FSHT_None, "none", "None."),
75                clEnumValN(FunctionSummary::FSHT_AllNonCritical,
76                           "all-non-critical", "All non-critical edges."),
77                clEnumValN(FunctionSummary::FSHT_All, "all", "All edges.")));
78 
79 static cl::opt<std::string> ModuleSummaryDotFile(
80     "module-summary-dot-file", cl::Hidden, cl::value_desc("filename"),
81     cl::desc("File to emit dot graph of new summary into"));
82 
83 extern cl::opt<bool> ScalePartialSampleProfileWorkingSetSize;
84 
85 // Walk through the operands of a given User via worklist iteration and populate
86 // the set of GlobalValue references encountered. Invoked either on an
87 // Instruction or a GlobalVariable (which walks its initializer).
88 // Return true if any of the operands contains blockaddress. This is important
89 // to know when computing summary for global var, because if global variable
90 // references basic block address we can't import it separately from function
91 // containing that basic block. For simplicity we currently don't import such
92 // global vars at all. When importing function we aren't interested if any
93 // instruction in it takes an address of any basic block, because instruction
94 // can only take an address of basic block located in the same function.
95 static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,
96                          SetVector<ValueInfo> &RefEdges,
97                          SmallPtrSet<const User *, 8> &Visited) {
98   bool HasBlockAddress = false;
99   SmallVector<const User *, 32> Worklist;
100   if (Visited.insert(CurUser).second)
101     Worklist.push_back(CurUser);
102 
103   while (!Worklist.empty()) {
104     const User *U = Worklist.pop_back_val();
105     const auto *CB = dyn_cast<CallBase>(U);
106 
107     for (const auto &OI : U->operands()) {
108       const User *Operand = dyn_cast<User>(OI);
109       if (!Operand)
110         continue;
111       if (isa<BlockAddress>(Operand)) {
112         HasBlockAddress = true;
113         continue;
114       }
115       if (auto *GV = dyn_cast<GlobalValue>(Operand)) {
116         // We have a reference to a global value. This should be added to
117         // the reference set unless it is a callee. Callees are handled
118         // specially by WriteFunction and are added to a separate list.
119         if (!(CB && CB->isCallee(&OI)))
120           RefEdges.insert(Index.getOrInsertValueInfo(GV));
121         continue;
122       }
123       if (Visited.insert(Operand).second)
124         Worklist.push_back(Operand);
125     }
126   }
127   return HasBlockAddress;
128 }
129 
130 static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount,
131                                           ProfileSummaryInfo *PSI) {
132   if (!PSI)
133     return CalleeInfo::HotnessType::Unknown;
134   if (PSI->isHotCount(ProfileCount))
135     return CalleeInfo::HotnessType::Hot;
136   if (PSI->isColdCount(ProfileCount))
137     return CalleeInfo::HotnessType::Cold;
138   return CalleeInfo::HotnessType::None;
139 }
140 
141 static bool isNonRenamableLocal(const GlobalValue &GV) {
142   return GV.hasSection() && GV.hasLocalLinkage();
143 }
144 
145 /// Determine whether this call has all constant integer arguments (excluding
146 /// "this") and summarize it to VCalls or ConstVCalls as appropriate.
147 static void addVCallToSet(DevirtCallSite Call, GlobalValue::GUID Guid,
148                           SetVector<FunctionSummary::VFuncId> &VCalls,
149                           SetVector<FunctionSummary::ConstVCall> &ConstVCalls) {
150   std::vector<uint64_t> Args;
151   // Start from the second argument to skip the "this" pointer.
152   for (auto &Arg : drop_begin(Call.CB.args())) {
153     auto *CI = dyn_cast<ConstantInt>(Arg);
154     if (!CI || CI->getBitWidth() > 64) {
155       VCalls.insert({Guid, Call.Offset});
156       return;
157     }
158     Args.push_back(CI->getZExtValue());
159   }
160   ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)});
161 }
162 
163 /// If this intrinsic call requires that we add information to the function
164 /// summary, do so via the non-constant reference arguments.
165 static void addIntrinsicToSummary(
166     const CallInst *CI, SetVector<GlobalValue::GUID> &TypeTests,
167     SetVector<FunctionSummary::VFuncId> &TypeTestAssumeVCalls,
168     SetVector<FunctionSummary::VFuncId> &TypeCheckedLoadVCalls,
169     SetVector<FunctionSummary::ConstVCall> &TypeTestAssumeConstVCalls,
170     SetVector<FunctionSummary::ConstVCall> &TypeCheckedLoadConstVCalls,
171     DominatorTree &DT) {
172   switch (CI->getCalledFunction()->getIntrinsicID()) {
173   case Intrinsic::type_test:
174   case Intrinsic::public_type_test: {
175     auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1));
176     auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
177     if (!TypeId)
178       break;
179     GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
180 
181     // Produce a summary from type.test intrinsics. We only summarize type.test
182     // intrinsics that are used other than by an llvm.assume intrinsic.
183     // Intrinsics that are assumed are relevant only to the devirtualization
184     // pass, not the type test lowering pass.
185     bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) {
186       return !isa<AssumeInst>(CIU.getUser());
187     });
188     if (HasNonAssumeUses)
189       TypeTests.insert(Guid);
190 
191     SmallVector<DevirtCallSite, 4> DevirtCalls;
192     SmallVector<CallInst *, 4> Assumes;
193     findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT);
194     for (auto &Call : DevirtCalls)
195       addVCallToSet(Call, Guid, TypeTestAssumeVCalls,
196                     TypeTestAssumeConstVCalls);
197 
198     break;
199   }
200 
201   case Intrinsic::type_checked_load_relative:
202   case Intrinsic::type_checked_load: {
203     auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2));
204     auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
205     if (!TypeId)
206       break;
207     GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
208 
209     SmallVector<DevirtCallSite, 4> DevirtCalls;
210     SmallVector<Instruction *, 4> LoadedPtrs;
211     SmallVector<Instruction *, 4> Preds;
212     bool HasNonCallUses = false;
213     findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
214                                                HasNonCallUses, CI, DT);
215     // Any non-call uses of the result of llvm.type.checked.load will
216     // prevent us from optimizing away the llvm.type.test.
217     if (HasNonCallUses)
218       TypeTests.insert(Guid);
219     for (auto &Call : DevirtCalls)
220       addVCallToSet(Call, Guid, TypeCheckedLoadVCalls,
221                     TypeCheckedLoadConstVCalls);
222 
223     break;
224   }
225   default:
226     break;
227   }
228 }
229 
230 static bool isNonVolatileLoad(const Instruction *I) {
231   if (const auto *LI = dyn_cast<LoadInst>(I))
232     return !LI->isVolatile();
233 
234   return false;
235 }
236 
237 static bool isNonVolatileStore(const Instruction *I) {
238   if (const auto *SI = dyn_cast<StoreInst>(I))
239     return !SI->isVolatile();
240 
241   return false;
242 }
243 
244 // Returns true if the function definition must be unreachable.
245 //
246 // Note if this helper function returns true, `F` is guaranteed
247 // to be unreachable; if it returns false, `F` might still
248 // be unreachable but not covered by this helper function.
249 static bool mustBeUnreachableFunction(const Function &F) {
250   // A function must be unreachable if its entry block ends with an
251   // 'unreachable'.
252   assert(!F.isDeclaration());
253   return isa<UnreachableInst>(F.getEntryBlock().getTerminator());
254 }
255 
256 static void computeFunctionSummary(
257     ModuleSummaryIndex &Index, const Module &M, const Function &F,
258     BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, DominatorTree &DT,
259     bool HasLocalsInUsedOrAsm, DenseSet<GlobalValue::GUID> &CantBePromoted,
260     bool IsThinLTO,
261     std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) {
262   // Summary not currently supported for anonymous functions, they should
263   // have been named.
264   assert(F.hasName());
265 
266   unsigned NumInsts = 0;
267   // Map from callee ValueId to profile count. Used to accumulate profile
268   // counts for all static calls to a given callee.
269   MapVector<ValueInfo, CalleeInfo> CallGraphEdges;
270   SetVector<ValueInfo> RefEdges, LoadRefEdges, StoreRefEdges;
271   SetVector<GlobalValue::GUID> TypeTests;
272   SetVector<FunctionSummary::VFuncId> TypeTestAssumeVCalls,
273       TypeCheckedLoadVCalls;
274   SetVector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls,
275       TypeCheckedLoadConstVCalls;
276   ICallPromotionAnalysis ICallAnalysis;
277   SmallPtrSet<const User *, 8> Visited;
278 
279   // Add personality function, prefix data and prologue data to function's ref
280   // list.
281   findRefEdges(Index, &F, RefEdges, Visited);
282   std::vector<const Instruction *> NonVolatileLoads;
283   std::vector<const Instruction *> NonVolatileStores;
284 
285   std::vector<CallsiteInfo> Callsites;
286   std::vector<AllocInfo> Allocs;
287 
288 #ifndef NDEBUG
289   DenseSet<const CallBase *> CallsThatMayHaveMemprofSummary;
290 #endif
291 
292   bool HasInlineAsmMaybeReferencingInternal = false;
293   bool HasIndirBranchToBlockAddress = false;
294   bool HasUnknownCall = false;
295   bool MayThrow = false;
296   for (const BasicBlock &BB : F) {
297     // We don't allow inlining of function with indirect branch to blockaddress.
298     // If the blockaddress escapes the function, e.g., via a global variable,
299     // inlining may lead to an invalid cross-function reference. So we shouldn't
300     // import such function either.
301     if (BB.hasAddressTaken()) {
302       for (User *U : BlockAddress::get(const_cast<BasicBlock *>(&BB))->users())
303         if (!isa<CallBrInst>(*U)) {
304           HasIndirBranchToBlockAddress = true;
305           break;
306         }
307     }
308 
309     for (const Instruction &I : BB) {
310       if (I.isDebugOrPseudoInst())
311         continue;
312       ++NumInsts;
313 
314       // Regular LTO module doesn't participate in ThinLTO import,
315       // so no reference from it can be read/writeonly, since this
316       // would require importing variable as local copy
317       if (IsThinLTO) {
318         if (isNonVolatileLoad(&I)) {
319           // Postpone processing of non-volatile load instructions
320           // See comments below
321           Visited.insert(&I);
322           NonVolatileLoads.push_back(&I);
323           continue;
324         } else if (isNonVolatileStore(&I)) {
325           Visited.insert(&I);
326           NonVolatileStores.push_back(&I);
327           // All references from second operand of store (destination address)
328           // can be considered write-only if they're not referenced by any
329           // non-store instruction. References from first operand of store
330           // (stored value) can't be treated either as read- or as write-only
331           // so we add them to RefEdges as we do with all other instructions
332           // except non-volatile load.
333           Value *Stored = I.getOperand(0);
334           if (auto *GV = dyn_cast<GlobalValue>(Stored))
335             // findRefEdges will try to examine GV operands, so instead
336             // of calling it we should add GV to RefEdges directly.
337             RefEdges.insert(Index.getOrInsertValueInfo(GV));
338           else if (auto *U = dyn_cast<User>(Stored))
339             findRefEdges(Index, U, RefEdges, Visited);
340           continue;
341         }
342       }
343       findRefEdges(Index, &I, RefEdges, Visited);
344       const auto *CB = dyn_cast<CallBase>(&I);
345       if (!CB) {
346         if (I.mayThrow())
347           MayThrow = true;
348         continue;
349       }
350 
351       const auto *CI = dyn_cast<CallInst>(&I);
352       // Since we don't know exactly which local values are referenced in inline
353       // assembly, conservatively mark the function as possibly referencing
354       // a local value from inline assembly to ensure we don't export a
355       // reference (which would require renaming and promotion of the
356       // referenced value).
357       if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm())
358         HasInlineAsmMaybeReferencingInternal = true;
359 
360       auto *CalledValue = CB->getCalledOperand();
361       auto *CalledFunction = CB->getCalledFunction();
362       if (CalledValue && !CalledFunction) {
363         CalledValue = CalledValue->stripPointerCasts();
364         // Stripping pointer casts can reveal a called function.
365         CalledFunction = dyn_cast<Function>(CalledValue);
366       }
367       // Check if this is an alias to a function. If so, get the
368       // called aliasee for the checks below.
369       if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
370         assert(!CalledFunction && "Expected null called function in callsite for alias");
371         CalledFunction = dyn_cast<Function>(GA->getAliaseeObject());
372       }
373       // Check if this is a direct call to a known function or a known
374       // intrinsic, or an indirect call with profile data.
375       if (CalledFunction) {
376         if (CI && CalledFunction->isIntrinsic()) {
377           addIntrinsicToSummary(
378               CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls,
379               TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls, DT);
380           continue;
381         }
382         // We should have named any anonymous globals
383         assert(CalledFunction->hasName());
384         auto ScaledCount = PSI->getProfileCount(*CB, BFI);
385         auto Hotness = ScaledCount ? getHotness(*ScaledCount, PSI)
386                                    : CalleeInfo::HotnessType::Unknown;
387         if (ForceSummaryEdgesCold != FunctionSummary::FSHT_None)
388           Hotness = CalleeInfo::HotnessType::Cold;
389 
390         // Use the original CalledValue, in case it was an alias. We want
391         // to record the call edge to the alias in that case. Eventually
392         // an alias summary will be created to associate the alias and
393         // aliasee.
394         auto &ValueInfo = CallGraphEdges[Index.getOrInsertValueInfo(
395             cast<GlobalValue>(CalledValue))];
396         ValueInfo.updateHotness(Hotness);
397         // Add the relative block frequency to CalleeInfo if there is no profile
398         // information.
399         if (BFI != nullptr && Hotness == CalleeInfo::HotnessType::Unknown) {
400           uint64_t BBFreq = BFI->getBlockFreq(&BB).getFrequency();
401           uint64_t EntryFreq = BFI->getEntryFreq();
402           ValueInfo.updateRelBlockFreq(BBFreq, EntryFreq);
403         }
404       } else {
405         HasUnknownCall = true;
406         // Skip inline assembly calls.
407         if (CI && CI->isInlineAsm())
408           continue;
409         // Skip direct calls.
410         if (!CalledValue || isa<Constant>(CalledValue))
411           continue;
412 
413         // Check if the instruction has a callees metadata. If so, add callees
414         // to CallGraphEdges to reflect the references from the metadata, and
415         // to enable importing for subsequent indirect call promotion and
416         // inlining.
417         if (auto *MD = I.getMetadata(LLVMContext::MD_callees)) {
418           for (const auto &Op : MD->operands()) {
419             Function *Callee = mdconst::extract_or_null<Function>(Op);
420             if (Callee)
421               CallGraphEdges[Index.getOrInsertValueInfo(Callee)];
422           }
423         }
424 
425         uint32_t NumVals, NumCandidates;
426         uint64_t TotalCount;
427         auto CandidateProfileData =
428             ICallAnalysis.getPromotionCandidatesForInstruction(
429                 &I, NumVals, TotalCount, NumCandidates);
430         for (const auto &Candidate : CandidateProfileData)
431           CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)]
432               .updateHotness(getHotness(Candidate.Count, PSI));
433       }
434 
435       // Summarize memprof related metadata. This is only needed for ThinLTO.
436       if (!IsThinLTO)
437         continue;
438 
439       // TODO: Skip indirect calls for now. Need to handle these better, likely
440       // by creating multiple Callsites, one per target, then speculatively
441       // devirtualize while applying clone info in the ThinLTO backends. This
442       // will also be important because we will have a different set of clone
443       // versions per target. This handling needs to match that in the ThinLTO
444       // backend so we handle things consistently for matching of callsite
445       // summaries to instructions.
446       if (!CalledFunction)
447         continue;
448 
449       // Ensure we keep this analysis in sync with the handling in the ThinLTO
450       // backend (see MemProfContextDisambiguation::applyImport). Save this call
451       // so that we can skip it in checking the reverse case later.
452       assert(mayHaveMemprofSummary(CB));
453 #ifndef NDEBUG
454       CallsThatMayHaveMemprofSummary.insert(CB);
455 #endif
456 
457       // Compute the list of stack ids first (so we can trim them from the stack
458       // ids on any MIBs).
459       CallStack<MDNode, MDNode::op_iterator> InstCallsite(
460           I.getMetadata(LLVMContext::MD_callsite));
461       auto *MemProfMD = I.getMetadata(LLVMContext::MD_memprof);
462       if (MemProfMD) {
463         std::vector<MIBInfo> MIBs;
464         for (auto &MDOp : MemProfMD->operands()) {
465           auto *MIBMD = cast<const MDNode>(MDOp);
466           MDNode *StackNode = getMIBStackNode(MIBMD);
467           assert(StackNode);
468           SmallVector<unsigned> StackIdIndices;
469           CallStack<MDNode, MDNode::op_iterator> StackContext(StackNode);
470           // Collapse out any on the allocation call (inlining).
471           for (auto ContextIter =
472                    StackContext.beginAfterSharedPrefix(InstCallsite);
473                ContextIter != StackContext.end(); ++ContextIter) {
474             unsigned StackIdIdx = Index.addOrGetStackIdIndex(*ContextIter);
475             // If this is a direct recursion, simply skip the duplicate
476             // entries. If this is mutual recursion, handling is left to
477             // the LTO link analysis client.
478             if (StackIdIndices.empty() || StackIdIndices.back() != StackIdIdx)
479               StackIdIndices.push_back(StackIdIdx);
480           }
481           MIBs.push_back(
482               MIBInfo(getMIBAllocType(MIBMD), std::move(StackIdIndices)));
483         }
484         Allocs.push_back(AllocInfo(std::move(MIBs)));
485       } else if (!InstCallsite.empty()) {
486         SmallVector<unsigned> StackIdIndices;
487         for (auto StackId : InstCallsite)
488           StackIdIndices.push_back(Index.addOrGetStackIdIndex(StackId));
489         // Use the original CalledValue, in case it was an alias. We want
490         // to record the call edge to the alias in that case. Eventually
491         // an alias summary will be created to associate the alias and
492         // aliasee.
493         auto CalleeValueInfo =
494             Index.getOrInsertValueInfo(cast<GlobalValue>(CalledValue));
495         Callsites.push_back({CalleeValueInfo, StackIdIndices});
496       }
497     }
498   }
499 
500   if (PSI->hasPartialSampleProfile() && ScalePartialSampleProfileWorkingSetSize)
501     Index.addBlockCount(F.size());
502 
503   std::vector<ValueInfo> Refs;
504   if (IsThinLTO) {
505     auto AddRefEdges = [&](const std::vector<const Instruction *> &Instrs,
506                            SetVector<ValueInfo> &Edges,
507                            SmallPtrSet<const User *, 8> &Cache) {
508       for (const auto *I : Instrs) {
509         Cache.erase(I);
510         findRefEdges(Index, I, Edges, Cache);
511       }
512     };
513 
514     // By now we processed all instructions in a function, except
515     // non-volatile loads and non-volatile value stores. Let's find
516     // ref edges for both of instruction sets
517     AddRefEdges(NonVolatileLoads, LoadRefEdges, Visited);
518     // We can add some values to the Visited set when processing load
519     // instructions which are also used by stores in NonVolatileStores.
520     // For example this can happen if we have following code:
521     //
522     // store %Derived* @foo, %Derived** bitcast (%Base** @bar to %Derived**)
523     // %42 = load %Derived*, %Derived** bitcast (%Base** @bar to %Derived**)
524     //
525     // After processing loads we'll add bitcast to the Visited set, and if
526     // we use the same set while processing stores, we'll never see store
527     // to @bar and @bar will be mistakenly treated as readonly.
528     SmallPtrSet<const llvm::User *, 8> StoreCache;
529     AddRefEdges(NonVolatileStores, StoreRefEdges, StoreCache);
530 
531     // If both load and store instruction reference the same variable
532     // we won't be able to optimize it. Add all such reference edges
533     // to RefEdges set.
534     for (const auto &VI : StoreRefEdges)
535       if (LoadRefEdges.remove(VI))
536         RefEdges.insert(VI);
537 
538     unsigned RefCnt = RefEdges.size();
539     // All new reference edges inserted in two loops below are either
540     // read or write only. They will be grouped in the end of RefEdges
541     // vector, so we can use a single integer value to identify them.
542     for (const auto &VI : LoadRefEdges)
543       RefEdges.insert(VI);
544 
545     unsigned FirstWORef = RefEdges.size();
546     for (const auto &VI : StoreRefEdges)
547       RefEdges.insert(VI);
548 
549     Refs = RefEdges.takeVector();
550     for (; RefCnt < FirstWORef; ++RefCnt)
551       Refs[RefCnt].setReadOnly();
552 
553     for (; RefCnt < Refs.size(); ++RefCnt)
554       Refs[RefCnt].setWriteOnly();
555   } else {
556     Refs = RefEdges.takeVector();
557   }
558   // Explicit add hot edges to enforce importing for designated GUIDs for
559   // sample PGO, to enable the same inlines as the profiled optimized binary.
560   for (auto &I : F.getImportGUIDs())
561     CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness(
562         ForceSummaryEdgesCold == FunctionSummary::FSHT_All
563             ? CalleeInfo::HotnessType::Cold
564             : CalleeInfo::HotnessType::Critical);
565 
566 #ifndef NDEBUG
567   // Make sure that all calls we decided could not have memprof summaries get a
568   // false value for mayHaveMemprofSummary, to ensure that this handling remains
569   // in sync with the ThinLTO backend handling.
570   if (IsThinLTO) {
571     for (const BasicBlock &BB : F) {
572       for (const Instruction &I : BB) {
573         const auto *CB = dyn_cast<CallBase>(&I);
574         if (!CB)
575           continue;
576         // We already checked these above.
577         if (CallsThatMayHaveMemprofSummary.count(CB))
578           continue;
579         assert(!mayHaveMemprofSummary(CB));
580       }
581     }
582   }
583 #endif
584 
585   bool NonRenamableLocal = isNonRenamableLocal(F);
586   bool NotEligibleForImport = NonRenamableLocal ||
587                               HasInlineAsmMaybeReferencingInternal ||
588                               HasIndirBranchToBlockAddress;
589   GlobalValueSummary::GVFlags Flags(
590       F.getLinkage(), F.getVisibility(), NotEligibleForImport,
591       /* Live = */ false, F.isDSOLocal(), F.canBeOmittedFromSymbolTable());
592   FunctionSummary::FFlags FunFlags{
593       F.doesNotAccessMemory(), F.onlyReadsMemory() && !F.doesNotAccessMemory(),
594       F.hasFnAttribute(Attribute::NoRecurse), F.returnDoesNotAlias(),
595       // FIXME: refactor this to use the same code that inliner is using.
596       // Don't try to import functions with noinline attribute.
597       F.getAttributes().hasFnAttr(Attribute::NoInline),
598       F.hasFnAttribute(Attribute::AlwaysInline),
599       F.hasFnAttribute(Attribute::NoUnwind), MayThrow, HasUnknownCall,
600       mustBeUnreachableFunction(F)};
601   std::vector<FunctionSummary::ParamAccess> ParamAccesses;
602   if (auto *SSI = GetSSICallback(F))
603     ParamAccesses = SSI->getParamAccesses(Index);
604   auto FuncSummary = std::make_unique<FunctionSummary>(
605       Flags, NumInsts, FunFlags, /*EntryCount=*/0, std::move(Refs),
606       CallGraphEdges.takeVector(), TypeTests.takeVector(),
607       TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(),
608       TypeTestAssumeConstVCalls.takeVector(),
609       TypeCheckedLoadConstVCalls.takeVector(), std::move(ParamAccesses),
610       std::move(Callsites), std::move(Allocs));
611   if (NonRenamableLocal)
612     CantBePromoted.insert(F.getGUID());
613   Index.addGlobalValueSummary(F, std::move(FuncSummary));
614 }
615 
616 /// Find function pointers referenced within the given vtable initializer
617 /// (or subset of an initializer) \p I. The starting offset of \p I within
618 /// the vtable initializer is \p StartingOffset. Any discovered function
619 /// pointers are added to \p VTableFuncs along with their cumulative offset
620 /// within the initializer.
621 static void findFuncPointers(const Constant *I, uint64_t StartingOffset,
622                              const Module &M, ModuleSummaryIndex &Index,
623                              VTableFuncList &VTableFuncs) {
624   // First check if this is a function pointer.
625   if (I->getType()->isPointerTy()) {
626     auto C = I->stripPointerCasts();
627     auto A = dyn_cast<GlobalAlias>(C);
628     if (isa<Function>(C) || (A && isa<Function>(A->getAliasee()))) {
629       auto GV = dyn_cast<GlobalValue>(C);
630       assert(GV);
631       // We can disregard __cxa_pure_virtual as a possible call target, as
632       // calls to pure virtuals are UB.
633       if (GV && GV->getName() != "__cxa_pure_virtual")
634         VTableFuncs.push_back({Index.getOrInsertValueInfo(GV), StartingOffset});
635       return;
636     }
637   }
638 
639   // Walk through the elements in the constant struct or array and recursively
640   // look for virtual function pointers.
641   const DataLayout &DL = M.getDataLayout();
642   if (auto *C = dyn_cast<ConstantStruct>(I)) {
643     StructType *STy = dyn_cast<StructType>(C->getType());
644     assert(STy);
645     const StructLayout *SL = DL.getStructLayout(C->getType());
646 
647     for (auto EI : llvm::enumerate(STy->elements())) {
648       auto Offset = SL->getElementOffset(EI.index());
649       unsigned Op = SL->getElementContainingOffset(Offset);
650       findFuncPointers(cast<Constant>(I->getOperand(Op)),
651                        StartingOffset + Offset, M, Index, VTableFuncs);
652     }
653   } else if (auto *C = dyn_cast<ConstantArray>(I)) {
654     ArrayType *ATy = C->getType();
655     Type *EltTy = ATy->getElementType();
656     uint64_t EltSize = DL.getTypeAllocSize(EltTy);
657     for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
658       findFuncPointers(cast<Constant>(I->getOperand(i)),
659                        StartingOffset + i * EltSize, M, Index, VTableFuncs);
660     }
661   }
662 }
663 
664 // Identify the function pointers referenced by vtable definition \p V.
665 static void computeVTableFuncs(ModuleSummaryIndex &Index,
666                                const GlobalVariable &V, const Module &M,
667                                VTableFuncList &VTableFuncs) {
668   if (!V.isConstant())
669     return;
670 
671   findFuncPointers(V.getInitializer(), /*StartingOffset=*/0, M, Index,
672                    VTableFuncs);
673 
674 #ifndef NDEBUG
675   // Validate that the VTableFuncs list is ordered by offset.
676   uint64_t PrevOffset = 0;
677   for (auto &P : VTableFuncs) {
678     // The findVFuncPointers traversal should have encountered the
679     // functions in offset order. We need to use ">=" since PrevOffset
680     // starts at 0.
681     assert(P.VTableOffset >= PrevOffset);
682     PrevOffset = P.VTableOffset;
683   }
684 #endif
685 }
686 
687 /// Record vtable definition \p V for each type metadata it references.
688 static void
689 recordTypeIdCompatibleVtableReferences(ModuleSummaryIndex &Index,
690                                        const GlobalVariable &V,
691                                        SmallVectorImpl<MDNode *> &Types) {
692   for (MDNode *Type : Types) {
693     auto TypeID = Type->getOperand(1).get();
694 
695     uint64_t Offset =
696         cast<ConstantInt>(
697             cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
698             ->getZExtValue();
699 
700     if (auto *TypeId = dyn_cast<MDString>(TypeID))
701       Index.getOrInsertTypeIdCompatibleVtableSummary(TypeId->getString())
702           .push_back({Offset, Index.getOrInsertValueInfo(&V)});
703   }
704 }
705 
706 static void computeVariableSummary(ModuleSummaryIndex &Index,
707                                    const GlobalVariable &V,
708                                    DenseSet<GlobalValue::GUID> &CantBePromoted,
709                                    const Module &M,
710                                    SmallVectorImpl<MDNode *> &Types) {
711   SetVector<ValueInfo> RefEdges;
712   SmallPtrSet<const User *, 8> Visited;
713   bool HasBlockAddress = findRefEdges(Index, &V, RefEdges, Visited);
714   bool NonRenamableLocal = isNonRenamableLocal(V);
715   GlobalValueSummary::GVFlags Flags(
716       V.getLinkage(), V.getVisibility(), NonRenamableLocal,
717       /* Live = */ false, V.isDSOLocal(), V.canBeOmittedFromSymbolTable());
718 
719   VTableFuncList VTableFuncs;
720   // If splitting is not enabled, then we compute the summary information
721   // necessary for index-based whole program devirtualization.
722   if (!Index.enableSplitLTOUnit()) {
723     Types.clear();
724     V.getMetadata(LLVMContext::MD_type, Types);
725     if (!Types.empty()) {
726       // Identify the function pointers referenced by this vtable definition.
727       computeVTableFuncs(Index, V, M, VTableFuncs);
728 
729       // Record this vtable definition for each type metadata it references.
730       recordTypeIdCompatibleVtableReferences(Index, V, Types);
731     }
732   }
733 
734   // Don't mark variables we won't be able to internalize as read/write-only.
735   bool CanBeInternalized =
736       !V.hasComdat() && !V.hasAppendingLinkage() && !V.isInterposable() &&
737       !V.hasAvailableExternallyLinkage() && !V.hasDLLExportStorageClass();
738   bool Constant = V.isConstant();
739   GlobalVarSummary::GVarFlags VarFlags(CanBeInternalized,
740                                        Constant ? false : CanBeInternalized,
741                                        Constant, V.getVCallVisibility());
742   auto GVarSummary = std::make_unique<GlobalVarSummary>(Flags, VarFlags,
743                                                          RefEdges.takeVector());
744   if (NonRenamableLocal)
745     CantBePromoted.insert(V.getGUID());
746   if (HasBlockAddress)
747     GVarSummary->setNotEligibleToImport();
748   if (!VTableFuncs.empty())
749     GVarSummary->setVTableFuncs(VTableFuncs);
750   Index.addGlobalValueSummary(V, std::move(GVarSummary));
751 }
752 
753 static void computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A,
754                                 DenseSet<GlobalValue::GUID> &CantBePromoted) {
755   // Skip summary for indirect function aliases as summary for aliasee will not
756   // be emitted.
757   const GlobalObject *Aliasee = A.getAliaseeObject();
758   if (isa<GlobalIFunc>(Aliasee))
759     return;
760   bool NonRenamableLocal = isNonRenamableLocal(A);
761   GlobalValueSummary::GVFlags Flags(
762       A.getLinkage(), A.getVisibility(), NonRenamableLocal,
763       /* Live = */ false, A.isDSOLocal(), A.canBeOmittedFromSymbolTable());
764   auto AS = std::make_unique<AliasSummary>(Flags);
765   auto AliaseeVI = Index.getValueInfo(Aliasee->getGUID());
766   assert(AliaseeVI && "Alias expects aliasee summary to be available");
767   assert(AliaseeVI.getSummaryList().size() == 1 &&
768          "Expected a single entry per aliasee in per-module index");
769   AS->setAliasee(AliaseeVI, AliaseeVI.getSummaryList()[0].get());
770   if (NonRenamableLocal)
771     CantBePromoted.insert(A.getGUID());
772   Index.addGlobalValueSummary(A, std::move(AS));
773 }
774 
775 // Set LiveRoot flag on entries matching the given value name.
776 static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
777   if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
778     for (const auto &Summary : VI.getSummaryList())
779       Summary->setLive(true);
780 }
781 
782 ModuleSummaryIndex llvm::buildModuleSummaryIndex(
783     const Module &M,
784     std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
785     ProfileSummaryInfo *PSI,
786     std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) {
787   assert(PSI);
788   bool EnableSplitLTOUnit = false;
789   if (auto *MD = mdconst::extract_or_null<ConstantInt>(
790           M.getModuleFlag("EnableSplitLTOUnit")))
791     EnableSplitLTOUnit = MD->getZExtValue();
792   ModuleSummaryIndex Index(/*HaveGVs=*/true, EnableSplitLTOUnit);
793 
794   // Identify the local values in the llvm.used and llvm.compiler.used sets,
795   // which should not be exported as they would then require renaming and
796   // promotion, but we may have opaque uses e.g. in inline asm. We collect them
797   // here because we use this information to mark functions containing inline
798   // assembly calls as not importable.
799   SmallPtrSet<GlobalValue *, 4> LocalsUsed;
800   SmallVector<GlobalValue *, 4> Used;
801   // First collect those in the llvm.used set.
802   collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/false);
803   // Next collect those in the llvm.compiler.used set.
804   collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/true);
805   DenseSet<GlobalValue::GUID> CantBePromoted;
806   for (auto *V : Used) {
807     if (V->hasLocalLinkage()) {
808       LocalsUsed.insert(V);
809       CantBePromoted.insert(V->getGUID());
810     }
811   }
812 
813   bool HasLocalInlineAsmSymbol = false;
814   if (!M.getModuleInlineAsm().empty()) {
815     // Collect the local values defined by module level asm, and set up
816     // summaries for these symbols so that they can be marked as NoRename,
817     // to prevent export of any use of them in regular IR that would require
818     // renaming within the module level asm. Note we don't need to create a
819     // summary for weak or global defs, as they don't need to be flagged as
820     // NoRename, and defs in module level asm can't be imported anyway.
821     // Also, any values used but not defined within module level asm should
822     // be listed on the llvm.used or llvm.compiler.used global and marked as
823     // referenced from there.
824     ModuleSymbolTable::CollectAsmSymbols(
825         M, [&](StringRef Name, object::BasicSymbolRef::Flags Flags) {
826           // Symbols not marked as Weak or Global are local definitions.
827           if (Flags & (object::BasicSymbolRef::SF_Weak |
828                        object::BasicSymbolRef::SF_Global))
829             return;
830           HasLocalInlineAsmSymbol = true;
831           GlobalValue *GV = M.getNamedValue(Name);
832           if (!GV)
833             return;
834           assert(GV->isDeclaration() && "Def in module asm already has definition");
835           GlobalValueSummary::GVFlags GVFlags(
836               GlobalValue::InternalLinkage, GlobalValue::DefaultVisibility,
837               /* NotEligibleToImport = */ true,
838               /* Live = */ true,
839               /* Local */ GV->isDSOLocal(), GV->canBeOmittedFromSymbolTable());
840           CantBePromoted.insert(GV->getGUID());
841           // Create the appropriate summary type.
842           if (Function *F = dyn_cast<Function>(GV)) {
843             std::unique_ptr<FunctionSummary> Summary =
844                 std::make_unique<FunctionSummary>(
845                     GVFlags, /*InstCount=*/0,
846                     FunctionSummary::FFlags{
847                         F->hasFnAttribute(Attribute::ReadNone),
848                         F->hasFnAttribute(Attribute::ReadOnly),
849                         F->hasFnAttribute(Attribute::NoRecurse),
850                         F->returnDoesNotAlias(),
851                         /* NoInline = */ false,
852                         F->hasFnAttribute(Attribute::AlwaysInline),
853                         F->hasFnAttribute(Attribute::NoUnwind),
854                         /* MayThrow */ true,
855                         /* HasUnknownCall */ true,
856                         /* MustBeUnreachable */ false},
857                     /*EntryCount=*/0, ArrayRef<ValueInfo>{},
858                     ArrayRef<FunctionSummary::EdgeTy>{},
859                     ArrayRef<GlobalValue::GUID>{},
860                     ArrayRef<FunctionSummary::VFuncId>{},
861                     ArrayRef<FunctionSummary::VFuncId>{},
862                     ArrayRef<FunctionSummary::ConstVCall>{},
863                     ArrayRef<FunctionSummary::ConstVCall>{},
864                     ArrayRef<FunctionSummary::ParamAccess>{},
865                     ArrayRef<CallsiteInfo>{}, ArrayRef<AllocInfo>{});
866             Index.addGlobalValueSummary(*GV, std::move(Summary));
867           } else {
868             std::unique_ptr<GlobalVarSummary> Summary =
869                 std::make_unique<GlobalVarSummary>(
870                     GVFlags,
871                     GlobalVarSummary::GVarFlags(
872                         false, false, cast<GlobalVariable>(GV)->isConstant(),
873                         GlobalObject::VCallVisibilityPublic),
874                     ArrayRef<ValueInfo>{});
875             Index.addGlobalValueSummary(*GV, std::move(Summary));
876           }
877         });
878   }
879 
880   bool IsThinLTO = true;
881   if (auto *MD =
882           mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
883     IsThinLTO = MD->getZExtValue();
884 
885   // Compute summaries for all functions defined in module, and save in the
886   // index.
887   for (const auto &F : M) {
888     if (F.isDeclaration())
889       continue;
890 
891     DominatorTree DT(const_cast<Function &>(F));
892     BlockFrequencyInfo *BFI = nullptr;
893     std::unique_ptr<BlockFrequencyInfo> BFIPtr;
894     if (GetBFICallback)
895       BFI = GetBFICallback(F);
896     else if (F.hasProfileData()) {
897       LoopInfo LI{DT};
898       BranchProbabilityInfo BPI{F, LI};
899       BFIPtr = std::make_unique<BlockFrequencyInfo>(F, BPI, LI);
900       BFI = BFIPtr.get();
901     }
902 
903     computeFunctionSummary(Index, M, F, BFI, PSI, DT,
904                            !LocalsUsed.empty() || HasLocalInlineAsmSymbol,
905                            CantBePromoted, IsThinLTO, GetSSICallback);
906   }
907 
908   // Compute summaries for all variables defined in module, and save in the
909   // index.
910   SmallVector<MDNode *, 2> Types;
911   for (const GlobalVariable &G : M.globals()) {
912     if (G.isDeclaration())
913       continue;
914     computeVariableSummary(Index, G, CantBePromoted, M, Types);
915   }
916 
917   // Compute summaries for all aliases defined in module, and save in the
918   // index.
919   for (const GlobalAlias &A : M.aliases())
920     computeAliasSummary(Index, A, CantBePromoted);
921 
922   // Iterate through ifuncs, set their resolvers all alive.
923   for (const GlobalIFunc &I : M.ifuncs()) {
924     I.applyAlongResolverPath([&Index](const GlobalValue &GV) {
925       Index.getGlobalValueSummary(GV)->setLive(true);
926     });
927   }
928 
929   for (auto *V : LocalsUsed) {
930     auto *Summary = Index.getGlobalValueSummary(*V);
931     assert(Summary && "Missing summary for global value");
932     Summary->setNotEligibleToImport();
933   }
934 
935   // The linker doesn't know about these LLVM produced values, so we need
936   // to flag them as live in the index to ensure index-based dead value
937   // analysis treats them as live roots of the analysis.
938   setLiveRoot(Index, "llvm.used");
939   setLiveRoot(Index, "llvm.compiler.used");
940   setLiveRoot(Index, "llvm.global_ctors");
941   setLiveRoot(Index, "llvm.global_dtors");
942   setLiveRoot(Index, "llvm.global.annotations");
943 
944   for (auto &GlobalList : Index) {
945     // Ignore entries for references that are undefined in the current module.
946     if (GlobalList.second.SummaryList.empty())
947       continue;
948 
949     assert(GlobalList.second.SummaryList.size() == 1 &&
950            "Expected module's index to have one summary per GUID");
951     auto &Summary = GlobalList.second.SummaryList[0];
952     if (!IsThinLTO) {
953       Summary->setNotEligibleToImport();
954       continue;
955     }
956 
957     bool AllRefsCanBeExternallyReferenced =
958         llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) {
959           return !CantBePromoted.count(VI.getGUID());
960         });
961     if (!AllRefsCanBeExternallyReferenced) {
962       Summary->setNotEligibleToImport();
963       continue;
964     }
965 
966     if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) {
967       bool AllCallsCanBeExternallyReferenced = llvm::all_of(
968           FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
969             return !CantBePromoted.count(Edge.first.getGUID());
970           });
971       if (!AllCallsCanBeExternallyReferenced)
972         Summary->setNotEligibleToImport();
973     }
974   }
975 
976   if (!ModuleSummaryDotFile.empty()) {
977     std::error_code EC;
978     raw_fd_ostream OSDot(ModuleSummaryDotFile, EC, sys::fs::OpenFlags::OF_None);
979     if (EC)
980       report_fatal_error(Twine("Failed to open dot file ") +
981                          ModuleSummaryDotFile + ": " + EC.message() + "\n");
982     Index.exportToDot(OSDot, {});
983   }
984 
985   return Index;
986 }
987 
988 AnalysisKey ModuleSummaryIndexAnalysis::Key;
989 
990 ModuleSummaryIndex
991 ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
992   ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
993   auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
994   bool NeedSSI = needsParamAccessSummary(M);
995   return buildModuleSummaryIndex(
996       M,
997       [&FAM](const Function &F) {
998         return &FAM.getResult<BlockFrequencyAnalysis>(
999             *const_cast<Function *>(&F));
1000       },
1001       &PSI,
1002       [&FAM, NeedSSI](const Function &F) -> const StackSafetyInfo * {
1003         return NeedSSI ? &FAM.getResult<StackSafetyAnalysis>(
1004                              const_cast<Function &>(F))
1005                        : nullptr;
1006       });
1007 }
1008 
1009 char ModuleSummaryIndexWrapperPass::ID = 0;
1010 
1011 INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
1012                       "Module Summary Analysis", false, true)
1013 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
1014 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
1015 INITIALIZE_PASS_DEPENDENCY(StackSafetyInfoWrapperPass)
1016 INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
1017                     "Module Summary Analysis", false, true)
1018 
1019 ModulePass *llvm::createModuleSummaryIndexWrapperPass() {
1020   return new ModuleSummaryIndexWrapperPass();
1021 }
1022 
1023 ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass()
1024     : ModulePass(ID) {
1025   initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry());
1026 }
1027 
1028 bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) {
1029   auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
1030   bool NeedSSI = needsParamAccessSummary(M);
1031   Index.emplace(buildModuleSummaryIndex(
1032       M,
1033       [this](const Function &F) {
1034         return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>(
1035                          *const_cast<Function *>(&F))
1036                      .getBFI());
1037       },
1038       PSI,
1039       [&](const Function &F) -> const StackSafetyInfo * {
1040         return NeedSSI ? &getAnalysis<StackSafetyInfoWrapperPass>(
1041                               const_cast<Function &>(F))
1042                               .getResult()
1043                        : nullptr;
1044       }));
1045   return false;
1046 }
1047 
1048 bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) {
1049   Index.reset();
1050   return false;
1051 }
1052 
1053 void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1054   AU.setPreservesAll();
1055   AU.addRequired<BlockFrequencyInfoWrapperPass>();
1056   AU.addRequired<ProfileSummaryInfoWrapperPass>();
1057   AU.addRequired<StackSafetyInfoWrapperPass>();
1058 }
1059 
1060 char ImmutableModuleSummaryIndexWrapperPass::ID = 0;
1061 
1062 ImmutableModuleSummaryIndexWrapperPass::ImmutableModuleSummaryIndexWrapperPass(
1063     const ModuleSummaryIndex *Index)
1064     : ImmutablePass(ID), Index(Index) {
1065   initializeImmutableModuleSummaryIndexWrapperPassPass(
1066       *PassRegistry::getPassRegistry());
1067 }
1068 
1069 void ImmutableModuleSummaryIndexWrapperPass::getAnalysisUsage(
1070     AnalysisUsage &AU) const {
1071   AU.setPreservesAll();
1072 }
1073 
1074 ImmutablePass *llvm::createImmutableModuleSummaryIndexWrapperPass(
1075     const ModuleSummaryIndex *Index) {
1076   return new ImmutableModuleSummaryIndexWrapperPass(Index);
1077 }
1078 
1079 INITIALIZE_PASS(ImmutableModuleSummaryIndexWrapperPass, "module-summary-info",
1080                 "Module summary info", false, true)
1081 
1082 bool llvm::mayHaveMemprofSummary(const CallBase *CB) {
1083   if (!CB)
1084     return false;
1085   if (CB->isDebugOrPseudoInst())
1086     return false;
1087   auto *CI = dyn_cast<CallInst>(CB);
1088   auto *CalledValue = CB->getCalledOperand();
1089   auto *CalledFunction = CB->getCalledFunction();
1090   if (CalledValue && !CalledFunction) {
1091     CalledValue = CalledValue->stripPointerCasts();
1092     // Stripping pointer casts can reveal a called function.
1093     CalledFunction = dyn_cast<Function>(CalledValue);
1094   }
1095   // Check if this is an alias to a function. If so, get the
1096   // called aliasee for the checks below.
1097   if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
1098     assert(!CalledFunction &&
1099            "Expected null called function in callsite for alias");
1100     CalledFunction = dyn_cast<Function>(GA->getAliaseeObject());
1101   }
1102   // Check if this is a direct call to a known function or a known
1103   // intrinsic, or an indirect call with profile data.
1104   if (CalledFunction) {
1105     if (CI && CalledFunction->isIntrinsic())
1106       return false;
1107   } else {
1108     // TODO: For now skip indirect calls. See comments in
1109     // computeFunctionSummary for what is needed to handle this.
1110     return false;
1111   }
1112   return true;
1113 }
1114