blob: cdd411cfa35ebfc1cbda026e86a45845ad7fa999 [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;
Bob Wilson5ec8fe12014-03-06 06:10:02 +000052 // 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');
Justin Bogneref512b92014-01-06 22:27:43 +000057 if (!CurPtr) {
58 ReportBadPGOData(CGM, "pgo data file has malformed function entry");
59 return;
60 }
Bob Wilson5ec8fe12014-03-06 06:10:02 +000061 while (*--CurPtr != ' ')
62 ;
Bob Wilsond0b78242014-03-06 04:55:37 +000063 StringRef FuncName(FuncStart, CurPtr - FuncStart);
Justin Bogneref512b92014-01-06 22:27:43 +000064
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
Manman Ren67a28132014-02-05 20:40:15 +000074 // 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 }
Manman Renf1a6a2d2014-02-15 01:29:02 +000080 CurPtr = EndPtr; // Point to '\n'.
Bob Wilsond0b78242014-03-06 04:55:37 +000081 FunctionCounts[FuncName] = Count;
Manman Ren67a28132014-02-05 20:40:15 +000082 MaxCount = Count > MaxCount ? Count : MaxCount;
83
Justin Bogneref512b92014-01-06 22:27:43 +000084 // There is one line for each counter; skip over those lines.
Manman Ren67a28132014-02-05 20:40:15 +000085 // Since function count is already read, we start the loop from 1.
86 for (unsigned N = 1; N < NumCounters; ++N) {
Justin Bogneref512b92014-01-06 22:27:43 +000087 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
Bob Wilsond0b78242014-03-06 04:55:37 +000097 DataOffsets[FuncName] = FuncStart - BufferStart;
Justin Bogneref512b92014-01-06 22:27:43 +000098 }
Manman Ren67a28132014-02-05 20:40:15 +000099 MaxFunctionCount = MaxCount;
100}
101
102/// Return true if a function is hot. If we know nothing about the function,
103/// return false.
Bob Wilsond0b78242014-03-06 04:55:37 +0000104bool PGOProfileData::isHotFunction(StringRef FuncName) {
Manman Ren67a28132014-02-05 20:40:15 +0000105 llvm::StringMap<uint64_t>::const_iterator CountIter =
Bob Wilsond0b78242014-03-06 04:55:37 +0000106 FunctionCounts.find(FuncName);
Manman Ren67a28132014-02-05 20:40:15 +0000107 // If we know nothing about the function, return false.
108 if (CountIter == FunctionCounts.end())
109 return false;
110 // FIXME: functions with >= 30% of the maximal function count are
111 // treated as hot. This number is from preliminary tuning on SPEC.
112 return CountIter->getValue() >= (uint64_t)(0.3 * (double)MaxFunctionCount);
113}
114
115/// Return true if a function is cold. If we know nothing about the function,
116/// return false.
Bob Wilsond0b78242014-03-06 04:55:37 +0000117bool PGOProfileData::isColdFunction(StringRef FuncName) {
Manman Ren67a28132014-02-05 20:40:15 +0000118 llvm::StringMap<uint64_t>::const_iterator CountIter =
Bob Wilsond0b78242014-03-06 04:55:37 +0000119 FunctionCounts.find(FuncName);
Manman Ren67a28132014-02-05 20:40:15 +0000120 // If we know nothing about the function, return false.
121 if (CountIter == FunctionCounts.end())
122 return false;
123 // FIXME: functions with <= 1% of the maximal function count are treated as
124 // cold. This number is from preliminary tuning on SPEC.
125 return CountIter->getValue() <= (uint64_t)(0.01 * (double)MaxFunctionCount);
Justin Bogneref512b92014-01-06 22:27:43 +0000126}
127
Bob Wilsond0b78242014-03-06 04:55:37 +0000128bool PGOProfileData::getFunctionCounts(StringRef FuncName,
Justin Bogneref512b92014-01-06 22:27:43 +0000129 std::vector<uint64_t> &Counts) {
130 // Find the relevant section of the pgo-data file.
131 llvm::StringMap<unsigned>::const_iterator OffsetIter =
Bob Wilsond0b78242014-03-06 04:55:37 +0000132 DataOffsets.find(FuncName);
Justin Bogneref512b92014-01-06 22:27:43 +0000133 if (OffsetIter == DataOffsets.end())
134 return true;
135 const char *CurPtr = DataBuffer->getBufferStart() + OffsetIter->getValue();
136
137 // Skip over the function name.
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000138 CurPtr = strchr(CurPtr, '\n');
Justin Bogneref512b92014-01-06 22:27:43 +0000139 assert(CurPtr && "pgo-data has corrupted function entry");
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000140 while (*--CurPtr != ' ')
141 ;
Justin Bogneref512b92014-01-06 22:27:43 +0000142
143 // Read the number of counters.
144 char *EndPtr;
145 unsigned NumCounters = strtol(++CurPtr, &EndPtr, 10);
146 assert(EndPtr != CurPtr && *EndPtr == '\n' && NumCounters > 0 &&
147 "pgo-data file has corrupted number of counters");
148 CurPtr = EndPtr;
149
150 Counts.reserve(NumCounters);
151
152 for (unsigned N = 0; N < NumCounters; ++N) {
153 // Read the count value.
154 uint64_t Count = strtoll(CurPtr, &EndPtr, 10);
155 if (EndPtr == CurPtr || *EndPtr != '\n') {
156 ReportBadPGOData(CGM, "pgo-data file has bad count value");
157 return true;
158 }
159 Counts.push_back(Count);
160 CurPtr = EndPtr + 1;
161 }
162
163 // Make sure the number of counters matches up.
164 if (Counts.size() != NumCounters) {
165 ReportBadPGOData(CGM, "pgo-data file has inconsistent counters");
166 return true;
167 }
168
169 return false;
170}
171
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000172void CodeGenPGO::setFuncName(llvm::Function *Fn) {
173 StringRef Func = Fn->getName();
174
175 // Function names may be prefixed with a binary '1' to indicate
176 // that the backend should not modify the symbols due to any platform
177 // naming convention. Do not include that '1' in the PGO profile name.
178 if (Func[0] == '\1')
179 Func = Func.substr(1);
180
181 if (!Fn->hasLocalLinkage()) {
182 FuncName = new std::string(Func);
183 return;
184 }
185
186 // For local symbols, prepend the main file name to distinguish them.
187 // Do not include the full path in the file name since there's no guarantee
188 // that it will stay the same, e.g., if the files are checked out from
189 // version control in different locations.
190 FuncName = new std::string(CGM.getCodeGenOpts().MainFileName);
191 if (FuncName->empty())
192 FuncName->assign("<unknown>");
193 FuncName->append(":");
194 FuncName->append(Func);
195}
196
197void CodeGenPGO::emitWriteoutFunction() {
Justin Bogneref512b92014-01-06 22:27:43 +0000198 if (!CGM.getCodeGenOpts().ProfileInstrGenerate)
199 return;
200
201 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
202
203 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx);
204 llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx);
205
206 llvm::Function *WriteoutF =
207 CGM.getModule().getFunction("__llvm_pgo_writeout");
208 if (!WriteoutF) {
209 llvm::FunctionType *WriteoutFTy =
210 llvm::FunctionType::get(llvm::Type::getVoidTy(Ctx), false);
211 WriteoutF = llvm::Function::Create(WriteoutFTy,
212 llvm::GlobalValue::InternalLinkage,
213 "__llvm_pgo_writeout", &CGM.getModule());
214 }
215 WriteoutF->setUnnamedAddr(true);
216 WriteoutF->addFnAttr(llvm::Attribute::NoInline);
217 if (CGM.getCodeGenOpts().DisableRedZone)
218 WriteoutF->addFnAttr(llvm::Attribute::NoRedZone);
219
220 llvm::BasicBlock *BB = WriteoutF->empty() ?
221 llvm::BasicBlock::Create(Ctx, "", WriteoutF) : &WriteoutF->getEntryBlock();
222
223 CGBuilderTy PGOBuilder(BB);
224
225 llvm::Instruction *I = BB->getTerminator();
226 if (!I)
227 I = PGOBuilder.CreateRetVoid();
228 PGOBuilder.SetInsertPoint(I);
229
230 llvm::Type *Int64PtrTy = llvm::Type::getInt64PtrTy(Ctx);
231 llvm::Type *Args[] = {
Bob Wilsond0b78242014-03-06 04:55:37 +0000232 Int8PtrTy, // const char *FuncName
Justin Bogneref512b92014-01-06 22:27:43 +0000233 Int32Ty, // uint32_t NumCounters
234 Int64PtrTy // uint64_t *Counters
235 };
236 llvm::FunctionType *FTy =
237 llvm::FunctionType::get(PGOBuilder.getVoidTy(), Args, false);
238 llvm::Constant *EmitFunc =
239 CGM.getModule().getOrInsertFunction("llvm_pgo_emit", FTy);
240
Bob Wilsond0b78242014-03-06 04:55:37 +0000241 llvm::Constant *NameString =
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000242 CGM.GetAddrOfConstantCString(getFuncName(), "__llvm_pgo_name");
Bob Wilsond0b78242014-03-06 04:55:37 +0000243 NameString = llvm::ConstantExpr::getBitCast(NameString, Int8PtrTy);
244 PGOBuilder.CreateCall3(EmitFunc, NameString,
Justin Bogneref512b92014-01-06 22:27:43 +0000245 PGOBuilder.getInt32(NumRegionCounters),
246 PGOBuilder.CreateBitCast(RegionCounters, Int64PtrTy));
247}
248
249llvm::Function *CodeGenPGO::emitInitialization(CodeGenModule &CGM) {
250 llvm::Function *WriteoutF =
251 CGM.getModule().getFunction("__llvm_pgo_writeout");
252 if (!WriteoutF)
253 return NULL;
254
255 // Create a small bit of code that registers the "__llvm_pgo_writeout" to
256 // be executed at exit.
257 llvm::Function *F = CGM.getModule().getFunction("__llvm_pgo_init");
258 if (F)
259 return NULL;
260
261 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
262 llvm::FunctionType *FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(Ctx),
263 false);
264 F = llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
265 "__llvm_pgo_init", &CGM.getModule());
266 F->setUnnamedAddr(true);
267 F->setLinkage(llvm::GlobalValue::InternalLinkage);
268 F->addFnAttr(llvm::Attribute::NoInline);
269 if (CGM.getCodeGenOpts().DisableRedZone)
270 F->addFnAttr(llvm::Attribute::NoRedZone);
271
272 llvm::BasicBlock *BB = llvm::BasicBlock::Create(CGM.getLLVMContext(), "", F);
273 CGBuilderTy PGOBuilder(BB);
274
275 FTy = llvm::FunctionType::get(PGOBuilder.getVoidTy(), false);
276 llvm::Type *Params[] = {
277 llvm::PointerType::get(FTy, 0)
278 };
279 FTy = llvm::FunctionType::get(PGOBuilder.getVoidTy(), Params, false);
280
281 // Inialize the environment and register the local writeout function.
282 llvm::Constant *PGOInit =
283 CGM.getModule().getOrInsertFunction("llvm_pgo_init", FTy);
284 PGOBuilder.CreateCall(PGOInit, WriteoutF);
285 PGOBuilder.CreateRetVoid();
286
287 return F;
288}
289
290namespace {
291 /// A StmtVisitor that fills a map of statements to PGO counters.
292 struct MapRegionCounters : public ConstStmtVisitor<MapRegionCounters> {
293 /// The next counter value to assign.
294 unsigned NextCounter;
295 /// The map of statements to counters.
296 llvm::DenseMap<const Stmt*, unsigned> *CounterMap;
297
298 MapRegionCounters(llvm::DenseMap<const Stmt*, unsigned> *CounterMap) :
299 NextCounter(0), CounterMap(CounterMap) {
300 }
301
302 void VisitChildren(const Stmt *S) {
303 for (Stmt::const_child_range I = S->children(); I; ++I)
304 if (*I)
305 this->Visit(*I);
306 }
307 void VisitStmt(const Stmt *S) { VisitChildren(S); }
308
Justin Bognerea278c32014-01-07 00:20:28 +0000309 /// Assign a counter to track entry to the function body.
Justin Bogneref512b92014-01-06 22:27:43 +0000310 void VisitFunctionDecl(const FunctionDecl *S) {
311 (*CounterMap)[S->getBody()] = NextCounter++;
312 Visit(S->getBody());
313 }
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000314 void VisitObjCMethodDecl(const ObjCMethodDecl *S) {
315 (*CounterMap)[S->getBody()] = NextCounter++;
316 Visit(S->getBody());
317 }
Bob Wilsonc845c002014-03-06 20:24:27 +0000318 void VisitBlockDecl(const BlockDecl *S) {
319 (*CounterMap)[S->getBody()] = NextCounter++;
320 Visit(S->getBody());
321 }
Justin Bognerea278c32014-01-07 00:20:28 +0000322 /// Assign a counter to track the block following a label.
Justin Bogneref512b92014-01-06 22:27:43 +0000323 void VisitLabelStmt(const LabelStmt *S) {
324 (*CounterMap)[S] = NextCounter++;
325 Visit(S->getSubStmt());
326 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000327 /// Assign a counter for the body of a while loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000328 void VisitWhileStmt(const WhileStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000329 (*CounterMap)[S] = NextCounter++;
Justin Bogneref512b92014-01-06 22:27:43 +0000330 Visit(S->getCond());
331 Visit(S->getBody());
332 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000333 /// Assign a counter for the body of a do-while loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000334 void VisitDoStmt(const DoStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000335 (*CounterMap)[S] = NextCounter++;
Justin Bogneref512b92014-01-06 22:27:43 +0000336 Visit(S->getBody());
337 Visit(S->getCond());
338 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000339 /// Assign a counter for the body of a for loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000340 void VisitForStmt(const ForStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000341 (*CounterMap)[S] = NextCounter++;
342 if (S->getInit())
343 Visit(S->getInit());
Justin Bogneref512b92014-01-06 22:27:43 +0000344 const Expr *E;
345 if ((E = S->getCond()))
346 Visit(E);
Justin Bogneref512b92014-01-06 22:27:43 +0000347 if ((E = S->getInc()))
348 Visit(E);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000349 Visit(S->getBody());
Justin Bogneref512b92014-01-06 22:27:43 +0000350 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000351 /// Assign a counter for the body of a for-range loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000352 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000353 (*CounterMap)[S] = NextCounter++;
354 Visit(S->getRangeStmt());
355 Visit(S->getBeginEndStmt());
356 Visit(S->getCond());
357 Visit(S->getLoopVarStmt());
Justin Bogneref512b92014-01-06 22:27:43 +0000358 Visit(S->getBody());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000359 Visit(S->getInc());
Justin Bogneref512b92014-01-06 22:27:43 +0000360 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000361 /// Assign a counter for the body of a for-collection loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000362 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000363 (*CounterMap)[S] = NextCounter++;
Justin Bogneref512b92014-01-06 22:27:43 +0000364 Visit(S->getElement());
365 Visit(S->getBody());
366 }
367 /// Assign a counter for the exit block of the switch statement.
368 void VisitSwitchStmt(const SwitchStmt *S) {
369 (*CounterMap)[S] = NextCounter++;
370 Visit(S->getCond());
371 Visit(S->getBody());
372 }
373 /// Assign a counter for a particular case in a switch. This counts jumps
374 /// from the switch header as well as fallthrough from the case before this
375 /// one.
376 void VisitCaseStmt(const CaseStmt *S) {
377 (*CounterMap)[S] = NextCounter++;
378 Visit(S->getSubStmt());
379 }
380 /// Assign a counter for the default case of a switch statement. The count
381 /// is the number of branches from the loop header to the default, and does
382 /// not include fallthrough from previous cases. If we have multiple
383 /// conditional branch blocks from the switch instruction to the default
384 /// block, as with large GNU case ranges, this is the counter for the last
385 /// edge in that series, rather than the first.
386 void VisitDefaultStmt(const DefaultStmt *S) {
387 (*CounterMap)[S] = NextCounter++;
388 Visit(S->getSubStmt());
389 }
390 /// Assign a counter for the "then" part of an if statement. The count for
391 /// the "else" part, if it exists, will be calculated from this counter.
392 void VisitIfStmt(const IfStmt *S) {
393 (*CounterMap)[S] = NextCounter++;
394 Visit(S->getCond());
395 Visit(S->getThen());
396 if (S->getElse())
397 Visit(S->getElse());
398 }
399 /// Assign a counter for the continuation block of a C++ try statement.
400 void VisitCXXTryStmt(const CXXTryStmt *S) {
401 (*CounterMap)[S] = NextCounter++;
402 Visit(S->getTryBlock());
403 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
404 Visit(S->getHandler(I));
405 }
406 /// Assign a counter for a catch statement's handler block.
407 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
408 (*CounterMap)[S] = NextCounter++;
409 Visit(S->getHandlerBlock());
410 }
411 /// Assign a counter for the "true" part of a conditional operator. The
412 /// count in the "false" part will be calculated from this counter.
413 void VisitConditionalOperator(const ConditionalOperator *E) {
414 (*CounterMap)[E] = NextCounter++;
415 Visit(E->getCond());
416 Visit(E->getTrueExpr());
417 Visit(E->getFalseExpr());
418 }
419 /// Assign a counter for the right hand side of a logical and operator.
420 void VisitBinLAnd(const BinaryOperator *E) {
421 (*CounterMap)[E] = NextCounter++;
422 Visit(E->getLHS());
423 Visit(E->getRHS());
424 }
425 /// Assign a counter for the right hand side of a logical or operator.
426 void VisitBinLOr(const BinaryOperator *E) {
427 (*CounterMap)[E] = NextCounter++;
428 Visit(E->getLHS());
429 Visit(E->getRHS());
430 }
431 };
Bob Wilsonbf854f02014-02-17 19:21:09 +0000432
433 /// A StmtVisitor that propagates the raw counts through the AST and
434 /// records the count at statements where the value may change.
435 struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> {
436 /// PGO state.
437 CodeGenPGO &PGO;
438
439 /// A flag that is set when the current count should be recorded on the
440 /// next statement, such as at the exit of a loop.
441 bool RecordNextStmtCount;
442
443 /// The map of statements to count values.
444 llvm::DenseMap<const Stmt*, uint64_t> *CountMap;
445
446 /// BreakContinueStack - Keep counts of breaks and continues inside loops.
447 struct BreakContinue {
448 uint64_t BreakCount;
449 uint64_t ContinueCount;
450 BreakContinue() : BreakCount(0), ContinueCount(0) {}
451 };
452 SmallVector<BreakContinue, 8> BreakContinueStack;
453
454 ComputeRegionCounts(llvm::DenseMap<const Stmt*, uint64_t> *CountMap,
455 CodeGenPGO &PGO) :
456 PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {
457 }
458
459 void RecordStmtCount(const Stmt *S) {
460 if (RecordNextStmtCount) {
461 (*CountMap)[S] = PGO.getCurrentRegionCount();
462 RecordNextStmtCount = false;
463 }
464 }
465
466 void VisitStmt(const Stmt *S) {
467 RecordStmtCount(S);
468 for (Stmt::const_child_range I = S->children(); I; ++I) {
469 if (*I)
470 this->Visit(*I);
471 }
472 }
473
474 void VisitFunctionDecl(const FunctionDecl *S) {
475 RegionCounter Cnt(PGO, S->getBody());
476 Cnt.beginRegion();
477 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
478 Visit(S->getBody());
479 }
480
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000481 void VisitObjCMethodDecl(const ObjCMethodDecl *S) {
482 RegionCounter Cnt(PGO, S->getBody());
483 Cnt.beginRegion();
484 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
485 Visit(S->getBody());
486 }
487
Bob Wilsonc845c002014-03-06 20:24:27 +0000488 void VisitBlockDecl(const BlockDecl *S) {
489 RegionCounter Cnt(PGO, S->getBody());
490 Cnt.beginRegion();
491 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
492 Visit(S->getBody());
493 }
494
Bob Wilsonbf854f02014-02-17 19:21:09 +0000495 void VisitReturnStmt(const ReturnStmt *S) {
496 RecordStmtCount(S);
497 if (S->getRetValue())
498 Visit(S->getRetValue());
499 PGO.setCurrentRegionUnreachable();
500 RecordNextStmtCount = true;
501 }
502
503 void VisitGotoStmt(const GotoStmt *S) {
504 RecordStmtCount(S);
505 PGO.setCurrentRegionUnreachable();
506 RecordNextStmtCount = true;
507 }
508
509 void VisitLabelStmt(const LabelStmt *S) {
510 RecordNextStmtCount = false;
511 RegionCounter Cnt(PGO, S);
512 Cnt.beginRegion();
513 (*CountMap)[S] = PGO.getCurrentRegionCount();
514 Visit(S->getSubStmt());
515 }
516
517 void VisitBreakStmt(const BreakStmt *S) {
518 RecordStmtCount(S);
519 assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
520 BreakContinueStack.back().BreakCount += PGO.getCurrentRegionCount();
521 PGO.setCurrentRegionUnreachable();
522 RecordNextStmtCount = true;
523 }
524
525 void VisitContinueStmt(const ContinueStmt *S) {
526 RecordStmtCount(S);
527 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
528 BreakContinueStack.back().ContinueCount += PGO.getCurrentRegionCount();
529 PGO.setCurrentRegionUnreachable();
530 RecordNextStmtCount = true;
531 }
532
533 void VisitWhileStmt(const WhileStmt *S) {
534 RecordStmtCount(S);
535 RegionCounter Cnt(PGO, S);
536 BreakContinueStack.push_back(BreakContinue());
537 // Visit the body region first so the break/continue adjustments can be
538 // included when visiting the condition.
539 Cnt.beginRegion();
540 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
541 Visit(S->getBody());
542 Cnt.adjustForControlFlow();
543
544 // ...then go back and propagate counts through the condition. The count
545 // at the start of the condition is the sum of the incoming edges,
546 // the backedge from the end of the loop body, and the edges from
547 // continue statements.
548 BreakContinue BC = BreakContinueStack.pop_back_val();
549 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
550 Cnt.getAdjustedCount() + BC.ContinueCount);
551 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
552 Visit(S->getCond());
553 Cnt.adjustForControlFlow();
554 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
555 RecordNextStmtCount = true;
556 }
557
558 void VisitDoStmt(const DoStmt *S) {
559 RecordStmtCount(S);
560 RegionCounter Cnt(PGO, S);
561 BreakContinueStack.push_back(BreakContinue());
562 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
563 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
564 Visit(S->getBody());
565 Cnt.adjustForControlFlow();
566
567 BreakContinue BC = BreakContinueStack.pop_back_val();
568 // The count at the start of the condition is equal to the count at the
569 // end of the body. The adjusted count does not include either the
570 // fall-through count coming into the loop or the continue count, so add
571 // both of those separately. This is coincidentally the same equation as
572 // with while loops but for different reasons.
573 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
574 Cnt.getAdjustedCount() + BC.ContinueCount);
575 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
576 Visit(S->getCond());
577 Cnt.adjustForControlFlow();
578 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
579 RecordNextStmtCount = true;
580 }
581
582 void VisitForStmt(const ForStmt *S) {
583 RecordStmtCount(S);
584 if (S->getInit())
585 Visit(S->getInit());
586 RegionCounter Cnt(PGO, S);
587 BreakContinueStack.push_back(BreakContinue());
588 // Visit the body region first. (This is basically the same as a while
589 // loop; see further comments in VisitWhileStmt.)
590 Cnt.beginRegion();
591 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
592 Visit(S->getBody());
593 Cnt.adjustForControlFlow();
594
595 // The increment is essentially part of the body but it needs to include
596 // the count for all the continue statements.
597 if (S->getInc()) {
598 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
599 BreakContinueStack.back().ContinueCount);
600 (*CountMap)[S->getInc()] = PGO.getCurrentRegionCount();
601 Visit(S->getInc());
602 Cnt.adjustForControlFlow();
603 }
604
605 BreakContinue BC = BreakContinueStack.pop_back_val();
606
607 // ...then go back and propagate counts through the condition.
608 if (S->getCond()) {
609 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
610 Cnt.getAdjustedCount() +
611 BC.ContinueCount);
612 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
613 Visit(S->getCond());
614 Cnt.adjustForControlFlow();
615 }
616 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
617 RecordNextStmtCount = true;
618 }
619
620 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
621 RecordStmtCount(S);
622 Visit(S->getRangeStmt());
623 Visit(S->getBeginEndStmt());
624 RegionCounter Cnt(PGO, S);
625 BreakContinueStack.push_back(BreakContinue());
626 // Visit the body region first. (This is basically the same as a while
627 // loop; see further comments in VisitWhileStmt.)
628 Cnt.beginRegion();
629 (*CountMap)[S->getLoopVarStmt()] = PGO.getCurrentRegionCount();
630 Visit(S->getLoopVarStmt());
631 Visit(S->getBody());
632 Cnt.adjustForControlFlow();
633
634 // The increment is essentially part of the body but it needs to include
635 // the count for all the continue statements.
636 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
637 BreakContinueStack.back().ContinueCount);
638 (*CountMap)[S->getInc()] = PGO.getCurrentRegionCount();
639 Visit(S->getInc());
640 Cnt.adjustForControlFlow();
641
642 BreakContinue BC = BreakContinueStack.pop_back_val();
643
644 // ...then go back and propagate counts through the condition.
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 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
652 RecordNextStmtCount = true;
653 }
654
655 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
656 RecordStmtCount(S);
657 Visit(S->getElement());
658 RegionCounter Cnt(PGO, S);
659 BreakContinueStack.push_back(BreakContinue());
660 Cnt.beginRegion();
661 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
662 Visit(S->getBody());
663 BreakContinue BC = BreakContinueStack.pop_back_val();
664 Cnt.adjustForControlFlow();
665 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
666 RecordNextStmtCount = true;
667 }
668
669 void VisitSwitchStmt(const SwitchStmt *S) {
670 RecordStmtCount(S);
671 Visit(S->getCond());
672 PGO.setCurrentRegionUnreachable();
673 BreakContinueStack.push_back(BreakContinue());
674 Visit(S->getBody());
675 // If the switch is inside a loop, add the continue counts.
676 BreakContinue BC = BreakContinueStack.pop_back_val();
677 if (!BreakContinueStack.empty())
678 BreakContinueStack.back().ContinueCount += BC.ContinueCount;
679 RegionCounter ExitCnt(PGO, S);
680 ExitCnt.beginRegion();
681 RecordNextStmtCount = true;
682 }
683
684 void VisitCaseStmt(const CaseStmt *S) {
685 RecordNextStmtCount = false;
686 RegionCounter Cnt(PGO, S);
687 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
688 (*CountMap)[S] = Cnt.getCount();
689 RecordNextStmtCount = true;
690 Visit(S->getSubStmt());
691 }
692
693 void VisitDefaultStmt(const DefaultStmt *S) {
694 RecordNextStmtCount = false;
695 RegionCounter Cnt(PGO, S);
696 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
697 (*CountMap)[S] = Cnt.getCount();
698 RecordNextStmtCount = true;
699 Visit(S->getSubStmt());
700 }
701
702 void VisitIfStmt(const IfStmt *S) {
703 RecordStmtCount(S);
704 RegionCounter Cnt(PGO, S);
705 Visit(S->getCond());
706
707 Cnt.beginRegion();
708 (*CountMap)[S->getThen()] = PGO.getCurrentRegionCount();
709 Visit(S->getThen());
710 Cnt.adjustForControlFlow();
711
712 if (S->getElse()) {
713 Cnt.beginElseRegion();
714 (*CountMap)[S->getElse()] = PGO.getCurrentRegionCount();
715 Visit(S->getElse());
716 Cnt.adjustForControlFlow();
717 }
718 Cnt.applyAdjustmentsToRegion(0);
719 RecordNextStmtCount = true;
720 }
721
722 void VisitCXXTryStmt(const CXXTryStmt *S) {
723 RecordStmtCount(S);
724 Visit(S->getTryBlock());
725 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
726 Visit(S->getHandler(I));
727 RegionCounter Cnt(PGO, S);
728 Cnt.beginRegion();
729 RecordNextStmtCount = true;
730 }
731
732 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
733 RecordNextStmtCount = false;
734 RegionCounter Cnt(PGO, S);
735 Cnt.beginRegion();
736 (*CountMap)[S] = PGO.getCurrentRegionCount();
737 Visit(S->getHandlerBlock());
738 }
739
740 void VisitConditionalOperator(const ConditionalOperator *E) {
741 RecordStmtCount(E);
742 RegionCounter Cnt(PGO, E);
743 Visit(E->getCond());
744
745 Cnt.beginRegion();
746 (*CountMap)[E->getTrueExpr()] = PGO.getCurrentRegionCount();
747 Visit(E->getTrueExpr());
748 Cnt.adjustForControlFlow();
749
750 Cnt.beginElseRegion();
751 (*CountMap)[E->getFalseExpr()] = PGO.getCurrentRegionCount();
752 Visit(E->getFalseExpr());
753 Cnt.adjustForControlFlow();
754
755 Cnt.applyAdjustmentsToRegion(0);
756 RecordNextStmtCount = true;
757 }
758
759 void VisitBinLAnd(const BinaryOperator *E) {
760 RecordStmtCount(E);
761 RegionCounter Cnt(PGO, E);
762 Visit(E->getLHS());
763 Cnt.beginRegion();
764 (*CountMap)[E->getRHS()] = PGO.getCurrentRegionCount();
765 Visit(E->getRHS());
766 Cnt.adjustForControlFlow();
767 Cnt.applyAdjustmentsToRegion(0);
768 RecordNextStmtCount = true;
769 }
770
771 void VisitBinLOr(const BinaryOperator *E) {
772 RecordStmtCount(E);
773 RegionCounter Cnt(PGO, E);
774 Visit(E->getLHS());
775 Cnt.beginRegion();
776 (*CountMap)[E->getRHS()] = PGO.getCurrentRegionCount();
777 Visit(E->getRHS());
778 Cnt.adjustForControlFlow();
779 Cnt.applyAdjustmentsToRegion(0);
780 RecordNextStmtCount = true;
781 }
782 };
Justin Bogneref512b92014-01-06 22:27:43 +0000783}
784
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000785void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) {
Justin Bogneref512b92014-01-06 22:27:43 +0000786 bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate;
787 PGOProfileData *PGOData = CGM.getPGOData();
788 if (!InstrumentRegions && !PGOData)
789 return;
Justin Bogneref512b92014-01-06 22:27:43 +0000790 if (!D)
791 return;
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000792 setFuncName(Fn);
Justin Bogneref512b92014-01-06 22:27:43 +0000793 mapRegionCounters(D);
794 if (InstrumentRegions)
795 emitCounterVariables();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000796 if (PGOData) {
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000797 loadRegionCounts(PGOData);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000798 computeRegionCounts(D);
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000799
800 // Turn on InlineHint attribute for hot functions.
801 if (PGOData->isHotFunction(getFuncName()))
802 Fn->addFnAttr(llvm::Attribute::InlineHint);
803 // Turn on Cold attribute for cold functions.
804 else if (PGOData->isColdFunction(getFuncName()))
805 Fn->addFnAttr(llvm::Attribute::Cold);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000806 }
Justin Bogneref512b92014-01-06 22:27:43 +0000807}
808
809void CodeGenPGO::mapRegionCounters(const Decl *D) {
810 RegionCounterMap = new llvm::DenseMap<const Stmt*, unsigned>();
811 MapRegionCounters Walker(RegionCounterMap);
812 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
813 Walker.VisitFunctionDecl(FD);
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000814 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
815 Walker.VisitObjCMethodDecl(MD);
Bob Wilsonc845c002014-03-06 20:24:27 +0000816 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
817 Walker.VisitBlockDecl(BD);
Justin Bogneref512b92014-01-06 22:27:43 +0000818 NumRegionCounters = Walker.NextCounter;
819}
820
Bob Wilsonbf854f02014-02-17 19:21:09 +0000821void CodeGenPGO::computeRegionCounts(const Decl *D) {
822 StmtCountMap = new llvm::DenseMap<const Stmt*, uint64_t>();
823 ComputeRegionCounts Walker(StmtCountMap, *this);
824 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
825 Walker.VisitFunctionDecl(FD);
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000826 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
827 Walker.VisitObjCMethodDecl(MD);
Bob Wilsonc845c002014-03-06 20:24:27 +0000828 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
829 Walker.VisitBlockDecl(BD);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000830}
831
Justin Bogneref512b92014-01-06 22:27:43 +0000832void CodeGenPGO::emitCounterVariables() {
833 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
834 llvm::ArrayType *CounterTy = llvm::ArrayType::get(llvm::Type::getInt64Ty(Ctx),
835 NumRegionCounters);
836 RegionCounters =
837 new llvm::GlobalVariable(CGM.getModule(), CounterTy, false,
838 llvm::GlobalVariable::PrivateLinkage,
839 llvm::Constant::getNullValue(CounterTy),
840 "__llvm_pgo_ctr");
841}
842
843void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) {
Bob Wilson749ebc72014-03-06 04:55:28 +0000844 if (!RegionCounters)
Justin Bogneref512b92014-01-06 22:27:43 +0000845 return;
846 llvm::Value *Addr =
847 Builder.CreateConstInBoundsGEP2_64(RegionCounters, 0, Counter);
848 llvm::Value *Count = Builder.CreateLoad(Addr, "pgocount");
849 Count = Builder.CreateAdd(Count, Builder.getInt64(1));
850 Builder.CreateStore(Count, Addr);
851}
852
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000853void CodeGenPGO::loadRegionCounts(PGOProfileData *PGOData) {
Justin Bogneref512b92014-01-06 22:27:43 +0000854 // For now, ignore the counts from the PGO data file only if the number of
855 // counters does not match. This could be tightened down in the future to
856 // ignore counts when the input changes in various ways, e.g., by comparing a
857 // hash value based on some characteristics of the input.
858 RegionCounts = new std::vector<uint64_t>();
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000859 if (PGOData->getFunctionCounts(getFuncName(), *RegionCounts) ||
Justin Bogneref512b92014-01-06 22:27:43 +0000860 RegionCounts->size() != NumRegionCounters) {
861 delete RegionCounts;
862 RegionCounts = 0;
863 }
864}
865
866void CodeGenPGO::destroyRegionCounters() {
867 if (RegionCounterMap != 0)
868 delete RegionCounterMap;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000869 if (StmtCountMap != 0)
870 delete StmtCountMap;
Justin Bogneref512b92014-01-06 22:27:43 +0000871 if (RegionCounts != 0)
872 delete RegionCounts;
873}
874
875llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount,
876 uint64_t FalseCount) {
877 if (!TrueCount && !FalseCount)
878 return 0;
879
880 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
881 // TODO: need to scale down to 32-bits
882 // According to Laplace's Rule of Succession, it is better to compute the
883 // weight based on the count plus 1.
884 return MDHelper.createBranchWeights(TrueCount + 1, FalseCount + 1);
885}
886
Bob Wilson95a27b02014-02-17 19:20:59 +0000887llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) {
Justin Bogneref512b92014-01-06 22:27:43 +0000888 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
889 // TODO: need to scale down to 32-bits, instead of just truncating.
890 // According to Laplace's Rule of Succession, it is better to compute the
891 // weight based on the count plus 1.
892 SmallVector<uint32_t, 16> ScaledWeights;
893 ScaledWeights.reserve(Weights.size());
894 for (ArrayRef<uint64_t>::iterator WI = Weights.begin(), WE = Weights.end();
895 WI != WE; ++WI) {
896 ScaledWeights.push_back(*WI + 1);
897 }
898 return MDHelper.createBranchWeights(ScaledWeights);
899}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000900
901llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond,
902 RegionCounter &Cnt) {
903 if (!haveRegionCounts())
904 return 0;
905 uint64_t LoopCount = Cnt.getCount();
906 uint64_t CondCount = 0;
907 bool Found = getStmtCount(Cond, CondCount);
908 assert(Found && "missing expected loop condition count");
909 (void)Found;
910 if (CondCount == 0)
911 return 0;
912 return createBranchWeights(LoopCount,
913 std::max(CondCount, LoopCount) - LoopCount);
914}