blob: 3c2e04de4a5411d736e608a648a4164e06abfd6f [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"
Easwaran Ramanb035f912017-01-13 01:34:00 +000016#include "llvm/Analysis/BlockFrequencyInfo.h"
Dehao Chen38a666d2016-11-09 23:36:02 +000017#include "llvm/IR/BasicBlock.h"
Easwaran Ramanb035f912017-01-13 01:34:00 +000018#include "llvm/IR/CallSite.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000019#include "llvm/IR/Metadata.h"
20#include "llvm/IR/Module.h"
21#include "llvm/IR/ProfileSummary.h"
22using namespace llvm;
23
24// The following two parameters determine the threshold for a count to be
25// considered hot/cold. These two parameters are percentile values (multiplied
26// by 10000). If the counts are sorted in descending order, the minimum count to
27// reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
28// Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
29// threshold for determining cold count (everything <= this threshold is
30// considered cold).
31
32static cl::opt<int> ProfileSummaryCutoffHot(
33 "profile-summary-cutoff-hot", cl::Hidden, cl::init(999000), cl::ZeroOrMore,
34 cl::desc("A count is hot if it exceeds the minimum count to"
35 " reach this percentile of total counts."));
36
37static cl::opt<int> ProfileSummaryCutoffCold(
38 "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore,
39 cl::desc("A count is cold if it is below the minimum count"
40 " to reach this percentile of total counts."));
41
Dehao Chenf58df392017-08-03 17:11:41 +000042static cl::opt<bool> AccurateSampleProfile(
43 "accurate-sample-profile", cl::Hidden, cl::init(false),
44 cl::desc("If the sample profile is accurate, we will mark all un-sampled "
45 "callsite as cold. Otherwise, treat un-sampled callsites as if "
46 "we have no profile."));
47
Easwaran Raman019e0bf2016-06-03 22:54:26 +000048// Find the minimum count to reach a desired percentile of counts.
49static uint64_t getMinCountForPercentile(SummaryEntryVector &DS,
50 uint64_t Percentile) {
51 auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) {
52 return Entry.Cutoff < Percentile;
53 };
54 auto It = std::lower_bound(DS.begin(), DS.end(), Percentile, Compare);
55 // The required percentile has to be <= one of the percentiles in the
56 // detailed summary.
57 if (It == DS.end())
58 report_fatal_error("Desired percentile exceeds the maximum cutoff");
59 return It->MinCount;
60}
61
62// The profile summary metadata may be attached either by the frontend or by
63// any backend passes (IR level instrumentation, for example). This method
64// checks if the Summary is null and if so checks if the summary metadata is now
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000065// available in the module and parses it to get the Summary object. Returns true
66// if a valid Summary is available.
67bool ProfileSummaryInfo::computeSummary() {
Easwaran Raman019e0bf2016-06-03 22:54:26 +000068 if (Summary)
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000069 return true;
Dehao Chen5461d8b2016-09-28 21:00:58 +000070 auto *SummaryMD = M.getProfileSummary();
Easwaran Raman019e0bf2016-06-03 22:54:26 +000071 if (!SummaryMD)
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000072 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000073 Summary.reset(ProfileSummary::getFromMD(SummaryMD));
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000074 return true;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000075}
76
Dehao Chenc2048152017-03-10 19:45:16 +000077Optional<uint64_t>
78ProfileSummaryInfo::getProfileCount(const Instruction *Inst,
79 BlockFrequencyInfo *BFI) {
80 if (!Inst)
81 return None;
82 assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) &&
83 "We can only get profile count for call/invoke instruction.");
Easwaran Ramandadc0f12017-05-16 20:14:39 +000084 if (hasSampleProfile()) {
Teresa Johnson2a6b7992017-05-11 23:18:05 +000085 // In sample PGO mode, check if there is a profile metadata on the
86 // instruction. If it is present, determine hotness solely based on that,
Dehao Chenf58df392017-08-03 17:11:41 +000087 // since the sampled entry count may not be accurate. If there is no
88 // annotated on the instruction, return None.
Teresa Johnson2a6b7992017-05-11 23:18:05 +000089 uint64_t TotalCount;
90 if (Inst->extractProfTotalWeight(TotalCount))
91 return TotalCount;
Dehao Chenf58df392017-08-03 17:11:41 +000092 return None;
Teresa Johnson2a6b7992017-05-11 23:18:05 +000093 }
Dehao Chenc2048152017-03-10 19:45:16 +000094 if (BFI)
95 return BFI->getBlockProfileCount(Inst->getParent());
96 return None;
97}
98
Dehao Chen84287ab2016-10-10 21:47:28 +000099/// Returns true if the function's entry is hot. If it returns false, it
100/// either means it is not hot or it is unknown whether it is hot or not (for
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000101/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +0000102bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000103 if (!F || !computeSummary())
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000104 return false;
105 auto FunctionCount = F->getEntryCount();
106 // FIXME: The heuristic used below for determining hotness is based on
107 // preliminary SPEC tuning for inliner. This will eventually be a
108 // convenience method that calls isHotCount.
Dehao Chenc87bc792016-10-11 05:19:00 +0000109 return FunctionCount && isHotCount(FunctionCount.getValue());
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000110}
111
Dehao Chen775341a2017-03-23 23:14:11 +0000112/// Returns true if the function's entry or total call edge count is hot.
113/// If it returns false, it either means it is not hot or it is unknown
114/// whether it is hot or not (for example, no profile data is available).
115bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F) {
116 if (!F || !computeSummary())
117 return false;
118 if (auto FunctionCount = F->getEntryCount())
119 if (isHotCount(FunctionCount.getValue()))
120 return true;
121
122 uint64_t TotalCallCount = 0;
123 for (const auto &BB : *F)
124 for (const auto &I : BB)
125 if (isa<CallInst>(I) || isa<InvokeInst>(I))
126 if (auto CallCount = getProfileCount(&I, nullptr))
127 TotalCallCount += CallCount.getValue();
128 return isHotCount(TotalCallCount);
129}
130
131/// Returns true if the function's entry and total call edge count is cold.
132/// If it returns false, it either means it is not cold or it is unknown
133/// whether it is cold or not (for example, no profile data is available).
134bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F) {
135 if (!F || !computeSummary())
136 return false;
137 if (auto FunctionCount = F->getEntryCount())
138 if (!isColdCount(FunctionCount.getValue()))
139 return false;
140
141 uint64_t TotalCallCount = 0;
142 for (const auto &BB : *F)
143 for (const auto &I : BB)
144 if (isa<CallInst>(I) || isa<InvokeInst>(I))
145 if (auto CallCount = getProfileCount(&I, nullptr))
146 TotalCallCount += CallCount.getValue();
147 return isColdCount(TotalCallCount);
148}
149
Dehao Chen84287ab2016-10-10 21:47:28 +0000150/// Returns true if the function's entry is a cold. If it returns false, it
151/// either means it is not cold or it is unknown whether it is cold or not (for
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000152/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +0000153bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
Richard Trieufae70b62016-06-10 01:42:05 +0000154 if (!F)
155 return false;
Davide Italiano574e5972017-03-10 20:50:51 +0000156 if (F->hasFnAttribute(Attribute::Cold))
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000157 return true;
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000158 if (!computeSummary())
Richard Trieufae70b62016-06-10 01:42:05 +0000159 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000160 auto FunctionCount = F->getEntryCount();
161 // FIXME: The heuristic used below for determining coldness is based on
162 // preliminary SPEC tuning for inliner. This will eventually be a
163 // convenience method that calls isHotCount.
Dehao Chenc87bc792016-10-11 05:19:00 +0000164 return FunctionCount && isColdCount(FunctionCount.getValue());
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000165}
166
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000167/// Compute the hot and cold thresholds.
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000168void ProfileSummaryInfo::computeThresholds() {
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000169 if (!computeSummary())
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000170 return;
171 auto &DetailedSummary = Summary->getDetailedSummary();
172 HotCountThreshold =
173 getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
174 ColdCountThreshold =
175 getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
176}
177
178bool ProfileSummaryInfo::isHotCount(uint64_t C) {
179 if (!HotCountThreshold)
180 computeThresholds();
181 return HotCountThreshold && C >= HotCountThreshold.getValue();
182}
183
184bool ProfileSummaryInfo::isColdCount(uint64_t C) {
185 if (!ColdCountThreshold)
186 computeThresholds();
187 return ColdCountThreshold && C <= ColdCountThreshold.getValue();
188}
189
Dehao Chen38a666d2016-11-09 23:36:02 +0000190bool ProfileSummaryInfo::isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI) {
191 auto Count = BFI->getBlockProfileCount(B);
Dehao Chen22645ee2017-03-10 01:44:37 +0000192 return Count && isHotCount(*Count);
Easwaran Ramanb035f912017-01-13 01:34:00 +0000193}
194
195bool ProfileSummaryInfo::isColdBB(const BasicBlock *B,
196 BlockFrequencyInfo *BFI) {
197 auto Count = BFI->getBlockProfileCount(B);
198 return Count && isColdCount(*Count);
199}
200
Easwaran Ramanb035f912017-01-13 01:34:00 +0000201bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS,
202 BlockFrequencyInfo *BFI) {
Dehao Chenc2048152017-03-10 19:45:16 +0000203 auto C = getProfileCount(CS.getInstruction(), BFI);
204 return C && isHotCount(*C);
Easwaran Ramanb035f912017-01-13 01:34:00 +0000205}
206
207bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS,
208 BlockFrequencyInfo *BFI) {
Dehao Chenc2048152017-03-10 19:45:16 +0000209 auto C = getProfileCount(CS.getInstruction(), BFI);
Dehao Chenf58df392017-08-03 17:11:41 +0000210 if (C)
211 return isColdCount(*C);
212
213 // In SamplePGO, if the caller has been sampled, and there is no profile
214 // annotatedon the callsite, we consider the callsite as cold.
215 // If there is no profile for the caller, and we know the profile is
216 // accurate, we consider the callsite as cold.
217 return (hasSampleProfile() &&
218 (CS.getCaller()->getEntryCount() || AccurateSampleProfile));
Dehao Chen38a666d2016-11-09 23:36:02 +0000219}
220
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000221INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
222 "Profile summary info", false, true)
223
224ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
225 : ImmutablePass(ID) {
226 initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
227}
228
Dehao Chen5461d8b2016-09-28 21:00:58 +0000229bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
230 PSI.reset(new ProfileSummaryInfo(M));
231 return false;
232}
233
234bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
235 PSI.reset();
236 return false;
237}
238
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000239AnalysisKey ProfileSummaryAnalysis::Key;
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000240ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
241 ModuleAnalysisManager &) {
Dehao Chen5461d8b2016-09-28 21:00:58 +0000242 return ProfileSummaryInfo(M);
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000243}
244
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000245PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
Sean Silvafd03ac62016-08-09 00:28:38 +0000246 ModuleAnalysisManager &AM) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000247 ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
248
249 OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
250 for (auto &F : M) {
251 OS << F.getName();
Dehao Chen84287ab2016-10-10 21:47:28 +0000252 if (PSI.isFunctionEntryHot(&F))
253 OS << " :hot entry ";
254 else if (PSI.isFunctionEntryCold(&F))
255 OS << " :cold entry ";
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000256 OS << "\n";
257 }
258 return PreservedAnalyses::all();
259}
260
261char ProfileSummaryInfoWrapperPass::ID = 0;