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