blob: 58778aeec16aa1462e490d4e6ab0800bdc0ed549 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001/*===- 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"
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080011#include "InstrProfilingInternal.h"
12#include <limits.h>
13#include <stdio.h>
14#include <stdlib.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -070015#include <string.h>
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080016#define INSTR_PROF_VALUE_PROF_DATA
17#include "InstrProfData.inc"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070018
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080019char *(*GetEnvHook)(const char *) = 0;
20
21COMPILER_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);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070024}
25
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080026/* Return the number of bytes needed to add to SizeInBytes to make it
27 * the result a multiple of 8.
28 */
29COMPILER_RT_VISIBILITY uint8_t
30__llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes) {
31 return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t));
Stephen Hines2d1fdb22014-05-28 23:58:16 -070032}
33
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080034COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_version(void) {
35 return INSTR_PROF_RAW_VERSION;
36}
37
38COMPILER_RT_VISIBILITY void __llvm_profile_reset_counters(void) {
Stephen Hines6d186232014-11-26 17:56:19 -080039 uint64_t *I = __llvm_profile_begin_counters();
40 uint64_t *E = __llvm_profile_end_counters();
Stephen Hines2d1fdb22014-05-28 23:58:16 -070041
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080042 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 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -070067}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080068