blob: d9016774fa11b6c3c31118326647ee5a0610d5d6 [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"
Justin Bogner837a6f62014-04-18 21:52:00 +000021#include "llvm/ProfileData/InstrProfReader.h"
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +000022#include "llvm/Support/Endian.h"
Justin Bogneref512b92014-01-06 22:27:43 +000023#include "llvm/Support/FileSystem.h"
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +000024#include "llvm/Support/MD5.h"
Justin Bogneref512b92014-01-06 22:27:43 +000025
26using namespace clang;
27using namespace CodeGen;
28
Alex Lorenzee024992014-08-04 18:41:51 +000029void CodeGenPGO::setFuncName(StringRef Name,
30 llvm::GlobalValue::LinkageTypes Linkage) {
Justin Bogner111c6532014-12-02 23:15:30 +000031 StringRef RawFuncName = Name;
Bob Wilsonda1ebed2014-03-06 04:55:41 +000032
33 // Function names may be prefixed with a binary '1' to indicate
34 // that the backend should not modify the symbols due to any platform
35 // naming convention. Do not include that '1' in the PGO profile name.
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000036 if (RawFuncName[0] == '\1')
37 RawFuncName = RawFuncName.substr(1);
Bob Wilsonda1ebed2014-03-06 04:55:41 +000038
Justin Bogner111c6532014-12-02 23:15:30 +000039 FuncName = RawFuncName;
40 if (llvm::GlobalValue::isLocalLinkage(Linkage)) {
41 // For local symbols, prepend the main file name to distinguish them.
42 // Do not include the full path in the file name since there's no guarantee
43 // that it will stay the same, e.g., if the files are checked out from
44 // version control in different locations.
45 if (CGM.getCodeGenOpts().MainFileName.empty())
46 FuncName = FuncName.insert(0, "<unknown>:");
47 else
48 FuncName = FuncName.insert(0, CGM.getCodeGenOpts().MainFileName + ":");
Bob Wilsonda1ebed2014-03-06 04:55:41 +000049 }
Justin Bogner970ac602014-12-08 19:04:51 +000050
51 // If we're generating a profile, create a variable for the name.
52 if (CGM.getCodeGenOpts().ProfileInstrGenerate)
53 createFuncNameVar(Linkage);
Bob Wilsonda1ebed2014-03-06 04:55:41 +000054}
55
Alex Lorenzee024992014-08-04 18:41:51 +000056void CodeGenPGO::setFuncName(llvm::Function *Fn) {
57 setFuncName(Fn->getName(), Fn->getLinkage());
58}
59
Justin Bogner970ac602014-12-08 19:04:51 +000060void CodeGenPGO::createFuncNameVar(llvm::GlobalValue::LinkageTypes Linkage) {
Justin Bognere9fe0a22015-03-20 06:34:38 +000061 // We generally want to match the function's linkage, but available_externally
62 // and extern_weak both have the wrong semantics, and anything that doesn't
63 // need to link across compilation units doesn't need to be visible at all.
Justin Bogner970ac602014-12-08 19:04:51 +000064 if (Linkage == llvm::GlobalValue::ExternalWeakLinkage)
65 Linkage = llvm::GlobalValue::LinkOnceAnyLinkage;
66 else if (Linkage == llvm::GlobalValue::AvailableExternallyLinkage)
67 Linkage = llvm::GlobalValue::LinkOnceODRLinkage;
Justin Bognere9fe0a22015-03-20 06:34:38 +000068 else if (Linkage == llvm::GlobalValue::InternalLinkage ||
69 Linkage == llvm::GlobalValue::ExternalLinkage)
70 Linkage = llvm::GlobalValue::PrivateLinkage;
Alex Lorenzee024992014-08-04 18:41:51 +000071
Justin Bogner970ac602014-12-08 19:04:51 +000072 auto *Value =
73 llvm::ConstantDataArray::getString(CGM.getLLVMContext(), FuncName, false);
74 FuncNameVar =
75 new llvm::GlobalVariable(CGM.getModule(), Value->getType(), true, Linkage,
76 Value, "__llvm_profile_name_" + FuncName);
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000077
Justin Bogner970ac602014-12-08 19:04:51 +000078 // Hide the symbol so that we correctly get a copy for each executable.
79 if (!llvm::GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
80 FuncNameVar->setVisibility(llvm::GlobalValue::HiddenVisibility);
Justin Bogneref512b92014-01-06 22:27:43 +000081}
82
83namespace {
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +000084/// \brief Stable hasher for PGO region counters.
85///
86/// PGOHash produces a stable hash of a given function's control flow.
87///
88/// Changing the output of this hash will invalidate all previously generated
89/// profiles -- i.e., don't do it.
90///
91/// \note When this hash does eventually change (years?), we still need to
92/// support old hashes. We'll need to pull in the version number from the
93/// profile data format and use the matching hash function.
94class PGOHash {
95 uint64_t Working;
96 unsigned Count;
97 llvm::MD5 MD5;
98
99 static const int NumBitsPerType = 6;
100 static const unsigned NumTypesPerWord = sizeof(uint64_t) * 8 / NumBitsPerType;
101 static const unsigned TooBig = 1u << NumBitsPerType;
102
103public:
104 /// \brief Hash values for AST nodes.
105 ///
106 /// Distinct values for AST nodes that have region counters attached.
107 ///
108 /// These values must be stable. All new members must be added at the end,
109 /// and no members should be removed. Changing the enumeration value for an
110 /// AST node will affect the hash of every function that contains that node.
111 enum HashType : unsigned char {
112 None = 0,
113 LabelStmt = 1,
114 WhileStmt,
115 DoStmt,
116 ForStmt,
117 CXXForRangeStmt,
118 ObjCForCollectionStmt,
119 SwitchStmt,
120 CaseStmt,
121 DefaultStmt,
122 IfStmt,
123 CXXTryStmt,
124 CXXCatchStmt,
125 ConditionalOperator,
126 BinaryOperatorLAnd,
127 BinaryOperatorLOr,
128 BinaryConditionalOperator,
129
130 // Keep this last. It's for the static assert that follows.
131 LastHashType
132 };
133 static_assert(LastHashType <= TooBig, "Too many types in HashType");
134
135 // TODO: When this format changes, take in a version number here, and use the
136 // old hash calculation for file formats that used the old hash.
137 PGOHash() : Working(0), Count(0) {}
138 void combine(HashType Type);
139 uint64_t finalize();
140};
141const int PGOHash::NumBitsPerType;
142const unsigned PGOHash::NumTypesPerWord;
143const unsigned PGOHash::TooBig;
144
Justin Bognere4ca4412015-02-23 19:27:00 +0000145/// A RecursiveASTVisitor that fills a map of statements to PGO counters.
146struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
147 /// The next counter value to assign.
148 unsigned NextCounter;
149 /// The function hash.
150 PGOHash Hash;
151 /// The map of statements to counters.
152 llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
Justin Bogneref512b92014-01-06 22:27:43 +0000153
Justin Bognere4ca4412015-02-23 19:27:00 +0000154 MapRegionCounters(llvm::DenseMap<const Stmt *, unsigned> &CounterMap)
155 : NextCounter(0), CounterMap(CounterMap) {}
Justin Bogneref512b92014-01-06 22:27:43 +0000156
Justin Bognere4ca4412015-02-23 19:27:00 +0000157 // Blocks and lambdas are handled as separate functions, so we need not
158 // traverse them in the parent context.
159 bool TraverseBlockExpr(BlockExpr *BE) { return true; }
160 bool TraverseLambdaBody(LambdaExpr *LE) { return true; }
161 bool TraverseCapturedStmt(CapturedStmt *CS) { return true; }
Justin Bogneref512b92014-01-06 22:27:43 +0000162
Justin Bognere4ca4412015-02-23 19:27:00 +0000163 bool VisitDecl(const Decl *D) {
164 switch (D->getKind()) {
165 default:
166 break;
167 case Decl::Function:
168 case Decl::CXXMethod:
169 case Decl::CXXConstructor:
170 case Decl::CXXDestructor:
171 case Decl::CXXConversion:
172 case Decl::ObjCMethod:
173 case Decl::Block:
174 case Decl::Captured:
175 CounterMap[D->getBody()] = NextCounter++;
176 break;
177 }
178 return true;
179 }
180
181 bool VisitStmt(const Stmt *S) {
182 auto Type = getHashType(S);
183 if (Type == PGOHash::None)
Bob Wilsond8931422014-04-11 17:16:13 +0000184 return true;
Bob Wilsond8931422014-04-11 17:16:13 +0000185
Justin Bognere4ca4412015-02-23 19:27:00 +0000186 CounterMap[S] = NextCounter++;
187 Hash.combine(Type);
188 return true;
189 }
190 PGOHash::HashType getHashType(const Stmt *S) {
191 switch (S->getStmtClass()) {
192 default:
193 break;
194 case Stmt::LabelStmtClass:
195 return PGOHash::LabelStmt;
196 case Stmt::WhileStmtClass:
197 return PGOHash::WhileStmt;
198 case Stmt::DoStmtClass:
199 return PGOHash::DoStmt;
200 case Stmt::ForStmtClass:
201 return PGOHash::ForStmt;
202 case Stmt::CXXForRangeStmtClass:
203 return PGOHash::CXXForRangeStmt;
204 case Stmt::ObjCForCollectionStmtClass:
205 return PGOHash::ObjCForCollectionStmt;
206 case Stmt::SwitchStmtClass:
207 return PGOHash::SwitchStmt;
208 case Stmt::CaseStmtClass:
209 return PGOHash::CaseStmt;
210 case Stmt::DefaultStmtClass:
211 return PGOHash::DefaultStmt;
212 case Stmt::IfStmtClass:
213 return PGOHash::IfStmt;
214 case Stmt::CXXTryStmtClass:
215 return PGOHash::CXXTryStmt;
216 case Stmt::CXXCatchStmtClass:
217 return PGOHash::CXXCatchStmt;
218 case Stmt::ConditionalOperatorClass:
219 return PGOHash::ConditionalOperator;
220 case Stmt::BinaryConditionalOperatorClass:
221 return PGOHash::BinaryConditionalOperator;
222 case Stmt::BinaryOperatorClass: {
223 const BinaryOperator *BO = cast<BinaryOperator>(S);
224 if (BO->getOpcode() == BO_LAnd)
225 return PGOHash::BinaryOperatorLAnd;
226 if (BO->getOpcode() == BO_LOr)
227 return PGOHash::BinaryOperatorLOr;
228 break;
229 }
230 }
231 return PGOHash::None;
232 }
233};
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000234
Justin Bognere4ca4412015-02-23 19:27:00 +0000235/// A StmtVisitor that propagates the raw counts through the AST and
236/// records the count at statements where the value may change.
237struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> {
238 /// PGO state.
239 CodeGenPGO &PGO;
240
241 /// A flag that is set when the current count should be recorded on the
242 /// next statement, such as at the exit of a loop.
243 bool RecordNextStmtCount;
244
245 /// The map of statements to count values.
246 llvm::DenseMap<const Stmt *, uint64_t> &CountMap;
247
248 /// BreakContinueStack - Keep counts of breaks and continues inside loops.
249 struct BreakContinue {
250 uint64_t BreakCount;
251 uint64_t ContinueCount;
252 BreakContinue() : BreakCount(0), ContinueCount(0) {}
Justin Bogneref512b92014-01-06 22:27:43 +0000253 };
Justin Bognere4ca4412015-02-23 19:27:00 +0000254 SmallVector<BreakContinue, 8> BreakContinueStack;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000255
Justin Bognere4ca4412015-02-23 19:27:00 +0000256 ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &CountMap,
257 CodeGenPGO &PGO)
258 : PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000259
Justin Bognere4ca4412015-02-23 19:27:00 +0000260 void RecordStmtCount(const Stmt *S) {
261 if (RecordNextStmtCount) {
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000262 CountMap[S] = PGO.getCurrentRegionCount();
Justin Bognere4ca4412015-02-23 19:27:00 +0000263 RecordNextStmtCount = false;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000264 }
Justin Bognere4ca4412015-02-23 19:27:00 +0000265 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000266
Justin Bogner07193cc2015-05-01 23:41:09 +0000267 /// Set and return the current count.
268 uint64_t setCount(uint64_t Count) {
269 PGO.setCurrentRegionCount(Count);
270 return Count;
271 }
272
Justin Bognere4ca4412015-02-23 19:27:00 +0000273 void VisitStmt(const Stmt *S) {
274 RecordStmtCount(S);
275 for (Stmt::const_child_range I = S->children(); I; ++I) {
276 if (*I)
277 this->Visit(*I);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000278 }
Justin Bognere4ca4412015-02-23 19:27:00 +0000279 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000280
Justin Bognere4ca4412015-02-23 19:27:00 +0000281 void VisitFunctionDecl(const FunctionDecl *D) {
282 // Counter tracks entry to the function body.
Justin Bogner07193cc2015-05-01 23:41:09 +0000283 uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
284 CountMap[D->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000285 Visit(D->getBody());
286 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000287
Justin Bognere4ca4412015-02-23 19:27:00 +0000288 // Skip lambda expressions. We visit these as FunctionDecls when we're
289 // generating them and aren't interested in the body when generating a
290 // parent context.
291 void VisitLambdaExpr(const LambdaExpr *LE) {}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000292
Justin Bognere4ca4412015-02-23 19:27:00 +0000293 void VisitCapturedDecl(const CapturedDecl *D) {
294 // Counter tracks entry to the capture body.
Justin Bogner07193cc2015-05-01 23:41:09 +0000295 uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
296 CountMap[D->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000297 Visit(D->getBody());
298 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000299
Justin Bognere4ca4412015-02-23 19:27:00 +0000300 void VisitObjCMethodDecl(const ObjCMethodDecl *D) {
301 // Counter tracks entry to the method body.
Justin Bogner07193cc2015-05-01 23:41:09 +0000302 uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
303 CountMap[D->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000304 Visit(D->getBody());
305 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000306
Justin Bognere4ca4412015-02-23 19:27:00 +0000307 void VisitBlockDecl(const BlockDecl *D) {
308 // Counter tracks entry to the block body.
Justin Bogner07193cc2015-05-01 23:41:09 +0000309 uint64_t BodyCount = setCount(PGO.getRegionCount(D->getBody()));
310 CountMap[D->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000311 Visit(D->getBody());
312 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000313
Justin Bognere4ca4412015-02-23 19:27:00 +0000314 void VisitReturnStmt(const ReturnStmt *S) {
315 RecordStmtCount(S);
316 if (S->getRetValue())
317 Visit(S->getRetValue());
318 PGO.setCurrentRegionUnreachable();
319 RecordNextStmtCount = true;
320 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000321
Justin Bognerf959feb2015-04-28 06:31:55 +0000322 void VisitCXXThrowExpr(const CXXThrowExpr *E) {
323 RecordStmtCount(E);
324 if (E->getSubExpr())
325 Visit(E->getSubExpr());
326 PGO.setCurrentRegionUnreachable();
327 RecordNextStmtCount = true;
328 }
329
Justin Bognere4ca4412015-02-23 19:27:00 +0000330 void VisitGotoStmt(const GotoStmt *S) {
331 RecordStmtCount(S);
332 PGO.setCurrentRegionUnreachable();
333 RecordNextStmtCount = true;
334 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000335
Justin Bognere4ca4412015-02-23 19:27:00 +0000336 void VisitLabelStmt(const LabelStmt *S) {
337 RecordNextStmtCount = false;
338 // Counter tracks the block following the label.
Justin Bogner07193cc2015-05-01 23:41:09 +0000339 uint64_t BlockCount = setCount(PGO.getRegionCount(S));
340 CountMap[S] = BlockCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000341 Visit(S->getSubStmt());
342 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000343
Justin Bognere4ca4412015-02-23 19:27:00 +0000344 void VisitBreakStmt(const BreakStmt *S) {
345 RecordStmtCount(S);
346 assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
347 BreakContinueStack.back().BreakCount += PGO.getCurrentRegionCount();
348 PGO.setCurrentRegionUnreachable();
349 RecordNextStmtCount = true;
350 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000351
Justin Bognere4ca4412015-02-23 19:27:00 +0000352 void VisitContinueStmt(const ContinueStmt *S) {
353 RecordStmtCount(S);
354 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
355 BreakContinueStack.back().ContinueCount += PGO.getCurrentRegionCount();
356 PGO.setCurrentRegionUnreachable();
357 RecordNextStmtCount = true;
358 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000359
Justin Bognere4ca4412015-02-23 19:27:00 +0000360 void VisitWhileStmt(const WhileStmt *S) {
361 RecordStmtCount(S);
Justin Bogner07193cc2015-05-01 23:41:09 +0000362 uint64_t ParentCount = PGO.getCurrentRegionCount();
363
Justin Bognere4ca4412015-02-23 19:27:00 +0000364 BreakContinueStack.push_back(BreakContinue());
365 // Visit the body region first so the break/continue adjustments can be
366 // included when visiting the condition.
Justin Bogner07193cc2015-05-01 23:41:09 +0000367 uint64_t BodyCount = setCount(PGO.getRegionCount(S));
Justin Bognere4ca4412015-02-23 19:27:00 +0000368 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
369 Visit(S->getBody());
Justin Bogner07193cc2015-05-01 23:41:09 +0000370 uint64_t BackedgeCount = PGO.getCurrentRegionCount();
Justin Bognere4ca4412015-02-23 19:27:00 +0000371
372 // ...then go back and propagate counts through the condition. The count
373 // at the start of the condition is the sum of the incoming edges,
374 // the backedge from the end of the loop body, and the edges from
375 // continue statements.
376 BreakContinue BC = BreakContinueStack.pop_back_val();
Justin Bogner07193cc2015-05-01 23:41:09 +0000377 uint64_t CondCount =
378 setCount(ParentCount + BackedgeCount + BC.ContinueCount);
379 CountMap[S->getCond()] = CondCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000380 Visit(S->getCond());
Justin Bogner07193cc2015-05-01 23:41:09 +0000381 setCount(BC.BreakCount + CondCount - BodyCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000382 RecordNextStmtCount = true;
383 }
384
385 void VisitDoStmt(const DoStmt *S) {
386 RecordStmtCount(S);
Justin Bogner07193cc2015-05-01 23:41:09 +0000387 uint64_t LoopCount = PGO.getRegionCount(S);
388
Justin Bognere4ca4412015-02-23 19:27:00 +0000389 BreakContinueStack.push_back(BreakContinue());
Justin Bogner07193cc2015-05-01 23:41:09 +0000390 // The count doesn't include the fallthrough from the parent scope. Add it.
391 uint64_t BodyCount = setCount(LoopCount + PGO.getCurrentRegionCount());
392 CountMap[S->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000393 Visit(S->getBody());
Justin Bogner07193cc2015-05-01 23:41:09 +0000394 uint64_t BackedgeCount = PGO.getCurrentRegionCount();
Justin Bognere4ca4412015-02-23 19:27:00 +0000395
396 BreakContinue BC = BreakContinueStack.pop_back_val();
397 // The count at the start of the condition is equal to the count at the
Justin Bogner07193cc2015-05-01 23:41:09 +0000398 // end of the body, plus any continues.
399 uint64_t CondCount = setCount(BackedgeCount + BC.ContinueCount);
400 CountMap[S->getCond()] = CondCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000401 Visit(S->getCond());
Justin Bogner07193cc2015-05-01 23:41:09 +0000402 setCount(BC.BreakCount + CondCount - LoopCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000403 RecordNextStmtCount = true;
404 }
405
406 void VisitForStmt(const ForStmt *S) {
407 RecordStmtCount(S);
408 if (S->getInit())
409 Visit(S->getInit());
Justin Bogner07193cc2015-05-01 23:41:09 +0000410
411 uint64_t ParentCount = PGO.getCurrentRegionCount();
412
Justin Bognere4ca4412015-02-23 19:27:00 +0000413 BreakContinueStack.push_back(BreakContinue());
414 // Visit the body region first. (This is basically the same as a while
415 // loop; see further comments in VisitWhileStmt.)
Justin Bogner07193cc2015-05-01 23:41:09 +0000416 uint64_t BodyCount = setCount(PGO.getRegionCount(S));
417 CountMap[S->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000418 Visit(S->getBody());
Justin Bogner07193cc2015-05-01 23:41:09 +0000419 uint64_t BackedgeCount = PGO.getCurrentRegionCount();
420 BreakContinue BC = BreakContinueStack.pop_back_val();
Justin Bognere4ca4412015-02-23 19:27:00 +0000421
422 // The increment is essentially part of the body but it needs to include
423 // the count for all the continue statements.
424 if (S->getInc()) {
Justin Bogner07193cc2015-05-01 23:41:09 +0000425 uint64_t IncCount = setCount(BackedgeCount + BC.ContinueCount);
426 CountMap[S->getInc()] = IncCount;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000427 Visit(S->getInc());
Justin Bognere4ca4412015-02-23 19:27:00 +0000428 }
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);
Justin Bognere4ca4412015-02-23 19:27:00 +0000433 if (S->getCond()) {
Justin Bogner07193cc2015-05-01 23:41:09 +0000434 CountMap[S->getCond()] = CondCount;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000435 Visit(S->getCond());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000436 }
Justin Bogner07193cc2015-05-01 23:41:09 +0000437 setCount(BC.BreakCount + CondCount - BodyCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000438 RecordNextStmtCount = true;
439 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000440
Justin Bognere4ca4412015-02-23 19:27:00 +0000441 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
442 RecordStmtCount(S);
Justin Bogner2e5d4842015-04-30 22:58:28 +0000443 Visit(S->getLoopVarStmt());
Justin Bognere4ca4412015-02-23 19:27:00 +0000444 Visit(S->getRangeStmt());
445 Visit(S->getBeginEndStmt());
Justin Bogner07193cc2015-05-01 23:41:09 +0000446
447 uint64_t ParentCount = PGO.getCurrentRegionCount();
448
Justin Bognere4ca4412015-02-23 19:27:00 +0000449 BreakContinueStack.push_back(BreakContinue());
450 // Visit the body region first. (This is basically the same as a while
451 // loop; see further comments in VisitWhileStmt.)
Justin Bogner07193cc2015-05-01 23:41:09 +0000452 uint64_t BodyCount = setCount(PGO.getRegionCount(S));
453 CountMap[S->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000454 Visit(S->getBody());
Justin Bogner07193cc2015-05-01 23:41:09 +0000455 uint64_t BackedgeCount = PGO.getCurrentRegionCount();
456 BreakContinue BC = BreakContinueStack.pop_back_val();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000457
Justin Bognere4ca4412015-02-23 19:27:00 +0000458 // The increment is essentially part of the body but it needs to include
459 // the count for all the continue statements.
Justin Bogner07193cc2015-05-01 23:41:09 +0000460 uint64_t IncCount = setCount(BackedgeCount + BC.ContinueCount);
461 CountMap[S->getInc()] = IncCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000462 Visit(S->getInc());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000463
Justin Bognere4ca4412015-02-23 19:27:00 +0000464 // ...then go back and propagate counts through the condition.
Justin Bogner07193cc2015-05-01 23:41:09 +0000465 uint64_t CondCount =
466 setCount(ParentCount + BackedgeCount + BC.ContinueCount);
467 CountMap[S->getCond()] = CondCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000468 Visit(S->getCond());
Justin Bogner07193cc2015-05-01 23:41:09 +0000469 setCount(BC.BreakCount + CondCount - BodyCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000470 RecordNextStmtCount = true;
471 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000472
Justin Bognere4ca4412015-02-23 19:27:00 +0000473 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
474 RecordStmtCount(S);
475 Visit(S->getElement());
Justin Bogner07193cc2015-05-01 23:41:09 +0000476 uint64_t ParentCount = PGO.getCurrentRegionCount();
Justin Bognere4ca4412015-02-23 19:27:00 +0000477 BreakContinueStack.push_back(BreakContinue());
Justin Bogner07193cc2015-05-01 23:41:09 +0000478 // Counter tracks the body of the loop.
479 uint64_t BodyCount = setCount(PGO.getRegionCount(S));
480 CountMap[S->getBody()] = BodyCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000481 Visit(S->getBody());
Justin Bogner07193cc2015-05-01 23:41:09 +0000482 uint64_t BackedgeCount = PGO.getCurrentRegionCount();
Justin Bognere4ca4412015-02-23 19:27:00 +0000483 BreakContinue BC = BreakContinueStack.pop_back_val();
Justin Bogner07193cc2015-05-01 23:41:09 +0000484
485 setCount(BC.BreakCount + ParentCount + BackedgeCount + BC.ContinueCount -
486 BodyCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000487 RecordNextStmtCount = true;
488 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000489
Justin Bognere4ca4412015-02-23 19:27:00 +0000490 void VisitSwitchStmt(const SwitchStmt *S) {
491 RecordStmtCount(S);
492 Visit(S->getCond());
493 PGO.setCurrentRegionUnreachable();
494 BreakContinueStack.push_back(BreakContinue());
495 Visit(S->getBody());
496 // If the switch is inside a loop, add the continue counts.
497 BreakContinue BC = BreakContinueStack.pop_back_val();
498 if (!BreakContinueStack.empty())
499 BreakContinueStack.back().ContinueCount += BC.ContinueCount;
500 // Counter tracks the exit block of the switch.
Justin Bogner07193cc2015-05-01 23:41:09 +0000501 setCount(PGO.getRegionCount(S));
Justin Bognere4ca4412015-02-23 19:27:00 +0000502 RecordNextStmtCount = true;
503 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000504
Justin Bogner07193cc2015-05-01 23:41:09 +0000505 void VisitSwitchCase(const SwitchCase *S) {
Justin Bognere4ca4412015-02-23 19:27:00 +0000506 RecordNextStmtCount = false;
507 // Counter for this particular case. This counts only jumps from the
508 // switch header and does not include fallthrough from the case before
509 // this one.
Justin Bogner07193cc2015-05-01 23:41:09 +0000510 uint64_t CaseCount = PGO.getRegionCount(S);
511 setCount(PGO.getCurrentRegionCount() + CaseCount);
512 // We need the count without fallthrough in the mapping, so it's more useful
513 // for branch probabilities.
514 CountMap[S] = CaseCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000515 RecordNextStmtCount = true;
516 Visit(S->getSubStmt());
517 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000518
Justin Bognere4ca4412015-02-23 19:27:00 +0000519 void VisitIfStmt(const IfStmt *S) {
520 RecordStmtCount(S);
Justin Bogner07193cc2015-05-01 23:41:09 +0000521 uint64_t ParentCount = PGO.getCurrentRegionCount();
Justin Bognere4ca4412015-02-23 19:27:00 +0000522 Visit(S->getCond());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000523
Justin Bogner07193cc2015-05-01 23:41:09 +0000524 // Counter tracks the "then" part of an if statement. The count for
525 // the "else" part, if it exists, will be calculated from this counter.
526 uint64_t ThenCount = setCount(PGO.getRegionCount(S));
527 CountMap[S->getThen()] = ThenCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000528 Visit(S->getThen());
Justin Bogner07193cc2015-05-01 23:41:09 +0000529 uint64_t OutCount = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000530
Justin Bogner07193cc2015-05-01 23:41:09 +0000531 uint64_t ElseCount = ParentCount - ThenCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000532 if (S->getElse()) {
Justin Bogner07193cc2015-05-01 23:41:09 +0000533 setCount(ElseCount);
534 CountMap[S->getElse()] = ElseCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000535 Visit(S->getElse());
Justin Bogner07193cc2015-05-01 23:41:09 +0000536 OutCount += PGO.getCurrentRegionCount();
537 } else
538 OutCount += ElseCount;
539 setCount(OutCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000540 RecordNextStmtCount = true;
541 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000542
Justin Bognere4ca4412015-02-23 19:27:00 +0000543 void VisitCXXTryStmt(const CXXTryStmt *S) {
544 RecordStmtCount(S);
545 Visit(S->getTryBlock());
546 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
547 Visit(S->getHandler(I));
548 // Counter tracks the continuation block of the try statement.
Justin Bogner07193cc2015-05-01 23:41:09 +0000549 setCount(PGO.getRegionCount(S));
Justin Bognere4ca4412015-02-23 19:27:00 +0000550 RecordNextStmtCount = true;
551 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000552
Justin Bognere4ca4412015-02-23 19:27:00 +0000553 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
554 RecordNextStmtCount = false;
555 // Counter tracks the catch statement's handler block.
Justin Bogner07193cc2015-05-01 23:41:09 +0000556 uint64_t CatchCount = setCount(PGO.getRegionCount(S));
557 CountMap[S] = CatchCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000558 Visit(S->getHandlerBlock());
559 }
560
561 void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
562 RecordStmtCount(E);
Justin Bogner07193cc2015-05-01 23:41:09 +0000563 uint64_t ParentCount = PGO.getCurrentRegionCount();
Justin Bognere4ca4412015-02-23 19:27:00 +0000564 Visit(E->getCond());
565
Justin Bogner07193cc2015-05-01 23:41:09 +0000566 // Counter tracks the "true" part of a conditional operator. The
567 // count in the "false" part will be calculated from this counter.
568 uint64_t TrueCount = setCount(PGO.getRegionCount(E));
569 CountMap[E->getTrueExpr()] = TrueCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000570 Visit(E->getTrueExpr());
Justin Bogner07193cc2015-05-01 23:41:09 +0000571 uint64_t OutCount = PGO.getCurrentRegionCount();
Justin Bognere4ca4412015-02-23 19:27:00 +0000572
Justin Bogner07193cc2015-05-01 23:41:09 +0000573 uint64_t FalseCount = setCount(ParentCount - TrueCount);
574 CountMap[E->getFalseExpr()] = FalseCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000575 Visit(E->getFalseExpr());
Justin Bogner07193cc2015-05-01 23:41:09 +0000576 OutCount += PGO.getCurrentRegionCount();
Justin Bognere4ca4412015-02-23 19:27:00 +0000577
Justin Bogner07193cc2015-05-01 23:41:09 +0000578 setCount(OutCount);
Justin Bognere4ca4412015-02-23 19:27:00 +0000579 RecordNextStmtCount = true;
580 }
581
582 void VisitBinLAnd(const BinaryOperator *E) {
583 RecordStmtCount(E);
Justin Bogner07193cc2015-05-01 23:41:09 +0000584 uint64_t ParentCount = PGO.getCurrentRegionCount();
Justin Bognere4ca4412015-02-23 19:27:00 +0000585 Visit(E->getLHS());
Justin Bogner07193cc2015-05-01 23:41:09 +0000586 // Counter tracks the right hand side of a logical and operator.
587 uint64_t RHSCount = setCount(PGO.getRegionCount(E));
588 CountMap[E->getRHS()] = RHSCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000589 Visit(E->getRHS());
Justin Bogner07193cc2015-05-01 23:41:09 +0000590 setCount(ParentCount + RHSCount - PGO.getCurrentRegionCount());
Justin Bognere4ca4412015-02-23 19:27:00 +0000591 RecordNextStmtCount = true;
592 }
593
594 void VisitBinLOr(const BinaryOperator *E) {
595 RecordStmtCount(E);
Justin Bogner07193cc2015-05-01 23:41:09 +0000596 uint64_t ParentCount = PGO.getCurrentRegionCount();
Justin Bognere4ca4412015-02-23 19:27:00 +0000597 Visit(E->getLHS());
Justin Bogner07193cc2015-05-01 23:41:09 +0000598 // Counter tracks the right hand side of a logical or operator.
599 uint64_t RHSCount = setCount(PGO.getRegionCount(E));
600 CountMap[E->getRHS()] = RHSCount;
Justin Bognere4ca4412015-02-23 19:27:00 +0000601 Visit(E->getRHS());
Justin Bogner07193cc2015-05-01 23:41:09 +0000602 setCount(ParentCount + RHSCount - PGO.getCurrentRegionCount());
Justin Bognere4ca4412015-02-23 19:27:00 +0000603 RecordNextStmtCount = true;
604 }
605};
Justin Bogneref512b92014-01-06 22:27:43 +0000606}
607
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000608void PGOHash::combine(HashType Type) {
609 // Check that we never combine 0 and only have six bits.
610 assert(Type && "Hash is invalid: unexpected type 0");
611 assert(unsigned(Type) < TooBig && "Hash is invalid: too many types");
612
613 // Pass through MD5 if enough work has built up.
614 if (Count && Count % NumTypesPerWord == 0) {
615 using namespace llvm::support;
616 uint64_t Swapped = endian::byte_swap<uint64_t, little>(Working);
617 MD5.update(llvm::makeArrayRef((uint8_t *)&Swapped, sizeof(Swapped)));
618 Working = 0;
619 }
620
621 // Accumulate the current type.
622 ++Count;
623 Working = Working << NumBitsPerType | Type;
624}
625
626uint64_t PGOHash::finalize() {
627 // Use Working as the hash directly if we never used MD5.
628 if (Count <= NumTypesPerWord)
629 // No need to byte swap here, since none of the math was endian-dependent.
630 // This number will be byte-swapped as required on endianness transitions,
631 // so we will see the same value on the other side.
632 return Working;
633
634 // Check for remaining work in Working.
635 if (Working)
636 MD5.update(Working);
637
638 // Finalize the MD5 and return the hash.
639 llvm::MD5::MD5Result Result;
640 MD5.final(Result);
641 using namespace llvm::support;
642 return endian::read<uint64_t, little, unaligned>(Result);
643}
644
Alex Lorenzee024992014-08-04 18:41:51 +0000645void CodeGenPGO::checkGlobalDecl(GlobalDecl GD) {
646 // Make sure we only emit coverage mapping for one constructor/destructor.
647 // Clang emits several functions for the constructor and the destructor of
648 // a class. Every function is instrumented, but we only want to provide
649 // coverage for one of them. Because of that we only emit the coverage mapping
650 // for the base constructor/destructor.
651 if ((isa<CXXConstructorDecl>(GD.getDecl()) &&
Alex Lorenzd4236742014-08-11 18:21:34 +0000652 GD.getCtorType() != Ctor_Base) ||
Alex Lorenzee024992014-08-04 18:41:51 +0000653 (isa<CXXDestructorDecl>(GD.getDecl()) &&
654 GD.getDtorType() != Dtor_Base)) {
655 SkipCoverageMapping = true;
656 }
657}
658
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000659void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) {
Justin Bogneref512b92014-01-06 22:27:43 +0000660 bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate;
Justin Bogner837a6f62014-04-18 21:52:00 +0000661 llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
662 if (!InstrumentRegions && !PGOReader)
Justin Bogneref512b92014-01-06 22:27:43 +0000663 return;
Justin Bogner3212b182014-04-25 07:20:05 +0000664 if (D->isImplicit())
Justin Bogneref512b92014-01-06 22:27:43 +0000665 return;
Alex Lorenzee024992014-08-04 18:41:51 +0000666 CGM.ClearUnusedCoverageMapping(D);
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000667 setFuncName(Fn);
Duncan P. N. Exon Smith7c414512014-03-20 22:50:08 +0000668
Justin Bogneref512b92014-01-06 22:27:43 +0000669 mapRegionCounters(D);
Justin Bogner970ac602014-12-08 19:04:51 +0000670 if (CGM.getCodeGenOpts().CoverageMapping)
671 emitCounterRegionMapping(D);
Justin Bogner837a6f62014-04-18 21:52:00 +0000672 if (PGOReader) {
Justin Bogner40b8ba12014-06-26 01:45:07 +0000673 SourceManager &SM = CGM.getContext().getSourceManager();
674 loadRegionCounts(PGOReader, SM.isInMainFile(D->getLocation()));
Bob Wilsonbf854f02014-02-17 19:21:09 +0000675 computeRegionCounts(D);
Justin Bogner837a6f62014-04-18 21:52:00 +0000676 applyFunctionAttributes(PGOReader, Fn);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000677 }
Justin Bogneref512b92014-01-06 22:27:43 +0000678}
679
680void CodeGenPGO::mapRegionCounters(const Decl *D) {
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +0000681 RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000682 MapRegionCounters Walker(*RegionCounterMap);
Justin Bogneref512b92014-01-06 22:27:43 +0000683 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000684 Walker.TraverseDecl(const_cast<FunctionDecl *>(FD));
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000685 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000686 Walker.TraverseDecl(const_cast<ObjCMethodDecl *>(MD));
Bob Wilsonc845c002014-03-06 20:24:27 +0000687 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000688 Walker.TraverseDecl(const_cast<BlockDecl *>(BD));
Justin Bogner81ab90f2014-04-15 00:50:54 +0000689 else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
690 Walker.TraverseDecl(const_cast<CapturedDecl *>(CD));
Justin Bogner3212b182014-04-25 07:20:05 +0000691 assert(Walker.NextCounter > 0 && "no entry counter mapped for decl");
Justin Bogneref512b92014-01-06 22:27:43 +0000692 NumRegionCounters = Walker.NextCounter;
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000693 FunctionHash = Walker.Hash.finalize();
Justin Bogneref512b92014-01-06 22:27:43 +0000694}
695
Alex Lorenzee024992014-08-04 18:41:51 +0000696void CodeGenPGO::emitCounterRegionMapping(const Decl *D) {
697 if (SkipCoverageMapping)
698 return;
699 // Don't map the functions inside the system headers
700 auto Loc = D->getBody()->getLocStart();
701 if (CGM.getContext().getSourceManager().isInSystemHeader(Loc))
702 return;
703
Justin Bogner970ac602014-12-08 19:04:51 +0000704 std::string CoverageMapping;
Alex Lorenzee024992014-08-04 18:41:51 +0000705 llvm::raw_string_ostream OS(CoverageMapping);
706 CoverageMappingGen MappingGen(*CGM.getCoverageMapping(),
707 CGM.getContext().getSourceManager(),
Justin Bognere5ee6c52014-10-02 16:44:01 +0000708 CGM.getLangOpts(), RegionCounterMap.get());
Alex Lorenzee024992014-08-04 18:41:51 +0000709 MappingGen.emitCounterMapping(D, OS);
710 OS.flush();
Justin Bogner970ac602014-12-08 19:04:51 +0000711
712 if (CoverageMapping.empty())
713 return;
714
715 CGM.getCoverageMapping()->addFunctionMappingRecord(
716 FuncNameVar, FuncName, FunctionHash, CoverageMapping);
Alex Lorenzee024992014-08-04 18:41:51 +0000717}
718
719void
Justin Bogner60d852b2015-04-23 00:31:16 +0000720CodeGenPGO::emitEmptyCounterMapping(const Decl *D, StringRef Name,
Alex Lorenzee024992014-08-04 18:41:51 +0000721 llvm::GlobalValue::LinkageTypes Linkage) {
722 if (SkipCoverageMapping)
723 return;
Alex Lorenzee024992014-08-04 18:41:51 +0000724 // Don't map the functions inside the system headers
725 auto Loc = D->getBody()->getLocStart();
726 if (CGM.getContext().getSourceManager().isInSystemHeader(Loc))
727 return;
728
Justin Bogner970ac602014-12-08 19:04:51 +0000729 std::string CoverageMapping;
Alex Lorenzee024992014-08-04 18:41:51 +0000730 llvm::raw_string_ostream OS(CoverageMapping);
731 CoverageMappingGen MappingGen(*CGM.getCoverageMapping(),
732 CGM.getContext().getSourceManager(),
733 CGM.getLangOpts());
734 MappingGen.emitEmptyMapping(D, OS);
735 OS.flush();
Justin Bogner970ac602014-12-08 19:04:51 +0000736
737 if (CoverageMapping.empty())
738 return;
739
Justin Bogner60d852b2015-04-23 00:31:16 +0000740 setFuncName(Name, Linkage);
Justin Bogner970ac602014-12-08 19:04:51 +0000741 CGM.getCoverageMapping()->addFunctionMappingRecord(
742 FuncNameVar, FuncName, FunctionHash, CoverageMapping);
Alex Lorenzee024992014-08-04 18:41:51 +0000743}
744
Bob Wilsonbf854f02014-02-17 19:21:09 +0000745void CodeGenPGO::computeRegionCounts(const Decl *D) {
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +0000746 StmtCountMap.reset(new llvm::DenseMap<const Stmt *, uint64_t>);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000747 ComputeRegionCounts Walker(*StmtCountMap, *this);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000748 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
749 Walker.VisitFunctionDecl(FD);
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000750 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
751 Walker.VisitObjCMethodDecl(MD);
Bob Wilsonc845c002014-03-06 20:24:27 +0000752 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
753 Walker.VisitBlockDecl(BD);
Justin Bogner81ab90f2014-04-15 00:50:54 +0000754 else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
755 Walker.VisitCapturedDecl(const_cast<CapturedDecl *>(CD));
Bob Wilsonbf854f02014-02-17 19:21:09 +0000756}
757
Justin Bogner837a6f62014-04-18 21:52:00 +0000758void
759CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
760 llvm::Function *Fn) {
Justin Bogner4c9c45c2014-03-12 18:14:32 +0000761 if (!haveRegionCounts())
762 return;
763
Justin Bogner837a6f62014-04-18 21:52:00 +0000764 uint64_t MaxFunctionCount = PGOReader->getMaximumFunctionCount();
Justin Bogner4c9c45c2014-03-12 18:14:32 +0000765 uint64_t FunctionCount = getRegionCount(0);
766 if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount))
767 // Turn on InlineHint attribute for hot functions.
768 // FIXME: 30% is from preliminary tuning on SPEC, it may not be optimal.
769 Fn->addFnAttr(llvm::Attribute::InlineHint);
770 else if (FunctionCount <= (uint64_t)(0.01 * (double)MaxFunctionCount))
771 // Turn on Cold attribute for cold functions.
772 // FIXME: 1% is from preliminary tuning on SPEC, it may not be optimal.
773 Fn->addFnAttr(llvm::Attribute::Cold);
774}
775
Justin Bogner66242d62015-04-23 23:06:47 +0000776void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S) {
Justin Bogner970ac602014-12-08 19:04:51 +0000777 if (!CGM.getCodeGenOpts().ProfileInstrGenerate || !RegionCounterMap)
Justin Bogneref512b92014-01-06 22:27:43 +0000778 return;
Justin Bogner203f91e2015-01-09 01:46:40 +0000779 if (!Builder.GetInsertPoint())
780 return;
Justin Bogner66242d62015-04-23 23:06:47 +0000781
782 unsigned Counter = (*RegionCounterMap)[S];
Justin Bogner970ac602014-12-08 19:04:51 +0000783 auto *I8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
784 Builder.CreateCall4(CGM.getIntrinsic(llvm::Intrinsic::instrprof_increment),
785 llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
786 Builder.getInt64(FunctionHash),
787 Builder.getInt32(NumRegionCounters),
788 Builder.getInt32(Counter));
Justin Bogneref512b92014-01-06 22:27:43 +0000789}
790
Justin Bogner40b8ba12014-06-26 01:45:07 +0000791void CodeGenPGO::loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
792 bool IsInMainFile) {
793 CGM.getPGOStats().addVisited(IsInMainFile);
Justin Bogner7f8cf5b2014-12-02 22:38:52 +0000794 RegionCounts.clear();
Justin Bogner970ac602014-12-08 19:04:51 +0000795 if (std::error_code EC =
796 PGOReader->getFunctionCounts(FuncName, FunctionHash, RegionCounts)) {
Justin Bogner9c6818e2014-08-01 22:50:16 +0000797 if (EC == llvm::instrprof_error::unknown_function)
798 CGM.getPGOStats().addMissing(IsInMainFile);
799 else if (EC == llvm::instrprof_error::hash_mismatch)
800 CGM.getPGOStats().addMismatched(IsInMainFile);
801 else if (EC == llvm::instrprof_error::malformed)
802 // TODO: Consider a more specific warning for this case.
803 CGM.getPGOStats().addMismatched(IsInMainFile);
Justin Bogner7f8cf5b2014-12-02 22:38:52 +0000804 RegionCounts.clear();
Justin Bognere2ef2a02014-04-15 21:22:35 +0000805 }
Justin Bogneref512b92014-01-06 22:27:43 +0000806}
807
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000808/// \brief Calculate what to divide by to scale weights.
809///
810/// Given the maximum weight, calculate a divisor that will scale all the
811/// weights to strictly less than UINT32_MAX.
812static uint64_t calculateWeightScale(uint64_t MaxWeight) {
813 return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1;
814}
815
816/// \brief Scale an individual branch weight (and add 1).
817///
818/// Scale a 64-bit weight down to 32-bits using \c Scale.
819///
820/// According to Laplace's Rule of Succession, it is better to compute the
821/// weight based on the count plus 1, so universally add 1 to the value.
822///
823/// \pre \c Scale was calculated by \a calculateWeightScale() with a weight no
824/// greater than \c Weight.
825static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale) {
826 assert(Scale && "scale by 0?");
827 uint64_t Scaled = Weight / Scale + 1;
828 assert(Scaled <= UINT32_MAX && "overflow 32-bits");
829 return Scaled;
830}
831
Justin Bogneref512b92014-01-06 22:27:43 +0000832llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount,
833 uint64_t FalseCount) {
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000834 // Check for empty weights.
Justin Bogneref512b92014-01-06 22:27:43 +0000835 if (!TrueCount && !FalseCount)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000836 return nullptr;
Justin Bogneref512b92014-01-06 22:27:43 +0000837
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000838 // Calculate how to scale down to 32-bits.
839 uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount));
840
Justin Bogneref512b92014-01-06 22:27:43 +0000841 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000842 return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale),
843 scaleBranchWeight(FalseCount, Scale));
Justin Bogneref512b92014-01-06 22:27:43 +0000844}
845
Bob Wilson95a27b02014-02-17 19:20:59 +0000846llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) {
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000847 // We need at least two elements to create meaningful weights.
848 if (Weights.size() < 2)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000849 return nullptr;
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000850
Justin Bognerf3aefca2014-04-04 02:48:51 +0000851 // Check for empty weights.
852 uint64_t MaxWeight = *std::max_element(Weights.begin(), Weights.end());
853 if (MaxWeight == 0)
854 return nullptr;
855
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000856 // Calculate how to scale down to 32-bits.
Justin Bognerf3aefca2014-04-04 02:48:51 +0000857 uint64_t Scale = calculateWeightScale(MaxWeight);
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000858
Justin Bogneref512b92014-01-06 22:27:43 +0000859 SmallVector<uint32_t, 16> ScaledWeights;
860 ScaledWeights.reserve(Weights.size());
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000861 for (uint64_t W : Weights)
862 ScaledWeights.push_back(scaleBranchWeight(W, Scale));
863
864 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
Justin Bogneref512b92014-01-06 22:27:43 +0000865 return MDHelper.createBranchWeights(ScaledWeights);
866}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000867
868llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond,
Justin Bogner66242d62015-04-23 23:06:47 +0000869 uint64_t LoopCount) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000870 if (!haveRegionCounts())
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000871 return nullptr;
Justin Bogner1c21c282015-04-13 12:23:19 +0000872 Optional<uint64_t> CondCount = getStmtCount(Cond);
873 assert(CondCount.hasValue() && "missing expected loop condition count");
874 if (*CondCount == 0)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000875 return nullptr;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000876 return createBranchWeights(LoopCount,
Justin Bogner1c21c282015-04-13 12:23:19 +0000877 std::max(*CondCount, LoopCount) - LoopCount);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000878}