xref: /llvm-project/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp (revision e27b058de355e7af398c999db022ef11cf444b20)
1 //===- ModuleSummaryAnalysis.cpp - Module summary index builder -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass builds a ModuleSummaryIndex object for the module, to be written
11 // to bitcode or LLVM assembly.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
16 #include "llvm/ADT/MapVector.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Analysis/BlockFrequencyInfo.h"
20 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
21 #include "llvm/Analysis/BranchProbabilityInfo.h"
22 #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
23 #include "llvm/Analysis/LoopInfo.h"
24 #include "llvm/Analysis/ProfileSummaryInfo.h"
25 #include "llvm/Analysis/TypeMetadataUtils.h"
26 #include "llvm/IR/CallSite.h"
27 #include "llvm/IR/Dominators.h"
28 #include "llvm/IR/InstIterator.h"
29 #include "llvm/IR/IntrinsicInst.h"
30 #include "llvm/IR/ValueSymbolTable.h"
31 #include "llvm/Object/IRObjectFile.h"
32 #include "llvm/Pass.h"
33 using namespace llvm;
34 
35 #define DEBUG_TYPE "module-summary-analysis"
36 
37 // Walk through the operands of a given User via worklist iteration and populate
38 // the set of GlobalValue references encountered. Invoked either on an
39 // Instruction or a GlobalVariable (which walks its initializer).
40 static void findRefEdges(const User *CurUser, SetVector<ValueInfo> &RefEdges,
41                          SmallPtrSet<const User *, 8> &Visited) {
42   SmallVector<const User *, 32> Worklist;
43   Worklist.push_back(CurUser);
44 
45   while (!Worklist.empty()) {
46     const User *U = Worklist.pop_back_val();
47 
48     if (!Visited.insert(U).second)
49       continue;
50 
51     ImmutableCallSite CS(U);
52 
53     for (const auto &OI : U->operands()) {
54       const User *Operand = dyn_cast<User>(OI);
55       if (!Operand)
56         continue;
57       if (isa<BlockAddress>(Operand))
58         continue;
59       if (auto *GV = dyn_cast<GlobalValue>(Operand)) {
60         // We have a reference to a global value. This should be added to
61         // the reference set unless it is a callee. Callees are handled
62         // specially by WriteFunction and are added to a separate list.
63         if (!(CS && CS.isCallee(&OI)))
64           RefEdges.insert(GV);
65         continue;
66       }
67       Worklist.push_back(Operand);
68     }
69   }
70 }
71 
72 static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount,
73                                           ProfileSummaryInfo *PSI) {
74   if (!PSI)
75     return CalleeInfo::HotnessType::Unknown;
76   if (PSI->isHotCount(ProfileCount))
77     return CalleeInfo::HotnessType::Hot;
78   if (PSI->isColdCount(ProfileCount))
79     return CalleeInfo::HotnessType::Cold;
80   return CalleeInfo::HotnessType::None;
81 }
82 
83 static bool isNonRenamableLocal(const GlobalValue &GV) {
84   return GV.hasSection() && GV.hasLocalLinkage();
85 }
86 
87 static void
88 computeFunctionSummary(ModuleSummaryIndex &Index, const Module &M,
89                        const Function &F, BlockFrequencyInfo *BFI,
90                        ProfileSummaryInfo *PSI, bool HasLocalsInUsed,
91                        DenseSet<GlobalValue::GUID> &CantBePromoted) {
92   // Summary not currently supported for anonymous functions, they should
93   // have been named.
94   assert(F.hasName());
95 
96   unsigned NumInsts = 0;
97   // Map from callee ValueId to profile count. Used to accumulate profile
98   // counts for all static calls to a given callee.
99   MapVector<ValueInfo, CalleeInfo> CallGraphEdges;
100   SetVector<ValueInfo> RefEdges;
101   SetVector<GlobalValue::GUID> TypeTests;
102   ICallPromotionAnalysis ICallAnalysis;
103 
104   bool HasInlineAsmMaybeReferencingInternal = false;
105   SmallPtrSet<const User *, 8> Visited;
106   for (const BasicBlock &BB : F)
107     for (const Instruction &I : BB) {
108       if (isa<DbgInfoIntrinsic>(I))
109         continue;
110       ++NumInsts;
111       findRefEdges(&I, RefEdges, Visited);
112       auto CS = ImmutableCallSite(&I);
113       if (!CS)
114         continue;
115 
116       const auto *CI = dyn_cast<CallInst>(&I);
117       // Since we don't know exactly which local values are referenced in inline
118       // assembly, conservatively mark the function as possibly referencing
119       // a local value from inline assembly to ensure we don't export a
120       // reference (which would require renaming and promotion of the
121       // referenced value).
122       if (HasLocalsInUsed && CI && CI->isInlineAsm())
123         HasInlineAsmMaybeReferencingInternal = true;
124 
125       auto *CalledValue = CS.getCalledValue();
126       auto *CalledFunction = CS.getCalledFunction();
127       // Check if this is an alias to a function. If so, get the
128       // called aliasee for the checks below.
129       if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
130         assert(!CalledFunction && "Expected null called function in callsite for alias");
131         CalledFunction = dyn_cast<Function>(GA->getBaseObject());
132       }
133       // Check if this is a direct call to a known function or a known
134       // intrinsic, or an indirect call with profile data.
135       if (CalledFunction) {
136         if (CalledFunction->isIntrinsic()) {
137           if (CalledFunction->getIntrinsicID() != Intrinsic::type_test)
138             continue;
139           // Produce a summary from type.test intrinsics. We only summarize
140           // type.test intrinsics that are used other than by an llvm.assume
141           // intrinsic. Intrinsics that are assumed are relevant only to the
142           // devirtualization pass, not the type test lowering pass.
143           bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) {
144             auto *AssumeCI = dyn_cast<CallInst>(CIU.getUser());
145             if (!AssumeCI)
146               return true;
147             Function *F = AssumeCI->getCalledFunction();
148             return !F || F->getIntrinsicID() != Intrinsic::assume;
149           });
150           if (HasNonAssumeUses) {
151             auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1));
152             if (auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata()))
153               TypeTests.insert(GlobalValue::getGUID(TypeId->getString()));
154           }
155         }
156         // We should have named any anonymous globals
157         assert(CalledFunction->hasName());
158         auto ScaledCount = BFI ? BFI->getBlockProfileCount(&BB) : None;
159         auto Hotness = ScaledCount ? getHotness(ScaledCount.getValue(), PSI)
160                                    : CalleeInfo::HotnessType::Unknown;
161 
162         // Use the original CalledValue, in case it was an alias. We want
163         // to record the call edge to the alias in that case. Eventually
164         // an alias summary will be created to associate the alias and
165         // aliasee.
166         CallGraphEdges[cast<GlobalValue>(CalledValue)].updateHotness(Hotness);
167       } else {
168         // Skip inline assembly calls.
169         if (CI && CI->isInlineAsm())
170           continue;
171         // Skip direct calls.
172         if (!CS.getCalledValue() || isa<Constant>(CS.getCalledValue()))
173           continue;
174 
175         uint32_t NumVals, NumCandidates;
176         uint64_t TotalCount;
177         auto CandidateProfileData =
178             ICallAnalysis.getPromotionCandidatesForInstruction(
179                 &I, NumVals, TotalCount, NumCandidates);
180         for (auto &Candidate : CandidateProfileData)
181           CallGraphEdges[Candidate.Value].updateHotness(
182               getHotness(Candidate.Count, PSI));
183       }
184     }
185 
186   bool NonRenamableLocal = isNonRenamableLocal(F);
187   bool NotEligibleForImport =
188       NonRenamableLocal || HasInlineAsmMaybeReferencingInternal ||
189       // Inliner doesn't handle variadic functions.
190       // FIXME: refactor this to use the same code that inliner is using.
191       F.isVarArg();
192   GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport);
193   auto FuncSummary = llvm::make_unique<FunctionSummary>(
194       Flags, NumInsts, RefEdges.takeVector(), CallGraphEdges.takeVector(),
195       TypeTests.takeVector());
196   if (NonRenamableLocal)
197     CantBePromoted.insert(F.getGUID());
198   Index.addGlobalValueSummary(F.getName(), std::move(FuncSummary));
199 }
200 
201 static void
202 computeVariableSummary(ModuleSummaryIndex &Index, const GlobalVariable &V,
203                        DenseSet<GlobalValue::GUID> &CantBePromoted) {
204   SetVector<ValueInfo> RefEdges;
205   SmallPtrSet<const User *, 8> Visited;
206   findRefEdges(&V, RefEdges, Visited);
207   bool NonRenamableLocal = isNonRenamableLocal(V);
208   GlobalValueSummary::GVFlags Flags(V.getLinkage(), NonRenamableLocal);
209   auto GVarSummary =
210       llvm::make_unique<GlobalVarSummary>(Flags, RefEdges.takeVector());
211   if (NonRenamableLocal)
212     CantBePromoted.insert(V.getGUID());
213   Index.addGlobalValueSummary(V.getName(), std::move(GVarSummary));
214 }
215 
216 static void
217 computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A,
218                     DenseSet<GlobalValue::GUID> &CantBePromoted) {
219   bool NonRenamableLocal = isNonRenamableLocal(A);
220   GlobalValueSummary::GVFlags Flags(A.getLinkage(), NonRenamableLocal);
221   auto AS = llvm::make_unique<AliasSummary>(Flags, ArrayRef<ValueInfo>{});
222   auto *Aliasee = A.getBaseObject();
223   auto *AliaseeSummary = Index.getGlobalValueSummary(*Aliasee);
224   assert(AliaseeSummary && "Alias expects aliasee summary to be parsed");
225   AS->setAliasee(AliaseeSummary);
226   if (NonRenamableLocal)
227     CantBePromoted.insert(A.getGUID());
228   Index.addGlobalValueSummary(A.getName(), std::move(AS));
229 }
230 
231 ModuleSummaryIndex llvm::buildModuleSummaryIndex(
232     const Module &M,
233     std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
234     ProfileSummaryInfo *PSI) {
235   ModuleSummaryIndex Index;
236 
237   // Identify the local values in the llvm.used and llvm.compiler.used sets,
238   // which should not be exported as they would then require renaming and
239   // promotion, but we may have opaque uses e.g. in inline asm. We collect them
240   // here because we use this information to mark functions containing inline
241   // assembly calls as not importable.
242   SmallPtrSet<GlobalValue *, 8> LocalsUsed;
243   SmallPtrSet<GlobalValue *, 8> Used;
244   // First collect those in the llvm.used set.
245   collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
246   // Next collect those in the llvm.compiler.used set.
247   collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
248   DenseSet<GlobalValue::GUID> CantBePromoted;
249   for (auto *V : Used) {
250     if (V->hasLocalLinkage()) {
251       LocalsUsed.insert(V);
252       CantBePromoted.insert(V->getGUID());
253     }
254   }
255 
256   // Compute summaries for all functions defined in module, and save in the
257   // index.
258   for (auto &F : M) {
259     if (F.isDeclaration())
260       continue;
261 
262     BlockFrequencyInfo *BFI = nullptr;
263     std::unique_ptr<BlockFrequencyInfo> BFIPtr;
264     if (GetBFICallback)
265       BFI = GetBFICallback(F);
266     else if (F.getEntryCount().hasValue()) {
267       LoopInfo LI{DominatorTree(const_cast<Function &>(F))};
268       BranchProbabilityInfo BPI{F, LI};
269       BFIPtr = llvm::make_unique<BlockFrequencyInfo>(F, BPI, LI);
270       BFI = BFIPtr.get();
271     }
272 
273     computeFunctionSummary(Index, M, F, BFI, PSI, !LocalsUsed.empty(),
274                            CantBePromoted);
275   }
276 
277   // Compute summaries for all variables defined in module, and save in the
278   // index.
279   for (const GlobalVariable &G : M.globals()) {
280     if (G.isDeclaration())
281       continue;
282     computeVariableSummary(Index, G, CantBePromoted);
283   }
284 
285   // Compute summaries for all aliases defined in module, and save in the
286   // index.
287   for (const GlobalAlias &A : M.aliases())
288     computeAliasSummary(Index, A, CantBePromoted);
289 
290   for (auto *V : LocalsUsed) {
291     auto *Summary = Index.getGlobalValueSummary(*V);
292     assert(Summary && "Missing summary for global value");
293     Summary->setNotEligibleToImport();
294   }
295 
296   if (!M.getModuleInlineAsm().empty()) {
297     // Collect the local values defined by module level asm, and set up
298     // summaries for these symbols so that they can be marked as NoRename,
299     // to prevent export of any use of them in regular IR that would require
300     // renaming within the module level asm. Note we don't need to create a
301     // summary for weak or global defs, as they don't need to be flagged as
302     // NoRename, and defs in module level asm can't be imported anyway.
303     // Also, any values used but not defined within module level asm should
304     // be listed on the llvm.used or llvm.compiler.used global and marked as
305     // referenced from there.
306     ModuleSymbolTable::CollectAsmSymbols(
307         Triple(M.getTargetTriple()), M.getModuleInlineAsm(),
308         [&M, &Index, &CantBePromoted](StringRef Name,
309                                       object::BasicSymbolRef::Flags Flags) {
310           // Symbols not marked as Weak or Global are local definitions.
311           if (Flags & (object::BasicSymbolRef::SF_Weak |
312                        object::BasicSymbolRef::SF_Global))
313             return;
314           GlobalValue *GV = M.getNamedValue(Name);
315           if (!GV)
316             return;
317           assert(GV->isDeclaration() && "Def in module asm already has definition");
318           GlobalValueSummary::GVFlags GVFlags(GlobalValue::InternalLinkage,
319                                               /* NotEligibleToImport */ true);
320           CantBePromoted.insert(GlobalValue::getGUID(Name));
321           // Create the appropriate summary type.
322           if (isa<Function>(GV)) {
323             std::unique_ptr<FunctionSummary> Summary =
324                 llvm::make_unique<FunctionSummary>(
325                     GVFlags, 0, ArrayRef<ValueInfo>{},
326                     ArrayRef<FunctionSummary::EdgeTy>{},
327                     ArrayRef<GlobalValue::GUID>{});
328             Index.addGlobalValueSummary(Name, std::move(Summary));
329           } else {
330             std::unique_ptr<GlobalVarSummary> Summary =
331                 llvm::make_unique<GlobalVarSummary>(GVFlags,
332                                                     ArrayRef<ValueInfo>{});
333             Index.addGlobalValueSummary(Name, std::move(Summary));
334           }
335         });
336   }
337 
338   for (auto &GlobalList : Index) {
339     assert(GlobalList.second.size() == 1 &&
340            "Expected module's index to have one summary per GUID");
341     auto &Summary = GlobalList.second[0];
342     bool AllRefsCanBeExternallyReferenced =
343         llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) {
344           return !CantBePromoted.count(VI.getValue()->getGUID());
345         });
346     if (!AllRefsCanBeExternallyReferenced) {
347       Summary->setNotEligibleToImport();
348       continue;
349     }
350 
351     if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) {
352       bool AllCallsCanBeExternallyReferenced = llvm::all_of(
353           FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
354             auto GUID = Edge.first.isGUID() ? Edge.first.getGUID()
355                                             : Edge.first.getValue()->getGUID();
356             return !CantBePromoted.count(GUID);
357           });
358       if (!AllCallsCanBeExternallyReferenced)
359         Summary->setNotEligibleToImport();
360     }
361   }
362 
363   return Index;
364 }
365 
366 AnalysisKey ModuleSummaryIndexAnalysis::Key;
367 
368 ModuleSummaryIndex
369 ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
370   ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
371   auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
372   return buildModuleSummaryIndex(
373       M,
374       [&FAM](const Function &F) {
375         return &FAM.getResult<BlockFrequencyAnalysis>(
376             *const_cast<Function *>(&F));
377       },
378       &PSI);
379 }
380 
381 char ModuleSummaryIndexWrapperPass::ID = 0;
382 INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
383                       "Module Summary Analysis", false, true)
384 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
385 INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
386                     "Module Summary Analysis", false, true)
387 
388 ModulePass *llvm::createModuleSummaryIndexWrapperPass() {
389   return new ModuleSummaryIndexWrapperPass();
390 }
391 
392 ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass()
393     : ModulePass(ID) {
394   initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry());
395 }
396 
397 bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) {
398   auto &PSI = *getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
399   Index = buildModuleSummaryIndex(
400       M,
401       [this](const Function &F) {
402         return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>(
403                          *const_cast<Function *>(&F))
404                      .getBFI());
405       },
406       &PSI);
407   return false;
408 }
409 
410 bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) {
411   Index.reset();
412   return false;
413 }
414 
415 void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
416   AU.setPreservesAll();
417   AU.addRequired<BlockFrequencyInfoWrapperPass>();
418   AU.addRequired<ProfileSummaryInfoWrapperPass>();
419 }
420