blob: af16fe6388416e7fd4bd41de5063a7076cfccd54 [file] [log] [blame]
Easwaran Raman019e0bf2016-06-03 22:54:26 +00001//===- ProfileSummaryInfo.cpp - Global profile summary information --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a pass that provides access to the global profile summary
11// information.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/ProfileSummaryInfo.h"
16#include "llvm/IR/Metadata.h"
17#include "llvm/IR/Module.h"
18#include "llvm/IR/ProfileSummary.h"
19using namespace llvm;
20
21// The following two parameters determine the threshold for a count to be
22// considered hot/cold. These two parameters are percentile values (multiplied
23// by 10000). If the counts are sorted in descending order, the minimum count to
24// reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
25// Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
26// threshold for determining cold count (everything <= this threshold is
27// considered cold).
28
29static cl::opt<int> ProfileSummaryCutoffHot(
30 "profile-summary-cutoff-hot", cl::Hidden, cl::init(999000), cl::ZeroOrMore,
31 cl::desc("A count is hot if it exceeds the minimum count to"
32 " reach this percentile of total counts."));
33
34static cl::opt<int> ProfileSummaryCutoffCold(
35 "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore,
36 cl::desc("A count is cold if it is below the minimum count"
37 " to reach this percentile of total counts."));
38
39// Find the minimum count to reach a desired percentile of counts.
40static uint64_t getMinCountForPercentile(SummaryEntryVector &DS,
41 uint64_t Percentile) {
42 auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) {
43 return Entry.Cutoff < Percentile;
44 };
45 auto It = std::lower_bound(DS.begin(), DS.end(), Percentile, Compare);
46 // The required percentile has to be <= one of the percentiles in the
47 // detailed summary.
48 if (It == DS.end())
49 report_fatal_error("Desired percentile exceeds the maximum cutoff");
50 return It->MinCount;
51}
52
53// The profile summary metadata may be attached either by the frontend or by
54// any backend passes (IR level instrumentation, for example). This method
55// checks if the Summary is null and if so checks if the summary metadata is now
56// available in the module and parses it to get the Summary object.
57void ProfileSummaryInfo::computeSummary() {
58 if (Summary)
59 return;
Dehao Chen5461d8b2016-09-28 21:00:58 +000060 auto *SummaryMD = M.getProfileSummary();
Easwaran Raman019e0bf2016-06-03 22:54:26 +000061 if (!SummaryMD)
62 return;
63 Summary.reset(ProfileSummary::getFromMD(SummaryMD));
64}
65
Dehao Chen84287ab2016-10-10 21:47:28 +000066/// Returns true if the function's entry is hot. If it returns false, it
67/// either means it is not hot or it is unknown whether it is hot or not (for
Piotr Padlewskif3d122c2016-09-30 21:05:49 +000068/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +000069bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +000070 computeSummary();
71 if (!F || !Summary)
72 return false;
73 auto FunctionCount = F->getEntryCount();
74 // FIXME: The heuristic used below for determining hotness is based on
75 // preliminary SPEC tuning for inliner. This will eventually be a
76 // convenience method that calls isHotCount.
Dehao Chenc87bc792016-10-11 05:19:00 +000077 return FunctionCount && isHotCount(FunctionCount.getValue());
Easwaran Raman019e0bf2016-06-03 22:54:26 +000078}
79
Dehao Chen84287ab2016-10-10 21:47:28 +000080/// Returns true if the function's entry is a cold. If it returns false, it
81/// either means it is not cold or it is unknown whether it is cold or not (for
Piotr Padlewskif3d122c2016-09-30 21:05:49 +000082/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +000083bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +000084 computeSummary();
Richard Trieufae70b62016-06-10 01:42:05 +000085 if (!F)
86 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000087 if (F->hasFnAttribute(Attribute::Cold)) {
88 return true;
89 }
Richard Trieufae70b62016-06-10 01:42:05 +000090 if (!Summary)
91 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000092 auto FunctionCount = F->getEntryCount();
93 // FIXME: The heuristic used below for determining coldness is based on
94 // preliminary SPEC tuning for inliner. This will eventually be a
95 // convenience method that calls isHotCount.
Dehao Chenc87bc792016-10-11 05:19:00 +000096 return FunctionCount && isColdCount(FunctionCount.getValue());
Easwaran Raman019e0bf2016-06-03 22:54:26 +000097}
98
Piotr Padlewskif3d122c2016-09-30 21:05:49 +000099/// Compute the hot and cold thresholds.
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000100void ProfileSummaryInfo::computeThresholds() {
101 if (!Summary)
102 computeSummary();
103 if (!Summary)
104 return;
105 auto &DetailedSummary = Summary->getDetailedSummary();
106 HotCountThreshold =
107 getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
108 ColdCountThreshold =
109 getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
110}
111
112bool ProfileSummaryInfo::isHotCount(uint64_t C) {
113 if (!HotCountThreshold)
114 computeThresholds();
115 return HotCountThreshold && C >= HotCountThreshold.getValue();
116}
117
118bool ProfileSummaryInfo::isColdCount(uint64_t C) {
119 if (!ColdCountThreshold)
120 computeThresholds();
121 return ColdCountThreshold && C <= ColdCountThreshold.getValue();
122}
123
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000124INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
125 "Profile summary info", false, true)
126
127ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
128 : ImmutablePass(ID) {
129 initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
130}
131
Dehao Chen5461d8b2016-09-28 21:00:58 +0000132bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
133 PSI.reset(new ProfileSummaryInfo(M));
134 return false;
135}
136
137bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
138 PSI.reset();
139 return false;
140}
141
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000142char ProfileSummaryAnalysis::PassID;
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000143ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
144 ModuleAnalysisManager &) {
Dehao Chen5461d8b2016-09-28 21:00:58 +0000145 return ProfileSummaryInfo(M);
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000146}
147
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000148PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
Sean Silvafd03ac62016-08-09 00:28:38 +0000149 ModuleAnalysisManager &AM) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000150 ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
151
152 OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
153 for (auto &F : M) {
154 OS << F.getName();
Dehao Chen84287ab2016-10-10 21:47:28 +0000155 if (PSI.isFunctionEntryHot(&F))
156 OS << " :hot entry ";
157 else if (PSI.isFunctionEntryCold(&F))
158 OS << " :cold entry ";
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000159 OS << "\n";
160 }
161 return PreservedAnalyses::all();
162}
163
164char ProfileSummaryInfoWrapperPass::ID = 0;