LLVM  13.0.0git
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 
19 
20 using namespace llvm;
21 
23  "thinlto-synthesize-entry-counts", cl::init(false), cl::Hidden,
24  cl::desc("Synthesize entry counts based on the summary"));
25 
26 namespace llvm {
28 }
29 
31  auto Root = Index.calculateCallGraphRoot();
32  // Root is a fake node. All its successors are the actual roots of the
33  // callgraph.
34  // FIXME: This initializes the entry counts of only the root nodes. This makes
35  // sense when compiling a binary with ThinLTO, but for libraries any of the
36  // non-root nodes could be called from outside.
37  for (auto &C : Root.calls()) {
38  auto &V = C.first;
39  for (auto &GVS : V.getSummaryList()) {
40  auto S = GVS.get()->getBaseObject();
41  auto *F = cast<FunctionSummary>(S);
42  F->setEntryCount(InitialSyntheticCount);
43  }
44  }
45 }
46 
49  return;
50 
53  auto GetCallSiteRelFreq = [](FunctionSummary::EdgeTy &Edge) {
54  return Scaled64(Edge.second.RelBlockFreq, -CalleeInfo::ScaleShift);
55  };
56  auto GetEntryCount = [](ValueInfo V) {
57  if (V.getSummaryList().size()) {
58  auto S = V.getSummaryList().front().get()->getBaseObject();
59  auto *F = cast<FunctionSummary>(S);
60  return F->entryCount();
61  } else {
62  return UINT64_C(0);
63  }
64  };
65  auto AddToEntryCount = [](ValueInfo V, Scaled64 New) {
66  if (!V.getSummaryList().size())
67  return;
68  for (auto &GVS : V.getSummaryList()) {
69  auto S = GVS.get()->getBaseObject();
70  auto *F = cast<FunctionSummary>(S);
71  F->setEntryCount(
72  SaturatingAdd(F->entryCount(), New.template toInt<uint64_t>()));
73  }
74  };
75 
76  auto GetProfileCount = [&](ValueInfo V, FunctionSummary::EdgeTy &Edge) {
77  auto RelFreq = GetCallSiteRelFreq(Edge);
78  Scaled64 EC(GetEntryCount(V), 0);
79  return RelFreq * EC;
80  };
81  // After initializing the counts in initializeCounts above, the counts have to
82  // be propagated across the combined callgraph.
83  // SyntheticCountsUtils::propagate takes care of this propagation on any
84  // callgraph that specialized GraphTraits.
86  AddToEntryCount);
87  Index.setHasSyntheticEntryCounts();
88 }
llvm::ValueInfo::getSummaryList
ArrayRef< std::unique_ptr< GlobalValueSummary > > getSummaryList() const
Definition: ModuleSummaryIndex.h:187
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::SaturatingAdd
std::enable_if_t< std::is_unsigned< T >::value, T > SaturatingAdd(T X, T Y, bool *ResultOverflowed=nullptr)
Add two unsigned integers, X and Y, of type T.
Definition: MathExtras.h:803
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::SyntheticCountsUtils::propagate
static void propagate(const CallGraphType &CG, GetProfCountTy GetProfCount, AddCountTy AddCount)
Propgate synthetic entry counts on a callgraph CG.
Definition: SyntheticCountsUtils.cpp:86
ModuleSummaryIndex.h
F
#define F(x, y, z)
Definition: MD5.cpp:56
initializeCounts
static void initializeCounts(ModuleSummaryIndex &Index)
Definition: SummaryBasedOptimizations.cpp:30
CommandLine.h
llvm::FunctionSummary::EdgeTy
std::pair< ValueInfo, CalleeInfo > EdgeTy
<CalleeValueInfo, CalleeInfo> call edge pair.
Definition: ModuleSummaryIndex.h:516
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::computeSyntheticCounts
void computeSyntheticCounts(ModuleSummaryIndex &Index)
Compute synthetic function entry counts.
Definition: SummaryBasedOptimizations.cpp:47
llvm::InitialSyntheticCount
cl::opt< int > InitialSyntheticCount
llvm::ValueInfo
Struct that holds a reference to a particular GUID in a global value summary.
Definition: ModuleSummaryIndex.h:168
llvm::cl::opt< bool >
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
SyntheticCountsUtils.h
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::ScaledNumber< uint64_t >
Scaled64
ScaledNumber< uint64_t > Scaled64
Definition: SyntheticCountsPropagation.cpp:42
ThinLTOSynthesizeEntryCounts
static cl::opt< bool > ThinLTOSynthesizeEntryCounts("thinlto-synthesize-entry-counts", cl::init(false), cl::Hidden, cl::desc("Synthesize entry counts based on the summary"))
llvm::ModuleSummaryIndex
Class to hold module path string table and global value map, and encapsulate methods for operating on...
Definition: ModuleSummaryIndex.h:1028
llvm::CalleeInfo::ScaleShift
static constexpr int32_t ScaleShift
Definition: ModuleSummaryIndex.h:73
llvm::cl::desc
Definition: CommandLine.h:414
SummaryBasedOptimizations.h