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