blob: 22534b828ee55dd30a50c01e35580fec37e013d2 [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"
18#include "llvm/IR/MDBuilder.h"
Justin Bogner837a6f62014-04-18 21:52:00 +000019#include "llvm/ProfileData/InstrProfReader.h"
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +000020#include "llvm/Support/Endian.h"
Justin Bogneref512b92014-01-06 22:27:43 +000021#include "llvm/Support/FileSystem.h"
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +000022#include "llvm/Support/MD5.h"
Justin Bogneref512b92014-01-06 22:27:43 +000023
24using namespace clang;
25using namespace CodeGen;
26
Bob Wilsonda1ebed2014-03-06 04:55:41 +000027void CodeGenPGO::setFuncName(llvm::Function *Fn) {
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000028 RawFuncName = Fn->getName();
Bob Wilsonda1ebed2014-03-06 04:55:41 +000029
30 // Function names may be prefixed with a binary '1' to indicate
31 // that the backend should not modify the symbols due to any platform
32 // naming convention. Do not include that '1' in the PGO profile name.
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000033 if (RawFuncName[0] == '\1')
34 RawFuncName = RawFuncName.substr(1);
Bob Wilsonda1ebed2014-03-06 04:55:41 +000035
36 if (!Fn->hasLocalLinkage()) {
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +000037 PrefixedFuncName.reset(new std::string(RawFuncName));
Bob Wilsonda1ebed2014-03-06 04:55:41 +000038 return;
39 }
40
41 // For local symbols, prepend the main file name to distinguish them.
42 // Do not include the full path in the file name since there's no guarantee
43 // that it will stay the same, e.g., if the files are checked out from
44 // version control in different locations.
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +000045 PrefixedFuncName.reset(new std::string(CGM.getCodeGenOpts().MainFileName));
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000046 if (PrefixedFuncName->empty())
47 PrefixedFuncName->assign("<unknown>");
48 PrefixedFuncName->append(":");
49 PrefixedFuncName->append(RawFuncName);
Bob Wilsonda1ebed2014-03-06 04:55:41 +000050}
51
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000052static llvm::Function *getRegisterFunc(CodeGenModule &CGM) {
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +000053 return CGM.getModule().getFunction("__llvm_profile_register_functions");
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000054}
55
56static llvm::BasicBlock *getOrInsertRegisterBB(CodeGenModule &CGM) {
Duncan P. N. Exon Smith780443e2014-03-20 03:57:11 +000057 // Don't do this for Darwin. compiler-rt uses linker magic.
58 if (CGM.getTarget().getTriple().isOSDarwin())
59 return nullptr;
60
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000061 // Only need to insert this once per module.
62 if (llvm::Function *RegisterF = getRegisterFunc(CGM))
63 return &RegisterF->getEntryBlock();
64
65 // Construct the function.
66 auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
67 auto *RegisterFTy = llvm::FunctionType::get(VoidTy, false);
68 auto *RegisterF = llvm::Function::Create(RegisterFTy,
69 llvm::GlobalValue::InternalLinkage,
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +000070 "__llvm_profile_register_functions",
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000071 &CGM.getModule());
72 RegisterF->setUnnamedAddr(true);
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000073 if (CGM.getCodeGenOpts().DisableRedZone)
74 RegisterF->addFnAttr(llvm::Attribute::NoRedZone);
75
76 // Construct and return the entry block.
77 auto *BB = llvm::BasicBlock::Create(CGM.getLLVMContext(), "", RegisterF);
78 CGBuilderTy Builder(BB);
79 Builder.CreateRetVoid();
80 return BB;
81}
82
83static llvm::Constant *getOrInsertRuntimeRegister(CodeGenModule &CGM) {
84 auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
85 auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
86 auto *RuntimeRegisterTy = llvm::FunctionType::get(VoidTy, VoidPtrTy, false);
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +000087 return CGM.getModule().getOrInsertFunction("__llvm_profile_register_function",
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000088 RuntimeRegisterTy);
89}
90
Duncan P. N. Exon Smith7134d472014-03-20 03:17:15 +000091static bool isMachO(const CodeGenModule &CGM) {
92 return CGM.getTarget().getTriple().isOSBinFormatMachO();
93}
94
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000095static StringRef getCountersSection(const CodeGenModule &CGM) {
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +000096 return isMachO(CGM) ? "__DATA,__llvm_prf_cnts" : "__llvm_prf_cnts";
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +000097}
98
99static StringRef getNameSection(const CodeGenModule &CGM) {
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +0000100 return isMachO(CGM) ? "__DATA,__llvm_prf_names" : "__llvm_prf_names";
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000101}
102
103static StringRef getDataSection(const CodeGenModule &CGM) {
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +0000104 return isMachO(CGM) ? "__DATA,__llvm_prf_data" : "__llvm_prf_data";
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000105}
106
107llvm::GlobalVariable *CodeGenPGO::buildDataVar() {
108 // Create name variable.
109 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
110 auto *VarName = llvm::ConstantDataArray::getString(Ctx, getFuncName(),
111 false);
112 auto *Name = new llvm::GlobalVariable(CGM.getModule(), VarName->getType(),
Duncan P. N. Exon Smith73f78622014-03-20 22:49:50 +0000113 true, VarLinkage, VarName,
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000114 getFuncVarName("name"));
115 Name->setSection(getNameSection(CGM));
116 Name->setAlignment(1);
117
118 // Create data variable.
119 auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
Justin Bognerb4416f52014-03-18 21:58:06 +0000120 auto *Int64Ty = llvm::Type::getInt64Ty(Ctx);
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000121 auto *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx);
122 auto *Int64PtrTy = llvm::Type::getInt64PtrTy(Ctx);
123 llvm::Type *DataTypes[] = {
Justin Bognerb4416f52014-03-18 21:58:06 +0000124 Int32Ty, Int32Ty, Int64Ty, Int8PtrTy, Int64PtrTy
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000125 };
126 auto *DataTy = llvm::StructType::get(Ctx, makeArrayRef(DataTypes));
127 llvm::Constant *DataVals[] = {
128 llvm::ConstantInt::get(Int32Ty, getFuncName().size()),
129 llvm::ConstantInt::get(Int32Ty, NumRegionCounters),
Justin Bognerb4416f52014-03-18 21:58:06 +0000130 llvm::ConstantInt::get(Int64Ty, FunctionHash),
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000131 llvm::ConstantExpr::getBitCast(Name, Int8PtrTy),
132 llvm::ConstantExpr::getBitCast(RegionCounters, Int64PtrTy)
133 };
134 auto *Data =
Duncan P. N. Exon Smith73f78622014-03-20 22:49:50 +0000135 new llvm::GlobalVariable(CGM.getModule(), DataTy, true, VarLinkage,
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000136 llvm::ConstantStruct::get(DataTy, DataVals),
137 getFuncVarName("data"));
138
139 // All the data should be packed into an array in its own section.
140 Data->setSection(getDataSection(CGM));
141 Data->setAlignment(8);
142
Duncan P. N. Exon Smith91212202014-05-16 01:24:00 +0000143 // Hide all these symbols so that we correctly get a copy for each
144 // executable. The profile format expects names and counters to be
145 // contiguous, so references into shared objects would be invalid.
146 if (!llvm::GlobalValue::isLocalLinkage(VarLinkage)) {
147 Name->setVisibility(llvm::GlobalValue::HiddenVisibility);
148 Data->setVisibility(llvm::GlobalValue::HiddenVisibility);
149 RegionCounters->setVisibility(llvm::GlobalValue::HiddenVisibility);
150 }
151
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000152 // Make sure the data doesn't get deleted.
153 CGM.addUsedGlobal(Data);
154 return Data;
155}
156
157void CodeGenPGO::emitInstrumentationData() {
Justin Bogner3212b182014-04-25 07:20:05 +0000158 if (!RegionCounters)
Justin Bogneref512b92014-01-06 22:27:43 +0000159 return;
160
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000161 // Build the data.
162 auto *Data = buildDataVar();
Justin Bogneref512b92014-01-06 22:27:43 +0000163
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000164 // Register the data.
Duncan P. N. Exon Smith780443e2014-03-20 03:57:11 +0000165 auto *RegisterBB = getOrInsertRegisterBB(CGM);
166 if (!RegisterBB)
167 return;
168 CGBuilderTy Builder(RegisterBB->getTerminator());
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000169 auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
170 Builder.CreateCall(getOrInsertRuntimeRegister(CGM),
171 Builder.CreateBitCast(Data, VoidPtrTy));
Justin Bogneref512b92014-01-06 22:27:43 +0000172}
173
174llvm::Function *CodeGenPGO::emitInitialization(CodeGenModule &CGM) {
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000175 if (!CGM.getCodeGenOpts().ProfileInstrGenerate)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000176 return nullptr;
Justin Bogneref512b92014-01-06 22:27:43 +0000177
Justin Bognerf2ea7752014-04-10 18:13:13 +0000178 assert(CGM.getModule().getFunction("__llvm_profile_init") == nullptr &&
179 "profile initialization already emitted");
Justin Bogneref512b92014-01-06 22:27:43 +0000180
Duncan P. N. Exon Smith5188e912014-03-20 19:23:46 +0000181 // Get the function to call at initialization.
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000182 llvm::Constant *RegisterF = getRegisterFunc(CGM);
Duncan P. N. Exon Smith5188e912014-03-20 19:23:46 +0000183 if (!RegisterF)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000184 return nullptr;
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000185
186 // Create the initialization function.
187 auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext());
188 auto *F = llvm::Function::Create(llvm::FunctionType::get(VoidTy, false),
189 llvm::GlobalValue::InternalLinkage,
Duncan P. N. Exon Smitha7807632014-03-20 20:00:41 +0000190 "__llvm_profile_init", &CGM.getModule());
Justin Bogneref512b92014-01-06 22:27:43 +0000191 F->setUnnamedAddr(true);
Justin Bogneref512b92014-01-06 22:27:43 +0000192 F->addFnAttr(llvm::Attribute::NoInline);
193 if (CGM.getCodeGenOpts().DisableRedZone)
194 F->addFnAttr(llvm::Attribute::NoRedZone);
195
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000196 // Add the basic block and the necessary calls.
197 CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", F));
Duncan P. N. Exon Smith5188e912014-03-20 19:23:46 +0000198 Builder.CreateCall(RegisterF);
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000199 Builder.CreateRetVoid();
Justin Bogneref512b92014-01-06 22:27:43 +0000200
201 return F;
202}
203
204namespace {
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000205/// \brief Stable hasher for PGO region counters.
206///
207/// PGOHash produces a stable hash of a given function's control flow.
208///
209/// Changing the output of this hash will invalidate all previously generated
210/// profiles -- i.e., don't do it.
211///
212/// \note When this hash does eventually change (years?), we still need to
213/// support old hashes. We'll need to pull in the version number from the
214/// profile data format and use the matching hash function.
215class PGOHash {
216 uint64_t Working;
217 unsigned Count;
218 llvm::MD5 MD5;
219
220 static const int NumBitsPerType = 6;
221 static const unsigned NumTypesPerWord = sizeof(uint64_t) * 8 / NumBitsPerType;
222 static const unsigned TooBig = 1u << NumBitsPerType;
223
224public:
225 /// \brief Hash values for AST nodes.
226 ///
227 /// Distinct values for AST nodes that have region counters attached.
228 ///
229 /// These values must be stable. All new members must be added at the end,
230 /// and no members should be removed. Changing the enumeration value for an
231 /// AST node will affect the hash of every function that contains that node.
232 enum HashType : unsigned char {
233 None = 0,
234 LabelStmt = 1,
235 WhileStmt,
236 DoStmt,
237 ForStmt,
238 CXXForRangeStmt,
239 ObjCForCollectionStmt,
240 SwitchStmt,
241 CaseStmt,
242 DefaultStmt,
243 IfStmt,
244 CXXTryStmt,
245 CXXCatchStmt,
246 ConditionalOperator,
247 BinaryOperatorLAnd,
248 BinaryOperatorLOr,
249 BinaryConditionalOperator,
250
251 // Keep this last. It's for the static assert that follows.
252 LastHashType
253 };
254 static_assert(LastHashType <= TooBig, "Too many types in HashType");
255
256 // TODO: When this format changes, take in a version number here, and use the
257 // old hash calculation for file formats that used the old hash.
258 PGOHash() : Working(0), Count(0) {}
259 void combine(HashType Type);
260 uint64_t finalize();
261};
262const int PGOHash::NumBitsPerType;
263const unsigned PGOHash::NumTypesPerWord;
264const unsigned PGOHash::TooBig;
265
Bob Wilsond8931422014-04-11 17:16:13 +0000266 /// A RecursiveASTVisitor that fills a map of statements to PGO counters.
267 struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
Justin Bogneref512b92014-01-06 22:27:43 +0000268 /// The next counter value to assign.
269 unsigned NextCounter;
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000270 /// The function hash.
271 PGOHash Hash;
Justin Bogneref512b92014-01-06 22:27:43 +0000272 /// The map of statements to counters.
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000273 llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
Justin Bogneref512b92014-01-06 22:27:43 +0000274
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000275 MapRegionCounters(llvm::DenseMap<const Stmt *, unsigned> &CounterMap)
276 : NextCounter(0), CounterMap(CounterMap) {}
Justin Bogneref512b92014-01-06 22:27:43 +0000277
Justin Bogner191ec632014-04-11 23:06:35 +0000278 // Blocks and lambdas are handled as separate functions, so we need not
279 // traverse them in the parent context.
280 bool TraverseBlockExpr(BlockExpr *BE) { return true; }
281 bool TraverseLambdaBody(LambdaExpr *LE) { return true; }
Justin Bogner81ab90f2014-04-15 00:50:54 +0000282 bool TraverseCapturedStmt(CapturedStmt *CS) { return true; }
Justin Bogneref512b92014-01-06 22:27:43 +0000283
Bob Wilsond8931422014-04-11 17:16:13 +0000284 bool VisitDecl(const Decl *D) {
285 switch (D->getKind()) {
286 default:
287 break;
288 case Decl::Function:
289 case Decl::CXXMethod:
290 case Decl::CXXConstructor:
291 case Decl::CXXDestructor:
292 case Decl::CXXConversion:
293 case Decl::ObjCMethod:
294 case Decl::Block:
Justin Bogner81ab90f2014-04-15 00:50:54 +0000295 case Decl::Captured:
Bob Wilsond8931422014-04-11 17:16:13 +0000296 CounterMap[D->getBody()] = NextCounter++;
297 break;
298 }
299 return true;
Justin Bogneref512b92014-01-06 22:27:43 +0000300 }
Bob Wilsond8931422014-04-11 17:16:13 +0000301
302 bool VisitStmt(const Stmt *S) {
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000303 auto Type = getHashType(S);
304 if (Type == PGOHash::None)
305 return true;
306
307 CounterMap[S] = NextCounter++;
308 Hash.combine(Type);
309 return true;
310 }
311 PGOHash::HashType getHashType(const Stmt *S) {
Bob Wilsond8931422014-04-11 17:16:13 +0000312 switch (S->getStmtClass()) {
313 default:
314 break;
315 case Stmt::LabelStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000316 return PGOHash::LabelStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000317 case Stmt::WhileStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000318 return PGOHash::WhileStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000319 case Stmt::DoStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000320 return PGOHash::DoStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000321 case Stmt::ForStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000322 return PGOHash::ForStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000323 case Stmt::CXXForRangeStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000324 return PGOHash::CXXForRangeStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000325 case Stmt::ObjCForCollectionStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000326 return PGOHash::ObjCForCollectionStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000327 case Stmt::SwitchStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000328 return PGOHash::SwitchStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000329 case Stmt::CaseStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000330 return PGOHash::CaseStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000331 case Stmt::DefaultStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000332 return PGOHash::DefaultStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000333 case Stmt::IfStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000334 return PGOHash::IfStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000335 case Stmt::CXXTryStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000336 return PGOHash::CXXTryStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000337 case Stmt::CXXCatchStmtClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000338 return PGOHash::CXXCatchStmt;
Bob Wilsond8931422014-04-11 17:16:13 +0000339 case Stmt::ConditionalOperatorClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000340 return PGOHash::ConditionalOperator;
Bob Wilsond8931422014-04-11 17:16:13 +0000341 case Stmt::BinaryConditionalOperatorClass:
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000342 return PGOHash::BinaryConditionalOperator;
Bob Wilsond8931422014-04-11 17:16:13 +0000343 case Stmt::BinaryOperatorClass: {
344 const BinaryOperator *BO = cast<BinaryOperator>(S);
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000345 if (BO->getOpcode() == BO_LAnd)
346 return PGOHash::BinaryOperatorLAnd;
347 if (BO->getOpcode() == BO_LOr)
348 return PGOHash::BinaryOperatorLOr;
Bob Wilsond8931422014-04-11 17:16:13 +0000349 break;
350 }
351 }
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000352 return PGOHash::None;
Justin Bogneref512b92014-01-06 22:27:43 +0000353 }
354 };
Bob Wilsonbf854f02014-02-17 19:21:09 +0000355
356 /// A StmtVisitor that propagates the raw counts through the AST and
357 /// records the count at statements where the value may change.
358 struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> {
359 /// PGO state.
360 CodeGenPGO &PGO;
361
362 /// A flag that is set when the current count should be recorded on the
363 /// next statement, such as at the exit of a loop.
364 bool RecordNextStmtCount;
365
366 /// The map of statements to count values.
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000367 llvm::DenseMap<const Stmt *, uint64_t> &CountMap;
Bob Wilsonbf854f02014-02-17 19:21:09 +0000368
Justin Bognerfc83c112014-04-18 21:51:09 +0000369 /// BreakContinueStack - Keep counts of breaks and continues inside loops.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000370 struct BreakContinue {
371 uint64_t BreakCount;
372 uint64_t ContinueCount;
373 BreakContinue() : BreakCount(0), ContinueCount(0) {}
374 };
375 SmallVector<BreakContinue, 8> BreakContinueStack;
376
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000377 ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &CountMap,
378 CodeGenPGO &PGO)
379 : PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000380
381 void RecordStmtCount(const Stmt *S) {
382 if (RecordNextStmtCount) {
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000383 CountMap[S] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000384 RecordNextStmtCount = false;
385 }
386 }
387
388 void VisitStmt(const Stmt *S) {
389 RecordStmtCount(S);
390 for (Stmt::const_child_range I = S->children(); I; ++I) {
391 if (*I)
392 this->Visit(*I);
393 }
394 }
395
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000396 void VisitFunctionDecl(const FunctionDecl *D) {
Bob Wilsond8931422014-04-11 17:16:13 +0000397 // Counter tracks entry to the function body.
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000398 RegionCounter Cnt(PGO, D->getBody());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000399 Cnt.beginRegion();
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000400 CountMap[D->getBody()] = PGO.getCurrentRegionCount();
401 Visit(D->getBody());
Bob Wilsonbf854f02014-02-17 19:21:09 +0000402 }
403
Justin Bogner191ec632014-04-11 23:06:35 +0000404 // Skip lambda expressions. We visit these as FunctionDecls when we're
405 // generating them and aren't interested in the body when generating a
406 // parent context.
407 void VisitLambdaExpr(const LambdaExpr *LE) {}
408
Justin Bogner81ab90f2014-04-15 00:50:54 +0000409 void VisitCapturedDecl(const CapturedDecl *D) {
410 // Counter tracks entry to the capture body.
411 RegionCounter Cnt(PGO, D->getBody());
412 Cnt.beginRegion();
413 CountMap[D->getBody()] = PGO.getCurrentRegionCount();
414 Visit(D->getBody());
415 }
416
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000417 void VisitObjCMethodDecl(const ObjCMethodDecl *D) {
Bob Wilsond8931422014-04-11 17:16:13 +0000418 // Counter tracks entry to the method body.
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000419 RegionCounter Cnt(PGO, D->getBody());
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000420 Cnt.beginRegion();
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000421 CountMap[D->getBody()] = PGO.getCurrentRegionCount();
422 Visit(D->getBody());
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000423 }
424
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000425 void VisitBlockDecl(const BlockDecl *D) {
Bob Wilsond8931422014-04-11 17:16:13 +0000426 // Counter tracks entry to the block body.
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000427 RegionCounter Cnt(PGO, D->getBody());
Bob Wilsonc845c002014-03-06 20:24:27 +0000428 Cnt.beginRegion();
Duncan P. N. Exon Smith4a2f5ae2014-04-10 23:37:36 +0000429 CountMap[D->getBody()] = PGO.getCurrentRegionCount();
430 Visit(D->getBody());
Bob Wilsonc845c002014-03-06 20:24:27 +0000431 }
432
Bob Wilsonbf854f02014-02-17 19:21:09 +0000433 void VisitReturnStmt(const ReturnStmt *S) {
434 RecordStmtCount(S);
435 if (S->getRetValue())
436 Visit(S->getRetValue());
437 PGO.setCurrentRegionUnreachable();
438 RecordNextStmtCount = true;
439 }
440
441 void VisitGotoStmt(const GotoStmt *S) {
442 RecordStmtCount(S);
443 PGO.setCurrentRegionUnreachable();
444 RecordNextStmtCount = true;
445 }
446
447 void VisitLabelStmt(const LabelStmt *S) {
448 RecordNextStmtCount = false;
Bob Wilsond8931422014-04-11 17:16:13 +0000449 // Counter tracks the block following the label.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000450 RegionCounter Cnt(PGO, S);
451 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000452 CountMap[S] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000453 Visit(S->getSubStmt());
454 }
455
456 void VisitBreakStmt(const BreakStmt *S) {
457 RecordStmtCount(S);
458 assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
459 BreakContinueStack.back().BreakCount += PGO.getCurrentRegionCount();
460 PGO.setCurrentRegionUnreachable();
461 RecordNextStmtCount = true;
462 }
463
464 void VisitContinueStmt(const ContinueStmt *S) {
465 RecordStmtCount(S);
466 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
467 BreakContinueStack.back().ContinueCount += PGO.getCurrentRegionCount();
468 PGO.setCurrentRegionUnreachable();
469 RecordNextStmtCount = true;
470 }
471
472 void VisitWhileStmt(const WhileStmt *S) {
473 RecordStmtCount(S);
Bob Wilsond8931422014-04-11 17:16:13 +0000474 // Counter tracks the body of the loop.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000475 RegionCounter Cnt(PGO, S);
476 BreakContinueStack.push_back(BreakContinue());
477 // Visit the body region first so the break/continue adjustments can be
478 // included when visiting the condition.
479 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000480 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000481 Visit(S->getBody());
482 Cnt.adjustForControlFlow();
483
484 // ...then go back and propagate counts through the condition. The count
485 // at the start of the condition is the sum of the incoming edges,
486 // the backedge from the end of the loop body, and the edges from
487 // continue statements.
488 BreakContinue BC = BreakContinueStack.pop_back_val();
489 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
490 Cnt.getAdjustedCount() + BC.ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000491 CountMap[S->getCond()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000492 Visit(S->getCond());
493 Cnt.adjustForControlFlow();
494 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
495 RecordNextStmtCount = true;
496 }
497
498 void VisitDoStmt(const DoStmt *S) {
499 RecordStmtCount(S);
Bob Wilsond8931422014-04-11 17:16:13 +0000500 // Counter tracks the body of the loop.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000501 RegionCounter Cnt(PGO, S);
502 BreakContinueStack.push_back(BreakContinue());
503 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000504 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000505 Visit(S->getBody());
506 Cnt.adjustForControlFlow();
507
508 BreakContinue BC = BreakContinueStack.pop_back_val();
509 // The count at the start of the condition is equal to the count at the
510 // end of the body. The adjusted count does not include either the
511 // fall-through count coming into the loop or the continue count, so add
512 // both of those separately. This is coincidentally the same equation as
513 // with while loops but for different reasons.
514 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
515 Cnt.getAdjustedCount() + BC.ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000516 CountMap[S->getCond()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000517 Visit(S->getCond());
518 Cnt.adjustForControlFlow();
519 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
520 RecordNextStmtCount = true;
521 }
522
523 void VisitForStmt(const ForStmt *S) {
524 RecordStmtCount(S);
525 if (S->getInit())
526 Visit(S->getInit());
Bob Wilsond8931422014-04-11 17:16:13 +0000527 // Counter tracks the body of the loop.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000528 RegionCounter Cnt(PGO, S);
529 BreakContinueStack.push_back(BreakContinue());
530 // Visit the body region first. (This is basically the same as a while
531 // loop; see further comments in VisitWhileStmt.)
532 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000533 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000534 Visit(S->getBody());
535 Cnt.adjustForControlFlow();
536
537 // The increment is essentially part of the body but it needs to include
538 // the count for all the continue statements.
539 if (S->getInc()) {
540 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
541 BreakContinueStack.back().ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000542 CountMap[S->getInc()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000543 Visit(S->getInc());
544 Cnt.adjustForControlFlow();
545 }
546
547 BreakContinue BC = BreakContinueStack.pop_back_val();
548
549 // ...then go back and propagate counts through the condition.
550 if (S->getCond()) {
551 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
552 Cnt.getAdjustedCount() +
553 BC.ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000554 CountMap[S->getCond()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000555 Visit(S->getCond());
556 Cnt.adjustForControlFlow();
557 }
558 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
559 RecordNextStmtCount = true;
560 }
561
562 void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
563 RecordStmtCount(S);
564 Visit(S->getRangeStmt());
565 Visit(S->getBeginEndStmt());
Bob Wilsond8931422014-04-11 17:16:13 +0000566 // Counter tracks the body of the loop.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000567 RegionCounter Cnt(PGO, S);
568 BreakContinueStack.push_back(BreakContinue());
569 // Visit the body region first. (This is basically the same as a while
570 // loop; see further comments in VisitWhileStmt.)
571 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000572 CountMap[S->getLoopVarStmt()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000573 Visit(S->getLoopVarStmt());
574 Visit(S->getBody());
575 Cnt.adjustForControlFlow();
576
577 // The increment is essentially part of the body but it needs to include
578 // the count for all the continue statements.
579 Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() +
580 BreakContinueStack.back().ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000581 CountMap[S->getInc()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000582 Visit(S->getInc());
583 Cnt.adjustForControlFlow();
584
585 BreakContinue BC = BreakContinueStack.pop_back_val();
586
587 // ...then go back and propagate counts through the condition.
588 Cnt.setCurrentRegionCount(Cnt.getParentCount() +
589 Cnt.getAdjustedCount() +
590 BC.ContinueCount);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000591 CountMap[S->getCond()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000592 Visit(S->getCond());
593 Cnt.adjustForControlFlow();
594 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
595 RecordNextStmtCount = true;
596 }
597
598 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
599 RecordStmtCount(S);
600 Visit(S->getElement());
Bob Wilsond8931422014-04-11 17:16:13 +0000601 // Counter tracks the body of the loop.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000602 RegionCounter Cnt(PGO, S);
603 BreakContinueStack.push_back(BreakContinue());
604 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000605 CountMap[S->getBody()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000606 Visit(S->getBody());
607 BreakContinue BC = BreakContinueStack.pop_back_val();
608 Cnt.adjustForControlFlow();
609 Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount);
610 RecordNextStmtCount = true;
611 }
612
613 void VisitSwitchStmt(const SwitchStmt *S) {
614 RecordStmtCount(S);
615 Visit(S->getCond());
616 PGO.setCurrentRegionUnreachable();
617 BreakContinueStack.push_back(BreakContinue());
618 Visit(S->getBody());
619 // If the switch is inside a loop, add the continue counts.
620 BreakContinue BC = BreakContinueStack.pop_back_val();
621 if (!BreakContinueStack.empty())
622 BreakContinueStack.back().ContinueCount += BC.ContinueCount;
Bob Wilsond8931422014-04-11 17:16:13 +0000623 // Counter tracks the exit block of the switch.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000624 RegionCounter ExitCnt(PGO, S);
625 ExitCnt.beginRegion();
626 RecordNextStmtCount = true;
627 }
628
629 void VisitCaseStmt(const CaseStmt *S) {
630 RecordNextStmtCount = false;
Bob Wilsond8931422014-04-11 17:16:13 +0000631 // Counter for this particular case. This counts only jumps from the
632 // switch header and does not include fallthrough from the case before
633 // this one.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000634 RegionCounter Cnt(PGO, S);
635 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000636 CountMap[S] = Cnt.getCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000637 RecordNextStmtCount = true;
638 Visit(S->getSubStmt());
639 }
640
641 void VisitDefaultStmt(const DefaultStmt *S) {
642 RecordNextStmtCount = false;
Bob Wilsond8931422014-04-11 17:16:13 +0000643 // Counter for this default case. This does not include fallthrough from
644 // the previous case.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000645 RegionCounter Cnt(PGO, S);
646 Cnt.beginRegion(/*AddIncomingFallThrough=*/true);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000647 CountMap[S] = Cnt.getCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000648 RecordNextStmtCount = true;
649 Visit(S->getSubStmt());
650 }
651
652 void VisitIfStmt(const IfStmt *S) {
653 RecordStmtCount(S);
Bob Wilsond8931422014-04-11 17:16:13 +0000654 // Counter tracks the "then" part of an if statement. The count for
655 // the "else" part, if it exists, will be calculated from this counter.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000656 RegionCounter Cnt(PGO, S);
657 Visit(S->getCond());
658
659 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000660 CountMap[S->getThen()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000661 Visit(S->getThen());
662 Cnt.adjustForControlFlow();
663
664 if (S->getElse()) {
665 Cnt.beginElseRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000666 CountMap[S->getElse()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000667 Visit(S->getElse());
668 Cnt.adjustForControlFlow();
669 }
670 Cnt.applyAdjustmentsToRegion(0);
671 RecordNextStmtCount = true;
672 }
673
674 void VisitCXXTryStmt(const CXXTryStmt *S) {
675 RecordStmtCount(S);
676 Visit(S->getTryBlock());
677 for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
678 Visit(S->getHandler(I));
Bob Wilsond8931422014-04-11 17:16:13 +0000679 // Counter tracks the continuation block of the try statement.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000680 RegionCounter Cnt(PGO, S);
681 Cnt.beginRegion();
682 RecordNextStmtCount = true;
683 }
684
685 void VisitCXXCatchStmt(const CXXCatchStmt *S) {
686 RecordNextStmtCount = false;
Bob Wilsond8931422014-04-11 17:16:13 +0000687 // Counter tracks the catch statement's handler block.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000688 RegionCounter Cnt(PGO, S);
689 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000690 CountMap[S] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000691 Visit(S->getHandlerBlock());
692 }
693
Justin Bogner53c55d92014-04-11 06:10:10 +0000694 void VisitAbstractConditionalOperator(
695 const AbstractConditionalOperator *E) {
Bob Wilsonbf854f02014-02-17 19:21:09 +0000696 RecordStmtCount(E);
Bob Wilsond8931422014-04-11 17:16:13 +0000697 // Counter tracks the "true" part of a conditional operator. The
698 // count in the "false" part will be calculated from this counter.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000699 RegionCounter Cnt(PGO, E);
700 Visit(E->getCond());
701
702 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000703 CountMap[E->getTrueExpr()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000704 Visit(E->getTrueExpr());
705 Cnt.adjustForControlFlow();
706
707 Cnt.beginElseRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000708 CountMap[E->getFalseExpr()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000709 Visit(E->getFalseExpr());
710 Cnt.adjustForControlFlow();
711
712 Cnt.applyAdjustmentsToRegion(0);
713 RecordNextStmtCount = true;
714 }
715
716 void VisitBinLAnd(const BinaryOperator *E) {
717 RecordStmtCount(E);
Bob Wilsond8931422014-04-11 17:16:13 +0000718 // Counter tracks the right hand side of a logical and operator.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000719 RegionCounter Cnt(PGO, E);
720 Visit(E->getLHS());
721 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000722 CountMap[E->getRHS()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000723 Visit(E->getRHS());
724 Cnt.adjustForControlFlow();
725 Cnt.applyAdjustmentsToRegion(0);
726 RecordNextStmtCount = true;
727 }
728
729 void VisitBinLOr(const BinaryOperator *E) {
730 RecordStmtCount(E);
Bob Wilsond8931422014-04-11 17:16:13 +0000731 // Counter tracks the right hand side of a logical or operator.
Bob Wilsonbf854f02014-02-17 19:21:09 +0000732 RegionCounter Cnt(PGO, E);
733 Visit(E->getLHS());
734 Cnt.beginRegion();
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000735 CountMap[E->getRHS()] = PGO.getCurrentRegionCount();
Bob Wilsonbf854f02014-02-17 19:21:09 +0000736 Visit(E->getRHS());
737 Cnt.adjustForControlFlow();
738 Cnt.applyAdjustmentsToRegion(0);
739 RecordNextStmtCount = true;
740 }
741 };
Justin Bogneref512b92014-01-06 22:27:43 +0000742}
743
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000744void PGOHash::combine(HashType Type) {
745 // Check that we never combine 0 and only have six bits.
746 assert(Type && "Hash is invalid: unexpected type 0");
747 assert(unsigned(Type) < TooBig && "Hash is invalid: too many types");
748
749 // Pass through MD5 if enough work has built up.
750 if (Count && Count % NumTypesPerWord == 0) {
751 using namespace llvm::support;
752 uint64_t Swapped = endian::byte_swap<uint64_t, little>(Working);
753 MD5.update(llvm::makeArrayRef((uint8_t *)&Swapped, sizeof(Swapped)));
754 Working = 0;
755 }
756
757 // Accumulate the current type.
758 ++Count;
759 Working = Working << NumBitsPerType | Type;
760}
761
762uint64_t PGOHash::finalize() {
763 // Use Working as the hash directly if we never used MD5.
764 if (Count <= NumTypesPerWord)
765 // No need to byte swap here, since none of the math was endian-dependent.
766 // This number will be byte-swapped as required on endianness transitions,
767 // so we will see the same value on the other side.
768 return Working;
769
770 // Check for remaining work in Working.
771 if (Working)
772 MD5.update(Working);
773
774 // Finalize the MD5 and return the hash.
775 llvm::MD5::MD5Result Result;
776 MD5.final(Result);
777 using namespace llvm::support;
778 return endian::read<uint64_t, little, unaligned>(Result);
779}
780
Duncan P. N. Exon Smithd971cd12014-03-28 17:53:22 +0000781static void emitRuntimeHook(CodeGenModule &CGM) {
Duncan P. N. Exon Smith3fefedb2014-04-11 00:43:16 +0000782 const char *const RuntimeVarName = "__llvm_profile_runtime";
783 const char *const RuntimeUserName = "__llvm_profile_runtime_user";
Duncan P. N. Exon Smithd971cd12014-03-28 17:53:22 +0000784 if (CGM.getModule().getGlobalVariable(RuntimeVarName))
785 return;
786
787 // Declare the runtime hook.
788 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
789 auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
790 auto *Var = new llvm::GlobalVariable(CGM.getModule(), Int32Ty, false,
791 llvm::GlobalValue::ExternalLinkage,
792 nullptr, RuntimeVarName);
793
794 // Make a function that uses it.
795 auto *User = llvm::Function::Create(llvm::FunctionType::get(Int32Ty, false),
796 llvm::GlobalValue::LinkOnceODRLinkage,
797 RuntimeUserName, &CGM.getModule());
798 User->addFnAttr(llvm::Attribute::NoInline);
799 if (CGM.getCodeGenOpts().DisableRedZone)
800 User->addFnAttr(llvm::Attribute::NoRedZone);
801 CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", User));
802 auto *Load = Builder.CreateLoad(Var);
803 Builder.CreateRet(Load);
804
805 // Create a use of the function. Now the definition of the runtime variable
806 // should get pulled in, along with any static initializears.
807 CGM.addUsedGlobal(User);
808}
809
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000810void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) {
Justin Bogneref512b92014-01-06 22:27:43 +0000811 bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate;
Justin Bogner837a6f62014-04-18 21:52:00 +0000812 llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
813 if (!InstrumentRegions && !PGOReader)
Justin Bogneref512b92014-01-06 22:27:43 +0000814 return;
Justin Bogner3212b182014-04-25 07:20:05 +0000815 if (D->isImplicit())
Justin Bogneref512b92014-01-06 22:27:43 +0000816 return;
Bob Wilsonda1ebed2014-03-06 04:55:41 +0000817 setFuncName(Fn);
Duncan P. N. Exon Smith7c414512014-03-20 22:50:08 +0000818
819 // Set the linkage for variables based on the function linkage. Usually, we
820 // want to match it, but available_externally and extern_weak both have the
821 // wrong semantics.
Duncan P. N. Exon Smith73f78622014-03-20 22:49:50 +0000822 VarLinkage = Fn->getLinkage();
Duncan P. N. Exon Smith7c414512014-03-20 22:50:08 +0000823 switch (VarLinkage) {
824 case llvm::GlobalValue::ExternalWeakLinkage:
825 VarLinkage = llvm::GlobalValue::LinkOnceAnyLinkage;
826 break;
827 case llvm::GlobalValue::AvailableExternallyLinkage:
828 VarLinkage = llvm::GlobalValue::LinkOnceODRLinkage;
829 break;
830 default:
831 break;
832 }
833
Justin Bogneref512b92014-01-06 22:27:43 +0000834 mapRegionCounters(D);
Duncan P. N. Exon Smithd971cd12014-03-28 17:53:22 +0000835 if (InstrumentRegions) {
836 emitRuntimeHook(CGM);
Justin Bogneref512b92014-01-06 22:27:43 +0000837 emitCounterVariables();
Duncan P. N. Exon Smithd971cd12014-03-28 17:53:22 +0000838 }
Justin Bogner837a6f62014-04-18 21:52:00 +0000839 if (PGOReader) {
840 loadRegionCounts(PGOReader);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000841 computeRegionCounts(D);
Justin Bogner837a6f62014-04-18 21:52:00 +0000842 applyFunctionAttributes(PGOReader, Fn);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000843 }
Justin Bogneref512b92014-01-06 22:27:43 +0000844}
845
846void CodeGenPGO::mapRegionCounters(const Decl *D) {
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +0000847 RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000848 MapRegionCounters Walker(*RegionCounterMap);
Justin Bogneref512b92014-01-06 22:27:43 +0000849 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000850 Walker.TraverseDecl(const_cast<FunctionDecl *>(FD));
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000851 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000852 Walker.TraverseDecl(const_cast<ObjCMethodDecl *>(MD));
Bob Wilsonc845c002014-03-06 20:24:27 +0000853 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
Bob Wilsond8931422014-04-11 17:16:13 +0000854 Walker.TraverseDecl(const_cast<BlockDecl *>(BD));
Justin Bogner81ab90f2014-04-15 00:50:54 +0000855 else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
856 Walker.TraverseDecl(const_cast<CapturedDecl *>(CD));
Justin Bogner3212b182014-04-25 07:20:05 +0000857 assert(Walker.NextCounter > 0 && "no entry counter mapped for decl");
Justin Bogneref512b92014-01-06 22:27:43 +0000858 NumRegionCounters = Walker.NextCounter;
Duncan P. N. Exon Smith4bc77312014-04-16 16:03:27 +0000859 FunctionHash = Walker.Hash.finalize();
Justin Bogneref512b92014-01-06 22:27:43 +0000860}
861
Bob Wilsonbf854f02014-02-17 19:21:09 +0000862void CodeGenPGO::computeRegionCounts(const Decl *D) {
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +0000863 StmtCountMap.reset(new llvm::DenseMap<const Stmt *, uint64_t>);
Duncan P. N. Exon Smith3586be72014-03-26 19:26:02 +0000864 ComputeRegionCounts Walker(*StmtCountMap, *this);
Bob Wilsonbf854f02014-02-17 19:21:09 +0000865 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
866 Walker.VisitFunctionDecl(FD);
Bob Wilson5ec8fe12014-03-06 06:10:02 +0000867 else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
868 Walker.VisitObjCMethodDecl(MD);
Bob Wilsonc845c002014-03-06 20:24:27 +0000869 else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
870 Walker.VisitBlockDecl(BD);
Justin Bogner81ab90f2014-04-15 00:50:54 +0000871 else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
872 Walker.VisitCapturedDecl(const_cast<CapturedDecl *>(CD));
Bob Wilsonbf854f02014-02-17 19:21:09 +0000873}
874
Justin Bogner837a6f62014-04-18 21:52:00 +0000875void
876CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
877 llvm::Function *Fn) {
Justin Bogner4c9c45c2014-03-12 18:14:32 +0000878 if (!haveRegionCounts())
879 return;
880
Justin Bogner837a6f62014-04-18 21:52:00 +0000881 uint64_t MaxFunctionCount = PGOReader->getMaximumFunctionCount();
Justin Bogner4c9c45c2014-03-12 18:14:32 +0000882 uint64_t FunctionCount = getRegionCount(0);
883 if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount))
884 // Turn on InlineHint attribute for hot functions.
885 // FIXME: 30% is from preliminary tuning on SPEC, it may not be optimal.
886 Fn->addFnAttr(llvm::Attribute::InlineHint);
887 else if (FunctionCount <= (uint64_t)(0.01 * (double)MaxFunctionCount))
888 // Turn on Cold attribute for cold functions.
889 // FIXME: 1% is from preliminary tuning on SPEC, it may not be optimal.
890 Fn->addFnAttr(llvm::Attribute::Cold);
891}
892
Justin Bogneref512b92014-01-06 22:27:43 +0000893void CodeGenPGO::emitCounterVariables() {
894 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
895 llvm::ArrayType *CounterTy = llvm::ArrayType::get(llvm::Type::getInt64Ty(Ctx),
896 NumRegionCounters);
897 RegionCounters =
Duncan P. N. Exon Smith73f78622014-03-20 22:49:50 +0000898 new llvm::GlobalVariable(CGM.getModule(), CounterTy, false, VarLinkage,
Justin Bogneref512b92014-01-06 22:27:43 +0000899 llvm::Constant::getNullValue(CounterTy),
Duncan P. N. Exon Smith2fe531c2014-03-17 21:18:30 +0000900 getFuncVarName("counters"));
901 RegionCounters->setAlignment(8);
902 RegionCounters->setSection(getCountersSection(CGM));
Justin Bogneref512b92014-01-06 22:27:43 +0000903}
904
905void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) {
Bob Wilson749ebc72014-03-06 04:55:28 +0000906 if (!RegionCounters)
Justin Bogneref512b92014-01-06 22:27:43 +0000907 return;
908 llvm::Value *Addr =
909 Builder.CreateConstInBoundsGEP2_64(RegionCounters, 0, Counter);
910 llvm::Value *Count = Builder.CreateLoad(Addr, "pgocount");
911 Count = Builder.CreateAdd(Count, Builder.getInt64(1));
912 Builder.CreateStore(Count, Addr);
913}
914
Justin Bogner837a6f62014-04-18 21:52:00 +0000915void CodeGenPGO::loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader) {
Justin Bognere2ef2a02014-04-15 21:22:35 +0000916 CGM.getPGOStats().Visited++;
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +0000917 RegionCounts.reset(new std::vector<uint64_t>);
Justin Bognerb4416f52014-03-18 21:58:06 +0000918 uint64_t Hash;
Justin Bogner837a6f62014-04-18 21:52:00 +0000919 if (PGOReader->getFunctionCounts(getFuncName(), Hash, *RegionCounts)) {
Justin Bognere2ef2a02014-04-15 21:22:35 +0000920 CGM.getPGOStats().Missing++;
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +0000921 RegionCounts.reset();
Justin Bognere2ef2a02014-04-15 21:22:35 +0000922 } else if (Hash != FunctionHash ||
923 RegionCounts->size() != NumRegionCounters) {
924 CGM.getPGOStats().Mismatched++;
925 RegionCounts.reset();
926 }
Justin Bogneref512b92014-01-06 22:27:43 +0000927}
928
929void CodeGenPGO::destroyRegionCounters() {
Duncan P. N. Exon Smith1b67cfd2014-03-26 19:26:05 +0000930 RegionCounterMap.reset();
931 StmtCountMap.reset();
932 RegionCounts.reset();
Justin Bogner3212b182014-04-25 07:20:05 +0000933 RegionCounters = nullptr;
Justin Bogneref512b92014-01-06 22:27:43 +0000934}
935
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000936/// \brief Calculate what to divide by to scale weights.
937///
938/// Given the maximum weight, calculate a divisor that will scale all the
939/// weights to strictly less than UINT32_MAX.
940static uint64_t calculateWeightScale(uint64_t MaxWeight) {
941 return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1;
942}
943
944/// \brief Scale an individual branch weight (and add 1).
945///
946/// Scale a 64-bit weight down to 32-bits using \c Scale.
947///
948/// According to Laplace's Rule of Succession, it is better to compute the
949/// weight based on the count plus 1, so universally add 1 to the value.
950///
951/// \pre \c Scale was calculated by \a calculateWeightScale() with a weight no
952/// greater than \c Weight.
953static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale) {
954 assert(Scale && "scale by 0?");
955 uint64_t Scaled = Weight / Scale + 1;
956 assert(Scaled <= UINT32_MAX && "overflow 32-bits");
957 return Scaled;
958}
959
Justin Bogneref512b92014-01-06 22:27:43 +0000960llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount,
961 uint64_t FalseCount) {
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000962 // Check for empty weights.
Justin Bogneref512b92014-01-06 22:27:43 +0000963 if (!TrueCount && !FalseCount)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000964 return nullptr;
Justin Bogneref512b92014-01-06 22:27:43 +0000965
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000966 // Calculate how to scale down to 32-bits.
967 uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount));
968
Justin Bogneref512b92014-01-06 22:27:43 +0000969 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000970 return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale),
971 scaleBranchWeight(FalseCount, Scale));
Justin Bogneref512b92014-01-06 22:27:43 +0000972}
973
Bob Wilson95a27b02014-02-17 19:20:59 +0000974llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) {
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000975 // We need at least two elements to create meaningful weights.
976 if (Weights.size() < 2)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000977 return nullptr;
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000978
Justin Bognerf3aefca2014-04-04 02:48:51 +0000979 // Check for empty weights.
980 uint64_t MaxWeight = *std::max_element(Weights.begin(), Weights.end());
981 if (MaxWeight == 0)
982 return nullptr;
983
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000984 // Calculate how to scale down to 32-bits.
Justin Bognerf3aefca2014-04-04 02:48:51 +0000985 uint64_t Scale = calculateWeightScale(MaxWeight);
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000986
Justin Bogneref512b92014-01-06 22:27:43 +0000987 SmallVector<uint32_t, 16> ScaledWeights;
988 ScaledWeights.reserve(Weights.size());
Duncan P. N. Exon Smith38402dc2014-03-11 18:18:10 +0000989 for (uint64_t W : Weights)
990 ScaledWeights.push_back(scaleBranchWeight(W, Scale));
991
992 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
Justin Bogneref512b92014-01-06 22:27:43 +0000993 return MDHelper.createBranchWeights(ScaledWeights);
994}
Bob Wilsonbf854f02014-02-17 19:21:09 +0000995
996llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond,
997 RegionCounter &Cnt) {
998 if (!haveRegionCounts())
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +0000999 return nullptr;
Bob Wilsonbf854f02014-02-17 19:21:09 +00001000 uint64_t LoopCount = Cnt.getCount();
1001 uint64_t CondCount = 0;
1002 bool Found = getStmtCount(Cond, CondCount);
1003 assert(Found && "missing expected loop condition count");
1004 (void)Found;
1005 if (CondCount == 0)
Duncan P. N. Exon Smitha5f804a2014-03-20 18:40:55 +00001006 return nullptr;
Bob Wilsonbf854f02014-02-17 19:21:09 +00001007 return createBranchWeights(LoopCount,
1008 std::max(CondCount, LoopCount) - LoopCount);
1009}