SummaryBasedOptimizations.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //==-SummaryBasedOptimizations.cpp - Optimizations based on ThinLTO summary-==//
  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 file implements optimizations that are based on the module summaries.
  10. // These optimizations are performed during the thinlink phase of the
  11. // compilation.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/LTO/SummaryBasedOptimizations.h"
  15. #include "llvm/Analysis/SyntheticCountsUtils.h"
  16. #include "llvm/IR/ModuleSummaryIndex.h"
  17. #include "llvm/Support/CommandLine.h"
  18. using namespace llvm;
  19. static cl::opt<bool> ThinLTOSynthesizeEntryCounts(
  20. "thinlto-synthesize-entry-counts", cl::init(false), cl::Hidden,
  21. cl::desc("Synthesize entry counts based on the summary"));
  22. namespace llvm {
  23. extern cl::opt<int> InitialSyntheticCount;
  24. }
  25. static void initializeCounts(ModuleSummaryIndex &Index) {
  26. auto Root = Index.calculateCallGraphRoot();
  27. // Root is a fake node. All its successors are the actual roots of the
  28. // callgraph.
  29. // FIXME: This initializes the entry counts of only the root nodes. This makes
  30. // sense when compiling a binary with ThinLTO, but for libraries any of the
  31. // non-root nodes could be called from outside.
  32. for (auto &C : Root.calls()) {
  33. auto &V = C.first;
  34. for (auto &GVS : V.getSummaryList()) {
  35. auto S = GVS.get()->getBaseObject();
  36. auto *F = cast<FunctionSummary>(S);
  37. F->setEntryCount(InitialSyntheticCount);
  38. }
  39. }
  40. }
  41. void llvm::computeSyntheticCounts(ModuleSummaryIndex &Index) {
  42. if (!ThinLTOSynthesizeEntryCounts)
  43. return;
  44. using Scaled64 = ScaledNumber<uint64_t>;
  45. initializeCounts(Index);
  46. auto GetCallSiteRelFreq = [](FunctionSummary::EdgeTy &Edge) {
  47. return Scaled64(Edge.second.RelBlockFreq, -CalleeInfo::ScaleShift);
  48. };
  49. auto GetEntryCount = [](ValueInfo V) {
  50. if (V.getSummaryList().size()) {
  51. auto S = V.getSummaryList().front()->getBaseObject();
  52. auto *F = cast<FunctionSummary>(S);
  53. return F->entryCount();
  54. } else {
  55. return UINT64_C(0);
  56. }
  57. };
  58. auto AddToEntryCount = [](ValueInfo V, Scaled64 New) {
  59. if (!V.getSummaryList().size())
  60. return;
  61. for (auto &GVS : V.getSummaryList()) {
  62. auto S = GVS.get()->getBaseObject();
  63. auto *F = cast<FunctionSummary>(S);
  64. F->setEntryCount(
  65. SaturatingAdd(F->entryCount(), New.template toInt<uint64_t>()));
  66. }
  67. };
  68. auto GetProfileCount = [&](ValueInfo V, FunctionSummary::EdgeTy &Edge) {
  69. auto RelFreq = GetCallSiteRelFreq(Edge);
  70. Scaled64 EC(GetEntryCount(V), 0);
  71. return RelFreq * EC;
  72. };
  73. // After initializing the counts in initializeCounts above, the counts have to
  74. // be propagated across the combined callgraph.
  75. // SyntheticCountsUtils::propagate takes care of this propagation on any
  76. // callgraph that specialized GraphTraits.
  77. SyntheticCountsUtils<ModuleSummaryIndex *>::propagate(&Index, GetProfileCount,
  78. AddToEntryCount);
  79. Index.setHasSyntheticEntryCounts();
  80. }