InstrProfiling.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*===- InstrProfiling.c - Support library for PGO instrumentation ---------===*\
  2. |*
  3. |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. |* See https://llvm.org/LICENSE.txt for license information.
  5. |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. |*
  7. \*===----------------------------------------------------------------------===*/
  8. // Note: This is linked into the Darwin kernel, and must remain compatible
  9. // with freestanding compilation. See `darwin_add_builtin_libraries`.
  10. #include <limits.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "InstrProfiling.h"
  15. #include "InstrProfilingInternal.h"
  16. #define INSTR_PROF_VALUE_PROF_DATA
  17. #include "profile/InstrProfData.inc"
  18. static uint32_t __llvm_profile_global_timestamp = 1;
  19. COMPILER_RT_VISIBILITY
  20. void INSTR_PROF_PROFILE_SET_TIMESTAMP(uint64_t *Probe) {
  21. if (*Probe == 0 || *Probe == (uint64_t)-1)
  22. *Probe = __llvm_profile_global_timestamp++;
  23. }
  24. COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_magic(void) {
  25. return sizeof(void *) == sizeof(uint64_t) ? (INSTR_PROF_RAW_MAGIC_64)
  26. : (INSTR_PROF_RAW_MAGIC_32);
  27. }
  28. COMPILER_RT_VISIBILITY void __llvm_profile_set_dumped(void) {
  29. lprofSetProfileDumped(1);
  30. }
  31. /* Return the number of bytes needed to add to SizeInBytes to make it
  32. * the result a multiple of 8.
  33. */
  34. COMPILER_RT_VISIBILITY uint8_t
  35. __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes) {
  36. return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t));
  37. }
  38. COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_version(void) {
  39. return INSTR_PROF_RAW_VERSION_VAR;
  40. }
  41. COMPILER_RT_VISIBILITY void __llvm_profile_reset_counters(void) {
  42. if (__llvm_profile_get_version() & VARIANT_MASK_TEMPORAL_PROF)
  43. __llvm_profile_global_timestamp = 1;
  44. char *I = __llvm_profile_begin_counters();
  45. char *E = __llvm_profile_end_counters();
  46. char ResetValue =
  47. (__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE) ? 0xFF : 0;
  48. memset(I, ResetValue, E - I);
  49. I = __llvm_profile_begin_bitmap();
  50. E = __llvm_profile_end_bitmap();
  51. memset(I, 0x0, E - I);
  52. const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
  53. const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
  54. const __llvm_profile_data *DI;
  55. for (DI = DataBegin; DI < DataEnd; ++DI) {
  56. uint64_t CurrentVSiteCount = 0;
  57. uint32_t VKI, i;
  58. if (!DI->Values)
  59. continue;
  60. ValueProfNode **ValueCounters = (ValueProfNode **)DI->Values;
  61. for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)
  62. CurrentVSiteCount += DI->NumValueSites[VKI];
  63. for (i = 0; i < CurrentVSiteCount; ++i) {
  64. ValueProfNode *CurrVNode = ValueCounters[i];
  65. while (CurrVNode) {
  66. CurrVNode->Count = 0;
  67. CurrVNode = CurrVNode->Next;
  68. }
  69. }
  70. }
  71. lprofSetProfileDumped(0);
  72. }