LLVM  8.0.0svn
ModuleSummaryAnalysis.cpp
Go to the documentation of this file.
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 
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/MapVector.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
27 #include "llvm/Analysis/LoopInfo.h"
30 #include "llvm/IR/Attributes.h"
31 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/CallSite.h"
33 #include "llvm/IR/Constant.h"
34 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/Dominators.h"
36 #include "llvm/IR/Function.h"
37 #include "llvm/IR/GlobalAlias.h"
38 #include "llvm/IR/GlobalValue.h"
39 #include "llvm/IR/GlobalVariable.h"
40 #include "llvm/IR/Instructions.h"
41 #include "llvm/IR/IntrinsicInst.h"
42 #include "llvm/IR/Intrinsics.h"
43 #include "llvm/IR/Metadata.h"
44 #include "llvm/IR/Module.h"
46 #include "llvm/IR/Use.h"
47 #include "llvm/IR/User.h"
50 #include "llvm/Pass.h"
51 #include "llvm/Support/Casting.h"
53 #include <algorithm>
54 #include <cassert>
55 #include <cstdint>
56 #include <vector>
57 
58 using namespace llvm;
59 
60 #define DEBUG_TYPE "module-summary-analysis"
61 
62 // Option to force edges cold which will block importing when the
63 // -import-cold-multiplier is set to 0. Useful for debugging.
67  "force-summary-edges-cold", cl::Hidden, cl::location(ForceSummaryEdgesCold),
68  cl::desc("Force all edges in the function summary to cold"),
71  "all-non-critical", "All non-critical edges."),
72  clEnumValN(FunctionSummary::FSHT_All, "all", "All edges.")));
73 
74 // Walk through the operands of a given User via worklist iteration and populate
75 // the set of GlobalValue references encountered. Invoked either on an
76 // Instruction or a GlobalVariable (which walks its initializer).
77 // Return true if any of the operands contains blockaddress. This is important
78 // to know when computing summary for global var, because if global variable
79 // references basic block address we can't import it separately from function
80 // containing that basic block. For simplicity we currently don't import such
81 // global vars at all. When importing function we aren't interested if any
82 // instruction in it takes an address of any basic block, because instruction
83 // can only take an address of basic block located in the same function.
84 static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,
85  SetVector<ValueInfo> &RefEdges,
87  bool HasBlockAddress = false;
89  Worklist.push_back(CurUser);
90 
91  while (!Worklist.empty()) {
92  const User *U = Worklist.pop_back_val();
93 
94  if (!Visited.insert(U).second)
95  continue;
96 
97  ImmutableCallSite CS(U);
98 
99  for (const auto &OI : U->operands()) {
100  const User *Operand = dyn_cast<User>(OI);
101  if (!Operand)
102  continue;
103  if (isa<BlockAddress>(Operand)) {
104  HasBlockAddress = true;
105  continue;
106  }
107  if (auto *GV = dyn_cast<GlobalValue>(Operand)) {
108  // We have a reference to a global value. This should be added to
109  // the reference set unless it is a callee. Callees are handled
110  // specially by WriteFunction and are added to a separate list.
111  if (!(CS && CS.isCallee(&OI)))
112  RefEdges.insert(Index.getOrInsertValueInfo(GV));
113  continue;
114  }
115  Worklist.push_back(Operand);
116  }
117  }
118  return HasBlockAddress;
119 }
120 
122  ProfileSummaryInfo *PSI) {
123  if (!PSI)
125  if (PSI->isHotCount(ProfileCount))
127  if (PSI->isColdCount(ProfileCount))
130 }
131 
132 static bool isNonRenamableLocal(const GlobalValue &GV) {
133  return GV.hasSection() && GV.hasLocalLinkage();
134 }
135 
136 /// Determine whether this call has all constant integer arguments (excluding
137 /// "this") and summarize it to VCalls or ConstVCalls as appropriate.
141  std::vector<uint64_t> Args;
142  // Start from the second argument to skip the "this" pointer.
143  for (auto &Arg : make_range(Call.CS.arg_begin() + 1, Call.CS.arg_end())) {
144  auto *CI = dyn_cast<ConstantInt>(Arg);
145  if (!CI || CI->getBitWidth() > 64) {
146  VCalls.insert({Guid, Call.Offset});
147  return;
148  }
149  Args.push_back(CI->getZExtValue());
150  }
151  ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)});
152 }
153 
154 /// If this intrinsic call requires that we add information to the function
155 /// summary, do so via the non-constant reference arguments.
157  const CallInst *CI, SetVector<GlobalValue::GUID> &TypeTests,
158  SetVector<FunctionSummary::VFuncId> &TypeTestAssumeVCalls,
159  SetVector<FunctionSummary::VFuncId> &TypeCheckedLoadVCalls,
160  SetVector<FunctionSummary::ConstVCall> &TypeTestAssumeConstVCalls,
161  SetVector<FunctionSummary::ConstVCall> &TypeCheckedLoadConstVCalls,
162  DominatorTree &DT) {
163  switch (CI->getCalledFunction()->getIntrinsicID()) {
164  case Intrinsic::type_test: {
165  auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1));
166  auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
167  if (!TypeId)
168  break;
169  GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
170 
171  // Produce a summary from type.test intrinsics. We only summarize type.test
172  // intrinsics that are used other than by an llvm.assume intrinsic.
173  // Intrinsics that are assumed are relevant only to the devirtualization
174  // pass, not the type test lowering pass.
175  bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) {
176  auto *AssumeCI = dyn_cast<CallInst>(CIU.getUser());
177  if (!AssumeCI)
178  return true;
179  Function *F = AssumeCI->getCalledFunction();
180  return !F || F->getIntrinsicID() != Intrinsic::assume;
181  });
182  if (HasNonAssumeUses)
183  TypeTests.insert(Guid);
184 
185  SmallVector<DevirtCallSite, 4> DevirtCalls;
187  findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT);
188  for (auto &Call : DevirtCalls)
189  addVCallToSet(Call, Guid, TypeTestAssumeVCalls,
190  TypeTestAssumeConstVCalls);
191 
192  break;
193  }
194 
195  case Intrinsic::type_checked_load: {
196  auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2));
197  auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
198  if (!TypeId)
199  break;
200  GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
201 
202  SmallVector<DevirtCallSite, 4> DevirtCalls;
205  bool HasNonCallUses = false;
206  findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
207  HasNonCallUses, CI, DT);
208  // Any non-call uses of the result of llvm.type.checked.load will
209  // prevent us from optimizing away the llvm.type.test.
210  if (HasNonCallUses)
211  TypeTests.insert(Guid);
212  for (auto &Call : DevirtCalls)
213  addVCallToSet(Call, Guid, TypeCheckedLoadVCalls,
214  TypeCheckedLoadConstVCalls);
215 
216  break;
217  }
218  default:
219  break;
220  }
221 }
222 
223 static bool isNonVolatileLoad(const Instruction *I) {
224  if (const auto *LI = dyn_cast<LoadInst>(I))
225  return !LI->isVolatile();
226 
227  return false;
228 }
229 
231  const Function &F, BlockFrequencyInfo *BFI,
233  bool HasLocalsInUsedOrAsm,
234  DenseSet<GlobalValue::GUID> &CantBePromoted,
235  bool IsThinLTO) {
236  // Summary not currently supported for anonymous functions, they should
237  // have been named.
238  assert(F.hasName());
239 
240  unsigned NumInsts = 0;
241  // Map from callee ValueId to profile count. Used to accumulate profile
242  // counts for all static calls to a given callee.
243  MapVector<ValueInfo, CalleeInfo> CallGraphEdges;
244  SetVector<ValueInfo> RefEdges;
246  SetVector<FunctionSummary::VFuncId> TypeTestAssumeVCalls,
247  TypeCheckedLoadVCalls;
248  SetVector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls,
249  TypeCheckedLoadConstVCalls;
250  ICallPromotionAnalysis ICallAnalysis;
252 
253  // Add personality function, prefix data and prologue data to function's ref
254  // list.
255  findRefEdges(Index, &F, RefEdges, Visited);
256  std::vector<const Instruction *> NonVolatileLoads;
257 
258  bool HasInlineAsmMaybeReferencingInternal = false;
259  bool InitsVarArgs = false;
260  for (const BasicBlock &BB : F)
261  for (const Instruction &I : BB) {
262  if (isa<DbgInfoIntrinsic>(I))
263  continue;
264  if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I)) {
265  if (II->getIntrinsicID() == Intrinsic::vastart)
266  InitsVarArgs = true;
267  }
268  ++NumInsts;
269  if (isNonVolatileLoad(&I)) {
270  // Postpone processing of non-volatile load instructions
271  // See comments below
272  Visited.insert(&I);
273  NonVolatileLoads.push_back(&I);
274  continue;
275  }
276  findRefEdges(Index, &I, RefEdges, Visited);
277  auto CS = ImmutableCallSite(&I);
278  if (!CS)
279  continue;
280 
281  const auto *CI = dyn_cast<CallInst>(&I);
282  // Since we don't know exactly which local values are referenced in inline
283  // assembly, conservatively mark the function as possibly referencing
284  // a local value from inline assembly to ensure we don't export a
285  // reference (which would require renaming and promotion of the
286  // referenced value).
287  if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm())
288  HasInlineAsmMaybeReferencingInternal = true;
289 
290  auto *CalledValue = CS.getCalledValue();
291  auto *CalledFunction = CS.getCalledFunction();
292  if (CalledValue && !CalledFunction) {
293  CalledValue = CalledValue->stripPointerCastsNoFollowAliases();
294  // Stripping pointer casts can reveal a called function.
295  CalledFunction = dyn_cast<Function>(CalledValue);
296  }
297  // Check if this is an alias to a function. If so, get the
298  // called aliasee for the checks below.
299  if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
300  assert(!CalledFunction && "Expected null called function in callsite for alias");
301  CalledFunction = dyn_cast<Function>(GA->getBaseObject());
302  }
303  // Check if this is a direct call to a known function or a known
304  // intrinsic, or an indirect call with profile data.
305  if (CalledFunction) {
306  if (CI && CalledFunction->isIntrinsic()) {
308  CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls,
309  TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls, DT);
310  continue;
311  }
312  // We should have named any anonymous globals
313  assert(CalledFunction->hasName());
314  auto ScaledCount = PSI->getProfileCount(&I, BFI);
315  auto Hotness = ScaledCount ? getHotness(ScaledCount.getValue(), PSI)
319 
320  // Use the original CalledValue, in case it was an alias. We want
321  // to record the call edge to the alias in that case. Eventually
322  // an alias summary will be created to associate the alias and
323  // aliasee.
324  auto &ValueInfo = CallGraphEdges[Index.getOrInsertValueInfo(
325  cast<GlobalValue>(CalledValue))];
326  ValueInfo.updateHotness(Hotness);
327  // Add the relative block frequency to CalleeInfo if there is no profile
328  // information.
329  if (BFI != nullptr && Hotness == CalleeInfo::HotnessType::Unknown) {
330  uint64_t BBFreq = BFI->getBlockFreq(&BB).getFrequency();
331  uint64_t EntryFreq = BFI->getEntryFreq();
332  ValueInfo.updateRelBlockFreq(BBFreq, EntryFreq);
333  }
334  } else {
335  // Skip inline assembly calls.
336  if (CI && CI->isInlineAsm())
337  continue;
338  // Skip direct calls.
339  if (!CalledValue || isa<Constant>(CalledValue))
340  continue;
341 
342  // Check if the instruction has a callees metadata. If so, add callees
343  // to CallGraphEdges to reflect the references from the metadata, and
344  // to enable importing for subsequent indirect call promotion and
345  // inlining.
346  if (auto *MD = I.getMetadata(LLVMContext::MD_callees)) {
347  for (auto &Op : MD->operands()) {
348  Function *Callee = mdconst::extract_or_null<Function>(Op);
349  if (Callee)
350  CallGraphEdges[Index.getOrInsertValueInfo(Callee)];
351  }
352  }
353 
354  uint32_t NumVals, NumCandidates;
355  uint64_t TotalCount;
356  auto CandidateProfileData =
358  &I, NumVals, TotalCount, NumCandidates);
359  for (auto &Candidate : CandidateProfileData)
360  CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)]
361  .updateHotness(getHotness(Candidate.Count, PSI));
362  }
363  }
364 
365  // By now we processed all instructions in a function, except
366  // non-volatile loads. All new refs we add in a loop below
367  // are obviously constant. All constant refs are grouped in the
368  // end of RefEdges vector, so we can use a single integer value
369  // to identify them.
370  unsigned RefCnt = RefEdges.size();
371  for (const Instruction *I : NonVolatileLoads) {
372  Visited.erase(I);
373  findRefEdges(Index, I, RefEdges, Visited);
374  }
375  std::vector<ValueInfo> Refs = RefEdges.takeVector();
376  // Regular LTO module doesn't participate in ThinLTO import,
377  // so no reference from it can be readonly, since this would
378  // require importing variable as local copy
379  if (IsThinLTO)
380  for (; RefCnt < Refs.size(); ++RefCnt)
381  Refs[RefCnt].setReadOnly();
382 
383  // Explicit add hot edges to enforce importing for designated GUIDs for
384  // sample PGO, to enable the same inlines as the profiled optimized binary.
385  for (auto &I : F.getImportGUIDs())
386  CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness(
390 
391  bool NonRenamableLocal = isNonRenamableLocal(F);
392  bool NotEligibleForImport =
393  NonRenamableLocal || HasInlineAsmMaybeReferencingInternal;
394  GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport,
395  /* Live = */ false, F.isDSOLocal());
396  FunctionSummary::FFlags FunFlags{
397  F.hasFnAttribute(Attribute::ReadNone),
398  F.hasFnAttribute(Attribute::ReadOnly),
399  F.hasFnAttribute(Attribute::NoRecurse), F.returnDoesNotAlias(),
400  // Inliner doesn't handle variadic functions with va_start calls.
401  // FIXME: refactor this to use the same code that inliner is using.
402  InitsVarArgs ||
403  // Don't try to import functions with noinline attribute.
404  F.getAttributes().hasFnAttribute(Attribute::NoInline)};
405  auto FuncSummary = llvm::make_unique<FunctionSummary>(
406  Flags, NumInsts, FunFlags, std::move(Refs), CallGraphEdges.takeVector(),
407  TypeTests.takeVector(), TypeTestAssumeVCalls.takeVector(),
408  TypeCheckedLoadVCalls.takeVector(),
409  TypeTestAssumeConstVCalls.takeVector(),
410  TypeCheckedLoadConstVCalls.takeVector());
411  if (NonRenamableLocal)
412  CantBePromoted.insert(F.getGUID());
413  Index.addGlobalValueSummary(F, std::move(FuncSummary));
414 }
415 
416 static void
418  DenseSet<GlobalValue::GUID> &CantBePromoted) {
419  SetVector<ValueInfo> RefEdges;
421  bool HasBlockAddress = findRefEdges(Index, &V, RefEdges, Visited);
422  bool NonRenamableLocal = isNonRenamableLocal(V);
423  GlobalValueSummary::GVFlags Flags(V.getLinkage(), NonRenamableLocal,
424  /* Live = */ false, V.isDSOLocal());
425 
426  // Don't mark variables we won't be able to internalize as read-only.
428  !V.hasComdat() && !V.hasAppendingLinkage() && !V.isInterposable() &&
430  auto GVarSummary = llvm::make_unique<GlobalVarSummary>(Flags, VarFlags,
431  RefEdges.takeVector());
432  if (NonRenamableLocal)
433  CantBePromoted.insert(V.getGUID());
434  if (HasBlockAddress)
435  GVarSummary->setNotEligibleToImport();
436  Index.addGlobalValueSummary(V, std::move(GVarSummary));
437 }
438 
439 static void
441  DenseSet<GlobalValue::GUID> &CantBePromoted) {
442  bool NonRenamableLocal = isNonRenamableLocal(A);
443  GlobalValueSummary::GVFlags Flags(A.getLinkage(), NonRenamableLocal,
444  /* Live = */ false, A.isDSOLocal());
445  auto AS = llvm::make_unique<AliasSummary>(Flags);
446  auto *Aliasee = A.getBaseObject();
447  auto *AliaseeSummary = Index.getGlobalValueSummary(*Aliasee);
448  assert(AliaseeSummary && "Alias expects aliasee summary to be parsed");
449  AS->setAliasee(AliaseeSummary);
450  if (NonRenamableLocal)
451  CantBePromoted.insert(A.getGUID());
452  Index.addGlobalValueSummary(A, std::move(AS));
453 }
454 
455 // Set LiveRoot flag on entries matching the given value name.
457  if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
458  for (auto &Summary : VI.getSummaryList())
459  Summary->setLive(true);
460 }
461 
463  const Module &M,
464  std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
465  ProfileSummaryInfo *PSI) {
466  assert(PSI);
467  ModuleSummaryIndex Index(/*HaveGVs=*/true);
468 
469  // Identify the local values in the llvm.used and llvm.compiler.used sets,
470  // which should not be exported as they would then require renaming and
471  // promotion, but we may have opaque uses e.g. in inline asm. We collect them
472  // here because we use this information to mark functions containing inline
473  // assembly calls as not importable.
476  // First collect those in the llvm.used set.
477  collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
478  // Next collect those in the llvm.compiler.used set.
479  collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
480  DenseSet<GlobalValue::GUID> CantBePromoted;
481  for (auto *V : Used) {
482  if (V->hasLocalLinkage()) {
483  LocalsUsed.insert(V);
484  CantBePromoted.insert(V->getGUID());
485  }
486  }
487 
488  bool HasLocalInlineAsmSymbol = false;
489  if (!M.getModuleInlineAsm().empty()) {
490  // Collect the local values defined by module level asm, and set up
491  // summaries for these symbols so that they can be marked as NoRename,
492  // to prevent export of any use of them in regular IR that would require
493  // renaming within the module level asm. Note we don't need to create a
494  // summary for weak or global defs, as they don't need to be flagged as
495  // NoRename, and defs in module level asm can't be imported anyway.
496  // Also, any values used but not defined within module level asm should
497  // be listed on the llvm.used or llvm.compiler.used global and marked as
498  // referenced from there.
501  // Symbols not marked as Weak or Global are local definitions.
502  if (Flags & (object::BasicSymbolRef::SF_Weak |
504  return;
505  HasLocalInlineAsmSymbol = true;
506  GlobalValue *GV = M.getNamedValue(Name);
507  if (!GV)
508  return;
509  assert(GV->isDeclaration() && "Def in module asm already has definition");
511  /* NotEligibleToImport = */ true,
512  /* Live = */ true,
513  /* Local */ GV->isDSOLocal());
514  CantBePromoted.insert(GV->getGUID());
515  // Create the appropriate summary type.
516  if (Function *F = dyn_cast<Function>(GV)) {
517  std::unique_ptr<FunctionSummary> Summary =
518  llvm::make_unique<FunctionSummary>(
519  GVFlags, 0,
521  F->hasFnAttribute(Attribute::ReadNone),
522  F->hasFnAttribute(Attribute::ReadOnly),
523  F->hasFnAttribute(Attribute::NoRecurse),
524  F->returnDoesNotAlias(),
525  /* NoInline = */ false},
532  Index.addGlobalValueSummary(*GV, std::move(Summary));
533  } else {
534  std::unique_ptr<GlobalVarSummary> Summary =
535  llvm::make_unique<GlobalVarSummary>(
536  GVFlags, GlobalVarSummary::GVarFlags(),
538  Index.addGlobalValueSummary(*GV, std::move(Summary));
539  }
540  });
541  }
542 
543  bool IsThinLTO = true;
544  if (auto *MD =
545  mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
546  IsThinLTO = MD->getZExtValue();
547 
548  // Compute summaries for all functions defined in module, and save in the
549  // index.
550  for (auto &F : M) {
551  if (F.isDeclaration())
552  continue;
553 
554  DominatorTree DT(const_cast<Function &>(F));
555  BlockFrequencyInfo *BFI = nullptr;
556  std::unique_ptr<BlockFrequencyInfo> BFIPtr;
557  if (GetBFICallback)
558  BFI = GetBFICallback(F);
559  else if (F.hasProfileData()) {
560  LoopInfo LI{DT};
561  BranchProbabilityInfo BPI{F, LI};
562  BFIPtr = llvm::make_unique<BlockFrequencyInfo>(F, BPI, LI);
563  BFI = BFIPtr.get();
564  }
565 
566  computeFunctionSummary(Index, M, F, BFI, PSI, DT,
567  !LocalsUsed.empty() || HasLocalInlineAsmSymbol,
568  CantBePromoted, IsThinLTO);
569  }
570 
571  // Compute summaries for all variables defined in module, and save in the
572  // index.
573  for (const GlobalVariable &G : M.globals()) {
574  if (G.isDeclaration())
575  continue;
576  computeVariableSummary(Index, G, CantBePromoted);
577  }
578 
579  // Compute summaries for all aliases defined in module, and save in the
580  // index.
581  for (const GlobalAlias &A : M.aliases())
582  computeAliasSummary(Index, A, CantBePromoted);
583 
584  for (auto *V : LocalsUsed) {
585  auto *Summary = Index.getGlobalValueSummary(*V);
586  assert(Summary && "Missing summary for global value");
587  Summary->setNotEligibleToImport();
588  }
589 
590  // The linker doesn't know about these LLVM produced values, so we need
591  // to flag them as live in the index to ensure index-based dead value
592  // analysis treats them as live roots of the analysis.
593  setLiveRoot(Index, "llvm.used");
594  setLiveRoot(Index, "llvm.compiler.used");
595  setLiveRoot(Index, "llvm.global_ctors");
596  setLiveRoot(Index, "llvm.global_dtors");
597  setLiveRoot(Index, "llvm.global.annotations");
598 
599  for (auto &GlobalList : Index) {
600  // Ignore entries for references that are undefined in the current module.
601  if (GlobalList.second.SummaryList.empty())
602  continue;
603 
604  assert(GlobalList.second.SummaryList.size() == 1 &&
605  "Expected module's index to have one summary per GUID");
606  auto &Summary = GlobalList.second.SummaryList[0];
607  if (!IsThinLTO) {
608  Summary->setNotEligibleToImport();
609  continue;
610  }
611 
612  bool AllRefsCanBeExternallyReferenced =
613  llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) {
614  return !CantBePromoted.count(VI.getGUID());
615  });
616  if (!AllRefsCanBeExternallyReferenced) {
617  Summary->setNotEligibleToImport();
618  continue;
619  }
620 
621  if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) {
622  bool AllCallsCanBeExternallyReferenced = llvm::all_of(
623  FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
624  return !CantBePromoted.count(Edge.first.getGUID());
625  });
626  if (!AllCallsCanBeExternallyReferenced)
627  Summary->setNotEligibleToImport();
628  }
629  }
630 
631  return Index;
632 }
633 
634 AnalysisKey ModuleSummaryIndexAnalysis::Key;
635 
639  auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
641  M,
642  [&FAM](const Function &F) {
643  return &FAM.getResult<BlockFrequencyAnalysis>(
644  *const_cast<Function *>(&F));
645  },
646  &PSI);
647 }
648 
650 
651 INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
652  "Module Summary Analysis", false, true)
656  "Module Summary Analysis", false, true)
657 
659  return new ModuleSummaryIndexWrapperPass();
660 }
661 
663  : ModulePass(ID) {
665 }
666 
668  auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
669  Index.emplace(buildModuleSummaryIndex(
670  M,
671  [this](const Function &F) {
672  return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>(
673  *const_cast<Function *>(&F))
674  .getBFI());
675  },
676  PSI));
677  return false;
678 }
679 
681  Index.reset();
682  return false;
683 }
684 
686  AU.setPreservesAll();
689 }
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.
Function * getCalledFunction() const
Return the function called, or null if this is an indirect function invocation.
const GlobalObject * getBaseObject() const
bool hasDLLExportStorageClass() const
Definition: GlobalValue.h:265
iterator_range< use_iterator > uses()
Definition: Value.h:355
bool hasLocalLinkage() const
Definition: GlobalValue.h:436
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:770
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:493
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:78
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:250
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:423
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:420
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:321
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:38
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:1048
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.
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:270
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
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:280
static AnalysisKey * ID()
Returns an opaque, unique ID for this analysis type.
Definition: PassManager.h:399
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
IterTy arg_end() const
Definition: CallSite.h:575
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:142
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:33
module summary analysis
LinkageTypes getLinkage() const
Definition: GlobalValue.h:451
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:145
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:433
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:251
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
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:305
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:639
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:92
bool returnDoesNotAlias() const
Determine if the parameter or return value is marked with NoAlias attribute.
Definition: Function.h:586
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:371
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:1055
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:238
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:497
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:261
VectorType takeVector()
Clear the MapVector and return the underlying vector.
Definition: MapVector.h:56
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:550
Analysis pass which computes BlockFrequencyInfo.
Vector takeVector()
Clear the SetVector and return the underlying vector.
Definition: SetVector.h:67
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
Definition: SmallPtrSet.h:378
static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser, SetVector< ValueInfo > &RefEdges, SmallPtrSet< const User *, 8 > &Visited)
IterTy arg_begin() const
Definition: CallSite.h:571
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
Module.h This file contains the declarations for the Module class.
const DataFlowGraph & G
Definition: RDFGraph.cpp:211
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:381
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:194
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:501
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:100
ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID)
Return a ValueInfo for GUID.
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:614
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:56
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:714
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:225
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:323
Rename collisions when linking (static functions).
Definition: GlobalValue.h:56
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:92
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:206
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
ModulePass * createModuleSummaryIndexWrapperPass()
A vector that has set insertion semantics.
Definition: SetVector.h:41
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:49
A single uniqued string.
Definition: Metadata.h:604
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:308
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:71
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:435
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount, ProfileSummaryInfo *PSI)
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44
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:1038
bool isCallee(Value::const_user_iterator UI) const
Determine whether the passed iterator points to the callee operand&#39;s Use.
Definition: CallSite.h:143