annotate lib/Analysis/ProfileSummaryInfo.cpp @ 128:c347d3398279 default tip

fix
author mir3636
date Wed, 06 Dec 2017 14:37:17 +0900
parents 803732b1fca8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
1 //===- ProfileSummaryInfo.cpp - Global profile summary information --------===//
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
2 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
4 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
7 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
9 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
10 // This file contains a pass that provides access to the global profile summary
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
11 // information.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
12 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
13 //===----------------------------------------------------------------------===//
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
14
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
15 #include "llvm/Analysis/ProfileSummaryInfo.h"
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
16 #include "llvm/Analysis/BlockFrequencyInfo.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
17 #include "llvm/IR/BasicBlock.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
18 #include "llvm/IR/CallSite.h"
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
19 #include "llvm/IR/Metadata.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
20 #include "llvm/IR/Module.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
21 #include "llvm/IR/ProfileSummary.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
22 using namespace llvm;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
23
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
24 // The following two parameters determine the threshold for a count to be
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
25 // considered hot/cold. These two parameters are percentile values (multiplied
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
26 // by 10000). If the counts are sorted in descending order, the minimum count to
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
27 // reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
28 // Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
29 // threshold for determining cold count (everything <= this threshold is
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
30 // considered cold).
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
31
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
32 static cl::opt<int> ProfileSummaryCutoffHot(
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
33 "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore,
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
34 cl::desc("A count is hot if it exceeds the minimum count to"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
35 " reach this percentile of total counts."));
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
36
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
37 static cl::opt<int> ProfileSummaryCutoffCold(
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
38 "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore,
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
39 cl::desc("A count is cold if it is below the minimum count"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
40 " to reach this percentile of total counts."));
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
41
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
42 static cl::opt<bool> ProfileSampleAccurate(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
43 "profile-sample-accurate", cl::Hidden, cl::init(false),
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
44 cl::desc("If the sample profile is accurate, we will mark all un-sampled "
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
45 "callsite as cold. Otherwise, treat un-sampled callsites as if "
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
46 "we have no profile."));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
47 static cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
48 "profile-summary-huge-working-set-size-threshold", cl::Hidden,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
49 cl::init(15000), cl::ZeroOrMore,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
50 cl::desc("The code working set size is considered huge if the number of"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
51 " blocks required to reach the -profile-summary-cutoff-hot"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
52 " percentile exceeds this count."));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
53
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
54 // Find the summary entry for a desired percentile of counts.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
55 static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
56 uint64_t Percentile) {
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
57 auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
58 return Entry.Cutoff < Percentile;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
59 };
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
60 auto It = std::lower_bound(DS.begin(), DS.end(), Percentile, Compare);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
61 // The required percentile has to be <= one of the percentiles in the
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
62 // detailed summary.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
63 if (It == DS.end())
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
64 report_fatal_error("Desired percentile exceeds the maximum cutoff");
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
65 return *It;
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
66 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
67
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
68 // The profile summary metadata may be attached either by the frontend or by
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
69 // any backend passes (IR level instrumentation, for example). This method
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
70 // checks if the Summary is null and if so checks if the summary metadata is now
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
71 // available in the module and parses it to get the Summary object. Returns true
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
72 // if a valid Summary is available.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
73 bool ProfileSummaryInfo::computeSummary() {
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
74 if (Summary)
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
75 return true;
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
76 auto *SummaryMD = M.getProfileSummary();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
77 if (!SummaryMD)
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
78 return false;
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
79 Summary.reset(ProfileSummary::getFromMD(SummaryMD));
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
80 return true;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
81 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
82
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
83 Optional<uint64_t>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
84 ProfileSummaryInfo::getProfileCount(const Instruction *Inst,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
85 BlockFrequencyInfo *BFI) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
86 if (!Inst)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
87 return None;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
88 assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) &&
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
89 "We can only get profile count for call/invoke instruction.");
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
90 if (hasSampleProfile()) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
91 // In sample PGO mode, check if there is a profile metadata on the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
92 // instruction. If it is present, determine hotness solely based on that,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
93 // since the sampled entry count may not be accurate. If there is no
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
94 // annotated on the instruction, return None.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
95 uint64_t TotalCount;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
96 if (Inst->extractProfTotalWeight(TotalCount))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
97 return TotalCount;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
98 return None;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
99 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
100 if (BFI)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
101 return BFI->getBlockProfileCount(Inst->getParent());
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
102 return None;
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
103 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
104
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
105 /// Returns true if the function's entry is hot. If it returns false, it
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
106 /// either means it is not hot or it is unknown whether it is hot or not (for
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
107 /// example, no profile data is available).
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
108 bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
109 if (!F || !computeSummary())
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
110 return false;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
111 auto FunctionCount = F->getEntryCount();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
112 // FIXME: The heuristic used below for determining hotness is based on
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
113 // preliminary SPEC tuning for inliner. This will eventually be a
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
114 // convenience method that calls isHotCount.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
115 return FunctionCount && isHotCount(FunctionCount.getValue());
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
116 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
117
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
118 /// Returns true if the function's entry or total call edge count is hot.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
119 /// If it returns false, it either means it is not hot or it is unknown
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
120 /// whether it is hot or not (for example, no profile data is available).
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
121 bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
122 if (!F || !computeSummary())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
123 return false;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
124 if (auto FunctionCount = F->getEntryCount())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
125 if (isHotCount(FunctionCount.getValue()))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
126 return true;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
127
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
128 uint64_t TotalCallCount = 0;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
129 for (const auto &BB : *F)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
130 for (const auto &I : BB)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
131 if (isa<CallInst>(I) || isa<InvokeInst>(I))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
132 if (auto CallCount = getProfileCount(&I, nullptr))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
133 TotalCallCount += CallCount.getValue();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
134 return isHotCount(TotalCallCount);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
135 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
136
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
137 /// Returns true if the function's entry and total call edge count is cold.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
138 /// If it returns false, it either means it is not cold or it is unknown
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
139 /// whether it is cold or not (for example, no profile data is available).
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
140 bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
141 if (!F || !computeSummary())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
142 return false;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
143 if (auto FunctionCount = F->getEntryCount())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
144 if (!isColdCount(FunctionCount.getValue()))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
145 return false;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
146
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
147 uint64_t TotalCallCount = 0;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
148 for (const auto &BB : *F)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
149 for (const auto &I : BB)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
150 if (isa<CallInst>(I) || isa<InvokeInst>(I))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
151 if (auto CallCount = getProfileCount(&I, nullptr))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
152 TotalCallCount += CallCount.getValue();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
153 return isColdCount(TotalCallCount);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
154 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
155
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
156 /// Returns true if the function's entry is a cold. If it returns false, it
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
157 /// either means it is not cold or it is unknown whether it is cold or not (for
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
158 /// example, no profile data is available).
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
159 bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
160 if (!F)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
161 return false;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
162 if (F->hasFnAttribute(Attribute::Cold))
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
163 return true;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
164 if (!computeSummary())
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
165 return false;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
166 auto FunctionCount = F->getEntryCount();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
167 // FIXME: The heuristic used below for determining coldness is based on
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
168 // preliminary SPEC tuning for inliner. This will eventually be a
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
169 // convenience method that calls isHotCount.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
170 return FunctionCount && isColdCount(FunctionCount.getValue());
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
171 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
172
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
173 /// Compute the hot and cold thresholds.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
174 void ProfileSummaryInfo::computeThresholds() {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
175 if (!computeSummary())
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
176 return;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
177 auto &DetailedSummary = Summary->getDetailedSummary();
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
178 auto &HotEntry =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
179 getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
180 HotCountThreshold = HotEntry.MinCount;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
181 auto &ColdEntry =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
182 getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
183 ColdCountThreshold = ColdEntry.MinCount;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
184 HasHugeWorkingSetSize =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
185 HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
186 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
187
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
188 bool ProfileSummaryInfo::hasHugeWorkingSetSize() {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
189 if (!HasHugeWorkingSetSize)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
190 computeThresholds();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
191 return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue();
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
192 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
193
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
194 bool ProfileSummaryInfo::isHotCount(uint64_t C) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
195 if (!HotCountThreshold)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
196 computeThresholds();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
197 return HotCountThreshold && C >= HotCountThreshold.getValue();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
198 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
199
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
200 bool ProfileSummaryInfo::isColdCount(uint64_t C) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
201 if (!ColdCountThreshold)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
202 computeThresholds();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
203 return ColdCountThreshold && C <= ColdCountThreshold.getValue();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
204 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
205
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
206 bool ProfileSummaryInfo::isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
207 auto Count = BFI->getBlockProfileCount(B);
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
208 return Count && isHotCount(*Count);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
209 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
210
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
211 bool ProfileSummaryInfo::isColdBB(const BasicBlock *B,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
212 BlockFrequencyInfo *BFI) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
213 auto Count = BFI->getBlockProfileCount(B);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
214 return Count && isColdCount(*Count);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
215 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
216
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
217 bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
218 BlockFrequencyInfo *BFI) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
219 auto C = getProfileCount(CS.getInstruction(), BFI);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
220 return C && isHotCount(*C);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
221 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
222
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
223 bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
224 BlockFrequencyInfo *BFI) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
225 auto C = getProfileCount(CS.getInstruction(), BFI);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
226 if (C)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
227 return isColdCount(*C);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
228
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
229 // In SamplePGO, if the caller has been sampled, and there is no profile
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
230 // annotatedon the callsite, we consider the callsite as cold.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
231 // If there is no profile for the caller, and we know the profile is
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
232 // accurate, we consider the callsite as cold.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
233 return (hasSampleProfile() &&
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
234 (CS.getCaller()->getEntryCount() || ProfileSampleAccurate ||
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
235 CS.getCaller()->hasFnAttribute("profile-sample-accurate")));
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
236 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
237
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
238 INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
239 "Profile summary info", false, true)
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
240
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
241 ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
242 : ImmutablePass(ID) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
243 initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
244 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
245
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
246 bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
247 PSI.reset(new ProfileSummaryInfo(M));
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
248 return false;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
249 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
250
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
251 bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
252 PSI.reset();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
253 return false;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
254 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
255
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
256 AnalysisKey ProfileSummaryAnalysis::Key;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
257 ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
258 ModuleAnalysisManager &) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
259 return ProfileSummaryInfo(M);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
260 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
261
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
262 PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
263 ModuleAnalysisManager &AM) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
264 ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
265
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
266 OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
267 for (auto &F : M) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
268 OS << F.getName();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
269 if (PSI.isFunctionEntryHot(&F))
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
270 OS << " :hot entry ";
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
271 else if (PSI.isFunctionEntryCold(&F))
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
272 OS << " :cold entry ";
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
273 OS << "\n";
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
274 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
275 return PreservedAnalyses::all();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
276 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
277
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
278 char ProfileSummaryInfoWrapperPass::ID = 0;