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