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