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