blob: 349160ad352c4f72ba9f5aa593f658881a2f8d24 [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 }
Justin Bognerea278c32014-01-07 00:20:28 +0000318 /// Assign a counter to track the block following a label.
Justin Bogneref512b92014-01-06 22:27:43 +0000319 void VisitLabelStmt(const LabelStmt *S) {
320 (*CounterMap)[S] = NextCounter++;
321 Visit(S->getSubStmt());
322 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000323 /// Assign a counter for the body of a while loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000324 void VisitWhileStmt(const WhileStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000325 (*CounterMap)[S] = NextCounter++;
Justin Bogneref512b92014-01-06 22:27:43 +0000326 Visit(S->getCond());
327 Visit(S->getBody());
328 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000329 /// Assign a counter for the body of a do-while loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000330 void VisitDoStmt(const DoStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000331 (*CounterMap)[S] = NextCounter++;
Justin Bogneref512b92014-01-06 22:27:43 +0000332 Visit(S->getBody());
333 Visit(S->getCond());
334 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000335 /// Assign a counter for the body of a for loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000336 void VisitForStmt(const ForStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000337 (*CounterMap)[S] = NextCounter++;
338 if (S->getInit())
339 Visit(S->getInit());
Justin Bogneref512b92014-01-06 22:27:43 +0000340 const Expr *E;
341 if ((E = S->getCond()))
342 Visit(E);
Justin Bogneref512b92014-01-06 22:27:43 +0000343 if ((E = S->getInc()))
344 Visit(E);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000345 Visit(S->getBody());
Justin Bogneref512b92014-01-06 22:27:43 +0000346 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000347 /// Assign a counter for the body of a for-range loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000348 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000349 (*CounterMap)[S] = NextCounter++;
350 Visit(S->getRangeStmt());
351 Visit(S->getBeginEndStmt());
352 Visit(S->getCond());
353 Visit(S->getLoopVarStmt());
Justin Bogneref512b92014-01-06 22:27:43 +0000354 Visit(S->getBody());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000355 Visit(S->getInc());
Justin Bogneref512b92014-01-06 22:27:43 +0000356 }
Bob Wilsonbf854f02014-02-17 19:21:09 +0000357 /// Assign a counter for the body of a for-collection loop.
Justin Bogneref512b92014-01-06 22:27:43 +0000358 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000359 (*CounterMap)[S] = NextCounter++;
Justin Bogneref512b92014-01-06 22:27:43 +0000360 Visit(S->getElement());
361 Visit(S->getBody());
362 }
363 /// Assign a counter for the exit block of the switch statement.
364 void VisitSwitchStmt(const SwitchStmt *S) {
365 (*CounterMap)[S] = NextCounter++;
366 Visit(S->getCond());
367 Visit(S->getBody());
368 }
369 /// Assign a counter for a particular case in a switch. This counts jumps
370 /// from the switch header as well as fallthrough from the case before this
371 /// one.
372 void VisitCaseStmt(const CaseStmt *S) {
373 (*CounterMap)[S] = NextCounter++;
374 Visit(S->getSubStmt());
375 }
376 /// Assign a counter for the default case of a switch statement. The count
377 /// is the number of branches from the loop header to the default, and does
378 /// not include fallthrough from previous cases. If we have multiple
379 /// conditional branch blocks from the switch instruction to the default
380 /// block, as with large GNU case ranges, this is the counter for the last
381 /// edge in that series, rather than the first.
382 void VisitDefaultStmt(const DefaultStmt *S) {
383 (*CounterMap)[S] = NextCounter++;
384 Visit(S->getSubStmt());
385 }
386 /// Assign a counter for the "then" part of an if statement. The count for
387 /// the "else" part, if it exists, will be calculated from this counter.
388 void VisitIfStmt(const IfStmt *S) {
389 (*CounterMap)[S] = NextCounter++;
390 Visit(S->getCond());
391 Visit(S->getThen());
392 if (S->getElse())
393 Visit(S->getElse());
394 }
395 /// Assign a counter for the continuation block of a C++ try statement.
396 void VisitCXXTryStmt(const CXXTryStmt *S) {
397 (*CounterMap)[S] = NextCounter++;
398 Visit(S->getTryBlock());
399 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
400 Visit(S->getHandler(I));
401 }
402 /// Assign a counter for a catch statement's handler block.
403 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
404 (*CounterMap)[S] = NextCounter++;
405 Visit(S->getHandlerBlock());
406 }
407 /// Assign a counter for the "true" part of a conditional operator. The
408 /// count in the "false" part will be calculated from this counter.
409 void VisitConditionalOperator(const ConditionalOperator *E) {
410 (*CounterMap)[E] = NextCounter++;
411 Visit(E->getCond());
412 Visit(E->getTrueExpr());
413 Visit(E->getFalseExpr());
414 }
415 /// Assign a counter for the right hand side of a logical and operator.
416 void VisitBinLAnd(const BinaryOperator *E) {
417 (*CounterMap)[E] = NextCounter++;
418 Visit(E->getLHS());
419 Visit(E->getRHS());
420 }
421 /// Assign a counter for the right hand side of a logical or operator.
422 void VisitBinLOr(const BinaryOperator *E) {
423 (*CounterMap)[E] = NextCounter++;
424 Visit(E->getLHS());
425 Visit(E->getRHS());
426 }
427 };
Bob Wilsonbf854f02014-02-17 19:21:09 +0000428
429 /// A StmtVisitor that propagates the raw counts through the AST and
430 /// records the count at statements where the value may change.
431 struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> {
432 /// PGO state.
433 CodeGenPGO &PGO;
434
435 /// A flag that is set when the current count should be recorded on the
436 /// next statement, such as at the exit of a loop.
437 bool RecordNextStmtCount;
438
439 /// The map of statements to count values.
440 llvm::DenseMap<const Stmt*, uint64_t> *CountMap;
441
442 /// BreakContinueStack - Keep counts of breaks and continues inside loops.
443 struct BreakContinue {
444 uint64_t BreakCount;
445 uint64_t ContinueCount;
446 BreakContinue() : BreakCount(0), ContinueCount(0) {}
447 };
448 SmallVector<BreakContinue, 8> BreakContinueStack;
449
450 ComputeRegionCounts(llvm::DenseMap<const Stmt*, uint64_t> *CountMap,
451 CodeGenPGO &PGO) :
452 PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {
453 }
454
455 void RecordStmtCount(const Stmt *S) {
456 if (RecordNextStmtCount) {
457 (*CountMap)[S] = PGO.getCurrentRegionCount();
458 RecordNextStmtCount = false;
459 }
460 }
461
462 void VisitStmt(const Stmt *S) {
463 RecordStmtCount(S);
464 for (Stmt::const_child_range I = S->children(); I; ++I) {
465 if (*I)
466 this->Visit(*I);
467 }
468 }
469
470 void VisitFunctionDecl(const FunctionDecl *S) {
471 RegionCounter Cnt(PGO, S->getBody());
472 Cnt.beginRegion();
473 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
474 Visit(S->getBody());
475 }
476
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000477 void VisitObjCMethodDecl(const ObjCMethodDecl *S) {
478 RegionCounter Cnt(PGO, S->getBody());
479 Cnt.beginRegion();
480 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
481 Visit(S->getBody());
482 }
483
Bob Wilsonbf854f02014-02-17 19:21:09 +0000484 void VisitReturnStmt(const ReturnStmt *S) {
485 RecordStmtCount(S);
486 if (S->getRetValue())
487 Visit(S->getRetValue());
488 PGO.setCurrentRegionUnreachable();
489 RecordNextStmtCount = true;
490 }
491
492 void VisitGotoStmt(const GotoStmt *S) {
493 RecordStmtCount(S);
494 PGO.setCurrentRegionUnreachable();
495 RecordNextStmtCount = true;
496 }
497
498 void VisitLabelStmt(const LabelStmt *S) {
499 RecordNextStmtCount = false;
500 RegionCounter Cnt(PGO, S);
501 Cnt.beginRegion();
502 (*CountMap)[S] = PGO.getCurrentRegionCount();
503 Visit(S->getSubStmt());
504 }
505
506 void VisitBreakStmt(const BreakStmt *S) {
507 RecordStmtCount(S);
508 assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
509 BreakContinueStack.back().BreakCount += PGO.getCurrentRegionCount();
510 PGO.setCurrentRegionUnreachable();
511 RecordNextStmtCount = true;
512 }
513
514 void VisitContinueStmt(const ContinueStmt *S) {
515 RecordStmtCount(S);
516 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
517 BreakContinueStack.back().ContinueCount += PGO.getCurrentRegionCount();
518 PGO.setCurrentRegionUnreachable();
519 RecordNextStmtCount = true;
520 }
521
522 void VisitWhileStmt(const WhileStmt *S) {
523 RecordStmtCount(S);
524 RegionCounter Cnt(PGO, S);
525 BreakContinueStack.push_back(BreakContinue());
526 // Visit the body region first so the break/continue adjustments can be
527 // included when visiting the condition.
528 Cnt.beginRegion();
529 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
530 Visit(S->getBody());
531 Cnt.adjustForControlFlow();
532
533 // ...then go back and propagate counts through the condition. The count
534 // at the start of the condition is the sum of the incoming edges,
535 // the backedge from the end of the loop body, and the edges from
536 // continue statements.
537 BreakContinue BC = BreakContinueStack.pop_back_val();
538 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
539 Cnt.getAdjustedCount() + BC.ContinueCount);
540 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
541 Visit(S->getCond());
542 Cnt.adjustForControlFlow();
543 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
544 RecordNextStmtCount = true;
545 }
546
547 void VisitDoStmt(const DoStmt *S) {
548 RecordStmtCount(S);
549 RegionCounter Cnt(PGO, S);
550 BreakContinueStack.push_back(BreakContinue());
551 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
552 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
553 Visit(S->getBody());
554 Cnt.adjustForControlFlow();
555
556 BreakContinue BC = BreakContinueStack.pop_back_val();
557 // The count at the start of the condition is equal to the count at the
558 // end of the body. The adjusted count does not include either the
559 // fall-through count coming into the loop or the continue count, so add
560 // both of those separately. This is coincidentally the same equation as
561 // with while loops but for different reasons.
562 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
563 Cnt.getAdjustedCount() + BC.ContinueCount);
564 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
565 Visit(S->getCond());
566 Cnt.adjustForControlFlow();
567 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
568 RecordNextStmtCount = true;
569 }
570
571 void VisitForStmt(const ForStmt *S) {
572 RecordStmtCount(S);
573 if (S->getInit())
574 Visit(S->getInit());
575 RegionCounter Cnt(PGO, S);
576 BreakContinueStack.push_back(BreakContinue());
577 // Visit the body region first. (This is basically the same as a while
578 // loop; see further comments in VisitWhileStmt.)
579 Cnt.beginRegion();
580 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
581 Visit(S->getBody());
582 Cnt.adjustForControlFlow();
583
584 // The increment is essentially part of the body but it needs to include
585 // the count for all the continue statements.
586 if (S->getInc()) {
587 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
588 BreakContinueStack.back().ContinueCount);
589 (*CountMap)[S->getInc()] = PGO.getCurrentRegionCount();
590 Visit(S->getInc());
591 Cnt.adjustForControlFlow();
592 }
593
594 BreakContinue BC = BreakContinueStack.pop_back_val();
595
596 // ...then go back and propagate counts through the condition.
597 if (S->getCond()) {
598 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
599 Cnt.getAdjustedCount() +
600 BC.ContinueCount);
601 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
602 Visit(S->getCond());
603 Cnt.adjustForControlFlow();
604 }
605 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
606 RecordNextStmtCount = true;
607 }
608
609 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
610 RecordStmtCount(S);
611 Visit(S->getRangeStmt());
612 Visit(S->getBeginEndStmt());
613 RegionCounter Cnt(PGO, S);
614 BreakContinueStack.push_back(BreakContinue());
615 // Visit the body region first. (This is basically the same as a while
616 // loop; see further comments in VisitWhileStmt.)
617 Cnt.beginRegion();
618 (*CountMap)[S->getLoopVarStmt()] = PGO.getCurrentRegionCount();
619 Visit(S->getLoopVarStmt());
620 Visit(S->getBody());
621 Cnt.adjustForControlFlow();
622
623 // The increment is essentially part of the body but it needs to include
624 // the count for all the continue statements.
625 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
626 BreakContinueStack.back().ContinueCount);
627 (*CountMap)[S->getInc()] = PGO.getCurrentRegionCount();
628 Visit(S->getInc());
629 Cnt.adjustForControlFlow();
630
631 BreakContinue BC = BreakContinueStack.pop_back_val();
632
633 // ...then go back and propagate counts through the condition.
634 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
635 Cnt.getAdjustedCount() +
636 BC.ContinueCount);
637 (*CountMap)[S->getCond()] = PGO.getCurrentRegionCount();
638 Visit(S->getCond());
639 Cnt.adjustForControlFlow();
640 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
641 RecordNextStmtCount = true;
642 }
643
644 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
645 RecordStmtCount(S);
646 Visit(S->getElement());
647 RegionCounter Cnt(PGO, S);
648 BreakContinueStack.push_back(BreakContinue());
649 Cnt.beginRegion();
650 (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
651 Visit(S->getBody());
652 BreakContinue BC = BreakContinueStack.pop_back_val();
653 Cnt.adjustForControlFlow();
654 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
655 RecordNextStmtCount = true;
656 }
657
658 void VisitSwitchStmt(const SwitchStmt *S) {
659 RecordStmtCount(S);
660 Visit(S->getCond());
661 PGO.setCurrentRegionUnreachable();
662 BreakContinueStack.push_back(BreakContinue());
663 Visit(S->getBody());
664 // If the switch is inside a loop, add the continue counts.
665 BreakContinue BC = BreakContinueStack.pop_back_val();
666 if (!BreakContinueStack.empty())
667 BreakContinueStack.back().ContinueCount += BC.ContinueCount;
668 RegionCounter ExitCnt(PGO, S);
669 ExitCnt.beginRegion();
670 RecordNextStmtCount = true;
671 }
672
673 void VisitCaseStmt(const CaseStmt *S) {
674 RecordNextStmtCount = false;
675 RegionCounter Cnt(PGO, S);
676 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
677 (*CountMap)[S] = Cnt.getCount();
678 RecordNextStmtCount = true;
679 Visit(S->getSubStmt());
680 }
681
682 void VisitDefaultStmt(const DefaultStmt *S) {
683 RecordNextStmtCount = false;
684 RegionCounter Cnt(PGO, S);
685 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
686 (*CountMap)[S] = Cnt.getCount();
687 RecordNextStmtCount = true;
688 Visit(S->getSubStmt());
689 }
690
691 void VisitIfStmt(const IfStmt *S) {
692 RecordStmtCount(S);
693 RegionCounter Cnt(PGO, S);
694 Visit(S->getCond());
695
696 Cnt.beginRegion();
697 (*CountMap)[S->getThen()] = PGO.getCurrentRegionCount();
698 Visit(S->getThen());
699 Cnt.adjustForControlFlow();
700
701 if (S->getElse()) {
702 Cnt.beginElseRegion();
703 (*CountMap)[S->getElse()] = PGO.getCurrentRegionCount();
704 Visit(S->getElse());
705 Cnt.adjustForControlFlow();
706 }
707 Cnt.applyAdjustmentsToRegion(0);
708 RecordNextStmtCount = true;
709 }
710
711 void VisitCXXTryStmt(const CXXTryStmt *S) {
712 RecordStmtCount(S);
713 Visit(S->getTryBlock());
714 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
715 Visit(S->getHandler(I));
716 RegionCounter Cnt(PGO, S);
717 Cnt.beginRegion();
718 RecordNextStmtCount = true;
719 }
720
721 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
722 RecordNextStmtCount = false;
723 RegionCounter Cnt(PGO, S);
724 Cnt.beginRegion();
725 (*CountMap)[S] = PGO.getCurrentRegionCount();
726 Visit(S->getHandlerBlock());
727 }
728
729 void VisitConditionalOperator(const ConditionalOperator *E) {
730 RecordStmtCount(E);
731 RegionCounter Cnt(PGO, E);
732 Visit(E->getCond());
733
734 Cnt.beginRegion();
735 (*CountMap)[E->getTrueExpr()] = PGO.getCurrentRegionCount();
736 Visit(E->getTrueExpr());
737 Cnt.adjustForControlFlow();
738
739 Cnt.beginElseRegion();
740 (*CountMap)[E->getFalseExpr()] = PGO.getCurrentRegionCount();
741 Visit(E->getFalseExpr());
742 Cnt.adjustForControlFlow();
743
744 Cnt.applyAdjustmentsToRegion(0);
745 RecordNextStmtCount = true;
746 }
747
748 void VisitBinLAnd(const BinaryOperator *E) {
749 RecordStmtCount(E);
750 RegionCounter Cnt(PGO, E);
751 Visit(E->getLHS());
752 Cnt.beginRegion();
753 (*CountMap)[E->getRHS()] = PGO.getCurrentRegionCount();
754 Visit(E->getRHS());
755 Cnt.adjustForControlFlow();
756 Cnt.applyAdjustmentsToRegion(0);
757 RecordNextStmtCount = true;
758 }
759
760 void VisitBinLOr(const BinaryOperator *E) {
761 RecordStmtCount(E);
762 RegionCounter Cnt(PGO, E);
763 Visit(E->getLHS());
764 Cnt.beginRegion();
765 (*CountMap)[E->getRHS()] = PGO.getCurrentRegionCount();
766 Visit(E->getRHS());
767 Cnt.adjustForControlFlow();
768 Cnt.applyAdjustmentsToRegion(0);
769 RecordNextStmtCount = true;
770 }
771 };
Justin Bogneref512b92014-01-06 22:27:43 +0000772}
773
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000774void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) {
Justin Bogneref512b92014-01-06 22:27:43 +0000775 bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate;
776 PGOProfileData *PGOData = CGM.getPGOData();
777 if (!InstrumentRegions && !PGOData)
778 return;
Justin Bogneref512b92014-01-06 22:27:43 +0000779 if (!D)
780 return;
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000781 setFuncName(Fn);
Justin Bogneref512b92014-01-06 22:27:43 +0000782 mapRegionCounters(D);
783 if (InstrumentRegions)
784 emitCounterVariables();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000785 if (PGOData) {
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000786 loadRegionCounts(PGOData);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000787 computeRegionCounts(D);
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000788
789 // Turn on InlineHint attribute for hot functions.
790 if (PGOData->isHotFunction(getFuncName()))
791 Fn->addFnAttr(llvm::Attribute::InlineHint);
792 // Turn on Cold attribute for cold functions.
793 else if (PGOData->isColdFunction(getFuncName()))
794 Fn->addFnAttr(llvm::Attribute::Cold);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000795 }
Justin Bogneref512b92014-01-06 22:27:43 +0000796}
797
798void CodeGenPGO::mapRegionCounters(const Decl *D) {
799 RegionCounterMap = new llvm::DenseMap<const Stmt*, unsigned>();
800 MapRegionCounters Walker(RegionCounterMap);
801 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
802 Walker.VisitFunctionDecl(FD);
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000803 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
804 Walker.VisitObjCMethodDecl(MD);
Justin Bogneref512b92014-01-06 22:27:43 +0000805 NumRegionCounters = Walker.NextCounter;
806}
807
Bob Wilsonbf854f02014-02-17 19:21:09 +0000808void CodeGenPGO::computeRegionCounts(const Decl *D) {
809 StmtCountMap = new llvm::DenseMap<const Stmt*, uint64_t>();
810 ComputeRegionCounts Walker(StmtCountMap, *this);
811 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
812 Walker.VisitFunctionDecl(FD);
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000813 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
814 Walker.VisitObjCMethodDecl(MD);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000815}
816
Justin Bogneref512b92014-01-06 22:27:43 +0000817void CodeGenPGO::emitCounterVariables() {
818 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
819 llvm::ArrayType *CounterTy = llvm::ArrayType::get(llvm::Type::getInt64Ty(Ctx),
820 NumRegionCounters);
821 RegionCounters =
822 new llvm::GlobalVariable(CGM.getModule(), CounterTy, false,
823 llvm::GlobalVariable::PrivateLinkage,
824 llvm::Constant::getNullValue(CounterTy),
825 "__llvm_pgo_ctr");
826}
827
828void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) {
Bob Wilson749ebc72014-03-06 04:55:28 +0000829 if (!RegionCounters)
Justin Bogneref512b92014-01-06 22:27:43 +0000830 return;
831 llvm::Value *Addr =
832 Builder.CreateConstInBoundsGEP2_64(RegionCounters, 0, Counter);
833 llvm::Value *Count = Builder.CreateLoad(Addr, "pgocount");
834 Count = Builder.CreateAdd(Count, Builder.getInt64(1));
835 Builder.CreateStore(Count, Addr);
836}
837
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000838void CodeGenPGO::loadRegionCounts(PGOProfileData *PGOData) {
Justin Bogneref512b92014-01-06 22:27:43 +0000839 // For now, ignore the counts from the PGO data file only if the number of
840 // counters does not match. This could be tightened down in the future to
841 // ignore counts when the input changes in various ways, e.g., by comparing a
842 // hash value based on some characteristics of the input.
843 RegionCounts = new std::vector<uint64_t>();
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000844 if (PGOData->getFunctionCounts(getFuncName(), *RegionCounts) ||
Justin Bogneref512b92014-01-06 22:27:43 +0000845 RegionCounts->size() != NumRegionCounters) {
846 delete RegionCounts;
847 RegionCounts = 0;
848 }
849}
850
851void CodeGenPGO::destroyRegionCounters() {
852 if (RegionCounterMap != 0)
853 delete RegionCounterMap;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000854 if (StmtCountMap != 0)
855 delete StmtCountMap;
Justin Bogneref512b92014-01-06 22:27:43 +0000856 if (RegionCounts != 0)
857 delete RegionCounts;
858}
859
860llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount,
861 uint64_t FalseCount) {
862 if (!TrueCount && !FalseCount)
863 return 0;
864
865 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
866 // TODO: need to scale down to 32-bits
867 // According to Laplace's Rule of Succession, it is better to compute the
868 // weight based on the count plus 1.
869 return MDHelper.createBranchWeights(TrueCount + 1, FalseCount + 1);
870}
871
Bob Wilson95a27b02014-02-17 19:20:59 +0000872llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) {
Justin Bogneref512b92014-01-06 22:27:43 +0000873 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
874 // TODO: need to scale down to 32-bits, instead of just truncating.
875 // According to Laplace's Rule of Succession, it is better to compute the
876 // weight based on the count plus 1.
877 SmallVector<uint32_t, 16> ScaledWeights;
878 ScaledWeights.reserve(Weights.size());
879 for (ArrayRef<uint64_t>::iterator WI = Weights.begin(), WE = Weights.end();
880 WI != WE; ++WI) {
881 ScaledWeights.push_back(*WI + 1);
882 }
883 return MDHelper.createBranchWeights(ScaledWeights);
884}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000885
886llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond,
887 RegionCounter &Cnt) {
888 if (!haveRegionCounts())
889 return 0;
890 uint64_t LoopCount = Cnt.getCount();
891 uint64_t CondCount = 0;
892 bool Found = getStmtCount(Cond, CondCount);
893 assert(Found && "missing expected loop condition count");
894 (void)Found;
895 if (CondCount == 0)
896 return 0;
897 return createBranchWeights(LoopCount,
898 std::max(CondCount, LoopCount) - LoopCount);
899}