blob: f309d344b8d15c2aa7b14518935eaf2261d42845 [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
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallSet.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/Instructions.h"
22#include "llvm/IR/PassManager.h"
23#include "llvm/IR/ProfileSummary.h"
24#include "llvm/IR/ValueHandle.h"
25#include "llvm/Pass.h"
26#include <memory>
27
28namespace llvm {
29class BasicBlock;
30class BlockFrequencyInfo;
31class CallSite;
32class ProfileSummary;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010033/// Analysis providing profile information.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010034///
35/// This is an immutable analysis pass that provides ability to query global
36/// (program-level) profile information. The main APIs are isHotCount and
37/// isColdCount that tells whether a given profile count is considered hot/cold
38/// based on the profile summary. This also provides convenience methods to
39/// check whether a function is hot or cold.
40
41// FIXME: Provide convenience methods to determine hotness/coldness of other IR
42// units. This would require making this depend on BFI.
43class ProfileSummaryInfo {
44private:
45 Module &M;
46 std::unique_ptr<ProfileSummary> Summary;
47 bool computeSummary();
48 void computeThresholds();
49 // Count thresholds to answer isHotCount and isColdCount queries.
50 Optional<uint64_t> HotCountThreshold, ColdCountThreshold;
51 // True if the working set size of the code is considered huge,
52 // because the number of profile counts required to reach the hot
53 // percentile is above a huge threshold.
54 Optional<bool> HasHugeWorkingSetSize;
55
56public:
57 ProfileSummaryInfo(Module &M) : M(M) {}
58 ProfileSummaryInfo(ProfileSummaryInfo &&Arg)
59 : M(Arg.M), Summary(std::move(Arg.Summary)) {}
60
Andrew Scullcdfcccc2018-10-05 20:58:37 +010061 /// Returns true if profile summary is available.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010062 bool hasProfileSummary() { return computeSummary(); }
63
Andrew Scullcdfcccc2018-10-05 20:58:37 +010064 /// Returns true if module \c M has sample profile.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010065 bool hasSampleProfile() {
66 return hasProfileSummary() &&
67 Summary->getKind() == ProfileSummary::PSK_Sample;
68 }
69
Andrew Scullcdfcccc2018-10-05 20:58:37 +010070 /// Returns true if module \c M has instrumentation profile.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010071 bool hasInstrumentationProfile() {
72 return hasProfileSummary() &&
73 Summary->getKind() == ProfileSummary::PSK_Instr;
74 }
75
Andrew Walbran3d2c1972020-04-07 12:24:26 +010076 /// Returns true if module \c M has context sensitive instrumentation profile.
77 bool hasCSInstrumentationProfile() {
78 return hasProfileSummary() &&
79 Summary->getKind() == ProfileSummary::PSK_CSInstr;
80 }
81
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010082 /// Handle the invalidation of this information.
83 ///
84 /// When used as a result of \c ProfileSummaryAnalysis this method will be
85 /// called when the module this was computed for changes. Since profile
86 /// summary is immutable after it is annotated on the module, we return false
87 /// here.
88 bool invalidate(Module &, const PreservedAnalyses &,
89 ModuleAnalysisManager::Invalidator &) {
90 return false;
91 }
92
93 /// Returns the profile count for \p CallInst.
94 Optional<uint64_t> getProfileCount(const Instruction *CallInst,
Andrew Walbran3d2c1972020-04-07 12:24:26 +010095 BlockFrequencyInfo *BFI,
96 bool AllowSynthetic = false);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010097 /// Returns true if the working set size of the code is considered huge.
98 bool hasHugeWorkingSetSize();
Andrew Scullcdfcccc2018-10-05 20:58:37 +010099 /// Returns true if \p F has hot function entry.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100100 bool isFunctionEntryHot(const Function *F);
101 /// Returns true if \p F contains hot code.
102 bool isFunctionHotInCallGraph(const Function *F, BlockFrequencyInfo &BFI);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100103 /// Returns true if \p F has cold function entry.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100104 bool isFunctionEntryCold(const Function *F);
105 /// Returns true if \p F contains only cold code.
106 bool isFunctionColdInCallGraph(const Function *F, BlockFrequencyInfo &BFI);
Andrew Walbran16937d02019-10-22 13:54:20 +0100107 /// Returns true if count \p C is considered hot.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100108 bool isHotCount(uint64_t C);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100109 /// Returns true if count \p C is considered cold.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100110 bool isColdCount(uint64_t C);
Andrew Walbran16937d02019-10-22 13:54:20 +0100111 /// Returns true if BasicBlock \p BB is considered hot.
112 bool isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI);
113 /// Returns true if BasicBlock \p BB is considered cold.
114 bool isColdBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100115 /// Returns true if CallSite \p CS is considered hot.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100116 bool isHotCallSite(const CallSite &CS, BlockFrequencyInfo *BFI);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100117 /// Returns true if Callsite \p CS is considered cold.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100118 bool isColdCallSite(const CallSite &CS, BlockFrequencyInfo *BFI);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100119 /// Returns HotCountThreshold if set. Recompute HotCountThreshold
120 /// if not set.
121 uint64_t getOrCompHotCountThreshold();
122 /// Returns ColdCountThreshold if set. Recompute HotCountThreshold
123 /// if not set.
124 uint64_t getOrCompColdCountThreshold();
125 /// Returns HotCountThreshold if set.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100126 uint64_t getHotCountThreshold() {
127 return HotCountThreshold ? HotCountThreshold.getValue() : 0;
128 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100129 /// Returns ColdCountThreshold if set.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100130 uint64_t getColdCountThreshold() {
131 return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
132 }
133};
134
135/// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
136class ProfileSummaryInfoWrapperPass : public ImmutablePass {
137 std::unique_ptr<ProfileSummaryInfo> PSI;
138
139public:
140 static char ID;
141 ProfileSummaryInfoWrapperPass();
142
Andrew Walbran16937d02019-10-22 13:54:20 +0100143 ProfileSummaryInfo &getPSI() { return *PSI; }
144 const ProfileSummaryInfo &getPSI() const { return *PSI; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100145
146 bool doInitialization(Module &M) override;
147 bool doFinalization(Module &M) override;
148 void getAnalysisUsage(AnalysisUsage &AU) const override {
149 AU.setPreservesAll();
150 }
151};
152
153/// An analysis pass based on the new PM to deliver ProfileSummaryInfo.
154class ProfileSummaryAnalysis
155 : public AnalysisInfoMixin<ProfileSummaryAnalysis> {
156public:
157 typedef ProfileSummaryInfo Result;
158
159 Result run(Module &M, ModuleAnalysisManager &);
160
161private:
162 friend AnalysisInfoMixin<ProfileSummaryAnalysis>;
163 static AnalysisKey Key;
164};
165
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100166/// Printer pass that uses \c ProfileSummaryAnalysis.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100167class ProfileSummaryPrinterPass
168 : public PassInfoMixin<ProfileSummaryPrinterPass> {
169 raw_ostream &OS;
170
171public:
172 explicit ProfileSummaryPrinterPass(raw_ostream &OS) : OS(OS) {}
173 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
174};
175
176} // end namespace llvm
177
178#endif