OSDN Git Service

Refactor the PSI to extract getCallSiteCount and remove checks for profile type.
[android-x86/external-llvm.git] / lib / Analysis / ProfileSummaryInfo.cpp
1 //===- 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/Analysis/BlockFrequencyInfo.h"
17 #include "llvm/IR/BasicBlock.h"
18 #include "llvm/IR/CallSite.h"
19 #include "llvm/IR/Metadata.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/ProfileSummary.h"
22 using 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
32 static 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
37 static 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
42 // Find the minimum count to reach a desired percentile of counts.
43 static uint64_t getMinCountForPercentile(SummaryEntryVector &DS,
44                                          uint64_t Percentile) {
45   auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) {
46     return Entry.Cutoff < Percentile;
47   };
48   auto It = std::lower_bound(DS.begin(), DS.end(), Percentile, Compare);
49   // The required percentile has to be <= one of the percentiles in the
50   // detailed summary.
51   if (It == DS.end())
52     report_fatal_error("Desired percentile exceeds the maximum cutoff");
53   return It->MinCount;
54 }
55
56 // The profile summary metadata may be attached either by the frontend or by
57 // any backend passes (IR level instrumentation, for example). This method
58 // checks if the Summary is null and if so checks if the summary metadata is now
59 // available in the module and parses it to get the Summary object. Returns true
60 // if a valid Summary is available.
61 bool ProfileSummaryInfo::computeSummary() {
62   if (Summary)
63     return true;
64   auto *SummaryMD = M.getProfileSummary();
65   if (!SummaryMD)
66     return false;
67   Summary.reset(ProfileSummary::getFromMD(SummaryMD));
68   return true;
69 }
70
71 Optional<uint64_t>
72 ProfileSummaryInfo::getProfileCount(const Instruction *Inst,
73                                     BlockFrequencyInfo *BFI) {
74   if (!Inst)
75     return None;
76   assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) &&
77          "We can only get profile count for call/invoke instruction.");
78   // Check if there is a profile metadata on the instruction. If it is present,
79   // determine hotness solely based on that.
80   uint64_t TotalCount;
81   if (Inst->extractProfTotalWeight(TotalCount))
82     return TotalCount;
83   if (BFI)
84     return BFI->getBlockProfileCount(Inst->getParent());
85   return None;
86 }
87
88 /// Returns true if the function's entry is hot. If it returns false, it
89 /// either means it is not hot or it is unknown whether it is hot or not (for
90 /// example, no profile data is available).
91 bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
92   if (!F || !computeSummary())
93     return false;
94   auto FunctionCount = F->getEntryCount();
95   // FIXME: The heuristic used below for determining hotness is based on
96   // preliminary SPEC tuning for inliner. This will eventually be a
97   // convenience method that calls isHotCount.
98   return FunctionCount && isHotCount(FunctionCount.getValue());
99 }
100
101 /// Returns true if the function's entry is a cold. If it returns false, it
102 /// either means it is not cold or it is unknown whether it is cold or not (for
103 /// example, no profile data is available).
104 bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
105   if (!F)
106     return false;
107   if (F->hasFnAttribute(Attribute::Cold)) {
108     return true;
109   }
110   if (!computeSummary())
111     return false;
112   auto FunctionCount = F->getEntryCount();
113   // FIXME: The heuristic used below for determining coldness is based on
114   // preliminary SPEC tuning for inliner. This will eventually be a
115   // convenience method that calls isHotCount.
116   return FunctionCount && isColdCount(FunctionCount.getValue());
117 }
118
119 /// Compute the hot and cold thresholds.
120 void ProfileSummaryInfo::computeThresholds() {
121   if (!computeSummary())
122     return;
123   auto &DetailedSummary = Summary->getDetailedSummary();
124   HotCountThreshold =
125       getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
126   ColdCountThreshold =
127       getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
128 }
129
130 bool ProfileSummaryInfo::isHotCount(uint64_t C) {
131   if (!HotCountThreshold)
132     computeThresholds();
133   return HotCountThreshold && C >= HotCountThreshold.getValue();
134 }
135
136 bool ProfileSummaryInfo::isColdCount(uint64_t C) {
137   if (!ColdCountThreshold)
138     computeThresholds();
139   return ColdCountThreshold && C <= ColdCountThreshold.getValue();
140 }
141
142 bool ProfileSummaryInfo::isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI) {
143   auto Count = BFI->getBlockProfileCount(B);
144   return Count && isHotCount(*Count);
145 }
146
147 bool ProfileSummaryInfo::isColdBB(const BasicBlock *B,
148                                   BlockFrequencyInfo *BFI) {
149   auto Count = BFI->getBlockProfileCount(B);
150   return Count && isColdCount(*Count);
151 }
152
153 bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS,
154                                        BlockFrequencyInfo *BFI) {
155   auto C = getProfileCount(CS.getInstruction(), BFI);
156   return C && isHotCount(*C);
157 }
158
159 bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS,
160                                         BlockFrequencyInfo *BFI) {
161   auto C = getProfileCount(CS.getInstruction(), BFI);
162   return C && isColdCount(*C);
163 }
164
165 INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
166                 "Profile summary info", false, true)
167
168 ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
169     : ImmutablePass(ID) {
170   initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
171 }
172
173 bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
174   PSI.reset(new ProfileSummaryInfo(M));
175   return false;
176 }
177
178 bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
179   PSI.reset();
180   return false;
181 }
182
183 AnalysisKey ProfileSummaryAnalysis::Key;
184 ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
185                                                ModuleAnalysisManager &) {
186   return ProfileSummaryInfo(M);
187 }
188
189 PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
190                                                  ModuleAnalysisManager &AM) {
191   ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
192
193   OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
194   for (auto &F : M) {
195     OS << F.getName();
196     if (PSI.isFunctionEntryHot(&F))
197       OS << " :hot entry ";
198     else if (PSI.isFunctionEntryCold(&F))
199       OS << " :cold entry ";
200     OS << "\n";
201   }
202   return PreservedAnalyses::all();
203 }
204
205 char ProfileSummaryInfoWrapperPass::ID = 0;