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
9// Note: This is linked into the Darwin kernel, and must remain compatible
10// with freestanding compilation. See `darwin_add_builtin_libraries`.
11
12#include <limits.h>
13#include <string.h>
14
15#include "InstrProfiling.h"
16#include "InstrProfilingInternal.h"
17
18#define INSTR_PROF_VALUE_PROF_DATA
19#include "profile/InstrProfData.inc"
20
21static uint32_t __llvm_profile_global_timestamp = 1;
22
23COMPILER_RT_VISIBILITY
24void INSTR_PROF_PROFILE_SET_TIMESTAMP(uint64_t *Probe) {
25 if (*Probe == 0 || *Probe == (uint64_t)-1)
26 *Probe = __llvm_profile_global_timestamp++;
27}
28
29COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_magic(void) {
30 return sizeof(void *) == sizeof(uint64_t) ? (INSTR_PROF_RAW_MAGIC_64)
31 : (INSTR_PROF_RAW_MAGIC_32);
32}
33
34COMPILER_RT_VISIBILITY void __llvm_profile_set_dumped(void) {
35 lprofSetProfileDumped(1);
36}
37
38/* Return the number of bytes needed to add to SizeInBytes to make it
39 * the result a multiple of 8.
40 */
41COMPILER_RT_VISIBILITY uint8_t
42__llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes) {
43 return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t));
44}
45
46COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_version(void) {
47 return INSTR_PROF_RAW_VERSION_VAR;
48}
49
50COMPILER_RT_VISIBILITY void __llvm_profile_reset_counters(void) {
51 if (__llvm_profile_get_version() & VARIANT_MASK_TEMPORAL_PROF)
52 __llvm_profile_global_timestamp = 1;
53
54 char *I = __llvm_profile_begin_counters();
55 char *E = __llvm_profile_end_counters();
56
57 char ResetValue =
58 (__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE) ? 0xFF : 0;
59 memset(s: I, c: ResetValue, n: E - I);
60
61 I = __llvm_profile_begin_bitmap();
62 E = __llvm_profile_end_bitmap();
63 memset(s: I, c: 0x0, n: E - I);
64
65 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
66 const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
67 const __llvm_profile_data *DI;
68 for (DI = DataBegin; DI < DataEnd; ++DI) {
69 uint64_t CurrentVSiteCount = 0;
70 uint32_t VKI, i;
71 if (!DI->Values)
72 continue;
73
74 ValueProfNode **ValueCounters = (ValueProfNode **)DI->Values;
75
76 for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)
77 CurrentVSiteCount += DI->NumValueSites[VKI];
78
79 for (i = 0; i < CurrentVSiteCount; ++i) {
80 ValueProfNode *CurrVNode = ValueCounters[i];
81
82 while (CurrVNode) {
83 CurrVNode->Count = 0;
84 CurrVNode = CurrVNode->Next;
85 }
86 }
87 }
88 lprofSetProfileDumped(0);
89}
90