blob: df043537dd472e1b95d4736f28ffa9bffd286ec7 [file] [log] [blame]
Justin Bogneref512b92014-01-06 22:27:43 +00001//===--- CodeGenPGO.cpp - 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#include "CodeGenPGO.h"
15#include "CodeGenFunction.h"
Alex Lorenzee024992014-08-04 18:41:51 +000016#include "CoverageMappingGen.h"
Justin Bogneref512b92014-01-06 22:27:43 +000017#include "clang/AST/RecursiveASTVisitor.h"
18#include "clang/AST/StmtVisitor.h"
Justin Bogner970ac602014-12-08 19:04:51 +000019#include "llvm/IR/Intrinsics.h"
Justin Bogneref512b92014-01-06 22:27:43 +000020#include "llvm/IR/MDBuilder.h"
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +000021#include "llvm/Support/Endian.h"
Justin Bogneref512b92014-01-06 22:27:43 +000022#include "llvm/Support/FileSystem.h"
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +000023#include "llvm/Support/MD5.h"
Justin Bogneref512b92014-01-06 22:27:43 +000024
Betul Buyukkurt518276a2016-01-23 22:50:44 +000025static llvm::cl::opt<bool> EnableValueProfiling(
26 "enable-value-profiling", llvm::cl::ZeroOrMore,
27 llvm::cl::desc("Enable value profiling"), llvm::cl::init(false));
28
Justin Bogneref512b92014-01-06 22:27:43 +000029using namespace clang;
30using namespace CodeGen;
31
Alex Lorenzee024992014-08-04 18:41:51 +000032void CodeGenPGO::setFuncName(StringRef Name,
33 llvm::GlobalValue::LinkageTypes Linkage) {
Xinliang David Lia569e242015-12-05 05:37:15 +000034 llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
35 FuncName = llvm::getPGOFuncName(
36 Name, Linkage, CGM.getCodeGenOpts().MainFileName,
37 PGOReader ? PGOReader->getVersion() : llvm::IndexedInstrProf::Version);
Justin Bogner970ac602014-12-08 19:04:51 +000038
39 // If we're generating a profile, create a variable for the name.
40 if (CGM.getCodeGenOpts().ProfileInstrGenerate)
Xinliang David Li2d7ec5a2015-11-09 00:04:16 +000041 FuncNameVar = llvm::createPGOFuncNameVar(CGM.getModule(), Linkage, FuncName);
Bob Wilsonda1ebed2014-03-06 04:55:41 +000042}
43
Alex Lorenzee024992014-08-04 18:41:51 +000044void CodeGenPGO::setFuncName(llvm::Function *Fn) {
45 setFuncName(Fn->getName(), Fn->getLinkage());
46}
47
Justin Bogneref512b92014-01-06 22:27:43 +000048namespace {
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +000049/// \brief Stable hasher for PGO region counters.
50///
51/// PGOHash produces a stable hash of a given function's control flow.
52///
53/// Changing the output of this hash will invalidate all previously generated
54/// profiles -- i.e., don't do it.
55///
56/// \note When this hash does eventually change (years?), we still need to
57/// support old hashes. We'll need to pull in the version number from the
58/// profile data format and use the matching hash function.
59class PGOHash {
60 uint64_t Working;
61 unsigned Count;
62 llvm::MD5 MD5;
63
64 static const int NumBitsPerType = 6;
65 static const unsigned NumTypesPerWord = sizeof(uint64_t) * 8 / NumBitsPerType;
66 static const unsigned TooBig = 1u << NumBitsPerType;
67
68public:
69 /// \brief Hash values for AST nodes.
70 ///
71 /// Distinct values for AST nodes that have region counters attached.
72 ///
73 /// These values must be stable. All new members must be added at the end,
74 /// and no members should be removed. Changing the enumeration value for an
75 /// AST node will affect the hash of every function that contains that node.
76 enum HashType : unsigned char {
77 None = 0,
78 LabelStmt = 1,
79 WhileStmt,
80 DoStmt,
81 ForStmt,
82 CXXForRangeStmt,
83 ObjCForCollectionStmt,
84 SwitchStmt,
85 CaseStmt,
86 DefaultStmt,
87 IfStmt,
88 CXXTryStmt,
89 CXXCatchStmt,
90 ConditionalOperator,
91 BinaryOperatorLAnd,
92 BinaryOperatorLOr,
93 BinaryConditionalOperator,
94
95 // Keep this last. It's for the static assert that follows.
96 LastHashType
97 };
98 static_assert(LastHashType <= TooBig, "Too many types in HashType");
99
100 // TODO: When this format changes, take in a version number here, and use the
101 // old hash calculation for file formats that used the old hash.
102 PGOHash() : Working(0), Count(0) {}
103 void combine(HashType Type);
104 uint64_t finalize();
105};
106const int PGOHash::NumBitsPerType;
107const unsigned PGOHash::NumTypesPerWord;
108const unsigned PGOHash::TooBig;
109
Justin Bognere4ca4412015-02-23 19:27:00 +0000110/// A RecursiveASTVisitor that fills a map of statements to PGO counters.
111struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
112 /// The next counter value to assign.
113 unsigned NextCounter;
114 /// The function hash.
115 PGOHash Hash;
116 /// The map of statements to counters.
117 llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
Justin Bogneref512b92014-01-06 22:27:43 +0000118
Justin Bognere4ca4412015-02-23 19:27:00 +0000119 MapRegionCounters(llvm::DenseMap<const Stmt *, unsigned> &CounterMap)
120 : NextCounter(0), CounterMap(CounterMap) {}
Justin Bogneref512b92014-01-06 22:27:43 +0000121
Justin Bognere4ca4412015-02-23 19:27:00 +0000122 // Blocks and lambdas are handled as separate functions, so we need not
123 // traverse them in the parent context.
124 bool TraverseBlockExpr(BlockExpr *BE) { return true; }
125 bool TraverseLambdaBody(LambdaExpr *LE) { return true; }
126 bool TraverseCapturedStmt(CapturedStmt *CS) { return true; }
Justin Bogneref512b92014-01-06 22:27:43 +0000127
Justin Bognere4ca4412015-02-23 19:27:00 +0000128 bool VisitDecl(const Decl *D) {
129 switch (D->getKind()) {
130 default:
131 break;
132 case Decl::Function:
133 case Decl::CXXMethod:
134 case Decl::CXXConstructor:
135 case Decl::CXXDestructor:
136 case Decl::CXXConversion:
137 case Decl::ObjCMethod:
138 case Decl::Block:
139 case Decl::Captured:
140 CounterMap[D->getBody()] = NextCounter++;
141 break;
142 }
143 return true;
144 }
145
146 bool VisitStmt(const Stmt *S) {
147 auto Type = getHashType(S);
148 if (Type == PGOHash::None)
Bob Wilsond8931422014-04-11 17:16:13 +0000149 return true;
Bob Wilsond8931422014-04-11 17:16:13 +0000150
Justin Bognere4ca4412015-02-23 19:27:00 +0000151 CounterMap[S] = NextCounter++;
152 Hash.combine(Type);
153 return true;
154 }
155 PGOHash::HashType getHashType(const Stmt *S) {
156 switch (S->getStmtClass()) {
157 default:
158 break;
159 case Stmt::LabelStmtClass:
160 return PGOHash::LabelStmt;
161 case Stmt::WhileStmtClass:
162 return PGOHash::WhileStmt;
163 case Stmt::DoStmtClass:
164 return PGOHash::DoStmt;
165 case Stmt::ForStmtClass:
166 return PGOHash::ForStmt;
167 case Stmt::CXXForRangeStmtClass:
168 return PGOHash::CXXForRangeStmt;
169 case Stmt::ObjCForCollectionStmtClass:
170 return PGOHash::ObjCForCollectionStmt;
171 case Stmt::SwitchStmtClass:
172 return PGOHash::SwitchStmt;
173 case Stmt::CaseStmtClass:
174 return PGOHash::CaseStmt;
175 case Stmt::DefaultStmtClass:
176 return PGOHash::DefaultStmt;
177 case Stmt::IfStmtClass:
178 return PGOHash::IfStmt;
179 case Stmt::CXXTryStmtClass:
180 return PGOHash::CXXTryStmt;
181 case Stmt::CXXCatchStmtClass:
182 return PGOHash::CXXCatchStmt;
183 case Stmt::ConditionalOperatorClass:
184 return PGOHash::ConditionalOperator;
185 case Stmt::BinaryConditionalOperatorClass:
186 return PGOHash::BinaryConditionalOperator;
187 case Stmt::BinaryOperatorClass: {
188 const BinaryOperator *BO = cast<BinaryOperator>(S);
189 if (BO->getOpcode() == BO_LAnd)
190 return PGOHash::BinaryOperatorLAnd;
191 if (BO->getOpcode() == BO_LOr)
192 return PGOHash::BinaryOperatorLOr;
193 break;
194 }
195 }
196 return PGOHash::None;
197 }
198};
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000199
Justin Bognere4ca4412015-02-23 19:27:00 +0000200/// A StmtVisitor that propagates the raw counts through the AST and
201/// records the count at statements where the value may change.
202struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> {
203 /// PGO state.
204 CodeGenPGO &PGO;
205
206 /// A flag that is set when the current count should be recorded on the
207 /// next statement, such as at the exit of a loop.
208 bool RecordNextStmtCount;
209
Justin Bognera909abf2015-05-02 00:48:27 +0000210 /// The count at the current location in the traversal.
211 uint64_t CurrentCount;
212
Justin Bognere4ca4412015-02-23 19:27:00 +0000213 /// The map of statements to count values.
214 llvm::DenseMap<const Stmt *, uint64_t> &CountMap;
215
216 /// BreakContinueStack - Keep counts of breaks and continues inside loops.
217 struct BreakContinue {
218 uint64_t BreakCount;
219 uint64_t ContinueCount;
220 BreakContinue() : BreakCount(0), ContinueCount(0) {}
Justin Bogneref512b92014-01-06 22:27:43 +0000221 };
Justin Bognere4ca4412015-02-23 19:27:00 +0000222 SmallVector<BreakContinue, 8> BreakContinueStack;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000223
Justin Bognere4ca4412015-02-23 19:27:00 +0000224 ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &CountMap,
225 CodeGenPGO &PGO)
226 : PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000227
Justin Bognere4ca4412015-02-23 19:27:00 +0000228 void RecordStmtCount(const Stmt *S) {
229 if (RecordNextStmtCount) {
Justin Bognera909abf2015-05-02 00:48:27 +0000230 CountMap[S] = CurrentCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000231 RecordNextStmtCount = false;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000232 }
Justin Bognere4ca4412015-02-23 19:27:00 +0000233 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000234
Justin Bogner07193cc2015-05-01 23:41:09 +0000235 /// Set and return the current count.
236 uint64_t setCount(uint64_t Count) {
Justin Bognera909abf2015-05-02 00:48:27 +0000237 CurrentCount = Count;
Justin Bogner07193cc2015-05-01 23:41:09 +0000238 return Count;
239 }
240
Justin Bognere4ca4412015-02-23 19:27:00 +0000241 void VisitStmt(const Stmt *S) {
242 RecordStmtCount(S);
Benjamin Kramer642f1732015-07-02 21:03:14 +0000243 for (const Stmt *Child : S->children())
244 if (Child)
245 this->Visit(Child);
Justin Bognere4ca4412015-02-23 19:27:00 +0000246 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000247
Justin Bognere4ca4412015-02-23 19:27:00 +0000248 void VisitFunctionDecl(const FunctionDecl *D) {
249 // Counter tracks entry to the function body.
Justin Bogner07193cc2015-05-01 23:41:09 +0000250 uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
251 CountMap[D->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000252 Visit(D->getBody());
253 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000254
Justin Bognere4ca4412015-02-23 19:27:00 +0000255 // Skip lambda expressions. We visit these as FunctionDecls when we're
256 // generating them and aren't interested in the body when generating a
257 // parent context.
258 void VisitLambdaExpr(const LambdaExpr *LE) {}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000259
Justin Bognere4ca4412015-02-23 19:27:00 +0000260 void VisitCapturedDecl(const CapturedDecl *D) {
261 // Counter tracks entry to the capture body.
Justin Bogner07193cc2015-05-01 23:41:09 +0000262 uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
263 CountMap[D->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000264 Visit(D->getBody());
265 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000266
Justin Bognere4ca4412015-02-23 19:27:00 +0000267 void VisitObjCMethodDecl(const ObjCMethodDecl *D) {
268 // Counter tracks entry to the method body.
Justin Bogner07193cc2015-05-01 23:41:09 +0000269 uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
270 CountMap[D->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000271 Visit(D->getBody());
272 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000273
Justin Bognere4ca4412015-02-23 19:27:00 +0000274 void VisitBlockDecl(const BlockDecl *D) {
275 // Counter tracks entry to the block body.
Justin Bogner07193cc2015-05-01 23:41:09 +0000276 uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
277 CountMap[D->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000278 Visit(D->getBody());
279 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000280
Justin Bognere4ca4412015-02-23 19:27:00 +0000281 void VisitReturnStmt(const ReturnStmt *S) {
282 RecordStmtCount(S);
283 if (S->getRetValue())
284 Visit(S->getRetValue());
Justin Bognera909abf2015-05-02 00:48:27 +0000285 CurrentCount = 0;
Justin Bognere4ca4412015-02-23 19:27:00 +0000286 RecordNextStmtCount = true;
287 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000288
Justin Bognerf959feb2015-04-28 06:31:55 +0000289 void VisitCXXThrowExpr(const CXXThrowExpr *E) {
290 RecordStmtCount(E);
291 if (E->getSubExpr())
292 Visit(E->getSubExpr());
Justin Bognera909abf2015-05-02 00:48:27 +0000293 CurrentCount = 0;
Justin Bognerf959feb2015-04-28 06:31:55 +0000294 RecordNextStmtCount = true;
295 }
296
Justin Bognere4ca4412015-02-23 19:27:00 +0000297 void VisitGotoStmt(const GotoStmt *S) {
298 RecordStmtCount(S);
Justin Bognera909abf2015-05-02 00:48:27 +0000299 CurrentCount = 0;
Justin Bognere4ca4412015-02-23 19:27:00 +0000300 RecordNextStmtCount = true;
301 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000302
Justin Bognere4ca4412015-02-23 19:27:00 +0000303 void VisitLabelStmt(const LabelStmt *S) {
304 RecordNextStmtCount = false;
305 // Counter tracks the block following the label.
Justin Bogner07193cc2015-05-01 23:41:09 +0000306 uint64_t BlockCount = setCount(PGO.getRegionCount(S));
307 CountMap[S] = BlockCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000308 Visit(S->getSubStmt());
309 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000310
Justin Bognere4ca4412015-02-23 19:27:00 +0000311 void VisitBreakStmt(const BreakStmt *S) {
312 RecordStmtCount(S);
313 assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
Justin Bognera909abf2015-05-02 00:48:27 +0000314 BreakContinueStack.back().BreakCount += CurrentCount;
315 CurrentCount = 0;
Justin Bognere4ca4412015-02-23 19:27:00 +0000316 RecordNextStmtCount = true;
317 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000318
Justin Bognere4ca4412015-02-23 19:27:00 +0000319 void VisitContinueStmt(const ContinueStmt *S) {
320 RecordStmtCount(S);
321 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
Justin Bognera909abf2015-05-02 00:48:27 +0000322 BreakContinueStack.back().ContinueCount += CurrentCount;
323 CurrentCount = 0;
Justin Bognere4ca4412015-02-23 19:27:00 +0000324 RecordNextStmtCount = true;
325 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000326
Justin Bognere4ca4412015-02-23 19:27:00 +0000327 void VisitWhileStmt(const WhileStmt *S) {
328 RecordStmtCount(S);
Justin Bognera909abf2015-05-02 00:48:27 +0000329 uint64_t ParentCount = CurrentCount;
Justin Bogner07193cc2015-05-01 23:41:09 +0000330
Justin Bognere4ca4412015-02-23 19:27:00 +0000331 BreakContinueStack.push_back(BreakContinue());
332 // Visit the body region first so the break/continue adjustments can be
333 // included when visiting the condition.
Justin Bogner07193cc2015-05-01 23:41:09 +0000334 uint64_t BodyCount = setCount(PGO.getRegionCount(S));
Justin Bognera909abf2015-05-02 00:48:27 +0000335 CountMap[S->getBody()] = CurrentCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000336 Visit(S->getBody());
Justin Bognera909abf2015-05-02 00:48:27 +0000337 uint64_t BackedgeCount = CurrentCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000338
339 // ...then go back and propagate counts through the condition. The count
340 // at the start of the condition is the sum of the incoming edges,
341 // the backedge from the end of the loop body, and the edges from
342 // continue statements.
343 BreakContinue BC = BreakContinueStack.pop_back_val();
Justin Bogner07193cc2015-05-01 23:41:09 +0000344 uint64_t CondCount =
345 setCount(ParentCount + BackedgeCount + BC.ContinueCount);
346 CountMap[S->getCond()] = CondCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000347 Visit(S->getCond());
Justin Bogner07193cc2015-05-01 23:41:09 +0000348 setCount(BC.BreakCount + CondCount - BodyCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000349 RecordNextStmtCount = true;
350 }
351
352 void VisitDoStmt(const DoStmt *S) {
353 RecordStmtCount(S);
Justin Bogner07193cc2015-05-01 23:41:09 +0000354 uint64_t LoopCount = PGO.getRegionCount(S);
355
Justin Bognere4ca4412015-02-23 19:27:00 +0000356 BreakContinueStack.push_back(BreakContinue());
Justin Bogner07193cc2015-05-01 23:41:09 +0000357 // The count doesn't include the fallthrough from the parent scope. Add it.
Justin Bognera909abf2015-05-02 00:48:27 +0000358 uint64_t BodyCount = setCount(LoopCount + CurrentCount);
Justin Bogner07193cc2015-05-01 23:41:09 +0000359 CountMap[S->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000360 Visit(S->getBody());
Justin Bognera909abf2015-05-02 00:48:27 +0000361 uint64_t BackedgeCount = CurrentCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000362
363 BreakContinue BC = BreakContinueStack.pop_back_val();
364 // The count at the start of the condition is equal to the count at the
Justin Bogner07193cc2015-05-01 23:41:09 +0000365 // end of the body, plus any continues.
366 uint64_t CondCount = setCount(BackedgeCount + BC.ContinueCount);
367 CountMap[S->getCond()] = CondCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000368 Visit(S->getCond());
Justin Bogner07193cc2015-05-01 23:41:09 +0000369 setCount(BC.BreakCount + CondCount - LoopCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000370 RecordNextStmtCount = true;
371 }
372
373 void VisitForStmt(const ForStmt *S) {
374 RecordStmtCount(S);
375 if (S->getInit())
376 Visit(S->getInit());
Justin Bogner07193cc2015-05-01 23:41:09 +0000377
Justin Bognera909abf2015-05-02 00:48:27 +0000378 uint64_t ParentCount = CurrentCount;
Justin Bogner07193cc2015-05-01 23:41:09 +0000379
Justin Bognere4ca4412015-02-23 19:27:00 +0000380 BreakContinueStack.push_back(BreakContinue());
381 // Visit the body region first. (This is basically the same as a while
382 // loop; see further comments in VisitWhileStmt.)
Justin Bogner07193cc2015-05-01 23:41:09 +0000383 uint64_t BodyCount = setCount(PGO.getRegionCount(S));
384 CountMap[S->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000385 Visit(S->getBody());
Justin Bognera909abf2015-05-02 00:48:27 +0000386 uint64_t BackedgeCount = CurrentCount;
Justin Bogner07193cc2015-05-01 23:41:09 +0000387 BreakContinue BC = BreakContinueStack.pop_back_val();
Justin Bognere4ca4412015-02-23 19:27:00 +0000388
389 // The increment is essentially part of the body but it needs to include
390 // the count for all the continue statements.
391 if (S->getInc()) {
Justin Bogner07193cc2015-05-01 23:41:09 +0000392 uint64_t IncCount = setCount(BackedgeCount + BC.ContinueCount);
393 CountMap[S->getInc()] = IncCount;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000394 Visit(S->getInc());
Justin Bognere4ca4412015-02-23 19:27:00 +0000395 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000396
Justin Bognere4ca4412015-02-23 19:27:00 +0000397 // ...then go back and propagate counts through the condition.
Justin Bogner07193cc2015-05-01 23:41:09 +0000398 uint64_t CondCount =
399 setCount(ParentCount + BackedgeCount + BC.ContinueCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000400 if (S->getCond()) {
Justin Bogner07193cc2015-05-01 23:41:09 +0000401 CountMap[S->getCond()] = CondCount;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000402 Visit(S->getCond());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000403 }
Justin Bogner07193cc2015-05-01 23:41:09 +0000404 setCount(BC.BreakCount + CondCount - BodyCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000405 RecordNextStmtCount = true;
406 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000407
Justin Bognere4ca4412015-02-23 19:27:00 +0000408 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
409 RecordStmtCount(S);
Justin Bogner2e5d4842015-04-30 22:58:28 +0000410 Visit(S->getLoopVarStmt());
Justin Bognere4ca4412015-02-23 19:27:00 +0000411 Visit(S->getRangeStmt());
412 Visit(S->getBeginEndStmt());
Justin Bogner07193cc2015-05-01 23:41:09 +0000413
Justin Bognera909abf2015-05-02 00:48:27 +0000414 uint64_t ParentCount = CurrentCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000415 BreakContinueStack.push_back(BreakContinue());
416 // Visit the body region first. (This is basically the same as a while
417 // loop; see further comments in VisitWhileStmt.)
Justin Bogner07193cc2015-05-01 23:41:09 +0000418 uint64_t BodyCount = setCount(PGO.getRegionCount(S));
419 CountMap[S->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000420 Visit(S->getBody());
Justin Bognera909abf2015-05-02 00:48:27 +0000421 uint64_t BackedgeCount = CurrentCount;
Justin Bogner07193cc2015-05-01 23:41:09 +0000422 BreakContinue BC = BreakContinueStack.pop_back_val();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000423
Justin Bognere4ca4412015-02-23 19:27:00 +0000424 // The increment is essentially part of the body but it needs to include
425 // the count for all the continue statements.
Justin Bogner07193cc2015-05-01 23:41:09 +0000426 uint64_t IncCount = setCount(BackedgeCount + BC.ContinueCount);
427 CountMap[S->getInc()] = IncCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000428 Visit(S->getInc());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000429
Justin Bognere4ca4412015-02-23 19:27:00 +0000430 // ...then go back and propagate counts through the condition.
Justin Bogner07193cc2015-05-01 23:41:09 +0000431 uint64_t CondCount =
432 setCount(ParentCount + BackedgeCount + BC.ContinueCount);
433 CountMap[S->getCond()] = CondCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000434 Visit(S->getCond());
Justin Bogner07193cc2015-05-01 23:41:09 +0000435 setCount(BC.BreakCount + CondCount - BodyCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000436 RecordNextStmtCount = true;
437 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000438
Justin Bognere4ca4412015-02-23 19:27:00 +0000439 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
440 RecordStmtCount(S);
441 Visit(S->getElement());
Justin Bognera909abf2015-05-02 00:48:27 +0000442 uint64_t ParentCount = CurrentCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000443 BreakContinueStack.push_back(BreakContinue());
Justin Bogner07193cc2015-05-01 23:41:09 +0000444 // Counter tracks the body of the loop.
445 uint64_t BodyCount = setCount(PGO.getRegionCount(S));
446 CountMap[S->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000447 Visit(S->getBody());
Justin Bognera909abf2015-05-02 00:48:27 +0000448 uint64_t BackedgeCount = CurrentCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000449 BreakContinue BC = BreakContinueStack.pop_back_val();
Justin Bogner07193cc2015-05-01 23:41:09 +0000450
451 setCount(BC.BreakCount + ParentCount + BackedgeCount + BC.ContinueCount -
452 BodyCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000453 RecordNextStmtCount = true;
454 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000455
Justin Bognere4ca4412015-02-23 19:27:00 +0000456 void VisitSwitchStmt(const SwitchStmt *S) {
457 RecordStmtCount(S);
458 Visit(S->getCond());
Justin Bognera909abf2015-05-02 00:48:27 +0000459 CurrentCount = 0;
Justin Bognere4ca4412015-02-23 19:27:00 +0000460 BreakContinueStack.push_back(BreakContinue());
461 Visit(S->getBody());
462 // If the switch is inside a loop, add the continue counts.
463 BreakContinue BC = BreakContinueStack.pop_back_val();
464 if (!BreakContinueStack.empty())
465 BreakContinueStack.back().ContinueCount += BC.ContinueCount;
466 // Counter tracks the exit block of the switch.
Justin Bogner07193cc2015-05-01 23:41:09 +0000467 setCount(PGO.getRegionCount(S));
Justin Bognere4ca4412015-02-23 19:27:00 +0000468 RecordNextStmtCount = true;
469 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000470
Justin Bogner07193cc2015-05-01 23:41:09 +0000471 void VisitSwitchCase(const SwitchCase *S) {
Justin Bognere4ca4412015-02-23 19:27:00 +0000472 RecordNextStmtCount = false;
473 // Counter for this particular case. This counts only jumps from the
474 // switch header and does not include fallthrough from the case before
475 // this one.
Justin Bogner07193cc2015-05-01 23:41:09 +0000476 uint64_t CaseCount = PGO.getRegionCount(S);
Justin Bognera909abf2015-05-02 00:48:27 +0000477 setCount(CurrentCount + CaseCount);
Justin Bogner07193cc2015-05-01 23:41:09 +0000478 // We need the count without fallthrough in the mapping, so it's more useful
479 // for branch probabilities.
480 CountMap[S] = CaseCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000481 RecordNextStmtCount = true;
482 Visit(S->getSubStmt());
483 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000484
Justin Bognere4ca4412015-02-23 19:27:00 +0000485 void VisitIfStmt(const IfStmt *S) {
486 RecordStmtCount(S);
Justin Bognera909abf2015-05-02 00:48:27 +0000487 uint64_t ParentCount = CurrentCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000488 Visit(S->getCond());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000489
Justin Bogner07193cc2015-05-01 23:41:09 +0000490 // Counter tracks the "then" part of an if statement. The count for
491 // the "else" part, if it exists, will be calculated from this counter.
492 uint64_t ThenCount = setCount(PGO.getRegionCount(S));
493 CountMap[S->getThen()] = ThenCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000494 Visit(S->getThen());
Justin Bognera909abf2015-05-02 00:48:27 +0000495 uint64_t OutCount = CurrentCount;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000496
Justin Bogner07193cc2015-05-01 23:41:09 +0000497 uint64_t ElseCount = ParentCount - ThenCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000498 if (S->getElse()) {
Justin Bogner07193cc2015-05-01 23:41:09 +0000499 setCount(ElseCount);
500 CountMap[S->getElse()] = ElseCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000501 Visit(S->getElse());
Justin Bognera909abf2015-05-02 00:48:27 +0000502 OutCount += CurrentCount;
Justin Bogner07193cc2015-05-01 23:41:09 +0000503 } else
504 OutCount += ElseCount;
505 setCount(OutCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000506 RecordNextStmtCount = true;
507 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000508
Justin Bognere4ca4412015-02-23 19:27:00 +0000509 void VisitCXXTryStmt(const CXXTryStmt *S) {
510 RecordStmtCount(S);
511 Visit(S->getTryBlock());
512 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
513 Visit(S->getHandler(I));
514 // Counter tracks the continuation block of the try statement.
Justin Bogner07193cc2015-05-01 23:41:09 +0000515 setCount(PGO.getRegionCount(S));
Justin Bognere4ca4412015-02-23 19:27:00 +0000516 RecordNextStmtCount = true;
517 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000518
Justin Bognere4ca4412015-02-23 19:27:00 +0000519 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
520 RecordNextStmtCount = false;
521 // Counter tracks the catch statement's handler block.
Justin Bogner07193cc2015-05-01 23:41:09 +0000522 uint64_t CatchCount = setCount(PGO.getRegionCount(S));
523 CountMap[S] = CatchCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000524 Visit(S->getHandlerBlock());
525 }
526
527 void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
528 RecordStmtCount(E);
Justin Bognera909abf2015-05-02 00:48:27 +0000529 uint64_t ParentCount = CurrentCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000530 Visit(E->getCond());
531
Justin Bogner07193cc2015-05-01 23:41:09 +0000532 // Counter tracks the "true" part of a conditional operator. The
533 // count in the "false" part will be calculated from this counter.
534 uint64_t TrueCount = setCount(PGO.getRegionCount(E));
535 CountMap[E->getTrueExpr()] = TrueCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000536 Visit(E->getTrueExpr());
Justin Bognera909abf2015-05-02 00:48:27 +0000537 uint64_t OutCount = CurrentCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000538
Justin Bogner07193cc2015-05-01 23:41:09 +0000539 uint64_t FalseCount = setCount(ParentCount - TrueCount);
540 CountMap[E->getFalseExpr()] = FalseCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000541 Visit(E->getFalseExpr());
Justin Bognera909abf2015-05-02 00:48:27 +0000542 OutCount += CurrentCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000543
Justin Bogner07193cc2015-05-01 23:41:09 +0000544 setCount(OutCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000545 RecordNextStmtCount = true;
546 }
547
548 void VisitBinLAnd(const BinaryOperator *E) {
549 RecordStmtCount(E);
Justin Bognera909abf2015-05-02 00:48:27 +0000550 uint64_t ParentCount = CurrentCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000551 Visit(E->getLHS());
Justin Bogner07193cc2015-05-01 23:41:09 +0000552 // Counter tracks the right hand side of a logical and operator.
553 uint64_t RHSCount = setCount(PGO.getRegionCount(E));
554 CountMap[E->getRHS()] = RHSCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000555 Visit(E->getRHS());
Justin Bognera909abf2015-05-02 00:48:27 +0000556 setCount(ParentCount + RHSCount - CurrentCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000557 RecordNextStmtCount = true;
558 }
559
560 void VisitBinLOr(const BinaryOperator *E) {
561 RecordStmtCount(E);
Justin Bognera909abf2015-05-02 00:48:27 +0000562 uint64_t ParentCount = CurrentCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000563 Visit(E->getLHS());
Justin Bogner07193cc2015-05-01 23:41:09 +0000564 // Counter tracks the right hand side of a logical or operator.
565 uint64_t RHSCount = setCount(PGO.getRegionCount(E));
566 CountMap[E->getRHS()] = RHSCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000567 Visit(E->getRHS());
Justin Bognera909abf2015-05-02 00:48:27 +0000568 setCount(ParentCount + RHSCount - CurrentCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000569 RecordNextStmtCount = true;
570 }
571};
Hans Wennborgdcfba332015-10-06 23:40:43 +0000572} // end anonymous namespace
Justin Bogneref512b92014-01-06 22:27:43 +0000573
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000574void PGOHash::combine(HashType Type) {
575 // Check that we never combine 0 and only have six bits.
576 assert(Type && "Hash is invalid: unexpected type 0");
577 assert(unsigned(Type) < TooBig && "Hash is invalid: too many types");
578
579 // Pass through MD5 if enough work has built up.
580 if (Count && Count % NumTypesPerWord == 0) {
581 using namespace llvm::support;
582 uint64_t Swapped = endian::byte_swap<uint64_t, little>(Working);
583 MD5.update(llvm::makeArrayRef((uint8_t *)&Swapped, sizeof(Swapped)));
584 Working = 0;
585 }
586
587 // Accumulate the current type.
588 ++Count;
589 Working = Working << NumBitsPerType | Type;
590}
591
592uint64_t PGOHash::finalize() {
593 // Use Working as the hash directly if we never used MD5.
594 if (Count <= NumTypesPerWord)
595 // No need to byte swap here, since none of the math was endian-dependent.
596 // This number will be byte-swapped as required on endianness transitions,
597 // so we will see the same value on the other side.
598 return Working;
599
600 // Check for remaining work in Working.
601 if (Working)
602 MD5.update(Working);
603
604 // Finalize the MD5 and return the hash.
605 llvm::MD5::MD5Result Result;
606 MD5.final(Result);
607 using namespace llvm::support;
608 return endian::read<uint64_t, little, unaligned>(Result);
609}
610
Serge Pavlov3a561452015-12-06 14:32:39 +0000611void CodeGenPGO::assignRegionCounters(GlobalDecl GD, llvm::Function *Fn) {
612 const Decl *D = GD.getDecl();
Justin Bogneref512b92014-01-06 22:27:43 +0000613 bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate;
Justin Bogner837a6f62014-04-18 21:52:00 +0000614 llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
615 if (!InstrumentRegions && !PGOReader)
Justin Bogneref512b92014-01-06 22:27:43 +0000616 return;
Justin Bogner3212b182014-04-25 07:20:05 +0000617 if (D->isImplicit())
Justin Bogneref512b92014-01-06 22:27:43 +0000618 return;
Serge Pavlov3a561452015-12-06 14:32:39 +0000619 // Constructors and destructors may be represented by several functions in IR.
620 // If so, instrument only base variant, others are implemented by delegation
621 // to the base one, it would be counted twice otherwise.
622 if (CGM.getTarget().getCXXABI().hasConstructorVariants() &&
623 ((isa<CXXConstructorDecl>(GD.getDecl()) &&
624 GD.getCtorType() != Ctor_Base) ||
625 (isa<CXXDestructorDecl>(GD.getDecl()) &&
626 GD.getDtorType() != Dtor_Base))) {
627 return;
628 }
Alex Lorenzee024992014-08-04 18:41:51 +0000629 CGM.ClearUnusedCoverageMapping(D);
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000630 setFuncName(Fn);
Duncan P. N. Exon Smith7c414512014-03-20 22:50:08 +0000631
Justin Bogneref512b92014-01-06 22:27:43 +0000632 mapRegionCounters(D);
Justin Bogner970ac602014-12-08 19:04:51 +0000633 if (CGM.getCodeGenOpts().CoverageMapping)
634 emitCounterRegionMapping(D);
Justin Bogner837a6f62014-04-18 21:52:00 +0000635 if (PGOReader) {
Justin Bogner40b8ba12014-06-26 01:45:07 +0000636 SourceManager &SM = CGM.getContext().getSourceManager();
637 loadRegionCounts(PGOReader, SM.isInMainFile(D->getLocation()));
Bob Wilsonbf854f02014-02-17 19:21:09 +0000638 computeRegionCounts(D);
Justin Bogner837a6f62014-04-18 21:52:00 +0000639 applyFunctionAttributes(PGOReader, Fn);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000640 }
Justin Bogneref512b92014-01-06 22:27:43 +0000641}
642
643void CodeGenPGO::mapRegionCounters(const Decl *D) {
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +0000644 RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000645 MapRegionCounters Walker(*RegionCounterMap);
Justin Bogneref512b92014-01-06 22:27:43 +0000646 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000647 Walker.TraverseDecl(const_cast<FunctionDecl *>(FD));
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000648 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000649 Walker.TraverseDecl(const_cast<ObjCMethodDecl *>(MD));
Bob Wilsonc845c002014-03-06 20:24:27 +0000650 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000651 Walker.TraverseDecl(const_cast<BlockDecl *>(BD));
Justin Bogner81ab90f2014-04-15 00:50:54 +0000652 else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
653 Walker.TraverseDecl(const_cast<CapturedDecl *>(CD));
Justin Bogner3212b182014-04-25 07:20:05 +0000654 assert(Walker.NextCounter > 0 && "no entry counter mapped for decl");
Justin Bogneref512b92014-01-06 22:27:43 +0000655 NumRegionCounters = Walker.NextCounter;
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000656 FunctionHash = Walker.Hash.finalize();
Justin Bogneref512b92014-01-06 22:27:43 +0000657}
658
Alex Lorenzee024992014-08-04 18:41:51 +0000659void CodeGenPGO::emitCounterRegionMapping(const Decl *D) {
660 if (SkipCoverageMapping)
661 return;
662 // Don't map the functions inside the system headers
663 auto Loc = D->getBody()->getLocStart();
664 if (CGM.getContext().getSourceManager().isInSystemHeader(Loc))
665 return;
666
Justin Bogner970ac602014-12-08 19:04:51 +0000667 std::string CoverageMapping;
Alex Lorenzee024992014-08-04 18:41:51 +0000668 llvm::raw_string_ostream OS(CoverageMapping);
669 CoverageMappingGen MappingGen(*CGM.getCoverageMapping(),
670 CGM.getContext().getSourceManager(),
Justin Bognere5ee6c52014-10-02 16:44:01 +0000671 CGM.getLangOpts(), RegionCounterMap.get());
Alex Lorenzee024992014-08-04 18:41:51 +0000672 MappingGen.emitCounterMapping(D, OS);
673 OS.flush();
Justin Bogner970ac602014-12-08 19:04:51 +0000674
675 if (CoverageMapping.empty())
676 return;
677
678 CGM.getCoverageMapping()->addFunctionMappingRecord(
679 FuncNameVar, FuncName, FunctionHash, CoverageMapping);
Alex Lorenzee024992014-08-04 18:41:51 +0000680}
681
682void
Justin Bogner60d852b2015-04-23 00:31:16 +0000683CodeGenPGO::emitEmptyCounterMapping(const Decl *D, StringRef Name,
Alex Lorenzee024992014-08-04 18:41:51 +0000684 llvm::GlobalValue::LinkageTypes Linkage) {
685 if (SkipCoverageMapping)
686 return;
Alex Lorenzee024992014-08-04 18:41:51 +0000687 // Don't map the functions inside the system headers
688 auto Loc = D->getBody()->getLocStart();
689 if (CGM.getContext().getSourceManager().isInSystemHeader(Loc))
690 return;
691
Justin Bogner970ac602014-12-08 19:04:51 +0000692 std::string CoverageMapping;
Alex Lorenzee024992014-08-04 18:41:51 +0000693 llvm::raw_string_ostream OS(CoverageMapping);
694 CoverageMappingGen MappingGen(*CGM.getCoverageMapping(),
695 CGM.getContext().getSourceManager(),
696 CGM.getLangOpts());
697 MappingGen.emitEmptyMapping(D, OS);
698 OS.flush();
Justin Bogner970ac602014-12-08 19:04:51 +0000699
700 if (CoverageMapping.empty())
701 return;
702
Justin Bogner60d852b2015-04-23 00:31:16 +0000703 setFuncName(Name, Linkage);
Justin Bogner970ac602014-12-08 19:04:51 +0000704 CGM.getCoverageMapping()->addFunctionMappingRecord(
Xinliang David Li2129ae52016-01-07 20:05:55 +0000705 FuncNameVar, FuncName, FunctionHash, CoverageMapping, false);
Alex Lorenzee024992014-08-04 18:41:51 +0000706}
707
Bob Wilsonbf854f02014-02-17 19:21:09 +0000708void CodeGenPGO::computeRegionCounts(const Decl *D) {
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +0000709 StmtCountMap.reset(new llvm::DenseMap<const Stmt *, uint64_t>);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000710 ComputeRegionCounts Walker(*StmtCountMap, *this);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000711 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
712 Walker.VisitFunctionDecl(FD);
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000713 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
714 Walker.VisitObjCMethodDecl(MD);
Bob Wilsonc845c002014-03-06 20:24:27 +0000715 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
716 Walker.VisitBlockDecl(BD);
Justin Bogner81ab90f2014-04-15 00:50:54 +0000717 else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
718 Walker.VisitCapturedDecl(const_cast<CapturedDecl *>(CD));
Bob Wilsonbf854f02014-02-17 19:21:09 +0000719}
720
Justin Bogner837a6f62014-04-18 21:52:00 +0000721void
722CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
723 llvm::Function *Fn) {
Justin Bogner4c9c45c2014-03-12 18:14:32 +0000724 if (!haveRegionCounts())
725 return;
726
Hans Wennborgdcfba332015-10-06 23:40:43 +0000727 uint64_t FunctionCount = getRegionCount(nullptr);
Diego Novilloaa8b1cb2015-05-27 21:58:42 +0000728 Fn->setEntryCount(FunctionCount);
Justin Bogner4c9c45c2014-03-12 18:14:32 +0000729}
730
Justin Bogner66242d62015-04-23 23:06:47 +0000731void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S) {
Justin Bogner970ac602014-12-08 19:04:51 +0000732 if (!CGM.getCodeGenOpts().ProfileInstrGenerate || !RegionCounterMap)
Justin Bogneref512b92014-01-06 22:27:43 +0000733 return;
Duncan P. N. Exon Smith9f5260a2015-11-06 23:00:41 +0000734 if (!Builder.GetInsertBlock())
Justin Bogner203f91e2015-01-09 01:46:40 +0000735 return;
Justin Bogner66242d62015-04-23 23:06:47 +0000736
737 unsigned Counter = (*RegionCounterMap)[S];
Justin Bogner970ac602014-12-08 19:04:51 +0000738 auto *I8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
David Blaikie43f9bb72015-05-18 22:14:03 +0000739 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::instrprof_increment),
740 {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
Justin Bogner970ac602014-12-08 19:04:51 +0000741 Builder.getInt64(FunctionHash),
742 Builder.getInt32(NumRegionCounters),
David Blaikie43f9bb72015-05-18 22:14:03 +0000743 Builder.getInt32(Counter)});
Justin Bogneref512b92014-01-06 22:27:43 +0000744}
745
Betul Buyukkurt518276a2016-01-23 22:50:44 +0000746// This method either inserts a call to the profile run-time during
747// instrumentation or puts profile data into metadata for PGO use.
748void CodeGenPGO::valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
749 llvm::Instruction *ValueSite, llvm::Value *ValuePtr) {
750
751 if (!EnableValueProfiling)
752 return;
753
754 if (!ValuePtr || !ValueSite || !Builder.GetInsertBlock())
755 return;
756
757 bool InstrumentValueSites = CGM.getCodeGenOpts().ProfileInstrGenerate;
758 if (InstrumentValueSites && RegionCounterMap) {
759 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
760 auto *I8PtrTy = llvm::Type::getInt8PtrTy(Ctx);
761 llvm::Value *Args[5] = {
762 llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
763 Builder.getInt64(FunctionHash),
764 Builder.CreatePtrToInt(ValuePtr, Builder.getInt64Ty()),
765 Builder.getInt32(ValueKind),
766 Builder.getInt32(NumValueSites[ValueKind]++)
767 };
768 Builder.CreateCall(
769 CGM.getIntrinsic(llvm::Intrinsic::instrprof_value_profile), Args);
770 return;
771 }
772
773 llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
774 if (PGOReader && haveRegionCounts()) {
775 // We record the top most called three functions at each call site.
776 // Profile metadata contains "VP" string identifying this metadata
777 // as value profiling data, then a uint32_t value for the value profiling
778 // kind, a uint64_t value for the total number of times the call is
779 // executed, followed by the function hash and execution count (uint64_t)
780 // pairs for each function.
781 if (NumValueSites[ValueKind] >= ProfRecord->getNumValueSites(ValueKind))
782 return;
783 uint32_t NV = ProfRecord->getNumValueDataForSite(ValueKind,
784 NumValueSites[ValueKind]);
785 std::unique_ptr<InstrProfValueData[]> VD =
786 ProfRecord->getValueForSite(ValueKind, NumValueSites[ValueKind]);
787
788 uint64_t Sum = 0;
789 for (uint32_t I = 0; I < NV; ++I)
790 Sum += VD[I].Count;
791
792 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
793 llvm::MDBuilder MDHelper(Ctx);
794 SmallVector<llvm::Metadata*, 3> Vals;
795 Vals.push_back(MDHelper.createString("VP"));
796 Vals.push_back(MDHelper.createConstant(
797 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), ValueKind)));
798 Vals.push_back(MDHelper.createConstant(
799 llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), Sum)));
800
801 uint32_t MDCount = 3;
802 for (uint32_t I = 0; I < NV; ++I) {
803 Vals.push_back(MDHelper.createConstant(
804 llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), VD[I].Value)));
805 Vals.push_back(MDHelper.createConstant(
806 llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), VD[I].Count)));
807 if (--MDCount == 0)
808 break;
809 }
810 ValueSite->setMetadata(
811 llvm::LLVMContext::MD_prof, llvm::MDNode::get(Ctx, Vals));
812 NumValueSites[ValueKind]++;
813 }
814}
815
Justin Bogner40b8ba12014-06-26 01:45:07 +0000816void CodeGenPGO::loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
817 bool IsInMainFile) {
818 CGM.getPGOStats().addVisited(IsInMainFile);
Justin Bogner7f8cf5b2014-12-02 22:38:52 +0000819 RegionCounts.clear();
Betul Buyukkurt518276a2016-01-23 22:50:44 +0000820 llvm::ErrorOr<llvm::InstrProfRecord> RecordErrorOr =
821 PGOReader->getInstrProfRecord(FuncName, FunctionHash);
822 if (std::error_code EC = RecordErrorOr.getError()) {
Justin Bogner9c6818e2014-08-01 22:50:16 +0000823 if (EC == llvm::instrprof_error::unknown_function)
824 CGM.getPGOStats().addMissing(IsInMainFile);
825 else if (EC == llvm::instrprof_error::hash_mismatch)
826 CGM.getPGOStats().addMismatched(IsInMainFile);
827 else if (EC == llvm::instrprof_error::malformed)
828 // TODO: Consider a more specific warning for this case.
829 CGM.getPGOStats().addMismatched(IsInMainFile);
Betul Buyukkurt518276a2016-01-23 22:50:44 +0000830 return;
Justin Bognere2ef2a02014-04-15 21:22:35 +0000831 }
Betul Buyukkurt518276a2016-01-23 22:50:44 +0000832 ProfRecord =
833 llvm::make_unique<llvm::InstrProfRecord>(std::move(RecordErrorOr.get()));
834 RegionCounts = ProfRecord->Counts;
Justin Bogneref512b92014-01-06 22:27:43 +0000835}
836
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000837/// \brief Calculate what to divide by to scale weights.
838///
839/// Given the maximum weight, calculate a divisor that will scale all the
840/// weights to strictly less than UINT32_MAX.
841static uint64_t calculateWeightScale(uint64_t MaxWeight) {
842 return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1;
843}
844
845/// \brief Scale an individual branch weight (and add 1).
846///
847/// Scale a 64-bit weight down to 32-bits using \c Scale.
848///
849/// According to Laplace's Rule of Succession, it is better to compute the
850/// weight based on the count plus 1, so universally add 1 to the value.
851///
852/// \pre \c Scale was calculated by \a calculateWeightScale() with a weight no
853/// greater than \c Weight.
854static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale) {
855 assert(Scale && "scale by 0?");
856 uint64_t Scaled = Weight / Scale + 1;
857 assert(Scaled <= UINT32_MAX && "overflow 32-bits");
858 return Scaled;
859}
860
Justin Bogner65512642015-05-02 05:00:55 +0000861llvm::MDNode *CodeGenFunction::createProfileWeights(uint64_t TrueCount,
862 uint64_t FalseCount) {
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000863 // Check for empty weights.
Justin Bogneref512b92014-01-06 22:27:43 +0000864 if (!TrueCount && !FalseCount)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000865 return nullptr;
Justin Bogneref512b92014-01-06 22:27:43 +0000866
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000867 // Calculate how to scale down to 32-bits.
868 uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount));
869
Justin Bogneref512b92014-01-06 22:27:43 +0000870 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000871 return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale),
872 scaleBranchWeight(FalseCount, Scale));
Justin Bogneref512b92014-01-06 22:27:43 +0000873}
874
Justin Bogner65512642015-05-02 05:00:55 +0000875llvm::MDNode *
876CodeGenFunction::createProfileWeights(ArrayRef<uint64_t> Weights) {
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000877 // We need at least two elements to create meaningful weights.
878 if (Weights.size() < 2)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000879 return nullptr;
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000880
Justin Bognerf3aefca2014-04-04 02:48:51 +0000881 // Check for empty weights.
882 uint64_t MaxWeight = *std::max_element(Weights.begin(), Weights.end());
883 if (MaxWeight == 0)
884 return nullptr;
885
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000886 // Calculate how to scale down to 32-bits.
Justin Bognerf3aefca2014-04-04 02:48:51 +0000887 uint64_t Scale = calculateWeightScale(MaxWeight);
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000888
Justin Bogneref512b92014-01-06 22:27:43 +0000889 SmallVector<uint32_t, 16> ScaledWeights;
890 ScaledWeights.reserve(Weights.size());
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000891 for (uint64_t W : Weights)
892 ScaledWeights.push_back(scaleBranchWeight(W, Scale));
893
894 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
Justin Bogneref512b92014-01-06 22:27:43 +0000895 return MDHelper.createBranchWeights(ScaledWeights);
896}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000897
Justin Bogner65512642015-05-02 05:00:55 +0000898llvm::MDNode *CodeGenFunction::createProfileWeightsForLoop(const Stmt *Cond,
899 uint64_t LoopCount) {
900 if (!PGO.haveRegionCounts())
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000901 return nullptr;
Justin Bogner65512642015-05-02 05:00:55 +0000902 Optional<uint64_t> CondCount = PGO.getStmtCount(Cond);
Justin Bogner1c21c282015-04-13 12:23:19 +0000903 assert(CondCount.hasValue() && "missing expected loop condition count");
904 if (*CondCount == 0)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000905 return nullptr;
Justin Bogner65512642015-05-02 05:00:55 +0000906 return createProfileWeights(LoopCount,
907 std::max(*CondCount, LoopCount) - LoopCount);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000908}