blob: 1d3ee4f8a6c8214f9049b06f1f8b2907aa9341b9 [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"
16#include "clang/AST/RecursiveASTVisitor.h"
17#include "clang/AST/StmtVisitor.h"
Justin Bogner529f6dd2014-01-07 03:43:15 +000018#include "llvm/Config/config.h" // for strtoull()/strtoll() define
Justin Bogneref512b92014-01-06 22:27:43 +000019#include "llvm/IR/MDBuilder.h"
20#include "llvm/Support/FileSystem.h"
21
22using namespace clang;
23using namespace CodeGen;
24
Justin Bognerd66a17d2014-03-12 21:06:31 +000025static void ReportBadPGOData(CodeGenModule &CGM, const char *Message) {
26 DiagnosticsEngine &Diags = CGM.getDiags();
27 unsigned diagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0");
28 Diags.Report(diagID) << Message;
29}
30
31PGOProfileData::PGOProfileData(CodeGenModule &CGM, std::string Path)
32 : CGM(CGM) {
33 if (llvm::MemoryBuffer::getFile(Path, DataBuffer)) {
34 ReportBadPGOData(CGM, "failed to open pgo data file");
35 return;
36 }
37
38 if (DataBuffer->getBufferSize() > std::numeric_limits<unsigned>::max()) {
39 ReportBadPGOData(CGM, "pgo data file too big");
40 return;
41 }
42
43 // Scan through the data file and map each function to the corresponding
44 // file offset where its counts are stored.
45 const char *BufferStart = DataBuffer->getBufferStart();
46 const char *BufferEnd = DataBuffer->getBufferEnd();
47 const char *CurPtr = BufferStart;
48 uint64_t MaxCount = 0;
49 while (CurPtr < BufferEnd) {
50 // Read the function name.
51 const char *FuncStart = CurPtr;
52 // For Objective-C methods, the name may include whitespace, so search
53 // backward from the end of the line to find the space that separates the
54 // name from the number of counters. (This is a temporary hack since we are
55 // going to completely replace this file format in the near future.)
56 CurPtr = strchr(CurPtr, '\n');
57 if (!CurPtr) {
58 ReportBadPGOData(CGM, "pgo data file has malformed function entry");
59 return;
60 }
61 while (*--CurPtr != ' ')
62 ;
63 StringRef FuncName(FuncStart, CurPtr - FuncStart);
64
65 // Read the number of counters.
66 char *EndPtr;
67 unsigned NumCounters = strtol(++CurPtr, &EndPtr, 10);
68 if (EndPtr == CurPtr || *EndPtr != '\n' || NumCounters <= 0) {
69 ReportBadPGOData(CGM, "pgo data file has unexpected number of counters");
70 return;
71 }
72 CurPtr = EndPtr;
73
74 // Read function count.
75 uint64_t Count = strtoll(CurPtr, &EndPtr, 10);
76 if (EndPtr == CurPtr || *EndPtr != '\n') {
77 ReportBadPGOData(CGM, "pgo-data file has bad count value");
78 return;
79 }
80 CurPtr = EndPtr; // Point to '\n'.
81 FunctionCounts[FuncName] = Count;
82 MaxCount = Count > MaxCount ? Count : MaxCount;
83
84 // There is one line for each counter; skip over those lines.
85 // Since function count is already read, we start the loop from 1.
86 for (unsigned N = 1; N < NumCounters; ++N) {
87 CurPtr = strchr(++CurPtr, '\n');
88 if (!CurPtr) {
89 ReportBadPGOData(CGM, "pgo data file is missing some counter info");
90 return;
91 }
92 }
93
94 // Skip over the blank line separating functions.
95 CurPtr += 2;
96
97 DataOffsets[FuncName] = FuncStart - BufferStart;
98 }
99 MaxFunctionCount = MaxCount;
100}
101
102bool PGOProfileData::getFunctionCounts(StringRef FuncName,
103 std::vector<uint64_t> &Counts) {
104 // Find the relevant section of the pgo-data file.
105 llvm::StringMap<unsigned>::const_iterator OffsetIter =
106 DataOffsets.find(FuncName);
107 if (OffsetIter == DataOffsets.end())
108 return true;
109 const char *CurPtr = DataBuffer->getBufferStart() + OffsetIter->getValue();
110
111 // Skip over the function name.
112 CurPtr = strchr(CurPtr, '\n');
113 assert(CurPtr && "pgo-data has corrupted function entry");
114 while (*--CurPtr != ' ')
115 ;
116
117 // Read the number of counters.
118 char *EndPtr;
119 unsigned NumCounters = strtol(++CurPtr, &EndPtr, 10);
120 assert(EndPtr != CurPtr && *EndPtr == '\n' && NumCounters > 0 &&
121 "pgo-data file has corrupted number of counters");
122 CurPtr = EndPtr;
123
124 Counts.reserve(NumCounters);
125
126 for (unsigned N = 0; N < NumCounters; ++N) {
127 // Read the count value.
128 uint64_t Count = strtoll(CurPtr, &EndPtr, 10);
129 if (EndPtr == CurPtr || *EndPtr != '\n') {
130 ReportBadPGOData(CGM, "pgo-data file has bad count value");
131 return true;
132 }
133 Counts.push_back(Count);
134 CurPtr = EndPtr + 1;
135 }
136
137 // Make sure the number of counters matches up.
138 if (Counts.size() != NumCounters) {
139 ReportBadPGOData(CGM, "pgo-data file has inconsistent counters");
140 return true;
141 }
142
143 return false;
144}
145
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000146void CodeGenPGO::setFuncName(llvm::Function *Fn) {
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000147 RawFuncName = Fn->getName();
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000148
149 // Function names may be prefixed with a binary '1' to indicate
150 // that the backend should not modify the symbols due to any platform
151 // naming convention. Do not include that '1' in the PGO profile name.
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000152 if (RawFuncName[0] == '\1')
153 RawFuncName = RawFuncName.substr(1);
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000154
155 if (!Fn->hasLocalLinkage()) {
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000156 PrefixedFuncName = new std::string(RawFuncName);
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000157 return;
158 }
159
160 // For local symbols, prepend the main file name to distinguish them.
161 // Do not include the full path in the file name since there's no guarantee
162 // that it will stay the same, e.g., if the files are checked out from
163 // version control in different locations.
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000164 PrefixedFuncName = new std::string(CGM.getCodeGenOpts().MainFileName);
165 if (PrefixedFuncName->empty())
166 PrefixedFuncName->assign("<unknown>");
167 PrefixedFuncName->append(":");
168 PrefixedFuncName->append(RawFuncName);
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000169}
170
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000171static llvm::Function *getRegisterFunc(CodeGenModule &CGM) {
172 return CGM.getModule().getFunction("__llvm_pgo_register_functions");
173}
174
175static llvm::BasicBlock *getOrInsertRegisterBB(CodeGenModule &CGM) {
176 // Only need to insert this once per module.
177 if (llvm::Function *RegisterF = getRegisterFunc(CGM))
178 return &RegisterF->getEntryBlock();
179
180 // Construct the function.
181 auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
182 auto *RegisterFTy = llvm::FunctionType::get(VoidTy, false);
183 auto *RegisterF = llvm::Function::Create(RegisterFTy,
184 llvm::GlobalValue::InternalLinkage,
185 "__llvm_pgo_register_functions",
186 &CGM.getModule());
187 RegisterF->setUnnamedAddr(true);
188 RegisterF->addFnAttr(llvm::Attribute::NoInline);
189 if (CGM.getCodeGenOpts().DisableRedZone)
190 RegisterF->addFnAttr(llvm::Attribute::NoRedZone);
191
192 // Construct and return the entry block.
193 auto *BB = llvm::BasicBlock::Create(CGM.getLLVMContext(), "", RegisterF);
194 CGBuilderTy Builder(BB);
195 Builder.CreateRetVoid();
196 return BB;
197}
198
199static llvm::Constant *getOrInsertRuntimeRegister(CodeGenModule &CGM) {
200 auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
201 auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
202 auto *RuntimeRegisterTy = llvm::FunctionType::get(VoidTy, VoidPtrTy, false);
203 return CGM.getModule().getOrInsertFunction("__llvm_pgo_register_function",
204 RuntimeRegisterTy);
205}
206
207static llvm::Constant *getOrInsertRuntimeWriteAtExit(CodeGenModule &CGM) {
208 // TODO: make this depend on a command-line option.
209 auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
210 auto *WriteAtExitTy = llvm::FunctionType::get(VoidTy, false);
211 return CGM.getModule().getOrInsertFunction("__llvm_pgo_register_write_atexit",
212 WriteAtExitTy);
213}
214
215static StringRef getCountersSection(const CodeGenModule &CGM) {
216 if (CGM.getTarget().getTriple().getObjectFormat() == llvm::Triple::MachO)
217 return "__DATA,__llvm_pgo_cnts";
218 else
219 return "__llvm_pgo_cnts";
220}
221
222static StringRef getNameSection(const CodeGenModule &CGM) {
223 if (CGM.getTarget().getTriple().getObjectFormat() == llvm::Triple::MachO)
224 return "__DATA,__llvm_pgo_names";
225 else
226 return "__llvm_pgo_names";
227}
228
229static StringRef getDataSection(const CodeGenModule &CGM) {
230 if (CGM.getTarget().getTriple().getObjectFormat() == llvm::Triple::MachO)
231 return "__DATA,__llvm_pgo_data";
232 else
233 return "__llvm_pgo_data";
234}
235
236llvm::GlobalVariable *CodeGenPGO::buildDataVar() {
237 // Create name variable.
238 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
239 auto *VarName = llvm::ConstantDataArray::getString(Ctx, getFuncName(),
240 false);
241 auto *Name = new llvm::GlobalVariable(CGM.getModule(), VarName->getType(),
242 true, FuncLinkage, VarName,
243 getFuncVarName("name"));
244 Name->setSection(getNameSection(CGM));
245 Name->setAlignment(1);
246
247 // Create data variable.
248 auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
249 auto *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx);
250 auto *Int64PtrTy = llvm::Type::getInt64PtrTy(Ctx);
251 llvm::Type *DataTypes[] = {
252 Int32Ty, Int32Ty, Int8PtrTy, Int64PtrTy
253 };
254 auto *DataTy = llvm::StructType::get(Ctx, makeArrayRef(DataTypes));
255 llvm::Constant *DataVals[] = {
256 llvm::ConstantInt::get(Int32Ty, getFuncName().size()),
257 llvm::ConstantInt::get(Int32Ty, NumRegionCounters),
258 llvm::ConstantExpr::getBitCast(Name, Int8PtrTy),
259 llvm::ConstantExpr::getBitCast(RegionCounters, Int64PtrTy)
260 };
261 auto *Data =
262 new llvm::GlobalVariable(CGM.getModule(), DataTy, true, FuncLinkage,
263 llvm::ConstantStruct::get(DataTy, DataVals),
264 getFuncVarName("data"));
265
266 // All the data should be packed into an array in its own section.
267 Data->setSection(getDataSection(CGM));
268 Data->setAlignment(8);
269
270 // Make sure the data doesn't get deleted.
271 CGM.addUsedGlobal(Data);
272 return Data;
273}
274
275void CodeGenPGO::emitInstrumentationData() {
Justin Bogneref512b92014-01-06 22:27:43 +0000276 if (!CGM.getCodeGenOpts().ProfileInstrGenerate)
277 return;
278
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000279 // Build the data.
280 auto *Data = buildDataVar();
Justin Bogneref512b92014-01-06 22:27:43 +0000281
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000282 // Register the data.
283 //
284 // TODO: only register when static initialization is required.
285 CGBuilderTy Builder(getOrInsertRegisterBB(CGM)->getTerminator());
286 auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
287 Builder.CreateCall(getOrInsertRuntimeRegister(CGM),
288 Builder.CreateBitCast(Data, VoidPtrTy));
Justin Bogneref512b92014-01-06 22:27:43 +0000289}
290
291llvm::Function *CodeGenPGO::emitInitialization(CodeGenModule &CGM) {
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000292 if (!CGM.getCodeGenOpts().ProfileInstrGenerate)
293 return 0;
Justin Bogneref512b92014-01-06 22:27:43 +0000294
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000295 // Only need to create this once per module.
296 if (CGM.getModule().getFunction("__llvm_pgo_init"))
297 return 0;
Justin Bogneref512b92014-01-06 22:27:43 +0000298
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000299 // Get the functions to call at initialization.
300 llvm::Constant *RegisterF = getRegisterFunc(CGM);
301 llvm::Constant *WriteAtExitF = getOrInsertRuntimeWriteAtExit(CGM);
302 if (!RegisterF && !WriteAtExitF)
303 return 0;
304
305 // Create the initialization function.
306 auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
307 auto *F = llvm::Function::Create(llvm::FunctionType::get(VoidTy, false),
308 llvm::GlobalValue::InternalLinkage,
309 "__llvm_pgo_init", &CGM.getModule());
Justin Bogneref512b92014-01-06 22:27:43 +0000310 F->setUnnamedAddr(true);
Justin Bogneref512b92014-01-06 22:27:43 +0000311 F->addFnAttr(llvm::Attribute::NoInline);
312 if (CGM.getCodeGenOpts().DisableRedZone)
313 F->addFnAttr(llvm::Attribute::NoRedZone);
314
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000315 // Add the basic block and the necessary calls.
316 CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", F));
317 if (RegisterF)
318 Builder.CreateCall(RegisterF);
319 if (WriteAtExitF)
320 Builder.CreateCall(WriteAtExitF);
321 Builder.CreateRetVoid();
Justin Bogneref512b92014-01-06 22:27:43 +0000322
323 return F;
324}
325
326namespace {
327 /// A StmtVisitor that fills a map of statements to PGO counters.
328 struct MapRegionCounters : public ConstStmtVisitor<MapRegionCounters> {
329 /// The next counter value to assign.
330 unsigned NextCounter;
331 /// The map of statements to counters.
332 llvm::DenseMap<const Stmt*, unsigned> *CounterMap;
333
334 MapRegionCounters(llvm::DenseMap<const Stmt*, unsigned> *CounterMap) :
335 NextCounter(0), CounterMap(CounterMap) {
336 }
337
338 void VisitChildren(const Stmt *S) {
339 for (Stmt::const_child_range I = S->children(); I; ++I)
340 if (*I)
341 this->Visit(*I);
342 }
343 void VisitStmt(const Stmt *S) { VisitChildren(S); }
344
Justin Bognerea278c32014-01-07 00:20:28 +0000345 /// Assign a counter to track entry to the function body.
Justin Bogneref512b92014-01-06 22:27:43 +0000346 void VisitFunctionDecl(const FunctionDecl *S) {
347 (*CounterMap)[S->getBody()] = NextCounter++;
348 Visit(S->getBody());
349 }
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000350 void VisitObjCMethodDecl(const ObjCMethodDecl *S) {
351 (*CounterMap)[S->getBody()] = NextCounter++;
352 Visit(S->getBody());
353 }
Bob Wilsonc845c002014-03-06 20:24:27 +0000354 void VisitBlockDecl(const BlockDecl *S) {
355 (*CounterMap)[S->getBody()] = NextCounter++;
356 Visit(S->getBody());
357 }
Justin Bognerea278c32014-01-07 00:20:28 +0000358 /// Assign a counter to track the block following a label.
Justin Bogneref512b92014-01-06 22:27:43 +0000359 void VisitLabelStmt(const LabelStmt *S) {
360 (*CounterMap)[S] = NextCounter++;
361 Visit(S->getSubStmt());
362 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000363 /// Assign a counter for the body of a while loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000364 void VisitWhileStmt(const WhileStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000365 (*CounterMap)[S] = NextCounter++;
Justin Bogneref512b92014-01-06 22:27:43 +0000366 Visit(S->getCond());
367 Visit(S->getBody());
368 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000369 /// Assign a counter for the body of a do-while loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000370 void VisitDoStmt(const DoStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000371 (*CounterMap)[S] = NextCounter++;
Justin Bogneref512b92014-01-06 22:27:43 +0000372 Visit(S->getBody());
373 Visit(S->getCond());
374 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000375 /// Assign a counter for the body of a for loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000376 void VisitForStmt(const ForStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000377 (*CounterMap)[S] = NextCounter++;
378 if (S->getInit())
379 Visit(S->getInit());
Justin Bogneref512b92014-01-06 22:27:43 +0000380 const Expr *E;
381 if ((E = S->getCond()))
382 Visit(E);
Justin Bogneref512b92014-01-06 22:27:43 +0000383 if ((E = S->getInc()))
384 Visit(E);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000385 Visit(S->getBody());
Justin Bogneref512b92014-01-06 22:27:43 +0000386 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000387 /// Assign a counter for the body of a for-range loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000388 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000389 (*CounterMap)[S] = NextCounter++;
390 Visit(S->getRangeStmt());
391 Visit(S->getBeginEndStmt());
392 Visit(S->getCond());
393 Visit(S->getLoopVarStmt());
Justin Bogneref512b92014-01-06 22:27:43 +0000394 Visit(S->getBody());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000395 Visit(S->getInc());
Justin Bogneref512b92014-01-06 22:27:43 +0000396 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000397 /// Assign a counter for the body of a for-collection loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000398 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000399 (*CounterMap)[S] = NextCounter++;
Justin Bogneref512b92014-01-06 22:27:43 +0000400 Visit(S->getElement());
401 Visit(S->getBody());
402 }
403 /// Assign a counter for the exit block of the switch statement.
404 void VisitSwitchStmt(const SwitchStmt *S) {
405 (*CounterMap)[S] = NextCounter++;
406 Visit(S->getCond());
407 Visit(S->getBody());
408 }
409 /// Assign a counter for a particular case in a switch. This counts jumps
410 /// from the switch header as well as fallthrough from the case before this
411 /// one.
412 void VisitCaseStmt(const CaseStmt *S) {
413 (*CounterMap)[S] = NextCounter++;
414 Visit(S->getSubStmt());
415 }
416 /// Assign a counter for the default case of a switch statement. The count
417 /// is the number of branches from the loop header to the default, and does
418 /// not include fallthrough from previous cases. If we have multiple
419 /// conditional branch blocks from the switch instruction to the default
420 /// block, as with large GNU case ranges, this is the counter for the last
421 /// edge in that series, rather than the first.
422 void VisitDefaultStmt(const DefaultStmt *S) {
423 (*CounterMap)[S] = NextCounter++;
424 Visit(S->getSubStmt());
425 }
426 /// Assign a counter for the "then" part of an if statement. The count for
427 /// the "else" part, if it exists, will be calculated from this counter.
428 void VisitIfStmt(const IfStmt *S) {
429 (*CounterMap)[S] = NextCounter++;
430 Visit(S->getCond());
431 Visit(S->getThen());
432 if (S->getElse())
433 Visit(S->getElse());
434 }
435 /// Assign a counter for the continuation block of a C++ try statement.
436 void VisitCXXTryStmt(const CXXTryStmt *S) {
437 (*CounterMap)[S] = NextCounter++;
438 Visit(S->getTryBlock());
439 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
440 Visit(S->getHandler(I));
441 }
442 /// Assign a counter for a catch statement's handler block.
443 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
444 (*CounterMap)[S] = NextCounter++;
445 Visit(S->getHandlerBlock());
446 }
447 /// Assign a counter for the "true" part of a conditional operator. The
448 /// count in the "false" part will be calculated from this counter.
449 void VisitConditionalOperator(const ConditionalOperator *E) {
450 (*CounterMap)[E] = NextCounter++;
451 Visit(E->getCond());
452 Visit(E->getTrueExpr());
453 Visit(E->getFalseExpr());
454 }
455 /// Assign a counter for the right hand side of a logical and operator.
456 void VisitBinLAnd(const BinaryOperator *E) {
457 (*CounterMap)[E] = NextCounter++;
458 Visit(E->getLHS());
459 Visit(E->getRHS());
460 }
461 /// Assign a counter for the right hand side of a logical or operator.
462 void VisitBinLOr(const BinaryOperator *E) {
463 (*CounterMap)[E] = NextCounter++;
464 Visit(E->getLHS());
465 Visit(E->getRHS());
466 }
467 };
Bob Wilsonbf854f02014-02-17 19:21:09 +0000468
469 /// A StmtVisitor that propagates the raw counts through the AST and
470 /// records the count at statements where the value may change.
471 struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> {
472 /// PGO state.
473 CodeGenPGO &PGO;
474
475 /// A flag that is set when the current count should be recorded on the
476 /// next statement, such as at the exit of a loop.
477 bool RecordNextStmtCount;
478
479 /// The map of statements to count values.
480 llvm::DenseMap<const Stmt*, uint64_t> *CountMap;
481
482 /// BreakContinueStack - Keep counts of breaks and continues inside loops.
483 struct BreakContinue {
484 uint64_t BreakCount;
485 uint64_t ContinueCount;
486 BreakContinue() : BreakCount(0), ContinueCount(0) {}
487 };
488 SmallVector<BreakContinue, 8> BreakContinueStack;
489
490 ComputeRegionCounts(llvm::DenseMap<const Stmt*, uint64_t> *CountMap,
491 CodeGenPGO &PGO) :
492 PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {
493 }
494
495 void RecordStmtCount(const Stmt *S) {
496 if (RecordNextStmtCount) {
497 (*CountMap)[S] = PGO.getCurrentRegionCount();
498 RecordNextStmtCount = false;
499 }
500 }
501
502 void VisitStmt(const Stmt *S) {
503 RecordStmtCount(S);
504 for (Stmt::const_child_range I = S->children(); I; ++I) {
505 if (*I)
506 this->Visit(*I);
507 }
508 }
509
510 void VisitFunctionDecl(const FunctionDecl *S) {
511 RegionCounter Cnt(PGO, S->getBody());
512 Cnt.beginRegion();
513 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
514 Visit(S->getBody());
515 }
516
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000517 void VisitObjCMethodDecl(const ObjCMethodDecl *S) {
518 RegionCounter Cnt(PGO, S->getBody());
519 Cnt.beginRegion();
520 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
521 Visit(S->getBody());
522 }
523
Bob Wilsonc845c002014-03-06 20:24:27 +0000524 void VisitBlockDecl(const BlockDecl *S) {
525 RegionCounter Cnt(PGO, S->getBody());
526 Cnt.beginRegion();
527 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
528 Visit(S->getBody());
529 }
530
Bob Wilsonbf854f02014-02-17 19:21:09 +0000531 void VisitReturnStmt(const ReturnStmt *S) {
532 RecordStmtCount(S);
533 if (S->getRetValue())
534 Visit(S->getRetValue());
535 PGO.setCurrentRegionUnreachable();
536 RecordNextStmtCount = true;
537 }
538
539 void VisitGotoStmt(const GotoStmt *S) {
540 RecordStmtCount(S);
541 PGO.setCurrentRegionUnreachable();
542 RecordNextStmtCount = true;
543 }
544
545 void VisitLabelStmt(const LabelStmt *S) {
546 RecordNextStmtCount = false;
547 RegionCounter Cnt(PGO, S);
548 Cnt.beginRegion();
549 (*CountMap)[S] = PGO.getCurrentRegionCount();
550 Visit(S->getSubStmt());
551 }
552
553 void VisitBreakStmt(const BreakStmt *S) {
554 RecordStmtCount(S);
555 assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
556 BreakContinueStack.back().BreakCount += PGO.getCurrentRegionCount();
557 PGO.setCurrentRegionUnreachable();
558 RecordNextStmtCount = true;
559 }
560
561 void VisitContinueStmt(const ContinueStmt *S) {
562 RecordStmtCount(S);
563 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
564 BreakContinueStack.back().ContinueCount += PGO.getCurrentRegionCount();
565 PGO.setCurrentRegionUnreachable();
566 RecordNextStmtCount = true;
567 }
568
569 void VisitWhileStmt(const WhileStmt *S) {
570 RecordStmtCount(S);
571 RegionCounter Cnt(PGO, S);
572 BreakContinueStack.push_back(BreakContinue());
573 // Visit the body region first so the break/continue adjustments can be
574 // included when visiting the condition.
575 Cnt.beginRegion();
576 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
577 Visit(S->getBody());
578 Cnt.adjustForControlFlow();
579
580 // ...then go back and propagate counts through the condition. The count
581 // at the start of the condition is the sum of the incoming edges,
582 // the backedge from the end of the loop body, and the edges from
583 // continue statements.
584 BreakContinue BC = BreakContinueStack.pop_back_val();
585 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
586 Cnt.getAdjustedCount() + BC.ContinueCount);
587 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
588 Visit(S->getCond());
589 Cnt.adjustForControlFlow();
590 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
591 RecordNextStmtCount = true;
592 }
593
594 void VisitDoStmt(const DoStmt *S) {
595 RecordStmtCount(S);
596 RegionCounter Cnt(PGO, S);
597 BreakContinueStack.push_back(BreakContinue());
598 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
599 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
600 Visit(S->getBody());
601 Cnt.adjustForControlFlow();
602
603 BreakContinue BC = BreakContinueStack.pop_back_val();
604 // The count at the start of the condition is equal to the count at the
605 // end of the body. The adjusted count does not include either the
606 // fall-through count coming into the loop or the continue count, so add
607 // both of those separately. This is coincidentally the same equation as
608 // with while loops but for different reasons.
609 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
610 Cnt.getAdjustedCount() + BC.ContinueCount);
611 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
612 Visit(S->getCond());
613 Cnt.adjustForControlFlow();
614 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
615 RecordNextStmtCount = true;
616 }
617
618 void VisitForStmt(const ForStmt *S) {
619 RecordStmtCount(S);
620 if (S->getInit())
621 Visit(S->getInit());
622 RegionCounter Cnt(PGO, S);
623 BreakContinueStack.push_back(BreakContinue());
624 // Visit the body region first. (This is basically the same as a while
625 // loop; see further comments in VisitWhileStmt.)
626 Cnt.beginRegion();
627 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
628 Visit(S->getBody());
629 Cnt.adjustForControlFlow();
630
631 // The increment is essentially part of the body but it needs to include
632 // the count for all the continue statements.
633 if (S->getInc()) {
634 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
635 BreakContinueStack.back().ContinueCount);
636 (*CountMap)[S->getInc()] = PGO.getCurrentRegionCount();
637 Visit(S->getInc());
638 Cnt.adjustForControlFlow();
639 }
640
641 BreakContinue BC = BreakContinueStack.pop_back_val();
642
643 // ...then go back and propagate counts through the condition.
644 if (S->getCond()) {
645 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
646 Cnt.getAdjustedCount() +
647 BC.ContinueCount);
648 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
649 Visit(S->getCond());
650 Cnt.adjustForControlFlow();
651 }
652 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
653 RecordNextStmtCount = true;
654 }
655
656 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
657 RecordStmtCount(S);
658 Visit(S->getRangeStmt());
659 Visit(S->getBeginEndStmt());
660 RegionCounter Cnt(PGO, S);
661 BreakContinueStack.push_back(BreakContinue());
662 // Visit the body region first. (This is basically the same as a while
663 // loop; see further comments in VisitWhileStmt.)
664 Cnt.beginRegion();
665 (*CountMap)[S->getLoopVarStmt()] = PGO.getCurrentRegionCount();
666 Visit(S->getLoopVarStmt());
667 Visit(S->getBody());
668 Cnt.adjustForControlFlow();
669
670 // The increment is essentially part of the body but it needs to include
671 // the count for all the continue statements.
672 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
673 BreakContinueStack.back().ContinueCount);
674 (*CountMap)[S->getInc()] = PGO.getCurrentRegionCount();
675 Visit(S->getInc());
676 Cnt.adjustForControlFlow();
677
678 BreakContinue BC = BreakContinueStack.pop_back_val();
679
680 // ...then go back and propagate counts through the condition.
681 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
682 Cnt.getAdjustedCount() +
683 BC.ContinueCount);
684 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
685 Visit(S->getCond());
686 Cnt.adjustForControlFlow();
687 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
688 RecordNextStmtCount = true;
689 }
690
691 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
692 RecordStmtCount(S);
693 Visit(S->getElement());
694 RegionCounter Cnt(PGO, S);
695 BreakContinueStack.push_back(BreakContinue());
696 Cnt.beginRegion();
697 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
698 Visit(S->getBody());
699 BreakContinue BC = BreakContinueStack.pop_back_val();
700 Cnt.adjustForControlFlow();
701 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
702 RecordNextStmtCount = true;
703 }
704
705 void VisitSwitchStmt(const SwitchStmt *S) {
706 RecordStmtCount(S);
707 Visit(S->getCond());
708 PGO.setCurrentRegionUnreachable();
709 BreakContinueStack.push_back(BreakContinue());
710 Visit(S->getBody());
711 // If the switch is inside a loop, add the continue counts.
712 BreakContinue BC = BreakContinueStack.pop_back_val();
713 if (!BreakContinueStack.empty())
714 BreakContinueStack.back().ContinueCount += BC.ContinueCount;
715 RegionCounter ExitCnt(PGO, S);
716 ExitCnt.beginRegion();
717 RecordNextStmtCount = true;
718 }
719
720 void VisitCaseStmt(const CaseStmt *S) {
721 RecordNextStmtCount = false;
722 RegionCounter Cnt(PGO, S);
723 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
724 (*CountMap)[S] = Cnt.getCount();
725 RecordNextStmtCount = true;
726 Visit(S->getSubStmt());
727 }
728
729 void VisitDefaultStmt(const DefaultStmt *S) {
730 RecordNextStmtCount = false;
731 RegionCounter Cnt(PGO, S);
732 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
733 (*CountMap)[S] = Cnt.getCount();
734 RecordNextStmtCount = true;
735 Visit(S->getSubStmt());
736 }
737
738 void VisitIfStmt(const IfStmt *S) {
739 RecordStmtCount(S);
740 RegionCounter Cnt(PGO, S);
741 Visit(S->getCond());
742
743 Cnt.beginRegion();
744 (*CountMap)[S->getThen()] = PGO.getCurrentRegionCount();
745 Visit(S->getThen());
746 Cnt.adjustForControlFlow();
747
748 if (S->getElse()) {
749 Cnt.beginElseRegion();
750 (*CountMap)[S->getElse()] = PGO.getCurrentRegionCount();
751 Visit(S->getElse());
752 Cnt.adjustForControlFlow();
753 }
754 Cnt.applyAdjustmentsToRegion(0);
755 RecordNextStmtCount = true;
756 }
757
758 void VisitCXXTryStmt(const CXXTryStmt *S) {
759 RecordStmtCount(S);
760 Visit(S->getTryBlock());
761 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
762 Visit(S->getHandler(I));
763 RegionCounter Cnt(PGO, S);
764 Cnt.beginRegion();
765 RecordNextStmtCount = true;
766 }
767
768 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
769 RecordNextStmtCount = false;
770 RegionCounter Cnt(PGO, S);
771 Cnt.beginRegion();
772 (*CountMap)[S] = PGO.getCurrentRegionCount();
773 Visit(S->getHandlerBlock());
774 }
775
776 void VisitConditionalOperator(const ConditionalOperator *E) {
777 RecordStmtCount(E);
778 RegionCounter Cnt(PGO, E);
779 Visit(E->getCond());
780
781 Cnt.beginRegion();
782 (*CountMap)[E->getTrueExpr()] = PGO.getCurrentRegionCount();
783 Visit(E->getTrueExpr());
784 Cnt.adjustForControlFlow();
785
786 Cnt.beginElseRegion();
787 (*CountMap)[E->getFalseExpr()] = PGO.getCurrentRegionCount();
788 Visit(E->getFalseExpr());
789 Cnt.adjustForControlFlow();
790
791 Cnt.applyAdjustmentsToRegion(0);
792 RecordNextStmtCount = true;
793 }
794
795 void VisitBinLAnd(const BinaryOperator *E) {
796 RecordStmtCount(E);
797 RegionCounter Cnt(PGO, E);
798 Visit(E->getLHS());
799 Cnt.beginRegion();
800 (*CountMap)[E->getRHS()] = PGO.getCurrentRegionCount();
801 Visit(E->getRHS());
802 Cnt.adjustForControlFlow();
803 Cnt.applyAdjustmentsToRegion(0);
804 RecordNextStmtCount = true;
805 }
806
807 void VisitBinLOr(const BinaryOperator *E) {
808 RecordStmtCount(E);
809 RegionCounter Cnt(PGO, E);
810 Visit(E->getLHS());
811 Cnt.beginRegion();
812 (*CountMap)[E->getRHS()] = PGO.getCurrentRegionCount();
813 Visit(E->getRHS());
814 Cnt.adjustForControlFlow();
815 Cnt.applyAdjustmentsToRegion(0);
816 RecordNextStmtCount = true;
817 }
818 };
Justin Bogneref512b92014-01-06 22:27:43 +0000819}
820
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000821void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) {
Justin Bogneref512b92014-01-06 22:27:43 +0000822 bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate;
Justin Bognerd66a17d2014-03-12 21:06:31 +0000823 PGOProfileData *PGOData = CGM.getPGOData();
824 if (!InstrumentRegions && !PGOData)
Justin Bogneref512b92014-01-06 22:27:43 +0000825 return;
Justin Bogneref512b92014-01-06 22:27:43 +0000826 if (!D)
827 return;
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000828 setFuncName(Fn);
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000829 FuncLinkage = Fn->getLinkage();
Justin Bogneref512b92014-01-06 22:27:43 +0000830 mapRegionCounters(D);
831 if (InstrumentRegions)
832 emitCounterVariables();
Justin Bognerd66a17d2014-03-12 21:06:31 +0000833 if (PGOData) {
834 loadRegionCounts(PGOData);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000835 computeRegionCounts(D);
Justin Bognerd66a17d2014-03-12 21:06:31 +0000836 applyFunctionAttributes(PGOData, Fn);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000837 }
Justin Bogneref512b92014-01-06 22:27:43 +0000838}
839
840void CodeGenPGO::mapRegionCounters(const Decl *D) {
841 RegionCounterMap = new llvm::DenseMap<const Stmt*, unsigned>();
842 MapRegionCounters Walker(RegionCounterMap);
843 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
844 Walker.VisitFunctionDecl(FD);
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000845 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
846 Walker.VisitObjCMethodDecl(MD);
Bob Wilsonc845c002014-03-06 20:24:27 +0000847 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
848 Walker.VisitBlockDecl(BD);
Justin Bogneref512b92014-01-06 22:27:43 +0000849 NumRegionCounters = Walker.NextCounter;
850}
851
Bob Wilsonbf854f02014-02-17 19:21:09 +0000852void CodeGenPGO::computeRegionCounts(const Decl *D) {
853 StmtCountMap = new llvm::DenseMap<const Stmt*, uint64_t>();
854 ComputeRegionCounts Walker(StmtCountMap, *this);
855 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
856 Walker.VisitFunctionDecl(FD);
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000857 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
858 Walker.VisitObjCMethodDecl(MD);
Bob Wilsonc845c002014-03-06 20:24:27 +0000859 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
860 Walker.VisitBlockDecl(BD);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000861}
862
Justin Bognerd66a17d2014-03-12 21:06:31 +0000863void CodeGenPGO::applyFunctionAttributes(PGOProfileData *PGOData,
Justin Bogner4c9c45c2014-03-12 18:14:32 +0000864 llvm::Function *Fn) {
865 if (!haveRegionCounts())
866 return;
867
Justin Bognerd66a17d2014-03-12 21:06:31 +0000868 uint64_t MaxFunctionCount = PGOData->getMaximumFunctionCount();
Justin Bogner4c9c45c2014-03-12 18:14:32 +0000869 uint64_t FunctionCount = getRegionCount(0);
870 if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount))
871 // Turn on InlineHint attribute for hot functions.
872 // FIXME: 30% is from preliminary tuning on SPEC, it may not be optimal.
873 Fn->addFnAttr(llvm::Attribute::InlineHint);
874 else if (FunctionCount <= (uint64_t)(0.01 * (double)MaxFunctionCount))
875 // Turn on Cold attribute for cold functions.
876 // FIXME: 1% is from preliminary tuning on SPEC, it may not be optimal.
877 Fn->addFnAttr(llvm::Attribute::Cold);
878}
879
Justin Bogneref512b92014-01-06 22:27:43 +0000880void CodeGenPGO::emitCounterVariables() {
881 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
882 llvm::ArrayType *CounterTy = llvm::ArrayType::get(llvm::Type::getInt64Ty(Ctx),
883 NumRegionCounters);
884 RegionCounters =
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000885 new llvm::GlobalVariable(CGM.getModule(), CounterTy, false, FuncLinkage,
Justin Bogneref512b92014-01-06 22:27:43 +0000886 llvm::Constant::getNullValue(CounterTy),
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000887 getFuncVarName("counters"));
888 RegionCounters->setAlignment(8);
889 RegionCounters->setSection(getCountersSection(CGM));
Justin Bogneref512b92014-01-06 22:27:43 +0000890}
891
892void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) {
Bob Wilson749ebc72014-03-06 04:55:28 +0000893 if (!RegionCounters)
Justin Bogneref512b92014-01-06 22:27:43 +0000894 return;
895 llvm::Value *Addr =
896 Builder.CreateConstInBoundsGEP2_64(RegionCounters, 0, Counter);
897 llvm::Value *Count = Builder.CreateLoad(Addr, "pgocount");
898 Count = Builder.CreateAdd(Count, Builder.getInt64(1));
899 Builder.CreateStore(Count, Addr);
900}
901
Justin Bognerd66a17d2014-03-12 21:06:31 +0000902void CodeGenPGO::loadRegionCounts(PGOProfileData *PGOData) {
Justin Bogneref512b92014-01-06 22:27:43 +0000903 // For now, ignore the counts from the PGO data file only if the number of
904 // counters does not match. This could be tightened down in the future to
905 // ignore counts when the input changes in various ways, e.g., by comparing a
906 // hash value based on some characteristics of the input.
907 RegionCounts = new std::vector<uint64_t>();
Justin Bognerd66a17d2014-03-12 21:06:31 +0000908 if (PGOData->getFunctionCounts(getFuncName(), *RegionCounts) ||
Justin Bogneref512b92014-01-06 22:27:43 +0000909 RegionCounts->size() != NumRegionCounters) {
910 delete RegionCounts;
911 RegionCounts = 0;
912 }
913}
914
915void CodeGenPGO::destroyRegionCounters() {
916 if (RegionCounterMap != 0)
917 delete RegionCounterMap;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000918 if (StmtCountMap != 0)
919 delete StmtCountMap;
Justin Bogneref512b92014-01-06 22:27:43 +0000920 if (RegionCounts != 0)
921 delete RegionCounts;
922}
923
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000924/// \brief Calculate what to divide by to scale weights.
925///
926/// Given the maximum weight, calculate a divisor that will scale all the
927/// weights to strictly less than UINT32_MAX.
928static uint64_t calculateWeightScale(uint64_t MaxWeight) {
929 return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1;
930}
931
932/// \brief Scale an individual branch weight (and add 1).
933///
934/// Scale a 64-bit weight down to 32-bits using \c Scale.
935///
936/// According to Laplace's Rule of Succession, it is better to compute the
937/// weight based on the count plus 1, so universally add 1 to the value.
938///
939/// \pre \c Scale was calculated by \a calculateWeightScale() with a weight no
940/// greater than \c Weight.
941static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale) {
942 assert(Scale && "scale by 0?");
943 uint64_t Scaled = Weight / Scale + 1;
944 assert(Scaled <= UINT32_MAX && "overflow 32-bits");
945 return Scaled;
946}
947
Justin Bogneref512b92014-01-06 22:27:43 +0000948llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount,
949 uint64_t FalseCount) {
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000950 // Check for empty weights.
Justin Bogneref512b92014-01-06 22:27:43 +0000951 if (!TrueCount && !FalseCount)
952 return 0;
953
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000954 // Calculate how to scale down to 32-bits.
955 uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount));
956
Justin Bogneref512b92014-01-06 22:27:43 +0000957 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000958 return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale),
959 scaleBranchWeight(FalseCount, Scale));
Justin Bogneref512b92014-01-06 22:27:43 +0000960}
961
Bob Wilson95a27b02014-02-17 19:20:59 +0000962llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) {
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000963 // We need at least two elements to create meaningful weights.
964 if (Weights.size() < 2)
965 return 0;
966
967 // Calculate how to scale down to 32-bits.
968 uint64_t Scale = calculateWeightScale(*std::max_element(Weights.begin(),
969 Weights.end()));
970
Justin Bogneref512b92014-01-06 22:27:43 +0000971 SmallVector<uint32_t, 16> ScaledWeights;
972 ScaledWeights.reserve(Weights.size());
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000973 for (uint64_t W : Weights)
974 ScaledWeights.push_back(scaleBranchWeight(W, Scale));
975
976 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
Justin Bogneref512b92014-01-06 22:27:43 +0000977 return MDHelper.createBranchWeights(ScaledWeights);
978}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000979
980llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond,
981 RegionCounter &Cnt) {
982 if (!haveRegionCounts())
983 return 0;
984 uint64_t LoopCount = Cnt.getCount();
985 uint64_t CondCount = 0;
986 bool Found = getStmtCount(Cond, CondCount);
987 assert(Found && "missing expected loop condition count");
988 (void)Found;
989 if (CondCount == 0)
990 return 0;
991 return createBranchWeights(LoopCount,
992 std::max(CondCount, LoopCount) - LoopCount);
993}