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