1 //=-- InstrProf.cpp - Instrumented profiling format support -----------------=// 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 // This file contains support for clang's instrumentation based PGO and 11 // coverage. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/ProfileData/InstrProf.h" 16 #include "llvm/ADT/StringExtras.h" 17 #include "llvm/IR/Constants.h" 18 #include "llvm/IR/Function.h" 19 #include "llvm/IR/GlobalVariable.h" 20 #include "llvm/IR/Module.h" 21 #include "llvm/Support/Compression.h" 22 #include "llvm/Support/ErrorHandling.h" 23 #include "llvm/Support/LEB128.h" 24 #include "llvm/Support/ManagedStatic.h" 25 26 using namespace llvm; 27 28 namespace { 29 class InstrProfErrorCategoryType : public std::error_category { 30 const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; } 31 std::string message(int IE) const override { 32 instrprof_error E = static_cast<instrprof_error>(IE); 33 switch (E) { 34 case instrprof_error::success: 35 return "Success"; 36 case instrprof_error::eof: 37 return "End of File"; 38 case instrprof_error::unrecognized_format: 39 return "Unrecognized instrumentation profile encoding format"; 40 case instrprof_error::bad_magic: 41 return "Invalid instrumentation profile data (bad magic)"; 42 case instrprof_error::bad_header: 43 return "Invalid instrumentation profile data (file header is corrupt)"; 44 case instrprof_error::unsupported_version: 45 return "Unsupported instrumentation profile format version"; 46 case instrprof_error::unsupported_hash_type: 47 return "Unsupported instrumentation profile hash type"; 48 case instrprof_error::too_large: 49 return "Too much profile data"; 50 case instrprof_error::truncated: 51 return "Truncated profile data"; 52 case instrprof_error::malformed: 53 return "Malformed instrumentation profile data"; 54 case instrprof_error::unknown_function: 55 return "No profile data available for function"; 56 case instrprof_error::hash_mismatch: 57 return "Function control flow change detected (hash mismatch)"; 58 case instrprof_error::count_mismatch: 59 return "Function basic block count change detected (counter mismatch)"; 60 case instrprof_error::counter_overflow: 61 return "Counter overflow"; 62 case instrprof_error::value_site_count_mismatch: 63 return "Function value site count change detected (counter mismatch)"; 64 } 65 llvm_unreachable("A value of instrprof_error has no message."); 66 } 67 }; 68 } // end anonymous namespace 69 70 static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory; 71 72 const std::error_category &llvm::instrprof_category() { 73 return *ErrorCategory; 74 } 75 76 namespace llvm { 77 78 std::string getPGOFuncName(StringRef RawFuncName, 79 GlobalValue::LinkageTypes Linkage, 80 StringRef FileName, 81 uint64_t Version LLVM_ATTRIBUTE_UNUSED) { 82 83 // Function names may be prefixed with a binary '1' to indicate 84 // that the backend should not modify the symbols due to any platform 85 // naming convention. Do not include that '1' in the PGO profile name. 86 if (RawFuncName[0] == '\1') 87 RawFuncName = RawFuncName.substr(1); 88 89 std::string FuncName = RawFuncName; 90 if (llvm::GlobalValue::isLocalLinkage(Linkage)) { 91 // For local symbols, prepend the main file name to distinguish them. 92 // Do not include the full path in the file name since there's no guarantee 93 // that it will stay the same, e.g., if the files are checked out from 94 // version control in different locations. 95 if (FileName.empty()) 96 FuncName = FuncName.insert(0, "<unknown>:"); 97 else 98 FuncName = FuncName.insert(0, FileName.str() + ":"); 99 } 100 return FuncName; 101 } 102 103 std::string getPGOFuncName(const Function &F, uint64_t Version) { 104 return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName(), 105 Version); 106 } 107 108 StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) { 109 if (FileName.empty()) 110 return PGOFuncName; 111 // Drop the file name including ':'. See also getPGOFuncName. 112 if (PGOFuncName.startswith(FileName)) 113 PGOFuncName = PGOFuncName.drop_front(FileName.size() + 1); 114 return PGOFuncName; 115 } 116 117 // \p FuncName is the string used as profile lookup key for the function. A 118 // symbol is created to hold the name. Return the legalized symbol name. 119 static std::string getPGOFuncNameVarName(StringRef FuncName, 120 GlobalValue::LinkageTypes Linkage) { 121 std::string VarName = getInstrProfNameVarPrefix(); 122 VarName += FuncName; 123 124 if (!GlobalValue::isLocalLinkage(Linkage)) 125 return VarName; 126 127 // Now fix up illegal chars in local VarName that may upset the assembler. 128 const char *InvalidChars = "-:<>\"'"; 129 size_t found = VarName.find_first_of(InvalidChars); 130 while (found != std::string::npos) { 131 VarName[found] = '_'; 132 found = VarName.find_first_of(InvalidChars, found + 1); 133 } 134 return VarName; 135 } 136 137 GlobalVariable *createPGOFuncNameVar(Module &M, 138 GlobalValue::LinkageTypes Linkage, 139 StringRef FuncName) { 140 141 // We generally want to match the function's linkage, but available_externally 142 // and extern_weak both have the wrong semantics, and anything that doesn't 143 // need to link across compilation units doesn't need to be visible at all. 144 if (Linkage == GlobalValue::ExternalWeakLinkage) 145 Linkage = GlobalValue::LinkOnceAnyLinkage; 146 else if (Linkage == GlobalValue::AvailableExternallyLinkage) 147 Linkage = GlobalValue::LinkOnceODRLinkage; 148 else if (Linkage == GlobalValue::InternalLinkage || 149 Linkage == GlobalValue::ExternalLinkage) 150 Linkage = GlobalValue::PrivateLinkage; 151 152 auto *Value = ConstantDataArray::getString(M.getContext(), FuncName, false); 153 auto FuncNameVar = 154 new GlobalVariable(M, Value->getType(), true, Linkage, Value, 155 getPGOFuncNameVarName(FuncName, Linkage)); 156 157 // Hide the symbol so that we correctly get a copy for each executable. 158 if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage())) 159 FuncNameVar->setVisibility(GlobalValue::HiddenVisibility); 160 161 return FuncNameVar; 162 } 163 164 GlobalVariable *createPGOFuncNameVar(Function &F, StringRef FuncName) { 165 return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), FuncName); 166 } 167 168 void InstrProfSymtab::create(const Module &M) { 169 for (const Function &F : M) 170 addFuncName(getPGOFuncName(F)); 171 172 finalizeSymtab(); 173 } 174 175 int collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs, 176 bool doCompression, std::string &Result) { 177 uint8_t Header[16], *P = Header; 178 std::string UncompressedNameStrings = 179 join(NameStrs.begin(), NameStrs.end(), StringRef(" ")); 180 181 unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P); 182 P += EncLen; 183 184 auto WriteStringToResult = [&](size_t CompressedLen, 185 const std::string &InputStr) { 186 EncLen = encodeULEB128(CompressedLen, P); 187 P += EncLen; 188 char *HeaderStr = reinterpret_cast<char *>(&Header[0]); 189 unsigned HeaderLen = P - &Header[0]; 190 Result.append(HeaderStr, HeaderLen); 191 Result += InputStr; 192 return 0; 193 }; 194 195 if (!doCompression) 196 return WriteStringToResult(0, UncompressedNameStrings); 197 198 SmallVector<char, 128> CompressedNameStrings; 199 zlib::Status Success = 200 zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings, 201 zlib::BestSizeCompression); 202 203 if (Success != zlib::StatusOK) 204 return 1; 205 206 return WriteStringToResult( 207 CompressedNameStrings.size(), 208 std::string(CompressedNameStrings.data(), CompressedNameStrings.size())); 209 } 210 211 StringRef getPGOFuncNameInitializer(GlobalVariable *NameVar) { 212 auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer()); 213 StringRef NameStr = 214 Arr->isCString() ? Arr->getAsCString() : Arr->getAsString(); 215 return NameStr; 216 } 217 218 int collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars, 219 std::string &Result, bool doCompression) { 220 std::vector<std::string> NameStrs; 221 for (auto *NameVar : NameVars) { 222 NameStrs.push_back(getPGOFuncNameInitializer(NameVar)); 223 } 224 return collectPGOFuncNameStrings( 225 NameStrs, zlib::isAvailable() && doCompression, Result); 226 } 227 228 int readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) { 229 const uint8_t *P = reinterpret_cast<const uint8_t *>(NameStrings.data()); 230 const uint8_t *EndP = reinterpret_cast<const uint8_t *>(NameStrings.data() + 231 NameStrings.size()); 232 while (P < EndP) { 233 uint32_t N; 234 uint64_t UncompressedSize = decodeULEB128(P, &N); 235 P += N; 236 uint64_t CompressedSize = decodeULEB128(P, &N); 237 P += N; 238 bool isCompressed = (CompressedSize != 0); 239 SmallString<128> UncompressedNameStrings; 240 StringRef NameStrings; 241 if (isCompressed) { 242 StringRef CompressedNameStrings(reinterpret_cast<const char *>(P), 243 CompressedSize); 244 if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings, 245 UncompressedSize) != zlib::StatusOK) 246 return 1; 247 P += CompressedSize; 248 NameStrings = StringRef(UncompressedNameStrings.data(), 249 UncompressedNameStrings.size()); 250 } else { 251 NameStrings = 252 StringRef(reinterpret_cast<const char *>(P), UncompressedSize); 253 P += UncompressedSize; 254 } 255 // Now parse the name strings. 256 SmallVector<StringRef, 0> Names; 257 NameStrings.split(Names, ' '); 258 for (StringRef &Name : Names) 259 Symtab.addFuncName(Name); 260 261 while (P < EndP && *P == 0) 262 P++; 263 } 264 Symtab.finalizeSymtab(); 265 return 0; 266 } 267 268 instrprof_error InstrProfValueSiteRecord::merge(InstrProfValueSiteRecord &Input, 269 uint64_t Weight) { 270 this->sortByTargetValues(); 271 Input.sortByTargetValues(); 272 auto I = ValueData.begin(); 273 auto IE = ValueData.end(); 274 instrprof_error Result = instrprof_error::success; 275 for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE; 276 ++J) { 277 while (I != IE && I->Value < J->Value) 278 ++I; 279 if (I != IE && I->Value == J->Value) { 280 bool Overflowed; 281 I->Count = SaturatingMultiplyAdd(J->Count, Weight, I->Count, &Overflowed); 282 if (Overflowed) 283 Result = instrprof_error::counter_overflow; 284 ++I; 285 continue; 286 } 287 ValueData.insert(I, *J); 288 } 289 return Result; 290 } 291 292 instrprof_error InstrProfValueSiteRecord::scale(uint64_t Weight) { 293 instrprof_error Result = instrprof_error::success; 294 for (auto I = ValueData.begin(), IE = ValueData.end(); I != IE; ++I) { 295 bool Overflowed; 296 I->Count = SaturatingMultiply(I->Count, Weight, &Overflowed); 297 if (Overflowed) 298 Result = instrprof_error::counter_overflow; 299 } 300 return Result; 301 } 302 303 // Merge Value Profile data from Src record to this record for ValueKind. 304 // Scale merged value counts by \p Weight. 305 instrprof_error InstrProfRecord::mergeValueProfData(uint32_t ValueKind, 306 InstrProfRecord &Src, 307 uint64_t Weight) { 308 uint32_t ThisNumValueSites = getNumValueSites(ValueKind); 309 uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind); 310 if (ThisNumValueSites != OtherNumValueSites) 311 return instrprof_error::value_site_count_mismatch; 312 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords = 313 getValueSitesForKind(ValueKind); 314 std::vector<InstrProfValueSiteRecord> &OtherSiteRecords = 315 Src.getValueSitesForKind(ValueKind); 316 instrprof_error Result = instrprof_error::success; 317 for (uint32_t I = 0; I < ThisNumValueSites; I++) 318 MergeResult(Result, ThisSiteRecords[I].merge(OtherSiteRecords[I], Weight)); 319 return Result; 320 } 321 322 instrprof_error InstrProfRecord::merge(InstrProfRecord &Other, 323 uint64_t Weight) { 324 // If the number of counters doesn't match we either have bad data 325 // or a hash collision. 326 if (Counts.size() != Other.Counts.size()) 327 return instrprof_error::count_mismatch; 328 329 instrprof_error Result = instrprof_error::success; 330 331 for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) { 332 bool Overflowed; 333 Counts[I] = 334 SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed); 335 if (Overflowed) 336 Result = instrprof_error::counter_overflow; 337 } 338 339 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) 340 MergeResult(Result, mergeValueProfData(Kind, Other, Weight)); 341 342 return Result; 343 } 344 345 instrprof_error InstrProfRecord::scaleValueProfData(uint32_t ValueKind, 346 uint64_t Weight) { 347 uint32_t ThisNumValueSites = getNumValueSites(ValueKind); 348 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords = 349 getValueSitesForKind(ValueKind); 350 instrprof_error Result = instrprof_error::success; 351 for (uint32_t I = 0; I < ThisNumValueSites; I++) 352 MergeResult(Result, ThisSiteRecords[I].scale(Weight)); 353 return Result; 354 } 355 356 instrprof_error InstrProfRecord::scale(uint64_t Weight) { 357 instrprof_error Result = instrprof_error::success; 358 for (auto &Count : this->Counts) { 359 bool Overflowed; 360 Count = SaturatingMultiply(Count, Weight, &Overflowed); 361 if (Overflowed && Result == instrprof_error::success) { 362 Result = instrprof_error::counter_overflow; 363 } 364 } 365 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) 366 MergeResult(Result, scaleValueProfData(Kind, Weight)); 367 368 return Result; 369 } 370 371 // Map indirect call target name hash to name string. 372 uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind, 373 ValueMapType *ValueMap) { 374 if (!ValueMap) 375 return Value; 376 switch (ValueKind) { 377 case IPVK_IndirectCallTarget: { 378 auto Result = 379 std::lower_bound(ValueMap->begin(), ValueMap->end(), Value, 380 [](const std::pair<uint64_t, uint64_t> &LHS, 381 uint64_t RHS) { return LHS.first < RHS; }); 382 if (Result != ValueMap->end()) 383 Value = (uint64_t)Result->second; 384 break; 385 } 386 } 387 return Value; 388 } 389 390 void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site, 391 InstrProfValueData *VData, uint32_t N, 392 ValueMapType *ValueMap) { 393 for (uint32_t I = 0; I < N; I++) { 394 VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap); 395 } 396 std::vector<InstrProfValueSiteRecord> &ValueSites = 397 getValueSitesForKind(ValueKind); 398 if (N == 0) 399 ValueSites.push_back(InstrProfValueSiteRecord()); 400 else 401 ValueSites.emplace_back(VData, VData + N); 402 } 403 404 #define INSTR_PROF_COMMON_API_IMPL 405 #include "llvm/ProfileData/InstrProfData.inc" 406 407 /*! 408 * \brief ValueProfRecordClosure Interface implementation for InstrProfRecord 409 * class. These C wrappers are used as adaptors so that C++ code can be 410 * invoked as callbacks. 411 */ 412 uint32_t getNumValueKindsInstrProf(const void *Record) { 413 return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds(); 414 } 415 416 uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) { 417 return reinterpret_cast<const InstrProfRecord *>(Record) 418 ->getNumValueSites(VKind); 419 } 420 421 uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) { 422 return reinterpret_cast<const InstrProfRecord *>(Record) 423 ->getNumValueData(VKind); 424 } 425 426 uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK, 427 uint32_t S) { 428 return reinterpret_cast<const InstrProfRecord *>(R) 429 ->getNumValueDataForSite(VK, S); 430 } 431 432 void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst, 433 uint32_t K, uint32_t S, 434 uint64_t (*Mapper)(uint32_t, uint64_t)) { 435 return reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite( 436 Dst, K, S, Mapper); 437 } 438 439 ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) { 440 ValueProfData *VD = 441 (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData()); 442 memset(VD, 0, TotalSizeInBytes); 443 return VD; 444 } 445 446 static ValueProfRecordClosure InstrProfRecordClosure = { 447 nullptr, 448 getNumValueKindsInstrProf, 449 getNumValueSitesInstrProf, 450 getNumValueDataInstrProf, 451 getNumValueDataForSiteInstrProf, 452 nullptr, 453 getValueForSiteInstrProf, 454 allocValueProfDataInstrProf}; 455 456 // Wrapper implementation using the closure mechanism. 457 uint32_t ValueProfData::getSize(const InstrProfRecord &Record) { 458 InstrProfRecordClosure.Record = &Record; 459 return getValueProfDataSize(&InstrProfRecordClosure); 460 } 461 462 // Wrapper implementation using the closure mechanism. 463 std::unique_ptr<ValueProfData> 464 ValueProfData::serializeFrom(const InstrProfRecord &Record) { 465 InstrProfRecordClosure.Record = &Record; 466 467 std::unique_ptr<ValueProfData> VPD( 468 serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr)); 469 return VPD; 470 } 471 472 void ValueProfRecord::deserializeTo(InstrProfRecord &Record, 473 InstrProfRecord::ValueMapType *VMap) { 474 Record.reserveSites(Kind, NumValueSites); 475 476 InstrProfValueData *ValueData = getValueProfRecordValueData(this); 477 for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) { 478 uint8_t ValueDataCount = this->SiteCountArray[VSite]; 479 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap); 480 ValueData += ValueDataCount; 481 } 482 } 483 484 // For writing/serializing, Old is the host endianness, and New is 485 // byte order intended on disk. For Reading/deserialization, Old 486 // is the on-disk source endianness, and New is the host endianness. 487 void ValueProfRecord::swapBytes(support::endianness Old, 488 support::endianness New) { 489 using namespace support; 490 if (Old == New) 491 return; 492 493 if (getHostEndianness() != Old) { 494 sys::swapByteOrder<uint32_t>(NumValueSites); 495 sys::swapByteOrder<uint32_t>(Kind); 496 } 497 uint32_t ND = getValueProfRecordNumValueData(this); 498 InstrProfValueData *VD = getValueProfRecordValueData(this); 499 500 // No need to swap byte array: SiteCountArrray. 501 for (uint32_t I = 0; I < ND; I++) { 502 sys::swapByteOrder<uint64_t>(VD[I].Value); 503 sys::swapByteOrder<uint64_t>(VD[I].Count); 504 } 505 if (getHostEndianness() == Old) { 506 sys::swapByteOrder<uint32_t>(NumValueSites); 507 sys::swapByteOrder<uint32_t>(Kind); 508 } 509 } 510 511 void ValueProfData::deserializeTo(InstrProfRecord &Record, 512 InstrProfRecord::ValueMapType *VMap) { 513 if (NumValueKinds == 0) 514 return; 515 516 ValueProfRecord *VR = getFirstValueProfRecord(this); 517 for (uint32_t K = 0; K < NumValueKinds; K++) { 518 VR->deserializeTo(Record, VMap); 519 VR = getValueProfRecordNext(VR); 520 } 521 } 522 523 template <class T> 524 static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) { 525 using namespace support; 526 if (Orig == little) 527 return endian::readNext<T, little, unaligned>(D); 528 else 529 return endian::readNext<T, big, unaligned>(D); 530 } 531 532 static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) { 533 return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize)) 534 ValueProfData()); 535 } 536 537 instrprof_error ValueProfData::checkIntegrity() { 538 if (NumValueKinds > IPVK_Last + 1) 539 return instrprof_error::malformed; 540 // Total size needs to be mulltiple of quadword size. 541 if (TotalSize % sizeof(uint64_t)) 542 return instrprof_error::malformed; 543 544 ValueProfRecord *VR = getFirstValueProfRecord(this); 545 for (uint32_t K = 0; K < this->NumValueKinds; K++) { 546 if (VR->Kind > IPVK_Last) 547 return instrprof_error::malformed; 548 VR = getValueProfRecordNext(VR); 549 if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize) 550 return instrprof_error::malformed; 551 } 552 return instrprof_error::success; 553 } 554 555 ErrorOr<std::unique_ptr<ValueProfData>> 556 ValueProfData::getValueProfData(const unsigned char *D, 557 const unsigned char *const BufferEnd, 558 support::endianness Endianness) { 559 using namespace support; 560 if (D + sizeof(ValueProfData) > BufferEnd) 561 return instrprof_error::truncated; 562 563 const unsigned char *Header = D; 564 uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness); 565 if (D + TotalSize > BufferEnd) 566 return instrprof_error::too_large; 567 568 std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize); 569 memcpy(VPD.get(), D, TotalSize); 570 // Byte swap. 571 VPD->swapBytesToHost(Endianness); 572 573 instrprof_error EC = VPD->checkIntegrity(); 574 if (EC != instrprof_error::success) 575 return EC; 576 577 return std::move(VPD); 578 } 579 580 void ValueProfData::swapBytesToHost(support::endianness Endianness) { 581 using namespace support; 582 if (Endianness == getHostEndianness()) 583 return; 584 585 sys::swapByteOrder<uint32_t>(TotalSize); 586 sys::swapByteOrder<uint32_t>(NumValueKinds); 587 588 ValueProfRecord *VR = getFirstValueProfRecord(this); 589 for (uint32_t K = 0; K < NumValueKinds; K++) { 590 VR->swapBytes(Endianness, getHostEndianness()); 591 VR = getValueProfRecordNext(VR); 592 } 593 } 594 595 void ValueProfData::swapBytesFromHost(support::endianness Endianness) { 596 using namespace support; 597 if (Endianness == getHostEndianness()) 598 return; 599 600 ValueProfRecord *VR = getFirstValueProfRecord(this); 601 for (uint32_t K = 0; K < NumValueKinds; K++) { 602 ValueProfRecord *NVR = getValueProfRecordNext(VR); 603 VR->swapBytes(getHostEndianness(), Endianness); 604 VR = NVR; 605 } 606 sys::swapByteOrder<uint32_t>(TotalSize); 607 sys::swapByteOrder<uint32_t>(NumValueKinds); 608 } 609 610 // The argument to this method is a vector of cutoff percentages and the return 611 // value is a vector of (Cutoff, MinBlockCount, NumBlocks) triplets. 612 void ProfileSummary::computeDetailedSummary() { 613 if (DetailedSummaryCutoffs.empty()) 614 return; 615 auto Iter = CountFrequencies.begin(); 616 auto End = CountFrequencies.end(); 617 std::sort(DetailedSummaryCutoffs.begin(), DetailedSummaryCutoffs.end()); 618 619 uint32_t BlocksSeen = 0; 620 uint64_t CurrSum = 0, Count; 621 622 for (uint32_t Cutoff : DetailedSummaryCutoffs) { 623 assert(Cutoff <= 999999); 624 APInt Temp(128, TotalCount); 625 APInt N(128, Cutoff); 626 APInt D(128, ProfileSummary::Scale); 627 Temp *= N; 628 Temp = Temp.sdiv(D); 629 uint64_t DesiredCount = Temp.getZExtValue(); 630 assert(DesiredCount <= TotalCount); 631 while (CurrSum < DesiredCount && Iter != End) { 632 Count = Iter->first; 633 uint32_t Freq = Iter->second; 634 CurrSum += (Count * Freq); 635 BlocksSeen += Freq; 636 Iter++; 637 } 638 assert(CurrSum >= DesiredCount); 639 ProfileSummaryEntry PSE = {Cutoff, Count, BlocksSeen}; 640 DetailedSummary.push_back(PSE); 641 } 642 } 643 644 } // end namespace llvm 645