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" |
Justin Bogner | 529f6dd | 2014-01-07 03:43:15 +0000 | [diff] [blame] | 18 | #include "llvm/Config/config.h" // for strtoull()/strtoll() define |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 19 | #include "llvm/IR/MDBuilder.h" |
| 20 | #include "llvm/Support/FileSystem.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
Justin Bogner | d66a17d | 2014-03-12 21:06:31 +0000 | [diff] [blame] | 25 | static void ReportBadPGOData(CodeGenModule &CGM, const char *Message) { |
| 26 | DiagnosticsEngine &Diags = CGM.getDiags(); |
| 27 | unsigned diagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0"); |
| 28 | Diags.Report(diagID) << Message; |
| 29 | } |
| 30 | |
| 31 | PGOProfileData::PGOProfileData(CodeGenModule &CGM, std::string Path) |
| 32 | : CGM(CGM) { |
| 33 | if (llvm::MemoryBuffer::getFile(Path, DataBuffer)) { |
| 34 | ReportBadPGOData(CGM, "failed to open pgo data file"); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | if (DataBuffer->getBufferSize() > std::numeric_limits<unsigned>::max()) { |
| 39 | ReportBadPGOData(CGM, "pgo data file too big"); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | // Scan through the data file and map each function to the corresponding |
| 44 | // file offset where its counts are stored. |
| 45 | const char *BufferStart = DataBuffer->getBufferStart(); |
| 46 | const char *BufferEnd = DataBuffer->getBufferEnd(); |
| 47 | const char *CurPtr = BufferStart; |
| 48 | uint64_t MaxCount = 0; |
| 49 | while (CurPtr < BufferEnd) { |
| 50 | // Read the function name. |
| 51 | const char *FuncStart = CurPtr; |
| 52 | // For Objective-C methods, the name may include whitespace, so search |
| 53 | // backward from the end of the line to find the space that separates the |
| 54 | // name from the number of counters. (This is a temporary hack since we are |
| 55 | // going to completely replace this file format in the near future.) |
| 56 | CurPtr = strchr(CurPtr, '\n'); |
| 57 | if (!CurPtr) { |
| 58 | ReportBadPGOData(CGM, "pgo data file has malformed function entry"); |
| 59 | return; |
| 60 | } |
Justin Bogner | d66a17d | 2014-03-12 21:06:31 +0000 | [diff] [blame] | 61 | StringRef FuncName(FuncStart, CurPtr - FuncStart); |
| 62 | |
Justin Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 63 | // Skip over the function hash. |
| 64 | CurPtr = strchr(++CurPtr, '\n'); |
| 65 | if (!CurPtr) { |
| 66 | ReportBadPGOData(CGM, "pgo data file is missing the function hash"); |
| 67 | return; |
| 68 | } |
| 69 | |
Justin Bogner | d66a17d | 2014-03-12 21:06:31 +0000 | [diff] [blame] | 70 | // Read the number of counters. |
| 71 | char *EndPtr; |
| 72 | unsigned NumCounters = strtol(++CurPtr, &EndPtr, 10); |
| 73 | if (EndPtr == CurPtr || *EndPtr != '\n' || NumCounters <= 0) { |
| 74 | ReportBadPGOData(CGM, "pgo data file has unexpected number of counters"); |
| 75 | return; |
| 76 | } |
| 77 | CurPtr = EndPtr; |
| 78 | |
| 79 | // Read function count. |
| 80 | uint64_t Count = strtoll(CurPtr, &EndPtr, 10); |
| 81 | if (EndPtr == CurPtr || *EndPtr != '\n') { |
| 82 | ReportBadPGOData(CGM, "pgo-data file has bad count value"); |
| 83 | return; |
| 84 | } |
| 85 | CurPtr = EndPtr; // Point to '\n'. |
| 86 | FunctionCounts[FuncName] = Count; |
| 87 | MaxCount = Count > MaxCount ? Count : MaxCount; |
| 88 | |
| 89 | // There is one line for each counter; skip over those lines. |
| 90 | // Since function count is already read, we start the loop from 1. |
| 91 | for (unsigned N = 1; N < NumCounters; ++N) { |
| 92 | CurPtr = strchr(++CurPtr, '\n'); |
| 93 | if (!CurPtr) { |
| 94 | ReportBadPGOData(CGM, "pgo data file is missing some counter info"); |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Skip over the blank line separating functions. |
| 100 | CurPtr += 2; |
| 101 | |
| 102 | DataOffsets[FuncName] = FuncStart - BufferStart; |
| 103 | } |
| 104 | MaxFunctionCount = MaxCount; |
| 105 | } |
| 106 | |
Justin Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 107 | bool PGOProfileData::getFunctionCounts(StringRef FuncName, uint64_t &FuncHash, |
Justin Bogner | d66a17d | 2014-03-12 21:06:31 +0000 | [diff] [blame] | 108 | std::vector<uint64_t> &Counts) { |
| 109 | // Find the relevant section of the pgo-data file. |
| 110 | llvm::StringMap<unsigned>::const_iterator OffsetIter = |
| 111 | DataOffsets.find(FuncName); |
| 112 | if (OffsetIter == DataOffsets.end()) |
| 113 | return true; |
| 114 | const char *CurPtr = DataBuffer->getBufferStart() + OffsetIter->getValue(); |
| 115 | |
| 116 | // Skip over the function name. |
| 117 | CurPtr = strchr(CurPtr, '\n'); |
| 118 | assert(CurPtr && "pgo-data has corrupted function entry"); |
Justin Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 119 | |
| 120 | char *EndPtr; |
| 121 | // Read the function hash. |
| 122 | FuncHash = strtoll(++CurPtr, &EndPtr, 10); |
| 123 | assert(EndPtr != CurPtr && *EndPtr == '\n' && |
| 124 | "pgo-data file has corrupted function hash"); |
| 125 | CurPtr = EndPtr; |
Justin Bogner | d66a17d | 2014-03-12 21:06:31 +0000 | [diff] [blame] | 126 | |
| 127 | // Read the number of counters. |
Justin Bogner | d66a17d | 2014-03-12 21:06:31 +0000 | [diff] [blame] | 128 | unsigned NumCounters = strtol(++CurPtr, &EndPtr, 10); |
| 129 | assert(EndPtr != CurPtr && *EndPtr == '\n' && NumCounters > 0 && |
| 130 | "pgo-data file has corrupted number of counters"); |
| 131 | CurPtr = EndPtr; |
| 132 | |
| 133 | Counts.reserve(NumCounters); |
| 134 | |
| 135 | for (unsigned N = 0; N < NumCounters; ++N) { |
| 136 | // Read the count value. |
| 137 | uint64_t Count = strtoll(CurPtr, &EndPtr, 10); |
| 138 | if (EndPtr == CurPtr || *EndPtr != '\n') { |
| 139 | ReportBadPGOData(CGM, "pgo-data file has bad count value"); |
| 140 | return true; |
| 141 | } |
| 142 | Counts.push_back(Count); |
| 143 | CurPtr = EndPtr + 1; |
| 144 | } |
| 145 | |
| 146 | // Make sure the number of counters matches up. |
| 147 | if (Counts.size() != NumCounters) { |
| 148 | ReportBadPGOData(CGM, "pgo-data file has inconsistent counters"); |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | return false; |
| 153 | } |
| 154 | |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 155 | void CodeGenPGO::setFuncName(llvm::Function *Fn) { |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 156 | RawFuncName = Fn->getName(); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 157 | |
| 158 | // Function names may be prefixed with a binary '1' to indicate |
| 159 | // that the backend should not modify the symbols due to any platform |
| 160 | // 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] | 161 | if (RawFuncName[0] == '\1') |
| 162 | RawFuncName = RawFuncName.substr(1); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 163 | |
| 164 | if (!Fn->hasLocalLinkage()) { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 165 | PrefixedFuncName.reset(new std::string(RawFuncName)); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
| 169 | // For local symbols, prepend the main file name to distinguish them. |
| 170 | // Do not include the full path in the file name since there's no guarantee |
| 171 | // that it will stay the same, e.g., if the files are checked out from |
| 172 | // version control in different locations. |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 173 | PrefixedFuncName.reset(new std::string(CGM.getCodeGenOpts().MainFileName)); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 174 | if (PrefixedFuncName->empty()) |
| 175 | PrefixedFuncName->assign("<unknown>"); |
| 176 | PrefixedFuncName->append(":"); |
| 177 | PrefixedFuncName->append(RawFuncName); |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 180 | static llvm::Function *getRegisterFunc(CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 181 | return CGM.getModule().getFunction("__llvm_profile_register_functions"); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static llvm::BasicBlock *getOrInsertRegisterBB(CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | 780443e | 2014-03-20 03:57:11 +0000 | [diff] [blame] | 185 | // Don't do this for Darwin. compiler-rt uses linker magic. |
| 186 | if (CGM.getTarget().getTriple().isOSDarwin()) |
| 187 | return nullptr; |
| 188 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 189 | // Only need to insert this once per module. |
| 190 | if (llvm::Function *RegisterF = getRegisterFunc(CGM)) |
| 191 | return &RegisterF->getEntryBlock(); |
| 192 | |
| 193 | // Construct the function. |
| 194 | auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext()); |
| 195 | auto *RegisterFTy = llvm::FunctionType::get(VoidTy, false); |
| 196 | auto *RegisterF = llvm::Function::Create(RegisterFTy, |
| 197 | llvm::GlobalValue::InternalLinkage, |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 198 | "__llvm_profile_register_functions", |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 199 | &CGM.getModule()); |
| 200 | RegisterF->setUnnamedAddr(true); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 201 | if (CGM.getCodeGenOpts().DisableRedZone) |
| 202 | RegisterF->addFnAttr(llvm::Attribute::NoRedZone); |
| 203 | |
| 204 | // Construct and return the entry block. |
| 205 | auto *BB = llvm::BasicBlock::Create(CGM.getLLVMContext(), "", RegisterF); |
| 206 | CGBuilderTy Builder(BB); |
| 207 | Builder.CreateRetVoid(); |
| 208 | return BB; |
| 209 | } |
| 210 | |
| 211 | static llvm::Constant *getOrInsertRuntimeRegister(CodeGenModule &CGM) { |
| 212 | auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext()); |
| 213 | auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
| 214 | auto *RuntimeRegisterTy = llvm::FunctionType::get(VoidTy, VoidPtrTy, false); |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 215 | return CGM.getModule().getOrInsertFunction("__llvm_profile_register_function", |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 216 | RuntimeRegisterTy); |
| 217 | } |
| 218 | |
Duncan P. N. Exon Smith | 7134d47 | 2014-03-20 03:17:15 +0000 | [diff] [blame] | 219 | static bool isMachO(const CodeGenModule &CGM) { |
| 220 | return CGM.getTarget().getTriple().isOSBinFormatMachO(); |
| 221 | } |
| 222 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 223 | static StringRef getCountersSection(const CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 224 | 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] | 225 | } |
| 226 | |
| 227 | static StringRef getNameSection(const CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 228 | 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] | 229 | } |
| 230 | |
| 231 | static StringRef getDataSection(const CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 232 | 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] | 233 | } |
| 234 | |
| 235 | llvm::GlobalVariable *CodeGenPGO::buildDataVar() { |
| 236 | // Create name variable. |
| 237 | llvm::LLVMContext &Ctx = CGM.getLLVMContext(); |
| 238 | auto *VarName = llvm::ConstantDataArray::getString(Ctx, getFuncName(), |
| 239 | false); |
| 240 | auto *Name = new llvm::GlobalVariable(CGM.getModule(), VarName->getType(), |
Duncan P. N. Exon Smith | 73f7862 | 2014-03-20 22:49:50 +0000 | [diff] [blame] | 241 | true, VarLinkage, VarName, |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 242 | getFuncVarName("name")); |
| 243 | Name->setSection(getNameSection(CGM)); |
| 244 | Name->setAlignment(1); |
| 245 | |
| 246 | // Create data variable. |
| 247 | auto *Int32Ty = llvm::Type::getInt32Ty(Ctx); |
Justin Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 248 | auto *Int64Ty = llvm::Type::getInt64Ty(Ctx); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 249 | auto *Int8PtrTy = llvm::Type::getInt8PtrTy(Ctx); |
| 250 | auto *Int64PtrTy = llvm::Type::getInt64PtrTy(Ctx); |
| 251 | llvm::Type *DataTypes[] = { |
Justin Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 252 | Int32Ty, Int32Ty, Int64Ty, Int8PtrTy, Int64PtrTy |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 253 | }; |
| 254 | auto *DataTy = llvm::StructType::get(Ctx, makeArrayRef(DataTypes)); |
| 255 | llvm::Constant *DataVals[] = { |
| 256 | llvm::ConstantInt::get(Int32Ty, getFuncName().size()), |
| 257 | llvm::ConstantInt::get(Int32Ty, NumRegionCounters), |
Justin Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 258 | llvm::ConstantInt::get(Int64Ty, FunctionHash), |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 259 | llvm::ConstantExpr::getBitCast(Name, Int8PtrTy), |
| 260 | llvm::ConstantExpr::getBitCast(RegionCounters, Int64PtrTy) |
| 261 | }; |
| 262 | auto *Data = |
Duncan P. N. Exon Smith | 73f7862 | 2014-03-20 22:49:50 +0000 | [diff] [blame] | 263 | new llvm::GlobalVariable(CGM.getModule(), DataTy, true, VarLinkage, |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 264 | llvm::ConstantStruct::get(DataTy, DataVals), |
| 265 | getFuncVarName("data")); |
| 266 | |
| 267 | // All the data should be packed into an array in its own section. |
| 268 | Data->setSection(getDataSection(CGM)); |
| 269 | Data->setAlignment(8); |
| 270 | |
| 271 | // Make sure the data doesn't get deleted. |
| 272 | CGM.addUsedGlobal(Data); |
| 273 | return Data; |
| 274 | } |
| 275 | |
| 276 | void CodeGenPGO::emitInstrumentationData() { |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 277 | if (!CGM.getCodeGenOpts().ProfileInstrGenerate) |
| 278 | return; |
| 279 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 280 | // Build the data. |
| 281 | auto *Data = buildDataVar(); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 282 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 283 | // Register the data. |
Duncan P. N. Exon Smith | 780443e | 2014-03-20 03:57:11 +0000 | [diff] [blame] | 284 | auto *RegisterBB = getOrInsertRegisterBB(CGM); |
| 285 | if (!RegisterBB) |
| 286 | return; |
| 287 | CGBuilderTy Builder(RegisterBB->getTerminator()); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 288 | auto *VoidPtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
| 289 | Builder.CreateCall(getOrInsertRuntimeRegister(CGM), |
| 290 | Builder.CreateBitCast(Data, VoidPtrTy)); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | llvm::Function *CodeGenPGO::emitInitialization(CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 294 | if (!CGM.getCodeGenOpts().ProfileInstrGenerate) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 295 | return nullptr; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 296 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 297 | // Only need to create this once per module. |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 298 | if (CGM.getModule().getFunction("__llvm_profile_init")) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 299 | return nullptr; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 300 | |
Duncan P. N. Exon Smith | 5188e91 | 2014-03-20 19:23:46 +0000 | [diff] [blame] | 301 | // Get the function to call at initialization. |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 302 | llvm::Constant *RegisterF = getRegisterFunc(CGM); |
Duncan P. N. Exon Smith | 5188e91 | 2014-03-20 19:23:46 +0000 | [diff] [blame] | 303 | if (!RegisterF) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 304 | return nullptr; |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 305 | |
| 306 | // Create the initialization function. |
| 307 | auto *VoidTy = llvm::Type::getVoidTy(CGM.getLLVMContext()); |
| 308 | auto *F = llvm::Function::Create(llvm::FunctionType::get(VoidTy, false), |
| 309 | llvm::GlobalValue::InternalLinkage, |
Duncan P. N. Exon Smith | a780763 | 2014-03-20 20:00:41 +0000 | [diff] [blame] | 310 | "__llvm_profile_init", &CGM.getModule()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 311 | F->setUnnamedAddr(true); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 312 | F->addFnAttr(llvm::Attribute::NoInline); |
| 313 | if (CGM.getCodeGenOpts().DisableRedZone) |
| 314 | F->addFnAttr(llvm::Attribute::NoRedZone); |
| 315 | |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 316 | // Add the basic block and the necessary calls. |
| 317 | CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", F)); |
Duncan P. N. Exon Smith | 5188e91 | 2014-03-20 19:23:46 +0000 | [diff] [blame] | 318 | Builder.CreateCall(RegisterF); |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 319 | Builder.CreateRetVoid(); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 320 | |
| 321 | return F; |
| 322 | } |
| 323 | |
| 324 | namespace { |
| 325 | /// A StmtVisitor that fills a map of statements to PGO counters. |
| 326 | struct MapRegionCounters : public ConstStmtVisitor<MapRegionCounters> { |
| 327 | /// The next counter value to assign. |
| 328 | unsigned NextCounter; |
| 329 | /// The map of statements to counters. |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 330 | llvm::DenseMap<const Stmt *, unsigned> &CounterMap; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 331 | |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 332 | MapRegionCounters(llvm::DenseMap<const Stmt *, unsigned> &CounterMap) |
| 333 | : NextCounter(0), CounterMap(CounterMap) {} |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 334 | |
| 335 | void VisitChildren(const Stmt *S) { |
| 336 | for (Stmt::const_child_range I = S->children(); I; ++I) |
| 337 | if (*I) |
| 338 | this->Visit(*I); |
| 339 | } |
| 340 | void VisitStmt(const Stmt *S) { VisitChildren(S); } |
| 341 | |
Justin Bogner | ea278c3 | 2014-01-07 00:20:28 +0000 | [diff] [blame] | 342 | /// Assign a counter to track entry to the function body. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 343 | void VisitFunctionDecl(const FunctionDecl *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 344 | CounterMap[S->getBody()] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 345 | Visit(S->getBody()); |
| 346 | } |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 347 | void VisitObjCMethodDecl(const ObjCMethodDecl *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 348 | CounterMap[S->getBody()] = NextCounter++; |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 349 | Visit(S->getBody()); |
| 350 | } |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 351 | void VisitBlockDecl(const BlockDecl *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 352 | CounterMap[S->getBody()] = NextCounter++; |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 353 | Visit(S->getBody()); |
| 354 | } |
Justin Bogner | ea278c3 | 2014-01-07 00:20:28 +0000 | [diff] [blame] | 355 | /// Assign a counter to track the block following a label. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 356 | void VisitLabelStmt(const LabelStmt *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 357 | CounterMap[S] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 358 | Visit(S->getSubStmt()); |
| 359 | } |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 360 | /// Assign a counter for the body of a while loop. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 361 | void VisitWhileStmt(const WhileStmt *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 362 | CounterMap[S] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 363 | Visit(S->getCond()); |
| 364 | Visit(S->getBody()); |
| 365 | } |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 366 | /// Assign a counter for the body of a do-while loop. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 367 | void VisitDoStmt(const DoStmt *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 368 | CounterMap[S] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 369 | Visit(S->getBody()); |
| 370 | Visit(S->getCond()); |
| 371 | } |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 372 | /// Assign a counter for the body of a for loop. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 373 | void VisitForStmt(const ForStmt *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 374 | CounterMap[S] = NextCounter++; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 375 | if (S->getInit()) |
| 376 | Visit(S->getInit()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 377 | const Expr *E; |
| 378 | if ((E = S->getCond())) |
| 379 | Visit(E); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 380 | if ((E = S->getInc())) |
| 381 | Visit(E); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 382 | Visit(S->getBody()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 383 | } |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 384 | /// Assign a counter for the body of a for-range loop. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 385 | void VisitCXXForRangeStmt(const CXXForRangeStmt *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 386 | CounterMap[S] = NextCounter++; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 387 | Visit(S->getRangeStmt()); |
| 388 | Visit(S->getBeginEndStmt()); |
| 389 | Visit(S->getCond()); |
| 390 | Visit(S->getLoopVarStmt()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 391 | Visit(S->getBody()); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 392 | Visit(S->getInc()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 393 | } |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 394 | /// Assign a counter for the body of a for-collection loop. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 395 | void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 396 | CounterMap[S] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 397 | Visit(S->getElement()); |
| 398 | Visit(S->getBody()); |
| 399 | } |
| 400 | /// Assign a counter for the exit block of the switch statement. |
| 401 | void VisitSwitchStmt(const SwitchStmt *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 402 | CounterMap[S] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 403 | Visit(S->getCond()); |
| 404 | Visit(S->getBody()); |
| 405 | } |
| 406 | /// Assign a counter for a particular case in a switch. This counts jumps |
| 407 | /// from the switch header as well as fallthrough from the case before this |
| 408 | /// one. |
| 409 | void VisitCaseStmt(const CaseStmt *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 410 | CounterMap[S] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 411 | Visit(S->getSubStmt()); |
| 412 | } |
| 413 | /// Assign a counter for the default case of a switch statement. The count |
| 414 | /// is the number of branches from the loop header to the default, and does |
| 415 | /// not include fallthrough from previous cases. If we have multiple |
| 416 | /// conditional branch blocks from the switch instruction to the default |
| 417 | /// block, as with large GNU case ranges, this is the counter for the last |
| 418 | /// edge in that series, rather than the first. |
| 419 | void VisitDefaultStmt(const DefaultStmt *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 420 | CounterMap[S] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 421 | Visit(S->getSubStmt()); |
| 422 | } |
| 423 | /// Assign a counter for the "then" part of an if statement. The count for |
| 424 | /// the "else" part, if it exists, will be calculated from this counter. |
| 425 | void VisitIfStmt(const IfStmt *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 426 | CounterMap[S] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 427 | Visit(S->getCond()); |
| 428 | Visit(S->getThen()); |
| 429 | if (S->getElse()) |
| 430 | Visit(S->getElse()); |
| 431 | } |
| 432 | /// Assign a counter for the continuation block of a C++ try statement. |
| 433 | void VisitCXXTryStmt(const CXXTryStmt *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 434 | CounterMap[S] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 435 | Visit(S->getTryBlock()); |
| 436 | for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I) |
| 437 | Visit(S->getHandler(I)); |
| 438 | } |
| 439 | /// Assign a counter for a catch statement's handler block. |
| 440 | void VisitCXXCatchStmt(const CXXCatchStmt *S) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 441 | CounterMap[S] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 442 | Visit(S->getHandlerBlock()); |
| 443 | } |
| 444 | /// Assign a counter for the "true" part of a conditional operator. The |
| 445 | /// count in the "false" part will be calculated from this counter. |
| 446 | void VisitConditionalOperator(const ConditionalOperator *E) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 447 | CounterMap[E] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 448 | Visit(E->getCond()); |
| 449 | Visit(E->getTrueExpr()); |
| 450 | Visit(E->getFalseExpr()); |
| 451 | } |
| 452 | /// Assign a counter for the right hand side of a logical and operator. |
| 453 | void VisitBinLAnd(const BinaryOperator *E) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 454 | CounterMap[E] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 455 | Visit(E->getLHS()); |
| 456 | Visit(E->getRHS()); |
| 457 | } |
| 458 | /// Assign a counter for the right hand side of a logical or operator. |
| 459 | void VisitBinLOr(const BinaryOperator *E) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 460 | CounterMap[E] = NextCounter++; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 461 | Visit(E->getLHS()); |
| 462 | Visit(E->getRHS()); |
| 463 | } |
| 464 | }; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 465 | |
| 466 | /// A StmtVisitor that propagates the raw counts through the AST and |
| 467 | /// records the count at statements where the value may change. |
| 468 | struct ComputeRegionCounts : public ConstStmtVisitor<ComputeRegionCounts> { |
| 469 | /// PGO state. |
| 470 | CodeGenPGO &PGO; |
| 471 | |
| 472 | /// A flag that is set when the current count should be recorded on the |
| 473 | /// next statement, such as at the exit of a loop. |
| 474 | bool RecordNextStmtCount; |
| 475 | |
| 476 | /// The map of statements to count values. |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 477 | llvm::DenseMap<const Stmt *, uint64_t> &CountMap; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 478 | |
| 479 | /// BreakContinueStack - Keep counts of breaks and continues inside loops. |
| 480 | struct BreakContinue { |
| 481 | uint64_t BreakCount; |
| 482 | uint64_t ContinueCount; |
| 483 | BreakContinue() : BreakCount(0), ContinueCount(0) {} |
| 484 | }; |
| 485 | SmallVector<BreakContinue, 8> BreakContinueStack; |
| 486 | |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 487 | ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &CountMap, |
| 488 | CodeGenPGO &PGO) |
| 489 | : PGO(PGO), RecordNextStmtCount(false), CountMap(CountMap) {} |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 490 | |
| 491 | void RecordStmtCount(const Stmt *S) { |
| 492 | if (RecordNextStmtCount) { |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 493 | CountMap[S] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 494 | RecordNextStmtCount = false; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void VisitStmt(const Stmt *S) { |
| 499 | RecordStmtCount(S); |
| 500 | for (Stmt::const_child_range I = S->children(); I; ++I) { |
| 501 | if (*I) |
| 502 | this->Visit(*I); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void VisitFunctionDecl(const FunctionDecl *S) { |
| 507 | RegionCounter Cnt(PGO, S->getBody()); |
| 508 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 509 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 510 | Visit(S->getBody()); |
| 511 | } |
| 512 | |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 513 | void VisitObjCMethodDecl(const ObjCMethodDecl *S) { |
| 514 | RegionCounter Cnt(PGO, S->getBody()); |
| 515 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 516 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 517 | Visit(S->getBody()); |
| 518 | } |
| 519 | |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 520 | void VisitBlockDecl(const BlockDecl *S) { |
| 521 | RegionCounter Cnt(PGO, S->getBody()); |
| 522 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 523 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 524 | Visit(S->getBody()); |
| 525 | } |
| 526 | |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 527 | void VisitReturnStmt(const ReturnStmt *S) { |
| 528 | RecordStmtCount(S); |
| 529 | if (S->getRetValue()) |
| 530 | Visit(S->getRetValue()); |
| 531 | PGO.setCurrentRegionUnreachable(); |
| 532 | RecordNextStmtCount = true; |
| 533 | } |
| 534 | |
| 535 | void VisitGotoStmt(const GotoStmt *S) { |
| 536 | RecordStmtCount(S); |
| 537 | PGO.setCurrentRegionUnreachable(); |
| 538 | RecordNextStmtCount = true; |
| 539 | } |
| 540 | |
| 541 | void VisitLabelStmt(const LabelStmt *S) { |
| 542 | RecordNextStmtCount = false; |
| 543 | RegionCounter Cnt(PGO, S); |
| 544 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 545 | CountMap[S] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 546 | Visit(S->getSubStmt()); |
| 547 | } |
| 548 | |
| 549 | void VisitBreakStmt(const BreakStmt *S) { |
| 550 | RecordStmtCount(S); |
| 551 | assert(!BreakContinueStack.empty() && "break not in a loop or switch!"); |
| 552 | BreakContinueStack.back().BreakCount += PGO.getCurrentRegionCount(); |
| 553 | PGO.setCurrentRegionUnreachable(); |
| 554 | RecordNextStmtCount = true; |
| 555 | } |
| 556 | |
| 557 | void VisitContinueStmt(const ContinueStmt *S) { |
| 558 | RecordStmtCount(S); |
| 559 | assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); |
| 560 | BreakContinueStack.back().ContinueCount += PGO.getCurrentRegionCount(); |
| 561 | PGO.setCurrentRegionUnreachable(); |
| 562 | RecordNextStmtCount = true; |
| 563 | } |
| 564 | |
| 565 | void VisitWhileStmt(const WhileStmt *S) { |
| 566 | RecordStmtCount(S); |
| 567 | RegionCounter Cnt(PGO, S); |
| 568 | BreakContinueStack.push_back(BreakContinue()); |
| 569 | // Visit the body region first so the break/continue adjustments can be |
| 570 | // included when visiting the condition. |
| 571 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 572 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 573 | Visit(S->getBody()); |
| 574 | Cnt.adjustForControlFlow(); |
| 575 | |
| 576 | // ...then go back and propagate counts through the condition. The count |
| 577 | // at the start of the condition is the sum of the incoming edges, |
| 578 | // the backedge from the end of the loop body, and the edges from |
| 579 | // continue statements. |
| 580 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 581 | Cnt.setCurrentRegionCount(Cnt.getParentCount() + |
| 582 | Cnt.getAdjustedCount() + BC.ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 583 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 584 | Visit(S->getCond()); |
| 585 | Cnt.adjustForControlFlow(); |
| 586 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 587 | RecordNextStmtCount = true; |
| 588 | } |
| 589 | |
| 590 | void VisitDoStmt(const DoStmt *S) { |
| 591 | RecordStmtCount(S); |
| 592 | RegionCounter Cnt(PGO, S); |
| 593 | BreakContinueStack.push_back(BreakContinue()); |
| 594 | Cnt.beginRegion(/*AddIncomingFallThrough=*/true); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 595 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 596 | Visit(S->getBody()); |
| 597 | Cnt.adjustForControlFlow(); |
| 598 | |
| 599 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 600 | // The count at the start of the condition is equal to the count at the |
| 601 | // end of the body. The adjusted count does not include either the |
| 602 | // fall-through count coming into the loop or the continue count, so add |
| 603 | // both of those separately. This is coincidentally the same equation as |
| 604 | // with while loops but for different reasons. |
| 605 | Cnt.setCurrentRegionCount(Cnt.getParentCount() + |
| 606 | Cnt.getAdjustedCount() + BC.ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 607 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 608 | Visit(S->getCond()); |
| 609 | Cnt.adjustForControlFlow(); |
| 610 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 611 | RecordNextStmtCount = true; |
| 612 | } |
| 613 | |
| 614 | void VisitForStmt(const ForStmt *S) { |
| 615 | RecordStmtCount(S); |
| 616 | if (S->getInit()) |
| 617 | Visit(S->getInit()); |
| 618 | RegionCounter Cnt(PGO, S); |
| 619 | BreakContinueStack.push_back(BreakContinue()); |
| 620 | // Visit the body region first. (This is basically the same as a while |
| 621 | // loop; see further comments in VisitWhileStmt.) |
| 622 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 623 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 624 | Visit(S->getBody()); |
| 625 | Cnt.adjustForControlFlow(); |
| 626 | |
| 627 | // The increment is essentially part of the body but it needs to include |
| 628 | // the count for all the continue statements. |
| 629 | if (S->getInc()) { |
| 630 | Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() + |
| 631 | BreakContinueStack.back().ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 632 | CountMap[S->getInc()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 633 | Visit(S->getInc()); |
| 634 | Cnt.adjustForControlFlow(); |
| 635 | } |
| 636 | |
| 637 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 638 | |
| 639 | // ...then go back and propagate counts through the condition. |
| 640 | if (S->getCond()) { |
| 641 | Cnt.setCurrentRegionCount(Cnt.getParentCount() + |
| 642 | Cnt.getAdjustedCount() + |
| 643 | BC.ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 644 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 645 | Visit(S->getCond()); |
| 646 | Cnt.adjustForControlFlow(); |
| 647 | } |
| 648 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 649 | RecordNextStmtCount = true; |
| 650 | } |
| 651 | |
| 652 | void VisitCXXForRangeStmt(const CXXForRangeStmt *S) { |
| 653 | RecordStmtCount(S); |
| 654 | Visit(S->getRangeStmt()); |
| 655 | Visit(S->getBeginEndStmt()); |
| 656 | RegionCounter Cnt(PGO, S); |
| 657 | BreakContinueStack.push_back(BreakContinue()); |
| 658 | // Visit the body region first. (This is basically the same as a while |
| 659 | // loop; see further comments in VisitWhileStmt.) |
| 660 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 661 | CountMap[S->getLoopVarStmt()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 662 | Visit(S->getLoopVarStmt()); |
| 663 | Visit(S->getBody()); |
| 664 | Cnt.adjustForControlFlow(); |
| 665 | |
| 666 | // The increment is essentially part of the body but it needs to include |
| 667 | // the count for all the continue statements. |
| 668 | Cnt.setCurrentRegionCount(PGO.getCurrentRegionCount() + |
| 669 | BreakContinueStack.back().ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 670 | CountMap[S->getInc()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 671 | Visit(S->getInc()); |
| 672 | Cnt.adjustForControlFlow(); |
| 673 | |
| 674 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 675 | |
| 676 | // ...then go back and propagate counts through the condition. |
| 677 | Cnt.setCurrentRegionCount(Cnt.getParentCount() + |
| 678 | Cnt.getAdjustedCount() + |
| 679 | BC.ContinueCount); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 680 | CountMap[S->getCond()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 681 | Visit(S->getCond()); |
| 682 | Cnt.adjustForControlFlow(); |
| 683 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 684 | RecordNextStmtCount = true; |
| 685 | } |
| 686 | |
| 687 | void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) { |
| 688 | RecordStmtCount(S); |
| 689 | Visit(S->getElement()); |
| 690 | RegionCounter Cnt(PGO, S); |
| 691 | BreakContinueStack.push_back(BreakContinue()); |
| 692 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 693 | CountMap[S->getBody()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 694 | Visit(S->getBody()); |
| 695 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 696 | Cnt.adjustForControlFlow(); |
| 697 | Cnt.applyAdjustmentsToRegion(BC.BreakCount + BC.ContinueCount); |
| 698 | RecordNextStmtCount = true; |
| 699 | } |
| 700 | |
| 701 | void VisitSwitchStmt(const SwitchStmt *S) { |
| 702 | RecordStmtCount(S); |
| 703 | Visit(S->getCond()); |
| 704 | PGO.setCurrentRegionUnreachable(); |
| 705 | BreakContinueStack.push_back(BreakContinue()); |
| 706 | Visit(S->getBody()); |
| 707 | // If the switch is inside a loop, add the continue counts. |
| 708 | BreakContinue BC = BreakContinueStack.pop_back_val(); |
| 709 | if (!BreakContinueStack.empty()) |
| 710 | BreakContinueStack.back().ContinueCount += BC.ContinueCount; |
| 711 | RegionCounter ExitCnt(PGO, S); |
| 712 | ExitCnt.beginRegion(); |
| 713 | RecordNextStmtCount = true; |
| 714 | } |
| 715 | |
| 716 | void VisitCaseStmt(const CaseStmt *S) { |
| 717 | RecordNextStmtCount = false; |
| 718 | RegionCounter Cnt(PGO, S); |
| 719 | Cnt.beginRegion(/*AddIncomingFallThrough=*/true); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 720 | CountMap[S] = Cnt.getCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 721 | RecordNextStmtCount = true; |
| 722 | Visit(S->getSubStmt()); |
| 723 | } |
| 724 | |
| 725 | void VisitDefaultStmt(const DefaultStmt *S) { |
| 726 | RecordNextStmtCount = false; |
| 727 | RegionCounter Cnt(PGO, S); |
| 728 | Cnt.beginRegion(/*AddIncomingFallThrough=*/true); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 729 | CountMap[S] = Cnt.getCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 730 | RecordNextStmtCount = true; |
| 731 | Visit(S->getSubStmt()); |
| 732 | } |
| 733 | |
| 734 | void VisitIfStmt(const IfStmt *S) { |
| 735 | RecordStmtCount(S); |
| 736 | RegionCounter Cnt(PGO, S); |
| 737 | Visit(S->getCond()); |
| 738 | |
| 739 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 740 | CountMap[S->getThen()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 741 | Visit(S->getThen()); |
| 742 | Cnt.adjustForControlFlow(); |
| 743 | |
| 744 | if (S->getElse()) { |
| 745 | Cnt.beginElseRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 746 | CountMap[S->getElse()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 747 | Visit(S->getElse()); |
| 748 | Cnt.adjustForControlFlow(); |
| 749 | } |
| 750 | Cnt.applyAdjustmentsToRegion(0); |
| 751 | RecordNextStmtCount = true; |
| 752 | } |
| 753 | |
| 754 | void VisitCXXTryStmt(const CXXTryStmt *S) { |
| 755 | RecordStmtCount(S); |
| 756 | Visit(S->getTryBlock()); |
| 757 | for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I) |
| 758 | Visit(S->getHandler(I)); |
| 759 | RegionCounter Cnt(PGO, S); |
| 760 | Cnt.beginRegion(); |
| 761 | RecordNextStmtCount = true; |
| 762 | } |
| 763 | |
| 764 | void VisitCXXCatchStmt(const CXXCatchStmt *S) { |
| 765 | RecordNextStmtCount = false; |
| 766 | RegionCounter Cnt(PGO, S); |
| 767 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 768 | CountMap[S] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 769 | Visit(S->getHandlerBlock()); |
| 770 | } |
| 771 | |
| 772 | void VisitConditionalOperator(const ConditionalOperator *E) { |
| 773 | RecordStmtCount(E); |
| 774 | RegionCounter Cnt(PGO, E); |
| 775 | Visit(E->getCond()); |
| 776 | |
| 777 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 778 | CountMap[E->getTrueExpr()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 779 | Visit(E->getTrueExpr()); |
| 780 | Cnt.adjustForControlFlow(); |
| 781 | |
| 782 | Cnt.beginElseRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 783 | CountMap[E->getFalseExpr()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 784 | Visit(E->getFalseExpr()); |
| 785 | Cnt.adjustForControlFlow(); |
| 786 | |
| 787 | Cnt.applyAdjustmentsToRegion(0); |
| 788 | RecordNextStmtCount = true; |
| 789 | } |
| 790 | |
| 791 | void VisitBinLAnd(const BinaryOperator *E) { |
| 792 | RecordStmtCount(E); |
| 793 | RegionCounter Cnt(PGO, E); |
| 794 | Visit(E->getLHS()); |
| 795 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 796 | CountMap[E->getRHS()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 797 | Visit(E->getRHS()); |
| 798 | Cnt.adjustForControlFlow(); |
| 799 | Cnt.applyAdjustmentsToRegion(0); |
| 800 | RecordNextStmtCount = true; |
| 801 | } |
| 802 | |
| 803 | void VisitBinLOr(const BinaryOperator *E) { |
| 804 | RecordStmtCount(E); |
| 805 | RegionCounter Cnt(PGO, E); |
| 806 | Visit(E->getLHS()); |
| 807 | Cnt.beginRegion(); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 808 | CountMap[E->getRHS()] = PGO.getCurrentRegionCount(); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 809 | Visit(E->getRHS()); |
| 810 | Cnt.adjustForControlFlow(); |
| 811 | Cnt.applyAdjustmentsToRegion(0); |
| 812 | RecordNextStmtCount = true; |
| 813 | } |
| 814 | }; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Duncan P. N. Exon Smith | d971cd1 | 2014-03-28 17:53:22 +0000 | [diff] [blame] | 817 | static void emitRuntimeHook(CodeGenModule &CGM) { |
Duncan P. N. Exon Smith | 966e320 | 2014-03-28 19:27:37 +0000 | [diff] [blame] | 818 | LLVM_CONSTEXPR const char *RuntimeVarName = "__llvm_profile_runtime"; |
| 819 | LLVM_CONSTEXPR const char *RuntimeUserName = "__llvm_profile_runtime_user"; |
Duncan P. N. Exon Smith | d971cd1 | 2014-03-28 17:53:22 +0000 | [diff] [blame] | 820 | if (CGM.getModule().getGlobalVariable(RuntimeVarName)) |
| 821 | return; |
| 822 | |
| 823 | // Declare the runtime hook. |
| 824 | llvm::LLVMContext &Ctx = CGM.getLLVMContext(); |
| 825 | auto *Int32Ty = llvm::Type::getInt32Ty(Ctx); |
| 826 | auto *Var = new llvm::GlobalVariable(CGM.getModule(), Int32Ty, false, |
| 827 | llvm::GlobalValue::ExternalLinkage, |
| 828 | nullptr, RuntimeVarName); |
| 829 | |
| 830 | // Make a function that uses it. |
| 831 | auto *User = llvm::Function::Create(llvm::FunctionType::get(Int32Ty, false), |
| 832 | llvm::GlobalValue::LinkOnceODRLinkage, |
| 833 | RuntimeUserName, &CGM.getModule()); |
| 834 | User->addFnAttr(llvm::Attribute::NoInline); |
| 835 | if (CGM.getCodeGenOpts().DisableRedZone) |
| 836 | User->addFnAttr(llvm::Attribute::NoRedZone); |
| 837 | CGBuilderTy Builder(llvm::BasicBlock::Create(CGM.getLLVMContext(), "", User)); |
| 838 | auto *Load = Builder.CreateLoad(Var); |
| 839 | Builder.CreateRet(Load); |
| 840 | |
| 841 | // Create a use of the function. Now the definition of the runtime variable |
| 842 | // should get pulled in, along with any static initializears. |
| 843 | CGM.addUsedGlobal(User); |
| 844 | } |
| 845 | |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 846 | void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) { |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 847 | bool InstrumentRegions = CGM.getCodeGenOpts().ProfileInstrGenerate; |
Justin Bogner | d66a17d | 2014-03-12 21:06:31 +0000 | [diff] [blame] | 848 | PGOProfileData *PGOData = CGM.getPGOData(); |
| 849 | if (!InstrumentRegions && !PGOData) |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 850 | return; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 851 | if (!D) |
| 852 | return; |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 853 | setFuncName(Fn); |
Duncan P. N. Exon Smith | 7c41451 | 2014-03-20 22:50:08 +0000 | [diff] [blame] | 854 | |
| 855 | // Set the linkage for variables based on the function linkage. Usually, we |
| 856 | // want to match it, but available_externally and extern_weak both have the |
| 857 | // wrong semantics. |
Duncan P. N. Exon Smith | 73f7862 | 2014-03-20 22:49:50 +0000 | [diff] [blame] | 858 | VarLinkage = Fn->getLinkage(); |
Duncan P. N. Exon Smith | 7c41451 | 2014-03-20 22:50:08 +0000 | [diff] [blame] | 859 | switch (VarLinkage) { |
| 860 | case llvm::GlobalValue::ExternalWeakLinkage: |
| 861 | VarLinkage = llvm::GlobalValue::LinkOnceAnyLinkage; |
| 862 | break; |
| 863 | case llvm::GlobalValue::AvailableExternallyLinkage: |
| 864 | VarLinkage = llvm::GlobalValue::LinkOnceODRLinkage; |
| 865 | break; |
| 866 | default: |
| 867 | break; |
| 868 | } |
| 869 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 870 | mapRegionCounters(D); |
Duncan P. N. Exon Smith | d971cd1 | 2014-03-28 17:53:22 +0000 | [diff] [blame] | 871 | if (InstrumentRegions) { |
| 872 | emitRuntimeHook(CGM); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 873 | emitCounterVariables(); |
Duncan P. N. Exon Smith | d971cd1 | 2014-03-28 17:53:22 +0000 | [diff] [blame] | 874 | } |
Justin Bogner | d66a17d | 2014-03-12 21:06:31 +0000 | [diff] [blame] | 875 | if (PGOData) { |
| 876 | loadRegionCounts(PGOData); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 877 | computeRegionCounts(D); |
Justin Bogner | d66a17d | 2014-03-12 21:06:31 +0000 | [diff] [blame] | 878 | applyFunctionAttributes(PGOData, Fn); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 879 | } |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | void CodeGenPGO::mapRegionCounters(const Decl *D) { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 883 | RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 884 | MapRegionCounters Walker(*RegionCounterMap); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 885 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 886 | Walker.VisitFunctionDecl(FD); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 887 | else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) |
| 888 | Walker.VisitObjCMethodDecl(MD); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 889 | else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D)) |
| 890 | Walker.VisitBlockDecl(BD); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 891 | NumRegionCounters = Walker.NextCounter; |
Justin Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 892 | // FIXME: The number of counters isn't sufficient for the hash |
| 893 | FunctionHash = NumRegionCounters; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 896 | void CodeGenPGO::computeRegionCounts(const Decl *D) { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 897 | StmtCountMap.reset(new llvm::DenseMap<const Stmt *, uint64_t>); |
Duncan P. N. Exon Smith | 3586be7 | 2014-03-26 19:26:02 +0000 | [diff] [blame] | 898 | ComputeRegionCounts Walker(*StmtCountMap, *this); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 899 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 900 | Walker.VisitFunctionDecl(FD); |
Bob Wilson | 5ec8fe1 | 2014-03-06 06:10:02 +0000 | [diff] [blame] | 901 | else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) |
| 902 | Walker.VisitObjCMethodDecl(MD); |
Bob Wilson | c845c00 | 2014-03-06 20:24:27 +0000 | [diff] [blame] | 903 | else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D)) |
| 904 | Walker.VisitBlockDecl(BD); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Justin Bogner | d66a17d | 2014-03-12 21:06:31 +0000 | [diff] [blame] | 907 | void CodeGenPGO::applyFunctionAttributes(PGOProfileData *PGOData, |
Justin Bogner | 4c9c45c | 2014-03-12 18:14:32 +0000 | [diff] [blame] | 908 | llvm::Function *Fn) { |
| 909 | if (!haveRegionCounts()) |
| 910 | return; |
| 911 | |
Justin Bogner | d66a17d | 2014-03-12 21:06:31 +0000 | [diff] [blame] | 912 | uint64_t MaxFunctionCount = PGOData->getMaximumFunctionCount(); |
Justin Bogner | 4c9c45c | 2014-03-12 18:14:32 +0000 | [diff] [blame] | 913 | uint64_t FunctionCount = getRegionCount(0); |
| 914 | if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount)) |
| 915 | // Turn on InlineHint attribute for hot functions. |
| 916 | // FIXME: 30% is from preliminary tuning on SPEC, it may not be optimal. |
| 917 | Fn->addFnAttr(llvm::Attribute::InlineHint); |
| 918 | else if (FunctionCount <= (uint64_t)(0.01 * (double)MaxFunctionCount)) |
| 919 | // Turn on Cold attribute for cold functions. |
| 920 | // FIXME: 1% is from preliminary tuning on SPEC, it may not be optimal. |
| 921 | Fn->addFnAttr(llvm::Attribute::Cold); |
| 922 | } |
| 923 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 924 | void CodeGenPGO::emitCounterVariables() { |
| 925 | llvm::LLVMContext &Ctx = CGM.getLLVMContext(); |
| 926 | llvm::ArrayType *CounterTy = llvm::ArrayType::get(llvm::Type::getInt64Ty(Ctx), |
| 927 | NumRegionCounters); |
| 928 | RegionCounters = |
Duncan P. N. Exon Smith | 73f7862 | 2014-03-20 22:49:50 +0000 | [diff] [blame] | 929 | new llvm::GlobalVariable(CGM.getModule(), CounterTy, false, VarLinkage, |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 930 | llvm::Constant::getNullValue(CounterTy), |
Duncan P. N. Exon Smith | 2fe531c | 2014-03-17 21:18:30 +0000 | [diff] [blame] | 931 | getFuncVarName("counters")); |
| 932 | RegionCounters->setAlignment(8); |
| 933 | RegionCounters->setSection(getCountersSection(CGM)); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | void CodeGenPGO::emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter) { |
Bob Wilson | 749ebc7 | 2014-03-06 04:55:28 +0000 | [diff] [blame] | 937 | if (!RegionCounters) |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 938 | return; |
| 939 | llvm::Value *Addr = |
| 940 | Builder.CreateConstInBoundsGEP2_64(RegionCounters, 0, Counter); |
| 941 | llvm::Value *Count = Builder.CreateLoad(Addr, "pgocount"); |
| 942 | Count = Builder.CreateAdd(Count, Builder.getInt64(1)); |
| 943 | Builder.CreateStore(Count, Addr); |
| 944 | } |
| 945 | |
Justin Bogner | d66a17d | 2014-03-12 21:06:31 +0000 | [diff] [blame] | 946 | void CodeGenPGO::loadRegionCounts(PGOProfileData *PGOData) { |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 947 | // For now, ignore the counts from the PGO data file only if the number of |
| 948 | // counters does not match. This could be tightened down in the future to |
| 949 | // ignore counts when the input changes in various ways, e.g., by comparing a |
| 950 | // hash value based on some characteristics of the input. |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 951 | RegionCounts.reset(new std::vector<uint64_t>); |
Justin Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 952 | uint64_t Hash; |
| 953 | if (PGOData->getFunctionCounts(getFuncName(), Hash, *RegionCounts) || |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 954 | Hash != FunctionHash || RegionCounts->size() != NumRegionCounters) |
| 955 | RegionCounts.reset(); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | void CodeGenPGO::destroyRegionCounters() { |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 959 | RegionCounterMap.reset(); |
| 960 | StmtCountMap.reset(); |
| 961 | RegionCounts.reset(); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 964 | /// \brief Calculate what to divide by to scale weights. |
| 965 | /// |
| 966 | /// Given the maximum weight, calculate a divisor that will scale all the |
| 967 | /// weights to strictly less than UINT32_MAX. |
| 968 | static uint64_t calculateWeightScale(uint64_t MaxWeight) { |
| 969 | return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1; |
| 970 | } |
| 971 | |
| 972 | /// \brief Scale an individual branch weight (and add 1). |
| 973 | /// |
| 974 | /// Scale a 64-bit weight down to 32-bits using \c Scale. |
| 975 | /// |
| 976 | /// According to Laplace's Rule of Succession, it is better to compute the |
| 977 | /// weight based on the count plus 1, so universally add 1 to the value. |
| 978 | /// |
| 979 | /// \pre \c Scale was calculated by \a calculateWeightScale() with a weight no |
| 980 | /// greater than \c Weight. |
| 981 | static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale) { |
| 982 | assert(Scale && "scale by 0?"); |
| 983 | uint64_t Scaled = Weight / Scale + 1; |
| 984 | assert(Scaled <= UINT32_MAX && "overflow 32-bits"); |
| 985 | return Scaled; |
| 986 | } |
| 987 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 988 | llvm::MDNode *CodeGenPGO::createBranchWeights(uint64_t TrueCount, |
| 989 | uint64_t FalseCount) { |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 990 | // Check for empty weights. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 991 | if (!TrueCount && !FalseCount) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 992 | return nullptr; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 993 | |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 994 | // Calculate how to scale down to 32-bits. |
| 995 | uint64_t Scale = calculateWeightScale(std::max(TrueCount, FalseCount)); |
| 996 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 997 | llvm::MDBuilder MDHelper(CGM.getLLVMContext()); |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 998 | return MDHelper.createBranchWeights(scaleBranchWeight(TrueCount, Scale), |
| 999 | scaleBranchWeight(FalseCount, Scale)); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Bob Wilson | 95a27b0 | 2014-02-17 19:20:59 +0000 | [diff] [blame] | 1002 | llvm::MDNode *CodeGenPGO::createBranchWeights(ArrayRef<uint64_t> Weights) { |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1003 | // We need at least two elements to create meaningful weights. |
| 1004 | if (Weights.size() < 2) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 1005 | return nullptr; |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1006 | |
Justin Bogner | f3aefca | 2014-04-04 02:48:51 +0000 | [diff] [blame^] | 1007 | // Check for empty weights. |
| 1008 | uint64_t MaxWeight = *std::max_element(Weights.begin(), Weights.end()); |
| 1009 | if (MaxWeight == 0) |
| 1010 | return nullptr; |
| 1011 | |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1012 | // Calculate how to scale down to 32-bits. |
Justin Bogner | f3aefca | 2014-04-04 02:48:51 +0000 | [diff] [blame^] | 1013 | uint64_t Scale = calculateWeightScale(MaxWeight); |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1014 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1015 | SmallVector<uint32_t, 16> ScaledWeights; |
| 1016 | ScaledWeights.reserve(Weights.size()); |
Duncan P. N. Exon Smith | 38402dc | 2014-03-11 18:18:10 +0000 | [diff] [blame] | 1017 | for (uint64_t W : Weights) |
| 1018 | ScaledWeights.push_back(scaleBranchWeight(W, Scale)); |
| 1019 | |
| 1020 | llvm::MDBuilder MDHelper(CGM.getLLVMContext()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 1021 | return MDHelper.createBranchWeights(ScaledWeights); |
| 1022 | } |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 1023 | |
| 1024 | llvm::MDNode *CodeGenPGO::createLoopWeights(const Stmt *Cond, |
| 1025 | RegionCounter &Cnt) { |
| 1026 | if (!haveRegionCounts()) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 1027 | return nullptr; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 1028 | uint64_t LoopCount = Cnt.getCount(); |
| 1029 | uint64_t CondCount = 0; |
| 1030 | bool Found = getStmtCount(Cond, CondCount); |
| 1031 | assert(Found && "missing expected loop condition count"); |
| 1032 | (void)Found; |
| 1033 | if (CondCount == 0) |
Duncan P. N. Exon Smith | a5f804a | 2014-03-20 18:40:55 +0000 | [diff] [blame] | 1034 | return nullptr; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 1035 | return createBranchWeights(LoopCount, |
| 1036 | std::max(CondCount, LoopCount) - LoopCount); |
| 1037 | } |