blob: a4e6ffc3dd58ea8fb9ef92a21e2817252361a861 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/Analysis/ProfileSummaryInfo.h - profile summary ---*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains a pass that provides access to profile summary
10// information.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_PROFILE_SUMMARY_INFO_H
15#define LLVM_ANALYSIS_PROFILE_SUMMARY_INFO_H
16
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010017#include "llvm/ADT/DenseMap.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010018#include "llvm/IR/PassManager.h"
19#include "llvm/IR/ProfileSummary.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010020#include "llvm/Pass.h"
21#include <memory>
22
23namespace llvm {
24class BasicBlock;
25class BlockFrequencyInfo;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020026class CallBase;
27class Function;
28
Andrew Scullcdfcccc2018-10-05 20:58:37 +010029/// Analysis providing profile information.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010030///
31/// This is an immutable analysis pass that provides ability to query global
32/// (program-level) profile information. The main APIs are isHotCount and
33/// isColdCount that tells whether a given profile count is considered hot/cold
34/// based on the profile summary. This also provides convenience methods to
35/// check whether a function is hot or cold.
36
37// FIXME: Provide convenience methods to determine hotness/coldness of other IR
38// units. This would require making this depend on BFI.
39class ProfileSummaryInfo {
40private:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020041 const Module &M;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042 std::unique_ptr<ProfileSummary> Summary;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010043 void computeThresholds();
44 // Count thresholds to answer isHotCount and isColdCount queries.
45 Optional<uint64_t> HotCountThreshold, ColdCountThreshold;
46 // True if the working set size of the code is considered huge,
47 // because the number of profile counts required to reach the hot
48 // percentile is above a huge threshold.
49 Optional<bool> HasHugeWorkingSetSize;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020050 // True if the working set size of the code is considered large,
51 // because the number of profile counts required to reach the hot
52 // percentile is above a large threshold.
53 Optional<bool> HasLargeWorkingSetSize;
54 // Compute the threshold for a given cutoff.
55 Optional<uint64_t> computeThreshold(int PercentileCutoff) const;
56 // The map that caches the threshold values. The keys are the percentile
57 // cutoff values and the values are the corresponding threshold values.
58 mutable DenseMap<int, uint64_t> ThresholdCache;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010059
60public:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020061 ProfileSummaryInfo(const Module &M) : M(M) { refresh(); }
62
63 ProfileSummaryInfo(ProfileSummaryInfo &&Arg) = default;
64
65 /// If no summary is present, attempt to refresh.
66 void refresh();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010067
Andrew Scullcdfcccc2018-10-05 20:58:37 +010068 /// Returns true if profile summary is available.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020069 bool hasProfileSummary() const { return Summary != nullptr; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010070
Andrew Scullcdfcccc2018-10-05 20:58:37 +010071 /// Returns true if module \c M has sample profile.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020072 bool hasSampleProfile() const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073 return hasProfileSummary() &&
74 Summary->getKind() == ProfileSummary::PSK_Sample;
75 }
76
Andrew Scullcdfcccc2018-10-05 20:58:37 +010077 /// Returns true if module \c M has instrumentation profile.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020078 bool hasInstrumentationProfile() const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010079 return hasProfileSummary() &&
80 Summary->getKind() == ProfileSummary::PSK_Instr;
81 }
82
Andrew Walbran3d2c1972020-04-07 12:24:26 +010083 /// Returns true if module \c M has context sensitive instrumentation profile.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020084 bool hasCSInstrumentationProfile() const {
Andrew Walbran3d2c1972020-04-07 12:24:26 +010085 return hasProfileSummary() &&
86 Summary->getKind() == ProfileSummary::PSK_CSInstr;
87 }
88
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010089 /// Handle the invalidation of this information.
90 ///
91 /// When used as a result of \c ProfileSummaryAnalysis this method will be
92 /// called when the module this was computed for changes. Since profile
93 /// summary is immutable after it is annotated on the module, we return false
94 /// here.
95 bool invalidate(Module &, const PreservedAnalyses &,
96 ModuleAnalysisManager::Invalidator &) {
97 return false;
98 }
99
100 /// Returns the profile count for \p CallInst.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200101 Optional<uint64_t> getProfileCount(const CallBase &CallInst,
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100102 BlockFrequencyInfo *BFI,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200103 bool AllowSynthetic = false) const;
104 /// Returns true if module \c M has partial-profile sample profile.
105 bool hasPartialSampleProfile() const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100106 /// Returns true if the working set size of the code is considered huge.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200107 bool hasHugeWorkingSetSize() const;
108 /// Returns true if the working set size of the code is considered large.
109 bool hasLargeWorkingSetSize() const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100110 /// Returns true if \p F has hot function entry.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200111 bool isFunctionEntryHot(const Function *F) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100112 /// Returns true if \p F contains hot code.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200113 bool isFunctionHotInCallGraph(const Function *F,
114 BlockFrequencyInfo &BFI) const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100115 /// Returns true if \p F has cold function entry.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200116 bool isFunctionEntryCold(const Function *F) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100117 /// Returns true if \p F contains only cold code.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200118 bool isFunctionColdInCallGraph(const Function *F,
119 BlockFrequencyInfo &BFI) const;
120 /// Returns true if the hotness of \p F is unknown.
121 bool isFunctionHotnessUnknown(const Function &F) const;
122 /// Returns true if \p F contains hot code with regard to a given hot
123 /// percentile cutoff value.
124 bool isFunctionHotInCallGraphNthPercentile(int PercentileCutoff,
125 const Function *F,
126 BlockFrequencyInfo &BFI) const;
127 /// Returns true if \p F contains cold code with regard to a given cold
128 /// percentile cutoff value.
129 bool isFunctionColdInCallGraphNthPercentile(int PercentileCutoff,
130 const Function *F,
131 BlockFrequencyInfo &BFI) const;
Andrew Walbran16937d02019-10-22 13:54:20 +0100132 /// Returns true if count \p C is considered hot.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200133 bool isHotCount(uint64_t C) const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100134 /// Returns true if count \p C is considered cold.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200135 bool isColdCount(uint64_t C) const;
136 /// Returns true if count \p C is considered hot with regard to a given
137 /// hot percentile cutoff value.
138 bool isHotCountNthPercentile(int PercentileCutoff, uint64_t C) const;
139 /// Returns true if count \p C is considered cold with regard to a given
140 /// cold percentile cutoff value.
141 bool isColdCountNthPercentile(int PercentileCutoff, uint64_t C) const;
Andrew Walbran16937d02019-10-22 13:54:20 +0100142 /// Returns true if BasicBlock \p BB is considered hot.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200143 bool isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) const;
Andrew Walbran16937d02019-10-22 13:54:20 +0100144 /// Returns true if BasicBlock \p BB is considered cold.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200145 bool isColdBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) const;
146 /// Returns true if BasicBlock \p BB is considered hot with regard to a given
147 /// hot percentile cutoff value.
148 bool isHotBlockNthPercentile(int PercentileCutoff, const BasicBlock *BB,
149 BlockFrequencyInfo *BFI) const;
150 /// Returns true if BasicBlock \p BB is considered cold with regard to a given
151 /// cold percentile cutoff value.
152 bool isColdBlockNthPercentile(int PercentileCutoff, const BasicBlock *BB,
153 BlockFrequencyInfo *BFI) const;
154 /// Returns true if the call site \p CB is considered hot.
155 bool isHotCallSite(const CallBase &CB, BlockFrequencyInfo *BFI) const;
156 /// Returns true if call site \p CB is considered cold.
157 bool isColdCallSite(const CallBase &CB, BlockFrequencyInfo *BFI) const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100158 /// Returns HotCountThreshold if set. Recompute HotCountThreshold
159 /// if not set.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200160 uint64_t getOrCompHotCountThreshold() const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100161 /// Returns ColdCountThreshold if set. Recompute HotCountThreshold
162 /// if not set.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200163 uint64_t getOrCompColdCountThreshold() const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100164 /// Returns HotCountThreshold if set.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200165 uint64_t getHotCountThreshold() const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100166 return HotCountThreshold ? HotCountThreshold.getValue() : 0;
167 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100168 /// Returns ColdCountThreshold if set.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200169 uint64_t getColdCountThreshold() const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100170 return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
171 }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200172
173 private:
174 template <bool isHot>
175 bool isFunctionHotOrColdInCallGraphNthPercentile(
176 int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const;
177 template <bool isHot>
178 bool isHotOrColdCountNthPercentile(int PercentileCutoff, uint64_t C) const;
179 template <bool isHot>
180 bool isHotOrColdBlockNthPercentile(int PercentileCutoff,
181 const BasicBlock *BB,
182 BlockFrequencyInfo *BFI) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100183};
184
185/// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
186class ProfileSummaryInfoWrapperPass : public ImmutablePass {
187 std::unique_ptr<ProfileSummaryInfo> PSI;
188
189public:
190 static char ID;
191 ProfileSummaryInfoWrapperPass();
192
Andrew Walbran16937d02019-10-22 13:54:20 +0100193 ProfileSummaryInfo &getPSI() { return *PSI; }
194 const ProfileSummaryInfo &getPSI() const { return *PSI; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100195
196 bool doInitialization(Module &M) override;
197 bool doFinalization(Module &M) override;
198 void getAnalysisUsage(AnalysisUsage &AU) const override {
199 AU.setPreservesAll();
200 }
201};
202
203/// An analysis pass based on the new PM to deliver ProfileSummaryInfo.
204class ProfileSummaryAnalysis
205 : public AnalysisInfoMixin<ProfileSummaryAnalysis> {
206public:
207 typedef ProfileSummaryInfo Result;
208
209 Result run(Module &M, ModuleAnalysisManager &);
210
211private:
212 friend AnalysisInfoMixin<ProfileSummaryAnalysis>;
213 static AnalysisKey Key;
214};
215
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100216/// Printer pass that uses \c ProfileSummaryAnalysis.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100217class ProfileSummaryPrinterPass
218 : public PassInfoMixin<ProfileSummaryPrinterPass> {
219 raw_ostream &OS;
220
221public:
222 explicit ProfileSummaryPrinterPass(raw_ostream &OS) : OS(OS) {}
223 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
224};
225
226} // end namespace llvm
227
228#endif