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 
74  "module-summary-dot-file", cl::init(""), cl::Hidden,
75  cl::value_desc("filename"),
76  cl::desc("File to emit dot graph of new summary into."));
77 
78 // Walk through the operands of a given User via worklist iteration and populate
79 // the set of GlobalValue references encountered. Invoked either on an
80 // Instruction or a GlobalVariable (which walks its initializer).
81 // Return true if any of the operands contains blockaddress. This is important
82 // to know when computing summary for global var, because if global variable
83 // references basic block address we can't import it separately from function
84 // containing that basic block. For simplicity we currently don't import such
85 // global vars at all. When importing function we aren't interested if any
86 // instruction in it takes an address of any basic block, because instruction
87 // can only take an address of basic block located in the same function.
88 static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,
89  SetVector<ValueInfo> &RefEdges,
91  bool HasBlockAddress = false;
93  Worklist.push_back(CurUser);
94 
95  while (!Worklist.empty()) {
96  const User *U = Worklist.pop_back_val();
97 
98  if (!Visited.insert(U).second)
99  continue;
100 
101  ImmutableCallSite CS(U);
102 
103  for (const auto &OI : U->operands()) {
104  const User *Operand = dyn_cast<User>(OI);
105  if (!Operand)
106  continue;
107  if (isa<BlockAddress>(Operand)) {
108  HasBlockAddress = true;
109  continue;
110  }
111  if (auto *GV = dyn_cast<GlobalValue>(Operand)) {
112  // We have a reference to a global value. This should be added to
113  // the reference set unless it is a callee. Callees are handled
114  // specially by WriteFunction and are added to a separate list.
115  if (!(CS && CS.isCallee(&OI)))
116  RefEdges.insert(Index.getOrInsertValueInfo(GV));
117  continue;
118  }
119  Worklist.push_back(Operand);
120  }
121  }
122  return HasBlockAddress;
123 }
124 
126  ProfileSummaryInfo *PSI) {
127  if (!PSI)
129  if (PSI->isHotCount(ProfileCount))
131  if (PSI->isColdCount(ProfileCount))
134 }
135 
136 static bool isNonRenamableLocal(const GlobalValue &GV) {
137  return GV.hasSection() && GV.hasLocalLinkage();
138 }
139 
140 /// Determine whether this call has all constant integer arguments (excluding
141 /// "this") and summarize it to VCalls or ConstVCalls as appropriate.
145  std::vector<uint64_t> Args;
146  // Start from the second argument to skip the "this" pointer.
147  for (auto &Arg : make_range(Call.CS.arg_begin() + 1, Call.CS.arg_end())) {
148  auto *CI = dyn_cast<ConstantInt>(Arg);
149  if (!CI || CI->getBitWidth() > 64) {
150  VCalls.insert({Guid, Call.Offset});
151  return;
152  }
153  Args.push_back(CI->getZExtValue());
154  }
155  ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)});
156 }
157 
158 /// If this intrinsic call requires that we add information to the function
159 /// summary, do so via the non-constant reference arguments.
161  const CallInst *CI, SetVector<GlobalValue::GUID> &TypeTests,
162  SetVector<FunctionSummary::VFuncId> &TypeTestAssumeVCalls,
163  SetVector<FunctionSummary::VFuncId> &TypeCheckedLoadVCalls,
164  SetVector<FunctionSummary::ConstVCall> &TypeTestAssumeConstVCalls,
165  SetVector<FunctionSummary::ConstVCall> &TypeCheckedLoadConstVCalls,
166  DominatorTree &DT) {
167  switch (CI->getCalledFunction()->getIntrinsicID()) {
168  case Intrinsic::type_test: {
169  auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1));
170  auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
171  if (!TypeId)
172  break;
173  GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
174 
175  // Produce a summary from type.test intrinsics. We only summarize type.test
176  // intrinsics that are used other than by an llvm.assume intrinsic.
177  // Intrinsics that are assumed are relevant only to the devirtualization
178  // pass, not the type test lowering pass.
179  bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) {
180  auto *AssumeCI = dyn_cast<CallInst>(CIU.getUser());
181  if (!AssumeCI)
182  return true;
183  Function *F = AssumeCI->getCalledFunction();
184  return !F || F->getIntrinsicID() != Intrinsic::assume;
185  });
186  if (HasNonAssumeUses)
187  TypeTests.insert(Guid);
188 
189  SmallVector<DevirtCallSite, 4> DevirtCalls;
191  findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT);
192  for (auto &Call : DevirtCalls)
193  addVCallToSet(Call, Guid, TypeTestAssumeVCalls,
194  TypeTestAssumeConstVCalls);
195 
196  break;
197  }
198 
199  case Intrinsic::type_checked_load: {
200  auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2));
201  auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
202  if (!TypeId)
203  break;
204  GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
205 
206  SmallVector<DevirtCallSite, 4> DevirtCalls;
209  bool HasNonCallUses = false;
210  findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
211  HasNonCallUses, CI, DT);
212  // Any non-call uses of the result of llvm.type.checked.load will
213  // prevent us from optimizing away the llvm.type.test.
214  if (HasNonCallUses)
215  TypeTests.insert(Guid);
216  for (auto &Call : DevirtCalls)
217  addVCallToSet(Call, Guid, TypeCheckedLoadVCalls,
218  TypeCheckedLoadConstVCalls);
219 
220  break;
221  }
222  default:
223  break;
224  }
225 }
226 
227 static bool isNonVolatileLoad(const Instruction *I) {
228  if (const auto *LI = dyn_cast<LoadInst>(I))
229  return !LI->isVolatile();
230 
231  return false;
232 }
233 
235  const Function &F, BlockFrequencyInfo *BFI,
237  bool HasLocalsInUsedOrAsm,
238  DenseSet<GlobalValue::GUID> &CantBePromoted,
239  bool IsThinLTO) {
240  // Summary not currently supported for anonymous functions, they should
241  // have been named.
242  assert(F.hasName());
243 
244  unsigned NumInsts = 0;
245  // Map from callee ValueId to profile count. Used to accumulate profile
246  // counts for all static calls to a given callee.
247  MapVector<ValueInfo, CalleeInfo> CallGraphEdges;
248  SetVector<ValueInfo> RefEdges;
250  SetVector<FunctionSummary::VFuncId> TypeTestAssumeVCalls,
251  TypeCheckedLoadVCalls;
252  SetVector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls,
253  TypeCheckedLoadConstVCalls;
254  ICallPromotionAnalysis ICallAnalysis;
256 
257  // Add personality function, prefix data and prologue data to function's ref
258  // list.
259  findRefEdges(Index, &F, RefEdges, Visited);
260  std::vector<const Instruction *> NonVolatileLoads;
261 
262  bool HasInlineAsmMaybeReferencingInternal = false;
263  for (const BasicBlock &BB : F)
264  for (const Instruction &I : BB) {
265  if (isa<DbgInfoIntrinsic>(I))
266  continue;
267  ++NumInsts;
268  if (isNonVolatileLoad(&I)) {
269  // Postpone processing of non-volatile load instructions
270  // See comments below
271  Visited.insert(&I);
272  NonVolatileLoads.push_back(&I);
273  continue;
274  }
275  findRefEdges(Index, &I, RefEdges, Visited);
276  auto CS = ImmutableCallSite(&I);
277  if (!CS)
278  continue;
279 
280  const auto *CI = dyn_cast<CallInst>(&I);
281  // Since we don't know exactly which local values are referenced in inline
282  // assembly, conservatively mark the function as possibly referencing
283  // a local value from inline assembly to ensure we don't export a
284  // reference (which would require renaming and promotion of the
285  // referenced value).
286  if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm())
287  HasInlineAsmMaybeReferencingInternal = true;
288 
289  auto *CalledValue = CS.getCalledValue();
290  auto *CalledFunction = CS.getCalledFunction();
291  if (CalledValue && !CalledFunction) {
292  CalledValue = CalledValue->stripPointerCastsNoFollowAliases();
293  // Stripping pointer casts can reveal a called function.
294  CalledFunction = dyn_cast<Function>(CalledValue);
295  }
296  // Check if this is an alias to a function. If so, get the
297  // called aliasee for the checks below.
298  if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
299  assert(!CalledFunction && "Expected null called function in callsite for alias");
300  CalledFunction = dyn_cast<Function>(GA->getBaseObject());
301  }
302  // Check if this is a direct call to a known function or a known
303  // intrinsic, or an indirect call with profile data.
304  if (CalledFunction) {
305  if (CI && CalledFunction->isIntrinsic()) {
307  CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls,
308  TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls, DT);
309  continue;
310  }
311  // We should have named any anonymous globals
312  assert(CalledFunction->hasName());
313  auto ScaledCount = PSI->getProfileCount(&I, BFI);
314  auto Hotness = ScaledCount ? getHotness(ScaledCount.getValue(), PSI)
318 
319  // Use the original CalledValue, in case it was an alias. We want
320  // to record the call edge to the alias in that case. Eventually
321  // an alias summary will be created to associate the alias and
322  // aliasee.
323  auto &ValueInfo = CallGraphEdges[Index.getOrInsertValueInfo(
324  cast<GlobalValue>(CalledValue))];
325  ValueInfo.updateHotness(Hotness);
326  // Add the relative block frequency to CalleeInfo if there is no profile
327  // information.
328  if (BFI != nullptr && Hotness == CalleeInfo::HotnessType::Unknown) {
329  uint64_t BBFreq = BFI->getBlockFreq(&BB).getFrequency();
330  uint64_t EntryFreq = BFI->getEntryFreq();
331  ValueInfo.updateRelBlockFreq(BBFreq, EntryFreq);
332  }
333  } else {
334  // Skip inline assembly calls.
335  if (CI && CI->isInlineAsm())
336  continue;
337  // Skip direct calls.
338  if (!CalledValue || isa<Constant>(CalledValue))
339  continue;
340 
341  // Check if the instruction has a callees metadata. If so, add callees
342  // to CallGraphEdges to reflect the references from the metadata, and
343  // to enable importing for subsequent indirect call promotion and
344  // inlining.
345  if (auto *MD = I.getMetadata(LLVMContext::MD_callees)) {
346  for (auto &Op : MD->operands()) {
347  Function *Callee = mdconst::extract_or_null<Function>(Op);
348  if (Callee)
349  CallGraphEdges[Index.getOrInsertValueInfo(Callee)];
350  }
351  }
352 
353  uint32_t NumVals, NumCandidates;
354  uint64_t TotalCount;
355  auto CandidateProfileData =
357  &I, NumVals, TotalCount, NumCandidates);
358  for (auto &Candidate : CandidateProfileData)
359  CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)]
360  .updateHotness(getHotness(Candidate.Count, PSI));
361  }
362  }
363 
364  // By now we processed all instructions in a function, except
365  // non-volatile loads. All new refs we add in a loop below
366  // are obviously constant. All constant refs are grouped in the
367  // end of RefEdges vector, so we can use a single integer value
368  // to identify them.
369  unsigned RefCnt = RefEdges.size();
370  for (const Instruction *I : NonVolatileLoads) {
371  Visited.erase(I);
372  findRefEdges(Index, I, RefEdges, Visited);
373  }
374  std::vector<ValueInfo> Refs = RefEdges.takeVector();
375  // Regular LTO module doesn't participate in ThinLTO import,
376  // so no reference from it can be readonly, since this would
377  // require importing variable as local copy
378  if (IsThinLTO)
379  for (; RefCnt < Refs.size(); ++RefCnt)
380  Refs[RefCnt].setReadOnly();
381 
382  // Explicit add hot edges to enforce importing for designated GUIDs for
383  // sample PGO, to enable the same inlines as the profiled optimized binary.
384  for (auto &I : F.getImportGUIDs())
385  CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness(
389 
390  bool NonRenamableLocal = isNonRenamableLocal(F);
391  bool NotEligibleForImport =
392  NonRenamableLocal || HasInlineAsmMaybeReferencingInternal;
393  GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport,
394  /* Live = */ false, F.isDSOLocal());
395  FunctionSummary::FFlags FunFlags{
396  F.hasFnAttribute(Attribute::ReadNone),
397  F.hasFnAttribute(Attribute::ReadOnly),
398  F.hasFnAttribute(Attribute::NoRecurse), F.returnDoesNotAlias(),
399  // FIXME: refactor this to use the same code that inliner is using.
400  // Don't try to import functions with noinline attribute.
401  F.getAttributes().hasFnAttribute(Attribute::NoInline)};
402  auto FuncSummary = llvm::make_unique<FunctionSummary>(
403  Flags, NumInsts, FunFlags, /*EntryCount=*/0, std::move(Refs),
404  CallGraphEdges.takeVector(), TypeTests.takeVector(),
405  TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(),
406  TypeTestAssumeConstVCalls.takeVector(),
407  TypeCheckedLoadConstVCalls.takeVector());
408  if (NonRenamableLocal)
409  CantBePromoted.insert(F.getGUID());
410  Index.addGlobalValueSummary(F, std::move(FuncSummary));
411 }
412 
413 static void
415  DenseSet<GlobalValue::GUID> &CantBePromoted) {
416  SetVector<ValueInfo> RefEdges;
418  bool HasBlockAddress = findRefEdges(Index, &V, RefEdges, Visited);
419  bool NonRenamableLocal = isNonRenamableLocal(V);
420  GlobalValueSummary::GVFlags Flags(V.getLinkage(), NonRenamableLocal,
421  /* Live = */ false, V.isDSOLocal());
422 
423  // Don't mark variables we won't be able to internalize as read-only.
425  !V.hasComdat() && !V.hasAppendingLinkage() && !V.isInterposable() &&
427  auto GVarSummary = llvm::make_unique<GlobalVarSummary>(Flags, VarFlags,
428  RefEdges.takeVector());
429  if (NonRenamableLocal)
430  CantBePromoted.insert(V.getGUID());
431  if (HasBlockAddress)
432  GVarSummary->setNotEligibleToImport();
433  Index.addGlobalValueSummary(V, std::move(GVarSummary));
434 }
435 
436 static void
438  DenseSet<GlobalValue::GUID> &CantBePromoted) {
439  bool NonRenamableLocal = isNonRenamableLocal(A);
440  GlobalValueSummary::GVFlags Flags(A.getLinkage(), NonRenamableLocal,
441  /* Live = */ false, A.isDSOLocal());
442  auto AS = llvm::make_unique<AliasSummary>(Flags);
443  auto *Aliasee = A.getBaseObject();
444  auto AliaseeVI = Index.getValueInfo(Aliasee->getGUID());
445  assert(AliaseeVI && "Alias expects aliasee summary to be available");
446  assert(AliaseeVI.getSummaryList().size() == 1 &&
447  "Expected a single entry per aliasee in per-module index");
448  AS->setAliasee(AliaseeVI, AliaseeVI.getSummaryList()[0].get());
449  if (NonRenamableLocal)
450  CantBePromoted.insert(A.getGUID());
451  Index.addGlobalValueSummary(A, std::move(AS));
452 }
453 
454 // Set LiveRoot flag on entries matching the given value name.
456  if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
457  for (auto &Summary : VI.getSummaryList())
458  Summary->setLive(true);
459 }
460 
462  const Module &M,
463  std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
464  ProfileSummaryInfo *PSI) {
465  assert(PSI);
466  bool EnableSplitLTOUnit = false;
467  if (auto *MD = mdconst::extract_or_null<ConstantInt>(
468  M.getModuleFlag("EnableSplitLTOUnit")))
469  EnableSplitLTOUnit = MD->getZExtValue();
470  ModuleSummaryIndex Index(/*HaveGVs=*/true, EnableSplitLTOUnit);
471 
472  // Identify the local values in the llvm.used and llvm.compiler.used sets,
473  // which should not be exported as they would then require renaming and
474  // promotion, but we may have opaque uses e.g. in inline asm. We collect them
475  // here because we use this information to mark functions containing inline
476  // assembly calls as not importable.
479  // First collect those in the llvm.used set.
480  collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
481  // Next collect those in the llvm.compiler.used set.
482  collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
483  DenseSet<GlobalValue::GUID> CantBePromoted;
484  for (auto *V : Used) {
485  if (V->hasLocalLinkage()) {
486  LocalsUsed.insert(V);
487  CantBePromoted.insert(V->getGUID());
488  }
489  }
490 
491  bool HasLocalInlineAsmSymbol = false;
492  if (!M.getModuleInlineAsm().empty()) {
493  // Collect the local values defined by module level asm, and set up
494  // summaries for these symbols so that they can be marked as NoRename,
495  // to prevent export of any use of them in regular IR that would require
496  // renaming within the module level asm. Note we don't need to create a
497  // summary for weak or global defs, as they don't need to be flagged as
498  // NoRename, and defs in module level asm can't be imported anyway.
499  // Also, any values used but not defined within module level asm should
500  // be listed on the llvm.used or llvm.compiler.used global and marked as
501  // referenced from there.
504  // Symbols not marked as Weak or Global are local definitions.
505  if (Flags & (object::BasicSymbolRef::SF_Weak |
507  return;
508  HasLocalInlineAsmSymbol = true;
509  GlobalValue *GV = M.getNamedValue(Name);
510  if (!GV)
511  return;
512  assert(GV->isDeclaration() && "Def in module asm already has definition");
514  /* NotEligibleToImport = */ true,
515  /* Live = */ true,
516  /* Local */ GV->isDSOLocal());
517  CantBePromoted.insert(GV->getGUID());
518  // Create the appropriate summary type.
519  if (Function *F = dyn_cast<Function>(GV)) {
520  std::unique_ptr<FunctionSummary> Summary =
521  llvm::make_unique<FunctionSummary>(
522  GVFlags, /*InstCount=*/0,
524  F->hasFnAttribute(Attribute::ReadNone),
525  F->hasFnAttribute(Attribute::ReadOnly),
526  F->hasFnAttribute(Attribute::NoRecurse),
527  F->returnDoesNotAlias(),
528  /* NoInline = */ false},
529  /*EntryCount=*/0, ArrayRef<ValueInfo>{},
536  Index.addGlobalValueSummary(*GV, std::move(Summary));
537  } else {
538  std::unique_ptr<GlobalVarSummary> Summary =
539  llvm::make_unique<GlobalVarSummary>(
540  GVFlags, GlobalVarSummary::GVarFlags(),
542  Index.addGlobalValueSummary(*GV, std::move(Summary));
543  }
544  });
545  }
546 
547  bool IsThinLTO = true;
548  if (auto *MD =
549  mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
550  IsThinLTO = MD->getZExtValue();
551 
552  // Compute summaries for all functions defined in module, and save in the
553  // index.
554  for (auto &F : M) {
555  if (F.isDeclaration())
556  continue;
557 
558  DominatorTree DT(const_cast<Function &>(F));
559  BlockFrequencyInfo *BFI = nullptr;
560  std::unique_ptr<BlockFrequencyInfo> BFIPtr;
561  if (GetBFICallback)
562  BFI = GetBFICallback(F);
563  else if (F.hasProfileData()) {
564  LoopInfo LI{DT};
565  BranchProbabilityInfo BPI{F, LI};
566  BFIPtr = llvm::make_unique<BlockFrequencyInfo>(F, BPI, LI);
567  BFI = BFIPtr.get();
568  }
569 
570  computeFunctionSummary(Index, M, F, BFI, PSI, DT,
571  !LocalsUsed.empty() || HasLocalInlineAsmSymbol,
572  CantBePromoted, IsThinLTO);
573  }
574 
575  // Compute summaries for all variables defined in module, and save in the
576  // index.
577  for (const GlobalVariable &G : M.globals()) {
578  if (G.isDeclaration())
579  continue;
580  computeVariableSummary(Index, G, CantBePromoted);
581  }
582 
583  // Compute summaries for all aliases defined in module, and save in the
584  // index.
585  for (const GlobalAlias &A : M.aliases())
586  computeAliasSummary(Index, A, CantBePromoted);
587 
588  for (auto *V : LocalsUsed) {
589  auto *Summary = Index.getGlobalValueSummary(*V);
590  assert(Summary && "Missing summary for global value");
591  Summary->setNotEligibleToImport();
592  }
593 
594  // The linker doesn't know about these LLVM produced values, so we need
595  // to flag them as live in the index to ensure index-based dead value
596  // analysis treats them as live roots of the analysis.
597  setLiveRoot(Index, "llvm.used");
598  setLiveRoot(Index, "llvm.compiler.used");
599  setLiveRoot(Index, "llvm.global_ctors");
600  setLiveRoot(Index, "llvm.global_dtors");
601  setLiveRoot(Index, "llvm.global.annotations");
602 
603  for (auto &GlobalList : Index) {
604  // Ignore entries for references that are undefined in the current module.
605  if (GlobalList.second.SummaryList.empty())
606  continue;
607 
608  assert(GlobalList.second.SummaryList.size() == 1 &&
609  "Expected module's index to have one summary per GUID");
610  auto &Summary = GlobalList.second.SummaryList[0];
611  if (!IsThinLTO) {
612  Summary->setNotEligibleToImport();
613  continue;
614  }
615 
616  bool AllRefsCanBeExternallyReferenced =
617  llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) {
618  return !CantBePromoted.count(VI.getGUID());
619  });
620  if (!AllRefsCanBeExternallyReferenced) {
621  Summary->setNotEligibleToImport();
622  continue;
623  }
624 
625  if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) {
626  bool AllCallsCanBeExternallyReferenced = llvm::all_of(
627  FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
628  return !CantBePromoted.count(Edge.first.getGUID());
629  });
630  if (!AllCallsCanBeExternallyReferenced)
631  Summary->setNotEligibleToImport();
632  }
633  }
634 
635  if (!ModuleSummaryDotFile.empty()) {
636  std::error_code EC;
638  if (EC)
639  report_fatal_error(Twine("Failed to open dot file ") +
640  ModuleSummaryDotFile + ": " + EC.message() + "\n");
641  Index.exportToDot(OSDot);
642  }
643 
644  return Index;
645 }
646 
647 AnalysisKey ModuleSummaryIndexAnalysis::Key;
648 
652  auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
654  M,
655  [&FAM](const Function &F) {
656  return &FAM.getResult<BlockFrequencyAnalysis>(
657  *const_cast<Function *>(&F));
658  },
659  &PSI);
660 }
661 
663 
664 INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
665  "Module Summary Analysis", false, true)
669  "Module Summary Analysis", false, true)
670 
672  return new ModuleSummaryIndexWrapperPass();
673 }
674 
676  : ModulePass(ID) {
678 }
679 
681  auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
682  Index.emplace(buildModuleSummaryIndex(
683  M,
684  [this](const Function &F) {
685  return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>(
686  *const_cast<Function *>(&F))
687  .getBFI());
688  },
689  PSI));
690  return false;
691 }
692 
694  Index.reset();
695  return false;
696 }
697 
699  AU.setPreservesAll();
702 }
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.
IterTy arg_end() const
Definition: CallSite.h:583
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
IterTy arg_begin() const
Definition: CallSite.h:579
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
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:65
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
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:1155
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
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
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
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
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...
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
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:310
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:643
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...
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
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:598
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)
cl::opt< std::string > ModuleSummaryDotFile("module-summary-dot-file", cl::init(""), cl::Hidden, cl::value_desc("filename"), cl::desc("File to emit dot graph of new summary into."))
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:841
Module.h This file contains the declarations for the Module class.
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:374
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)
amdgpu Simplify well known AMD library false FunctionCallee Callee
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:618
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.
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:365
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:892
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1201
#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:248
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:205
bool isCallee(Value::const_user_iterator UI) const
Determine whether the passed iterator points to the callee operand&#39;s Use.
Definition: CallSite.h:150
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:439
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