SummaryBasedOptimizations.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. extern cl::opt<int> InitialSyntheticCount;
  23. static void initializeCounts(ModuleSummaryIndex &Index) {
  24. auto Root = Index.calculateCallGraphRoot();
  25. // Root is a fake node. All its successors are the actual roots of the
  26. // callgraph.
  27. // FIXME: This initializes the entry counts of only the root nodes. This makes
  28. // sense when compiling a binary with ThinLTO, but for libraries any of the
  29. // non-root nodes could be called from outside.
  30. for (auto &C : Root.calls()) {
  31. auto &V = C.first;
  32. for (auto &GVS : V.getSummaryList()) {
  33. auto S = GVS.get()->getBaseObject();
  34. auto *F = cast<FunctionSummary>(S);
  35. F->setEntryCount(InitialSyntheticCount);
  36. }
  37. }
  38. }
  39. void llvm::computeSyntheticCounts(ModuleSummaryIndex &Index) {
  40. if (!ThinLTOSynthesizeEntryCounts)
  41. return;
  42. using Scaled64 = ScaledNumber<uint64_t>;
  43. initializeCounts(Index);
  44. auto GetCallSiteRelFreq = [](FunctionSummary::EdgeTy &Edge) {
  45. return Scaled64(Edge.second.RelBlockFreq, -CalleeInfo::ScaleShift);
  46. };
  47. auto GetEntryCount = [](ValueInfo V) {
  48. if (V.getSummaryList().size()) {
  49. auto S = V.getSummaryList().front().get()->getBaseObject();
  50. auto *F = cast<FunctionSummary>(S);
  51. return F->entryCount();
  52. } else {
  53. return UINT64_C(0);
  54. }
  55. };
  56. auto AddToEntryCount = [](ValueInfo V, Scaled64 New) {
  57. if (!V.getSummaryList().size())
  58. return;
  59. for (auto &GVS : V.getSummaryList()) {
  60. auto S = GVS.get()->getBaseObject();
  61. auto *F = cast<FunctionSummary>(S);
  62. F->setEntryCount(
  63. SaturatingAdd(F->entryCount(), New.template toInt<uint64_t>()));
  64. }
  65. };
  66. auto GetProfileCount = [&](ValueInfo V, FunctionSummary::EdgeTy &Edge) {
  67. auto RelFreq = GetCallSiteRelFreq(Edge);
  68. Scaled64 EC(GetEntryCount(V), 0);
  69. return RelFreq * EC;
  70. };
  71. // After initializing the counts in initializeCounts above, the counts have to
  72. // be propagated across the combined callgraph.
  73. // SyntheticCountsUtils::propagate takes care of this propagation on any
  74. // callgraph that specialized GraphTraits.
  75. SyntheticCountsUtils<ModuleSummaryIndex *>::propagate(&Index, GetProfileCount,
  76. AddToEntryCount);
  77. Index.setHasSyntheticEntryCounts();
  78. }