Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1 | //===--- 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 Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 19 | #include "llvm/ProfileData/InstrProfReader.h" |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Endian.h" |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileSystem.h" |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MD5.h" |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 27 | void CodeGenPGO::setFuncName(llvm::Function *Fn) { |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 28 | RawFuncName = Fn->getName(); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 29 | |
| 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 Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 33 | if (RawFuncName[0] == '\1') |
| 34 | RawFuncName = RawFuncName.substr(1); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 35 | |
| 36 | if (!Fn->hasLocalLinkage()) { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 37 | PrefixedFuncName.reset(new std::string(RawFuncName)); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 38 | 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 Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 45 | PrefixedFuncName.reset(new std::string(CGM.getCodeGenOpts().MainFileName)); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 46 | if (PrefixedFuncName->empty()) |
| 47 | PrefixedFuncName->assign("<unknown>"); |
| 48 | PrefixedFuncName->append(":"); |
| 49 | PrefixedFuncName->append(RawFuncName); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 52 | static llvm::Function *getRegisterFunc(CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 53 | return CGM.getModule().getFunction("__llvm_profile_register_functions"); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static llvm::BasicBlock *getOrInsertRegisterBB(CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | 780443e | 2014-03-20 03:57:11 +0000 | [diff] [blame] | 57 | // 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 Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 61 | // 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 Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 70 | "__llvm_profile_register_functions", |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 71 | &CGM.getModule()); |
| 72 | RegisterF->setUnnamedAddr(true); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 73 | 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 | |
| 83 | static 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 Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 87 | return CGM.getModule().getOrInsertFunction("__llvm_profile_register_function", |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 88 | RuntimeRegisterTy); |
| 89 | } |
| 90 | |
Duncan P. N. Exon Smith | 7134d47 | 2014-03-20 03:17:15 +0000 | [diff] [blame] | 91 | static bool isMachO(const CodeGenModule &CGM) { |
| 92 | return CGM.getTarget().getTriple().isOSBinFormatMachO(); |
| 93 | } |
| 94 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 95 | static StringRef getCountersSection(const CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 96 | return isMachO(CGM) ? "__DATA,__llvm_prf_cnts" : "__llvm_prf_cnts"; |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static StringRef getNameSection(const CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 100 | return isMachO(CGM) ? "__DATA,__llvm_prf_names" : "__llvm_prf_names"; |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static StringRef getDataSection(const CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 104 | return isMachO(CGM) ? "__DATA,__llvm_prf_data" : "__llvm_prf_data"; |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | llvm::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 Smith | 73f7862 | 2014-03-20 22:49:50 +0000 | [diff] [blame] | 113 | true, VarLinkage, VarName, |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 114 | getFuncVarName("name")); |
| 115 | Name->setSection(getNameSection(CGM)); |
| 116 | Name->setAlignment(1); |
| 117 | |
| 118 | // Create data variable. |
| 119 | auto *Int32Ty = llvm::Type::getInt32Ty(Ctx); |
Justin Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 120 | auto *Int64Ty = llvm::Type::getInt64Ty(Ctx); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 121 | auto *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx); |
| 122 | auto *Int64PtrTy = llvm::Type::getInt64PtrTy(Ctx); |
| 123 | llvm::Type *DataTypes[] = { |
Justin Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 124 | Int32Ty, Int32Ty, Int64Ty, Int8PtrTy, Int64PtrTy |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 125 | }; |
| 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 Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 130 | llvm::ConstantInt::get(Int64Ty, FunctionHash), |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 131 | llvm::ConstantExpr::getBitCast(Name, Int8PtrTy), |
| 132 | llvm::ConstantExpr::getBitCast(RegionCounters, Int64PtrTy) |
| 133 | }; |
| 134 | auto *Data = |
Duncan P. N. Exon Smith | 73f7862 | 2014-03-20 22:49:50 +0000 | [diff] [blame] | 135 | new llvm::GlobalVariable(CGM.getModule(), DataTy, true, VarLinkage, |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 136 | 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 Smith | 9121220 | 2014-05-16 01:24:00 +0000 | [diff] [blame] | 143 | // 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 Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 152 | // Make sure the data doesn't get deleted. |
| 153 | CGM.addUsedGlobal(Data); |
| 154 | return Data; |
| 155 | } |
| 156 | |
| 157 | void CodeGenPGO::emitInstrumentationData() { |
Justin Bogner | 3212b18 | 2014-04-25 07:20:05 +0000 | [diff] [blame] | 158 | if (!RegionCounters) |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 159 | return; |
| 160 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 161 | // Build the data. |
| 162 | auto *Data = buildDataVar(); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 163 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 164 | // Register the data. |
Duncan P. N. Exon Smith | 780443e | 2014-03-20 03:57:11 +0000 | [diff] [blame] | 165 | auto *RegisterBB = getOrInsertRegisterBB(CGM); |
| 166 | if (!RegisterBB) |
| 167 | return; |
| 168 | CGBuilderTy Builder(RegisterBB->getTerminator()); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 169 | auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
| 170 | Builder.CreateCall(getOrInsertRuntimeRegister(CGM), |
| 171 | Builder.CreateBitCast(Data, VoidPtrTy)); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | llvm::Function *CodeGenPGO::emitInitialization(CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 175 | if (!CGM.getCodeGenOpts().ProfileInstrGenerate) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 176 | return nullptr; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 177 | |
Justin Bogner | f2ea775 | 2014-04-10 18:13:13 +0000 | [diff] [blame] | 178 | assert(CGM.getModule().getFunction("__llvm_profile_init") == nullptr && |
| 179 | "profile initialization already emitted"); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 180 | |
Duncan P. N. Exon Smith | 5188e91 | 2014-03-20 19:23:46 +0000 | [diff] [blame] | 181 | // Get the function to call at initialization. |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 182 | llvm::Constant *RegisterF = getRegisterFunc(CGM); |
Duncan P. N. Exon Smith | 5188e91 | 2014-03-20 19:23:46 +0000 | [diff] [blame] | 183 | if (!RegisterF) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 184 | return nullptr; |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 185 | |
| 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 Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 190 | "__llvm_profile_init", &CGM.getModule()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 191 | F->setUnnamedAddr(true); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 192 | F->addFnAttr(llvm::Attribute::NoInline); |
| 193 | if (CGM.getCodeGenOpts().DisableRedZone) |
| 194 | F->addFnAttr(llvm::Attribute::NoRedZone); |
| 195 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 196 | // Add the basic block and the necessary calls. |
| 197 | CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", F)); |
Duncan P. N. Exon Smith | 5188e91 | 2014-03-20 19:23:46 +0000 | [diff] [blame] | 198 | Builder.CreateCall(RegisterF); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 199 | Builder.CreateRetVoid(); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 200 | |
| 201 | return F; |
| 202 | } |
| 203 | |
| 204 | namespace { |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 205 | /// \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. |
| 215 | class 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 | |
| 224 | public: |
| 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 | }; |
| 262 | const int PGOHash::NumBitsPerType; |
| 263 | const unsigned PGOHash::NumTypesPerWord; |
| 264 | const unsigned PGOHash::TooBig; |
| 265 | |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 266 | /// A RecursiveASTVisitor that fills a map of statements to PGO counters. |
| 267 | struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> { |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 268 | /// The next counter value to assign. |
| 269 | unsigned NextCounter; |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 270 | /// The function hash. |
| 271 | PGOHash Hash; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 272 | /// The map of statements to counters. |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 273 | llvm::DenseMap<const Stmt *, unsigned> &CounterMap; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 274 | |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 275 | MapRegionCounters(llvm::DenseMap<const Stmt *, unsigned> &CounterMap) |
| 276 | : NextCounter(0), CounterMap(CounterMap) {} |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 277 | |
Justin Bogner | 191ec63 | 2014-04-11 23:06:35 +0000 | [diff] [blame] | 278 | // 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 Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 282 | bool TraverseCapturedStmt(CapturedStmt *CS) { return true; } |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 283 | |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 284 | 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 Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 295 | case Decl::Captured: |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 296 | CounterMap[D->getBody()] = NextCounter++; |
| 297 | break; |
| 298 | } |
| 299 | return true; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 300 | } |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 301 | |
| 302 | bool VisitStmt(const Stmt *S) { |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 303 | 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 Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 312 | switch (S->getStmtClass()) { |
| 313 | default: |
| 314 | break; |
| 315 | case Stmt::LabelStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 316 | return PGOHash::LabelStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 317 | case Stmt::WhileStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 318 | return PGOHash::WhileStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 319 | case Stmt::DoStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 320 | return PGOHash::DoStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 321 | case Stmt::ForStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 322 | return PGOHash::ForStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 323 | case Stmt::CXXForRangeStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 324 | return PGOHash::CXXForRangeStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 325 | case Stmt::ObjCForCollectionStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 326 | return PGOHash::ObjCForCollectionStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 327 | case Stmt::SwitchStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 328 | return PGOHash::SwitchStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 329 | case Stmt::CaseStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 330 | return PGOHash::CaseStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 331 | case Stmt::DefaultStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 332 | return PGOHash::DefaultStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 333 | case Stmt::IfStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 334 | return PGOHash::IfStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 335 | case Stmt::CXXTryStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 336 | return PGOHash::CXXTryStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 337 | case Stmt::CXXCatchStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 338 | return PGOHash::CXXCatchStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 339 | case Stmt::ConditionalOperatorClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 340 | return PGOHash::ConditionalOperator; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 341 | case Stmt::BinaryConditionalOperatorClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 342 | return PGOHash::BinaryConditionalOperator; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 343 | case Stmt::BinaryOperatorClass: { |
| 344 | const BinaryOperator *BO = cast<BinaryOperator>(S); |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 345 | if (BO->getOpcode() == BO_LAnd) |
| 346 | return PGOHash::BinaryOperatorLAnd; |
| 347 | if (BO->getOpcode() == BO_LOr) |
| 348 | return PGOHash::BinaryOperatorLOr; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 349 | break; |
| 350 | } |
| 351 | } |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 352 | return PGOHash::None; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 353 | } |
| 354 | }; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 355 | |
| 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 Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 367 | llvm::DenseMap<const Stmt *, uint64_t> &CountMap; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 368 | |
Justin Bogner | fc83c11 | 2014-04-18 21:51:09 +0000 | [diff] [blame] | 369 | /// BreakContinueStack - Keep counts of breaks and continues inside loops. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 370 | 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 Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 377 | ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &CountMap, |
| 378 | CodeGenPGO &PGO) |
| 379 | : PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {} |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 380 | |
| 381 | void RecordStmtCount(const Stmt *S) { |
| 382 | if (RecordNextStmtCount) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 383 | CountMap[S] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 384 | 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 Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 396 | void VisitFunctionDecl(const FunctionDecl *D) { |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 397 | // Counter tracks entry to the function body. |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 398 | RegionCounter Cnt(PGO, D->getBody()); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 399 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 400 | CountMap[D->getBody()] = PGO.getCurrentRegionCount(); |
| 401 | Visit(D->getBody()); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Justin Bogner | 191ec63 | 2014-04-11 23:06:35 +0000 | [diff] [blame] | 404 | // 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 Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 409 | 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 Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 417 | void VisitObjCMethodDecl(const ObjCMethodDecl *D) { |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 418 | // Counter tracks entry to the method body. |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 419 | RegionCounter Cnt(PGO, D->getBody()); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 420 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 421 | CountMap[D->getBody()] = PGO.getCurrentRegionCount(); |
| 422 | Visit(D->getBody()); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 425 | void VisitBlockDecl(const BlockDecl *D) { |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 426 | // Counter tracks entry to the block body. |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 427 | RegionCounter Cnt(PGO, D->getBody()); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 428 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 429 | CountMap[D->getBody()] = PGO.getCurrentRegionCount(); |
| 430 | Visit(D->getBody()); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 433 | 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 Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 449 | // Counter tracks the block following the label. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 450 | RegionCounter Cnt(PGO, S); |
| 451 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 452 | CountMap[S] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 453 | 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 Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 474 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 475 | 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 Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 480 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 481 | 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 Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 491 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 492 | 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 Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 500 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 501 | RegionCounter Cnt(PGO, S); |
| 502 | BreakContinueStack.push_back(BreakContinue()); |
| 503 | Cnt.beginRegion(/*AddIncomingFallThrough=*/true); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 504 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 505 | 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 Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 516 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 517 | 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 Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 527 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 528 | 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 Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 533 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 534 | 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 Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 542 | CountMap[S->getInc()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 543 | 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 Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 554 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 555 | 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 Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 566 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 567 | 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 Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 572 | CountMap[S->getLoopVarStmt()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 573 | 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 Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 581 | CountMap[S->getInc()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 582 | 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 Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 591 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 592 | 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 Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 601 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 602 | RegionCounter Cnt(PGO, S); |
| 603 | BreakContinueStack.push_back(BreakContinue()); |
| 604 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 605 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 606 | 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 Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 623 | // Counter tracks the exit block of the switch. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 624 | RegionCounter ExitCnt(PGO, S); |
| 625 | ExitCnt.beginRegion(); |
| 626 | RecordNextStmtCount = true; |
| 627 | } |
| 628 | |
| 629 | void VisitCaseStmt(const CaseStmt *S) { |
| 630 | RecordNextStmtCount = false; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 631 | // 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 Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 634 | RegionCounter Cnt(PGO, S); |
| 635 | Cnt.beginRegion(/*AddIncomingFallThrough=*/true); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 636 | CountMap[S] = Cnt.getCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 637 | RecordNextStmtCount = true; |
| 638 | Visit(S->getSubStmt()); |
| 639 | } |
| 640 | |
| 641 | void VisitDefaultStmt(const DefaultStmt *S) { |
| 642 | RecordNextStmtCount = false; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 643 | // Counter for this default case. This does not include fallthrough from |
| 644 | // the previous case. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 645 | RegionCounter Cnt(PGO, S); |
| 646 | Cnt.beginRegion(/*AddIncomingFallThrough=*/true); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 647 | CountMap[S] = Cnt.getCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 648 | RecordNextStmtCount = true; |
| 649 | Visit(S->getSubStmt()); |
| 650 | } |
| 651 | |
| 652 | void VisitIfStmt(const IfStmt *S) { |
| 653 | RecordStmtCount(S); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 654 | // 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 Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 656 | RegionCounter Cnt(PGO, S); |
| 657 | Visit(S->getCond()); |
| 658 | |
| 659 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 660 | CountMap[S->getThen()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 661 | Visit(S->getThen()); |
| 662 | Cnt.adjustForControlFlow(); |
| 663 | |
| 664 | if (S->getElse()) { |
| 665 | Cnt.beginElseRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 666 | CountMap[S->getElse()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 667 | 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 Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 679 | // Counter tracks the continuation block of the try statement. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 680 | RegionCounter Cnt(PGO, S); |
| 681 | Cnt.beginRegion(); |
| 682 | RecordNextStmtCount = true; |
| 683 | } |
| 684 | |
| 685 | void VisitCXXCatchStmt(const CXXCatchStmt *S) { |
| 686 | RecordNextStmtCount = false; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 687 | // Counter tracks the catch statement's handler block. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 688 | RegionCounter Cnt(PGO, S); |
| 689 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 690 | CountMap[S] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 691 | Visit(S->getHandlerBlock()); |
| 692 | } |
| 693 | |
Justin Bogner | 53c55d9 | 2014-04-11 06:10:10 +0000 | [diff] [blame] | 694 | void VisitAbstractConditionalOperator( |
| 695 | const AbstractConditionalOperator *E) { |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 696 | RecordStmtCount(E); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 697 | // Counter tracks the "true" part of a conditional operator. The |
| 698 | // count in the "false" part will be calculated from this counter. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 699 | RegionCounter Cnt(PGO, E); |
| 700 | Visit(E->getCond()); |
| 701 | |
| 702 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 703 | CountMap[E->getTrueExpr()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 704 | Visit(E->getTrueExpr()); |
| 705 | Cnt.adjustForControlFlow(); |
| 706 | |
| 707 | Cnt.beginElseRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 708 | CountMap[E->getFalseExpr()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 709 | 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 Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 718 | // Counter tracks the right hand side of a logical and operator. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 719 | RegionCounter Cnt(PGO, E); |
| 720 | Visit(E->getLHS()); |
| 721 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 722 | CountMap[E->getRHS()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 723 | 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 Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 731 | // Counter tracks the right hand side of a logical or operator. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 732 | RegionCounter Cnt(PGO, E); |
| 733 | Visit(E->getLHS()); |
| 734 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 735 | CountMap[E->getRHS()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 736 | Visit(E->getRHS()); |
| 737 | Cnt.adjustForControlFlow(); |
| 738 | Cnt.applyAdjustmentsToRegion(0); |
| 739 | RecordNextStmtCount = true; |
| 740 | } |
| 741 | }; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 744 | void 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 | |
| 762 | uint64_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 Smith | d971cd1 | 2014-03-28 17:53:22 +0000 | [diff] [blame] | 781 | static void emitRuntimeHook(CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | 3fefedb | 2014-04-11 00:43:16 +0000 | [diff] [blame] | 782 | const char *const RuntimeVarName = "__llvm_profile_runtime"; |
| 783 | const char *const RuntimeUserName = "__llvm_profile_runtime_user"; |
Duncan P. N. Exon Smith | d971cd1 | 2014-03-28 17:53:22 +0000 | [diff] [blame] | 784 | 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 Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 810 | void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) { |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 811 | bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate; |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 812 | llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader(); |
| 813 | if (!InstrumentRegions && !PGOReader) |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 814 | return; |
Justin Bogner | 3212b18 | 2014-04-25 07:20:05 +0000 | [diff] [blame] | 815 | if (D->isImplicit()) |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 816 | return; |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 817 | setFuncName(Fn); |
Duncan P. N. Exon Smith | 7c41451 | 2014-03-20 22:50:08 +0000 | [diff] [blame] | 818 | |
| 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 Smith | 73f7862 | 2014-03-20 22:49:50 +0000 | [diff] [blame] | 822 | VarLinkage = Fn->getLinkage(); |
Duncan P. N. Exon Smith | 7c41451 | 2014-03-20 22:50:08 +0000 | [diff] [blame] | 823 | 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 Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 834 | mapRegionCounters(D); |
Duncan P. N. Exon Smith | d971cd1 | 2014-03-28 17:53:22 +0000 | [diff] [blame] | 835 | if (InstrumentRegions) { |
| 836 | emitRuntimeHook(CGM); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 837 | emitCounterVariables(); |
Duncan P. N. Exon Smith | d971cd1 | 2014-03-28 17:53:22 +0000 | [diff] [blame] | 838 | } |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 839 | if (PGOReader) { |
| 840 | loadRegionCounts(PGOReader); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 841 | computeRegionCounts(D); |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 842 | applyFunctionAttributes(PGOReader, Fn); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 843 | } |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | void CodeGenPGO::mapRegionCounters(const Decl *D) { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 847 | RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 848 | MapRegionCounters Walker(*RegionCounterMap); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 849 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 850 | Walker.TraverseDecl(const_cast<FunctionDecl *>(FD)); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 851 | else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 852 | Walker.TraverseDecl(const_cast<ObjCMethodDecl *>(MD)); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 853 | else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D)) |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 854 | Walker.TraverseDecl(const_cast<BlockDecl *>(BD)); |
Justin Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 855 | else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D)) |
| 856 | Walker.TraverseDecl(const_cast<CapturedDecl *>(CD)); |
Justin Bogner | 3212b18 | 2014-04-25 07:20:05 +0000 | [diff] [blame] | 857 | assert(Walker.NextCounter > 0 && "no entry counter mapped for decl"); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 858 | NumRegionCounters = Walker.NextCounter; |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 859 | FunctionHash = Walker.Hash.finalize(); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 862 | void CodeGenPGO::computeRegionCounts(const Decl *D) { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 863 | StmtCountMap.reset(new llvm::DenseMap<const Stmt *, uint64_t>); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 864 | ComputeRegionCounts Walker(*StmtCountMap, *this); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 865 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 866 | Walker.VisitFunctionDecl(FD); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 867 | else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) |
| 868 | Walker.VisitObjCMethodDecl(MD); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 869 | else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D)) |
| 870 | Walker.VisitBlockDecl(BD); |
Justin Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 871 | else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D)) |
| 872 | Walker.VisitCapturedDecl(const_cast<CapturedDecl *>(CD)); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 875 | void |
| 876 | CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader, |
| 877 | llvm::Function *Fn) { |
Justin Bogner | 4c9c45c | 2014-03-12 18:14:32 +0000 | [diff] [blame] | 878 | if (!haveRegionCounts()) |
| 879 | return; |
| 880 | |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 881 | uint64_t MaxFunctionCount = PGOReader->getMaximumFunctionCount(); |
Justin Bogner | 4c9c45c | 2014-03-12 18:14:32 +0000 | [diff] [blame] | 882 | 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 Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 893 | void 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 Smith | 73f7862 | 2014-03-20 22:49:50 +0000 | [diff] [blame] | 898 | new llvm::GlobalVariable(CGM.getModule(), CounterTy, false, VarLinkage, |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 899 | llvm::Constant::getNullValue(CounterTy), |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 900 | getFuncVarName("counters")); |
| 901 | RegionCounters->setAlignment(8); |
| 902 | RegionCounters->setSection(getCountersSection(CGM)); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) { |
Bob Wilson | 749ebc7 | 2014-03-06 04:55:28 +0000 | [diff] [blame] | 906 | if (!RegionCounters) |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 907 | 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 Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 915 | void CodeGenPGO::loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader) { |
Justin Bogner | e2ef2a0 | 2014-04-15 21:22:35 +0000 | [diff] [blame] | 916 | CGM.getPGOStats().Visited++; |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 917 | RegionCounts.reset(new std::vector<uint64_t>); |
Justin Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 918 | uint64_t Hash; |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 919 | if (PGOReader->getFunctionCounts(getFuncName(), Hash, *RegionCounts)) { |
Justin Bogner | e2ef2a0 | 2014-04-15 21:22:35 +0000 | [diff] [blame] | 920 | CGM.getPGOStats().Missing++; |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 921 | RegionCounts.reset(); |
Justin Bogner | e2ef2a0 | 2014-04-15 21:22:35 +0000 | [diff] [blame] | 922 | } else if (Hash != FunctionHash || |
| 923 | RegionCounts->size() != NumRegionCounters) { |
| 924 | CGM.getPGOStats().Mismatched++; |
| 925 | RegionCounts.reset(); |
| 926 | } |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | void CodeGenPGO::destroyRegionCounters() { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 930 | RegionCounterMap.reset(); |
| 931 | StmtCountMap.reset(); |
| 932 | RegionCounts.reset(); |
Justin Bogner | 3212b18 | 2014-04-25 07:20:05 +0000 | [diff] [blame] | 933 | RegionCounters = nullptr; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 936 | /// \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. |
| 940 | static 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. |
| 953 | static 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 Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 960 | llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount, |
| 961 | uint64_t FalseCount) { |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 962 | // Check for empty weights. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 963 | if (!TrueCount && !FalseCount) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 964 | return nullptr; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 965 | |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 966 | // Calculate how to scale down to 32-bits. |
| 967 | uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount)); |
| 968 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 969 | llvm::MDBuilder MDHelper(CGM.getLLVMContext()); |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 970 | return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale), |
| 971 | scaleBranchWeight(FalseCount, Scale)); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Bob Wilson | 95a27b0 | 2014-02-17 19:20:59 +0000 | [diff] [blame] | 974 | llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) { |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 975 | // We need at least two elements to create meaningful weights. |
| 976 | if (Weights.size() < 2) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 977 | return nullptr; |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 978 | |
Justin Bogner | f3aefca | 2014-04-04 02:48:51 +0000 | [diff] [blame] | 979 | // 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 Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 984 | // Calculate how to scale down to 32-bits. |
Justin Bogner | f3aefca | 2014-04-04 02:48:51 +0000 | [diff] [blame] | 985 | uint64_t Scale = calculateWeightScale(MaxWeight); |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 986 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 987 | SmallVector<uint32_t, 16> ScaledWeights; |
| 988 | ScaledWeights.reserve(Weights.size()); |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 989 | for (uint64_t W : Weights) |
| 990 | ScaledWeights.push_back(scaleBranchWeight(W, Scale)); |
| 991 | |
| 992 | llvm::MDBuilder MDHelper(CGM.getLLVMContext()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 993 | return MDHelper.createBranchWeights(ScaledWeights); |
| 994 | } |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 995 | |
| 996 | llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond, |
| 997 | RegionCounter &Cnt) { |
| 998 | if (!haveRegionCounts()) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 999 | return nullptr; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 1000 | 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 Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 1006 | return nullptr; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 1007 | return createBranchWeights(LoopCount, |
| 1008 | std::max(CondCount, LoopCount) - LoopCount); |
| 1009 | } |