blob: e919fd530fb012c71caf65632a00675954347058 [file] [log] [blame]
Easwaran Raman5a7056f2018-12-13 19:54:27 +00001//==-SummaryBasedOptimizations.cpp - Optimizations based on ThinLTO summary-==//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Easwaran Raman5a7056f2018-12-13 19:54:27 +00006//
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
15#include "llvm/LTO/SummaryBasedOptimizations.h"
16#include "llvm/Analysis/SyntheticCountsUtils.h"
17#include "llvm/IR/ModuleSummaryIndex.h"
18
19using namespace llvm;
20
21cl::opt<bool> ThinLTOSynthesizeEntryCounts(
22 "thinlto-synthesize-entry-counts", cl::init(false), cl::Hidden,
23 cl::desc("Synthesize entry counts based on the summary"));
24
25extern cl::opt<int> InitialSyntheticCount;
26
27static void initializeCounts(ModuleSummaryIndex &Index) {
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
44void llvm::computeSyntheticCounts(ModuleSummaryIndex &Index) {
45 if (!ThinLTOSynthesizeEntryCounts)
46 return;
47
48 using Scaled64 = ScaledNumber<uint64_t>;
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 };
Easwaran Ramanb45994b2019-01-09 20:10:27 +000062 auto AddToEntryCount = [](ValueInfo V, Scaled64 New) {
Easwaran Raman5a7056f2018-12-13 19:54:27 +000063 if (!V.getSummaryList().size())
64 return;
65 for (auto &GVS : V.getSummaryList()) {
66 auto S = GVS.get()->getBaseObject();
67 auto *F = cast<FunctionSummary>(S);
Easwaran Ramanb45994b2019-01-09 20:10:27 +000068 F->setEntryCount(
69 SaturatingAdd(F->entryCount(), New.template toInt<uint64_t>()));
Easwaran Raman5a7056f2018-12-13 19:54:27 +000070 }
71 };
72
Easwaran Ramanb45994b2019-01-09 20:10:27 +000073 auto GetProfileCount = [&](ValueInfo V, FunctionSummary::EdgeTy &Edge) {
74 auto RelFreq = GetCallSiteRelFreq(Edge);
75 Scaled64 EC(GetEntryCount(V), 0);
76 return RelFreq * EC;
77 };
Easwaran Raman5a7056f2018-12-13 19:54:27 +000078 // 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.
Easwaran Ramanb45994b2019-01-09 20:10:27 +000082 SyntheticCountsUtils<ModuleSummaryIndex *>::propagate(&Index, GetProfileCount,
83 AddToEntryCount);
Easwaran Raman5a7056f2018-12-13 19:54:27 +000084 Index.setHasSyntheticEntryCounts();
85}