blob: 2f1fe5dba8aa1c427b6da9fa0d700faa61579458 [file] [log] [blame]
Justin Bogneref512b92014-01-06 22:27:43 +00001//===--- CodeGenPGO.h - PGO Instrumentation for LLVM CodeGen ----*- C++ -*-===//
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// Instrumentation-based profile-guided optimization
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_CODEGEN_CODEGENPGO_H
15#define CLANG_CODEGEN_CODEGENPGO_H
16
17#include "CGBuilder.h"
18#include "CodeGenModule.h"
19#include "CodeGenTypes.h"
20#include "clang/Frontend/CodeGenOptions.h"
Justin Bogneref512b92014-01-06 22:27:43 +000021#include "llvm/ADT/StringMap.h"
22#include "llvm/Support/MemoryBuffer.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000023#include <memory>
Justin Bogneref512b92014-01-06 22:27:43 +000024
25namespace clang {
26namespace CodeGen {
27class RegionCounter;
28
Justin Bognerd66a17d2014-03-12 21:06:31 +000029/// The raw counter data from an instrumented PGO binary
30class PGOProfileData {
31private:
32 /// The PGO data
33 std::unique_ptr<llvm::MemoryBuffer> DataBuffer;
34 /// Offsets into DataBuffer for each function's counters
35 llvm::StringMap<unsigned> DataOffsets;
36 /// Execution counts for each function.
37 llvm::StringMap<uint64_t> FunctionCounts;
38 /// The maximal execution count among all functions.
39 uint64_t MaxFunctionCount;
40 CodeGenModule &CGM;
41public:
42 PGOProfileData(CodeGenModule &CGM, std::string Path);
43 /// Fill Counts with the profile data for the given function name. Returns
44 /// false on success.
45 bool getFunctionCounts(StringRef FuncName, std::vector<uint64_t> &Counts);
46 /// Return the maximum of all known function counts.
47 uint64_t getMaximumFunctionCount() { return MaxFunctionCount; }
48};
49
Justin Bogneref512b92014-01-06 22:27:43 +000050/// Per-function PGO state. This class should generally not be used directly,
51/// but instead through the CodeGenFunction and RegionCounter types.
52class CodeGenPGO {
53private:
54 CodeGenModule &CGM;
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000055 std::string *PrefixedFuncName;
56 StringRef RawFuncName;
57 llvm::GlobalValue::LinkageTypes FuncLinkage;
Justin Bogneref512b92014-01-06 22:27:43 +000058
59 unsigned NumRegionCounters;
60 llvm::GlobalVariable *RegionCounters;
61 llvm::DenseMap<const Stmt*, unsigned> *RegionCounterMap;
Bob Wilsonbf854f02014-02-17 19:21:09 +000062 llvm::DenseMap<const Stmt*, uint64_t> *StmtCountMap;
Justin Bogneref512b92014-01-06 22:27:43 +000063 std::vector<uint64_t> *RegionCounts;
64 uint64_t CurrentRegionCount;
65
66public:
67 CodeGenPGO(CodeGenModule &CGM)
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000068 : CGM(CGM), PrefixedFuncName(0), NumRegionCounters(0), RegionCounters(0),
Bob Wilsonda1ebed2014-03-06 04:55:41 +000069 RegionCounterMap(0), StmtCountMap(0), RegionCounts(0),
70 CurrentRegionCount(0) {}
71 ~CodeGenPGO() {
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000072 if (PrefixedFuncName) delete PrefixedFuncName;
Bob Wilsonda1ebed2014-03-06 04:55:41 +000073 }
Justin Bogneref512b92014-01-06 22:27:43 +000074
75 /// Whether or not we have PGO region data for the current function. This is
76 /// false both when we have no data at all and when our data has been
77 /// discarded.
78 bool haveRegionCounts() const { return RegionCounts != 0; }
79
Bob Wilsonda1ebed2014-03-06 04:55:41 +000080 /// Get the string used to identify this function in the profile data.
81 /// For functions with local linkage, this includes the main file name.
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000082 StringRef getFuncName() const { return StringRef(*PrefixedFuncName); }
83 std::string getFuncVarName(StringRef VarName) const {
84 return ("__llvm_pgo_" + VarName + "_" + RawFuncName).str();
85 }
Bob Wilsonda1ebed2014-03-06 04:55:41 +000086
Justin Bogneref512b92014-01-06 22:27:43 +000087 /// Return the counter value of the current region.
88 uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
Bob Wilsonbf854f02014-02-17 19:21:09 +000089
Justin Bogneref512b92014-01-06 22:27:43 +000090 /// Set the counter value for the current region. This is used to keep track
91 /// of changes to the most recent counter from control flow and non-local
92 /// exits.
93 void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
Bob Wilsonbf854f02014-02-17 19:21:09 +000094
Justin Bogner06bd6d02014-01-13 21:24:18 +000095 /// Indicate that the current region is never reached, and thus should have a
96 /// counter value of zero. This is important so that subsequent regions can
97 /// correctly track their parent counts.
98 void setCurrentRegionUnreachable() { setCurrentRegionCount(0); }
Justin Bogneref512b92014-01-06 22:27:43 +000099
Bob Wilsonbf854f02014-02-17 19:21:09 +0000100 /// Check if an execution count is known for a given statement. If so, return
101 /// true and put the value in Count; else return false.
102 bool getStmtCount(const Stmt *S, uint64_t &Count) {
103 if (!StmtCountMap)
104 return false;
105 llvm::DenseMap<const Stmt*, uint64_t>::const_iterator
106 I = StmtCountMap->find(S);
107 if (I == StmtCountMap->end())
108 return false;
109 Count = I->second;
110 return true;
111 }
112
113 /// If the execution count for the current statement is known, record that
114 /// as the current count.
115 void setCurrentStmt(const Stmt *S) {
116 uint64_t Count;
117 if (getStmtCount(S, Count))
118 setCurrentRegionCount(Count);
119 }
120
Justin Bogneref512b92014-01-06 22:27:43 +0000121 /// Calculate branch weights appropriate for PGO data
122 llvm::MDNode *createBranchWeights(uint64_t TrueCount, uint64_t FalseCount);
123 llvm::MDNode *createBranchWeights(ArrayRef<uint64_t> Weights);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000124 llvm::MDNode *createLoopWeights(const Stmt *Cond, RegionCounter &Cnt);
Justin Bogneref512b92014-01-06 22:27:43 +0000125
126 /// Assign counters to regions and configure them for PGO of a given
127 /// function. Does nothing if instrumentation is not enabled and either
128 /// generates global variables or associates PGO data with each of the
129 /// counters depending on whether we are generating or using instrumentation.
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000130 void assignRegionCounters(const Decl *D, llvm::Function *Fn);
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000131 /// Emit static data structures for instrumentation data.
132 void emitInstrumentationData();
Justin Bogneref512b92014-01-06 22:27:43 +0000133 /// Clean up region counter state. Must be called if assignRegionCounters is
134 /// used.
135 void destroyRegionCounters();
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000136 /// Emit static initialization code, if any.
Justin Bogneref512b92014-01-06 22:27:43 +0000137 static llvm::Function *emitInitialization(CodeGenModule &CGM);
138
139private:
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000140 void setFuncName(llvm::Function *Fn);
Justin Bogneref512b92014-01-06 22:27:43 +0000141 void mapRegionCounters(const Decl *D);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000142 void computeRegionCounts(const Decl *D);
Justin Bognerd66a17d2014-03-12 21:06:31 +0000143 void applyFunctionAttributes(PGOProfileData *PGOData, llvm::Function *Fn);
144 void loadRegionCounts(PGOProfileData *PGOData);
Justin Bogneref512b92014-01-06 22:27:43 +0000145 void emitCounterVariables();
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000146 llvm::GlobalVariable *buildDataVar();
Justin Bogneref512b92014-01-06 22:27:43 +0000147
148 /// Emit code to increment the counter at the given index
149 void emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter);
150
151 /// Return the region counter for the given statement. This should only be
152 /// called on statements that have a dedicated counter.
153 unsigned getRegionCounter(const Stmt *S) {
154 if (RegionCounterMap == 0)
155 return 0;
156 return (*RegionCounterMap)[S];
157 }
158
159 /// Return the region count for the counter at the given index.
160 uint64_t getRegionCount(unsigned Counter) {
161 if (!haveRegionCounts())
162 return 0;
163 return (*RegionCounts)[Counter];
164 }
165
166 friend class RegionCounter;
167};
168
169/// A counter for a particular region. This is the primary interface through
170/// which clients manage PGO counters and their values.
171class RegionCounter {
172 CodeGenPGO *PGO;
173 unsigned Counter;
174 uint64_t Count;
175 uint64_t ParentCount;
176 uint64_t RegionCount;
177 int64_t Adjust;
178
179 RegionCounter(CodeGenPGO &PGO, unsigned CounterIndex)
180 : PGO(&PGO), Counter(CounterIndex), Count(PGO.getRegionCount(Counter)),
181 ParentCount(PGO.getCurrentRegionCount()), Adjust(0) {}
182
183public:
184 RegionCounter(CodeGenPGO &PGO, const Stmt *S)
185 : PGO(&PGO), Counter(PGO.getRegionCounter(S)),
186 Count(PGO.getRegionCount(Counter)),
187 ParentCount(PGO.getCurrentRegionCount()), Adjust(0) {}
188
189 /// Get the value of the counter. In most cases this is the number of times
190 /// the region of the counter was entered, but for switch labels it's the
191 /// number of direct jumps to that label.
192 uint64_t getCount() const { return Count; }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000193
Justin Bogneref512b92014-01-06 22:27:43 +0000194 /// Get the value of the counter with adjustments applied. Adjustments occur
Bob Wilsona7b16e02014-02-17 19:21:03 +0000195 /// when control enters or leaves the region abnormally; i.e., if there is a
Justin Bogneref512b92014-01-06 22:27:43 +0000196 /// jump to a label within the region, or if the function can return from
197 /// within the region. The adjusted count, then, is the value of the counter
198 /// at the end of the region.
199 uint64_t getAdjustedCount() const {
Justin Bogneref512b92014-01-06 22:27:43 +0000200 return Count + Adjust;
201 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000202
Bob Wilsona7b16e02014-02-17 19:21:03 +0000203 /// Get the value of the counter in this region's parent, i.e., the region
204 /// that was active when this region began. This is useful for deriving
205 /// counts in implicitly counted regions, like the false case of a condition
206 /// or the normal exits of a loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000207 uint64_t getParentCount() const { return ParentCount; }
208
Justin Bogneref512b92014-01-06 22:27:43 +0000209 /// Activate the counter by emitting an increment and starting to track
210 /// adjustments. If AddIncomingFallThrough is true, the current region count
211 /// will be added to the counter for the purposes of tracking the region.
212 void beginRegion(CGBuilderTy &Builder, bool AddIncomingFallThrough=false) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000213 beginRegion(AddIncomingFallThrough);
214 PGO->emitCounterIncrement(Builder, Counter);
215 }
216 void beginRegion(bool AddIncomingFallThrough=false) {
Justin Bogneref512b92014-01-06 22:27:43 +0000217 RegionCount = Count;
218 if (AddIncomingFallThrough)
219 RegionCount += PGO->getCurrentRegionCount();
220 PGO->setCurrentRegionCount(RegionCount);
Justin Bogneref512b92014-01-06 22:27:43 +0000221 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000222
Justin Bogneref512b92014-01-06 22:27:43 +0000223 /// For counters on boolean branches, begins tracking adjustments for the
224 /// uncounted path.
225 void beginElseRegion() {
226 RegionCount = ParentCount - Count;
227 PGO->setCurrentRegionCount(RegionCount);
228 }
229
Bob Wilsonbf854f02014-02-17 19:21:09 +0000230 /// Reset the current region count.
231 void setCurrentRegionCount(uint64_t CurrentCount) {
232 RegionCount = CurrentCount;
233 PGO->setCurrentRegionCount(RegionCount);
234 }
235
Justin Bogner0718a3a2014-01-13 21:24:22 +0000236 /// Adjust for non-local control flow after emitting a subexpression or
237 /// substatement. This must be called to account for constructs such as gotos,
238 /// labels, and returns, so that we can ensure that our region's count is
239 /// correct in the code that follows.
240 void adjustForControlFlow() {
Justin Bogneref512b92014-01-06 22:27:43 +0000241 Adjust += PGO->getCurrentRegionCount() - RegionCount;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000242 // Reset the region count in case this is called again later.
243 RegionCount = PGO->getCurrentRegionCount();
Justin Bogneref512b92014-01-06 22:27:43 +0000244 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000245
246 /// Commit all adjustments to the current region. If the region is a loop,
247 /// the LoopAdjust value should be the count of all the breaks and continues
248 /// from the loop, to compensate for those counts being deducted from the
249 /// adjustments for the body of the loop.
250 void applyAdjustmentsToRegion(uint64_t LoopAdjust) {
251 PGO->setCurrentRegionCount(ParentCount + Adjust + LoopAdjust);
Justin Bogneref512b92014-01-06 22:27:43 +0000252 }
253};
254
255} // end namespace CodeGen
256} // end namespace clang
257
258#endif