xref: /openbsd-src/gnu/llvm/compiler-rt/lib/profile/InstrProfiling.c (revision 810390e339a5425391477d5d41c78d7cab2424ac)
13cab2bb3Spatrick /*===- InstrProfiling.c - Support library for PGO instrumentation ---------===*\
23cab2bb3Spatrick |*
33cab2bb3Spatrick |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick |* See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick |*
73cab2bb3Spatrick \*===----------------------------------------------------------------------===*/
83cab2bb3Spatrick 
9d89ec533Spatrick // Note: This is linked into the Darwin kernel, and must remain compatible
10d89ec533Spatrick // with freestanding compilation. See `darwin_add_builtin_libraries`.
11d89ec533Spatrick 
123cab2bb3Spatrick #include <limits.h>
133cab2bb3Spatrick #include <stdio.h>
143cab2bb3Spatrick #include <stdlib.h>
153cab2bb3Spatrick #include <string.h>
163cab2bb3Spatrick 
173cab2bb3Spatrick #include "InstrProfiling.h"
183cab2bb3Spatrick #include "InstrProfilingInternal.h"
193cab2bb3Spatrick 
203cab2bb3Spatrick #define INSTR_PROF_VALUE_PROF_DATA
213cab2bb3Spatrick #include "profile/InstrProfData.inc"
223cab2bb3Spatrick 
__llvm_profile_get_magic(void)233cab2bb3Spatrick COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_magic(void) {
243cab2bb3Spatrick   return sizeof(void *) == sizeof(uint64_t) ? (INSTR_PROF_RAW_MAGIC_64)
253cab2bb3Spatrick                                             : (INSTR_PROF_RAW_MAGIC_32);
263cab2bb3Spatrick }
273cab2bb3Spatrick 
__llvm_profile_set_dumped(void)28*810390e3Srobert COMPILER_RT_VISIBILITY void __llvm_profile_set_dumped(void) {
291f9cb04fSpatrick   lprofSetProfileDumped(1);
303cab2bb3Spatrick }
313cab2bb3Spatrick 
323cab2bb3Spatrick /* Return the number of bytes needed to add to SizeInBytes to make it
333cab2bb3Spatrick  *   the result a multiple of 8.
343cab2bb3Spatrick  */
353cab2bb3Spatrick COMPILER_RT_VISIBILITY uint8_t
__llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes)363cab2bb3Spatrick __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes) {
373cab2bb3Spatrick   return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t));
383cab2bb3Spatrick }
393cab2bb3Spatrick 
__llvm_profile_get_version(void)403cab2bb3Spatrick COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_version(void) {
41*810390e3Srobert   return INSTR_PROF_RAW_VERSION_VAR;
423cab2bb3Spatrick }
433cab2bb3Spatrick 
__llvm_profile_reset_counters(void)443cab2bb3Spatrick COMPILER_RT_VISIBILITY void __llvm_profile_reset_counters(void) {
45*810390e3Srobert   char *I = __llvm_profile_begin_counters();
46*810390e3Srobert   char *E = __llvm_profile_end_counters();
473cab2bb3Spatrick 
48*810390e3Srobert   char ResetValue =
49*810390e3Srobert       (__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE) ? 0xFF : 0;
50*810390e3Srobert   memset(I, ResetValue, E - I);
513cab2bb3Spatrick 
523cab2bb3Spatrick   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
533cab2bb3Spatrick   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
543cab2bb3Spatrick   const __llvm_profile_data *DI;
553cab2bb3Spatrick   for (DI = DataBegin; DI < DataEnd; ++DI) {
563cab2bb3Spatrick     uint64_t CurrentVSiteCount = 0;
573cab2bb3Spatrick     uint32_t VKI, i;
583cab2bb3Spatrick     if (!DI->Values)
593cab2bb3Spatrick       continue;
603cab2bb3Spatrick 
613cab2bb3Spatrick     ValueProfNode **ValueCounters = (ValueProfNode **)DI->Values;
623cab2bb3Spatrick 
633cab2bb3Spatrick     for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)
643cab2bb3Spatrick       CurrentVSiteCount += DI->NumValueSites[VKI];
653cab2bb3Spatrick 
663cab2bb3Spatrick     for (i = 0; i < CurrentVSiteCount; ++i) {
67*810390e3Srobert       ValueProfNode *CurrVNode = ValueCounters[i];
683cab2bb3Spatrick 
69*810390e3Srobert       while (CurrVNode) {
70*810390e3Srobert         CurrVNode->Count = 0;
71*810390e3Srobert         CurrVNode = CurrVNode->Next;
723cab2bb3Spatrick       }
733cab2bb3Spatrick     }
743cab2bb3Spatrick   }
751f9cb04fSpatrick   lprofSetProfileDumped(0);
763cab2bb3Spatrick }
77