blob: 6678c37929b238e3adfae5cff724d5fc84ab4352 [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
25static void ReportBadPGOData(CodeGenModule &CGM, const char *Message) {
26 DiagnosticsEngine &Diags = CGM.getDiags();
Alp Toker29cb66b2014-01-26 06:17:37 +000027 unsigned diagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0");
28 Diags.Report(diagID) << Message;
Justin Bogneref512b92014-01-06 22:27:43 +000029}
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;
Manman Ren67a28132014-02-05 20:40:15 +000048 uint64_t MaxCount = 0;
Justin Bogneref512b92014-01-06 22:27:43 +000049 while (CurPtr < BufferEnd) {
Bob Wilsond0b78242014-03-06 04:55:37 +000050 // Read the function name.
51 const char *FuncStart = CurPtr;
Justin Bogneref512b92014-01-06 22:27:43 +000052 CurPtr = strchr(CurPtr, ' ');
53 if (!CurPtr) {
54 ReportBadPGOData(CGM, "pgo data file has malformed function entry");
55 return;
56 }
Bob Wilsond0b78242014-03-06 04:55:37 +000057 StringRef FuncName(FuncStart, CurPtr - FuncStart);
Justin Bogneref512b92014-01-06 22:27:43 +000058
59 // Read the number of counters.
60 char *EndPtr;
61 unsigned NumCounters = strtol(++CurPtr, &EndPtr, 10);
62 if (EndPtr == CurPtr || *EndPtr != '\n' || NumCounters <= 0) {
63 ReportBadPGOData(CGM, "pgo data file has unexpected number of counters");
64 return;
65 }
66 CurPtr = EndPtr;
67
Manman Ren67a28132014-02-05 20:40:15 +000068 // Read function count.
69 uint64_t Count = strtoll(CurPtr, &EndPtr, 10);
70 if (EndPtr == CurPtr || *EndPtr != '\n') {
71 ReportBadPGOData(CGM, "pgo-data file has bad count value");
72 return;
73 }
Manman Renf1a6a2d2014-02-15 01:29:02 +000074 CurPtr = EndPtr; // Point to '\n'.
Bob Wilsond0b78242014-03-06 04:55:37 +000075 FunctionCounts[FuncName] = Count;
Manman Ren67a28132014-02-05 20:40:15 +000076 MaxCount = Count > MaxCount ? Count : MaxCount;
77
Justin Bogneref512b92014-01-06 22:27:43 +000078 // There is one line for each counter; skip over those lines.
Manman Ren67a28132014-02-05 20:40:15 +000079 // Since function count is already read, we start the loop from 1.
80 for (unsigned N = 1; N < NumCounters; ++N) {
Justin Bogneref512b92014-01-06 22:27:43 +000081 CurPtr = strchr(++CurPtr, '\n');
82 if (!CurPtr) {
83 ReportBadPGOData(CGM, "pgo data file is missing some counter info");
84 return;
85 }
86 }
87
88 // Skip over the blank line separating functions.
89 CurPtr += 2;
90
Bob Wilsond0b78242014-03-06 04:55:37 +000091 DataOffsets[FuncName] = FuncStart - BufferStart;
Justin Bogneref512b92014-01-06 22:27:43 +000092 }
Manman Ren67a28132014-02-05 20:40:15 +000093 MaxFunctionCount = MaxCount;
94}
95
96/// Return true if a function is hot. If we know nothing about the function,
97/// return false.
Bob Wilsond0b78242014-03-06 04:55:37 +000098bool PGOProfileData::isHotFunction(StringRef FuncName) {
Manman Ren67a28132014-02-05 20:40:15 +000099 llvm::StringMap<uint64_t>::const_iterator CountIter =
Bob Wilsond0b78242014-03-06 04:55:37 +0000100 FunctionCounts.find(FuncName);
Manman Ren67a28132014-02-05 20:40:15 +0000101 // If we know nothing about the function, return false.
102 if (CountIter == FunctionCounts.end())
103 return false;
104 // FIXME: functions with >= 30% of the maximal function count are
105 // treated as hot. This number is from preliminary tuning on SPEC.
106 return CountIter->getValue() >= (uint64_t)(0.3 * (double)MaxFunctionCount);
107}
108
109/// Return true if a function is cold. If we know nothing about the function,
110/// return false.
Bob Wilsond0b78242014-03-06 04:55:37 +0000111bool PGOProfileData::isColdFunction(StringRef FuncName) {
Manman Ren67a28132014-02-05 20:40:15 +0000112 llvm::StringMap<uint64_t>::const_iterator CountIter =
Bob Wilsond0b78242014-03-06 04:55:37 +0000113 FunctionCounts.find(FuncName);
Manman Ren67a28132014-02-05 20:40:15 +0000114 // If we know nothing about the function, return false.
115 if (CountIter == FunctionCounts.end())
116 return false;
117 // FIXME: functions with <= 1% of the maximal function count are treated as
118 // cold. This number is from preliminary tuning on SPEC.
119 return CountIter->getValue() <= (uint64_t)(0.01 * (double)MaxFunctionCount);
Justin Bogneref512b92014-01-06 22:27:43 +0000120}
121
Bob Wilsond0b78242014-03-06 04:55:37 +0000122bool PGOProfileData::getFunctionCounts(StringRef FuncName,
Justin Bogneref512b92014-01-06 22:27:43 +0000123 std::vector<uint64_t> &Counts) {
124 // Find the relevant section of the pgo-data file.
125 llvm::StringMap<unsigned>::const_iterator OffsetIter =
Bob Wilsond0b78242014-03-06 04:55:37 +0000126 DataOffsets.find(FuncName);
Justin Bogneref512b92014-01-06 22:27:43 +0000127 if (OffsetIter == DataOffsets.end())
128 return true;
129 const char *CurPtr = DataBuffer->getBufferStart() + OffsetIter->getValue();
130
131 // Skip over the function name.
132 CurPtr = strchr(CurPtr, ' ');
133 assert(CurPtr && "pgo-data has corrupted function entry");
134
135 // Read the number of counters.
136 char *EndPtr;
137 unsigned NumCounters = strtol(++CurPtr, &EndPtr, 10);
138 assert(EndPtr != CurPtr && *EndPtr == '\n' && NumCounters > 0 &&
139 "pgo-data file has corrupted number of counters");
140 CurPtr = EndPtr;
141
142 Counts.reserve(NumCounters);
143
144 for (unsigned N = 0; N < NumCounters; ++N) {
145 // Read the count value.
146 uint64_t Count = strtoll(CurPtr, &EndPtr, 10);
147 if (EndPtr == CurPtr || *EndPtr != '\n') {
148 ReportBadPGOData(CGM, "pgo-data file has bad count value");
149 return true;
150 }
151 Counts.push_back(Count);
152 CurPtr = EndPtr + 1;
153 }
154
155 // Make sure the number of counters matches up.
156 if (Counts.size() != NumCounters) {
157 ReportBadPGOData(CGM, "pgo-data file has inconsistent counters");
158 return true;
159 }
160
161 return false;
162}
163
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000164void CodeGenPGO::setFuncName(llvm::Function *Fn) {
165 StringRef Func = Fn->getName();
166
167 // Function names may be prefixed with a binary '1' to indicate
168 // that the backend should not modify the symbols due to any platform
169 // naming convention. Do not include that '1' in the PGO profile name.
170 if (Func[0] == '\1')
171 Func = Func.substr(1);
172
173 if (!Fn->hasLocalLinkage()) {
174 FuncName = new std::string(Func);
175 return;
176 }
177
178 // For local symbols, prepend the main file name to distinguish them.
179 // Do not include the full path in the file name since there's no guarantee
180 // that it will stay the same, e.g., if the files are checked out from
181 // version control in different locations.
182 FuncName = new std::string(CGM.getCodeGenOpts().MainFileName);
183 if (FuncName->empty())
184 FuncName->assign("<unknown>");
185 FuncName->append(":");
186 FuncName->append(Func);
187}
188
189void CodeGenPGO::emitWriteoutFunction() {
Justin Bogneref512b92014-01-06 22:27:43 +0000190 if (!CGM.getCodeGenOpts().ProfileInstrGenerate)
191 return;
192
193 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
194
195 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx);
196 llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx);
197
198 llvm::Function *WriteoutF =
199 CGM.getModule().getFunction("__llvm_pgo_writeout");
200 if (!WriteoutF) {
201 llvm::FunctionType *WriteoutFTy =
202 llvm::FunctionType::get(llvm::Type::getVoidTy(Ctx), false);
203 WriteoutF = llvm::Function::Create(WriteoutFTy,
204 llvm::GlobalValue::InternalLinkage,
205 "__llvm_pgo_writeout", &CGM.getModule());
206 }
207 WriteoutF->setUnnamedAddr(true);
208 WriteoutF->addFnAttr(llvm::Attribute::NoInline);
209 if (CGM.getCodeGenOpts().DisableRedZone)
210 WriteoutF->addFnAttr(llvm::Attribute::NoRedZone);
211
212 llvm::BasicBlock *BB = WriteoutF->empty() ?
213 llvm::BasicBlock::Create(Ctx, "", WriteoutF) : &WriteoutF->getEntryBlock();
214
215 CGBuilderTy PGOBuilder(BB);
216
217 llvm::Instruction *I = BB->getTerminator();
218 if (!I)
219 I = PGOBuilder.CreateRetVoid();
220 PGOBuilder.SetInsertPoint(I);
221
222 llvm::Type *Int64PtrTy = llvm::Type::getInt64PtrTy(Ctx);
223 llvm::Type *Args[] = {
Bob Wilsond0b78242014-03-06 04:55:37 +0000224 Int8PtrTy, // const char *FuncName
Justin Bogneref512b92014-01-06 22:27:43 +0000225 Int32Ty, // uint32_t NumCounters
226 Int64PtrTy // uint64_t *Counters
227 };
228 llvm::FunctionType *FTy =
229 llvm::FunctionType::get(PGOBuilder.getVoidTy(), Args, false);
230 llvm::Constant *EmitFunc =
231 CGM.getModule().getOrInsertFunction("llvm_pgo_emit", FTy);
232
Bob Wilsond0b78242014-03-06 04:55:37 +0000233 llvm::Constant *NameString =
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000234 CGM.GetAddrOfConstantCString(getFuncName(), "__llvm_pgo_name");
Bob Wilsond0b78242014-03-06 04:55:37 +0000235 NameString = llvm::ConstantExpr::getBitCast(NameString, Int8PtrTy);
236 PGOBuilder.CreateCall3(EmitFunc, NameString,
Justin Bogneref512b92014-01-06 22:27:43 +0000237 PGOBuilder.getInt32(NumRegionCounters),
238 PGOBuilder.CreateBitCast(RegionCounters, Int64PtrTy));
239}
240
241llvm::Function *CodeGenPGO::emitInitialization(CodeGenModule &CGM) {
242 llvm::Function *WriteoutF =
243 CGM.getModule().getFunction("__llvm_pgo_writeout");
244 if (!WriteoutF)
245 return NULL;
246
247 // Create a small bit of code that registers the "__llvm_pgo_writeout" to
248 // be executed at exit.
249 llvm::Function *F = CGM.getModule().getFunction("__llvm_pgo_init");
250 if (F)
251 return NULL;
252
253 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
254 llvm::FunctionType *FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(Ctx),
255 false);
256 F = llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
257 "__llvm_pgo_init", &CGM.getModule());
258 F->setUnnamedAddr(true);
259 F->setLinkage(llvm::GlobalValue::InternalLinkage);
260 F->addFnAttr(llvm::Attribute::NoInline);
261 if (CGM.getCodeGenOpts().DisableRedZone)
262 F->addFnAttr(llvm::Attribute::NoRedZone);
263
264 llvm::BasicBlock *BB = llvm::BasicBlock::Create(CGM.getLLVMContext(), "", F);
265 CGBuilderTy PGOBuilder(BB);
266
267 FTy = llvm::FunctionType::get(PGOBuilder.getVoidTy(), false);
268 llvm::Type *Params[] = {
269 llvm::PointerType::get(FTy, 0)
270 };
271 FTy = llvm::FunctionType::get(PGOBuilder.getVoidTy(), Params, false);
272
273 // Inialize the environment and register the local writeout function.
274 llvm::Constant *PGOInit =
275 CGM.getModule().getOrInsertFunction("llvm_pgo_init", FTy);
276 PGOBuilder.CreateCall(PGOInit, WriteoutF);
277 PGOBuilder.CreateRetVoid();
278
279 return F;
280}
281
282namespace {
283 /// A StmtVisitor that fills a map of statements to PGO counters.
284 struct MapRegionCounters : public ConstStmtVisitor<MapRegionCounters> {
285 /// The next counter value to assign.
286 unsigned NextCounter;
287 /// The map of statements to counters.
288 llvm::DenseMap<const Stmt*, unsigned> *CounterMap;
289
290 MapRegionCounters(llvm::DenseMap<const Stmt*, unsigned> *CounterMap) :
291 NextCounter(0), CounterMap(CounterMap) {
292 }
293
294 void VisitChildren(const Stmt *S) {
295 for (Stmt::const_child_range I = S->children(); I; ++I)
296 if (*I)
297 this->Visit(*I);
298 }
299 void VisitStmt(const Stmt *S) { VisitChildren(S); }
300
Justin Bognerea278c32014-01-07 00:20:28 +0000301 /// Assign a counter to track entry to the function body.
Justin Bogneref512b92014-01-06 22:27:43 +0000302 void VisitFunctionDecl(const FunctionDecl *S) {
303 (*CounterMap)[S->getBody()] = NextCounter++;
304 Visit(S->getBody());
305 }
Justin Bognerea278c32014-01-07 00:20:28 +0000306 /// Assign a counter to track the block following a label.
Justin Bogneref512b92014-01-06 22:27:43 +0000307 void VisitLabelStmt(const LabelStmt *S) {
308 (*CounterMap)[S] = NextCounter++;
309 Visit(S->getSubStmt());
310 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000311 /// Assign a counter for the body of a while loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000312 void VisitWhileStmt(const WhileStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000313 (*CounterMap)[S] = NextCounter++;
Justin Bogneref512b92014-01-06 22:27:43 +0000314 Visit(S->getCond());
315 Visit(S->getBody());
316 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000317 /// Assign a counter for the body of a do-while loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000318 void VisitDoStmt(const DoStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000319 (*CounterMap)[S] = NextCounter++;
Justin Bogneref512b92014-01-06 22:27:43 +0000320 Visit(S->getBody());
321 Visit(S->getCond());
322 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000323 /// Assign a counter for the body of a for loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000324 void VisitForStmt(const ForStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000325 (*CounterMap)[S] = NextCounter++;
326 if (S->getInit())
327 Visit(S->getInit());
Justin Bogneref512b92014-01-06 22:27:43 +0000328 const Expr *E;
329 if ((E = S->getCond()))
330 Visit(E);
Justin Bogneref512b92014-01-06 22:27:43 +0000331 if ((E = S->getInc()))
332 Visit(E);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000333 Visit(S->getBody());
Justin Bogneref512b92014-01-06 22:27:43 +0000334 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000335 /// Assign a counter for the body of a for-range loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000336 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000337 (*CounterMap)[S] = NextCounter++;
338 Visit(S->getRangeStmt());
339 Visit(S->getBeginEndStmt());
340 Visit(S->getCond());
341 Visit(S->getLoopVarStmt());
Justin Bogneref512b92014-01-06 22:27:43 +0000342 Visit(S->getBody());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000343 Visit(S->getInc());
Justin Bogneref512b92014-01-06 22:27:43 +0000344 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000345 /// Assign a counter for the body of a for-collection loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000346 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000347 (*CounterMap)[S] = NextCounter++;
Justin Bogneref512b92014-01-06 22:27:43 +0000348 Visit(S->getElement());
349 Visit(S->getBody());
350 }
351 /// Assign a counter for the exit block of the switch statement.
352 void VisitSwitchStmt(const SwitchStmt *S) {
353 (*CounterMap)[S] = NextCounter++;
354 Visit(S->getCond());
355 Visit(S->getBody());
356 }
357 /// Assign a counter for a particular case in a switch. This counts jumps
358 /// from the switch header as well as fallthrough from the case before this
359 /// one.
360 void VisitCaseStmt(const CaseStmt *S) {
361 (*CounterMap)[S] = NextCounter++;
362 Visit(S->getSubStmt());
363 }
364 /// Assign a counter for the default case of a switch statement. The count
365 /// is the number of branches from the loop header to the default, and does
366 /// not include fallthrough from previous cases. If we have multiple
367 /// conditional branch blocks from the switch instruction to the default
368 /// block, as with large GNU case ranges, this is the counter for the last
369 /// edge in that series, rather than the first.
370 void VisitDefaultStmt(const DefaultStmt *S) {
371 (*CounterMap)[S] = NextCounter++;
372 Visit(S->getSubStmt());
373 }
374 /// Assign a counter for the "then" part of an if statement. The count for
375 /// the "else" part, if it exists, will be calculated from this counter.
376 void VisitIfStmt(const IfStmt *S) {
377 (*CounterMap)[S] = NextCounter++;
378 Visit(S->getCond());
379 Visit(S->getThen());
380 if (S->getElse())
381 Visit(S->getElse());
382 }
383 /// Assign a counter for the continuation block of a C++ try statement.
384 void VisitCXXTryStmt(const CXXTryStmt *S) {
385 (*CounterMap)[S] = NextCounter++;
386 Visit(S->getTryBlock());
387 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
388 Visit(S->getHandler(I));
389 }
390 /// Assign a counter for a catch statement's handler block.
391 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
392 (*CounterMap)[S] = NextCounter++;
393 Visit(S->getHandlerBlock());
394 }
395 /// Assign a counter for the "true" part of a conditional operator. The
396 /// count in the "false" part will be calculated from this counter.
397 void VisitConditionalOperator(const ConditionalOperator *E) {
398 (*CounterMap)[E] = NextCounter++;
399 Visit(E->getCond());
400 Visit(E->getTrueExpr());
401 Visit(E->getFalseExpr());
402 }
403 /// Assign a counter for the right hand side of a logical and operator.
404 void VisitBinLAnd(const BinaryOperator *E) {
405 (*CounterMap)[E] = NextCounter++;
406 Visit(E->getLHS());
407 Visit(E->getRHS());
408 }
409 /// Assign a counter for the right hand side of a logical or operator.
410 void VisitBinLOr(const BinaryOperator *E) {
411 (*CounterMap)[E] = NextCounter++;
412 Visit(E->getLHS());
413 Visit(E->getRHS());
414 }
415 };
Bob Wilsonbf854f02014-02-17 19:21:09 +0000416
417 /// A StmtVisitor that propagates the raw counts through the AST and
418 /// records the count at statements where the value may change.
419 struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> {
420 /// PGO state.
421 CodeGenPGO &PGO;
422
423 /// A flag that is set when the current count should be recorded on the
424 /// next statement, such as at the exit of a loop.
425 bool RecordNextStmtCount;
426
427 /// The map of statements to count values.
428 llvm::DenseMap<const Stmt*, uint64_t> *CountMap;
429
430 /// BreakContinueStack - Keep counts of breaks and continues inside loops.
431 struct BreakContinue {
432 uint64_t BreakCount;
433 uint64_t ContinueCount;
434 BreakContinue() : BreakCount(0), ContinueCount(0) {}
435 };
436 SmallVector<BreakContinue, 8> BreakContinueStack;
437
438 ComputeRegionCounts(llvm::DenseMap<const Stmt*, uint64_t> *CountMap,
439 CodeGenPGO &PGO) :
440 PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {
441 }
442
443 void RecordStmtCount(const Stmt *S) {
444 if (RecordNextStmtCount) {
445 (*CountMap)[S] = PGO.getCurrentRegionCount();
446 RecordNextStmtCount = false;
447 }
448 }
449
450 void VisitStmt(const Stmt *S) {
451 RecordStmtCount(S);
452 for (Stmt::const_child_range I = S->children(); I; ++I) {
453 if (*I)
454 this->Visit(*I);
455 }
456 }
457
458 void VisitFunctionDecl(const FunctionDecl *S) {
459 RegionCounter Cnt(PGO, S->getBody());
460 Cnt.beginRegion();
461 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
462 Visit(S->getBody());
463 }
464
465 void VisitReturnStmt(const ReturnStmt *S) {
466 RecordStmtCount(S);
467 if (S->getRetValue())
468 Visit(S->getRetValue());
469 PGO.setCurrentRegionUnreachable();
470 RecordNextStmtCount = true;
471 }
472
473 void VisitGotoStmt(const GotoStmt *S) {
474 RecordStmtCount(S);
475 PGO.setCurrentRegionUnreachable();
476 RecordNextStmtCount = true;
477 }
478
479 void VisitLabelStmt(const LabelStmt *S) {
480 RecordNextStmtCount = false;
481 RegionCounter Cnt(PGO, S);
482 Cnt.beginRegion();
483 (*CountMap)[S] = PGO.getCurrentRegionCount();
484 Visit(S->getSubStmt());
485 }
486
487 void VisitBreakStmt(const BreakStmt *S) {
488 RecordStmtCount(S);
489 assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
490 BreakContinueStack.back().BreakCount += PGO.getCurrentRegionCount();
491 PGO.setCurrentRegionUnreachable();
492 RecordNextStmtCount = true;
493 }
494
495 void VisitContinueStmt(const ContinueStmt *S) {
496 RecordStmtCount(S);
497 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
498 BreakContinueStack.back().ContinueCount += PGO.getCurrentRegionCount();
499 PGO.setCurrentRegionUnreachable();
500 RecordNextStmtCount = true;
501 }
502
503 void VisitWhileStmt(const WhileStmt *S) {
504 RecordStmtCount(S);
505 RegionCounter Cnt(PGO, S);
506 BreakContinueStack.push_back(BreakContinue());
507 // Visit the body region first so the break/continue adjustments can be
508 // included when visiting the condition.
509 Cnt.beginRegion();
510 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
511 Visit(S->getBody());
512 Cnt.adjustForControlFlow();
513
514 // ...then go back and propagate counts through the condition. The count
515 // at the start of the condition is the sum of the incoming edges,
516 // the backedge from the end of the loop body, and the edges from
517 // continue statements.
518 BreakContinue BC = BreakContinueStack.pop_back_val();
519 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
520 Cnt.getAdjustedCount() + BC.ContinueCount);
521 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
522 Visit(S->getCond());
523 Cnt.adjustForControlFlow();
524 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
525 RecordNextStmtCount = true;
526 }
527
528 void VisitDoStmt(const DoStmt *S) {
529 RecordStmtCount(S);
530 RegionCounter Cnt(PGO, S);
531 BreakContinueStack.push_back(BreakContinue());
532 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
533 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
534 Visit(S->getBody());
535 Cnt.adjustForControlFlow();
536
537 BreakContinue BC = BreakContinueStack.pop_back_val();
538 // The count at the start of the condition is equal to the count at the
539 // end of the body. The adjusted count does not include either the
540 // fall-through count coming into the loop or the continue count, so add
541 // both of those separately. This is coincidentally the same equation as
542 // with while loops but for different reasons.
543 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
544 Cnt.getAdjustedCount() + BC.ContinueCount);
545 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
546 Visit(S->getCond());
547 Cnt.adjustForControlFlow();
548 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
549 RecordNextStmtCount = true;
550 }
551
552 void VisitForStmt(const ForStmt *S) {
553 RecordStmtCount(S);
554 if (S->getInit())
555 Visit(S->getInit());
556 RegionCounter Cnt(PGO, S);
557 BreakContinueStack.push_back(BreakContinue());
558 // Visit the body region first. (This is basically the same as a while
559 // loop; see further comments in VisitWhileStmt.)
560 Cnt.beginRegion();
561 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
562 Visit(S->getBody());
563 Cnt.adjustForControlFlow();
564
565 // The increment is essentially part of the body but it needs to include
566 // the count for all the continue statements.
567 if (S->getInc()) {
568 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
569 BreakContinueStack.back().ContinueCount);
570 (*CountMap)[S->getInc()] = PGO.getCurrentRegionCount();
571 Visit(S->getInc());
572 Cnt.adjustForControlFlow();
573 }
574
575 BreakContinue BC = BreakContinueStack.pop_back_val();
576
577 // ...then go back and propagate counts through the condition.
578 if (S->getCond()) {
579 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
580 Cnt.getAdjustedCount() +
581 BC.ContinueCount);
582 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
583 Visit(S->getCond());
584 Cnt.adjustForControlFlow();
585 }
586 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
587 RecordNextStmtCount = true;
588 }
589
590 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
591 RecordStmtCount(S);
592 Visit(S->getRangeStmt());
593 Visit(S->getBeginEndStmt());
594 RegionCounter Cnt(PGO, S);
595 BreakContinueStack.push_back(BreakContinue());
596 // Visit the body region first. (This is basically the same as a while
597 // loop; see further comments in VisitWhileStmt.)
598 Cnt.beginRegion();
599 (*CountMap)[S->getLoopVarStmt()] = PGO.getCurrentRegionCount();
600 Visit(S->getLoopVarStmt());
601 Visit(S->getBody());
602 Cnt.adjustForControlFlow();
603
604 // The increment is essentially part of the body but it needs to include
605 // the count for all the continue statements.
606 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
607 BreakContinueStack.back().ContinueCount);
608 (*CountMap)[S->getInc()] = PGO.getCurrentRegionCount();
609 Visit(S->getInc());
610 Cnt.adjustForControlFlow();
611
612 BreakContinue BC = BreakContinueStack.pop_back_val();
613
614 // ...then go back and propagate counts through the condition.
615 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
616 Cnt.getAdjustedCount() +
617 BC.ContinueCount);
618 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
619 Visit(S->getCond());
620 Cnt.adjustForControlFlow();
621 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
622 RecordNextStmtCount = true;
623 }
624
625 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
626 RecordStmtCount(S);
627 Visit(S->getElement());
628 RegionCounter Cnt(PGO, S);
629 BreakContinueStack.push_back(BreakContinue());
630 Cnt.beginRegion();
631 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
632 Visit(S->getBody());
633 BreakContinue BC = BreakContinueStack.pop_back_val();
634 Cnt.adjustForControlFlow();
635 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
636 RecordNextStmtCount = true;
637 }
638
639 void VisitSwitchStmt(const SwitchStmt *S) {
640 RecordStmtCount(S);
641 Visit(S->getCond());
642 PGO.setCurrentRegionUnreachable();
643 BreakContinueStack.push_back(BreakContinue());
644 Visit(S->getBody());
645 // If the switch is inside a loop, add the continue counts.
646 BreakContinue BC = BreakContinueStack.pop_back_val();
647 if (!BreakContinueStack.empty())
648 BreakContinueStack.back().ContinueCount += BC.ContinueCount;
649 RegionCounter ExitCnt(PGO, S);
650 ExitCnt.beginRegion();
651 RecordNextStmtCount = true;
652 }
653
654 void VisitCaseStmt(const CaseStmt *S) {
655 RecordNextStmtCount = false;
656 RegionCounter Cnt(PGO, S);
657 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
658 (*CountMap)[S] = Cnt.getCount();
659 RecordNextStmtCount = true;
660 Visit(S->getSubStmt());
661 }
662
663 void VisitDefaultStmt(const DefaultStmt *S) {
664 RecordNextStmtCount = false;
665 RegionCounter Cnt(PGO, S);
666 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
667 (*CountMap)[S] = Cnt.getCount();
668 RecordNextStmtCount = true;
669 Visit(S->getSubStmt());
670 }
671
672 void VisitIfStmt(const IfStmt *S) {
673 RecordStmtCount(S);
674 RegionCounter Cnt(PGO, S);
675 Visit(S->getCond());
676
677 Cnt.beginRegion();
678 (*CountMap)[S->getThen()] = PGO.getCurrentRegionCount();
679 Visit(S->getThen());
680 Cnt.adjustForControlFlow();
681
682 if (S->getElse()) {
683 Cnt.beginElseRegion();
684 (*CountMap)[S->getElse()] = PGO.getCurrentRegionCount();
685 Visit(S->getElse());
686 Cnt.adjustForControlFlow();
687 }
688 Cnt.applyAdjustmentsToRegion(0);
689 RecordNextStmtCount = true;
690 }
691
692 void VisitCXXTryStmt(const CXXTryStmt *S) {
693 RecordStmtCount(S);
694 Visit(S->getTryBlock());
695 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
696 Visit(S->getHandler(I));
697 RegionCounter Cnt(PGO, S);
698 Cnt.beginRegion();
699 RecordNextStmtCount = true;
700 }
701
702 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
703 RecordNextStmtCount = false;
704 RegionCounter Cnt(PGO, S);
705 Cnt.beginRegion();
706 (*CountMap)[S] = PGO.getCurrentRegionCount();
707 Visit(S->getHandlerBlock());
708 }
709
710 void VisitConditionalOperator(const ConditionalOperator *E) {
711 RecordStmtCount(E);
712 RegionCounter Cnt(PGO, E);
713 Visit(E->getCond());
714
715 Cnt.beginRegion();
716 (*CountMap)[E->getTrueExpr()] = PGO.getCurrentRegionCount();
717 Visit(E->getTrueExpr());
718 Cnt.adjustForControlFlow();
719
720 Cnt.beginElseRegion();
721 (*CountMap)[E->getFalseExpr()] = PGO.getCurrentRegionCount();
722 Visit(E->getFalseExpr());
723 Cnt.adjustForControlFlow();
724
725 Cnt.applyAdjustmentsToRegion(0);
726 RecordNextStmtCount = true;
727 }
728
729 void VisitBinLAnd(const BinaryOperator *E) {
730 RecordStmtCount(E);
731 RegionCounter Cnt(PGO, E);
732 Visit(E->getLHS());
733 Cnt.beginRegion();
734 (*CountMap)[E->getRHS()] = PGO.getCurrentRegionCount();
735 Visit(E->getRHS());
736 Cnt.adjustForControlFlow();
737 Cnt.applyAdjustmentsToRegion(0);
738 RecordNextStmtCount = true;
739 }
740
741 void VisitBinLOr(const BinaryOperator *E) {
742 RecordStmtCount(E);
743 RegionCounter Cnt(PGO, E);
744 Visit(E->getLHS());
745 Cnt.beginRegion();
746 (*CountMap)[E->getRHS()] = PGO.getCurrentRegionCount();
747 Visit(E->getRHS());
748 Cnt.adjustForControlFlow();
749 Cnt.applyAdjustmentsToRegion(0);
750 RecordNextStmtCount = true;
751 }
752 };
Justin Bogneref512b92014-01-06 22:27:43 +0000753}
754
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000755void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) {
Justin Bogneref512b92014-01-06 22:27:43 +0000756 bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate;
757 PGOProfileData *PGOData = CGM.getPGOData();
758 if (!InstrumentRegions && !PGOData)
759 return;
Justin Bogneref512b92014-01-06 22:27:43 +0000760 if (!D)
761 return;
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000762 setFuncName(Fn);
Justin Bogneref512b92014-01-06 22:27:43 +0000763 mapRegionCounters(D);
764 if (InstrumentRegions)
765 emitCounterVariables();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000766 if (PGOData) {
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000767 loadRegionCounts(PGOData);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000768 computeRegionCounts(D);
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000769
770 // Turn on InlineHint attribute for hot functions.
771 if (PGOData->isHotFunction(getFuncName()))
772 Fn->addFnAttr(llvm::Attribute::InlineHint);
773 // Turn on Cold attribute for cold functions.
774 else if (PGOData->isColdFunction(getFuncName()))
775 Fn->addFnAttr(llvm::Attribute::Cold);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000776 }
Justin Bogneref512b92014-01-06 22:27:43 +0000777}
778
779void CodeGenPGO::mapRegionCounters(const Decl *D) {
780 RegionCounterMap = new llvm::DenseMap<const Stmt*, unsigned>();
781 MapRegionCounters Walker(RegionCounterMap);
782 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
783 Walker.VisitFunctionDecl(FD);
784 NumRegionCounters = Walker.NextCounter;
785}
786
Bob Wilsonbf854f02014-02-17 19:21:09 +0000787void CodeGenPGO::computeRegionCounts(const Decl *D) {
788 StmtCountMap = new llvm::DenseMap<const Stmt*, uint64_t>();
789 ComputeRegionCounts Walker(StmtCountMap, *this);
790 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
791 Walker.VisitFunctionDecl(FD);
792}
793
Justin Bogneref512b92014-01-06 22:27:43 +0000794void CodeGenPGO::emitCounterVariables() {
795 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
796 llvm::ArrayType *CounterTy = llvm::ArrayType::get(llvm::Type::getInt64Ty(Ctx),
797 NumRegionCounters);
798 RegionCounters =
799 new llvm::GlobalVariable(CGM.getModule(), CounterTy, false,
800 llvm::GlobalVariable::PrivateLinkage,
801 llvm::Constant::getNullValue(CounterTy),
802 "__llvm_pgo_ctr");
803}
804
805void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) {
Bob Wilson749ebc72014-03-06 04:55:28 +0000806 if (!RegionCounters)
Justin Bogneref512b92014-01-06 22:27:43 +0000807 return;
808 llvm::Value *Addr =
809 Builder.CreateConstInBoundsGEP2_64(RegionCounters, 0, Counter);
810 llvm::Value *Count = Builder.CreateLoad(Addr, "pgocount");
811 Count = Builder.CreateAdd(Count, Builder.getInt64(1));
812 Builder.CreateStore(Count, Addr);
813}
814
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000815void CodeGenPGO::loadRegionCounts(PGOProfileData *PGOData) {
Justin Bogneref512b92014-01-06 22:27:43 +0000816 // For now, ignore the counts from the PGO data file only if the number of
817 // counters does not match. This could be tightened down in the future to
818 // ignore counts when the input changes in various ways, e.g., by comparing a
819 // hash value based on some characteristics of the input.
820 RegionCounts = new std::vector<uint64_t>();
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000821 if (PGOData->getFunctionCounts(getFuncName(), *RegionCounts) ||
Justin Bogneref512b92014-01-06 22:27:43 +0000822 RegionCounts->size() != NumRegionCounters) {
823 delete RegionCounts;
824 RegionCounts = 0;
825 }
826}
827
828void CodeGenPGO::destroyRegionCounters() {
829 if (RegionCounterMap != 0)
830 delete RegionCounterMap;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000831 if (StmtCountMap != 0)
832 delete StmtCountMap;
Justin Bogneref512b92014-01-06 22:27:43 +0000833 if (RegionCounts != 0)
834 delete RegionCounts;
835}
836
837llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount,
838 uint64_t FalseCount) {
839 if (!TrueCount && !FalseCount)
840 return 0;
841
842 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
843 // TODO: need to scale down to 32-bits
844 // According to Laplace's Rule of Succession, it is better to compute the
845 // weight based on the count plus 1.
846 return MDHelper.createBranchWeights(TrueCount + 1, FalseCount + 1);
847}
848
Bob Wilson95a27b02014-02-17 19:20:59 +0000849llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) {
Justin Bogneref512b92014-01-06 22:27:43 +0000850 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
851 // TODO: need to scale down to 32-bits, instead of just truncating.
852 // According to Laplace's Rule of Succession, it is better to compute the
853 // weight based on the count plus 1.
854 SmallVector<uint32_t, 16> ScaledWeights;
855 ScaledWeights.reserve(Weights.size());
856 for (ArrayRef<uint64_t>::iterator WI = Weights.begin(), WE = Weights.end();
857 WI != WE; ++WI) {
858 ScaledWeights.push_back(*WI + 1);
859 }
860 return MDHelper.createBranchWeights(ScaledWeights);
861}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000862
863llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond,
864 RegionCounter &Cnt) {
865 if (!haveRegionCounts())
866 return 0;
867 uint64_t LoopCount = Cnt.getCount();
868 uint64_t CondCount = 0;
869 bool Found = getStmtCount(Cond, CondCount);
870 assert(Found && "missing expected loop condition count");
871 (void)Found;
872 if (CondCount == 0)
873 return 0;
874 return createBranchWeights(LoopCount,
875 std::max(CondCount, LoopCount) - LoopCount);
876}