xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfiling.c (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric /*===- InstrProfiling.c - Support library for PGO instrumentation ---------===*\
20b57cec5SDimitry Andric |*
30b57cec5SDimitry Andric |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric |* See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric |*
70b57cec5SDimitry Andric \*===----------------------------------------------------------------------===*/
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include <limits.h>
100b57cec5SDimitry Andric #include <stdio.h>
110b57cec5SDimitry Andric #include <stdlib.h>
120b57cec5SDimitry Andric #include <string.h>
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "InstrProfiling.h"
150b57cec5SDimitry Andric #include "InstrProfilingInternal.h"
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #define INSTR_PROF_VALUE_PROF_DATA
18480093f4SDimitry Andric #include "profile/InstrProfData.inc"
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric COMPILER_RT_WEAK uint64_t INSTR_PROF_RAW_VERSION_VAR = INSTR_PROF_RAW_VERSION;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_magic(void) {
240b57cec5SDimitry Andric   return sizeof(void *) == sizeof(uint64_t) ? (INSTR_PROF_RAW_MAGIC_64)
250b57cec5SDimitry Andric                                             : (INSTR_PROF_RAW_MAGIC_32);
260b57cec5SDimitry Andric }
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric COMPILER_RT_VISIBILITY void __llvm_profile_set_dumped() {
29*5ffd83dbSDimitry Andric   lprofSetProfileDumped(1);
300b57cec5SDimitry Andric }
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric /* Return the number of bytes needed to add to SizeInBytes to make it
330b57cec5SDimitry Andric  *   the result a multiple of 8.
340b57cec5SDimitry Andric  */
350b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint8_t
360b57cec5SDimitry Andric __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes) {
370b57cec5SDimitry Andric   return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t));
380b57cec5SDimitry Andric }
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_version(void) {
410b57cec5SDimitry Andric   return __llvm_profile_raw_version;
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric COMPILER_RT_VISIBILITY void __llvm_profile_reset_counters(void) {
450b57cec5SDimitry Andric   uint64_t *I = __llvm_profile_begin_counters();
460b57cec5SDimitry Andric   uint64_t *E = __llvm_profile_end_counters();
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   memset(I, 0, sizeof(uint64_t) * (E - I));
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
510b57cec5SDimitry Andric   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
520b57cec5SDimitry Andric   const __llvm_profile_data *DI;
530b57cec5SDimitry Andric   for (DI = DataBegin; DI < DataEnd; ++DI) {
540b57cec5SDimitry Andric     uint64_t CurrentVSiteCount = 0;
550b57cec5SDimitry Andric     uint32_t VKI, i;
560b57cec5SDimitry Andric     if (!DI->Values)
570b57cec5SDimitry Andric       continue;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric     ValueProfNode **ValueCounters = (ValueProfNode **)DI->Values;
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric     for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)
620b57cec5SDimitry Andric       CurrentVSiteCount += DI->NumValueSites[VKI];
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric     for (i = 0; i < CurrentVSiteCount; ++i) {
650b57cec5SDimitry Andric       ValueProfNode *CurrentVNode = ValueCounters[i];
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric       while (CurrentVNode) {
680b57cec5SDimitry Andric         CurrentVNode->Count = 0;
690b57cec5SDimitry Andric         CurrentVNode = CurrentVNode->Next;
700b57cec5SDimitry Andric       }
710b57cec5SDimitry Andric     }
720b57cec5SDimitry Andric   }
73*5ffd83dbSDimitry Andric   lprofSetProfileDumped(0);
740b57cec5SDimitry Andric }
75