blob: 38f21626e4e816221b3f5794d4d4f8b46a6e4542 [file] [log] [blame]
Justin Bogneref512b92014-01-06 22:27:43 +00001//===--- CodeGenPGO.cpp - PGO Instrumentation for LLVM CodeGen --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Instrumentation-based profile-guided optimization
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenPGO.h"
15#include "CodeGenFunction.h"
Alex Lorenzee024992014-08-04 18:41:51 +000016#include "CoverageMappingGen.h"
Justin Bogneref512b92014-01-06 22:27:43 +000017#include "clang/AST/RecursiveASTVisitor.h"
18#include "clang/AST/StmtVisitor.h"
19#include "llvm/IR/MDBuilder.h"
Justin Bogner837a6f62014-04-18 21:52:00 +000020#include "llvm/ProfileData/InstrProfReader.h"
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +000021#include "llvm/Support/Endian.h"
Justin Bogneref512b92014-01-06 22:27:43 +000022#include "llvm/Support/FileSystem.h"
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +000023#include "llvm/Support/MD5.h"
Justin Bogneref512b92014-01-06 22:27:43 +000024
25using namespace clang;
26using namespace CodeGen;
27
Alex Lorenzee024992014-08-04 18:41:51 +000028void CodeGenPGO::setFuncName(StringRef Name,
29 llvm::GlobalValue::LinkageTypes Linkage) {
Justin Bogner111c6532014-12-02 23:15:30 +000030 StringRef RawFuncName = Name;
Bob Wilsonda1ebed2014-03-06 04:55:41 +000031
32 // Function names may be prefixed with a binary '1' to indicate
33 // that the backend should not modify the symbols due to any platform
34 // naming convention. Do not include that '1' in the PGO profile name.
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000035 if (RawFuncName[0] == '\1')
36 RawFuncName = RawFuncName.substr(1);
Bob Wilsonda1ebed2014-03-06 04:55:41 +000037
Justin Bogner111c6532014-12-02 23:15:30 +000038 FuncName = RawFuncName;
39 if (llvm::GlobalValue::isLocalLinkage(Linkage)) {
40 // For local symbols, prepend the main file name to distinguish them.
41 // Do not include the full path in the file name since there's no guarantee
42 // that it will stay the same, e.g., if the files are checked out from
43 // version control in different locations.
44 if (CGM.getCodeGenOpts().MainFileName.empty())
45 FuncName = FuncName.insert(0, "<unknown>:");
46 else
47 FuncName = FuncName.insert(0, CGM.getCodeGenOpts().MainFileName + ":");
Bob Wilsonda1ebed2014-03-06 04:55:41 +000048 }
Bob Wilsonda1ebed2014-03-06 04:55:41 +000049}
50
Alex Lorenzee024992014-08-04 18:41:51 +000051void CodeGenPGO::setFuncName(llvm::Function *Fn) {
52 setFuncName(Fn->getName(), Fn->getLinkage());
53}
54
55void CodeGenPGO::setVarLinkage(llvm::GlobalValue::LinkageTypes Linkage) {
56 // Set the linkage for variables based on the function linkage. Usually, we
57 // want to match it, but available_externally and extern_weak both have the
58 // wrong semantics.
59 VarLinkage = Linkage;
60 switch (VarLinkage) {
61 case llvm::GlobalValue::ExternalWeakLinkage:
62 VarLinkage = llvm::GlobalValue::LinkOnceAnyLinkage;
63 break;
64 case llvm::GlobalValue::AvailableExternallyLinkage:
65 VarLinkage = llvm::GlobalValue::LinkOnceODRLinkage;
66 break;
67 default:
68 break;
69 }
70}
71
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000072static llvm::Function *getRegisterFunc(CodeGenModule &CGM) {
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +000073 return CGM.getModule().getFunction("__llvm_profile_register_functions");
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000074}
75
76static llvm::BasicBlock *getOrInsertRegisterBB(CodeGenModule &CGM) {
Duncan P. N. Exon Smith780443e2014-03-20 03:57:11 +000077 // Don't do this for Darwin. compiler-rt uses linker magic.
78 if (CGM.getTarget().getTriple().isOSDarwin())
79 return nullptr;
80
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000081 // Only need to insert this once per module.
82 if (llvm::Function *RegisterF = getRegisterFunc(CGM))
83 return &RegisterF->getEntryBlock();
84
85 // Construct the function.
86 auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
87 auto *RegisterFTy = llvm::FunctionType::get(VoidTy, false);
88 auto *RegisterF = llvm::Function::Create(RegisterFTy,
89 llvm::GlobalValue::InternalLinkage,
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +000090 "__llvm_profile_register_functions",
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000091 &CGM.getModule());
92 RegisterF->setUnnamedAddr(true);
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000093 if (CGM.getCodeGenOpts().DisableRedZone)
94 RegisterF->addFnAttr(llvm::Attribute::NoRedZone);
95
96 // Construct and return the entry block.
97 auto *BB = llvm::BasicBlock::Create(CGM.getLLVMContext(), "", RegisterF);
98 CGBuilderTy Builder(BB);
99 Builder.CreateRetVoid();
100 return BB;
101}
102
103static llvm::Constant *getOrInsertRuntimeRegister(CodeGenModule &CGM) {
104 auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
105 auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
106 auto *RuntimeRegisterTy = llvm::FunctionType::get(VoidTy, VoidPtrTy, false);
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +0000107 return CGM.getModule().getOrInsertFunction("__llvm_profile_register_function",
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000108 RuntimeRegisterTy);
109}
110
Duncan P. N. Exon Smith7134d472014-03-20 03:17:15 +0000111static bool isMachO(const CodeGenModule &CGM) {
112 return CGM.getTarget().getTriple().isOSBinFormatMachO();
113}
114
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000115static StringRef getCountersSection(const CodeGenModule &CGM) {
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +0000116 return isMachO(CGM) ? "__DATA,__llvm_prf_cnts" : "__llvm_prf_cnts";
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000117}
118
119static StringRef getNameSection(const CodeGenModule &CGM) {
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +0000120 return isMachO(CGM) ? "__DATA,__llvm_prf_names" : "__llvm_prf_names";
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000121}
122
123static StringRef getDataSection(const CodeGenModule &CGM) {
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +0000124 return isMachO(CGM) ? "__DATA,__llvm_prf_data" : "__llvm_prf_data";
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000125}
126
127llvm::GlobalVariable *CodeGenPGO::buildDataVar() {
128 // Create name variable.
129 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
130 auto *VarName = llvm::ConstantDataArray::getString(Ctx, getFuncName(),
131 false);
132 auto *Name = new llvm::GlobalVariable(CGM.getModule(), VarName->getType(),
Duncan P. N. Exon Smith73f78622014-03-20 22:49:50 +0000133 true, VarLinkage, VarName,
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000134 getFuncVarName("name"));
135 Name->setSection(getNameSection(CGM));
136 Name->setAlignment(1);
137
138 // Create data variable.
139 auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
Justin Bognerb4416f52014-03-18 21:58:06 +0000140 auto *Int64Ty = llvm::Type::getInt64Ty(Ctx);
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000141 auto *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx);
142 auto *Int64PtrTy = llvm::Type::getInt64PtrTy(Ctx);
Alex Lorenzee024992014-08-04 18:41:51 +0000143 llvm::GlobalVariable *Data = nullptr;
144 if (RegionCounters) {
145 llvm::Type *DataTypes[] = {
146 Int32Ty, Int32Ty, Int64Ty, Int8PtrTy, Int64PtrTy
147 };
148 auto *DataTy = llvm::StructType::get(Ctx, makeArrayRef(DataTypes));
149 llvm::Constant *DataVals[] = {
150 llvm::ConstantInt::get(Int32Ty, getFuncName().size()),
151 llvm::ConstantInt::get(Int32Ty, NumRegionCounters),
152 llvm::ConstantInt::get(Int64Ty, FunctionHash),
153 llvm::ConstantExpr::getBitCast(Name, Int8PtrTy),
154 llvm::ConstantExpr::getBitCast(RegionCounters, Int64PtrTy)
155 };
156 Data =
157 new llvm::GlobalVariable(CGM.getModule(), DataTy, true, VarLinkage,
158 llvm::ConstantStruct::get(DataTy, DataVals),
159 getFuncVarName("data"));
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000160
Alex Lorenzee024992014-08-04 18:41:51 +0000161 // All the data should be packed into an array in its own section.
162 Data->setSection(getDataSection(CGM));
163 Data->setAlignment(8);
164 }
165
166 // Create coverage mapping data variable.
167 if (!CoverageMapping.empty())
Alex Lorenz1d45c5b2014-08-21 19:25:27 +0000168 CGM.getCoverageMapping()->addFunctionMappingRecord(Name, getFuncName(),
169 FunctionHash,
Alex Lorenzee024992014-08-04 18:41:51 +0000170 CoverageMapping);
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000171
Duncan P. N. Exon Smith91212202014-05-16 01:24:00 +0000172 // Hide all these symbols so that we correctly get a copy for each
173 // executable. The profile format expects names and counters to be
174 // contiguous, so references into shared objects would be invalid.
175 if (!llvm::GlobalValue::isLocalLinkage(VarLinkage)) {
176 Name->setVisibility(llvm::GlobalValue::HiddenVisibility);
Alex Lorenzee024992014-08-04 18:41:51 +0000177 if (Data) {
178 Data->setVisibility(llvm::GlobalValue::HiddenVisibility);
179 RegionCounters->setVisibility(llvm::GlobalValue::HiddenVisibility);
180 }
Duncan P. N. Exon Smith91212202014-05-16 01:24:00 +0000181 }
182
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000183 // Make sure the data doesn't get deleted.
Alex Lorenzee024992014-08-04 18:41:51 +0000184 if (Data) CGM.addUsedGlobal(Data);
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000185 return Data;
186}
187
188void CodeGenPGO::emitInstrumentationData() {
Justin Bogner3212b182014-04-25 07:20:05 +0000189 if (!RegionCounters)
Justin Bogneref512b92014-01-06 22:27:43 +0000190 return;
191
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000192 // Build the data.
193 auto *Data = buildDataVar();
Justin Bogneref512b92014-01-06 22:27:43 +0000194
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000195 // Register the data.
Duncan P. N. Exon Smith780443e2014-03-20 03:57:11 +0000196 auto *RegisterBB = getOrInsertRegisterBB(CGM);
197 if (!RegisterBB)
198 return;
199 CGBuilderTy Builder(RegisterBB->getTerminator());
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000200 auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
201 Builder.CreateCall(getOrInsertRuntimeRegister(CGM),
202 Builder.CreateBitCast(Data, VoidPtrTy));
Justin Bogneref512b92014-01-06 22:27:43 +0000203}
204
205llvm::Function *CodeGenPGO::emitInitialization(CodeGenModule &CGM) {
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000206 if (!CGM.getCodeGenOpts().ProfileInstrGenerate)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000207 return nullptr;
Justin Bogneref512b92014-01-06 22:27:43 +0000208
Justin Bognerf2ea7752014-04-10 18:13:13 +0000209 assert(CGM.getModule().getFunction("__llvm_profile_init") == nullptr &&
210 "profile initialization already emitted");
Justin Bogneref512b92014-01-06 22:27:43 +0000211
Duncan P. N. Exon Smith5188e912014-03-20 19:23:46 +0000212 // Get the function to call at initialization.
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000213 llvm::Constant *RegisterF = getRegisterFunc(CGM);
Duncan P. N. Exon Smith5188e912014-03-20 19:23:46 +0000214 if (!RegisterF)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000215 return nullptr;
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000216
217 // Create the initialization function.
218 auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
219 auto *F = llvm::Function::Create(llvm::FunctionType::get(VoidTy, false),
220 llvm::GlobalValue::InternalLinkage,
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +0000221 "__llvm_profile_init", &CGM.getModule());
Justin Bogneref512b92014-01-06 22:27:43 +0000222 F->setUnnamedAddr(true);
Justin Bogneref512b92014-01-06 22:27:43 +0000223 F->addFnAttr(llvm::Attribute::NoInline);
224 if (CGM.getCodeGenOpts().DisableRedZone)
225 F->addFnAttr(llvm::Attribute::NoRedZone);
226
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000227 // Add the basic block and the necessary calls.
228 CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", F));
Duncan P. N. Exon Smith5188e912014-03-20 19:23:46 +0000229 Builder.CreateCall(RegisterF);
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000230 Builder.CreateRetVoid();
Justin Bogneref512b92014-01-06 22:27:43 +0000231
232 return F;
233}
234
235namespace {
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000236/// \brief Stable hasher for PGO region counters.
237///
238/// PGOHash produces a stable hash of a given function's control flow.
239///
240/// Changing the output of this hash will invalidate all previously generated
241/// profiles -- i.e., don't do it.
242///
243/// \note When this hash does eventually change (years?), we still need to
244/// support old hashes. We'll need to pull in the version number from the
245/// profile data format and use the matching hash function.
246class PGOHash {
247 uint64_t Working;
248 unsigned Count;
249 llvm::MD5 MD5;
250
251 static const int NumBitsPerType = 6;
252 static const unsigned NumTypesPerWord = sizeof(uint64_t) * 8 / NumBitsPerType;
253 static const unsigned TooBig = 1u << NumBitsPerType;
254
255public:
256 /// \brief Hash values for AST nodes.
257 ///
258 /// Distinct values for AST nodes that have region counters attached.
259 ///
260 /// These values must be stable. All new members must be added at the end,
261 /// and no members should be removed. Changing the enumeration value for an
262 /// AST node will affect the hash of every function that contains that node.
263 enum HashType : unsigned char {
264 None = 0,
265 LabelStmt = 1,
266 WhileStmt,
267 DoStmt,
268 ForStmt,
269 CXXForRangeStmt,
270 ObjCForCollectionStmt,
271 SwitchStmt,
272 CaseStmt,
273 DefaultStmt,
274 IfStmt,
275 CXXTryStmt,
276 CXXCatchStmt,
277 ConditionalOperator,
278 BinaryOperatorLAnd,
279 BinaryOperatorLOr,
280 BinaryConditionalOperator,
281
282 // Keep this last. It's for the static assert that follows.
283 LastHashType
284 };
285 static_assert(LastHashType <= TooBig, "Too many types in HashType");
286
287 // TODO: When this format changes, take in a version number here, and use the
288 // old hash calculation for file formats that used the old hash.
289 PGOHash() : Working(0), Count(0) {}
290 void combine(HashType Type);
291 uint64_t finalize();
292};
293const int PGOHash::NumBitsPerType;
294const unsigned PGOHash::NumTypesPerWord;
295const unsigned PGOHash::TooBig;
296
Bob Wilsond8931422014-04-11 17:16:13 +0000297 /// A RecursiveASTVisitor that fills a map of statements to PGO counters.
298 struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
Justin Bogneref512b92014-01-06 22:27:43 +0000299 /// The next counter value to assign.
300 unsigned NextCounter;
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000301 /// The function hash.
302 PGOHash Hash;
Justin Bogneref512b92014-01-06 22:27:43 +0000303 /// The map of statements to counters.
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000304 llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
Justin Bogneref512b92014-01-06 22:27:43 +0000305
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000306 MapRegionCounters(llvm::DenseMap<const Stmt *, unsigned> &CounterMap)
307 : NextCounter(0), CounterMap(CounterMap) {}
Justin Bogneref512b92014-01-06 22:27:43 +0000308
Justin Bogner191ec632014-04-11 23:06:35 +0000309 // Blocks and lambdas are handled as separate functions, so we need not
310 // traverse them in the parent context.
311 bool TraverseBlockExpr(BlockExpr *BE) { return true; }
312 bool TraverseLambdaBody(LambdaExpr *LE) { return true; }
Justin Bogner81ab90f2014-04-15 00:50:54 +0000313 bool TraverseCapturedStmt(CapturedStmt *CS) { return true; }
Justin Bogneref512b92014-01-06 22:27:43 +0000314
Bob Wilsond8931422014-04-11 17:16:13 +0000315 bool VisitDecl(const Decl *D) {
316 switch (D->getKind()) {
317 default:
318 break;
319 case Decl::Function:
320 case Decl::CXXMethod:
321 case Decl::CXXConstructor:
322 case Decl::CXXDestructor:
323 case Decl::CXXConversion:
324 case Decl::ObjCMethod:
325 case Decl::Block:
Justin Bogner81ab90f2014-04-15 00:50:54 +0000326 case Decl::Captured:
Bob Wilsond8931422014-04-11 17:16:13 +0000327 CounterMap[D->getBody()] = NextCounter++;
328 break;
329 }
330 return true;
Justin Bogneref512b92014-01-06 22:27:43 +0000331 }
Bob Wilsond8931422014-04-11 17:16:13 +0000332
333 bool VisitStmt(const Stmt *S) {
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000334 auto Type = getHashType(S);
335 if (Type == PGOHash::None)
336 return true;
337
338 CounterMap[S] = NextCounter++;
339 Hash.combine(Type);
340 return true;
341 }
342 PGOHash::HashType getHashType(const Stmt *S) {
Bob Wilsond8931422014-04-11 17:16:13 +0000343 switch (S->getStmtClass()) {
344 default:
345 break;
346 case Stmt::LabelStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000347 return PGOHash::LabelStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000348 case Stmt::WhileStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000349 return PGOHash::WhileStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000350 case Stmt::DoStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000351 return PGOHash::DoStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000352 case Stmt::ForStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000353 return PGOHash::ForStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000354 case Stmt::CXXForRangeStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000355 return PGOHash::CXXForRangeStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000356 case Stmt::ObjCForCollectionStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000357 return PGOHash::ObjCForCollectionStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000358 case Stmt::SwitchStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000359 return PGOHash::SwitchStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000360 case Stmt::CaseStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000361 return PGOHash::CaseStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000362 case Stmt::DefaultStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000363 return PGOHash::DefaultStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000364 case Stmt::IfStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000365 return PGOHash::IfStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000366 case Stmt::CXXTryStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000367 return PGOHash::CXXTryStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000368 case Stmt::CXXCatchStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000369 return PGOHash::CXXCatchStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000370 case Stmt::ConditionalOperatorClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000371 return PGOHash::ConditionalOperator;
Bob Wilsond8931422014-04-11 17:16:13 +0000372 case Stmt::BinaryConditionalOperatorClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000373 return PGOHash::BinaryConditionalOperator;
Bob Wilsond8931422014-04-11 17:16:13 +0000374 case Stmt::BinaryOperatorClass: {
375 const BinaryOperator *BO = cast<BinaryOperator>(S);
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000376 if (BO->getOpcode() == BO_LAnd)
377 return PGOHash::BinaryOperatorLAnd;
378 if (BO->getOpcode() == BO_LOr)
379 return PGOHash::BinaryOperatorLOr;
Bob Wilsond8931422014-04-11 17:16:13 +0000380 break;
381 }
382 }
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000383 return PGOHash::None;
Justin Bogneref512b92014-01-06 22:27:43 +0000384 }
385 };
Bob Wilsonbf854f02014-02-17 19:21:09 +0000386
387 /// A StmtVisitor that propagates the raw counts through the AST and
388 /// records the count at statements where the value may change.
389 struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> {
390 /// PGO state.
391 CodeGenPGO &PGO;
392
393 /// A flag that is set when the current count should be recorded on the
394 /// next statement, such as at the exit of a loop.
395 bool RecordNextStmtCount;
396
397 /// The map of statements to count values.
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000398 llvm::DenseMap<const Stmt *, uint64_t> &CountMap;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000399
Justin Bognerfc83c112014-04-18 21:51:09 +0000400 /// BreakContinueStack - Keep counts of breaks and continues inside loops.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000401 struct BreakContinue {
402 uint64_t BreakCount;
403 uint64_t ContinueCount;
404 BreakContinue() : BreakCount(0), ContinueCount(0) {}
405 };
406 SmallVector<BreakContinue, 8> BreakContinueStack;
407
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000408 ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &CountMap,
409 CodeGenPGO &PGO)
410 : PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000411
412 void RecordStmtCount(const Stmt *S) {
413 if (RecordNextStmtCount) {
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000414 CountMap[S] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000415 RecordNextStmtCount = false;
416 }
417 }
418
419 void VisitStmt(const Stmt *S) {
420 RecordStmtCount(S);
421 for (Stmt::const_child_range I = S->children(); I; ++I) {
422 if (*I)
423 this->Visit(*I);
424 }
425 }
426
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000427 void VisitFunctionDecl(const FunctionDecl *D) {
Bob Wilsond8931422014-04-11 17:16:13 +0000428 // Counter tracks entry to the function body.
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000429 RegionCounter Cnt(PGO, D->getBody());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000430 Cnt.beginRegion();
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000431 CountMap[D->getBody()] = PGO.getCurrentRegionCount();
432 Visit(D->getBody());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000433 }
434
Justin Bogner191ec632014-04-11 23:06:35 +0000435 // Skip lambda expressions. We visit these as FunctionDecls when we're
436 // generating them and aren't interested in the body when generating a
437 // parent context.
438 void VisitLambdaExpr(const LambdaExpr *LE) {}
439
Justin Bogner81ab90f2014-04-15 00:50:54 +0000440 void VisitCapturedDecl(const CapturedDecl *D) {
441 // Counter tracks entry to the capture body.
442 RegionCounter Cnt(PGO, D->getBody());
443 Cnt.beginRegion();
444 CountMap[D->getBody()] = PGO.getCurrentRegionCount();
445 Visit(D->getBody());
446 }
447
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000448 void VisitObjCMethodDecl(const ObjCMethodDecl *D) {
Bob Wilsond8931422014-04-11 17:16:13 +0000449 // Counter tracks entry to the method body.
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000450 RegionCounter Cnt(PGO, D->getBody());
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000451 Cnt.beginRegion();
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000452 CountMap[D->getBody()] = PGO.getCurrentRegionCount();
453 Visit(D->getBody());
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000454 }
455
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000456 void VisitBlockDecl(const BlockDecl *D) {
Bob Wilsond8931422014-04-11 17:16:13 +0000457 // Counter tracks entry to the block body.
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000458 RegionCounter Cnt(PGO, D->getBody());
Bob Wilsonc845c002014-03-06 20:24:27 +0000459 Cnt.beginRegion();
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000460 CountMap[D->getBody()] = PGO.getCurrentRegionCount();
461 Visit(D->getBody());
Bob Wilsonc845c002014-03-06 20:24:27 +0000462 }
463
Bob Wilsonbf854f02014-02-17 19:21:09 +0000464 void VisitReturnStmt(const ReturnStmt *S) {
465 RecordStmtCount(S);
466 if (S->getRetValue())
467 Visit(S->getRetValue());
468 PGO.setCurrentRegionUnreachable();
469 RecordNextStmtCount = true;
470 }
471
472 void VisitGotoStmt(const GotoStmt *S) {
473 RecordStmtCount(S);
474 PGO.setCurrentRegionUnreachable();
475 RecordNextStmtCount = true;
476 }
477
478 void VisitLabelStmt(const LabelStmt *S) {
479 RecordNextStmtCount = false;
Bob Wilsond8931422014-04-11 17:16:13 +0000480 // Counter tracks the block following the label.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000481 RegionCounter Cnt(PGO, S);
482 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000483 CountMap[S] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000484 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);
Bob Wilsond8931422014-04-11 17:16:13 +0000505 // Counter tracks the body of the loop.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000506 RegionCounter Cnt(PGO, S);
507 BreakContinueStack.push_back(BreakContinue());
508 // Visit the body region first so the break/continue adjustments can be
509 // included when visiting the condition.
510 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000511 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000512 Visit(S->getBody());
513 Cnt.adjustForControlFlow();
514
515 // ...then go back and propagate counts through the condition. The count
516 // at the start of the condition is the sum of the incoming edges,
517 // the backedge from the end of the loop body, and the edges from
518 // continue statements.
519 BreakContinue BC = BreakContinueStack.pop_back_val();
520 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
521 Cnt.getAdjustedCount() + BC.ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000522 CountMap[S->getCond()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000523 Visit(S->getCond());
524 Cnt.adjustForControlFlow();
525 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
526 RecordNextStmtCount = true;
527 }
528
529 void VisitDoStmt(const DoStmt *S) {
530 RecordStmtCount(S);
Bob Wilsond8931422014-04-11 17:16:13 +0000531 // Counter tracks the body of the loop.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000532 RegionCounter Cnt(PGO, S);
533 BreakContinueStack.push_back(BreakContinue());
534 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000535 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000536 Visit(S->getBody());
537 Cnt.adjustForControlFlow();
538
539 BreakContinue BC = BreakContinueStack.pop_back_val();
540 // The count at the start of the condition is equal to the count at the
541 // end of the body. The adjusted count does not include either the
542 // fall-through count coming into the loop or the continue count, so add
543 // both of those separately. This is coincidentally the same equation as
544 // with while loops but for different reasons.
545 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
546 Cnt.getAdjustedCount() + BC.ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000547 CountMap[S->getCond()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000548 Visit(S->getCond());
549 Cnt.adjustForControlFlow();
550 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
551 RecordNextStmtCount = true;
552 }
553
554 void VisitForStmt(const ForStmt *S) {
555 RecordStmtCount(S);
556 if (S->getInit())
557 Visit(S->getInit());
Bob Wilsond8931422014-04-11 17:16:13 +0000558 // Counter tracks the body of the loop.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000559 RegionCounter Cnt(PGO, S);
560 BreakContinueStack.push_back(BreakContinue());
561 // Visit the body region first. (This is basically the same as a while
562 // loop; see further comments in VisitWhileStmt.)
563 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000564 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000565 Visit(S->getBody());
566 Cnt.adjustForControlFlow();
567
568 // The increment is essentially part of the body but it needs to include
569 // the count for all the continue statements.
570 if (S->getInc()) {
571 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
572 BreakContinueStack.back().ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000573 CountMap[S->getInc()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000574 Visit(S->getInc());
575 Cnt.adjustForControlFlow();
576 }
577
578 BreakContinue BC = BreakContinueStack.pop_back_val();
579
580 // ...then go back and propagate counts through the condition.
581 if (S->getCond()) {
582 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
583 Cnt.getAdjustedCount() +
584 BC.ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000585 CountMap[S->getCond()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000586 Visit(S->getCond());
587 Cnt.adjustForControlFlow();
588 }
589 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
590 RecordNextStmtCount = true;
591 }
592
593 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
594 RecordStmtCount(S);
595 Visit(S->getRangeStmt());
596 Visit(S->getBeginEndStmt());
Bob Wilsond8931422014-04-11 17:16:13 +0000597 // Counter tracks the body of the loop.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000598 RegionCounter Cnt(PGO, S);
599 BreakContinueStack.push_back(BreakContinue());
600 // Visit the body region first. (This is basically the same as a while
601 // loop; see further comments in VisitWhileStmt.)
602 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000603 CountMap[S->getLoopVarStmt()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000604 Visit(S->getLoopVarStmt());
605 Visit(S->getBody());
606 Cnt.adjustForControlFlow();
607
608 // The increment is essentially part of the body but it needs to include
609 // the count for all the continue statements.
610 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
611 BreakContinueStack.back().ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000612 CountMap[S->getInc()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000613 Visit(S->getInc());
614 Cnt.adjustForControlFlow();
615
616 BreakContinue BC = BreakContinueStack.pop_back_val();
617
618 // ...then go back and propagate counts through the condition.
619 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
620 Cnt.getAdjustedCount() +
621 BC.ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000622 CountMap[S->getCond()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000623 Visit(S->getCond());
624 Cnt.adjustForControlFlow();
625 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
626 RecordNextStmtCount = true;
627 }
628
629 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
630 RecordStmtCount(S);
631 Visit(S->getElement());
Bob Wilsond8931422014-04-11 17:16:13 +0000632 // Counter tracks the body of the loop.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000633 RegionCounter Cnt(PGO, S);
634 BreakContinueStack.push_back(BreakContinue());
635 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000636 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000637 Visit(S->getBody());
638 BreakContinue BC = BreakContinueStack.pop_back_val();
639 Cnt.adjustForControlFlow();
640 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
641 RecordNextStmtCount = true;
642 }
643
644 void VisitSwitchStmt(const SwitchStmt *S) {
645 RecordStmtCount(S);
646 Visit(S->getCond());
647 PGO.setCurrentRegionUnreachable();
648 BreakContinueStack.push_back(BreakContinue());
649 Visit(S->getBody());
650 // If the switch is inside a loop, add the continue counts.
651 BreakContinue BC = BreakContinueStack.pop_back_val();
652 if (!BreakContinueStack.empty())
653 BreakContinueStack.back().ContinueCount += BC.ContinueCount;
Bob Wilsond8931422014-04-11 17:16:13 +0000654 // Counter tracks the exit block of the switch.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000655 RegionCounter ExitCnt(PGO, S);
656 ExitCnt.beginRegion();
657 RecordNextStmtCount = true;
658 }
659
660 void VisitCaseStmt(const CaseStmt *S) {
661 RecordNextStmtCount = false;
Bob Wilsond8931422014-04-11 17:16:13 +0000662 // Counter for this particular case. This counts only jumps from the
663 // switch header and does not include fallthrough from the case before
664 // this one.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000665 RegionCounter Cnt(PGO, S);
666 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000667 CountMap[S] = Cnt.getCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000668 RecordNextStmtCount = true;
669 Visit(S->getSubStmt());
670 }
671
672 void VisitDefaultStmt(const DefaultStmt *S) {
673 RecordNextStmtCount = false;
Bob Wilsond8931422014-04-11 17:16:13 +0000674 // Counter for this default case. This does not include fallthrough from
675 // the previous case.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000676 RegionCounter Cnt(PGO, S);
677 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000678 CountMap[S] = Cnt.getCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000679 RecordNextStmtCount = true;
680 Visit(S->getSubStmt());
681 }
682
683 void VisitIfStmt(const IfStmt *S) {
684 RecordStmtCount(S);
Bob Wilsond8931422014-04-11 17:16:13 +0000685 // Counter tracks the "then" part of an if statement. The count for
686 // the "else" part, if it exists, will be calculated from this counter.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000687 RegionCounter Cnt(PGO, S);
688 Visit(S->getCond());
689
690 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000691 CountMap[S->getThen()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000692 Visit(S->getThen());
693 Cnt.adjustForControlFlow();
694
695 if (S->getElse()) {
696 Cnt.beginElseRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000697 CountMap[S->getElse()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000698 Visit(S->getElse());
699 Cnt.adjustForControlFlow();
700 }
701 Cnt.applyAdjustmentsToRegion(0);
702 RecordNextStmtCount = true;
703 }
704
705 void VisitCXXTryStmt(const CXXTryStmt *S) {
706 RecordStmtCount(S);
707 Visit(S->getTryBlock());
708 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
709 Visit(S->getHandler(I));
Bob Wilsond8931422014-04-11 17:16:13 +0000710 // Counter tracks the continuation block of the try statement.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000711 RegionCounter Cnt(PGO, S);
712 Cnt.beginRegion();
713 RecordNextStmtCount = true;
714 }
715
716 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
717 RecordNextStmtCount = false;
Bob Wilsond8931422014-04-11 17:16:13 +0000718 // Counter tracks the catch statement's handler block.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000719 RegionCounter Cnt(PGO, S);
720 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000721 CountMap[S] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000722 Visit(S->getHandlerBlock());
723 }
724
Justin Bogner53c55d92014-04-11 06:10:10 +0000725 void VisitAbstractConditionalOperator(
726 const AbstractConditionalOperator *E) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000727 RecordStmtCount(E);
Bob Wilsond8931422014-04-11 17:16:13 +0000728 // Counter tracks the "true" part of a conditional operator. The
729 // count in the "false" part will be calculated from this counter.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000730 RegionCounter Cnt(PGO, E);
731 Visit(E->getCond());
732
733 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000734 CountMap[E->getTrueExpr()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000735 Visit(E->getTrueExpr());
736 Cnt.adjustForControlFlow();
737
738 Cnt.beginElseRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000739 CountMap[E->getFalseExpr()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000740 Visit(E->getFalseExpr());
741 Cnt.adjustForControlFlow();
742
743 Cnt.applyAdjustmentsToRegion(0);
744 RecordNextStmtCount = true;
745 }
746
747 void VisitBinLAnd(const BinaryOperator *E) {
748 RecordStmtCount(E);
Bob Wilsond8931422014-04-11 17:16:13 +0000749 // Counter tracks the right hand side of a logical and operator.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000750 RegionCounter Cnt(PGO, E);
751 Visit(E->getLHS());
752 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000753 CountMap[E->getRHS()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000754 Visit(E->getRHS());
755 Cnt.adjustForControlFlow();
756 Cnt.applyAdjustmentsToRegion(0);
757 RecordNextStmtCount = true;
758 }
759
760 void VisitBinLOr(const BinaryOperator *E) {
761 RecordStmtCount(E);
Bob Wilsond8931422014-04-11 17:16:13 +0000762 // Counter tracks the right hand side of a logical or operator.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000763 RegionCounter Cnt(PGO, E);
764 Visit(E->getLHS());
765 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000766 CountMap[E->getRHS()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000767 Visit(E->getRHS());
768 Cnt.adjustForControlFlow();
769 Cnt.applyAdjustmentsToRegion(0);
770 RecordNextStmtCount = true;
771 }
772 };
Justin Bogneref512b92014-01-06 22:27:43 +0000773}
774
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000775void PGOHash::combine(HashType Type) {
776 // Check that we never combine 0 and only have six bits.
777 assert(Type && "Hash is invalid: unexpected type 0");
778 assert(unsigned(Type) < TooBig && "Hash is invalid: too many types");
779
780 // Pass through MD5 if enough work has built up.
781 if (Count && Count % NumTypesPerWord == 0) {
782 using namespace llvm::support;
783 uint64_t Swapped = endian::byte_swap<uint64_t, little>(Working);
784 MD5.update(llvm::makeArrayRef((uint8_t *)&Swapped, sizeof(Swapped)));
785 Working = 0;
786 }
787
788 // Accumulate the current type.
789 ++Count;
790 Working = Working << NumBitsPerType | Type;
791}
792
793uint64_t PGOHash::finalize() {
794 // Use Working as the hash directly if we never used MD5.
795 if (Count <= NumTypesPerWord)
796 // No need to byte swap here, since none of the math was endian-dependent.
797 // This number will be byte-swapped as required on endianness transitions,
798 // so we will see the same value on the other side.
799 return Working;
800
801 // Check for remaining work in Working.
802 if (Working)
803 MD5.update(Working);
804
805 // Finalize the MD5 and return the hash.
806 llvm::MD5::MD5Result Result;
807 MD5.final(Result);
808 using namespace llvm::support;
809 return endian::read<uint64_t, little, unaligned>(Result);
810}
811
Duncan P. N. Exon Smithd971cd12014-03-28 17:53:22 +0000812static void emitRuntimeHook(CodeGenModule &CGM) {
Duncan P. N. Exon Smith3fefedb2014-04-11 00:43:16 +0000813 const char *const RuntimeVarName = "__llvm_profile_runtime";
814 const char *const RuntimeUserName = "__llvm_profile_runtime_user";
Duncan P. N. Exon Smithd971cd12014-03-28 17:53:22 +0000815 if (CGM.getModule().getGlobalVariable(RuntimeVarName))
816 return;
817
818 // Declare the runtime hook.
819 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
820 auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
821 auto *Var = new llvm::GlobalVariable(CGM.getModule(), Int32Ty, false,
822 llvm::GlobalValue::ExternalLinkage,
823 nullptr, RuntimeVarName);
824
825 // Make a function that uses it.
826 auto *User = llvm::Function::Create(llvm::FunctionType::get(Int32Ty, false),
827 llvm::GlobalValue::LinkOnceODRLinkage,
828 RuntimeUserName, &CGM.getModule());
829 User->addFnAttr(llvm::Attribute::NoInline);
830 if (CGM.getCodeGenOpts().DisableRedZone)
831 User->addFnAttr(llvm::Attribute::NoRedZone);
832 CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", User));
833 auto *Load = Builder.CreateLoad(Var);
834 Builder.CreateRet(Load);
835
836 // Create a use of the function. Now the definition of the runtime variable
837 // should get pulled in, along with any static initializears.
838 CGM.addUsedGlobal(User);
839}
840
Alex Lorenzee024992014-08-04 18:41:51 +0000841void CodeGenPGO::checkGlobalDecl(GlobalDecl GD) {
842 // Make sure we only emit coverage mapping for one constructor/destructor.
843 // Clang emits several functions for the constructor and the destructor of
844 // a class. Every function is instrumented, but we only want to provide
845 // coverage for one of them. Because of that we only emit the coverage mapping
846 // for the base constructor/destructor.
847 if ((isa<CXXConstructorDecl>(GD.getDecl()) &&
Alex Lorenzd4236742014-08-11 18:21:34 +0000848 GD.getCtorType() != Ctor_Base) ||
Alex Lorenzee024992014-08-04 18:41:51 +0000849 (isa<CXXDestructorDecl>(GD.getDecl()) &&
850 GD.getDtorType() != Dtor_Base)) {
851 SkipCoverageMapping = true;
852 }
853}
854
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000855void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) {
Justin Bogneref512b92014-01-06 22:27:43 +0000856 bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate;
Justin Bogner837a6f62014-04-18 21:52:00 +0000857 llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
858 if (!InstrumentRegions && !PGOReader)
Justin Bogneref512b92014-01-06 22:27:43 +0000859 return;
Justin Bogner3212b182014-04-25 07:20:05 +0000860 if (D->isImplicit())
Justin Bogneref512b92014-01-06 22:27:43 +0000861 return;
Alex Lorenzee024992014-08-04 18:41:51 +0000862 CGM.ClearUnusedCoverageMapping(D);
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000863 setFuncName(Fn);
Alex Lorenzee024992014-08-04 18:41:51 +0000864 setVarLinkage(Fn->getLinkage());
Duncan P. N. Exon Smith7c414512014-03-20 22:50:08 +0000865
Justin Bogneref512b92014-01-06 22:27:43 +0000866 mapRegionCounters(D);
Duncan P. N. Exon Smithd971cd12014-03-28 17:53:22 +0000867 if (InstrumentRegions) {
868 emitRuntimeHook(CGM);
Justin Bogneref512b92014-01-06 22:27:43 +0000869 emitCounterVariables();
Alex Lorenzee024992014-08-04 18:41:51 +0000870 if (CGM.getCodeGenOpts().CoverageMapping)
871 emitCounterRegionMapping(D);
Duncan P. N. Exon Smithd971cd12014-03-28 17:53:22 +0000872 }
Justin Bogner837a6f62014-04-18 21:52:00 +0000873 if (PGOReader) {
Justin Bogner40b8ba12014-06-26 01:45:07 +0000874 SourceManager &SM = CGM.getContext().getSourceManager();
875 loadRegionCounts(PGOReader, SM.isInMainFile(D->getLocation()));
Bob Wilsonbf854f02014-02-17 19:21:09 +0000876 computeRegionCounts(D);
Justin Bogner837a6f62014-04-18 21:52:00 +0000877 applyFunctionAttributes(PGOReader, Fn);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000878 }
Justin Bogneref512b92014-01-06 22:27:43 +0000879}
880
881void CodeGenPGO::mapRegionCounters(const Decl *D) {
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +0000882 RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000883 MapRegionCounters Walker(*RegionCounterMap);
Justin Bogneref512b92014-01-06 22:27:43 +0000884 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000885 Walker.TraverseDecl(const_cast<FunctionDecl *>(FD));
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000886 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000887 Walker.TraverseDecl(const_cast<ObjCMethodDecl *>(MD));
Bob Wilsonc845c002014-03-06 20:24:27 +0000888 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000889 Walker.TraverseDecl(const_cast<BlockDecl *>(BD));
Justin Bogner81ab90f2014-04-15 00:50:54 +0000890 else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
891 Walker.TraverseDecl(const_cast<CapturedDecl *>(CD));
Justin Bogner3212b182014-04-25 07:20:05 +0000892 assert(Walker.NextCounter > 0 && "no entry counter mapped for decl");
Justin Bogneref512b92014-01-06 22:27:43 +0000893 NumRegionCounters = Walker.NextCounter;
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000894 FunctionHash = Walker.Hash.finalize();
Justin Bogneref512b92014-01-06 22:27:43 +0000895}
896
Alex Lorenzee024992014-08-04 18:41:51 +0000897void CodeGenPGO::emitCounterRegionMapping(const Decl *D) {
898 if (SkipCoverageMapping)
899 return;
900 // Don't map the functions inside the system headers
901 auto Loc = D->getBody()->getLocStart();
902 if (CGM.getContext().getSourceManager().isInSystemHeader(Loc))
903 return;
904
905 llvm::raw_string_ostream OS(CoverageMapping);
906 CoverageMappingGen MappingGen(*CGM.getCoverageMapping(),
907 CGM.getContext().getSourceManager(),
Justin Bognere5ee6c52014-10-02 16:44:01 +0000908 CGM.getLangOpts(), RegionCounterMap.get());
Alex Lorenzee024992014-08-04 18:41:51 +0000909 MappingGen.emitCounterMapping(D, OS);
910 OS.flush();
911}
912
913void
914CodeGenPGO::emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
915 llvm::GlobalValue::LinkageTypes Linkage) {
916 if (SkipCoverageMapping)
917 return;
918 setFuncName(FuncName, Linkage);
919 setVarLinkage(Linkage);
920
921 // Don't map the functions inside the system headers
922 auto Loc = D->getBody()->getLocStart();
923 if (CGM.getContext().getSourceManager().isInSystemHeader(Loc))
924 return;
925
926 llvm::raw_string_ostream OS(CoverageMapping);
927 CoverageMappingGen MappingGen(*CGM.getCoverageMapping(),
928 CGM.getContext().getSourceManager(),
929 CGM.getLangOpts());
930 MappingGen.emitEmptyMapping(D, OS);
931 OS.flush();
932 buildDataVar();
933}
934
Bob Wilsonbf854f02014-02-17 19:21:09 +0000935void CodeGenPGO::computeRegionCounts(const Decl *D) {
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +0000936 StmtCountMap.reset(new llvm::DenseMap<const Stmt *, uint64_t>);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000937 ComputeRegionCounts Walker(*StmtCountMap, *this);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000938 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
939 Walker.VisitFunctionDecl(FD);
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000940 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
941 Walker.VisitObjCMethodDecl(MD);
Bob Wilsonc845c002014-03-06 20:24:27 +0000942 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
943 Walker.VisitBlockDecl(BD);
Justin Bogner81ab90f2014-04-15 00:50:54 +0000944 else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
945 Walker.VisitCapturedDecl(const_cast<CapturedDecl *>(CD));
Bob Wilsonbf854f02014-02-17 19:21:09 +0000946}
947
Justin Bogner837a6f62014-04-18 21:52:00 +0000948void
949CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
950 llvm::Function *Fn) {
Justin Bogner4c9c45c2014-03-12 18:14:32 +0000951 if (!haveRegionCounts())
952 return;
953
Justin Bogner837a6f62014-04-18 21:52:00 +0000954 uint64_t MaxFunctionCount = PGOReader->getMaximumFunctionCount();
Justin Bogner4c9c45c2014-03-12 18:14:32 +0000955 uint64_t FunctionCount = getRegionCount(0);
956 if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount))
957 // Turn on InlineHint attribute for hot functions.
958 // FIXME: 30% is from preliminary tuning on SPEC, it may not be optimal.
959 Fn->addFnAttr(llvm::Attribute::InlineHint);
960 else if (FunctionCount <= (uint64_t)(0.01 * (double)MaxFunctionCount))
961 // Turn on Cold attribute for cold functions.
962 // FIXME: 1% is from preliminary tuning on SPEC, it may not be optimal.
963 Fn->addFnAttr(llvm::Attribute::Cold);
964}
965
Justin Bogneref512b92014-01-06 22:27:43 +0000966void CodeGenPGO::emitCounterVariables() {
967 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
968 llvm::ArrayType *CounterTy = llvm::ArrayType::get(llvm::Type::getInt64Ty(Ctx),
969 NumRegionCounters);
970 RegionCounters =
Duncan P. N. Exon Smith73f78622014-03-20 22:49:50 +0000971 new llvm::GlobalVariable(CGM.getModule(), CounterTy, false, VarLinkage,
Justin Bogneref512b92014-01-06 22:27:43 +0000972 llvm::Constant::getNullValue(CounterTy),
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000973 getFuncVarName("counters"));
974 RegionCounters->setAlignment(8);
975 RegionCounters->setSection(getCountersSection(CGM));
Justin Bogneref512b92014-01-06 22:27:43 +0000976}
977
978void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) {
Bob Wilson749ebc72014-03-06 04:55:28 +0000979 if (!RegionCounters)
Justin Bogneref512b92014-01-06 22:27:43 +0000980 return;
981 llvm::Value *Addr =
982 Builder.CreateConstInBoundsGEP2_64(RegionCounters, 0, Counter);
983 llvm::Value *Count = Builder.CreateLoad(Addr, "pgocount");
984 Count = Builder.CreateAdd(Count, Builder.getInt64(1));
985 Builder.CreateStore(Count, Addr);
986}
987
Justin Bogner40b8ba12014-06-26 01:45:07 +0000988void CodeGenPGO::loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
989 bool IsInMainFile) {
990 CGM.getPGOStats().addVisited(IsInMainFile);
Justin Bogner7f8cf5b2014-12-02 22:38:52 +0000991 RegionCounts.clear();
Justin Bogner9c6818e2014-08-01 22:50:16 +0000992 if (std::error_code EC = PGOReader->getFunctionCounts(
Justin Bogner7f8cf5b2014-12-02 22:38:52 +0000993 getFuncName(), FunctionHash, RegionCounts)) {
Justin Bogner9c6818e2014-08-01 22:50:16 +0000994 if (EC == llvm::instrprof_error::unknown_function)
995 CGM.getPGOStats().addMissing(IsInMainFile);
996 else if (EC == llvm::instrprof_error::hash_mismatch)
997 CGM.getPGOStats().addMismatched(IsInMainFile);
998 else if (EC == llvm::instrprof_error::malformed)
999 // TODO: Consider a more specific warning for this case.
1000 CGM.getPGOStats().addMismatched(IsInMainFile);
Justin Bogner7f8cf5b2014-12-02 22:38:52 +00001001 RegionCounts.clear();
Justin Bognere2ef2a02014-04-15 21:22:35 +00001002 }
Justin Bogneref512b92014-01-06 22:27:43 +00001003}
1004
1005void CodeGenPGO::destroyRegionCounters() {
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +00001006 RegionCounterMap.reset();
1007 StmtCountMap.reset();
Justin Bogner7f8cf5b2014-12-02 22:38:52 +00001008 RegionCounts.clear();
Justin Bogner3212b182014-04-25 07:20:05 +00001009 RegionCounters = nullptr;
Justin Bogneref512b92014-01-06 22:27:43 +00001010}
1011
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +00001012/// \brief Calculate what to divide by to scale weights.
1013///
1014/// Given the maximum weight, calculate a divisor that will scale all the
1015/// weights to strictly less than UINT32_MAX.
1016static uint64_t calculateWeightScale(uint64_t MaxWeight) {
1017 return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1;
1018}
1019
1020/// \brief Scale an individual branch weight (and add 1).
1021///
1022/// Scale a 64-bit weight down to 32-bits using \c Scale.
1023///
1024/// According to Laplace's Rule of Succession, it is better to compute the
1025/// weight based on the count plus 1, so universally add 1 to the value.
1026///
1027/// \pre \c Scale was calculated by \a calculateWeightScale() with a weight no
1028/// greater than \c Weight.
1029static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale) {
1030 assert(Scale && "scale by 0?");
1031 uint64_t Scaled = Weight / Scale + 1;
1032 assert(Scaled <= UINT32_MAX && "overflow 32-bits");
1033 return Scaled;
1034}
1035
Justin Bogneref512b92014-01-06 22:27:43 +00001036llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount,
1037 uint64_t FalseCount) {
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +00001038 // Check for empty weights.
Justin Bogneref512b92014-01-06 22:27:43 +00001039 if (!TrueCount && !FalseCount)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +00001040 return nullptr;
Justin Bogneref512b92014-01-06 22:27:43 +00001041
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +00001042 // Calculate how to scale down to 32-bits.
1043 uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount));
1044
Justin Bogneref512b92014-01-06 22:27:43 +00001045 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +00001046 return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale),
1047 scaleBranchWeight(FalseCount, Scale));
Justin Bogneref512b92014-01-06 22:27:43 +00001048}
1049
Bob Wilson95a27b02014-02-17 19:20:59 +00001050llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) {
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +00001051 // We need at least two elements to create meaningful weights.
1052 if (Weights.size() < 2)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +00001053 return nullptr;
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +00001054
Justin Bognerf3aefca2014-04-04 02:48:51 +00001055 // Check for empty weights.
1056 uint64_t MaxWeight = *std::max_element(Weights.begin(), Weights.end());
1057 if (MaxWeight == 0)
1058 return nullptr;
1059
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +00001060 // Calculate how to scale down to 32-bits.
Justin Bognerf3aefca2014-04-04 02:48:51 +00001061 uint64_t Scale = calculateWeightScale(MaxWeight);
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +00001062
Justin Bogneref512b92014-01-06 22:27:43 +00001063 SmallVector<uint32_t, 16> ScaledWeights;
1064 ScaledWeights.reserve(Weights.size());
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +00001065 for (uint64_t W : Weights)
1066 ScaledWeights.push_back(scaleBranchWeight(W, Scale));
1067
1068 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
Justin Bogneref512b92014-01-06 22:27:43 +00001069 return MDHelper.createBranchWeights(ScaledWeights);
1070}
Bob Wilsonbf854f02014-02-17 19:21:09 +00001071
1072llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond,
1073 RegionCounter &Cnt) {
1074 if (!haveRegionCounts())
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +00001075 return nullptr;
Bob Wilsonbf854f02014-02-17 19:21:09 +00001076 uint64_t LoopCount = Cnt.getCount();
1077 uint64_t CondCount = 0;
1078 bool Found = getStmtCount(Cond, CondCount);
1079 assert(Found && "missing expected loop condition count");
1080 (void)Found;
1081 if (CondCount == 0)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +00001082 return nullptr;
Bob Wilsonbf854f02014-02-17 19:21:09 +00001083 return createBranchWeights(LoopCount,
1084 std::max(CondCount, LoopCount) - LoopCount);
1085}