1 //==-SummaryBasedOptimizations.cpp - Optimizations based on ThinLTO summary-==//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements optimizations that are based on the module summaries.
10 // These optimizations are performed during the thinlink phase of the
13 //===----------------------------------------------------------------------===//
15 #include "llvm/LTO/SummaryBasedOptimizations.h"
16 #include "llvm/Analysis/SyntheticCountsUtils.h"
17 #include "llvm/IR/ModuleSummaryIndex.h"
21 static cl::opt
<bool> ThinLTOSynthesizeEntryCounts(
22 "thinlto-synthesize-entry-counts", cl::init(false), cl::Hidden
,
23 cl::desc("Synthesize entry counts based on the summary"));
25 extern cl::opt
<int> InitialSyntheticCount
;
27 static void initializeCounts(ModuleSummaryIndex
&Index
) {
28 auto Root
= Index
.calculateCallGraphRoot();
29 // Root is a fake node. All its successors are the actual roots of the
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()) {
36 for (auto &GVS
: V
.getSummaryList()) {
37 auto S
= GVS
.get()->getBaseObject();
38 auto *F
= cast
<FunctionSummary
>(S
);
39 F
->setEntryCount(InitialSyntheticCount
);
44 void llvm::computeSyntheticCounts(ModuleSummaryIndex
&Index
) {
45 if (!ThinLTOSynthesizeEntryCounts
)
48 using Scaled64
= ScaledNumber
<uint64_t>;
49 initializeCounts(Index
);
50 auto GetCallSiteRelFreq
= [](FunctionSummary::EdgeTy
&Edge
) {
51 return Scaled64(Edge
.second
.RelBlockFreq
, -CalleeInfo::ScaleShift
);
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();
62 auto AddToEntryCount
= [](ValueInfo V
, Scaled64 New
) {
63 if (!V
.getSummaryList().size())
65 for (auto &GVS
: V
.getSummaryList()) {
66 auto S
= GVS
.get()->getBaseObject();
67 auto *F
= cast
<FunctionSummary
>(S
);
69 SaturatingAdd(F
->entryCount(), New
.template toInt
<uint64_t>()));
73 auto GetProfileCount
= [&](ValueInfo V
, FunctionSummary::EdgeTy
&Edge
) {
74 auto RelFreq
= GetCallSiteRelFreq(Edge
);
75 Scaled64
EC(GetEntryCount(V
), 0);
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.
82 SyntheticCountsUtils
<ModuleSummaryIndex
*>::propagate(&Index
, GetProfileCount
,
84 Index
.setHasSyntheticEntryCounts();