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