LLVM  9.0.0svn
ModuleSummaryAnalysis.cpp
Go to the documentation of this file.
1 //===- ModuleSummaryAnalysis.cpp - Module summary index builder -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass builds a ModuleSummaryIndex object for the module, to be written
10 // to bitcode or LLVM assembly.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/MapVector.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
26 #include "llvm/Analysis/LoopInfo.h"
29 #include "llvm/IR/Attributes.h"
30 #include "llvm/IR/BasicBlock.h"
31 #include "llvm/IR/CallSite.h"
32 #include "llvm/IR/Constant.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/Dominators.h"
35 #include "llvm/IR/Function.h"
36 #include "llvm/IR/GlobalAlias.h"
37 #include "llvm/IR/GlobalValue.h"
38 #include "llvm/IR/GlobalVariable.h"
39 #include "llvm/IR/Instructions.h"
40 #include "llvm/IR/IntrinsicInst.h"
41 #include "llvm/IR/Intrinsics.h"
42 #include "llvm/IR/Metadata.h"
43 #include "llvm/IR/Module.h"
45 #include "llvm/IR/Use.h"
46 #include "llvm/IR/User.h"
49 #include "llvm/Pass.h"
50 #include "llvm/Support/Casting.h"
52 #include <algorithm>
53 #include <cassert>
54 #include <cstdint>
55 #include <vector>
56 
57 using namespace llvm;
58 
59 #define DEBUG_TYPE "module-summary-analysis"
60 
61 // Option to force edges cold which will block importing when the
62 // -import-cold-multiplier is set to 0. Useful for debugging.
66  "force-summary-edges-cold", cl::Hidden, cl::location(ForceSummaryEdgesCold),
67  cl::desc("Force all edges in the function summary to cold"),
70  "all-non-critical", "All non-critical edges."),
71  clEnumValN(FunctionSummary::FSHT_All, "all", "All edges.")));
72 
73 // Walk through the operands of a given User via worklist iteration and populate
74 // the set of GlobalValue references encountered. Invoked either on an
75 // Instruction or a GlobalVariable (which walks its initializer).
76 // Return true if any of the operands contains blockaddress. This is important
77 // to know when computing summary for global var, because if global variable
78 // references basic block address we can't import it separately from function
79 // containing that basic block. For simplicity we currently don't import such
80 // global vars at all. When importing function we aren't interested if any
81 // instruction in it takes an address of any basic block, because instruction
82 // can only take an address of basic block located in the same function.
83 static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,
84  SetVector<ValueInfo> &RefEdges,
86  bool HasBlockAddress = false;
88  Worklist.push_back(CurUser);
89 
90  while (!Worklist.empty()) {
91  const User *U = Worklist.pop_back_val();
92 
93  if (!Visited.insert(U).second)
94  continue;
95 
96  ImmutableCallSite CS(U);
97 
98  for (const auto &OI : U->operands()) {
99  const User *Operand = dyn_cast<User>(OI);
100  if (!Operand)
101  continue;
102  if (isa<BlockAddress>(Operand)) {
103  HasBlockAddress = true;
104  continue;
105  }
106  if (auto *GV = dyn_cast<GlobalValue>(Operand)) {
107  // We have a reference to a global value. This should be added to
108  // the reference set unless it is a callee. Callees are handled
109  // specially by WriteFunction and are added to a separate list.
110  if (!(CS && CS.isCallee(&OI)))
111  RefEdges.insert(Index.getOrInsertValueInfo(GV));
112  continue;
113  }
114  Worklist.push_back(Operand);
115  }
116  }
117  return HasBlockAddress;
118 }
119 
121  ProfileSummaryInfo *PSI) {
122  if (!PSI)
124  if (PSI->isHotCount(ProfileCount))
126  if (PSI->isColdCount(ProfileCount))
129 }
130 
131 static bool isNonRenamableLocal(const GlobalValue &GV) {
132  return GV.hasSection() && GV.hasLocalLinkage();
133 }
134 
135 /// Determine whether this call has all constant integer arguments (excluding
136 /// "this") and summarize it to VCalls or ConstVCalls as appropriate.
140  std::vector<uint64_t> Args;
141  // Start from the second argument to skip the "this" pointer.
142  for (auto &Arg : make_range(Call.CS.arg_begin() + 1, Call.CS.arg_end())) {
143  auto *CI = dyn_cast<ConstantInt>(Arg);
144  if (!CI || CI->getBitWidth() > 64) {
145  VCalls.insert({Guid, Call.Offset});
146  return;
147  }
148  Args.push_back(CI->getZExtValue());
149  }
150  ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)});
151 }
152 
153 /// If this intrinsic call requires that we add information to the function
154 /// summary, do so via the non-constant reference arguments.
156  const CallInst *CI, SetVector<GlobalValue::GUID> &TypeTests,
157  SetVector<FunctionSummary::VFuncId> &TypeTestAssumeVCalls,
158  SetVector<FunctionSummary::VFuncId> &TypeCheckedLoadVCalls,
159  SetVector<FunctionSummary::ConstVCall> &TypeTestAssumeConstVCalls,
160  SetVector<FunctionSummary::ConstVCall> &TypeCheckedLoadConstVCalls,
161  DominatorTree &DT) {
162  switch (CI->getCalledFunction()->getIntrinsicID()) {
163  case Intrinsic::type_test: {
164  auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1));
165  auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
166  if (!TypeId)
167  break;
168  GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
169 
170  // Produce a summary from type.test intrinsics. We only summarize type.test
171  // intrinsics that are used other than by an llvm.assume intrinsic.
172  // Intrinsics that are assumed are relevant only to the devirtualization
173  // pass, not the type test lowering pass.
174  bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) {
175  auto *AssumeCI = dyn_cast<CallInst>(CIU.getUser());
176  if (!AssumeCI)
177  return true;
178  Function *F = AssumeCI->getCalledFunction();
179  return !F || F->getIntrinsicID() != Intrinsic::assume;
180  });
181  if (HasNonAssumeUses)
182  TypeTests.insert(Guid);
183 
184  SmallVector<DevirtCallSite, 4> DevirtCalls;
186  findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT);
187  for (auto &Call : DevirtCalls)
188  addVCallToSet(Call, Guid, TypeTestAssumeVCalls,
189  TypeTestAssumeConstVCalls);
190 
191  break;
192  }
193 
194  case Intrinsic::type_checked_load: {
195  auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2));
196  auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
197  if (!TypeId)
198  break;
199  GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
200 
201  SmallVector<DevirtCallSite, 4> DevirtCalls;
204  bool HasNonCallUses = false;
205  findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
206  HasNonCallUses, CI, DT);
207  // Any non-call uses of the result of llvm.type.checked.load will
208  // prevent us from optimizing away the llvm.type.test.
209  if (HasNonCallUses)
210  TypeTests.insert(Guid);
211  for (auto &Call : DevirtCalls)
212  addVCallToSet(Call, Guid, TypeCheckedLoadVCalls,
213  TypeCheckedLoadConstVCalls);
214 
215  break;
216  }
217  default:
218  break;
219  }
220 }
221 
222 static bool isNonVolatileLoad(const Instruction *I) {
223  if (const auto *LI = dyn_cast<LoadInst>(I))
224  return !LI->isVolatile();
225 
226  return false;
227 }
228 
230  const Function &F, BlockFrequencyInfo *BFI,
232  bool HasLocalsInUsedOrAsm,
233  DenseSet<GlobalValue::GUID> &CantBePromoted,
234  bool IsThinLTO) {
235  // Summary not currently supported for anonymous functions, they should
236  // have been named.
237  assert(F.hasName());
238 
239  unsigned NumInsts = 0;
240  // Map from callee ValueId to profile count. Used to accumulate profile
241  // counts for all static calls to a given callee.
242  MapVector<ValueInfo, CalleeInfo> CallGraphEdges;
243  SetVector<ValueInfo> RefEdges;
245  SetVector<FunctionSummary::VFuncId> TypeTestAssumeVCalls,
246  TypeCheckedLoadVCalls;
247  SetVector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls,
248  TypeCheckedLoadConstVCalls;
249  ICallPromotionAnalysis ICallAnalysis;
251 
252  // Add personality function, prefix data and prologue data to function's ref
253  // list.
254  findRefEdges(Index, &F, RefEdges, Visited);
255  std::vector<const Instruction *> NonVolatileLoads;
256 
257  bool HasInlineAsmMaybeReferencingInternal = false;
258  for (const BasicBlock &BB : F)
259  for (const Instruction &I : BB) {
260  if (isa<DbgInfoIntrinsic>(I))
261  continue;
262  ++NumInsts;
263  if (isNonVolatileLoad(&I)) {
264  // Postpone processing of non-volatile load instructions
265  // See comments below
266  Visited.insert(&I);
267  NonVolatileLoads.push_back(&I);
268  continue;
269  }
270  findRefEdges(Index, &I, RefEdges, Visited);
271  auto CS = ImmutableCallSite(&I);
272  if (!CS)
273  continue;
274 
275  const auto *CI = dyn_cast<CallInst>(&I);
276  // Since we don't know exactly which local values are referenced in inline
277  // assembly, conservatively mark the function as possibly referencing
278  // a local value from inline assembly to ensure we don't export a
279  // reference (which would require renaming and promotion of the
280  // referenced value).
281  if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm())
282  HasInlineAsmMaybeReferencingInternal = true;
283 
284  auto *CalledValue = CS.getCalledValue();
285  auto *CalledFunction = CS.getCalledFunction();
286  if (CalledValue && !CalledFunction) {
287  CalledValue = CalledValue->stripPointerCastsNoFollowAliases();
288  // Stripping pointer casts can reveal a called function.
289  CalledFunction = dyn_cast<Function>(CalledValue);
290  }
291  // Check if this is an alias to a function. If so, get the
292  // called aliasee for the checks below.
293  if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
294  assert(!CalledFunction && "Expected null called function in callsite for alias");
295  CalledFunction = dyn_cast<Function>(GA->getBaseObject());
296  }
297  // Check if this is a direct call to a known function or a known
298  // intrinsic, or an indirect call with profile data.
299  if (CalledFunction) {
300  if (CI && CalledFunction->isIntrinsic()) {
302  CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls,
303  TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls, DT);
304  continue;
305  }
306  // We should have named any anonymous globals
307  assert(CalledFunction->hasName());
308  auto ScaledCount = PSI->getProfileCount(&I, BFI);
309  auto Hotness = ScaledCount ? getHotness(ScaledCount.getValue(), PSI)
313 
314  // Use the original CalledValue, in case it was an alias. We want
315  // to record the call edge to the alias in that case. Eventually
316  // an alias summary will be created to associate the alias and
317  // aliasee.
318  auto &ValueInfo = CallGraphEdges[Index.getOrInsertValueInfo(
319  cast<GlobalValue>(CalledValue))];
320  ValueInfo.updateHotness(Hotness);
321  // Add the relative block frequency to CalleeInfo if there is no profile
322  // information.
323  if (BFI != nullptr && Hotness == CalleeInfo::HotnessType::Unknown) {
324  uint64_t BBFreq = BFI->getBlockFreq(&BB).getFrequency();
325  uint64_t EntryFreq = BFI->getEntryFreq();
326  ValueInfo.updateRelBlockFreq(BBFreq, EntryFreq);
327  }
328  } else {
329  // Skip inline assembly calls.
330  if (CI && CI->isInlineAsm())
331  continue;
332  // Skip direct calls.
333  if (!CalledValue || isa<Constant>(CalledValue))
334  continue;
335 
336  // Check if the instruction has a callees metadata. If so, add callees
337  // to CallGraphEdges to reflect the references from the metadata, and
338  // to enable importing for subsequent indirect call promotion and
339  // inlining.
340  if (auto *MD = I.getMetadata(LLVMContext::MD_callees)) {
341  for (auto &Op : MD->operands()) {
342  Function *Callee = mdconst::extract_or_null<Function>(Op);
343  if (Callee)
344  CallGraphEdges[Index.getOrInsertValueInfo(Callee)];
345  }
346  }
347 
348  uint32_t NumVals, NumCandidates;
349  uint64_t TotalCount;
350  auto CandidateProfileData =
352  &I, NumVals, TotalCount, NumCandidates);
353  for (auto &Candidate : CandidateProfileData)
354  CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)]
355  .updateHotness(getHotness(Candidate.Count, PSI));
356  }
357  }
358 
359  // By now we processed all instructions in a function, except
360  // non-volatile loads. All new refs we add in a loop below
361  // are obviously constant. All constant refs are grouped in the
362  // end of RefEdges vector, so we can use a single integer value
363  // to identify them.
364  unsigned RefCnt = RefEdges.size();
365  for (const Instruction *I : NonVolatileLoads) {
366  Visited.erase(I);
367  findRefEdges(Index, I, RefEdges, Visited);
368  }
369  std::vector<ValueInfo> Refs = RefEdges.takeVector();
370  // Regular LTO module doesn't participate in ThinLTO import,
371  // so no reference from it can be readonly, since this would
372  // require importing variable as local copy
373  if (IsThinLTO)
374  for (; RefCnt < Refs.size(); ++RefCnt)
375  Refs[RefCnt].setReadOnly();
376 
377  // Explicit add hot edges to enforce importing for designated GUIDs for
378  // sample PGO, to enable the same inlines as the profiled optimized binary.
379  for (auto &I : F.getImportGUIDs())
380  CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness(
384 
385  bool NonRenamableLocal = isNonRenamableLocal(F);
386  bool NotEligibleForImport =
387  NonRenamableLocal || HasInlineAsmMaybeReferencingInternal;
388  GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport,
389  /* Live = */ false, F.isDSOLocal());
390  FunctionSummary::FFlags FunFlags{
391  F.hasFnAttribute(Attribute::ReadNone),
392  F.hasFnAttribute(Attribute::ReadOnly),
393  F.hasFnAttribute(Attribute::NoRecurse), F.returnDoesNotAlias(),
394  // FIXME: refactor this to use the same code that inliner is using.
395  // Don't try to import functions with noinline attribute.
396  F.getAttributes().hasFnAttribute(Attribute::NoInline)};
397  auto FuncSummary = llvm::make_unique<FunctionSummary>(
398  Flags, NumInsts, FunFlags, /*EntryCount=*/0, std::move(Refs),
399  CallGraphEdges.takeVector(), TypeTests.takeVector(),
400  TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(),
401  TypeTestAssumeConstVCalls.takeVector(),
402  TypeCheckedLoadConstVCalls.takeVector());
403  if (NonRenamableLocal)
404  CantBePromoted.insert(F.getGUID());
405  Index.addGlobalValueSummary(F, std::move(FuncSummary));
406 }
407 
408 static void
410  DenseSet<GlobalValue::GUID> &CantBePromoted) {
411  SetVector<ValueInfo> RefEdges;
413  bool HasBlockAddress = findRefEdges(Index, &V, RefEdges, Visited);
414  bool NonRenamableLocal = isNonRenamableLocal(V);
415  GlobalValueSummary::GVFlags Flags(V.getLinkage(), NonRenamableLocal,
416  /* Live = */ false, V.isDSOLocal());
417 
418  // Don't mark variables we won't be able to internalize as read-only.
420  !V.hasComdat() && !V.hasAppendingLinkage() && !V.isInterposable() &&
422  auto GVarSummary = llvm::make_unique<GlobalVarSummary>(Flags, VarFlags,
423  RefEdges.takeVector());
424  if (NonRenamableLocal)
425  CantBePromoted.insert(V.getGUID());
426  if (HasBlockAddress)
427  GVarSummary->setNotEligibleToImport();
428  Index.addGlobalValueSummary(V, std::move(GVarSummary));
429 }
430 
431 static void
433  DenseSet<GlobalValue::GUID> &CantBePromoted) {
434  bool NonRenamableLocal = isNonRenamableLocal(A);
435  GlobalValueSummary::GVFlags Flags(A.getLinkage(), NonRenamableLocal,
436  /* Live = */ false, A.isDSOLocal());
437  auto AS = llvm::make_unique<AliasSummary>(Flags);
438  auto *Aliasee = A.getBaseObject();
439  auto *AliaseeSummary = Index.getGlobalValueSummary(*Aliasee);
440  assert(AliaseeSummary && "Alias expects aliasee summary to be parsed");
441  AS->setAliasee(AliaseeSummary);
442  if (NonRenamableLocal)
443  CantBePromoted.insert(A.getGUID());
444  Index.addGlobalValueSummary(A, std::move(AS));
445 }
446 
447 // Set LiveRoot flag on entries matching the given value name.
449  if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
450  for (auto &Summary : VI.getSummaryList())
451  Summary->setLive(true);
452 }
453 
455  const Module &M,
456  std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
457  ProfileSummaryInfo *PSI) {
458  assert(PSI);
459  bool EnableSplitLTOUnit = false;
460  if (auto *MD = mdconst::extract_or_null<ConstantInt>(
461  M.getModuleFlag("EnableSplitLTOUnit")))
462  EnableSplitLTOUnit = MD->getZExtValue();
463  ModuleSummaryIndex Index(/*HaveGVs=*/true, EnableSplitLTOUnit);
464 
465  // Identify the local values in the llvm.used and llvm.compiler.used sets,
466  // which should not be exported as they would then require renaming and
467  // promotion, but we may have opaque uses e.g. in inline asm. We collect them
468  // here because we use this information to mark functions containing inline
469  // assembly calls as not importable.
472  // First collect those in the llvm.used set.
473  collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
474  // Next collect those in the llvm.compiler.used set.
475  collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
476  DenseSet<GlobalValue::GUID> CantBePromoted;
477  for (auto *V : Used) {
478  if (V->hasLocalLinkage()) {
479  LocalsUsed.insert(V);
480  CantBePromoted.insert(V->getGUID());
481  }
482  }
483 
484  bool HasLocalInlineAsmSymbol = false;
485  if (!M.getModuleInlineAsm().empty()) {
486  // Collect the local values defined by module level asm, and set up
487  // summaries for these symbols so that they can be marked as NoRename,
488  // to prevent export of any use of them in regular IR that would require
489  // renaming within the module level asm. Note we don't need to create a
490  // summary for weak or global defs, as they don't need to be flagged as
491  // NoRename, and defs in module level asm can't be imported anyway.
492  // Also, any values used but not defined within module level asm should
493  // be listed on the llvm.used or llvm.compiler.used global and marked as
494  // referenced from there.
497  // Symbols not marked as Weak or Global are local definitions.
498  if (Flags & (object::BasicSymbolRef::SF_Weak |
500  return;
501  HasLocalInlineAsmSymbol = true;
502  GlobalValue *GV = M.getNamedValue(Name);
503  if (!GV)
504  return;
505  assert(GV->isDeclaration() && "Def in module asm already has definition");
507  /* NotEligibleToImport = */ true,
508  /* Live = */ true,
509  /* Local */ GV->isDSOLocal());
510  CantBePromoted.insert(GV->getGUID());
511  // Create the appropriate summary type.
512  if (Function *F = dyn_cast<Function>(GV)) {
513  std::unique_ptr<FunctionSummary> Summary =
514  llvm::make_unique<FunctionSummary>(
515  GVFlags, /*InstCount=*/0,
517  F->hasFnAttribute(Attribute::ReadNone),
518  F->hasFnAttribute(Attribute::ReadOnly),
519  F->hasFnAttribute(Attribute::NoRecurse),
520  F->returnDoesNotAlias(),
521  /* NoInline = */ false},
522  /*EntryCount=*/0, ArrayRef<ValueInfo>{},
529  Index.addGlobalValueSummary(*GV, std::move(Summary));
530  } else {
531  std::unique_ptr<GlobalVarSummary> Summary =
532  llvm::make_unique<GlobalVarSummary>(
533  GVFlags, GlobalVarSummary::GVarFlags(),
535  Index.addGlobalValueSummary(*GV, std::move(Summary));
536  }
537  });
538  }
539 
540  bool IsThinLTO = true;
541  if (auto *MD =
542  mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
543  IsThinLTO = MD->getZExtValue();
544 
545  // Compute summaries for all functions defined in module, and save in the
546  // index.
547  for (auto &F : M) {
548  if (F.isDeclaration())
549  continue;
550 
551  DominatorTree DT(const_cast<Function &>(F));
552  BlockFrequencyInfo *BFI = nullptr;
553  std::unique_ptr<BlockFrequencyInfo> BFIPtr;
554  if (GetBFICallback)
555  BFI = GetBFICallback(F);
556  else if (F.hasProfileData()) {
557  LoopInfo LI{DT};
558  BranchProbabilityInfo BPI{F, LI};
559  BFIPtr = llvm::make_unique<BlockFrequencyInfo>(F, BPI, LI);
560  BFI = BFIPtr.get();
561  }
562 
563  computeFunctionSummary(Index, M, F, BFI, PSI, DT,
564  !LocalsUsed.empty() || HasLocalInlineAsmSymbol,
565  CantBePromoted, IsThinLTO);
566  }
567 
568  // Compute summaries for all variables defined in module, and save in the
569  // index.
570  for (const GlobalVariable &G : M.globals()) {
571  if (G.isDeclaration())
572  continue;
573  computeVariableSummary(Index, G, CantBePromoted);
574  }
575 
576  // Compute summaries for all aliases defined in module, and save in the
577  // index.
578  for (const GlobalAlias &A : M.aliases())
579  computeAliasSummary(Index, A, CantBePromoted);
580 
581  for (auto *V : LocalsUsed) {
582  auto *Summary = Index.getGlobalValueSummary(*V);
583  assert(Summary && "Missing summary for global value");
584  Summary->setNotEligibleToImport();
585  }
586 
587  // The linker doesn't know about these LLVM produced values, so we need
588  // to flag them as live in the index to ensure index-based dead value
589  // analysis treats them as live roots of the analysis.
590  setLiveRoot(Index, "llvm.used");
591  setLiveRoot(Index, "llvm.compiler.used");
592  setLiveRoot(Index, "llvm.global_ctors");
593  setLiveRoot(Index, "llvm.global_dtors");
594  setLiveRoot(Index, "llvm.global.annotations");
595 
596  for (auto &GlobalList : Index) {
597  // Ignore entries for references that are undefined in the current module.
598  if (GlobalList.second.SummaryList.empty())
599  continue;
600 
601  assert(GlobalList.second.SummaryList.size() == 1 &&
602  "Expected module's index to have one summary per GUID");
603  auto &Summary = GlobalList.second.SummaryList[0];
604  if (!IsThinLTO) {
605  Summary->setNotEligibleToImport();
606  continue;
607  }
608 
609  bool AllRefsCanBeExternallyReferenced =
610  llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) {
611  return !CantBePromoted.count(VI.getGUID());
612  });
613  if (!AllRefsCanBeExternallyReferenced) {
614  Summary->setNotEligibleToImport();
615  continue;
616  }
617 
618  if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) {
619  bool AllCallsCanBeExternallyReferenced = llvm::all_of(
620  FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
621  return !CantBePromoted.count(Edge.first.getGUID());
622  });
623  if (!AllCallsCanBeExternallyReferenced)
624  Summary->setNotEligibleToImport();
625  }
626  }
627 
628  return Index;
629 }
630 
631 AnalysisKey ModuleSummaryIndexAnalysis::Key;
632 
636  auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
638  M,
639  [&FAM](const Function &F) {
640  return &FAM.getResult<BlockFrequencyAnalysis>(
641  *const_cast<Function *>(&F));
642  },
643  &PSI);
644 }
645 
647 
648 INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
649  "Module Summary Analysis", false, true)
653  "Module Summary Analysis", false, true)
654 
656  return new ModuleSummaryIndexWrapperPass();
657 }
658 
660  : ModulePass(ID) {
662 }
663 
665  auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
666  Index.emplace(buildModuleSummaryIndex(
667  M,
668  [this](const Function &F) {
669  return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>(
670  *const_cast<Function *>(&F))
671  .getBFI());
672  },
673  PSI));
674  return false;
675 }
676 
678  Index.reset();
679  return false;
680 }
681 
683  AU.setPreservesAll();
686 }
ArrayRef< InstrProfValueData > getPromotionCandidatesForInstruction(const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount, uint32_t &NumCandidates)
Returns reference to array of InstrProfValueData for the given instruction I.
const GlobalObject * getBaseObject() const
bool hasDLLExportStorageClass() const
Definition: GlobalValue.h:264
iterator_range< use_iterator > uses()
Definition: Value.h:354
bool hasLocalLinkage() const
Definition: GlobalValue.h:435
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:769
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:492
This class represents lattice values for constants.
Definition: AllocatorList.h:23
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:77
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
GlobalValueSummary * getGlobalValueSummary(const GlobalValue &GV, bool PerModuleIndex=true) const
Returns the first GlobalValueSummary for GV, asserting that there is only one if PerModuleIndex.
void findDevirtualizableCallsForTypeTest(SmallVectorImpl< DevirtCallSite > &DevirtCalls, SmallVectorImpl< CallInst *> &Assumes, const CallInst *CI, DominatorTree &DT)
Given a call to the intrinsic @llvm.type.test, find all devirtualizable call sites based on the call ...
CallSite CS
The call site itself.
This is the interface to build a ModuleSummaryIndex for a module.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:249
bool isColdCount(uint64_t C)
Returns true if count C is considered cold.
Analysis providing profile information.
This class represents a function call, abstracting a target machine&#39;s calling convention.
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:422
This file contains the declarations for metadata subclasses.
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
uint64_t Offset
The offset from the address point to the virtual function.
bool isInterposable() const
Return true if this global&#39;s definition can be substituted with an arbitrary definition at link time...
Definition: GlobalValue.h:419
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:320
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:37
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1185
Optional< uint64_t > getProfileCount(const Instruction *CallInst, BlockFrequencyInfo *BFI)
Returns the profile count for CallInst.
F(f)
void initializeModuleSummaryIndexWrapperPassPass(PassRegistry &)
block Block Frequency true
This defines the Use class.
bool isHotCount(uint64_t C)
Returns true if count C is considered hot.
ModuleSummaryIndex buildModuleSummaryIndex(const Module &M, std::function< BlockFrequencyInfo *(const Function &F)> GetBFICallback, ProfileSummaryInfo *PSI)
Direct function to compute a ModuleSummaryIndex from a given module.
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1134
void findDevirtualizableCallsForTypeCheckedLoad(SmallVectorImpl< DevirtCallSite > &DevirtCalls, SmallVectorImpl< Instruction *> &LoadedPtrs, SmallVectorImpl< Instruction *> &Preds, bool &HasNonCallUses, const CallInst *CI, DominatorTree &DT)
Given a call to the intrinsic @llvm.type.checked.load, find all devirtualizable call sites based on t...
bool hasSection() const
Definition: GlobalValue.h:269
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
amdgpu Simplify well known AMD library false Value Value const Twine & Name
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
bool isDSOLocal() const
Definition: GlobalValue.h:279
static AnalysisKey * ID()
Returns an opaque, unique ID for this analysis type.
Definition: PassManager.h:398
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
IterTy arg_end() const
Definition: CallSite.h:574
This file contains the simple types necessary to represent the attributes associated with functions a...
Legacy analysis pass which computes BlockFrequencyInfo.
static void CollectAsmSymbols(const Module &M, function_ref< void(StringRef, object::BasicSymbolRef::Flags)> AsmSymbol)
Parse inline ASM and collect the symbols that are defined or referenced in the current module...
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:141
std::pair< ValueInfo, CalleeInfo > EdgeTy
<CalleeValueInfo, CalleeInfo> call edge pair.
GlobalValue * getNamedValue(StringRef Name) const
Return the global value in the module with the specified name, of arbitrary type. ...
Definition: Module.cpp:113
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
module summary analysis
LinkageTypes getLinkage() const
Definition: GlobalValue.h:450
Class to hold module path string table and global value map, and encapsulate methods for operating on...
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
amdgpu Simplify well known AMD library false Value * Callee
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
bool hasAppendingLinkage() const
Definition: GlobalValue.h:432
static void computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A, DenseSet< GlobalValue::GUID > &CantBePromoted)
Interface to identify indirect call promotion candidates.
ForceSummaryHotnessType
Types for -force-summary-edges-cold debugging option.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
bool hasName() const
Definition: Value.h:250
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
A call site that could be devirtualized.
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition: Module.cpp:311
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:642
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:91
bool returnDoesNotAlias() const
Determine if the parameter or return value is marked with NoAlias attribute.
Definition: Function.h:585
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
Represent the analysis usage information of a pass.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1192
INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis", "Module Summary Analysis", false, true) INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass
static void addIntrinsicToSummary(const CallInst *CI, SetVector< GlobalValue::GUID > &TypeTests, SetVector< FunctionSummary::VFuncId > &TypeTestAssumeVCalls, SetVector< FunctionSummary::VFuncId > &TypeCheckedLoadVCalls, SetVector< FunctionSummary::ConstVCall > &TypeTestAssumeConstVCalls, SetVector< FunctionSummary::ConstVCall > &TypeCheckedLoadConstVCalls, DominatorTree &DT)
If this intrinsic call requires that we add information to the function summary, do so via the non-co...
op_range operands()
Definition: User.h:237
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:496
static void addVCallToSet(DevirtCallSite Call, GlobalValue::GUID Guid, SetVector< FunctionSummary::VFuncId > &VCalls, SetVector< FunctionSummary::ConstVCall > &ConstVCalls)
Determine whether this call has all constant integer arguments (excluding "this") and summarize it to...
static void computeVariableSummary(ModuleSummaryIndex &Index, const GlobalVariable &V, DenseSet< GlobalValue::GUID > &CantBePromoted)
Class to represent profile counts.
Definition: Function.h:260
VectorType takeVector()
Clear the MapVector and return the underlying vector.
Definition: MapVector.h:55
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
Struct that holds a reference to a particular GUID in a global value summary.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static bool isNonRenamableLocal(const GlobalValue &GV)
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallPtrSetImpl< GlobalValue *> &Set, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition: Module.cpp:595
Analysis pass which computes BlockFrequencyInfo.
Vector takeVector()
Clear the SetVector and return the underlying vector.
Definition: SetVector.h:66
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:417
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
Definition: SmallPtrSet.h:377
static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser, SetVector< ValueInfo > &RefEdges, SmallPtrSet< const User *, 8 > &Visited)
IterTy arg_begin() const
Definition: CallSite.h:570
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:846
Module.h This file contains the declarations for the Module class.
const DataFlowGraph & G
Definition: RDFGraph.cpp:210
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:380
static void computeFunctionSummary(ModuleSummaryIndex &Index, const Module &M, const Function &F, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, DominatorTree &DT, bool HasLocalsInUsedOrAsm, DenseSet< GlobalValue::GUID > &CantBePromoted, bool IsThinLTO)
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:193
BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
Result run(Module &M, ModuleAnalysisManager &AM)
void setPreservesAll()
Set by analyses that do not transform their input at all.
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:500
ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const
Return a ValueInfo for the index value_type (convenient when iterating index).
bool hasComdat() const
Definition: GlobalObject.h:99
ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID)
Return a ValueInfo for GUID.
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:617
amdgpu Simplify well known AMD library false Value Value * Arg
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Analysis providing branch probability information.
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
void addGlobalValueSummary(const GlobalValue &GV, std::unique_ptr< GlobalValueSummary > Summary)
Add a global value summary for a value.
Establish a view to a call site for examination.
Definition: CallSite.h:886
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1180
module summary Module Summary Analysis
#define I(x, y, z)
Definition: MD5.cpp:58
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:224
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:322
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:91
Flags specific to function summaries.
static bool isNonVolatileLoad(const Instruction *I)
FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold
cl::opt< FunctionSummary::ForceSummaryHotnessType, true > FSEC("force-summary-edges-cold", cl::Hidden, cl::location(ForceSummaryEdgesCold), cl::desc("Force all edges in the function summary to cold"), cl::values(clEnumValN(FunctionSummary::FSHT_None, "none", "None."), clEnumValN(FunctionSummary::FSHT_AllNonCritical, "all-non-critical", "All non-critical edges."), clEnumValN(FunctionSummary::FSHT_All, "all", "All edges.")))
const std::string & getModuleInlineAsm() const
Get any module-scope inline assembly blocks.
Definition: Module.h:247
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:205
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ModulePass * createModuleSummaryIndexWrapperPass()
A vector that has set insertion semantics.
Definition: SetVector.h:40
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on...
static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name)
Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
A single uniqued string.
Definition: Metadata.h:603
A container for analyses that lazily runs them and caches their results.
bool hasProfileData() const
Return true if the function is annotated with profile data.
Definition: Function.h:307
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:438
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount, ProfileSummaryInfo *PSI)
Legacy wrapper pass to provide the ModuleSummaryIndex object.
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:1037
bool isCallee(Value::const_user_iterator UI) const
Determine whether the passed iterator points to the callee operand&#39;s Use.
Definition: CallSite.h:142