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