1 //===- ModuleSummaryAnalysis.cpp - Module summary index builder -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass builds a ModuleSummaryIndex object for the module, to be written
11 // to bitcode or LLVM assembly.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/MapVector.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Analysis/BlockFrequencyInfo.h"
25 #include "llvm/Analysis/BranchProbabilityInfo.h"
26 #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
27 #include "llvm/Analysis/LoopInfo.h"
28 #include "llvm/Analysis/ProfileSummaryInfo.h"
29 #include "llvm/Analysis/TypeMetadataUtils.h"
30 #include "llvm/IR/Attributes.h"
31 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/CallSite.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/Intrinsics.h"
43 #include "llvm/IR/Metadata.h"
44 #include "llvm/IR/Module.h"
45 #include "llvm/IR/ModuleSummaryIndex.h"
46 #include "llvm/IR/Use.h"
47 #include "llvm/IR/User.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 <algorithm>
53 #include <cassert>
54 #include <cstdint>
55 #include <vector>
56 
57 using namespace llvm;
58 
59 #define DEBUG_TYPE "module-summary-analysis"
60 
61 // Walk through the operands of a given User via worklist iteration and populate
62 // the set of GlobalValue references encountered. Invoked either on an
63 // Instruction or a GlobalVariable (which walks its initializer).
64 static void findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,
65                          SetVector<ValueInfo> &RefEdges,
66                          SmallPtrSet<const User *, 8> &Visited) {
67   SmallVector<const User *, 32> Worklist;
68   Worklist.push_back(CurUser);
69 
70   while (!Worklist.empty()) {
71     const User *U = Worklist.pop_back_val();
72 
73     if (!Visited.insert(U).second)
74       continue;
75 
76     ImmutableCallSite CS(U);
77 
78     for (const auto &OI : U->operands()) {
79       const User *Operand = dyn_cast<User>(OI);
80       if (!Operand)
81         continue;
82       if (isa<BlockAddress>(Operand))
83         continue;
84       if (auto *GV = dyn_cast<GlobalValue>(Operand)) {
85         // We have a reference to a global value. This should be added to
86         // the reference set unless it is a callee. Callees are handled
87         // specially by WriteFunction and are added to a separate list.
88         if (!(CS && CS.isCallee(&OI)))
89           RefEdges.insert(Index.getOrInsertValueInfo(GV));
90         continue;
91       }
92       Worklist.push_back(Operand);
93     }
94   }
95 }
96 
97 static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount,
98                                           ProfileSummaryInfo *PSI) {
99   if (!PSI)
100     return CalleeInfo::HotnessType::Unknown;
101   if (PSI->isHotCount(ProfileCount))
102     return CalleeInfo::HotnessType::Hot;
103   if (PSI->isColdCount(ProfileCount))
104     return CalleeInfo::HotnessType::Cold;
105   return CalleeInfo::HotnessType::None;
106 }
107 
108 static bool isNonRenamableLocal(const GlobalValue &GV) {
109   return GV.hasSection() && GV.hasLocalLinkage();
110 }
111 
112 /// Determine whether this call has all constant integer arguments (excluding
113 /// "this") and summarize it to VCalls or ConstVCalls as appropriate.
114 static void addVCallToSet(DevirtCallSite Call, GlobalValue::GUID Guid,
115                           SetVector<FunctionSummary::VFuncId> &VCalls,
116                           SetVector<FunctionSummary::ConstVCall> &ConstVCalls) {
117   std::vector<uint64_t> Args;
118   // Start from the second argument to skip the "this" pointer.
119   for (auto &Arg : make_range(Call.CS.arg_begin() + 1, Call.CS.arg_end())) {
120     auto *CI = dyn_cast<ConstantInt>(Arg);
121     if (!CI || CI->getBitWidth() > 64) {
122       VCalls.insert({Guid, Call.Offset});
123       return;
124     }
125     Args.push_back(CI->getZExtValue());
126   }
127   ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)});
128 }
129 
130 /// If this intrinsic call requires that we add information to the function
131 /// summary, do so via the non-constant reference arguments.
132 static void addIntrinsicToSummary(
133     const CallInst *CI, SetVector<GlobalValue::GUID> &TypeTests,
134     SetVector<FunctionSummary::VFuncId> &TypeTestAssumeVCalls,
135     SetVector<FunctionSummary::VFuncId> &TypeCheckedLoadVCalls,
136     SetVector<FunctionSummary::ConstVCall> &TypeTestAssumeConstVCalls,
137     SetVector<FunctionSummary::ConstVCall> &TypeCheckedLoadConstVCalls) {
138   switch (CI->getCalledFunction()->getIntrinsicID()) {
139   case Intrinsic::type_test: {
140     auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1));
141     auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
142     if (!TypeId)
143       break;
144     GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
145 
146     // Produce a summary from type.test intrinsics. We only summarize type.test
147     // intrinsics that are used other than by an llvm.assume intrinsic.
148     // Intrinsics that are assumed are relevant only to the devirtualization
149     // pass, not the type test lowering pass.
150     bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) {
151       auto *AssumeCI = dyn_cast<CallInst>(CIU.getUser());
152       if (!AssumeCI)
153         return true;
154       Function *F = AssumeCI->getCalledFunction();
155       return !F || F->getIntrinsicID() != Intrinsic::assume;
156     });
157     if (HasNonAssumeUses)
158       TypeTests.insert(Guid);
159 
160     SmallVector<DevirtCallSite, 4> DevirtCalls;
161     SmallVector<CallInst *, 4> Assumes;
162     findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI);
163     for (auto &Call : DevirtCalls)
164       addVCallToSet(Call, Guid, TypeTestAssumeVCalls,
165                     TypeTestAssumeConstVCalls);
166 
167     break;
168   }
169 
170   case Intrinsic::type_checked_load: {
171     auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2));
172     auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
173     if (!TypeId)
174       break;
175     GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
176 
177     SmallVector<DevirtCallSite, 4> DevirtCalls;
178     SmallVector<Instruction *, 4> LoadedPtrs;
179     SmallVector<Instruction *, 4> Preds;
180     bool HasNonCallUses = false;
181     findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
182                                                HasNonCallUses, CI);
183     // Any non-call uses of the result of llvm.type.checked.load will
184     // prevent us from optimizing away the llvm.type.test.
185     if (HasNonCallUses)
186       TypeTests.insert(Guid);
187     for (auto &Call : DevirtCalls)
188       addVCallToSet(Call, Guid, TypeCheckedLoadVCalls,
189                     TypeCheckedLoadConstVCalls);
190 
191     break;
192   }
193   default:
194     break;
195   }
196 }
197 
198 static void
199 computeFunctionSummary(ModuleSummaryIndex &Index, const Module &M,
200                        const Function &F, BlockFrequencyInfo *BFI,
201                        ProfileSummaryInfo *PSI, bool HasLocalsInUsedOrAsm,
202                        DenseSet<GlobalValue::GUID> &CantBePromoted) {
203   // Summary not currently supported for anonymous functions, they should
204   // have been named.
205   assert(F.hasName());
206 
207   unsigned NumInsts = 0;
208   // Map from callee ValueId to profile count. Used to accumulate profile
209   // counts for all static calls to a given callee.
210   MapVector<ValueInfo, CalleeInfo> CallGraphEdges;
211   SetVector<ValueInfo> RefEdges;
212   SetVector<GlobalValue::GUID> TypeTests;
213   SetVector<FunctionSummary::VFuncId> TypeTestAssumeVCalls,
214       TypeCheckedLoadVCalls;
215   SetVector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls,
216       TypeCheckedLoadConstVCalls;
217   ICallPromotionAnalysis ICallAnalysis;
218   SmallPtrSet<const User *, 8> Visited;
219 
220   // Add personality function, prefix data and prologue data to function's ref
221   // list.
222   findRefEdges(Index, &F, RefEdges, Visited);
223 
224   bool HasInlineAsmMaybeReferencingInternal = false;
225   for (const BasicBlock &BB : F)
226     for (const Instruction &I : BB) {
227       if (isa<DbgInfoIntrinsic>(I))
228         continue;
229       ++NumInsts;
230       findRefEdges(Index, &I, RefEdges, Visited);
231       auto CS = ImmutableCallSite(&I);
232       if (!CS)
233         continue;
234 
235       const auto *CI = dyn_cast<CallInst>(&I);
236       // Since we don't know exactly which local values are referenced in inline
237       // assembly, conservatively mark the function as possibly referencing
238       // a local value from inline assembly to ensure we don't export a
239       // reference (which would require renaming and promotion of the
240       // referenced value).
241       if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm())
242         HasInlineAsmMaybeReferencingInternal = true;
243 
244       auto *CalledValue = CS.getCalledValue();
245       auto *CalledFunction = CS.getCalledFunction();
246       if (CalledValue && !CalledFunction) {
247         CalledValue = CalledValue->stripPointerCastsNoFollowAliases();
248         // Stripping pointer casts can reveal a called function.
249         CalledFunction = dyn_cast<Function>(CalledValue);
250       }
251       // Check if this is an alias to a function. If so, get the
252       // called aliasee for the checks below.
253       if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
254         assert(!CalledFunction && "Expected null called function in callsite for alias");
255         CalledFunction = dyn_cast<Function>(GA->getBaseObject());
256       }
257       // Check if this is a direct call to a known function or a known
258       // intrinsic, or an indirect call with profile data.
259       if (CalledFunction) {
260         if (CI && CalledFunction->isIntrinsic()) {
261           addIntrinsicToSummary(
262               CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls,
263               TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls);
264           continue;
265         }
266         // We should have named any anonymous globals
267         assert(CalledFunction->hasName());
268         auto ScaledCount = PSI->getProfileCount(&I, BFI);
269         auto Hotness = ScaledCount ? getHotness(ScaledCount.getValue(), PSI)
270                                    : CalleeInfo::HotnessType::Unknown;
271 
272         // Use the original CalledValue, in case it was an alias. We want
273         // to record the call edge to the alias in that case. Eventually
274         // an alias summary will be created to associate the alias and
275         // aliasee.
276         auto &ValueInfo = CallGraphEdges[Index.getOrInsertValueInfo(
277             cast<GlobalValue>(CalledValue))];
278         ValueInfo.updateHotness(Hotness);
279         // Add the relative block frequency to CalleeInfo if there is no profile
280         // information.
281         if (BFI != nullptr && Hotness == CalleeInfo::HotnessType::Unknown) {
282           uint64_t BBFreq = BFI->getBlockFreq(&BB).getFrequency();
283           uint64_t EntryFreq = BFI->getEntryFreq();
284           ValueInfo.updateRelBlockFreq(BBFreq, EntryFreq);
285         }
286       } else {
287         // Skip inline assembly calls.
288         if (CI && CI->isInlineAsm())
289           continue;
290         // Skip direct calls.
291         if (!CalledValue || isa<Constant>(CalledValue))
292           continue;
293 
294         // Check if the instruction has a callees metadata. If so, add callees
295         // to CallGraphEdges to reflect the references from the metadata, and
296         // to enable importing for subsequent indirect call promotion and
297         // inlining.
298         if (auto *MD = I.getMetadata(LLVMContext::MD_callees)) {
299           for (auto &Op : MD->operands()) {
300             Function *Callee = mdconst::extract_or_null<Function>(Op);
301             if (Callee)
302               CallGraphEdges[Index.getOrInsertValueInfo(Callee)];
303           }
304         }
305 
306         uint32_t NumVals, NumCandidates;
307         uint64_t TotalCount;
308         auto CandidateProfileData =
309             ICallAnalysis.getPromotionCandidatesForInstruction(
310                 &I, NumVals, TotalCount, NumCandidates);
311         for (auto &Candidate : CandidateProfileData)
312           CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)]
313               .updateHotness(getHotness(Candidate.Count, PSI));
314       }
315     }
316 
317   // Explicit add hot edges to enforce importing for designated GUIDs for
318   // sample PGO, to enable the same inlines as the profiled optimized binary.
319   for (auto &I : F.getImportGUIDs())
320     CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness(
321         CalleeInfo::HotnessType::Critical);
322 
323   bool NonRenamableLocal = isNonRenamableLocal(F);
324   bool NotEligibleForImport =
325       NonRenamableLocal || HasInlineAsmMaybeReferencingInternal ||
326       // Inliner doesn't handle variadic functions.
327       // FIXME: refactor this to use the same code that inliner is using.
328       F.isVarArg() ||
329       // Don't try to import functions with noinline attribute.
330       F.getAttributes().hasFnAttribute(Attribute::NoInline);
331   GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport,
332                                     /* Live = */ false, F.isDSOLocal());
333   FunctionSummary::FFlags FunFlags{
334       F.hasFnAttribute(Attribute::ReadNone),
335       F.hasFnAttribute(Attribute::ReadOnly),
336       F.hasFnAttribute(Attribute::NoRecurse),
337       F.returnDoesNotAlias(),
338   };
339   auto FuncSummary = llvm::make_unique<FunctionSummary>(
340       Flags, NumInsts, FunFlags, RefEdges.takeVector(),
341       CallGraphEdges.takeVector(), TypeTests.takeVector(),
342       TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(),
343       TypeTestAssumeConstVCalls.takeVector(),
344       TypeCheckedLoadConstVCalls.takeVector());
345   if (NonRenamableLocal)
346     CantBePromoted.insert(F.getGUID());
347   Index.addGlobalValueSummary(F.getName(), std::move(FuncSummary));
348 }
349 
350 static void
351 computeVariableSummary(ModuleSummaryIndex &Index, const GlobalVariable &V,
352                        DenseSet<GlobalValue::GUID> &CantBePromoted) {
353   SetVector<ValueInfo> RefEdges;
354   SmallPtrSet<const User *, 8> Visited;
355   findRefEdges(Index, &V, RefEdges, Visited);
356   bool NonRenamableLocal = isNonRenamableLocal(V);
357   GlobalValueSummary::GVFlags Flags(V.getLinkage(), NonRenamableLocal,
358                                     /* Live = */ false, V.isDSOLocal());
359   auto GVarSummary =
360       llvm::make_unique<GlobalVarSummary>(Flags, RefEdges.takeVector());
361   if (NonRenamableLocal)
362     CantBePromoted.insert(V.getGUID());
363   Index.addGlobalValueSummary(V.getName(), std::move(GVarSummary));
364 }
365 
366 static void
367 computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A,
368                     DenseSet<GlobalValue::GUID> &CantBePromoted) {
369   bool NonRenamableLocal = isNonRenamableLocal(A);
370   GlobalValueSummary::GVFlags Flags(A.getLinkage(), NonRenamableLocal,
371                                     /* Live = */ false, A.isDSOLocal());
372   auto AS = llvm::make_unique<AliasSummary>(Flags);
373   auto *Aliasee = A.getBaseObject();
374   auto *AliaseeSummary = Index.getGlobalValueSummary(*Aliasee);
375   assert(AliaseeSummary && "Alias expects aliasee summary to be parsed");
376   AS->setAliasee(AliaseeSummary);
377   if (NonRenamableLocal)
378     CantBePromoted.insert(A.getGUID());
379   Index.addGlobalValueSummary(A.getName(), std::move(AS));
380 }
381 
382 // Set LiveRoot flag on entries matching the given value name.
383 static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
384   if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
385     for (auto &Summary : VI.getSummaryList())
386       Summary->setLive(true);
387 }
388 
389 ModuleSummaryIndex llvm::buildModuleSummaryIndex(
390     const Module &M,
391     std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
392     ProfileSummaryInfo *PSI) {
393   assert(PSI);
394   ModuleSummaryIndex Index(/*IsPerformingAnalysis=*/true);
395 
396   // Identify the local values in the llvm.used and llvm.compiler.used sets,
397   // which should not be exported as they would then require renaming and
398   // promotion, but we may have opaque uses e.g. in inline asm. We collect them
399   // here because we use this information to mark functions containing inline
400   // assembly calls as not importable.
401   SmallPtrSet<GlobalValue *, 8> LocalsUsed;
402   SmallPtrSet<GlobalValue *, 8> Used;
403   // First collect those in the llvm.used set.
404   collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
405   // Next collect those in the llvm.compiler.used set.
406   collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
407   DenseSet<GlobalValue::GUID> CantBePromoted;
408   for (auto *V : Used) {
409     if (V->hasLocalLinkage()) {
410       LocalsUsed.insert(V);
411       CantBePromoted.insert(V->getGUID());
412     }
413   }
414 
415   bool HasLocalInlineAsmSymbol = false;
416   if (!M.getModuleInlineAsm().empty()) {
417     // Collect the local values defined by module level asm, and set up
418     // summaries for these symbols so that they can be marked as NoRename,
419     // to prevent export of any use of them in regular IR that would require
420     // renaming within the module level asm. Note we don't need to create a
421     // summary for weak or global defs, as they don't need to be flagged as
422     // NoRename, and defs in module level asm can't be imported anyway.
423     // Also, any values used but not defined within module level asm should
424     // be listed on the llvm.used or llvm.compiler.used global and marked as
425     // referenced from there.
426     ModuleSymbolTable::CollectAsmSymbols(
427         M, [&](StringRef Name, object::BasicSymbolRef::Flags Flags) {
428           // Symbols not marked as Weak or Global are local definitions.
429           if (Flags & (object::BasicSymbolRef::SF_Weak |
430                        object::BasicSymbolRef::SF_Global))
431             return;
432           HasLocalInlineAsmSymbol = true;
433           GlobalValue *GV = M.getNamedValue(Name);
434           if (!GV)
435             return;
436           assert(GV->isDeclaration() && "Def in module asm already has definition");
437           GlobalValueSummary::GVFlags GVFlags(GlobalValue::InternalLinkage,
438                                               /* NotEligibleToImport = */ true,
439                                               /* Live = */ true,
440                                               /* Local */ GV->isDSOLocal());
441           CantBePromoted.insert(GlobalValue::getGUID(Name));
442           // Create the appropriate summary type.
443           if (Function *F = dyn_cast<Function>(GV)) {
444             std::unique_ptr<FunctionSummary> Summary =
445                 llvm::make_unique<FunctionSummary>(
446                     GVFlags, 0,
447                     FunctionSummary::FFlags{
448                         F->hasFnAttribute(Attribute::ReadNone),
449                         F->hasFnAttribute(Attribute::ReadOnly),
450                         F->hasFnAttribute(Attribute::NoRecurse),
451                         F->returnDoesNotAlias()},
452                     ArrayRef<ValueInfo>{}, ArrayRef<FunctionSummary::EdgeTy>{},
453                     ArrayRef<GlobalValue::GUID>{},
454                     ArrayRef<FunctionSummary::VFuncId>{},
455                     ArrayRef<FunctionSummary::VFuncId>{},
456                     ArrayRef<FunctionSummary::ConstVCall>{},
457                     ArrayRef<FunctionSummary::ConstVCall>{});
458             Index.addGlobalValueSummary(Name, std::move(Summary));
459           } else {
460             std::unique_ptr<GlobalVarSummary> Summary =
461                 llvm::make_unique<GlobalVarSummary>(GVFlags,
462                                                     ArrayRef<ValueInfo>{});
463             Index.addGlobalValueSummary(Name, std::move(Summary));
464           }
465         });
466   }
467 
468   // Compute summaries for all functions defined in module, and save in the
469   // index.
470   for (auto &F : M) {
471     if (F.isDeclaration())
472       continue;
473 
474     BlockFrequencyInfo *BFI = nullptr;
475     std::unique_ptr<BlockFrequencyInfo> BFIPtr;
476     if (GetBFICallback)
477       BFI = GetBFICallback(F);
478     else if (F.hasProfileData()) {
479       LoopInfo LI{DominatorTree(const_cast<Function &>(F))};
480       BranchProbabilityInfo BPI{F, LI};
481       BFIPtr = llvm::make_unique<BlockFrequencyInfo>(F, BPI, LI);
482       BFI = BFIPtr.get();
483     }
484 
485     computeFunctionSummary(Index, M, F, BFI, PSI,
486                            !LocalsUsed.empty() || HasLocalInlineAsmSymbol,
487                            CantBePromoted);
488   }
489 
490   // Compute summaries for all variables defined in module, and save in the
491   // index.
492   for (const GlobalVariable &G : M.globals()) {
493     if (G.isDeclaration())
494       continue;
495     computeVariableSummary(Index, G, CantBePromoted);
496   }
497 
498   // Compute summaries for all aliases defined in module, and save in the
499   // index.
500   for (const GlobalAlias &A : M.aliases())
501     computeAliasSummary(Index, A, CantBePromoted);
502 
503   for (auto *V : LocalsUsed) {
504     auto *Summary = Index.getGlobalValueSummary(*V);
505     assert(Summary && "Missing summary for global value");
506     Summary->setNotEligibleToImport();
507   }
508 
509   // The linker doesn't know about these LLVM produced values, so we need
510   // to flag them as live in the index to ensure index-based dead value
511   // analysis treats them as live roots of the analysis.
512   setLiveRoot(Index, "llvm.used");
513   setLiveRoot(Index, "llvm.compiler.used");
514   setLiveRoot(Index, "llvm.global_ctors");
515   setLiveRoot(Index, "llvm.global_dtors");
516   setLiveRoot(Index, "llvm.global.annotations");
517 
518   bool IsThinLTO = true;
519   if (auto *MD =
520           mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
521     IsThinLTO = MD->getZExtValue();
522 
523   for (auto &GlobalList : Index) {
524     // Ignore entries for references that are undefined in the current module.
525     if (GlobalList.second.SummaryList.empty())
526       continue;
527 
528     assert(GlobalList.second.SummaryList.size() == 1 &&
529            "Expected module's index to have one summary per GUID");
530     auto &Summary = GlobalList.second.SummaryList[0];
531     if (!IsThinLTO) {
532       Summary->setNotEligibleToImport();
533       continue;
534     }
535 
536     bool AllRefsCanBeExternallyReferenced =
537         llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) {
538           return !CantBePromoted.count(VI.getGUID());
539         });
540     if (!AllRefsCanBeExternallyReferenced) {
541       Summary->setNotEligibleToImport();
542       continue;
543     }
544 
545     if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) {
546       bool AllCallsCanBeExternallyReferenced = llvm::all_of(
547           FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
548             return !CantBePromoted.count(Edge.first.getGUID());
549           });
550       if (!AllCallsCanBeExternallyReferenced)
551         Summary->setNotEligibleToImport();
552     }
553   }
554 
555   return Index;
556 }
557 
558 AnalysisKey ModuleSummaryIndexAnalysis::Key;
559 
560 ModuleSummaryIndex
561 ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
562   ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
563   auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
564   return buildModuleSummaryIndex(
565       M,
566       [&FAM](const Function &F) {
567         return &FAM.getResult<BlockFrequencyAnalysis>(
568             *const_cast<Function *>(&F));
569       },
570       &PSI);
571 }
572 
573 char ModuleSummaryIndexWrapperPass::ID = 0;
574 
575 INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
576                       "Module Summary Analysis", false, true)
577 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
578 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
579 INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
580                     "Module Summary Analysis", false, true)
581 
582 ModulePass *llvm::createModuleSummaryIndexWrapperPass() {
583   return new ModuleSummaryIndexWrapperPass();
584 }
585 
586 ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass()
587     : ModulePass(ID) {
588   initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry());
589 }
590 
591 bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) {
592   auto &PSI = *getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
593   Index = buildModuleSummaryIndex(
594       M,
595       [this](const Function &F) {
596         return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>(
597                          *const_cast<Function *>(&F))
598                      .getBFI());
599       },
600       &PSI);
601   return false;
602 }
603 
604 bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) {
605   Index.reset();
606   return false;
607 }
608 
609 void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
610   AU.setPreservesAll();
611   AU.addRequired<BlockFrequencyInfoWrapperPass>();
612   AU.addRequired<ProfileSummaryInfoWrapperPass>();
613 }
614