xref: /src/contrib/llvm-project/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
101095a5dSDimitry Andric //===- ModuleSummaryAnalysis.cpp - Module summary index builder -----------===//
201095a5dSDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
601095a5dSDimitry Andric //
701095a5dSDimitry Andric //===----------------------------------------------------------------------===//
801095a5dSDimitry Andric //
901095a5dSDimitry Andric // This pass builds a ModuleSummaryIndex object for the module, to be written
1001095a5dSDimitry Andric // to bitcode or LLVM assembly.
1101095a5dSDimitry Andric //
1201095a5dSDimitry Andric //===----------------------------------------------------------------------===//
1301095a5dSDimitry Andric 
1401095a5dSDimitry Andric #include "llvm/Analysis/ModuleSummaryAnalysis.h"
15044eb2f6SDimitry Andric #include "llvm/ADT/ArrayRef.h"
16044eb2f6SDimitry Andric #include "llvm/ADT/DenseSet.h"
17b915e9e0SDimitry Andric #include "llvm/ADT/MapVector.h"
18044eb2f6SDimitry Andric #include "llvm/ADT/STLExtras.h"
19b915e9e0SDimitry Andric #include "llvm/ADT/SetVector.h"
20044eb2f6SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
21044eb2f6SDimitry Andric #include "llvm/ADT/SmallVector.h"
22044eb2f6SDimitry Andric #include "llvm/ADT/StringRef.h"
2301095a5dSDimitry Andric #include "llvm/Analysis/BlockFrequencyInfo.h"
2401095a5dSDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h"
25ac9a064cSDimitry Andric #include "llvm/Analysis/ConstantFolding.h"
2601095a5dSDimitry Andric #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
2701095a5dSDimitry Andric #include "llvm/Analysis/LoopInfo.h"
28e3b55780SDimitry Andric #include "llvm/Analysis/MemoryProfileInfo.h"
29b915e9e0SDimitry Andric #include "llvm/Analysis/ProfileSummaryInfo.h"
30cfca06d7SDimitry Andric #include "llvm/Analysis/StackSafetyAnalysis.h"
31b915e9e0SDimitry Andric #include "llvm/Analysis/TypeMetadataUtils.h"
32044eb2f6SDimitry Andric #include "llvm/IR/Attributes.h"
33044eb2f6SDimitry Andric #include "llvm/IR/BasicBlock.h"
34044eb2f6SDimitry Andric #include "llvm/IR/Constant.h"
35044eb2f6SDimitry Andric #include "llvm/IR/Constants.h"
3601095a5dSDimitry Andric #include "llvm/IR/Dominators.h"
37044eb2f6SDimitry Andric #include "llvm/IR/Function.h"
38044eb2f6SDimitry Andric #include "llvm/IR/GlobalAlias.h"
39044eb2f6SDimitry Andric #include "llvm/IR/GlobalValue.h"
40044eb2f6SDimitry Andric #include "llvm/IR/GlobalVariable.h"
41044eb2f6SDimitry Andric #include "llvm/IR/Instructions.h"
4201095a5dSDimitry Andric #include "llvm/IR/IntrinsicInst.h"
43044eb2f6SDimitry Andric #include "llvm/IR/Metadata.h"
44044eb2f6SDimitry Andric #include "llvm/IR/Module.h"
45044eb2f6SDimitry Andric #include "llvm/IR/ModuleSummaryIndex.h"
46044eb2f6SDimitry Andric #include "llvm/IR/Use.h"
47044eb2f6SDimitry Andric #include "llvm/IR/User.h"
48706b4fc4SDimitry Andric #include "llvm/InitializePasses.h"
4971d5a254SDimitry Andric #include "llvm/Object/ModuleSymbolTable.h"
50044eb2f6SDimitry Andric #include "llvm/Object/SymbolicFile.h"
5101095a5dSDimitry Andric #include "llvm/Pass.h"
52044eb2f6SDimitry Andric #include "llvm/Support/Casting.h"
53eb11fae6SDimitry Andric #include "llvm/Support/CommandLine.h"
54344a3780SDimitry Andric #include "llvm/Support/FileSystem.h"
55044eb2f6SDimitry Andric #include <algorithm>
56044eb2f6SDimitry Andric #include <cassert>
57044eb2f6SDimitry Andric #include <cstdint>
58044eb2f6SDimitry Andric #include <vector>
59044eb2f6SDimitry Andric 
6001095a5dSDimitry Andric using namespace llvm;
61e3b55780SDimitry Andric using namespace llvm::memprof;
6201095a5dSDimitry Andric 
6301095a5dSDimitry Andric #define DEBUG_TYPE "module-summary-analysis"
6401095a5dSDimitry Andric 
65eb11fae6SDimitry Andric // Option to force edges cold which will block importing when the
66eb11fae6SDimitry Andric // -import-cold-multiplier is set to 0. Useful for debugging.
67e3b55780SDimitry Andric namespace llvm {
68eb11fae6SDimitry Andric FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold =
69eb11fae6SDimitry Andric     FunctionSummary::FSHT_None;
70e3b55780SDimitry Andric } // namespace llvm
71e3b55780SDimitry Andric 
72e3b55780SDimitry Andric static cl::opt<FunctionSummary::ForceSummaryHotnessType, true> FSEC(
73eb11fae6SDimitry Andric     "force-summary-edges-cold", cl::Hidden, cl::location(ForceSummaryEdgesCold),
74eb11fae6SDimitry Andric     cl::desc("Force all edges in the function summary to cold"),
75eb11fae6SDimitry Andric     cl::values(clEnumValN(FunctionSummary::FSHT_None, "none", "None."),
76eb11fae6SDimitry Andric                clEnumValN(FunctionSummary::FSHT_AllNonCritical,
77eb11fae6SDimitry Andric                           "all-non-critical", "All non-critical edges."),
78eb11fae6SDimitry Andric                clEnumValN(FunctionSummary::FSHT_All, "all", "All edges.")));
79eb11fae6SDimitry Andric 
80e3b55780SDimitry Andric static cl::opt<std::string> ModuleSummaryDotFile(
81e3b55780SDimitry Andric     "module-summary-dot-file", cl::Hidden, cl::value_desc("filename"),
82e3b55780SDimitry Andric     cl::desc("File to emit dot graph of new summary into"));
83e6d15924SDimitry Andric 
847fa27ce4SDimitry Andric extern cl::opt<bool> ScalePartialSampleProfileWorkingSetSize;
857fa27ce4SDimitry Andric 
86ac9a064cSDimitry Andric extern cl::opt<unsigned> MaxNumVTableAnnotations;
87ac9a064cSDimitry Andric 
88ac9a064cSDimitry Andric extern cl::opt<bool> MemProfReportHintedSizes;
89ac9a064cSDimitry Andric 
9001095a5dSDimitry Andric // Walk through the operands of a given User via worklist iteration and populate
9101095a5dSDimitry Andric // the set of GlobalValue references encountered. Invoked either on an
9201095a5dSDimitry Andric // Instruction or a GlobalVariable (which walks its initializer).
93d8e91e46SDimitry Andric // Return true if any of the operands contains blockaddress. This is important
94d8e91e46SDimitry Andric // to know when computing summary for global var, because if global variable
95d8e91e46SDimitry Andric // references basic block address we can't import it separately from function
96d8e91e46SDimitry Andric // containing that basic block. For simplicity we currently don't import such
97d8e91e46SDimitry Andric // global vars at all. When importing function we aren't interested if any
98d8e91e46SDimitry Andric // instruction in it takes an address of any basic block, because instruction
99d8e91e46SDimitry Andric // can only take an address of basic block located in the same function.
100ac9a064cSDimitry Andric // Set `RefLocalLinkageIFunc` to true if the analyzed value references a
101ac9a064cSDimitry Andric // local-linkage ifunc.
findRefEdges(ModuleSummaryIndex & Index,const User * CurUser,SetVector<ValueInfo,std::vector<ValueInfo>> & RefEdges,SmallPtrSet<const User *,8> & Visited,bool & RefLocalLinkageIFunc)102d8e91e46SDimitry Andric static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,
103b1c73532SDimitry Andric                          SetVector<ValueInfo, std::vector<ValueInfo>> &RefEdges,
104ac9a064cSDimitry Andric                          SmallPtrSet<const User *, 8> &Visited,
105ac9a064cSDimitry Andric                          bool &RefLocalLinkageIFunc) {
106d8e91e46SDimitry Andric   bool HasBlockAddress = false;
10701095a5dSDimitry Andric   SmallVector<const User *, 32> Worklist;
108344a3780SDimitry Andric   if (Visited.insert(CurUser).second)
10901095a5dSDimitry Andric     Worklist.push_back(CurUser);
11001095a5dSDimitry Andric 
11101095a5dSDimitry Andric   while (!Worklist.empty()) {
11201095a5dSDimitry Andric     const User *U = Worklist.pop_back_val();
113cfca06d7SDimitry Andric     const auto *CB = dyn_cast<CallBase>(U);
11401095a5dSDimitry Andric 
11501095a5dSDimitry Andric     for (const auto &OI : U->operands()) {
11601095a5dSDimitry Andric       const User *Operand = dyn_cast<User>(OI);
11701095a5dSDimitry Andric       if (!Operand)
11801095a5dSDimitry Andric         continue;
119d8e91e46SDimitry Andric       if (isa<BlockAddress>(Operand)) {
120d8e91e46SDimitry Andric         HasBlockAddress = true;
12101095a5dSDimitry Andric         continue;
122d8e91e46SDimitry Andric       }
123b915e9e0SDimitry Andric       if (auto *GV = dyn_cast<GlobalValue>(Operand)) {
12401095a5dSDimitry Andric         // We have a reference to a global value. This should be added to
12501095a5dSDimitry Andric         // the reference set unless it is a callee. Callees are handled
12601095a5dSDimitry Andric         // specially by WriteFunction and are added to a separate list.
127ac9a064cSDimitry Andric         if (!(CB && CB->isCallee(&OI))) {
128ac9a064cSDimitry Andric           // If an ifunc has local linkage, do not add it into ref edges, and
129ac9a064cSDimitry Andric           // sets `RefLocalLinkageIFunc` to true. The referencer is not eligible
130ac9a064cSDimitry Andric           // for import. An ifunc doesn't have summary and ThinLTO cannot
131ac9a064cSDimitry Andric           // promote it; importing the referencer may cause linkage errors.
132ac9a064cSDimitry Andric           if (auto *GI = dyn_cast_if_present<GlobalIFunc>(GV);
133ac9a064cSDimitry Andric               GI && GI->hasLocalLinkage()) {
134ac9a064cSDimitry Andric             RefLocalLinkageIFunc = true;
135ac9a064cSDimitry Andric             continue;
136ac9a064cSDimitry Andric           }
137c46e6a59SDimitry Andric           RefEdges.insert(Index.getOrInsertValueInfo(GV));
138ac9a064cSDimitry Andric         }
13901095a5dSDimitry Andric         continue;
14001095a5dSDimitry Andric       }
141344a3780SDimitry Andric       if (Visited.insert(Operand).second)
14201095a5dSDimitry Andric         Worklist.push_back(Operand);
14301095a5dSDimitry Andric     }
14401095a5dSDimitry Andric   }
145ac9a064cSDimitry Andric 
146ac9a064cSDimitry Andric   const Instruction *I = dyn_cast<Instruction>(CurUser);
147ac9a064cSDimitry Andric   if (I) {
148ac9a064cSDimitry Andric     uint64_t TotalCount = 0;
149ac9a064cSDimitry Andric     // MaxNumVTableAnnotations is the maximum number of vtables annotated on
150ac9a064cSDimitry Andric     // the instruction.
151ac9a064cSDimitry Andric     auto ValueDataArray = getValueProfDataFromInst(
152ac9a064cSDimitry Andric         *I, IPVK_VTableTarget, MaxNumVTableAnnotations, TotalCount);
153ac9a064cSDimitry Andric 
154ac9a064cSDimitry Andric     for (const auto &V : ValueDataArray)
155ac9a064cSDimitry Andric       RefEdges.insert(Index.getOrInsertValueInfo(/* VTableGUID = */
156ac9a064cSDimitry Andric                                                  V.Value));
157ac9a064cSDimitry Andric   }
158d8e91e46SDimitry Andric   return HasBlockAddress;
15901095a5dSDimitry Andric }
16001095a5dSDimitry Andric 
getHotness(uint64_t ProfileCount,ProfileSummaryInfo * PSI)161b915e9e0SDimitry Andric static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount,
162b915e9e0SDimitry Andric                                           ProfileSummaryInfo *PSI) {
163b915e9e0SDimitry Andric   if (!PSI)
164b915e9e0SDimitry Andric     return CalleeInfo::HotnessType::Unknown;
165b915e9e0SDimitry Andric   if (PSI->isHotCount(ProfileCount))
166b915e9e0SDimitry Andric     return CalleeInfo::HotnessType::Hot;
167b915e9e0SDimitry Andric   if (PSI->isColdCount(ProfileCount))
168b915e9e0SDimitry Andric     return CalleeInfo::HotnessType::Cold;
169b915e9e0SDimitry Andric   return CalleeInfo::HotnessType::None;
170b915e9e0SDimitry Andric }
171b915e9e0SDimitry Andric 
isNonRenamableLocal(const GlobalValue & GV)1727e7b6700SDimitry Andric static bool isNonRenamableLocal(const GlobalValue &GV) {
1737e7b6700SDimitry Andric   return GV.hasSection() && GV.hasLocalLinkage();
1747e7b6700SDimitry Andric }
1757e7b6700SDimitry Andric 
17671d5a254SDimitry Andric /// Determine whether this call has all constant integer arguments (excluding
17771d5a254SDimitry Andric /// "this") and summarize it to VCalls or ConstVCalls as appropriate.
addVCallToSet(DevirtCallSite Call,GlobalValue::GUID Guid,SetVector<FunctionSummary::VFuncId,std::vector<FunctionSummary::VFuncId>> & VCalls,SetVector<FunctionSummary::ConstVCall,std::vector<FunctionSummary::ConstVCall>> & ConstVCalls)178b1c73532SDimitry Andric static void addVCallToSet(
179b1c73532SDimitry Andric     DevirtCallSite Call, GlobalValue::GUID Guid,
180b1c73532SDimitry Andric     SetVector<FunctionSummary::VFuncId, std::vector<FunctionSummary::VFuncId>>
181b1c73532SDimitry Andric         &VCalls,
182b1c73532SDimitry Andric     SetVector<FunctionSummary::ConstVCall,
183b1c73532SDimitry Andric               std::vector<FunctionSummary::ConstVCall>> &ConstVCalls) {
18471d5a254SDimitry Andric   std::vector<uint64_t> Args;
18571d5a254SDimitry Andric   // Start from the second argument to skip the "this" pointer.
186b60736ecSDimitry Andric   for (auto &Arg : drop_begin(Call.CB.args())) {
18771d5a254SDimitry Andric     auto *CI = dyn_cast<ConstantInt>(Arg);
18871d5a254SDimitry Andric     if (!CI || CI->getBitWidth() > 64) {
18971d5a254SDimitry Andric       VCalls.insert({Guid, Call.Offset});
19071d5a254SDimitry Andric       return;
19171d5a254SDimitry Andric     }
19271d5a254SDimitry Andric     Args.push_back(CI->getZExtValue());
19371d5a254SDimitry Andric   }
19471d5a254SDimitry Andric   ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)});
19571d5a254SDimitry Andric }
19671d5a254SDimitry Andric 
19771d5a254SDimitry Andric /// If this intrinsic call requires that we add information to the function
19871d5a254SDimitry Andric /// summary, do so via the non-constant reference arguments.
addIntrinsicToSummary(const CallInst * CI,SetVector<GlobalValue::GUID,std::vector<GlobalValue::GUID>> & TypeTests,SetVector<FunctionSummary::VFuncId,std::vector<FunctionSummary::VFuncId>> & TypeTestAssumeVCalls,SetVector<FunctionSummary::VFuncId,std::vector<FunctionSummary::VFuncId>> & TypeCheckedLoadVCalls,SetVector<FunctionSummary::ConstVCall,std::vector<FunctionSummary::ConstVCall>> & TypeTestAssumeConstVCalls,SetVector<FunctionSummary::ConstVCall,std::vector<FunctionSummary::ConstVCall>> & TypeCheckedLoadConstVCalls,DominatorTree & DT)19971d5a254SDimitry Andric static void addIntrinsicToSummary(
200b1c73532SDimitry Andric     const CallInst *CI,
201b1c73532SDimitry Andric     SetVector<GlobalValue::GUID, std::vector<GlobalValue::GUID>> &TypeTests,
202b1c73532SDimitry Andric     SetVector<FunctionSummary::VFuncId, std::vector<FunctionSummary::VFuncId>>
203b1c73532SDimitry Andric         &TypeTestAssumeVCalls,
204b1c73532SDimitry Andric     SetVector<FunctionSummary::VFuncId, std::vector<FunctionSummary::VFuncId>>
205b1c73532SDimitry Andric         &TypeCheckedLoadVCalls,
206b1c73532SDimitry Andric     SetVector<FunctionSummary::ConstVCall,
207b1c73532SDimitry Andric               std::vector<FunctionSummary::ConstVCall>>
208b1c73532SDimitry Andric         &TypeTestAssumeConstVCalls,
209b1c73532SDimitry Andric     SetVector<FunctionSummary::ConstVCall,
210b1c73532SDimitry Andric               std::vector<FunctionSummary::ConstVCall>>
211b1c73532SDimitry Andric         &TypeCheckedLoadConstVCalls,
212d8e91e46SDimitry Andric     DominatorTree &DT) {
21371d5a254SDimitry Andric   switch (CI->getCalledFunction()->getIntrinsicID()) {
21408e8dd7bSDimitry Andric   case Intrinsic::type_test:
21508e8dd7bSDimitry Andric   case Intrinsic::public_type_test: {
21671d5a254SDimitry Andric     auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1));
21771d5a254SDimitry Andric     auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
21871d5a254SDimitry Andric     if (!TypeId)
21971d5a254SDimitry Andric       break;
22071d5a254SDimitry Andric     GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
22171d5a254SDimitry Andric 
22271d5a254SDimitry Andric     // Produce a summary from type.test intrinsics. We only summarize type.test
22371d5a254SDimitry Andric     // intrinsics that are used other than by an llvm.assume intrinsic.
22471d5a254SDimitry Andric     // Intrinsics that are assumed are relevant only to the devirtualization
22571d5a254SDimitry Andric     // pass, not the type test lowering pass.
22671d5a254SDimitry Andric     bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) {
227344a3780SDimitry Andric       return !isa<AssumeInst>(CIU.getUser());
22871d5a254SDimitry Andric     });
22971d5a254SDimitry Andric     if (HasNonAssumeUses)
23071d5a254SDimitry Andric       TypeTests.insert(Guid);
23171d5a254SDimitry Andric 
23271d5a254SDimitry Andric     SmallVector<DevirtCallSite, 4> DevirtCalls;
23371d5a254SDimitry Andric     SmallVector<CallInst *, 4> Assumes;
234d8e91e46SDimitry Andric     findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT);
23571d5a254SDimitry Andric     for (auto &Call : DevirtCalls)
23671d5a254SDimitry Andric       addVCallToSet(Call, Guid, TypeTestAssumeVCalls,
23771d5a254SDimitry Andric                     TypeTestAssumeConstVCalls);
23871d5a254SDimitry Andric 
23971d5a254SDimitry Andric     break;
24071d5a254SDimitry Andric   }
24171d5a254SDimitry Andric 
2427fa27ce4SDimitry Andric   case Intrinsic::type_checked_load_relative:
24371d5a254SDimitry Andric   case Intrinsic::type_checked_load: {
24471d5a254SDimitry Andric     auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2));
24571d5a254SDimitry Andric     auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
24671d5a254SDimitry Andric     if (!TypeId)
24771d5a254SDimitry Andric       break;
24871d5a254SDimitry Andric     GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
24971d5a254SDimitry Andric 
25071d5a254SDimitry Andric     SmallVector<DevirtCallSite, 4> DevirtCalls;
25171d5a254SDimitry Andric     SmallVector<Instruction *, 4> LoadedPtrs;
25271d5a254SDimitry Andric     SmallVector<Instruction *, 4> Preds;
25371d5a254SDimitry Andric     bool HasNonCallUses = false;
25471d5a254SDimitry Andric     findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
255d8e91e46SDimitry Andric                                                HasNonCallUses, CI, DT);
25671d5a254SDimitry Andric     // Any non-call uses of the result of llvm.type.checked.load will
25771d5a254SDimitry Andric     // prevent us from optimizing away the llvm.type.test.
25871d5a254SDimitry Andric     if (HasNonCallUses)
25971d5a254SDimitry Andric       TypeTests.insert(Guid);
26071d5a254SDimitry Andric     for (auto &Call : DevirtCalls)
26171d5a254SDimitry Andric       addVCallToSet(Call, Guid, TypeCheckedLoadVCalls,
26271d5a254SDimitry Andric                     TypeCheckedLoadConstVCalls);
26371d5a254SDimitry Andric 
26471d5a254SDimitry Andric     break;
26571d5a254SDimitry Andric   }
26671d5a254SDimitry Andric   default:
26771d5a254SDimitry Andric     break;
26871d5a254SDimitry Andric   }
26971d5a254SDimitry Andric }
27071d5a254SDimitry Andric 
isNonVolatileLoad(const Instruction * I)271d8e91e46SDimitry Andric static bool isNonVolatileLoad(const Instruction *I) {
272d8e91e46SDimitry Andric   if (const auto *LI = dyn_cast<LoadInst>(I))
273d8e91e46SDimitry Andric     return !LI->isVolatile();
274d8e91e46SDimitry Andric 
275d8e91e46SDimitry Andric   return false;
276d8e91e46SDimitry Andric }
277d8e91e46SDimitry Andric 
isNonVolatileStore(const Instruction * I)278e6d15924SDimitry Andric static bool isNonVolatileStore(const Instruction *I) {
279e6d15924SDimitry Andric   if (const auto *SI = dyn_cast<StoreInst>(I))
280e6d15924SDimitry Andric     return !SI->isVolatile();
281e6d15924SDimitry Andric 
282e6d15924SDimitry Andric   return false;
283e6d15924SDimitry Andric }
284e6d15924SDimitry Andric 
28577fc4c14SDimitry Andric // Returns true if the function definition must be unreachable.
28677fc4c14SDimitry Andric //
28777fc4c14SDimitry Andric // Note if this helper function returns true, `F` is guaranteed
28877fc4c14SDimitry Andric // to be unreachable; if it returns false, `F` might still
28977fc4c14SDimitry Andric // be unreachable but not covered by this helper function.
mustBeUnreachableFunction(const Function & F)29077fc4c14SDimitry Andric static bool mustBeUnreachableFunction(const Function &F) {
29177fc4c14SDimitry Andric   // A function must be unreachable if its entry block ends with an
29277fc4c14SDimitry Andric   // 'unreachable'.
29377fc4c14SDimitry Andric   assert(!F.isDeclaration());
29477fc4c14SDimitry Andric   return isa<UnreachableInst>(F.getEntryBlock().getTerminator());
29577fc4c14SDimitry Andric }
29677fc4c14SDimitry Andric 
computeFunctionSummary(ModuleSummaryIndex & Index,const Module & M,const Function & F,BlockFrequencyInfo * BFI,ProfileSummaryInfo * PSI,DominatorTree & DT,bool HasLocalsInUsedOrAsm,DenseSet<GlobalValue::GUID> & CantBePromoted,bool IsThinLTO,std::function<const StackSafetyInfo * (const Function & F)> GetSSICallback)297cfca06d7SDimitry Andric static void computeFunctionSummary(
298cfca06d7SDimitry Andric     ModuleSummaryIndex &Index, const Module &M, const Function &F,
299cfca06d7SDimitry Andric     BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, DominatorTree &DT,
300cfca06d7SDimitry Andric     bool HasLocalsInUsedOrAsm, DenseSet<GlobalValue::GUID> &CantBePromoted,
301cfca06d7SDimitry Andric     bool IsThinLTO,
302cfca06d7SDimitry Andric     std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) {
303b915e9e0SDimitry Andric   // Summary not currently supported for anonymous functions, they should
304b915e9e0SDimitry Andric   // have been named.
305b915e9e0SDimitry Andric   assert(F.hasName());
30601095a5dSDimitry Andric 
30701095a5dSDimitry Andric   unsigned NumInsts = 0;
30801095a5dSDimitry Andric   // Map from callee ValueId to profile count. Used to accumulate profile
30901095a5dSDimitry Andric   // counts for all static calls to a given callee.
3107fa27ce4SDimitry Andric   MapVector<ValueInfo, CalleeInfo, DenseMap<ValueInfo, unsigned>,
3117fa27ce4SDimitry Andric             std::vector<std::pair<ValueInfo, CalleeInfo>>>
3127fa27ce4SDimitry Andric       CallGraphEdges;
313b1c73532SDimitry Andric   SetVector<ValueInfo, std::vector<ValueInfo>> RefEdges, LoadRefEdges,
314b1c73532SDimitry Andric       StoreRefEdges;
315b1c73532SDimitry Andric   SetVector<GlobalValue::GUID, std::vector<GlobalValue::GUID>> TypeTests;
316b1c73532SDimitry Andric   SetVector<FunctionSummary::VFuncId, std::vector<FunctionSummary::VFuncId>>
317b1c73532SDimitry Andric       TypeTestAssumeVCalls, TypeCheckedLoadVCalls;
318b1c73532SDimitry Andric   SetVector<FunctionSummary::ConstVCall,
319b1c73532SDimitry Andric             std::vector<FunctionSummary::ConstVCall>>
320b1c73532SDimitry Andric       TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls;
32101095a5dSDimitry Andric   ICallPromotionAnalysis ICallAnalysis;
322044eb2f6SDimitry Andric   SmallPtrSet<const User *, 8> Visited;
323044eb2f6SDimitry Andric 
324044eb2f6SDimitry Andric   // Add personality function, prefix data and prologue data to function's ref
325044eb2f6SDimitry Andric   // list.
326ac9a064cSDimitry Andric   bool HasLocalIFuncCallOrRef = false;
327ac9a064cSDimitry Andric   findRefEdges(Index, &F, RefEdges, Visited, HasLocalIFuncCallOrRef);
328d8e91e46SDimitry Andric   std::vector<const Instruction *> NonVolatileLoads;
329e6d15924SDimitry Andric   std::vector<const Instruction *> NonVolatileStores;
33001095a5dSDimitry Andric 
331e3b55780SDimitry Andric   std::vector<CallsiteInfo> Callsites;
332e3b55780SDimitry Andric   std::vector<AllocInfo> Allocs;
333e3b55780SDimitry Andric 
3347fa27ce4SDimitry Andric #ifndef NDEBUG
3357fa27ce4SDimitry Andric   DenseSet<const CallBase *> CallsThatMayHaveMemprofSummary;
3367fa27ce4SDimitry Andric #endif
3377fa27ce4SDimitry Andric 
338b915e9e0SDimitry Andric   bool HasInlineAsmMaybeReferencingInternal = false;
339c0981da4SDimitry Andric   bool HasIndirBranchToBlockAddress = false;
340c0981da4SDimitry Andric   bool HasUnknownCall = false;
341c0981da4SDimitry Andric   bool MayThrow = false;
342c0981da4SDimitry Andric   for (const BasicBlock &BB : F) {
343c0981da4SDimitry Andric     // We don't allow inlining of function with indirect branch to blockaddress.
344c0981da4SDimitry Andric     // If the blockaddress escapes the function, e.g., via a global variable,
345c0981da4SDimitry Andric     // inlining may lead to an invalid cross-function reference. So we shouldn't
346c0981da4SDimitry Andric     // import such function either.
347c0981da4SDimitry Andric     if (BB.hasAddressTaken()) {
348c0981da4SDimitry Andric       for (User *U : BlockAddress::get(const_cast<BasicBlock *>(&BB))->users())
349c0981da4SDimitry Andric         if (!isa<CallBrInst>(*U)) {
350c0981da4SDimitry Andric           HasIndirBranchToBlockAddress = true;
351c0981da4SDimitry Andric           break;
352c0981da4SDimitry Andric         }
353c0981da4SDimitry Andric     }
354c0981da4SDimitry Andric 
35501095a5dSDimitry Andric     for (const Instruction &I : BB) {
356c0981da4SDimitry Andric       if (I.isDebugOrPseudoInst())
357b915e9e0SDimitry Andric         continue;
35801095a5dSDimitry Andric       ++NumInsts;
359c0981da4SDimitry Andric 
360e6d15924SDimitry Andric       // Regular LTO module doesn't participate in ThinLTO import,
361e6d15924SDimitry Andric       // so no reference from it can be read/writeonly, since this
362e6d15924SDimitry Andric       // would require importing variable as local copy
363e6d15924SDimitry Andric       if (IsThinLTO) {
364d8e91e46SDimitry Andric         if (isNonVolatileLoad(&I)) {
365d8e91e46SDimitry Andric           // Postpone processing of non-volatile load instructions
366d8e91e46SDimitry Andric           // See comments below
367d8e91e46SDimitry Andric           Visited.insert(&I);
368d8e91e46SDimitry Andric           NonVolatileLoads.push_back(&I);
369d8e91e46SDimitry Andric           continue;
370e6d15924SDimitry Andric         } else if (isNonVolatileStore(&I)) {
371e6d15924SDimitry Andric           Visited.insert(&I);
372e6d15924SDimitry Andric           NonVolatileStores.push_back(&I);
373e6d15924SDimitry Andric           // All references from second operand of store (destination address)
374e6d15924SDimitry Andric           // can be considered write-only if they're not referenced by any
375e6d15924SDimitry Andric           // non-store instruction. References from first operand of store
376e6d15924SDimitry Andric           // (stored value) can't be treated either as read- or as write-only
377e6d15924SDimitry Andric           // so we add them to RefEdges as we do with all other instructions
378e6d15924SDimitry Andric           // except non-volatile load.
379e6d15924SDimitry Andric           Value *Stored = I.getOperand(0);
380e6d15924SDimitry Andric           if (auto *GV = dyn_cast<GlobalValue>(Stored))
381e6d15924SDimitry Andric             // findRefEdges will try to examine GV operands, so instead
382e6d15924SDimitry Andric             // of calling it we should add GV to RefEdges directly.
383e6d15924SDimitry Andric             RefEdges.insert(Index.getOrInsertValueInfo(GV));
384e6d15924SDimitry Andric           else if (auto *U = dyn_cast<User>(Stored))
385ac9a064cSDimitry Andric             findRefEdges(Index, U, RefEdges, Visited, HasLocalIFuncCallOrRef);
386e6d15924SDimitry Andric           continue;
387e6d15924SDimitry Andric         }
388d8e91e46SDimitry Andric       }
389ac9a064cSDimitry Andric       findRefEdges(Index, &I, RefEdges, Visited, HasLocalIFuncCallOrRef);
390cfca06d7SDimitry Andric       const auto *CB = dyn_cast<CallBase>(&I);
391c0981da4SDimitry Andric       if (!CB) {
392c0981da4SDimitry Andric         if (I.mayThrow())
393c0981da4SDimitry Andric           MayThrow = true;
394b915e9e0SDimitry Andric         continue;
395c0981da4SDimitry Andric       }
39601095a5dSDimitry Andric 
397b915e9e0SDimitry Andric       const auto *CI = dyn_cast<CallInst>(&I);
398b915e9e0SDimitry Andric       // Since we don't know exactly which local values are referenced in inline
399b915e9e0SDimitry Andric       // assembly, conservatively mark the function as possibly referencing
400b915e9e0SDimitry Andric       // a local value from inline assembly to ensure we don't export a
401b915e9e0SDimitry Andric       // reference (which would require renaming and promotion of the
402b915e9e0SDimitry Andric       // referenced value).
403044eb2f6SDimitry Andric       if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm())
404b915e9e0SDimitry Andric         HasInlineAsmMaybeReferencingInternal = true;
405b915e9e0SDimitry Andric 
406cfca06d7SDimitry Andric       auto *CalledValue = CB->getCalledOperand();
407cfca06d7SDimitry Andric       auto *CalledFunction = CB->getCalledFunction();
408044eb2f6SDimitry Andric       if (CalledValue && !CalledFunction) {
4091d5ae102SDimitry Andric         CalledValue = CalledValue->stripPointerCasts();
410044eb2f6SDimitry Andric         // Stripping pointer casts can reveal a called function.
411044eb2f6SDimitry Andric         CalledFunction = dyn_cast<Function>(CalledValue);
412044eb2f6SDimitry Andric       }
413b915e9e0SDimitry Andric       // Check if this is an alias to a function. If so, get the
414b915e9e0SDimitry Andric       // called aliasee for the checks below.
415b915e9e0SDimitry Andric       if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
416b915e9e0SDimitry Andric         assert(!CalledFunction && "Expected null called function in callsite for alias");
417c0981da4SDimitry Andric         CalledFunction = dyn_cast<Function>(GA->getAliaseeObject());
41801095a5dSDimitry Andric       }
419b915e9e0SDimitry Andric       // Check if this is a direct call to a known function or a known
420b915e9e0SDimitry Andric       // intrinsic, or an indirect call with profile data.
421b915e9e0SDimitry Andric       if (CalledFunction) {
42271d5a254SDimitry Andric         if (CI && CalledFunction->isIntrinsic()) {
42371d5a254SDimitry Andric           addIntrinsicToSummary(
42471d5a254SDimitry Andric               CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls,
425d8e91e46SDimitry Andric               TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls, DT);
426b915e9e0SDimitry Andric           continue;
427b915e9e0SDimitry Andric         }
428b915e9e0SDimitry Andric         // We should have named any anonymous globals
429b915e9e0SDimitry Andric         assert(CalledFunction->hasName());
430cfca06d7SDimitry Andric         auto ScaledCount = PSI->getProfileCount(*CB, BFI);
431145449b1SDimitry Andric         auto Hotness = ScaledCount ? getHotness(*ScaledCount, PSI)
432b915e9e0SDimitry Andric                                    : CalleeInfo::HotnessType::Unknown;
433eb11fae6SDimitry Andric         if (ForceSummaryEdgesCold != FunctionSummary::FSHT_None)
434eb11fae6SDimitry Andric           Hotness = CalleeInfo::HotnessType::Cold;
435b915e9e0SDimitry Andric 
436b915e9e0SDimitry Andric         // Use the original CalledValue, in case it was an alias. We want
437b915e9e0SDimitry Andric         // to record the call edge to the alias in that case. Eventually
438b915e9e0SDimitry Andric         // an alias summary will be created to associate the alias and
439b915e9e0SDimitry Andric         // aliasee.
440eb11fae6SDimitry Andric         auto &ValueInfo = CallGraphEdges[Index.getOrInsertValueInfo(
441eb11fae6SDimitry Andric             cast<GlobalValue>(CalledValue))];
442eb11fae6SDimitry Andric         ValueInfo.updateHotness(Hotness);
443b1c73532SDimitry Andric         if (CB->isTailCall())
444b1c73532SDimitry Andric           ValueInfo.setHasTailCall(true);
445eb11fae6SDimitry Andric         // Add the relative block frequency to CalleeInfo if there is no profile
446eb11fae6SDimitry Andric         // information.
447eb11fae6SDimitry Andric         if (BFI != nullptr && Hotness == CalleeInfo::HotnessType::Unknown) {
448eb11fae6SDimitry Andric           uint64_t BBFreq = BFI->getBlockFreq(&BB).getFrequency();
449b1c73532SDimitry Andric           uint64_t EntryFreq = BFI->getEntryFreq().getFrequency();
450eb11fae6SDimitry Andric           ValueInfo.updateRelBlockFreq(BBFreq, EntryFreq);
451eb11fae6SDimitry Andric         }
45201095a5dSDimitry Andric       } else {
453c0981da4SDimitry Andric         HasUnknownCall = true;
454b1c73532SDimitry Andric         // If F is imported, a local linkage ifunc (e.g. target_clones on a
455b1c73532SDimitry Andric         // static function) called by F will be cloned. Since summaries don't
456b1c73532SDimitry Andric         // track ifunc, we do not know implementation functions referenced by
457b1c73532SDimitry Andric         // the ifunc resolver need to be promoted in the exporter, and we will
458b1c73532SDimitry Andric         // get linker errors due to cloned declarations for implementation
459b1c73532SDimitry Andric         // functions. As a simple fix, just mark F as not eligible for import.
460b1c73532SDimitry Andric         // Non-local ifunc is not cloned and does not have the issue.
461b1c73532SDimitry Andric         if (auto *GI = dyn_cast_if_present<GlobalIFunc>(CalledValue))
462b1c73532SDimitry Andric           if (GI->hasLocalLinkage())
463ac9a064cSDimitry Andric             HasLocalIFuncCallOrRef = true;
464b915e9e0SDimitry Andric         // Skip inline assembly calls.
465b915e9e0SDimitry Andric         if (CI && CI->isInlineAsm())
466b915e9e0SDimitry Andric           continue;
467b915e9e0SDimitry Andric         // Skip direct calls.
468044eb2f6SDimitry Andric         if (!CalledValue || isa<Constant>(CalledValue))
469b915e9e0SDimitry Andric           continue;
470b915e9e0SDimitry Andric 
471eb11fae6SDimitry Andric         // Check if the instruction has a callees metadata. If so, add callees
472eb11fae6SDimitry Andric         // to CallGraphEdges to reflect the references from the metadata, and
473eb11fae6SDimitry Andric         // to enable importing for subsequent indirect call promotion and
474eb11fae6SDimitry Andric         // inlining.
475eb11fae6SDimitry Andric         if (auto *MD = I.getMetadata(LLVMContext::MD_callees)) {
4764b4fe385SDimitry Andric           for (const auto &Op : MD->operands()) {
477eb11fae6SDimitry Andric             Function *Callee = mdconst::extract_or_null<Function>(Op);
478eb11fae6SDimitry Andric             if (Callee)
479eb11fae6SDimitry Andric               CallGraphEdges[Index.getOrInsertValueInfo(Callee)];
480eb11fae6SDimitry Andric           }
481eb11fae6SDimitry Andric         }
482eb11fae6SDimitry Andric 
483ac9a064cSDimitry Andric         uint32_t NumCandidates;
48401095a5dSDimitry Andric         uint64_t TotalCount;
48501095a5dSDimitry Andric         auto CandidateProfileData =
486ac9a064cSDimitry Andric             ICallAnalysis.getPromotionCandidatesForInstruction(&I, TotalCount,
487ac9a064cSDimitry Andric                                                                NumCandidates);
4884b4fe385SDimitry Andric         for (const auto &Candidate : CandidateProfileData)
489c46e6a59SDimitry Andric           CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)]
490c46e6a59SDimitry Andric               .updateHotness(getHotness(Candidate.Count, PSI));
49101095a5dSDimitry Andric       }
492e3b55780SDimitry Andric 
4937fa27ce4SDimitry Andric       // Summarize memprof related metadata. This is only needed for ThinLTO.
4947fa27ce4SDimitry Andric       if (!IsThinLTO)
4957fa27ce4SDimitry Andric         continue;
4967fa27ce4SDimitry Andric 
497e3b55780SDimitry Andric       // TODO: Skip indirect calls for now. Need to handle these better, likely
498e3b55780SDimitry Andric       // by creating multiple Callsites, one per target, then speculatively
499e3b55780SDimitry Andric       // devirtualize while applying clone info in the ThinLTO backends. This
500e3b55780SDimitry Andric       // will also be important because we will have a different set of clone
501e3b55780SDimitry Andric       // versions per target. This handling needs to match that in the ThinLTO
502e3b55780SDimitry Andric       // backend so we handle things consistently for matching of callsite
503e3b55780SDimitry Andric       // summaries to instructions.
504e3b55780SDimitry Andric       if (!CalledFunction)
505e3b55780SDimitry Andric         continue;
506e3b55780SDimitry Andric 
5077fa27ce4SDimitry Andric       // Ensure we keep this analysis in sync with the handling in the ThinLTO
5087fa27ce4SDimitry Andric       // backend (see MemProfContextDisambiguation::applyImport). Save this call
5097fa27ce4SDimitry Andric       // so that we can skip it in checking the reverse case later.
5107fa27ce4SDimitry Andric       assert(mayHaveMemprofSummary(CB));
5117fa27ce4SDimitry Andric #ifndef NDEBUG
5127fa27ce4SDimitry Andric       CallsThatMayHaveMemprofSummary.insert(CB);
5137fa27ce4SDimitry Andric #endif
5147fa27ce4SDimitry Andric 
515e3b55780SDimitry Andric       // Compute the list of stack ids first (so we can trim them from the stack
516e3b55780SDimitry Andric       // ids on any MIBs).
517e3b55780SDimitry Andric       CallStack<MDNode, MDNode::op_iterator> InstCallsite(
518e3b55780SDimitry Andric           I.getMetadata(LLVMContext::MD_callsite));
519e3b55780SDimitry Andric       auto *MemProfMD = I.getMetadata(LLVMContext::MD_memprof);
520e3b55780SDimitry Andric       if (MemProfMD) {
521e3b55780SDimitry Andric         std::vector<MIBInfo> MIBs;
522ac9a064cSDimitry Andric         std::vector<uint64_t> TotalSizes;
523e3b55780SDimitry Andric         for (auto &MDOp : MemProfMD->operands()) {
524e3b55780SDimitry Andric           auto *MIBMD = cast<const MDNode>(MDOp);
525e3b55780SDimitry Andric           MDNode *StackNode = getMIBStackNode(MIBMD);
526e3b55780SDimitry Andric           assert(StackNode);
527e3b55780SDimitry Andric           SmallVector<unsigned> StackIdIndices;
528e3b55780SDimitry Andric           CallStack<MDNode, MDNode::op_iterator> StackContext(StackNode);
529e3b55780SDimitry Andric           // Collapse out any on the allocation call (inlining).
530e3b55780SDimitry Andric           for (auto ContextIter =
531e3b55780SDimitry Andric                    StackContext.beginAfterSharedPrefix(InstCallsite);
532e3b55780SDimitry Andric                ContextIter != StackContext.end(); ++ContextIter) {
533e3b55780SDimitry Andric             unsigned StackIdIdx = Index.addOrGetStackIdIndex(*ContextIter);
534e3b55780SDimitry Andric             // If this is a direct recursion, simply skip the duplicate
535e3b55780SDimitry Andric             // entries. If this is mutual recursion, handling is left to
536e3b55780SDimitry Andric             // the LTO link analysis client.
537e3b55780SDimitry Andric             if (StackIdIndices.empty() || StackIdIndices.back() != StackIdIdx)
538e3b55780SDimitry Andric               StackIdIndices.push_back(StackIdIdx);
539e3b55780SDimitry Andric           }
540e3b55780SDimitry Andric           MIBs.push_back(
541e3b55780SDimitry Andric               MIBInfo(getMIBAllocType(MIBMD), std::move(StackIdIndices)));
542ac9a064cSDimitry Andric           if (MemProfReportHintedSizes) {
543ac9a064cSDimitry Andric             auto TotalSize = getMIBTotalSize(MIBMD);
544ac9a064cSDimitry Andric             assert(TotalSize);
545ac9a064cSDimitry Andric             TotalSizes.push_back(TotalSize);
546ac9a064cSDimitry Andric           }
547e3b55780SDimitry Andric         }
548e3b55780SDimitry Andric         Allocs.push_back(AllocInfo(std::move(MIBs)));
549ac9a064cSDimitry Andric         if (MemProfReportHintedSizes) {
550ac9a064cSDimitry Andric           assert(Allocs.back().MIBs.size() == TotalSizes.size());
551ac9a064cSDimitry Andric           Allocs.back().TotalSizes = std::move(TotalSizes);
552ac9a064cSDimitry Andric         }
553e3b55780SDimitry Andric       } else if (!InstCallsite.empty()) {
554e3b55780SDimitry Andric         SmallVector<unsigned> StackIdIndices;
555e3b55780SDimitry Andric         for (auto StackId : InstCallsite)
556e3b55780SDimitry Andric           StackIdIndices.push_back(Index.addOrGetStackIdIndex(StackId));
557e3b55780SDimitry Andric         // Use the original CalledValue, in case it was an alias. We want
558e3b55780SDimitry Andric         // to record the call edge to the alias in that case. Eventually
559e3b55780SDimitry Andric         // an alias summary will be created to associate the alias and
560e3b55780SDimitry Andric         // aliasee.
561e3b55780SDimitry Andric         auto CalleeValueInfo =
562e3b55780SDimitry Andric             Index.getOrInsertValueInfo(cast<GlobalValue>(CalledValue));
563e3b55780SDimitry Andric         Callsites.push_back({CalleeValueInfo, StackIdIndices});
564e3b55780SDimitry Andric       }
56501095a5dSDimitry Andric     }
566c0981da4SDimitry Andric   }
5677fa27ce4SDimitry Andric 
5687fa27ce4SDimitry Andric   if (PSI->hasPartialSampleProfile() && ScalePartialSampleProfileWorkingSetSize)
569cfca06d7SDimitry Andric     Index.addBlockCount(F.size());
57001095a5dSDimitry Andric 
571e6d15924SDimitry Andric   std::vector<ValueInfo> Refs;
572e6d15924SDimitry Andric   if (IsThinLTO) {
573e6d15924SDimitry Andric     auto AddRefEdges = [&](const std::vector<const Instruction *> &Instrs,
574b1c73532SDimitry Andric                            SetVector<ValueInfo, std::vector<ValueInfo>> &Edges,
575e6d15924SDimitry Andric                            SmallPtrSet<const User *, 8> &Cache) {
576e6d15924SDimitry Andric       for (const auto *I : Instrs) {
577e6d15924SDimitry Andric         Cache.erase(I);
578ac9a064cSDimitry Andric         findRefEdges(Index, I, Edges, Cache, HasLocalIFuncCallOrRef);
579d8e91e46SDimitry Andric       }
580e6d15924SDimitry Andric     };
581e6d15924SDimitry Andric 
582e6d15924SDimitry Andric     // By now we processed all instructions in a function, except
583e6d15924SDimitry Andric     // non-volatile loads and non-volatile value stores. Let's find
584e6d15924SDimitry Andric     // ref edges for both of instruction sets
585e6d15924SDimitry Andric     AddRefEdges(NonVolatileLoads, LoadRefEdges, Visited);
586e6d15924SDimitry Andric     // We can add some values to the Visited set when processing load
587e6d15924SDimitry Andric     // instructions which are also used by stores in NonVolatileStores.
588e6d15924SDimitry Andric     // For example this can happen if we have following code:
589e6d15924SDimitry Andric     //
590e6d15924SDimitry Andric     // store %Derived* @foo, %Derived** bitcast (%Base** @bar to %Derived**)
591e6d15924SDimitry Andric     // %42 = load %Derived*, %Derived** bitcast (%Base** @bar to %Derived**)
592e6d15924SDimitry Andric     //
593e6d15924SDimitry Andric     // After processing loads we'll add bitcast to the Visited set, and if
594e6d15924SDimitry Andric     // we use the same set while processing stores, we'll never see store
595e6d15924SDimitry Andric     // to @bar and @bar will be mistakenly treated as readonly.
596e6d15924SDimitry Andric     SmallPtrSet<const llvm::User *, 8> StoreCache;
597e6d15924SDimitry Andric     AddRefEdges(NonVolatileStores, StoreRefEdges, StoreCache);
598e6d15924SDimitry Andric 
599e6d15924SDimitry Andric     // If both load and store instruction reference the same variable
600e6d15924SDimitry Andric     // we won't be able to optimize it. Add all such reference edges
601e6d15924SDimitry Andric     // to RefEdges set.
6024b4fe385SDimitry Andric     for (const auto &VI : StoreRefEdges)
603e6d15924SDimitry Andric       if (LoadRefEdges.remove(VI))
604e6d15924SDimitry Andric         RefEdges.insert(VI);
605e6d15924SDimitry Andric 
606e6d15924SDimitry Andric     unsigned RefCnt = RefEdges.size();
607e6d15924SDimitry Andric     // All new reference edges inserted in two loops below are either
608e6d15924SDimitry Andric     // read or write only. They will be grouped in the end of RefEdges
609e6d15924SDimitry Andric     // vector, so we can use a single integer value to identify them.
6104b4fe385SDimitry Andric     for (const auto &VI : LoadRefEdges)
611e6d15924SDimitry Andric       RefEdges.insert(VI);
612e6d15924SDimitry Andric 
613e6d15924SDimitry Andric     unsigned FirstWORef = RefEdges.size();
6144b4fe385SDimitry Andric     for (const auto &VI : StoreRefEdges)
615e6d15924SDimitry Andric       RefEdges.insert(VI);
616e6d15924SDimitry Andric 
617e6d15924SDimitry Andric     Refs = RefEdges.takeVector();
618e6d15924SDimitry Andric     for (; RefCnt < FirstWORef; ++RefCnt)
619d8e91e46SDimitry Andric       Refs[RefCnt].setReadOnly();
620d8e91e46SDimitry Andric 
621e6d15924SDimitry Andric     for (; RefCnt < Refs.size(); ++RefCnt)
622e6d15924SDimitry Andric       Refs[RefCnt].setWriteOnly();
623e6d15924SDimitry Andric   } else {
624e6d15924SDimitry Andric     Refs = RefEdges.takeVector();
625e6d15924SDimitry Andric   }
62671d5a254SDimitry Andric   // Explicit add hot edges to enforce importing for designated GUIDs for
62771d5a254SDimitry Andric   // sample PGO, to enable the same inlines as the profiled optimized binary.
62871d5a254SDimitry Andric   for (auto &I : F.getImportGUIDs())
629c46e6a59SDimitry Andric     CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness(
630eb11fae6SDimitry Andric         ForceSummaryEdgesCold == FunctionSummary::FSHT_All
631eb11fae6SDimitry Andric             ? CalleeInfo::HotnessType::Cold
632eb11fae6SDimitry Andric             : CalleeInfo::HotnessType::Critical);
63371d5a254SDimitry Andric 
6347fa27ce4SDimitry Andric #ifndef NDEBUG
6357fa27ce4SDimitry Andric   // Make sure that all calls we decided could not have memprof summaries get a
6367fa27ce4SDimitry Andric   // false value for mayHaveMemprofSummary, to ensure that this handling remains
6377fa27ce4SDimitry Andric   // in sync with the ThinLTO backend handling.
6387fa27ce4SDimitry Andric   if (IsThinLTO) {
6397fa27ce4SDimitry Andric     for (const BasicBlock &BB : F) {
6407fa27ce4SDimitry Andric       for (const Instruction &I : BB) {
6417fa27ce4SDimitry Andric         const auto *CB = dyn_cast<CallBase>(&I);
6427fa27ce4SDimitry Andric         if (!CB)
6437fa27ce4SDimitry Andric           continue;
6447fa27ce4SDimitry Andric         // We already checked these above.
6457fa27ce4SDimitry Andric         if (CallsThatMayHaveMemprofSummary.count(CB))
6467fa27ce4SDimitry Andric           continue;
6477fa27ce4SDimitry Andric         assert(!mayHaveMemprofSummary(CB));
6487fa27ce4SDimitry Andric       }
6497fa27ce4SDimitry Andric     }
6507fa27ce4SDimitry Andric   }
6517fa27ce4SDimitry Andric #endif
6527fa27ce4SDimitry Andric 
6537e7b6700SDimitry Andric   bool NonRenamableLocal = isNonRenamableLocal(F);
654ac9a064cSDimitry Andric   bool NotEligibleForImport =
655ac9a064cSDimitry Andric       NonRenamableLocal || HasInlineAsmMaybeReferencingInternal ||
656ac9a064cSDimitry Andric       HasIndirBranchToBlockAddress || HasLocalIFuncCallOrRef;
657344a3780SDimitry Andric   GlobalValueSummary::GVFlags Flags(
658344a3780SDimitry Andric       F.getLinkage(), F.getVisibility(), NotEligibleForImport,
659ac9a064cSDimitry Andric       /* Live = */ false, F.isDSOLocal(), F.canBeOmittedFromSymbolTable(),
660ac9a064cSDimitry Andric       GlobalValueSummary::ImportKind::Definition);
661044eb2f6SDimitry Andric   FunctionSummary::FFlags FunFlags{
662e3b55780SDimitry Andric       F.doesNotAccessMemory(), F.onlyReadsMemory() && !F.doesNotAccessMemory(),
663d8e91e46SDimitry Andric       F.hasFnAttribute(Attribute::NoRecurse), F.returnDoesNotAlias(),
664d8e91e46SDimitry Andric       // FIXME: refactor this to use the same code that inliner is using.
665d8e91e46SDimitry Andric       // Don't try to import functions with noinline attribute.
666c0981da4SDimitry Andric       F.getAttributes().hasFnAttr(Attribute::NoInline),
667c0981da4SDimitry Andric       F.hasFnAttribute(Attribute::AlwaysInline),
66877fc4c14SDimitry Andric       F.hasFnAttribute(Attribute::NoUnwind), MayThrow, HasUnknownCall,
66977fc4c14SDimitry Andric       mustBeUnreachableFunction(F)};
670cfca06d7SDimitry Andric   std::vector<FunctionSummary::ParamAccess> ParamAccesses;
671cfca06d7SDimitry Andric   if (auto *SSI = GetSSICallback(F))
672b60736ecSDimitry Andric     ParamAccesses = SSI->getParamAccesses(Index);
6731d5ae102SDimitry Andric   auto FuncSummary = std::make_unique<FunctionSummary>(
674d8e91e46SDimitry Andric       Flags, NumInsts, FunFlags, /*EntryCount=*/0, std::move(Refs),
675044eb2f6SDimitry Andric       CallGraphEdges.takeVector(), TypeTests.takeVector(),
676044eb2f6SDimitry Andric       TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(),
67771d5a254SDimitry Andric       TypeTestAssumeConstVCalls.takeVector(),
678e3b55780SDimitry Andric       TypeCheckedLoadConstVCalls.takeVector(), std::move(ParamAccesses),
679e3b55780SDimitry Andric       std::move(Callsites), std::move(Allocs));
6807e7b6700SDimitry Andric   if (NonRenamableLocal)
6817e7b6700SDimitry Andric     CantBePromoted.insert(F.getGUID());
682eb11fae6SDimitry Andric   Index.addGlobalValueSummary(F, std::move(FuncSummary));
68301095a5dSDimitry Andric }
68401095a5dSDimitry Andric 
685e6d15924SDimitry Andric /// Find function pointers referenced within the given vtable initializer
686e6d15924SDimitry Andric /// (or subset of an initializer) \p I. The starting offset of \p I within
687e6d15924SDimitry Andric /// the vtable initializer is \p StartingOffset. Any discovered function
688e6d15924SDimitry Andric /// pointers are added to \p VTableFuncs along with their cumulative offset
689e6d15924SDimitry Andric /// within the initializer.
findFuncPointers(const Constant * I,uint64_t StartingOffset,const Module & M,ModuleSummaryIndex & Index,VTableFuncList & VTableFuncs,const GlobalVariable & OrigGV)690e6d15924SDimitry Andric static void findFuncPointers(const Constant *I, uint64_t StartingOffset,
691e6d15924SDimitry Andric                              const Module &M, ModuleSummaryIndex &Index,
692ac9a064cSDimitry Andric                              VTableFuncList &VTableFuncs,
693ac9a064cSDimitry Andric                              const GlobalVariable &OrigGV) {
694e6d15924SDimitry Andric   // First check if this is a function pointer.
695e6d15924SDimitry Andric   if (I->getType()->isPointerTy()) {
6967fa27ce4SDimitry Andric     auto C = I->stripPointerCasts();
6977fa27ce4SDimitry Andric     auto A = dyn_cast<GlobalAlias>(C);
6987fa27ce4SDimitry Andric     if (isa<Function>(C) || (A && isa<Function>(A->getAliasee()))) {
6997fa27ce4SDimitry Andric       auto GV = dyn_cast<GlobalValue>(C);
7007fa27ce4SDimitry Andric       assert(GV);
701e6d15924SDimitry Andric       // We can disregard __cxa_pure_virtual as a possible call target, as
702e6d15924SDimitry Andric       // calls to pure virtuals are UB.
7037fa27ce4SDimitry Andric       if (GV && GV->getName() != "__cxa_pure_virtual")
7047fa27ce4SDimitry Andric         VTableFuncs.push_back({Index.getOrInsertValueInfo(GV), StartingOffset});
705e6d15924SDimitry Andric       return;
706e6d15924SDimitry Andric     }
7077fa27ce4SDimitry Andric   }
708e6d15924SDimitry Andric 
709e6d15924SDimitry Andric   // Walk through the elements in the constant struct or array and recursively
710e6d15924SDimitry Andric   // look for virtual function pointers.
711e6d15924SDimitry Andric   const DataLayout &DL = M.getDataLayout();
712e6d15924SDimitry Andric   if (auto *C = dyn_cast<ConstantStruct>(I)) {
713e6d15924SDimitry Andric     StructType *STy = dyn_cast<StructType>(C->getType());
714e6d15924SDimitry Andric     assert(STy);
715e6d15924SDimitry Andric     const StructLayout *SL = DL.getStructLayout(C->getType());
716e6d15924SDimitry Andric 
717344a3780SDimitry Andric     for (auto EI : llvm::enumerate(STy->elements())) {
718344a3780SDimitry Andric       auto Offset = SL->getElementOffset(EI.index());
719e6d15924SDimitry Andric       unsigned Op = SL->getElementContainingOffset(Offset);
720e6d15924SDimitry Andric       findFuncPointers(cast<Constant>(I->getOperand(Op)),
721ac9a064cSDimitry Andric                        StartingOffset + Offset, M, Index, VTableFuncs, OrigGV);
722e6d15924SDimitry Andric     }
723e6d15924SDimitry Andric   } else if (auto *C = dyn_cast<ConstantArray>(I)) {
724e6d15924SDimitry Andric     ArrayType *ATy = C->getType();
725e6d15924SDimitry Andric     Type *EltTy = ATy->getElementType();
726e6d15924SDimitry Andric     uint64_t EltSize = DL.getTypeAllocSize(EltTy);
727e6d15924SDimitry Andric     for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
728e6d15924SDimitry Andric       findFuncPointers(cast<Constant>(I->getOperand(i)),
729ac9a064cSDimitry Andric                        StartingOffset + i * EltSize, M, Index, VTableFuncs,
730ac9a064cSDimitry Andric                        OrigGV);
731ac9a064cSDimitry Andric     }
732ac9a064cSDimitry Andric   } else if (const auto *CE = dyn_cast<ConstantExpr>(I)) {
733ac9a064cSDimitry Andric     // For relative vtables, the next sub-component should be a trunc.
734ac9a064cSDimitry Andric     if (CE->getOpcode() != Instruction::Trunc ||
735ac9a064cSDimitry Andric         !(CE = dyn_cast<ConstantExpr>(CE->getOperand(0))))
736ac9a064cSDimitry Andric       return;
737ac9a064cSDimitry Andric 
738ac9a064cSDimitry Andric     // If this constant can be reduced to the offset between a function and a
739ac9a064cSDimitry Andric     // global, then we know this is a valid virtual function if the RHS is the
740ac9a064cSDimitry Andric     // original vtable we're scanning through.
741ac9a064cSDimitry Andric     if (CE->getOpcode() == Instruction::Sub) {
742ac9a064cSDimitry Andric       GlobalValue *LHS, *RHS;
743ac9a064cSDimitry Andric       APSInt LHSOffset, RHSOffset;
744ac9a064cSDimitry Andric       if (IsConstantOffsetFromGlobal(CE->getOperand(0), LHS, LHSOffset, DL) &&
745ac9a064cSDimitry Andric           IsConstantOffsetFromGlobal(CE->getOperand(1), RHS, RHSOffset, DL) &&
746ac9a064cSDimitry Andric           RHS == &OrigGV &&
747ac9a064cSDimitry Andric 
748ac9a064cSDimitry Andric           // For relative vtables, this component should point to the callable
749ac9a064cSDimitry Andric           // function without any offsets.
750ac9a064cSDimitry Andric           LHSOffset == 0 &&
751ac9a064cSDimitry Andric 
752ac9a064cSDimitry Andric           // Also, the RHS should always point to somewhere within the vtable.
753ac9a064cSDimitry Andric           RHSOffset <=
754ac9a064cSDimitry Andric               static_cast<uint64_t>(DL.getTypeAllocSize(OrigGV.getInitializer()->getType()))) {
755ac9a064cSDimitry Andric         findFuncPointers(LHS, StartingOffset, M, Index, VTableFuncs, OrigGV);
756ac9a064cSDimitry Andric       }
757e6d15924SDimitry Andric     }
758e6d15924SDimitry Andric   }
759e6d15924SDimitry Andric }
760e6d15924SDimitry Andric 
761e6d15924SDimitry Andric // Identify the function pointers referenced by vtable definition \p V.
computeVTableFuncs(ModuleSummaryIndex & Index,const GlobalVariable & V,const Module & M,VTableFuncList & VTableFuncs)762e6d15924SDimitry Andric static void computeVTableFuncs(ModuleSummaryIndex &Index,
763e6d15924SDimitry Andric                                const GlobalVariable &V, const Module &M,
764e6d15924SDimitry Andric                                VTableFuncList &VTableFuncs) {
765e6d15924SDimitry Andric   if (!V.isConstant())
766e6d15924SDimitry Andric     return;
767e6d15924SDimitry Andric 
768e6d15924SDimitry Andric   findFuncPointers(V.getInitializer(), /*StartingOffset=*/0, M, Index,
769ac9a064cSDimitry Andric                    VTableFuncs, V);
770e6d15924SDimitry Andric 
771e6d15924SDimitry Andric #ifndef NDEBUG
772e6d15924SDimitry Andric   // Validate that the VTableFuncs list is ordered by offset.
773e6d15924SDimitry Andric   uint64_t PrevOffset = 0;
774e6d15924SDimitry Andric   for (auto &P : VTableFuncs) {
775e6d15924SDimitry Andric     // The findVFuncPointers traversal should have encountered the
776e6d15924SDimitry Andric     // functions in offset order. We need to use ">=" since PrevOffset
777e6d15924SDimitry Andric     // starts at 0.
778e6d15924SDimitry Andric     assert(P.VTableOffset >= PrevOffset);
779e6d15924SDimitry Andric     PrevOffset = P.VTableOffset;
780e6d15924SDimitry Andric   }
781e6d15924SDimitry Andric #endif
782e6d15924SDimitry Andric }
783e6d15924SDimitry Andric 
784e6d15924SDimitry Andric /// Record vtable definition \p V for each type metadata it references.
7857e7b6700SDimitry Andric static void
recordTypeIdCompatibleVtableReferences(ModuleSummaryIndex & Index,const GlobalVariable & V,SmallVectorImpl<MDNode * > & Types)786e6d15924SDimitry Andric recordTypeIdCompatibleVtableReferences(ModuleSummaryIndex &Index,
787e6d15924SDimitry Andric                                        const GlobalVariable &V,
788e6d15924SDimitry Andric                                        SmallVectorImpl<MDNode *> &Types) {
789e6d15924SDimitry Andric   for (MDNode *Type : Types) {
790e6d15924SDimitry Andric     auto TypeID = Type->getOperand(1).get();
791e6d15924SDimitry Andric 
792e6d15924SDimitry Andric     uint64_t Offset =
793e6d15924SDimitry Andric         cast<ConstantInt>(
794e6d15924SDimitry Andric             cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
795e6d15924SDimitry Andric             ->getZExtValue();
796e6d15924SDimitry Andric 
797e6d15924SDimitry Andric     if (auto *TypeId = dyn_cast<MDString>(TypeID))
798e6d15924SDimitry Andric       Index.getOrInsertTypeIdCompatibleVtableSummary(TypeId->getString())
799e6d15924SDimitry Andric           .push_back({Offset, Index.getOrInsertValueInfo(&V)});
800e6d15924SDimitry Andric   }
801e6d15924SDimitry Andric }
802e6d15924SDimitry Andric 
computeVariableSummary(ModuleSummaryIndex & Index,const GlobalVariable & V,DenseSet<GlobalValue::GUID> & CantBePromoted,const Module & M,SmallVectorImpl<MDNode * > & Types)803e6d15924SDimitry Andric static void computeVariableSummary(ModuleSummaryIndex &Index,
804e6d15924SDimitry Andric                                    const GlobalVariable &V,
805e6d15924SDimitry Andric                                    DenseSet<GlobalValue::GUID> &CantBePromoted,
806e6d15924SDimitry Andric                                    const Module &M,
807e6d15924SDimitry Andric                                    SmallVectorImpl<MDNode *> &Types) {
808b1c73532SDimitry Andric   SetVector<ValueInfo, std::vector<ValueInfo>> RefEdges;
80901095a5dSDimitry Andric   SmallPtrSet<const User *, 8> Visited;
810ac9a064cSDimitry Andric   bool RefLocalIFunc = false;
811ac9a064cSDimitry Andric   bool HasBlockAddress =
812ac9a064cSDimitry Andric       findRefEdges(Index, &V, RefEdges, Visited, RefLocalIFunc);
813ac9a064cSDimitry Andric   const bool NotEligibleForImport = (HasBlockAddress || RefLocalIFunc);
8147e7b6700SDimitry Andric   bool NonRenamableLocal = isNonRenamableLocal(V);
815344a3780SDimitry Andric   GlobalValueSummary::GVFlags Flags(
816344a3780SDimitry Andric       V.getLinkage(), V.getVisibility(), NonRenamableLocal,
817ac9a064cSDimitry Andric       /* Live = */ false, V.isDSOLocal(), V.canBeOmittedFromSymbolTable(),
818ac9a064cSDimitry Andric       GlobalValueSummary::Definition);
819d8e91e46SDimitry Andric 
820e6d15924SDimitry Andric   VTableFuncList VTableFuncs;
821e6d15924SDimitry Andric   // If splitting is not enabled, then we compute the summary information
822e6d15924SDimitry Andric   // necessary for index-based whole program devirtualization.
823e6d15924SDimitry Andric   if (!Index.enableSplitLTOUnit()) {
824e6d15924SDimitry Andric     Types.clear();
825e6d15924SDimitry Andric     V.getMetadata(LLVMContext::MD_type, Types);
826e6d15924SDimitry Andric     if (!Types.empty()) {
827e6d15924SDimitry Andric       // Identify the function pointers referenced by this vtable definition.
828e6d15924SDimitry Andric       computeVTableFuncs(Index, V, M, VTableFuncs);
829e6d15924SDimitry Andric 
830e6d15924SDimitry Andric       // Record this vtable definition for each type metadata it references.
831e6d15924SDimitry Andric       recordTypeIdCompatibleVtableReferences(Index, V, Types);
832e6d15924SDimitry Andric     }
833e6d15924SDimitry Andric   }
834e6d15924SDimitry Andric 
835e6d15924SDimitry Andric   // Don't mark variables we won't be able to internalize as read/write-only.
836e6d15924SDimitry Andric   bool CanBeInternalized =
837d8e91e46SDimitry Andric       !V.hasComdat() && !V.hasAppendingLinkage() && !V.isInterposable() &&
838e6d15924SDimitry Andric       !V.hasAvailableExternallyLinkage() && !V.hasDLLExportStorageClass();
839cfca06d7SDimitry Andric   bool Constant = V.isConstant();
840cfca06d7SDimitry Andric   GlobalVarSummary::GVarFlags VarFlags(CanBeInternalized,
841cfca06d7SDimitry Andric                                        Constant ? false : CanBeInternalized,
842cfca06d7SDimitry Andric                                        Constant, V.getVCallVisibility());
8431d5ae102SDimitry Andric   auto GVarSummary = std::make_unique<GlobalVarSummary>(Flags, VarFlags,
844d8e91e46SDimitry Andric                                                          RefEdges.takeVector());
8457e7b6700SDimitry Andric   if (NonRenamableLocal)
8467e7b6700SDimitry Andric     CantBePromoted.insert(V.getGUID());
847ac9a064cSDimitry Andric   if (NotEligibleForImport)
848d8e91e46SDimitry Andric     GVarSummary->setNotEligibleToImport();
849e6d15924SDimitry Andric   if (!VTableFuncs.empty())
850e6d15924SDimitry Andric     GVarSummary->setVTableFuncs(VTableFuncs);
851eb11fae6SDimitry Andric   Index.addGlobalValueSummary(V, std::move(GVarSummary));
85201095a5dSDimitry Andric }
85301095a5dSDimitry Andric 
computeAliasSummary(ModuleSummaryIndex & Index,const GlobalAlias & A,DenseSet<GlobalValue::GUID> & CantBePromoted)8544b4fe385SDimitry Andric static void computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A,
8557e7b6700SDimitry Andric                                 DenseSet<GlobalValue::GUID> &CantBePromoted) {
8564b4fe385SDimitry Andric   // Skip summary for indirect function aliases as summary for aliasee will not
8574b4fe385SDimitry Andric   // be emitted.
8584b4fe385SDimitry Andric   const GlobalObject *Aliasee = A.getAliaseeObject();
8594b4fe385SDimitry Andric   if (isa<GlobalIFunc>(Aliasee))
8604b4fe385SDimitry Andric     return;
8617e7b6700SDimitry Andric   bool NonRenamableLocal = isNonRenamableLocal(A);
862344a3780SDimitry Andric   GlobalValueSummary::GVFlags Flags(
863344a3780SDimitry Andric       A.getLinkage(), A.getVisibility(), NonRenamableLocal,
864ac9a064cSDimitry Andric       /* Live = */ false, A.isDSOLocal(), A.canBeOmittedFromSymbolTable(),
865ac9a064cSDimitry Andric       GlobalValueSummary::Definition);
8661d5ae102SDimitry Andric   auto AS = std::make_unique<AliasSummary>(Flags);
867e6d15924SDimitry Andric   auto AliaseeVI = Index.getValueInfo(Aliasee->getGUID());
868e6d15924SDimitry Andric   assert(AliaseeVI && "Alias expects aliasee summary to be available");
869e6d15924SDimitry Andric   assert(AliaseeVI.getSummaryList().size() == 1 &&
870e6d15924SDimitry Andric          "Expected a single entry per aliasee in per-module index");
871e6d15924SDimitry Andric   AS->setAliasee(AliaseeVI, AliaseeVI.getSummaryList()[0].get());
8727e7b6700SDimitry Andric   if (NonRenamableLocal)
8737e7b6700SDimitry Andric     CantBePromoted.insert(A.getGUID());
874eb11fae6SDimitry Andric   Index.addGlobalValueSummary(A, std::move(AS));
875b915e9e0SDimitry Andric }
876b915e9e0SDimitry Andric 
8777e7b6700SDimitry Andric // Set LiveRoot flag on entries matching the given value name.
setLiveRoot(ModuleSummaryIndex & Index,StringRef Name)8787e7b6700SDimitry Andric static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
879c46e6a59SDimitry Andric   if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
8804b4fe385SDimitry Andric     for (const auto &Summary : VI.getSummaryList())
881d288ef4cSDimitry Andric       Summary->setLive(true);
8827e7b6700SDimitry Andric }
8837e7b6700SDimitry Andric 
buildModuleSummaryIndex(const Module & M,std::function<BlockFrequencyInfo * (const Function & F)> GetBFICallback,ProfileSummaryInfo * PSI,std::function<const StackSafetyInfo * (const Function & F)> GetSSICallback)884b915e9e0SDimitry Andric ModuleSummaryIndex llvm::buildModuleSummaryIndex(
885b915e9e0SDimitry Andric     const Module &M,
886b915e9e0SDimitry Andric     std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
887cfca06d7SDimitry Andric     ProfileSummaryInfo *PSI,
888cfca06d7SDimitry Andric     std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) {
8896b3f41edSDimitry Andric   assert(PSI);
890d8e91e46SDimitry Andric   bool EnableSplitLTOUnit = false;
8917fa27ce4SDimitry Andric   bool UnifiedLTO = false;
892d8e91e46SDimitry Andric   if (auto *MD = mdconst::extract_or_null<ConstantInt>(
893d8e91e46SDimitry Andric           M.getModuleFlag("EnableSplitLTOUnit")))
894d8e91e46SDimitry Andric     EnableSplitLTOUnit = MD->getZExtValue();
8957fa27ce4SDimitry Andric   if (auto *MD =
8967fa27ce4SDimitry Andric           mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("UnifiedLTO")))
8977fa27ce4SDimitry Andric     UnifiedLTO = MD->getZExtValue();
8987fa27ce4SDimitry Andric   ModuleSummaryIndex Index(/*HaveGVs=*/true, EnableSplitLTOUnit, UnifiedLTO);
899b915e9e0SDimitry Andric 
900b915e9e0SDimitry Andric   // Identify the local values in the llvm.used and llvm.compiler.used sets,
901b915e9e0SDimitry Andric   // which should not be exported as they would then require renaming and
902b915e9e0SDimitry Andric   // promotion, but we may have opaque uses e.g. in inline asm. We collect them
903b915e9e0SDimitry Andric   // here because we use this information to mark functions containing inline
904b915e9e0SDimitry Andric   // assembly calls as not importable.
905344a3780SDimitry Andric   SmallPtrSet<GlobalValue *, 4> LocalsUsed;
906344a3780SDimitry Andric   SmallVector<GlobalValue *, 4> Used;
907b915e9e0SDimitry Andric   // First collect those in the llvm.used set.
908344a3780SDimitry Andric   collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/false);
909b915e9e0SDimitry Andric   // Next collect those in the llvm.compiler.used set.
910344a3780SDimitry Andric   collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/true);
9117e7b6700SDimitry Andric   DenseSet<GlobalValue::GUID> CantBePromoted;
912b915e9e0SDimitry Andric   for (auto *V : Used) {
9137e7b6700SDimitry Andric     if (V->hasLocalLinkage()) {
914b915e9e0SDimitry Andric       LocalsUsed.insert(V);
9157e7b6700SDimitry Andric       CantBePromoted.insert(V->getGUID());
9167e7b6700SDimitry Andric     }
917b915e9e0SDimitry Andric   }
91801095a5dSDimitry Andric 
919044eb2f6SDimitry Andric   bool HasLocalInlineAsmSymbol = false;
920044eb2f6SDimitry Andric   if (!M.getModuleInlineAsm().empty()) {
921044eb2f6SDimitry Andric     // Collect the local values defined by module level asm, and set up
922044eb2f6SDimitry Andric     // summaries for these symbols so that they can be marked as NoRename,
923044eb2f6SDimitry Andric     // to prevent export of any use of them in regular IR that would require
924044eb2f6SDimitry Andric     // renaming within the module level asm. Note we don't need to create a
925044eb2f6SDimitry Andric     // summary for weak or global defs, as they don't need to be flagged as
926044eb2f6SDimitry Andric     // NoRename, and defs in module level asm can't be imported anyway.
927044eb2f6SDimitry Andric     // Also, any values used but not defined within module level asm should
928044eb2f6SDimitry Andric     // be listed on the llvm.used or llvm.compiler.used global and marked as
929044eb2f6SDimitry Andric     // referenced from there.
930044eb2f6SDimitry Andric     ModuleSymbolTable::CollectAsmSymbols(
931044eb2f6SDimitry Andric         M, [&](StringRef Name, object::BasicSymbolRef::Flags Flags) {
932044eb2f6SDimitry Andric           // Symbols not marked as Weak or Global are local definitions.
933044eb2f6SDimitry Andric           if (Flags & (object::BasicSymbolRef::SF_Weak |
934044eb2f6SDimitry Andric                        object::BasicSymbolRef::SF_Global))
935044eb2f6SDimitry Andric             return;
936044eb2f6SDimitry Andric           HasLocalInlineAsmSymbol = true;
937044eb2f6SDimitry Andric           GlobalValue *GV = M.getNamedValue(Name);
938044eb2f6SDimitry Andric           if (!GV)
939044eb2f6SDimitry Andric             return;
940044eb2f6SDimitry Andric           assert(GV->isDeclaration() && "Def in module asm already has definition");
941344a3780SDimitry Andric           GlobalValueSummary::GVFlags GVFlags(
942344a3780SDimitry Andric               GlobalValue::InternalLinkage, GlobalValue::DefaultVisibility,
943044eb2f6SDimitry Andric               /* NotEligibleToImport = */ true,
944044eb2f6SDimitry Andric               /* Live = */ true,
945ac9a064cSDimitry Andric               /* Local */ GV->isDSOLocal(), GV->canBeOmittedFromSymbolTable(),
946ac9a064cSDimitry Andric               GlobalValueSummary::Definition);
947eb11fae6SDimitry Andric           CantBePromoted.insert(GV->getGUID());
948044eb2f6SDimitry Andric           // Create the appropriate summary type.
949044eb2f6SDimitry Andric           if (Function *F = dyn_cast<Function>(GV)) {
950044eb2f6SDimitry Andric             std::unique_ptr<FunctionSummary> Summary =
9511d5ae102SDimitry Andric                 std::make_unique<FunctionSummary>(
952d8e91e46SDimitry Andric                     GVFlags, /*InstCount=*/0,
953044eb2f6SDimitry Andric                     FunctionSummary::FFlags{
954044eb2f6SDimitry Andric                         F->hasFnAttribute(Attribute::ReadNone),
955044eb2f6SDimitry Andric                         F->hasFnAttribute(Attribute::ReadOnly),
956044eb2f6SDimitry Andric                         F->hasFnAttribute(Attribute::NoRecurse),
957d8e91e46SDimitry Andric                         F->returnDoesNotAlias(),
958706b4fc4SDimitry Andric                         /* NoInline = */ false,
959c0981da4SDimitry Andric                         F->hasFnAttribute(Attribute::AlwaysInline),
960c0981da4SDimitry Andric                         F->hasFnAttribute(Attribute::NoUnwind),
961c0981da4SDimitry Andric                         /* MayThrow */ true,
96277fc4c14SDimitry Andric                         /* HasUnknownCall */ true,
96377fc4c14SDimitry Andric                         /* MustBeUnreachable */ false},
964d8e91e46SDimitry Andric                     /*EntryCount=*/0, ArrayRef<ValueInfo>{},
965d8e91e46SDimitry Andric                     ArrayRef<FunctionSummary::EdgeTy>{},
966044eb2f6SDimitry Andric                     ArrayRef<GlobalValue::GUID>{},
967044eb2f6SDimitry Andric                     ArrayRef<FunctionSummary::VFuncId>{},
968044eb2f6SDimitry Andric                     ArrayRef<FunctionSummary::VFuncId>{},
969044eb2f6SDimitry Andric                     ArrayRef<FunctionSummary::ConstVCall>{},
970cfca06d7SDimitry Andric                     ArrayRef<FunctionSummary::ConstVCall>{},
971e3b55780SDimitry Andric                     ArrayRef<FunctionSummary::ParamAccess>{},
972e3b55780SDimitry Andric                     ArrayRef<CallsiteInfo>{}, ArrayRef<AllocInfo>{});
973eb11fae6SDimitry Andric             Index.addGlobalValueSummary(*GV, std::move(Summary));
974044eb2f6SDimitry Andric           } else {
975044eb2f6SDimitry Andric             std::unique_ptr<GlobalVarSummary> Summary =
9761d5ae102SDimitry Andric                 std::make_unique<GlobalVarSummary>(
977cfca06d7SDimitry Andric                     GVFlags,
978cfca06d7SDimitry Andric                     GlobalVarSummary::GVarFlags(
979cfca06d7SDimitry Andric                         false, false, cast<GlobalVariable>(GV)->isConstant(),
980cfca06d7SDimitry Andric                         GlobalObject::VCallVisibilityPublic),
981044eb2f6SDimitry Andric                     ArrayRef<ValueInfo>{});
982eb11fae6SDimitry Andric             Index.addGlobalValueSummary(*GV, std::move(Summary));
983044eb2f6SDimitry Andric           }
984044eb2f6SDimitry Andric         });
985044eb2f6SDimitry Andric   }
986044eb2f6SDimitry Andric 
987d8e91e46SDimitry Andric   bool IsThinLTO = true;
988d8e91e46SDimitry Andric   if (auto *MD =
989d8e91e46SDimitry Andric           mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
990d8e91e46SDimitry Andric     IsThinLTO = MD->getZExtValue();
991d8e91e46SDimitry Andric 
99201095a5dSDimitry Andric   // Compute summaries for all functions defined in module, and save in the
99301095a5dSDimitry Andric   // index.
9944b4fe385SDimitry Andric   for (const auto &F : M) {
99501095a5dSDimitry Andric     if (F.isDeclaration())
99601095a5dSDimitry Andric       continue;
99701095a5dSDimitry Andric 
998d8e91e46SDimitry Andric     DominatorTree DT(const_cast<Function &>(F));
99901095a5dSDimitry Andric     BlockFrequencyInfo *BFI = nullptr;
100001095a5dSDimitry Andric     std::unique_ptr<BlockFrequencyInfo> BFIPtr;
1001b915e9e0SDimitry Andric     if (GetBFICallback)
1002b915e9e0SDimitry Andric       BFI = GetBFICallback(F);
1003c7dac04cSDimitry Andric     else if (F.hasProfileData()) {
1004d8e91e46SDimitry Andric       LoopInfo LI{DT};
100501095a5dSDimitry Andric       BranchProbabilityInfo BPI{F, LI};
10061d5ae102SDimitry Andric       BFIPtr = std::make_unique<BlockFrequencyInfo>(F, BPI, LI);
100701095a5dSDimitry Andric       BFI = BFIPtr.get();
100801095a5dSDimitry Andric     }
100901095a5dSDimitry Andric 
1010d8e91e46SDimitry Andric     computeFunctionSummary(Index, M, F, BFI, PSI, DT,
1011044eb2f6SDimitry Andric                            !LocalsUsed.empty() || HasLocalInlineAsmSymbol,
1012cfca06d7SDimitry Andric                            CantBePromoted, IsThinLTO, GetSSICallback);
101301095a5dSDimitry Andric   }
101401095a5dSDimitry Andric 
101501095a5dSDimitry Andric   // Compute summaries for all variables defined in module, and save in the
101601095a5dSDimitry Andric   // index.
1017e6d15924SDimitry Andric   SmallVector<MDNode *, 2> Types;
1018b915e9e0SDimitry Andric   for (const GlobalVariable &G : M.globals()) {
101901095a5dSDimitry Andric     if (G.isDeclaration())
102001095a5dSDimitry Andric       continue;
1021e6d15924SDimitry Andric     computeVariableSummary(Index, G, CantBePromoted, M, Types);
102201095a5dSDimitry Andric   }
1023b915e9e0SDimitry Andric 
1024b915e9e0SDimitry Andric   // Compute summaries for all aliases defined in module, and save in the
1025b915e9e0SDimitry Andric   // index.
1026b915e9e0SDimitry Andric   for (const GlobalAlias &A : M.aliases())
10277e7b6700SDimitry Andric     computeAliasSummary(Index, A, CantBePromoted);
1028b915e9e0SDimitry Andric 
10294b4fe385SDimitry Andric   // Iterate through ifuncs, set their resolvers all alive.
10304b4fe385SDimitry Andric   for (const GlobalIFunc &I : M.ifuncs()) {
10314b4fe385SDimitry Andric     I.applyAlongResolverPath([&Index](const GlobalValue &GV) {
10324b4fe385SDimitry Andric       Index.getGlobalValueSummary(GV)->setLive(true);
10334b4fe385SDimitry Andric     });
10344b4fe385SDimitry Andric   }
10354b4fe385SDimitry Andric 
1036b915e9e0SDimitry Andric   for (auto *V : LocalsUsed) {
1037b915e9e0SDimitry Andric     auto *Summary = Index.getGlobalValueSummary(*V);
1038b915e9e0SDimitry Andric     assert(Summary && "Missing summary for global value");
10397e7b6700SDimitry Andric     Summary->setNotEligibleToImport();
1040b915e9e0SDimitry Andric   }
1041b915e9e0SDimitry Andric 
10427e7b6700SDimitry Andric   // The linker doesn't know about these LLVM produced values, so we need
10437e7b6700SDimitry Andric   // to flag them as live in the index to ensure index-based dead value
10447e7b6700SDimitry Andric   // analysis treats them as live roots of the analysis.
10457e7b6700SDimitry Andric   setLiveRoot(Index, "llvm.used");
10467e7b6700SDimitry Andric   setLiveRoot(Index, "llvm.compiler.used");
10477e7b6700SDimitry Andric   setLiveRoot(Index, "llvm.global_ctors");
10487e7b6700SDimitry Andric   setLiveRoot(Index, "llvm.global_dtors");
10497e7b6700SDimitry Andric   setLiveRoot(Index, "llvm.global.annotations");
10507e7b6700SDimitry Andric 
10517e7b6700SDimitry Andric   for (auto &GlobalList : Index) {
1052c46e6a59SDimitry Andric     // Ignore entries for references that are undefined in the current module.
1053c46e6a59SDimitry Andric     if (GlobalList.second.SummaryList.empty())
1054c46e6a59SDimitry Andric       continue;
1055c46e6a59SDimitry Andric 
1056c46e6a59SDimitry Andric     assert(GlobalList.second.SummaryList.size() == 1 &&
10577e7b6700SDimitry Andric            "Expected module's index to have one summary per GUID");
1058c46e6a59SDimitry Andric     auto &Summary = GlobalList.second.SummaryList[0];
10597ab83427SDimitry Andric     if (!IsThinLTO) {
10607ab83427SDimitry Andric       Summary->setNotEligibleToImport();
10617ab83427SDimitry Andric       continue;
10627ab83427SDimitry Andric     }
10637ab83427SDimitry Andric 
10647e7b6700SDimitry Andric     bool AllRefsCanBeExternallyReferenced =
10657e7b6700SDimitry Andric         llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) {
1066c46e6a59SDimitry Andric           return !CantBePromoted.count(VI.getGUID());
10677e7b6700SDimitry Andric         });
10687e7b6700SDimitry Andric     if (!AllRefsCanBeExternallyReferenced) {
10697e7b6700SDimitry Andric       Summary->setNotEligibleToImport();
10707e7b6700SDimitry Andric       continue;
10717e7b6700SDimitry Andric     }
10727e7b6700SDimitry Andric 
10737e7b6700SDimitry Andric     if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) {
10747e7b6700SDimitry Andric       bool AllCallsCanBeExternallyReferenced = llvm::all_of(
10757e7b6700SDimitry Andric           FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
1076c46e6a59SDimitry Andric             return !CantBePromoted.count(Edge.first.getGUID());
10777e7b6700SDimitry Andric           });
10787e7b6700SDimitry Andric       if (!AllCallsCanBeExternallyReferenced)
10797e7b6700SDimitry Andric         Summary->setNotEligibleToImport();
10807e7b6700SDimitry Andric     }
10817e7b6700SDimitry Andric   }
10827e7b6700SDimitry Andric 
1083e6d15924SDimitry Andric   if (!ModuleSummaryDotFile.empty()) {
1084e6d15924SDimitry Andric     std::error_code EC;
1085ac9a064cSDimitry Andric     raw_fd_ostream OSDot(ModuleSummaryDotFile, EC, sys::fs::OpenFlags::OF_Text);
1086e6d15924SDimitry Andric     if (EC)
1087e6d15924SDimitry Andric       report_fatal_error(Twine("Failed to open dot file ") +
1088e6d15924SDimitry Andric                          ModuleSummaryDotFile + ": " + EC.message() + "\n");
1089706b4fc4SDimitry Andric     Index.exportToDot(OSDot, {});
1090e6d15924SDimitry Andric   }
1091e6d15924SDimitry Andric 
1092b915e9e0SDimitry Andric   return Index;
1093b915e9e0SDimitry Andric }
1094b915e9e0SDimitry Andric 
1095b915e9e0SDimitry Andric AnalysisKey ModuleSummaryIndexAnalysis::Key;
1096b915e9e0SDimitry Andric 
1097b915e9e0SDimitry Andric ModuleSummaryIndex
run(Module & M,ModuleAnalysisManager & AM)1098b915e9e0SDimitry Andric ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
1099b915e9e0SDimitry Andric   ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
1100b915e9e0SDimitry Andric   auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
1101cfca06d7SDimitry Andric   bool NeedSSI = needsParamAccessSummary(M);
1102b915e9e0SDimitry Andric   return buildModuleSummaryIndex(
1103b915e9e0SDimitry Andric       M,
1104b915e9e0SDimitry Andric       [&FAM](const Function &F) {
1105b915e9e0SDimitry Andric         return &FAM.getResult<BlockFrequencyAnalysis>(
1106b915e9e0SDimitry Andric             *const_cast<Function *>(&F));
1107b915e9e0SDimitry Andric       },
1108cfca06d7SDimitry Andric       &PSI,
1109cfca06d7SDimitry Andric       [&FAM, NeedSSI](const Function &F) -> const StackSafetyInfo * {
1110cfca06d7SDimitry Andric         return NeedSSI ? &FAM.getResult<StackSafetyAnalysis>(
1111cfca06d7SDimitry Andric                              const_cast<Function &>(F))
1112cfca06d7SDimitry Andric                        : nullptr;
1113cfca06d7SDimitry Andric       });
111401095a5dSDimitry Andric }
111501095a5dSDimitry Andric 
111601095a5dSDimitry Andric char ModuleSummaryIndexWrapperPass::ID = 0;
1117044eb2f6SDimitry Andric 
111801095a5dSDimitry Andric INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
111901095a5dSDimitry Andric                       "Module Summary Analysis", false, true)
INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)112001095a5dSDimitry Andric INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
11217c71d32aSDimitry Andric INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
1122cfca06d7SDimitry Andric INITIALIZE_PASS_DEPENDENCY(StackSafetyInfoWrapperPass)
112301095a5dSDimitry Andric INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
112401095a5dSDimitry Andric                     "Module Summary Analysis", false, true)
112501095a5dSDimitry Andric 
112601095a5dSDimitry Andric ModulePass *llvm::createModuleSummaryIndexWrapperPass() {
112701095a5dSDimitry Andric   return new ModuleSummaryIndexWrapperPass();
112801095a5dSDimitry Andric }
112901095a5dSDimitry Andric 
ModuleSummaryIndexWrapperPass()113001095a5dSDimitry Andric ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass()
113101095a5dSDimitry Andric     : ModulePass(ID) {
113201095a5dSDimitry Andric   initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry());
113301095a5dSDimitry Andric }
113401095a5dSDimitry Andric 
runOnModule(Module & M)113501095a5dSDimitry Andric bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) {
1136d8e91e46SDimitry Andric   auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
1137cfca06d7SDimitry Andric   bool NeedSSI = needsParamAccessSummary(M);
1138eb11fae6SDimitry Andric   Index.emplace(buildModuleSummaryIndex(
1139b915e9e0SDimitry Andric       M,
1140b915e9e0SDimitry Andric       [this](const Function &F) {
114101095a5dSDimitry Andric         return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>(
114201095a5dSDimitry Andric                          *const_cast<Function *>(&F))
114301095a5dSDimitry Andric                      .getBFI());
1144b915e9e0SDimitry Andric       },
1145cfca06d7SDimitry Andric       PSI,
1146cfca06d7SDimitry Andric       [&](const Function &F) -> const StackSafetyInfo * {
1147cfca06d7SDimitry Andric         return NeedSSI ? &getAnalysis<StackSafetyInfoWrapperPass>(
1148cfca06d7SDimitry Andric                               const_cast<Function &>(F))
1149cfca06d7SDimitry Andric                               .getResult()
1150cfca06d7SDimitry Andric                        : nullptr;
1151cfca06d7SDimitry Andric       }));
115201095a5dSDimitry Andric   return false;
115301095a5dSDimitry Andric }
115401095a5dSDimitry Andric 
doFinalization(Module & M)115501095a5dSDimitry Andric bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) {
1156b915e9e0SDimitry Andric   Index.reset();
115701095a5dSDimitry Andric   return false;
115801095a5dSDimitry Andric }
115901095a5dSDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const116001095a5dSDimitry Andric void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
116101095a5dSDimitry Andric   AU.setPreservesAll();
116201095a5dSDimitry Andric   AU.addRequired<BlockFrequencyInfoWrapperPass>();
1163b915e9e0SDimitry Andric   AU.addRequired<ProfileSummaryInfoWrapperPass>();
1164cfca06d7SDimitry Andric   AU.addRequired<StackSafetyInfoWrapperPass>();
116501095a5dSDimitry Andric }
1166cfca06d7SDimitry Andric 
1167cfca06d7SDimitry Andric char ImmutableModuleSummaryIndexWrapperPass::ID = 0;
1168cfca06d7SDimitry Andric 
ImmutableModuleSummaryIndexWrapperPass(const ModuleSummaryIndex * Index)1169cfca06d7SDimitry Andric ImmutableModuleSummaryIndexWrapperPass::ImmutableModuleSummaryIndexWrapperPass(
1170cfca06d7SDimitry Andric     const ModuleSummaryIndex *Index)
1171cfca06d7SDimitry Andric     : ImmutablePass(ID), Index(Index) {
1172cfca06d7SDimitry Andric   initializeImmutableModuleSummaryIndexWrapperPassPass(
1173cfca06d7SDimitry Andric       *PassRegistry::getPassRegistry());
1174cfca06d7SDimitry Andric }
1175cfca06d7SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const1176cfca06d7SDimitry Andric void ImmutableModuleSummaryIndexWrapperPass::getAnalysisUsage(
1177cfca06d7SDimitry Andric     AnalysisUsage &AU) const {
1178cfca06d7SDimitry Andric   AU.setPreservesAll();
1179cfca06d7SDimitry Andric }
1180cfca06d7SDimitry Andric 
createImmutableModuleSummaryIndexWrapperPass(const ModuleSummaryIndex * Index)1181cfca06d7SDimitry Andric ImmutablePass *llvm::createImmutableModuleSummaryIndexWrapperPass(
1182cfca06d7SDimitry Andric     const ModuleSummaryIndex *Index) {
1183cfca06d7SDimitry Andric   return new ImmutableModuleSummaryIndexWrapperPass(Index);
1184cfca06d7SDimitry Andric }
1185cfca06d7SDimitry Andric 
1186cfca06d7SDimitry Andric INITIALIZE_PASS(ImmutableModuleSummaryIndexWrapperPass, "module-summary-info",
1187cfca06d7SDimitry Andric                 "Module summary info", false, true)
11887fa27ce4SDimitry Andric 
mayHaveMemprofSummary(const CallBase * CB)11897fa27ce4SDimitry Andric bool llvm::mayHaveMemprofSummary(const CallBase *CB) {
11907fa27ce4SDimitry Andric   if (!CB)
11917fa27ce4SDimitry Andric     return false;
11927fa27ce4SDimitry Andric   if (CB->isDebugOrPseudoInst())
11937fa27ce4SDimitry Andric     return false;
11947fa27ce4SDimitry Andric   auto *CI = dyn_cast<CallInst>(CB);
11957fa27ce4SDimitry Andric   auto *CalledValue = CB->getCalledOperand();
11967fa27ce4SDimitry Andric   auto *CalledFunction = CB->getCalledFunction();
11977fa27ce4SDimitry Andric   if (CalledValue && !CalledFunction) {
11987fa27ce4SDimitry Andric     CalledValue = CalledValue->stripPointerCasts();
11997fa27ce4SDimitry Andric     // Stripping pointer casts can reveal a called function.
12007fa27ce4SDimitry Andric     CalledFunction = dyn_cast<Function>(CalledValue);
12017fa27ce4SDimitry Andric   }
12027fa27ce4SDimitry Andric   // Check if this is an alias to a function. If so, get the
12037fa27ce4SDimitry Andric   // called aliasee for the checks below.
12047fa27ce4SDimitry Andric   if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
12057fa27ce4SDimitry Andric     assert(!CalledFunction &&
12067fa27ce4SDimitry Andric            "Expected null called function in callsite for alias");
12077fa27ce4SDimitry Andric     CalledFunction = dyn_cast<Function>(GA->getAliaseeObject());
12087fa27ce4SDimitry Andric   }
12097fa27ce4SDimitry Andric   // Check if this is a direct call to a known function or a known
12107fa27ce4SDimitry Andric   // intrinsic, or an indirect call with profile data.
12117fa27ce4SDimitry Andric   if (CalledFunction) {
12127fa27ce4SDimitry Andric     if (CI && CalledFunction->isIntrinsic())
12137fa27ce4SDimitry Andric       return false;
12147fa27ce4SDimitry Andric   } else {
12157fa27ce4SDimitry Andric     // TODO: For now skip indirect calls. See comments in
12167fa27ce4SDimitry Andric     // computeFunctionSummary for what is needed to handle this.
12177fa27ce4SDimitry Andric     return false;
12187fa27ce4SDimitry Andric   }
12197fa27ce4SDimitry Andric   return true;
12207fa27ce4SDimitry Andric }
1221