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