blob: c90b025e55110382df574aaf61cc3d0a11845543 [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 Bognere4ca4412015-02-23 19:27:00 +0000267 void VisitStmt(const Stmt *S) {
268 RecordStmtCount(S);
269 for (Stmt::const_child_range I = S->children(); I; ++I) {
270 if (*I)
271 this->Visit(*I);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000272 }
Justin Bognere4ca4412015-02-23 19:27:00 +0000273 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000274
Justin Bognere4ca4412015-02-23 19:27:00 +0000275 void VisitFunctionDecl(const FunctionDecl *D) {
276 // Counter tracks entry to the function body.
277 RegionCounter Cnt(PGO, D->getBody());
278 Cnt.beginRegion();
279 CountMap[D->getBody()] = PGO.getCurrentRegionCount();
280 Visit(D->getBody());
281 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000282
Justin Bognere4ca4412015-02-23 19:27:00 +0000283 // Skip lambda expressions. We visit these as FunctionDecls when we're
284 // generating them and aren't interested in the body when generating a
285 // parent context.
286 void VisitLambdaExpr(const LambdaExpr *LE) {}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000287
Justin Bognere4ca4412015-02-23 19:27:00 +0000288 void VisitCapturedDecl(const CapturedDecl *D) {
289 // Counter tracks entry to the capture body.
290 RegionCounter Cnt(PGO, D->getBody());
291 Cnt.beginRegion();
292 CountMap[D->getBody()] = PGO.getCurrentRegionCount();
293 Visit(D->getBody());
294 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000295
Justin Bognere4ca4412015-02-23 19:27:00 +0000296 void VisitObjCMethodDecl(const ObjCMethodDecl *D) {
297 // Counter tracks entry to the method body.
298 RegionCounter Cnt(PGO, D->getBody());
299 Cnt.beginRegion();
300 CountMap[D->getBody()] = PGO.getCurrentRegionCount();
301 Visit(D->getBody());
302 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000303
Justin Bognere4ca4412015-02-23 19:27:00 +0000304 void VisitBlockDecl(const BlockDecl *D) {
305 // Counter tracks entry to the block body.
306 RegionCounter Cnt(PGO, D->getBody());
307 Cnt.beginRegion();
308 CountMap[D->getBody()] = PGO.getCurrentRegionCount();
309 Visit(D->getBody());
310 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000311
Justin Bognere4ca4412015-02-23 19:27:00 +0000312 void VisitReturnStmt(const ReturnStmt *S) {
313 RecordStmtCount(S);
314 if (S->getRetValue())
315 Visit(S->getRetValue());
316 PGO.setCurrentRegionUnreachable();
317 RecordNextStmtCount = true;
318 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000319
Justin Bognerf959feb2015-04-28 06:31:55 +0000320 void VisitCXXThrowExpr(const CXXThrowExpr *E) {
321 RecordStmtCount(E);
322 if (E->getSubExpr())
323 Visit(E->getSubExpr());
324 PGO.setCurrentRegionUnreachable();
325 RecordNextStmtCount = true;
326 }
327
Justin Bognere4ca4412015-02-23 19:27:00 +0000328 void VisitGotoStmt(const GotoStmt *S) {
329 RecordStmtCount(S);
330 PGO.setCurrentRegionUnreachable();
331 RecordNextStmtCount = true;
332 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000333
Justin Bognere4ca4412015-02-23 19:27:00 +0000334 void VisitLabelStmt(const LabelStmt *S) {
335 RecordNextStmtCount = false;
336 // Counter tracks the block following the label.
337 RegionCounter Cnt(PGO, S);
338 Cnt.beginRegion();
339 CountMap[S] = PGO.getCurrentRegionCount();
340 Visit(S->getSubStmt());
341 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000342
Justin Bognere4ca4412015-02-23 19:27:00 +0000343 void VisitBreakStmt(const BreakStmt *S) {
344 RecordStmtCount(S);
345 assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
346 BreakContinueStack.back().BreakCount += PGO.getCurrentRegionCount();
347 PGO.setCurrentRegionUnreachable();
348 RecordNextStmtCount = true;
349 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000350
Justin Bognere4ca4412015-02-23 19:27:00 +0000351 void VisitContinueStmt(const ContinueStmt *S) {
352 RecordStmtCount(S);
353 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
354 BreakContinueStack.back().ContinueCount += PGO.getCurrentRegionCount();
355 PGO.setCurrentRegionUnreachable();
356 RecordNextStmtCount = true;
357 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000358
Justin Bognere4ca4412015-02-23 19:27:00 +0000359 void VisitWhileStmt(const WhileStmt *S) {
360 RecordStmtCount(S);
361 // Counter tracks the body of the loop.
362 RegionCounter Cnt(PGO, S);
363 BreakContinueStack.push_back(BreakContinue());
364 // Visit the body region first so the break/continue adjustments can be
365 // included when visiting the condition.
366 Cnt.beginRegion();
367 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
368 Visit(S->getBody());
369 Cnt.adjustForControlFlow();
370
371 // ...then go back and propagate counts through the condition. The count
372 // at the start of the condition is the sum of the incoming edges,
373 // the backedge from the end of the loop body, and the edges from
374 // continue statements.
375 BreakContinue BC = BreakContinueStack.pop_back_val();
376 Cnt.setCurrentRegionCount(Cnt.getParentCount() + Cnt.getAdjustedCount() +
377 BC.ContinueCount);
378 CountMap[S->getCond()] = PGO.getCurrentRegionCount();
379 Visit(S->getCond());
380 Cnt.adjustForControlFlow();
381 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
382 RecordNextStmtCount = true;
383 }
384
385 void VisitDoStmt(const DoStmt *S) {
386 RecordStmtCount(S);
387 // Counter tracks the body of the loop.
388 RegionCounter Cnt(PGO, S);
389 BreakContinueStack.push_back(BreakContinue());
390 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
391 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
392 Visit(S->getBody());
393 Cnt.adjustForControlFlow();
394
395 BreakContinue BC = BreakContinueStack.pop_back_val();
396 // The count at the start of the condition is equal to the count at the
397 // end of the body. The adjusted count does not include either the
398 // fall-through count coming into the loop or the continue count, so add
399 // both of those separately. This is coincidentally the same equation as
400 // with while loops but for different reasons.
401 Cnt.setCurrentRegionCount(Cnt.getParentCount() + Cnt.getAdjustedCount() +
402 BC.ContinueCount);
403 CountMap[S->getCond()] = PGO.getCurrentRegionCount();
404 Visit(S->getCond());
405 Cnt.adjustForControlFlow();
406 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
407 RecordNextStmtCount = true;
408 }
409
410 void VisitForStmt(const ForStmt *S) {
411 RecordStmtCount(S);
412 if (S->getInit())
413 Visit(S->getInit());
414 // Counter tracks the body of the loop.
415 RegionCounter Cnt(PGO, S);
416 BreakContinueStack.push_back(BreakContinue());
417 // Visit the body region first. (This is basically the same as a while
418 // loop; see further comments in VisitWhileStmt.)
419 Cnt.beginRegion();
420 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
421 Visit(S->getBody());
422 Cnt.adjustForControlFlow();
423
424 // The increment is essentially part of the body but it needs to include
425 // the count for all the continue statements.
426 if (S->getInc()) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000427 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
428 BreakContinueStack.back().ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000429 CountMap[S->getInc()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000430 Visit(S->getInc());
431 Cnt.adjustForControlFlow();
Justin Bognere4ca4412015-02-23 19:27:00 +0000432 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000433
Justin Bognere4ca4412015-02-23 19:27:00 +0000434 BreakContinue BC = BreakContinueStack.pop_back_val();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000435
Justin Bognere4ca4412015-02-23 19:27:00 +0000436 // ...then go back and propagate counts through the condition.
437 if (S->getCond()) {
438 Cnt.setCurrentRegionCount(Cnt.getParentCount() + Cnt.getAdjustedCount() +
Bob Wilsonbf854f02014-02-17 19:21:09 +0000439 BC.ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000440 CountMap[S->getCond()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000441 Visit(S->getCond());
442 Cnt.adjustForControlFlow();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000443 }
Justin Bognere4ca4412015-02-23 19:27:00 +0000444 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
445 RecordNextStmtCount = true;
446 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000447
Justin Bognere4ca4412015-02-23 19:27:00 +0000448 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
449 RecordStmtCount(S);
Justin Bogner2e5d4842015-04-30 22:58:28 +0000450 Visit(S->getLoopVarStmt());
Justin Bognere4ca4412015-02-23 19:27:00 +0000451 Visit(S->getRangeStmt());
452 Visit(S->getBeginEndStmt());
453 // Counter tracks the body of the loop.
454 RegionCounter Cnt(PGO, S);
455 BreakContinueStack.push_back(BreakContinue());
456 // Visit the body region first. (This is basically the same as a while
457 // loop; see further comments in VisitWhileStmt.)
458 Cnt.beginRegion();
Justin Bogner2e5d4842015-04-30 22:58:28 +0000459 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
Justin Bognere4ca4412015-02-23 19:27:00 +0000460 Visit(S->getBody());
461 Cnt.adjustForControlFlow();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000462
Justin Bognere4ca4412015-02-23 19:27:00 +0000463 // The increment is essentially part of the body but it needs to include
464 // the count for all the continue statements.
465 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
466 BreakContinueStack.back().ContinueCount);
467 CountMap[S->getInc()] = PGO.getCurrentRegionCount();
468 Visit(S->getInc());
469 Cnt.adjustForControlFlow();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000470
Justin Bognere4ca4412015-02-23 19:27:00 +0000471 BreakContinue BC = BreakContinueStack.pop_back_val();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000472
Justin Bognere4ca4412015-02-23 19:27:00 +0000473 // ...then go back and propagate counts through the condition.
474 Cnt.setCurrentRegionCount(Cnt.getParentCount() + Cnt.getAdjustedCount() +
475 BC.ContinueCount);
476 CountMap[S->getCond()] = PGO.getCurrentRegionCount();
477 Visit(S->getCond());
Justin Bognere4ca4412015-02-23 19:27:00 +0000478 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
479 RecordNextStmtCount = true;
480 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000481
Justin Bognere4ca4412015-02-23 19:27:00 +0000482 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
483 RecordStmtCount(S);
484 Visit(S->getElement());
485 // Counter tracks the body of the loop.
486 RegionCounter Cnt(PGO, S);
487 BreakContinueStack.push_back(BreakContinue());
488 Cnt.beginRegion();
489 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
490 Visit(S->getBody());
491 BreakContinue BC = BreakContinueStack.pop_back_val();
492 Cnt.adjustForControlFlow();
493 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
494 RecordNextStmtCount = true;
495 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000496
Justin Bognere4ca4412015-02-23 19:27:00 +0000497 void VisitSwitchStmt(const SwitchStmt *S) {
498 RecordStmtCount(S);
499 Visit(S->getCond());
500 PGO.setCurrentRegionUnreachable();
501 BreakContinueStack.push_back(BreakContinue());
502 Visit(S->getBody());
503 // If the switch is inside a loop, add the continue counts.
504 BreakContinue BC = BreakContinueStack.pop_back_val();
505 if (!BreakContinueStack.empty())
506 BreakContinueStack.back().ContinueCount += BC.ContinueCount;
507 // Counter tracks the exit block of the switch.
508 RegionCounter ExitCnt(PGO, S);
509 ExitCnt.beginRegion();
510 RecordNextStmtCount = true;
511 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000512
Justin Bognere4ca4412015-02-23 19:27:00 +0000513 void VisitCaseStmt(const CaseStmt *S) {
514 RecordNextStmtCount = false;
515 // Counter for this particular case. This counts only jumps from the
516 // switch header and does not include fallthrough from the case before
517 // this one.
518 RegionCounter Cnt(PGO, S);
519 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
520 CountMap[S] = Cnt.getCount();
521 RecordNextStmtCount = true;
522 Visit(S->getSubStmt());
523 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000524
Justin Bognere4ca4412015-02-23 19:27:00 +0000525 void VisitDefaultStmt(const DefaultStmt *S) {
526 RecordNextStmtCount = false;
527 // Counter for this default case. This does not include fallthrough from
528 // the previous case.
529 RegionCounter Cnt(PGO, S);
530 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
531 CountMap[S] = Cnt.getCount();
532 RecordNextStmtCount = true;
533 Visit(S->getSubStmt());
534 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000535
Justin Bognere4ca4412015-02-23 19:27:00 +0000536 void VisitIfStmt(const IfStmt *S) {
537 RecordStmtCount(S);
538 // Counter tracks the "then" part of an if statement. The count for
539 // the "else" part, if it exists, will be calculated from this counter.
540 RegionCounter Cnt(PGO, S);
541 Visit(S->getCond());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000542
Justin Bognere4ca4412015-02-23 19:27:00 +0000543 Cnt.beginRegion();
544 CountMap[S->getThen()] = PGO.getCurrentRegionCount();
545 Visit(S->getThen());
546 Cnt.adjustForControlFlow();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000547
Justin Bognere4ca4412015-02-23 19:27:00 +0000548 if (S->getElse()) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000549 Cnt.beginElseRegion();
Justin Bognere4ca4412015-02-23 19:27:00 +0000550 CountMap[S->getElse()] = PGO.getCurrentRegionCount();
551 Visit(S->getElse());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000552 Cnt.adjustForControlFlow();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000553 }
Justin Bognere4ca4412015-02-23 19:27:00 +0000554 Cnt.applyAdjustmentsToRegion(0);
555 RecordNextStmtCount = true;
556 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000557
Justin Bognere4ca4412015-02-23 19:27:00 +0000558 void VisitCXXTryStmt(const CXXTryStmt *S) {
559 RecordStmtCount(S);
560 Visit(S->getTryBlock());
561 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
562 Visit(S->getHandler(I));
563 // Counter tracks the continuation block of the try statement.
564 RegionCounter Cnt(PGO, S);
565 Cnt.beginRegion();
566 RecordNextStmtCount = true;
567 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000568
Justin Bognere4ca4412015-02-23 19:27:00 +0000569 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
570 RecordNextStmtCount = false;
571 // Counter tracks the catch statement's handler block.
572 RegionCounter Cnt(PGO, S);
573 Cnt.beginRegion();
574 CountMap[S] = PGO.getCurrentRegionCount();
575 Visit(S->getHandlerBlock());
576 }
577
578 void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
579 RecordStmtCount(E);
580 // Counter tracks the "true" part of a conditional operator. The
581 // count in the "false" part will be calculated from this counter.
582 RegionCounter Cnt(PGO, E);
583 Visit(E->getCond());
584
585 Cnt.beginRegion();
586 CountMap[E->getTrueExpr()] = PGO.getCurrentRegionCount();
587 Visit(E->getTrueExpr());
588 Cnt.adjustForControlFlow();
589
590 Cnt.beginElseRegion();
591 CountMap[E->getFalseExpr()] = PGO.getCurrentRegionCount();
592 Visit(E->getFalseExpr());
593 Cnt.adjustForControlFlow();
594
595 Cnt.applyAdjustmentsToRegion(0);
596 RecordNextStmtCount = true;
597 }
598
599 void VisitBinLAnd(const BinaryOperator *E) {
600 RecordStmtCount(E);
601 // Counter tracks the right hand side of a logical and operator.
602 RegionCounter Cnt(PGO, E);
603 Visit(E->getLHS());
604 Cnt.beginRegion();
605 CountMap[E->getRHS()] = PGO.getCurrentRegionCount();
606 Visit(E->getRHS());
607 Cnt.adjustForControlFlow();
608 Cnt.applyAdjustmentsToRegion(0);
609 RecordNextStmtCount = true;
610 }
611
612 void VisitBinLOr(const BinaryOperator *E) {
613 RecordStmtCount(E);
614 // Counter tracks the right hand side of a logical or operator.
615 RegionCounter Cnt(PGO, E);
616 Visit(E->getLHS());
617 Cnt.beginRegion();
618 CountMap[E->getRHS()] = PGO.getCurrentRegionCount();
619 Visit(E->getRHS());
620 Cnt.adjustForControlFlow();
621 Cnt.applyAdjustmentsToRegion(0);
622 RecordNextStmtCount = true;
623 }
624};
Justin Bogneref512b92014-01-06 22:27:43 +0000625}
626
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000627void PGOHash::combine(HashType Type) {
628 // Check that we never combine 0 and only have six bits.
629 assert(Type && "Hash is invalid: unexpected type 0");
630 assert(unsigned(Type) < TooBig && "Hash is invalid: too many types");
631
632 // Pass through MD5 if enough work has built up.
633 if (Count && Count % NumTypesPerWord == 0) {
634 using namespace llvm::support;
635 uint64_t Swapped = endian::byte_swap<uint64_t, little>(Working);
636 MD5.update(llvm::makeArrayRef((uint8_t *)&Swapped, sizeof(Swapped)));
637 Working = 0;
638 }
639
640 // Accumulate the current type.
641 ++Count;
642 Working = Working << NumBitsPerType | Type;
643}
644
645uint64_t PGOHash::finalize() {
646 // Use Working as the hash directly if we never used MD5.
647 if (Count <= NumTypesPerWord)
648 // No need to byte swap here, since none of the math was endian-dependent.
649 // This number will be byte-swapped as required on endianness transitions,
650 // so we will see the same value on the other side.
651 return Working;
652
653 // Check for remaining work in Working.
654 if (Working)
655 MD5.update(Working);
656
657 // Finalize the MD5 and return the hash.
658 llvm::MD5::MD5Result Result;
659 MD5.final(Result);
660 using namespace llvm::support;
661 return endian::read<uint64_t, little, unaligned>(Result);
662}
663
Alex Lorenzee024992014-08-04 18:41:51 +0000664void CodeGenPGO::checkGlobalDecl(GlobalDecl GD) {
665 // Make sure we only emit coverage mapping for one constructor/destructor.
666 // Clang emits several functions for the constructor and the destructor of
667 // a class. Every function is instrumented, but we only want to provide
668 // coverage for one of them. Because of that we only emit the coverage mapping
669 // for the base constructor/destructor.
670 if ((isa<CXXConstructorDecl>(GD.getDecl()) &&
Alex Lorenzd4236742014-08-11 18:21:34 +0000671 GD.getCtorType() != Ctor_Base) ||
Alex Lorenzee024992014-08-04 18:41:51 +0000672 (isa<CXXDestructorDecl>(GD.getDecl()) &&
673 GD.getDtorType() != Dtor_Base)) {
674 SkipCoverageMapping = true;
675 }
676}
677
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000678void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) {
Justin Bogneref512b92014-01-06 22:27:43 +0000679 bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate;
Justin Bogner837a6f62014-04-18 21:52:00 +0000680 llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
681 if (!InstrumentRegions && !PGOReader)
Justin Bogneref512b92014-01-06 22:27:43 +0000682 return;
Justin Bogner3212b182014-04-25 07:20:05 +0000683 if (D->isImplicit())
Justin Bogneref512b92014-01-06 22:27:43 +0000684 return;
Alex Lorenzee024992014-08-04 18:41:51 +0000685 CGM.ClearUnusedCoverageMapping(D);
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000686 setFuncName(Fn);
Duncan P. N. Exon Smith7c414512014-03-20 22:50:08 +0000687
Justin Bogneref512b92014-01-06 22:27:43 +0000688 mapRegionCounters(D);
Justin Bogner970ac602014-12-08 19:04:51 +0000689 if (CGM.getCodeGenOpts().CoverageMapping)
690 emitCounterRegionMapping(D);
Justin Bogner837a6f62014-04-18 21:52:00 +0000691 if (PGOReader) {
Justin Bogner40b8ba12014-06-26 01:45:07 +0000692 SourceManager &SM = CGM.getContext().getSourceManager();
693 loadRegionCounts(PGOReader, SM.isInMainFile(D->getLocation()));
Bob Wilsonbf854f02014-02-17 19:21:09 +0000694 computeRegionCounts(D);
Justin Bogner837a6f62014-04-18 21:52:00 +0000695 applyFunctionAttributes(PGOReader, Fn);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000696 }
Justin Bogneref512b92014-01-06 22:27:43 +0000697}
698
699void CodeGenPGO::mapRegionCounters(const Decl *D) {
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +0000700 RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000701 MapRegionCounters Walker(*RegionCounterMap);
Justin Bogneref512b92014-01-06 22:27:43 +0000702 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000703 Walker.TraverseDecl(const_cast<FunctionDecl *>(FD));
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000704 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000705 Walker.TraverseDecl(const_cast<ObjCMethodDecl *>(MD));
Bob Wilsonc845c002014-03-06 20:24:27 +0000706 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000707 Walker.TraverseDecl(const_cast<BlockDecl *>(BD));
Justin Bogner81ab90f2014-04-15 00:50:54 +0000708 else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
709 Walker.TraverseDecl(const_cast<CapturedDecl *>(CD));
Justin Bogner3212b182014-04-25 07:20:05 +0000710 assert(Walker.NextCounter > 0 && "no entry counter mapped for decl");
Justin Bogneref512b92014-01-06 22:27:43 +0000711 NumRegionCounters = Walker.NextCounter;
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000712 FunctionHash = Walker.Hash.finalize();
Justin Bogneref512b92014-01-06 22:27:43 +0000713}
714
Alex Lorenzee024992014-08-04 18:41:51 +0000715void CodeGenPGO::emitCounterRegionMapping(const Decl *D) {
716 if (SkipCoverageMapping)
717 return;
718 // Don't map the functions inside the system headers
719 auto Loc = D->getBody()->getLocStart();
720 if (CGM.getContext().getSourceManager().isInSystemHeader(Loc))
721 return;
722
Justin Bogner970ac602014-12-08 19:04:51 +0000723 std::string CoverageMapping;
Alex Lorenzee024992014-08-04 18:41:51 +0000724 llvm::raw_string_ostream OS(CoverageMapping);
725 CoverageMappingGen MappingGen(*CGM.getCoverageMapping(),
726 CGM.getContext().getSourceManager(),
Justin Bognere5ee6c52014-10-02 16:44:01 +0000727 CGM.getLangOpts(), RegionCounterMap.get());
Alex Lorenzee024992014-08-04 18:41:51 +0000728 MappingGen.emitCounterMapping(D, OS);
729 OS.flush();
Justin Bogner970ac602014-12-08 19:04:51 +0000730
731 if (CoverageMapping.empty())
732 return;
733
734 CGM.getCoverageMapping()->addFunctionMappingRecord(
735 FuncNameVar, FuncName, FunctionHash, CoverageMapping);
Alex Lorenzee024992014-08-04 18:41:51 +0000736}
737
738void
Justin Bogner60d852b2015-04-23 00:31:16 +0000739CodeGenPGO::emitEmptyCounterMapping(const Decl *D, StringRef Name,
Alex Lorenzee024992014-08-04 18:41:51 +0000740 llvm::GlobalValue::LinkageTypes Linkage) {
741 if (SkipCoverageMapping)
742 return;
Alex Lorenzee024992014-08-04 18:41:51 +0000743 // Don't map the functions inside the system headers
744 auto Loc = D->getBody()->getLocStart();
745 if (CGM.getContext().getSourceManager().isInSystemHeader(Loc))
746 return;
747
Justin Bogner970ac602014-12-08 19:04:51 +0000748 std::string CoverageMapping;
Alex Lorenzee024992014-08-04 18:41:51 +0000749 llvm::raw_string_ostream OS(CoverageMapping);
750 CoverageMappingGen MappingGen(*CGM.getCoverageMapping(),
751 CGM.getContext().getSourceManager(),
752 CGM.getLangOpts());
753 MappingGen.emitEmptyMapping(D, OS);
754 OS.flush();
Justin Bogner970ac602014-12-08 19:04:51 +0000755
756 if (CoverageMapping.empty())
757 return;
758
Justin Bogner60d852b2015-04-23 00:31:16 +0000759 setFuncName(Name, Linkage);
Justin Bogner970ac602014-12-08 19:04:51 +0000760 CGM.getCoverageMapping()->addFunctionMappingRecord(
761 FuncNameVar, FuncName, FunctionHash, CoverageMapping);
Alex Lorenzee024992014-08-04 18:41:51 +0000762}
763
Bob Wilsonbf854f02014-02-17 19:21:09 +0000764void CodeGenPGO::computeRegionCounts(const Decl *D) {
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +0000765 StmtCountMap.reset(new llvm::DenseMap<const Stmt *, uint64_t>);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000766 ComputeRegionCounts Walker(*StmtCountMap, *this);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000767 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
768 Walker.VisitFunctionDecl(FD);
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000769 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
770 Walker.VisitObjCMethodDecl(MD);
Bob Wilsonc845c002014-03-06 20:24:27 +0000771 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
772 Walker.VisitBlockDecl(BD);
Justin Bogner81ab90f2014-04-15 00:50:54 +0000773 else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
774 Walker.VisitCapturedDecl(const_cast<CapturedDecl *>(CD));
Bob Wilsonbf854f02014-02-17 19:21:09 +0000775}
776
Justin Bogner837a6f62014-04-18 21:52:00 +0000777void
778CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
779 llvm::Function *Fn) {
Justin Bogner4c9c45c2014-03-12 18:14:32 +0000780 if (!haveRegionCounts())
781 return;
782
Justin Bogner837a6f62014-04-18 21:52:00 +0000783 uint64_t MaxFunctionCount = PGOReader->getMaximumFunctionCount();
Justin Bogner4c9c45c2014-03-12 18:14:32 +0000784 uint64_t FunctionCount = getRegionCount(0);
785 if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount))
786 // Turn on InlineHint attribute for hot functions.
787 // FIXME: 30% is from preliminary tuning on SPEC, it may not be optimal.
788 Fn->addFnAttr(llvm::Attribute::InlineHint);
789 else if (FunctionCount <= (uint64_t)(0.01 * (double)MaxFunctionCount))
790 // Turn on Cold attribute for cold functions.
791 // FIXME: 1% is from preliminary tuning on SPEC, it may not be optimal.
792 Fn->addFnAttr(llvm::Attribute::Cold);
793}
794
Justin Bogner66242d62015-04-23 23:06:47 +0000795void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S) {
Justin Bogner970ac602014-12-08 19:04:51 +0000796 if (!CGM.getCodeGenOpts().ProfileInstrGenerate || !RegionCounterMap)
Justin Bogneref512b92014-01-06 22:27:43 +0000797 return;
Justin Bogner203f91e2015-01-09 01:46:40 +0000798 if (!Builder.GetInsertPoint())
799 return;
Justin Bogner66242d62015-04-23 23:06:47 +0000800
801 unsigned Counter = (*RegionCounterMap)[S];
Justin Bogner970ac602014-12-08 19:04:51 +0000802 auto *I8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
803 Builder.CreateCall4(CGM.getIntrinsic(llvm::Intrinsic::instrprof_increment),
804 llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
805 Builder.getInt64(FunctionHash),
806 Builder.getInt32(NumRegionCounters),
807 Builder.getInt32(Counter));
Justin Bogneref512b92014-01-06 22:27:43 +0000808}
809
Justin Bogner40b8ba12014-06-26 01:45:07 +0000810void CodeGenPGO::loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
811 bool IsInMainFile) {
812 CGM.getPGOStats().addVisited(IsInMainFile);
Justin Bogner7f8cf5b2014-12-02 22:38:52 +0000813 RegionCounts.clear();
Justin Bogner970ac602014-12-08 19:04:51 +0000814 if (std::error_code EC =
815 PGOReader->getFunctionCounts(FuncName, FunctionHash, RegionCounts)) {
Justin Bogner9c6818e2014-08-01 22:50:16 +0000816 if (EC == llvm::instrprof_error::unknown_function)
817 CGM.getPGOStats().addMissing(IsInMainFile);
818 else if (EC == llvm::instrprof_error::hash_mismatch)
819 CGM.getPGOStats().addMismatched(IsInMainFile);
820 else if (EC == llvm::instrprof_error::malformed)
821 // TODO: Consider a more specific warning for this case.
822 CGM.getPGOStats().addMismatched(IsInMainFile);
Justin Bogner7f8cf5b2014-12-02 22:38:52 +0000823 RegionCounts.clear();
Justin Bognere2ef2a02014-04-15 21:22:35 +0000824 }
Justin Bogneref512b92014-01-06 22:27:43 +0000825}
826
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000827/// \brief Calculate what to divide by to scale weights.
828///
829/// Given the maximum weight, calculate a divisor that will scale all the
830/// weights to strictly less than UINT32_MAX.
831static uint64_t calculateWeightScale(uint64_t MaxWeight) {
832 return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1;
833}
834
835/// \brief Scale an individual branch weight (and add 1).
836///
837/// Scale a 64-bit weight down to 32-bits using \c Scale.
838///
839/// According to Laplace's Rule of Succession, it is better to compute the
840/// weight based on the count plus 1, so universally add 1 to the value.
841///
842/// \pre \c Scale was calculated by \a calculateWeightScale() with a weight no
843/// greater than \c Weight.
844static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale) {
845 assert(Scale && "scale by 0?");
846 uint64_t Scaled = Weight / Scale + 1;
847 assert(Scaled <= UINT32_MAX && "overflow 32-bits");
848 return Scaled;
849}
850
Justin Bogneref512b92014-01-06 22:27:43 +0000851llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount,
852 uint64_t FalseCount) {
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000853 // Check for empty weights.
Justin Bogneref512b92014-01-06 22:27:43 +0000854 if (!TrueCount && !FalseCount)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000855 return nullptr;
Justin Bogneref512b92014-01-06 22:27:43 +0000856
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000857 // Calculate how to scale down to 32-bits.
858 uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount));
859
Justin Bogneref512b92014-01-06 22:27:43 +0000860 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000861 return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale),
862 scaleBranchWeight(FalseCount, Scale));
Justin Bogneref512b92014-01-06 22:27:43 +0000863}
864
Bob Wilson95a27b02014-02-17 19:20:59 +0000865llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) {
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000866 // We need at least two elements to create meaningful weights.
867 if (Weights.size() < 2)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000868 return nullptr;
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000869
Justin Bognerf3aefca2014-04-04 02:48:51 +0000870 // Check for empty weights.
871 uint64_t MaxWeight = *std::max_element(Weights.begin(), Weights.end());
872 if (MaxWeight == 0)
873 return nullptr;
874
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000875 // Calculate how to scale down to 32-bits.
Justin Bognerf3aefca2014-04-04 02:48:51 +0000876 uint64_t Scale = calculateWeightScale(MaxWeight);
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000877
Justin Bogneref512b92014-01-06 22:27:43 +0000878 SmallVector<uint32_t, 16> ScaledWeights;
879 ScaledWeights.reserve(Weights.size());
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000880 for (uint64_t W : Weights)
881 ScaledWeights.push_back(scaleBranchWeight(W, Scale));
882
883 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
Justin Bogneref512b92014-01-06 22:27:43 +0000884 return MDHelper.createBranchWeights(ScaledWeights);
885}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000886
887llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond,
Justin Bogner66242d62015-04-23 23:06:47 +0000888 uint64_t LoopCount) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000889 if (!haveRegionCounts())
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000890 return nullptr;
Justin Bogner1c21c282015-04-13 12:23:19 +0000891 Optional<uint64_t> CondCount = getStmtCount(Cond);
892 assert(CondCount.hasValue() && "missing expected loop condition count");
893 if (*CondCount == 0)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000894 return nullptr;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000895 return createBranchWeights(LoopCount,
Justin Bogner1c21c282015-04-13 12:23:19 +0000896 std::max(*CondCount, LoopCount) - LoopCount);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000897}