LLVM  10.0.0svn
SummaryBasedOptimizations.cpp
Go to the documentation of this file.
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 
18 
19 using namespace llvm;
20 
22  "thinlto-synthesize-entry-counts", cl::init(false), cl::Hidden,
23  cl::desc("Synthesize entry counts based on the summary"));
24 
26 
28  auto Root = Index.calculateCallGraphRoot();
29  // Root is a fake node. All its successors are the actual roots of the
30  // callgraph.
31  // FIXME: This initializes the entry counts of only the root nodes. This makes
32  // sense when compiling a binary with ThinLTO, but for libraries any of the
33  // non-root nodes could be called from outside.
34  for (auto &C : Root.calls()) {
35  auto &V = C.first;
36  for (auto &GVS : V.getSummaryList()) {
37  auto S = GVS.get()->getBaseObject();
38  auto *F = cast<FunctionSummary>(S);
39  F->setEntryCount(InitialSyntheticCount);
40  }
41  }
42 }
43 
46  return;
47 
49  initializeCounts(Index);
50  auto GetCallSiteRelFreq = [](FunctionSummary::EdgeTy &Edge) {
51  return Scaled64(Edge.second.RelBlockFreq, -CalleeInfo::ScaleShift);
52  };
53  auto GetEntryCount = [](ValueInfo V) {
54  if (V.getSummaryList().size()) {
55  auto S = V.getSummaryList().front().get()->getBaseObject();
56  auto *F = cast<FunctionSummary>(S);
57  return F->entryCount();
58  } else {
59  return UINT64_C(0);
60  }
61  };
62  auto AddToEntryCount = [](ValueInfo V, Scaled64 New) {
63  if (!V.getSummaryList().size())
64  return;
65  for (auto &GVS : V.getSummaryList()) {
66  auto S = GVS.get()->getBaseObject();
67  auto *F = cast<FunctionSummary>(S);
68  F->setEntryCount(
69  SaturatingAdd(F->entryCount(), New.template toInt<uint64_t>()));
70  }
71  };
72 
73  auto GetProfileCount = [&](ValueInfo V, FunctionSummary::EdgeTy &Edge) {
74  auto RelFreq = GetCallSiteRelFreq(Edge);
75  Scaled64 EC(GetEntryCount(V), 0);
76  return RelFreq * EC;
77  };
78  // After initializing the counts in initializeCounts above, the counts have to
79  // be propagated across the combined callgraph.
80  // SyntheticCountsUtils::propagate takes care of this propagation on any
81  // callgraph that specialized GraphTraits.
83  AddToEntryCount);
85 }
uint64_t CallInst * C
This class represents lattice values for constants.
Definition: AllocatorList.h:23
F(f)
std::enable_if< std::is_unsigned< T >::value, T >::type SaturatingAdd(T X, T Y, bool *ResultOverflowed=nullptr)
Add two unsigned integers, X and Y, of type T.
Definition: MathExtras.h:760
std::pair< ValueInfo, CalleeInfo > EdgeTy
<CalleeValueInfo, CalleeInfo> call edge pair.
static constexpr int32_t ScaleShift
Class to hold module path string table and global value map, and encapsulate methods for operating on...
ArrayRef< std::unique_ptr< GlobalValueSummary > > getSummaryList() const
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
static void propagate(const CallGraphType &CG, GetProfCountTy GetProfCount, AddCountTy AddCount)
Propgate synthetic entry counts on a callgraph CG.
static void initializeCounts(ModuleSummaryIndex &Index)
Struct that holds a reference to a particular GUID in a global value summary.
void computeSyntheticCounts(ModuleSummaryIndex &Index)
Compute synthetic function entry counts.
FunctionSummary calculateCallGraphRoot()
cl::opt< int > InitialSyntheticCount
Initial synthetic count assigned to functions.
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
ScaledNumber< uint64_t > Scaled64
static cl::opt< bool > ThinLTOSynthesizeEntryCounts("thinlto-synthesize-entry-counts", cl::init(false), cl::Hidden, cl::desc("Synthesize entry counts based on the summary"))