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 <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 #include "InstrProfiling.h"
18 #include "InstrProfilingInternal.h"
19 
20 #define INSTR_PROF_VALUE_PROF_DATA
21 #include "profile/InstrProfData.inc"
22 
__llvm_profile_get_magic(void)23 COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_magic(void) {
24   return sizeof(void *) == sizeof(uint64_t) ? (INSTR_PROF_RAW_MAGIC_64)
25                                             : (INSTR_PROF_RAW_MAGIC_32);
26 }
27 
__llvm_profile_set_dumped()28 COMPILER_RT_VISIBILITY void __llvm_profile_set_dumped() {
29   lprofSetProfileDumped(1);
30 }
31 
32 /* Return the number of bytes needed to add to SizeInBytes to make it
33  *   the result a multiple of 8.
34  */
35 COMPILER_RT_VISIBILITY uint8_t
__llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes)36 __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes) {
37   return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t));
38 }
39 
__llvm_profile_get_version(void)40 COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_version(void) {
41   return __llvm_profile_raw_version;
42 }
43 
__llvm_profile_reset_counters(void)44 COMPILER_RT_VISIBILITY void __llvm_profile_reset_counters(void) {
45   uint64_t *I = __llvm_profile_begin_counters();
46   uint64_t *E = __llvm_profile_end_counters();
47 
48   memset(I, 0, sizeof(uint64_t) * (E - I));
49 
50   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
51   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
52   const __llvm_profile_data *DI;
53   for (DI = DataBegin; DI < DataEnd; ++DI) {
54     uint64_t CurrentVSiteCount = 0;
55     uint32_t VKI, i;
56     if (!DI->Values)
57       continue;
58 
59     ValueProfNode **ValueCounters = (ValueProfNode **)DI->Values;
60 
61     for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)
62       CurrentVSiteCount += DI->NumValueSites[VKI];
63 
64     for (i = 0; i < CurrentVSiteCount; ++i) {
65       ValueProfNode *CurrentVNode = ValueCounters[i];
66 
67       while (CurrentVNode) {
68         CurrentVNode->Count = 0;
69         CurrentVNode = CurrentVNode->Next;
70       }
71     }
72   }
73   lprofSetProfileDumped(0);
74 }
75