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" |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 16 | #include "CoverageMappingGen.h" |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 17 | #include "clang/AST/RecursiveASTVisitor.h" |
| 18 | #include "clang/AST/StmtVisitor.h" |
| 19 | #include "llvm/IR/MDBuilder.h" |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 20 | #include "llvm/ProfileData/InstrProfReader.h" |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Endian.h" |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileSystem.h" |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MD5.h" |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
| 27 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 28 | void CodeGenPGO::setFuncName(StringRef Name, |
| 29 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 30 | RawFuncName = Name; |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 31 | |
| 32 | // Function names may be prefixed with a binary '1' to indicate |
| 33 | // that the backend should not modify the symbols due to any platform |
| 34 | // naming convention. Do not include that '1' in the PGO profile name. |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 35 | if (RawFuncName[0] == '\1') |
| 36 | RawFuncName = RawFuncName.substr(1); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 37 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 38 | if (!llvm::GlobalValue::isLocalLinkage(Linkage)) { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 39 | PrefixedFuncName.reset(new std::string(RawFuncName)); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 40 | return; |
| 41 | } |
| 42 | |
| 43 | // For local symbols, prepend the main file name to distinguish them. |
| 44 | // Do not include the full path in the file name since there's no guarantee |
| 45 | // that it will stay the same, e.g., if the files are checked out from |
| 46 | // version control in different locations. |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 47 | PrefixedFuncName.reset(new std::string(CGM.getCodeGenOpts().MainFileName)); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 48 | if (PrefixedFuncName->empty()) |
| 49 | PrefixedFuncName->assign("<unknown>"); |
| 50 | PrefixedFuncName->append(":"); |
| 51 | PrefixedFuncName->append(RawFuncName); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 54 | void CodeGenPGO::setFuncName(llvm::Function *Fn) { |
| 55 | setFuncName(Fn->getName(), Fn->getLinkage()); |
| 56 | } |
| 57 | |
| 58 | void CodeGenPGO::setVarLinkage(llvm::GlobalValue::LinkageTypes Linkage) { |
| 59 | // Set the linkage for variables based on the function linkage. Usually, we |
| 60 | // want to match it, but available_externally and extern_weak both have the |
| 61 | // wrong semantics. |
| 62 | VarLinkage = Linkage; |
| 63 | switch (VarLinkage) { |
| 64 | case llvm::GlobalValue::ExternalWeakLinkage: |
| 65 | VarLinkage = llvm::GlobalValue::LinkOnceAnyLinkage; |
| 66 | break; |
| 67 | case llvm::GlobalValue::AvailableExternallyLinkage: |
| 68 | VarLinkage = llvm::GlobalValue::LinkOnceODRLinkage; |
| 69 | break; |
| 70 | default: |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 75 | static llvm::Function *getRegisterFunc(CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 76 | return CGM.getModule().getFunction("__llvm_profile_register_functions"); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static llvm::BasicBlock *getOrInsertRegisterBB(CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | 780443e | 2014-03-20 03:57:11 +0000 | [diff] [blame] | 80 | // Don't do this for Darwin. compiler-rt uses linker magic. |
| 81 | if (CGM.getTarget().getTriple().isOSDarwin()) |
| 82 | return nullptr; |
| 83 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 84 | // Only need to insert this once per module. |
| 85 | if (llvm::Function *RegisterF = getRegisterFunc(CGM)) |
| 86 | return &RegisterF->getEntryBlock(); |
| 87 | |
| 88 | // Construct the function. |
| 89 | auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext()); |
| 90 | auto *RegisterFTy = llvm::FunctionType::get(VoidTy, false); |
| 91 | auto *RegisterF = llvm::Function::Create(RegisterFTy, |
| 92 | llvm::GlobalValue::InternalLinkage, |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 93 | "__llvm_profile_register_functions", |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 94 | &CGM.getModule()); |
| 95 | RegisterF->setUnnamedAddr(true); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 96 | if (CGM.getCodeGenOpts().DisableRedZone) |
| 97 | RegisterF->addFnAttr(llvm::Attribute::NoRedZone); |
| 98 | |
| 99 | // Construct and return the entry block. |
| 100 | auto *BB = llvm::BasicBlock::Create(CGM.getLLVMContext(), "", RegisterF); |
| 101 | CGBuilderTy Builder(BB); |
| 102 | Builder.CreateRetVoid(); |
| 103 | return BB; |
| 104 | } |
| 105 | |
| 106 | static llvm::Constant *getOrInsertRuntimeRegister(CodeGenModule &CGM) { |
| 107 | auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext()); |
| 108 | auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
| 109 | auto *RuntimeRegisterTy = llvm::FunctionType::get(VoidTy, VoidPtrTy, false); |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 110 | return CGM.getModule().getOrInsertFunction("__llvm_profile_register_function", |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 111 | RuntimeRegisterTy); |
| 112 | } |
| 113 | |
Duncan P. N. Exon Smith | 7134d47 | 2014-03-20 03:17:15 +0000 | [diff] [blame] | 114 | static bool isMachO(const CodeGenModule &CGM) { |
| 115 | return CGM.getTarget().getTriple().isOSBinFormatMachO(); |
| 116 | } |
| 117 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 118 | static StringRef getCountersSection(const CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 119 | 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] | 120 | } |
| 121 | |
| 122 | static StringRef getNameSection(const CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 123 | 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] | 124 | } |
| 125 | |
| 126 | static StringRef getDataSection(const CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 127 | 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] | 128 | } |
| 129 | |
| 130 | llvm::GlobalVariable *CodeGenPGO::buildDataVar() { |
| 131 | // Create name variable. |
| 132 | llvm::LLVMContext &Ctx = CGM.getLLVMContext(); |
| 133 | auto *VarName = llvm::ConstantDataArray::getString(Ctx, getFuncName(), |
| 134 | false); |
| 135 | auto *Name = new llvm::GlobalVariable(CGM.getModule(), VarName->getType(), |
Duncan P. N. Exon Smith | 73f7862 | 2014-03-20 22:49:50 +0000 | [diff] [blame] | 136 | true, VarLinkage, VarName, |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 137 | getFuncVarName("name")); |
| 138 | Name->setSection(getNameSection(CGM)); |
| 139 | Name->setAlignment(1); |
| 140 | |
| 141 | // Create data variable. |
| 142 | auto *Int32Ty = llvm::Type::getInt32Ty(Ctx); |
Justin Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 143 | auto *Int64Ty = llvm::Type::getInt64Ty(Ctx); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 144 | auto *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx); |
| 145 | auto *Int64PtrTy = llvm::Type::getInt64PtrTy(Ctx); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 146 | llvm::GlobalVariable *Data = nullptr; |
| 147 | if (RegionCounters) { |
| 148 | llvm::Type *DataTypes[] = { |
| 149 | Int32Ty, Int32Ty, Int64Ty, Int8PtrTy, Int64PtrTy |
| 150 | }; |
| 151 | auto *DataTy = llvm::StructType::get(Ctx, makeArrayRef(DataTypes)); |
| 152 | llvm::Constant *DataVals[] = { |
| 153 | llvm::ConstantInt::get(Int32Ty, getFuncName().size()), |
| 154 | llvm::ConstantInt::get(Int32Ty, NumRegionCounters), |
| 155 | llvm::ConstantInt::get(Int64Ty, FunctionHash), |
| 156 | llvm::ConstantExpr::getBitCast(Name, Int8PtrTy), |
| 157 | llvm::ConstantExpr::getBitCast(RegionCounters, Int64PtrTy) |
| 158 | }; |
| 159 | Data = |
| 160 | new llvm::GlobalVariable(CGM.getModule(), DataTy, true, VarLinkage, |
| 161 | llvm::ConstantStruct::get(DataTy, DataVals), |
| 162 | getFuncVarName("data")); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 163 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 164 | // All the data should be packed into an array in its own section. |
| 165 | Data->setSection(getDataSection(CGM)); |
| 166 | Data->setAlignment(8); |
| 167 | } |
| 168 | |
| 169 | // Create coverage mapping data variable. |
| 170 | if (!CoverageMapping.empty()) |
| 171 | CGM.getCoverageMapping()->addFunctionMappingRecord(Name, |
Alex Lorenz | f2cf38e | 2014-08-08 23:41:24 +0000 | [diff] [blame] | 172 | getFuncName(), |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 173 | CoverageMapping); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 174 | |
Duncan P. N. Exon Smith | 9121220 | 2014-05-16 01:24:00 +0000 | [diff] [blame] | 175 | // Hide all these symbols so that we correctly get a copy for each |
| 176 | // executable. The profile format expects names and counters to be |
| 177 | // contiguous, so references into shared objects would be invalid. |
| 178 | if (!llvm::GlobalValue::isLocalLinkage(VarLinkage)) { |
| 179 | Name->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 180 | if (Data) { |
| 181 | Data->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 182 | RegionCounters->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 183 | } |
Duncan P. N. Exon Smith | 9121220 | 2014-05-16 01:24:00 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 186 | // Make sure the data doesn't get deleted. |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 187 | if (Data) CGM.addUsedGlobal(Data); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 188 | return Data; |
| 189 | } |
| 190 | |
| 191 | void CodeGenPGO::emitInstrumentationData() { |
Justin Bogner | 3212b18 | 2014-04-25 07:20:05 +0000 | [diff] [blame] | 192 | if (!RegionCounters) |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 193 | return; |
| 194 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 195 | // Build the data. |
| 196 | auto *Data = buildDataVar(); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 197 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 198 | // Register the data. |
Duncan P. N. Exon Smith | 780443e | 2014-03-20 03:57:11 +0000 | [diff] [blame] | 199 | auto *RegisterBB = getOrInsertRegisterBB(CGM); |
| 200 | if (!RegisterBB) |
| 201 | return; |
| 202 | CGBuilderTy Builder(RegisterBB->getTerminator()); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 203 | auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
| 204 | Builder.CreateCall(getOrInsertRuntimeRegister(CGM), |
| 205 | Builder.CreateBitCast(Data, VoidPtrTy)); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | llvm::Function *CodeGenPGO::emitInitialization(CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 209 | if (!CGM.getCodeGenOpts().ProfileInstrGenerate) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 210 | return nullptr; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 211 | |
Justin Bogner | f2ea775 | 2014-04-10 18:13:13 +0000 | [diff] [blame] | 212 | assert(CGM.getModule().getFunction("__llvm_profile_init") == nullptr && |
| 213 | "profile initialization already emitted"); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 214 | |
Duncan P. N. Exon Smith | 5188e91 | 2014-03-20 19:23:46 +0000 | [diff] [blame] | 215 | // Get the function to call at initialization. |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 216 | llvm::Constant *RegisterF = getRegisterFunc(CGM); |
Duncan P. N. Exon Smith | 5188e91 | 2014-03-20 19:23:46 +0000 | [diff] [blame] | 217 | if (!RegisterF) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 218 | return nullptr; |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 219 | |
| 220 | // Create the initialization function. |
| 221 | auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext()); |
| 222 | auto *F = llvm::Function::Create(llvm::FunctionType::get(VoidTy, false), |
| 223 | llvm::GlobalValue::InternalLinkage, |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 224 | "__llvm_profile_init", &CGM.getModule()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 225 | F->setUnnamedAddr(true); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 226 | F->addFnAttr(llvm::Attribute::NoInline); |
| 227 | if (CGM.getCodeGenOpts().DisableRedZone) |
| 228 | F->addFnAttr(llvm::Attribute::NoRedZone); |
| 229 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 230 | // Add the basic block and the necessary calls. |
| 231 | CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", F)); |
Duncan P. N. Exon Smith | 5188e91 | 2014-03-20 19:23:46 +0000 | [diff] [blame] | 232 | Builder.CreateCall(RegisterF); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 233 | Builder.CreateRetVoid(); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 234 | |
| 235 | return F; |
| 236 | } |
| 237 | |
| 238 | namespace { |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 239 | /// \brief Stable hasher for PGO region counters. |
| 240 | /// |
| 241 | /// PGOHash produces a stable hash of a given function's control flow. |
| 242 | /// |
| 243 | /// Changing the output of this hash will invalidate all previously generated |
| 244 | /// profiles -- i.e., don't do it. |
| 245 | /// |
| 246 | /// \note When this hash does eventually change (years?), we still need to |
| 247 | /// support old hashes. We'll need to pull in the version number from the |
| 248 | /// profile data format and use the matching hash function. |
| 249 | class PGOHash { |
| 250 | uint64_t Working; |
| 251 | unsigned Count; |
| 252 | llvm::MD5 MD5; |
| 253 | |
| 254 | static const int NumBitsPerType = 6; |
| 255 | static const unsigned NumTypesPerWord = sizeof(uint64_t) * 8 / NumBitsPerType; |
| 256 | static const unsigned TooBig = 1u << NumBitsPerType; |
| 257 | |
| 258 | public: |
| 259 | /// \brief Hash values for AST nodes. |
| 260 | /// |
| 261 | /// Distinct values for AST nodes that have region counters attached. |
| 262 | /// |
| 263 | /// These values must be stable. All new members must be added at the end, |
| 264 | /// and no members should be removed. Changing the enumeration value for an |
| 265 | /// AST node will affect the hash of every function that contains that node. |
| 266 | enum HashType : unsigned char { |
| 267 | None = 0, |
| 268 | LabelStmt = 1, |
| 269 | WhileStmt, |
| 270 | DoStmt, |
| 271 | ForStmt, |
| 272 | CXXForRangeStmt, |
| 273 | ObjCForCollectionStmt, |
| 274 | SwitchStmt, |
| 275 | CaseStmt, |
| 276 | DefaultStmt, |
| 277 | IfStmt, |
| 278 | CXXTryStmt, |
| 279 | CXXCatchStmt, |
| 280 | ConditionalOperator, |
| 281 | BinaryOperatorLAnd, |
| 282 | BinaryOperatorLOr, |
| 283 | BinaryConditionalOperator, |
| 284 | |
| 285 | // Keep this last. It's for the static assert that follows. |
| 286 | LastHashType |
| 287 | }; |
| 288 | static_assert(LastHashType <= TooBig, "Too many types in HashType"); |
| 289 | |
| 290 | // TODO: When this format changes, take in a version number here, and use the |
| 291 | // old hash calculation for file formats that used the old hash. |
| 292 | PGOHash() : Working(0), Count(0) {} |
| 293 | void combine(HashType Type); |
| 294 | uint64_t finalize(); |
| 295 | }; |
| 296 | const int PGOHash::NumBitsPerType; |
| 297 | const unsigned PGOHash::NumTypesPerWord; |
| 298 | const unsigned PGOHash::TooBig; |
| 299 | |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 300 | /// A RecursiveASTVisitor that fills a map of statements to PGO counters. |
| 301 | struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> { |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 302 | /// The next counter value to assign. |
| 303 | unsigned NextCounter; |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 304 | /// The function hash. |
| 305 | PGOHash Hash; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 306 | /// The map of statements to counters. |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 307 | llvm::DenseMap<const Stmt *, unsigned> &CounterMap; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 308 | |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 309 | MapRegionCounters(llvm::DenseMap<const Stmt *, unsigned> &CounterMap) |
| 310 | : NextCounter(0), CounterMap(CounterMap) {} |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 311 | |
Justin Bogner | 191ec63 | 2014-04-11 23:06:35 +0000 | [diff] [blame] | 312 | // Blocks and lambdas are handled as separate functions, so we need not |
| 313 | // traverse them in the parent context. |
| 314 | bool TraverseBlockExpr(BlockExpr *BE) { return true; } |
| 315 | bool TraverseLambdaBody(LambdaExpr *LE) { return true; } |
Justin Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 316 | bool TraverseCapturedStmt(CapturedStmt *CS) { return true; } |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 317 | |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 318 | bool VisitDecl(const Decl *D) { |
| 319 | switch (D->getKind()) { |
| 320 | default: |
| 321 | break; |
| 322 | case Decl::Function: |
| 323 | case Decl::CXXMethod: |
| 324 | case Decl::CXXConstructor: |
| 325 | case Decl::CXXDestructor: |
| 326 | case Decl::CXXConversion: |
| 327 | case Decl::ObjCMethod: |
| 328 | case Decl::Block: |
Justin Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 329 | case Decl::Captured: |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 330 | CounterMap[D->getBody()] = NextCounter++; |
| 331 | break; |
| 332 | } |
| 333 | return true; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 334 | } |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 335 | |
| 336 | bool VisitStmt(const Stmt *S) { |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 337 | auto Type = getHashType(S); |
| 338 | if (Type == PGOHash::None) |
| 339 | return true; |
| 340 | |
| 341 | CounterMap[S] = NextCounter++; |
| 342 | Hash.combine(Type); |
| 343 | return true; |
| 344 | } |
| 345 | PGOHash::HashType getHashType(const Stmt *S) { |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 346 | switch (S->getStmtClass()) { |
| 347 | default: |
| 348 | break; |
| 349 | case Stmt::LabelStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 350 | return PGOHash::LabelStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 351 | case Stmt::WhileStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 352 | return PGOHash::WhileStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 353 | case Stmt::DoStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 354 | return PGOHash::DoStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 355 | case Stmt::ForStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 356 | return PGOHash::ForStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 357 | case Stmt::CXXForRangeStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 358 | return PGOHash::CXXForRangeStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 359 | case Stmt::ObjCForCollectionStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 360 | return PGOHash::ObjCForCollectionStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 361 | case Stmt::SwitchStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 362 | return PGOHash::SwitchStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 363 | case Stmt::CaseStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 364 | return PGOHash::CaseStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 365 | case Stmt::DefaultStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 366 | return PGOHash::DefaultStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 367 | case Stmt::IfStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 368 | return PGOHash::IfStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 369 | case Stmt::CXXTryStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 370 | return PGOHash::CXXTryStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 371 | case Stmt::CXXCatchStmtClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 372 | return PGOHash::CXXCatchStmt; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 373 | case Stmt::ConditionalOperatorClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 374 | return PGOHash::ConditionalOperator; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 375 | case Stmt::BinaryConditionalOperatorClass: |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 376 | return PGOHash::BinaryConditionalOperator; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 377 | case Stmt::BinaryOperatorClass: { |
| 378 | const BinaryOperator *BO = cast<BinaryOperator>(S); |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 379 | if (BO->getOpcode() == BO_LAnd) |
| 380 | return PGOHash::BinaryOperatorLAnd; |
| 381 | if (BO->getOpcode() == BO_LOr) |
| 382 | return PGOHash::BinaryOperatorLOr; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 383 | break; |
| 384 | } |
| 385 | } |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 386 | return PGOHash::None; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 387 | } |
| 388 | }; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 389 | |
| 390 | /// A StmtVisitor that propagates the raw counts through the AST and |
| 391 | /// records the count at statements where the value may change. |
| 392 | struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> { |
| 393 | /// PGO state. |
| 394 | CodeGenPGO &PGO; |
| 395 | |
| 396 | /// A flag that is set when the current count should be recorded on the |
| 397 | /// next statement, such as at the exit of a loop. |
| 398 | bool RecordNextStmtCount; |
| 399 | |
| 400 | /// The map of statements to count values. |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 401 | llvm::DenseMap<const Stmt *, uint64_t> &CountMap; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 402 | |
Justin Bogner | fc83c11 | 2014-04-18 21:51:09 +0000 | [diff] [blame] | 403 | /// BreakContinueStack - Keep counts of breaks and continues inside loops. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 404 | struct BreakContinue { |
| 405 | uint64_t BreakCount; |
| 406 | uint64_t ContinueCount; |
| 407 | BreakContinue() : BreakCount(0), ContinueCount(0) {} |
| 408 | }; |
| 409 | SmallVector<BreakContinue, 8> BreakContinueStack; |
| 410 | |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 411 | ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &CountMap, |
| 412 | CodeGenPGO &PGO) |
| 413 | : PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {} |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 414 | |
| 415 | void RecordStmtCount(const Stmt *S) { |
| 416 | if (RecordNextStmtCount) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 417 | CountMap[S] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 418 | RecordNextStmtCount = false; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | void VisitStmt(const Stmt *S) { |
| 423 | RecordStmtCount(S); |
| 424 | for (Stmt::const_child_range I = S->children(); I; ++I) { |
| 425 | if (*I) |
| 426 | this->Visit(*I); |
| 427 | } |
| 428 | } |
| 429 | |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 430 | void VisitFunctionDecl(const FunctionDecl *D) { |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 431 | // Counter tracks entry to the function body. |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 432 | RegionCounter Cnt(PGO, D->getBody()); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 433 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 434 | CountMap[D->getBody()] = PGO.getCurrentRegionCount(); |
| 435 | Visit(D->getBody()); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Justin Bogner | 191ec63 | 2014-04-11 23:06:35 +0000 | [diff] [blame] | 438 | // Skip lambda expressions. We visit these as FunctionDecls when we're |
| 439 | // generating them and aren't interested in the body when generating a |
| 440 | // parent context. |
| 441 | void VisitLambdaExpr(const LambdaExpr *LE) {} |
| 442 | |
Justin Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 443 | void VisitCapturedDecl(const CapturedDecl *D) { |
| 444 | // Counter tracks entry to the capture body. |
| 445 | RegionCounter Cnt(PGO, D->getBody()); |
| 446 | Cnt.beginRegion(); |
| 447 | CountMap[D->getBody()] = PGO.getCurrentRegionCount(); |
| 448 | Visit(D->getBody()); |
| 449 | } |
| 450 | |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 451 | void VisitObjCMethodDecl(const ObjCMethodDecl *D) { |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 452 | // Counter tracks entry to the method body. |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 453 | RegionCounter Cnt(PGO, D->getBody()); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 454 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 455 | CountMap[D->getBody()] = PGO.getCurrentRegionCount(); |
| 456 | Visit(D->getBody()); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 459 | void VisitBlockDecl(const BlockDecl *D) { |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 460 | // Counter tracks entry to the block body. |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 461 | RegionCounter Cnt(PGO, D->getBody()); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 462 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 4a2f5ae | 2014-04-10 23:37:36 +0000 | [diff] [blame] | 463 | CountMap[D->getBody()] = PGO.getCurrentRegionCount(); |
| 464 | Visit(D->getBody()); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 467 | void VisitReturnStmt(const ReturnStmt *S) { |
| 468 | RecordStmtCount(S); |
| 469 | if (S->getRetValue()) |
| 470 | Visit(S->getRetValue()); |
| 471 | PGO.setCurrentRegionUnreachable(); |
| 472 | RecordNextStmtCount = true; |
| 473 | } |
| 474 | |
| 475 | void VisitGotoStmt(const GotoStmt *S) { |
| 476 | RecordStmtCount(S); |
| 477 | PGO.setCurrentRegionUnreachable(); |
| 478 | RecordNextStmtCount = true; |
| 479 | } |
| 480 | |
| 481 | void VisitLabelStmt(const LabelStmt *S) { |
| 482 | RecordNextStmtCount = false; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 483 | // Counter tracks the block following the label. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 484 | RegionCounter Cnt(PGO, S); |
| 485 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 486 | CountMap[S] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 487 | Visit(S->getSubStmt()); |
| 488 | } |
| 489 | |
| 490 | void VisitBreakStmt(const BreakStmt *S) { |
| 491 | RecordStmtCount(S); |
| 492 | assert(!BreakContinueStack.empty() && "break not in a loop or switch!"); |
| 493 | BreakContinueStack.back().BreakCount += PGO.getCurrentRegionCount(); |
| 494 | PGO.setCurrentRegionUnreachable(); |
| 495 | RecordNextStmtCount = true; |
| 496 | } |
| 497 | |
| 498 | void VisitContinueStmt(const ContinueStmt *S) { |
| 499 | RecordStmtCount(S); |
| 500 | assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); |
| 501 | BreakContinueStack.back().ContinueCount += PGO.getCurrentRegionCount(); |
| 502 | PGO.setCurrentRegionUnreachable(); |
| 503 | RecordNextStmtCount = true; |
| 504 | } |
| 505 | |
| 506 | void VisitWhileStmt(const WhileStmt *S) { |
| 507 | RecordStmtCount(S); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 508 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 509 | RegionCounter Cnt(PGO, S); |
| 510 | BreakContinueStack.push_back(BreakContinue()); |
| 511 | // Visit the body region first so the break/continue adjustments can be |
| 512 | // included when visiting the condition. |
| 513 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 514 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 515 | Visit(S->getBody()); |
| 516 | Cnt.adjustForControlFlow(); |
| 517 | |
| 518 | // ...then go back and propagate counts through the condition. The count |
| 519 | // at the start of the condition is the sum of the incoming edges, |
| 520 | // the backedge from the end of the loop body, and the edges from |
| 521 | // continue statements. |
| 522 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 523 | Cnt.setCurrentRegionCount(Cnt.getParentCount() + |
| 524 | Cnt.getAdjustedCount() + BC.ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 525 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 526 | Visit(S->getCond()); |
| 527 | Cnt.adjustForControlFlow(); |
| 528 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 529 | RecordNextStmtCount = true; |
| 530 | } |
| 531 | |
| 532 | void VisitDoStmt(const DoStmt *S) { |
| 533 | RecordStmtCount(S); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 534 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 535 | RegionCounter Cnt(PGO, S); |
| 536 | BreakContinueStack.push_back(BreakContinue()); |
| 537 | Cnt.beginRegion(/*AddIncomingFallThrough=*/true); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 538 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 539 | Visit(S->getBody()); |
| 540 | Cnt.adjustForControlFlow(); |
| 541 | |
| 542 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 543 | // The count at the start of the condition is equal to the count at the |
| 544 | // end of the body. The adjusted count does not include either the |
| 545 | // fall-through count coming into the loop or the continue count, so add |
| 546 | // both of those separately. This is coincidentally the same equation as |
| 547 | // with while loops but for different reasons. |
| 548 | Cnt.setCurrentRegionCount(Cnt.getParentCount() + |
| 549 | Cnt.getAdjustedCount() + BC.ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 550 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 551 | Visit(S->getCond()); |
| 552 | Cnt.adjustForControlFlow(); |
| 553 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 554 | RecordNextStmtCount = true; |
| 555 | } |
| 556 | |
| 557 | void VisitForStmt(const ForStmt *S) { |
| 558 | RecordStmtCount(S); |
| 559 | if (S->getInit()) |
| 560 | Visit(S->getInit()); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 561 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 562 | RegionCounter Cnt(PGO, S); |
| 563 | BreakContinueStack.push_back(BreakContinue()); |
| 564 | // Visit the body region first. (This is basically the same as a while |
| 565 | // loop; see further comments in VisitWhileStmt.) |
| 566 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 567 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 568 | Visit(S->getBody()); |
| 569 | Cnt.adjustForControlFlow(); |
| 570 | |
| 571 | // The increment is essentially part of the body but it needs to include |
| 572 | // the count for all the continue statements. |
| 573 | if (S->getInc()) { |
| 574 | Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() + |
| 575 | BreakContinueStack.back().ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 576 | CountMap[S->getInc()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 577 | Visit(S->getInc()); |
| 578 | Cnt.adjustForControlFlow(); |
| 579 | } |
| 580 | |
| 581 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 582 | |
| 583 | // ...then go back and propagate counts through the condition. |
| 584 | if (S->getCond()) { |
| 585 | Cnt.setCurrentRegionCount(Cnt.getParentCount() + |
| 586 | Cnt.getAdjustedCount() + |
| 587 | BC.ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 588 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 589 | Visit(S->getCond()); |
| 590 | Cnt.adjustForControlFlow(); |
| 591 | } |
| 592 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 593 | RecordNextStmtCount = true; |
| 594 | } |
| 595 | |
| 596 | void VisitCXXForRangeStmt(const CXXForRangeStmt *S) { |
| 597 | RecordStmtCount(S); |
| 598 | Visit(S->getRangeStmt()); |
| 599 | Visit(S->getBeginEndStmt()); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 600 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 601 | RegionCounter Cnt(PGO, S); |
| 602 | BreakContinueStack.push_back(BreakContinue()); |
| 603 | // Visit the body region first. (This is basically the same as a while |
| 604 | // loop; see further comments in VisitWhileStmt.) |
| 605 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 606 | CountMap[S->getLoopVarStmt()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 607 | Visit(S->getLoopVarStmt()); |
| 608 | Visit(S->getBody()); |
| 609 | Cnt.adjustForControlFlow(); |
| 610 | |
| 611 | // The increment is essentially part of the body but it needs to include |
| 612 | // the count for all the continue statements. |
| 613 | Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() + |
| 614 | BreakContinueStack.back().ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 615 | CountMap[S->getInc()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 616 | Visit(S->getInc()); |
| 617 | Cnt.adjustForControlFlow(); |
| 618 | |
| 619 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 620 | |
| 621 | // ...then go back and propagate counts through the condition. |
| 622 | Cnt.setCurrentRegionCount(Cnt.getParentCount() + |
| 623 | Cnt.getAdjustedCount() + |
| 624 | BC.ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 625 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 626 | Visit(S->getCond()); |
| 627 | Cnt.adjustForControlFlow(); |
| 628 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 629 | RecordNextStmtCount = true; |
| 630 | } |
| 631 | |
| 632 | void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) { |
| 633 | RecordStmtCount(S); |
| 634 | Visit(S->getElement()); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 635 | // Counter tracks the body of the loop. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 636 | RegionCounter Cnt(PGO, S); |
| 637 | BreakContinueStack.push_back(BreakContinue()); |
| 638 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 639 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 640 | Visit(S->getBody()); |
| 641 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 642 | Cnt.adjustForControlFlow(); |
| 643 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 644 | RecordNextStmtCount = true; |
| 645 | } |
| 646 | |
| 647 | void VisitSwitchStmt(const SwitchStmt *S) { |
| 648 | RecordStmtCount(S); |
| 649 | Visit(S->getCond()); |
| 650 | PGO.setCurrentRegionUnreachable(); |
| 651 | BreakContinueStack.push_back(BreakContinue()); |
| 652 | Visit(S->getBody()); |
| 653 | // If the switch is inside a loop, add the continue counts. |
| 654 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 655 | if (!BreakContinueStack.empty()) |
| 656 | BreakContinueStack.back().ContinueCount += BC.ContinueCount; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 657 | // Counter tracks the exit block of the switch. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 658 | RegionCounter ExitCnt(PGO, S); |
| 659 | ExitCnt.beginRegion(); |
| 660 | RecordNextStmtCount = true; |
| 661 | } |
| 662 | |
| 663 | void VisitCaseStmt(const CaseStmt *S) { |
| 664 | RecordNextStmtCount = false; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 665 | // Counter for this particular case. This counts only jumps from the |
| 666 | // switch header and does not include fallthrough from the case before |
| 667 | // this one. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 668 | RegionCounter Cnt(PGO, S); |
| 669 | Cnt.beginRegion(/*AddIncomingFallThrough=*/true); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 670 | CountMap[S] = Cnt.getCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 671 | RecordNextStmtCount = true; |
| 672 | Visit(S->getSubStmt()); |
| 673 | } |
| 674 | |
| 675 | void VisitDefaultStmt(const DefaultStmt *S) { |
| 676 | RecordNextStmtCount = false; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 677 | // Counter for this default case. This does not include fallthrough from |
| 678 | // the previous case. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 679 | RegionCounter Cnt(PGO, S); |
| 680 | Cnt.beginRegion(/*AddIncomingFallThrough=*/true); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 681 | CountMap[S] = Cnt.getCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 682 | RecordNextStmtCount = true; |
| 683 | Visit(S->getSubStmt()); |
| 684 | } |
| 685 | |
| 686 | void VisitIfStmt(const IfStmt *S) { |
| 687 | RecordStmtCount(S); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 688 | // Counter tracks the "then" part of an if statement. The count for |
| 689 | // the "else" part, if it exists, will be calculated from this counter. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 690 | RegionCounter Cnt(PGO, S); |
| 691 | Visit(S->getCond()); |
| 692 | |
| 693 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 694 | CountMap[S->getThen()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 695 | Visit(S->getThen()); |
| 696 | Cnt.adjustForControlFlow(); |
| 697 | |
| 698 | if (S->getElse()) { |
| 699 | Cnt.beginElseRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 700 | CountMap[S->getElse()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 701 | Visit(S->getElse()); |
| 702 | Cnt.adjustForControlFlow(); |
| 703 | } |
| 704 | Cnt.applyAdjustmentsToRegion(0); |
| 705 | RecordNextStmtCount = true; |
| 706 | } |
| 707 | |
| 708 | void VisitCXXTryStmt(const CXXTryStmt *S) { |
| 709 | RecordStmtCount(S); |
| 710 | Visit(S->getTryBlock()); |
| 711 | for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I) |
| 712 | Visit(S->getHandler(I)); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 713 | // Counter tracks the continuation block of the try statement. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 714 | RegionCounter Cnt(PGO, S); |
| 715 | Cnt.beginRegion(); |
| 716 | RecordNextStmtCount = true; |
| 717 | } |
| 718 | |
| 719 | void VisitCXXCatchStmt(const CXXCatchStmt *S) { |
| 720 | RecordNextStmtCount = false; |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 721 | // Counter tracks the catch statement's handler block. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 722 | RegionCounter Cnt(PGO, S); |
| 723 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 724 | CountMap[S] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 725 | Visit(S->getHandlerBlock()); |
| 726 | } |
| 727 | |
Justin Bogner | 53c55d9 | 2014-04-11 06:10:10 +0000 | [diff] [blame] | 728 | void VisitAbstractConditionalOperator( |
| 729 | const AbstractConditionalOperator *E) { |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 730 | RecordStmtCount(E); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 731 | // Counter tracks the "true" part of a conditional operator. The |
| 732 | // count in the "false" part will be calculated from this counter. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 733 | RegionCounter Cnt(PGO, E); |
| 734 | Visit(E->getCond()); |
| 735 | |
| 736 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 737 | CountMap[E->getTrueExpr()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 738 | Visit(E->getTrueExpr()); |
| 739 | Cnt.adjustForControlFlow(); |
| 740 | |
| 741 | Cnt.beginElseRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 742 | CountMap[E->getFalseExpr()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 743 | Visit(E->getFalseExpr()); |
| 744 | Cnt.adjustForControlFlow(); |
| 745 | |
| 746 | Cnt.applyAdjustmentsToRegion(0); |
| 747 | RecordNextStmtCount = true; |
| 748 | } |
| 749 | |
| 750 | void VisitBinLAnd(const BinaryOperator *E) { |
| 751 | RecordStmtCount(E); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 752 | // Counter tracks the right hand side of a logical and operator. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 753 | RegionCounter Cnt(PGO, E); |
| 754 | Visit(E->getLHS()); |
| 755 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 756 | CountMap[E->getRHS()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 757 | Visit(E->getRHS()); |
| 758 | Cnt.adjustForControlFlow(); |
| 759 | Cnt.applyAdjustmentsToRegion(0); |
| 760 | RecordNextStmtCount = true; |
| 761 | } |
| 762 | |
| 763 | void VisitBinLOr(const BinaryOperator *E) { |
| 764 | RecordStmtCount(E); |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 765 | // Counter tracks the right hand side of a logical or operator. |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 766 | RegionCounter Cnt(PGO, E); |
| 767 | Visit(E->getLHS()); |
| 768 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 769 | CountMap[E->getRHS()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 770 | Visit(E->getRHS()); |
| 771 | Cnt.adjustForControlFlow(); |
| 772 | Cnt.applyAdjustmentsToRegion(0); |
| 773 | RecordNextStmtCount = true; |
| 774 | } |
| 775 | }; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 778 | void PGOHash::combine(HashType Type) { |
| 779 | // Check that we never combine 0 and only have six bits. |
| 780 | assert(Type && "Hash is invalid: unexpected type 0"); |
| 781 | assert(unsigned(Type) < TooBig && "Hash is invalid: too many types"); |
| 782 | |
| 783 | // Pass through MD5 if enough work has built up. |
| 784 | if (Count && Count % NumTypesPerWord == 0) { |
| 785 | using namespace llvm::support; |
| 786 | uint64_t Swapped = endian::byte_swap<uint64_t, little>(Working); |
| 787 | MD5.update(llvm::makeArrayRef((uint8_t *)&Swapped, sizeof(Swapped))); |
| 788 | Working = 0; |
| 789 | } |
| 790 | |
| 791 | // Accumulate the current type. |
| 792 | ++Count; |
| 793 | Working = Working << NumBitsPerType | Type; |
| 794 | } |
| 795 | |
| 796 | uint64_t PGOHash::finalize() { |
| 797 | // Use Working as the hash directly if we never used MD5. |
| 798 | if (Count <= NumTypesPerWord) |
| 799 | // No need to byte swap here, since none of the math was endian-dependent. |
| 800 | // This number will be byte-swapped as required on endianness transitions, |
| 801 | // so we will see the same value on the other side. |
| 802 | return Working; |
| 803 | |
| 804 | // Check for remaining work in Working. |
| 805 | if (Working) |
| 806 | MD5.update(Working); |
| 807 | |
| 808 | // Finalize the MD5 and return the hash. |
| 809 | llvm::MD5::MD5Result Result; |
| 810 | MD5.final(Result); |
| 811 | using namespace llvm::support; |
| 812 | return endian::read<uint64_t, little, unaligned>(Result); |
| 813 | } |
| 814 | |
Duncan P. N. Exon Smith | d971cd1 | 2014-03-28 17:53:22 +0000 | [diff] [blame] | 815 | static void emitRuntimeHook(CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | 3fefedb | 2014-04-11 00:43:16 +0000 | [diff] [blame] | 816 | const char *const RuntimeVarName = "__llvm_profile_runtime"; |
| 817 | const char *const RuntimeUserName = "__llvm_profile_runtime_user"; |
Duncan P. N. Exon Smith | d971cd1 | 2014-03-28 17:53:22 +0000 | [diff] [blame] | 818 | if (CGM.getModule().getGlobalVariable(RuntimeVarName)) |
| 819 | return; |
| 820 | |
| 821 | // Declare the runtime hook. |
| 822 | llvm::LLVMContext &Ctx = CGM.getLLVMContext(); |
| 823 | auto *Int32Ty = llvm::Type::getInt32Ty(Ctx); |
| 824 | auto *Var = new llvm::GlobalVariable(CGM.getModule(), Int32Ty, false, |
| 825 | llvm::GlobalValue::ExternalLinkage, |
| 826 | nullptr, RuntimeVarName); |
| 827 | |
| 828 | // Make a function that uses it. |
| 829 | auto *User = llvm::Function::Create(llvm::FunctionType::get(Int32Ty, false), |
| 830 | llvm::GlobalValue::LinkOnceODRLinkage, |
| 831 | RuntimeUserName, &CGM.getModule()); |
| 832 | User->addFnAttr(llvm::Attribute::NoInline); |
| 833 | if (CGM.getCodeGenOpts().DisableRedZone) |
| 834 | User->addFnAttr(llvm::Attribute::NoRedZone); |
| 835 | CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", User)); |
| 836 | auto *Load = Builder.CreateLoad(Var); |
| 837 | Builder.CreateRet(Load); |
| 838 | |
| 839 | // Create a use of the function. Now the definition of the runtime variable |
| 840 | // should get pulled in, along with any static initializears. |
| 841 | CGM.addUsedGlobal(User); |
| 842 | } |
| 843 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 844 | void CodeGenPGO::checkGlobalDecl(GlobalDecl GD) { |
| 845 | // Make sure we only emit coverage mapping for one constructor/destructor. |
| 846 | // Clang emits several functions for the constructor and the destructor of |
| 847 | // a class. Every function is instrumented, but we only want to provide |
| 848 | // coverage for one of them. Because of that we only emit the coverage mapping |
| 849 | // for the base constructor/destructor. |
| 850 | if ((isa<CXXConstructorDecl>(GD.getDecl()) && |
Alex Lorenz | d423674 | 2014-08-11 18:21:34 +0000 | [diff] [blame^] | 851 | GD.getCtorType() != Ctor_Base) || |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 852 | (isa<CXXDestructorDecl>(GD.getDecl()) && |
| 853 | GD.getDtorType() != Dtor_Base)) { |
| 854 | SkipCoverageMapping = true; |
| 855 | } |
| 856 | } |
| 857 | |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 858 | void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) { |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 859 | bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate; |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 860 | llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader(); |
| 861 | if (!InstrumentRegions && !PGOReader) |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 862 | return; |
Justin Bogner | 3212b18 | 2014-04-25 07:20:05 +0000 | [diff] [blame] | 863 | if (D->isImplicit()) |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 864 | return; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 865 | CGM.ClearUnusedCoverageMapping(D); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 866 | setFuncName(Fn); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 867 | setVarLinkage(Fn->getLinkage()); |
Duncan P. N. Exon Smith | 7c41451 | 2014-03-20 22:50:08 +0000 | [diff] [blame] | 868 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 869 | mapRegionCounters(D); |
Duncan P. N. Exon Smith | d971cd1 | 2014-03-28 17:53:22 +0000 | [diff] [blame] | 870 | if (InstrumentRegions) { |
| 871 | emitRuntimeHook(CGM); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 872 | emitCounterVariables(); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 873 | if (CGM.getCodeGenOpts().CoverageMapping) |
| 874 | emitCounterRegionMapping(D); |
Duncan P. N. Exon Smith | d971cd1 | 2014-03-28 17:53:22 +0000 | [diff] [blame] | 875 | } |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 876 | if (PGOReader) { |
Justin Bogner | 40b8ba1 | 2014-06-26 01:45:07 +0000 | [diff] [blame] | 877 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 878 | loadRegionCounts(PGOReader, SM.isInMainFile(D->getLocation())); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 879 | computeRegionCounts(D); |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 880 | applyFunctionAttributes(PGOReader, Fn); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 881 | } |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | void CodeGenPGO::mapRegionCounters(const Decl *D) { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 885 | RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 886 | MapRegionCounters Walker(*RegionCounterMap); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 887 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 888 | Walker.TraverseDecl(const_cast<FunctionDecl *>(FD)); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 889 | else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 890 | Walker.TraverseDecl(const_cast<ObjCMethodDecl *>(MD)); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 891 | else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D)) |
Bob Wilson | d893142 | 2014-04-11 17:16:13 +0000 | [diff] [blame] | 892 | Walker.TraverseDecl(const_cast<BlockDecl *>(BD)); |
Justin Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 893 | else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D)) |
| 894 | Walker.TraverseDecl(const_cast<CapturedDecl *>(CD)); |
Justin Bogner | 3212b18 | 2014-04-25 07:20:05 +0000 | [diff] [blame] | 895 | assert(Walker.NextCounter > 0 && "no entry counter mapped for decl"); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 896 | NumRegionCounters = Walker.NextCounter; |
Duncan P. N. Exon Smith | 4bc7731 | 2014-04-16 16:03:27 +0000 | [diff] [blame] | 897 | FunctionHash = Walker.Hash.finalize(); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 900 | void CodeGenPGO::emitCounterRegionMapping(const Decl *D) { |
| 901 | if (SkipCoverageMapping) |
| 902 | return; |
| 903 | // Don't map the functions inside the system headers |
| 904 | auto Loc = D->getBody()->getLocStart(); |
| 905 | if (CGM.getContext().getSourceManager().isInSystemHeader(Loc)) |
| 906 | return; |
| 907 | |
| 908 | llvm::raw_string_ostream OS(CoverageMapping); |
| 909 | CoverageMappingGen MappingGen(*CGM.getCoverageMapping(), |
| 910 | CGM.getContext().getSourceManager(), |
| 911 | CGM.getLangOpts(), RegionCounterMap.get(), |
| 912 | NumRegionCounters); |
| 913 | MappingGen.emitCounterMapping(D, OS); |
| 914 | OS.flush(); |
| 915 | } |
| 916 | |
| 917 | void |
| 918 | CodeGenPGO::emitEmptyCounterMapping(const Decl *D, StringRef FuncName, |
| 919 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 920 | if (SkipCoverageMapping) |
| 921 | return; |
| 922 | setFuncName(FuncName, Linkage); |
| 923 | setVarLinkage(Linkage); |
| 924 | |
| 925 | // Don't map the functions inside the system headers |
| 926 | auto Loc = D->getBody()->getLocStart(); |
| 927 | if (CGM.getContext().getSourceManager().isInSystemHeader(Loc)) |
| 928 | return; |
| 929 | |
| 930 | llvm::raw_string_ostream OS(CoverageMapping); |
| 931 | CoverageMappingGen MappingGen(*CGM.getCoverageMapping(), |
| 932 | CGM.getContext().getSourceManager(), |
| 933 | CGM.getLangOpts()); |
| 934 | MappingGen.emitEmptyMapping(D, OS); |
| 935 | OS.flush(); |
| 936 | buildDataVar(); |
| 937 | } |
| 938 | |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 939 | void CodeGenPGO::computeRegionCounts(const Decl *D) { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 940 | StmtCountMap.reset(new llvm::DenseMap<const Stmt *, uint64_t>); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 941 | ComputeRegionCounts Walker(*StmtCountMap, *this); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 942 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 943 | Walker.VisitFunctionDecl(FD); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 944 | else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) |
| 945 | Walker.VisitObjCMethodDecl(MD); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 946 | else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D)) |
| 947 | Walker.VisitBlockDecl(BD); |
Justin Bogner | 81ab90f | 2014-04-15 00:50:54 +0000 | [diff] [blame] | 948 | else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D)) |
| 949 | Walker.VisitCapturedDecl(const_cast<CapturedDecl *>(CD)); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 952 | void |
| 953 | CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader, |
| 954 | llvm::Function *Fn) { |
Justin Bogner | 4c9c45c | 2014-03-12 18:14:32 +0000 | [diff] [blame] | 955 | if (!haveRegionCounts()) |
| 956 | return; |
| 957 | |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 958 | uint64_t MaxFunctionCount = PGOReader->getMaximumFunctionCount(); |
Justin Bogner | 4c9c45c | 2014-03-12 18:14:32 +0000 | [diff] [blame] | 959 | uint64_t FunctionCount = getRegionCount(0); |
| 960 | if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount)) |
| 961 | // Turn on InlineHint attribute for hot functions. |
| 962 | // FIXME: 30% is from preliminary tuning on SPEC, it may not be optimal. |
| 963 | Fn->addFnAttr(llvm::Attribute::InlineHint); |
| 964 | else if (FunctionCount <= (uint64_t)(0.01 * (double)MaxFunctionCount)) |
| 965 | // Turn on Cold attribute for cold functions. |
| 966 | // FIXME: 1% is from preliminary tuning on SPEC, it may not be optimal. |
| 967 | Fn->addFnAttr(llvm::Attribute::Cold); |
| 968 | } |
| 969 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 970 | void CodeGenPGO::emitCounterVariables() { |
| 971 | llvm::LLVMContext &Ctx = CGM.getLLVMContext(); |
| 972 | llvm::ArrayType *CounterTy = llvm::ArrayType::get(llvm::Type::getInt64Ty(Ctx), |
| 973 | NumRegionCounters); |
| 974 | RegionCounters = |
Duncan P. N. Exon Smith | 73f7862 | 2014-03-20 22:49:50 +0000 | [diff] [blame] | 975 | new llvm::GlobalVariable(CGM.getModule(), CounterTy, false, VarLinkage, |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 976 | llvm::Constant::getNullValue(CounterTy), |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 977 | getFuncVarName("counters")); |
| 978 | RegionCounters->setAlignment(8); |
| 979 | RegionCounters->setSection(getCountersSection(CGM)); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) { |
Bob Wilson | 749ebc7 | 2014-03-06 04:55:28 +0000 | [diff] [blame] | 983 | if (!RegionCounters) |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 984 | return; |
| 985 | llvm::Value *Addr = |
| 986 | Builder.CreateConstInBoundsGEP2_64(RegionCounters, 0, Counter); |
| 987 | llvm::Value *Count = Builder.CreateLoad(Addr, "pgocount"); |
| 988 | Count = Builder.CreateAdd(Count, Builder.getInt64(1)); |
| 989 | Builder.CreateStore(Count, Addr); |
| 990 | } |
| 991 | |
Justin Bogner | 40b8ba1 | 2014-06-26 01:45:07 +0000 | [diff] [blame] | 992 | void CodeGenPGO::loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader, |
| 993 | bool IsInMainFile) { |
| 994 | CGM.getPGOStats().addVisited(IsInMainFile); |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 995 | RegionCounts.reset(new std::vector<uint64_t>); |
Justin Bogner | 9c6818e | 2014-08-01 22:50:16 +0000 | [diff] [blame] | 996 | if (std::error_code EC = PGOReader->getFunctionCounts( |
| 997 | getFuncName(), FunctionHash, *RegionCounts)) { |
| 998 | if (EC == llvm::instrprof_error::unknown_function) |
| 999 | CGM.getPGOStats().addMissing(IsInMainFile); |
| 1000 | else if (EC == llvm::instrprof_error::hash_mismatch) |
| 1001 | CGM.getPGOStats().addMismatched(IsInMainFile); |
| 1002 | else if (EC == llvm::instrprof_error::malformed) |
| 1003 | // TODO: Consider a more specific warning for this case. |
| 1004 | CGM.getPGOStats().addMismatched(IsInMainFile); |
Justin Bogner | e2ef2a0 | 2014-04-15 21:22:35 +0000 | [diff] [blame] | 1005 | RegionCounts.reset(); |
| 1006 | } |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | void CodeGenPGO::destroyRegionCounters() { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 1010 | RegionCounterMap.reset(); |
| 1011 | StmtCountMap.reset(); |
| 1012 | RegionCounts.reset(); |
Justin Bogner | 3212b18 | 2014-04-25 07:20:05 +0000 | [diff] [blame] | 1013 | RegionCounters = nullptr; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1016 | /// \brief Calculate what to divide by to scale weights. |
| 1017 | /// |
| 1018 | /// Given the maximum weight, calculate a divisor that will scale all the |
| 1019 | /// weights to strictly less than UINT32_MAX. |
| 1020 | static uint64_t calculateWeightScale(uint64_t MaxWeight) { |
| 1021 | return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1; |
| 1022 | } |
| 1023 | |
| 1024 | /// \brief Scale an individual branch weight (and add 1). |
| 1025 | /// |
| 1026 | /// Scale a 64-bit weight down to 32-bits using \c Scale. |
| 1027 | /// |
| 1028 | /// According to Laplace's Rule of Succession, it is better to compute the |
| 1029 | /// weight based on the count plus 1, so universally add 1 to the value. |
| 1030 | /// |
| 1031 | /// \pre \c Scale was calculated by \a calculateWeightScale() with a weight no |
| 1032 | /// greater than \c Weight. |
| 1033 | static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale) { |
| 1034 | assert(Scale && "scale by 0?"); |
| 1035 | uint64_t Scaled = Weight / Scale + 1; |
| 1036 | assert(Scaled <= UINT32_MAX && "overflow 32-bits"); |
| 1037 | return Scaled; |
| 1038 | } |
| 1039 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1040 | llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount, |
| 1041 | uint64_t FalseCount) { |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1042 | // Check for empty weights. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1043 | if (!TrueCount && !FalseCount) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 1044 | return nullptr; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1045 | |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1046 | // Calculate how to scale down to 32-bits. |
| 1047 | uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount)); |
| 1048 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1049 | llvm::MDBuilder MDHelper(CGM.getLLVMContext()); |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1050 | return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale), |
| 1051 | scaleBranchWeight(FalseCount, Scale)); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Bob Wilson | 95a27b0 | 2014-02-17 19:20:59 +0000 | [diff] [blame] | 1054 | llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) { |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1055 | // We need at least two elements to create meaningful weights. |
| 1056 | if (Weights.size() < 2) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 1057 | return nullptr; |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1058 | |
Justin Bogner | f3aefca | 2014-04-04 02:48:51 +0000 | [diff] [blame] | 1059 | // Check for empty weights. |
| 1060 | uint64_t MaxWeight = *std::max_element(Weights.begin(), Weights.end()); |
| 1061 | if (MaxWeight == 0) |
| 1062 | return nullptr; |
| 1063 | |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1064 | // Calculate how to scale down to 32-bits. |
Justin Bogner | f3aefca | 2014-04-04 02:48:51 +0000 | [diff] [blame] | 1065 | uint64_t Scale = calculateWeightScale(MaxWeight); |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1066 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1067 | SmallVector<uint32_t, 16> ScaledWeights; |
| 1068 | ScaledWeights.reserve(Weights.size()); |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1069 | for (uint64_t W : Weights) |
| 1070 | ScaledWeights.push_back(scaleBranchWeight(W, Scale)); |
| 1071 | |
| 1072 | llvm::MDBuilder MDHelper(CGM.getLLVMContext()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1073 | return MDHelper.createBranchWeights(ScaledWeights); |
| 1074 | } |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 1075 | |
| 1076 | llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond, |
| 1077 | RegionCounter &Cnt) { |
| 1078 | if (!haveRegionCounts()) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 1079 | return nullptr; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 1080 | uint64_t LoopCount = Cnt.getCount(); |
| 1081 | uint64_t CondCount = 0; |
| 1082 | bool Found = getStmtCount(Cond, CondCount); |
| 1083 | assert(Found && "missing expected loop condition count"); |
| 1084 | (void)Found; |
| 1085 | if (CondCount == 0) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 1086 | return nullptr; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 1087 | return createBranchWeights(LoopCount, |
| 1088 | std::max(CondCount, LoopCount) - LoopCount); |
| 1089 | } |