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 9e8d8bef9SDimitry Andric // Note: This is linked into the Darwin kernel, and must remain compatible 10e8d8bef9SDimitry Andric // with freestanding compilation. See `darwin_add_builtin_libraries`. 11e8d8bef9SDimitry Andric 120b57cec5SDimitry Andric #include <limits.h> 130b57cec5SDimitry Andric #include <stdio.h> 140b57cec5SDimitry Andric #include <stdlib.h> 150b57cec5SDimitry Andric #include <string.h> 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include "InstrProfiling.h" 180b57cec5SDimitry Andric #include "InstrProfilingInternal.h" 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric #define INSTR_PROF_VALUE_PROF_DATA 21480093f4SDimitry Andric #include "profile/InstrProfData.inc" 220b57cec5SDimitry Andric 2306c3fb27SDimitry Andric static uint32_t __llvm_profile_global_timestamp = 1; 2406c3fb27SDimitry Andric 2506c3fb27SDimitry Andric COMPILER_RT_VISIBILITY INSTR_PROF_PROFILE_SET_TIMESTAMP(uint64_t * Probe)2606c3fb27SDimitry Andricvoid INSTR_PROF_PROFILE_SET_TIMESTAMP(uint64_t *Probe) { 2706c3fb27SDimitry Andric if (*Probe == 0 || *Probe == (uint64_t)-1) 2806c3fb27SDimitry Andric *Probe = __llvm_profile_global_timestamp++; 2906c3fb27SDimitry Andric } 3006c3fb27SDimitry Andric __llvm_profile_get_magic(void)310b57cec5SDimitry AndricCOMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_magic(void) { 320b57cec5SDimitry Andric return sizeof(void *) == sizeof(uint64_t) ? (INSTR_PROF_RAW_MAGIC_64) 330b57cec5SDimitry Andric : (INSTR_PROF_RAW_MAGIC_32); 340b57cec5SDimitry Andric } 350b57cec5SDimitry Andric __llvm_profile_set_dumped(void)3681ad6265SDimitry AndricCOMPILER_RT_VISIBILITY void __llvm_profile_set_dumped(void) { 375ffd83dbSDimitry Andric lprofSetProfileDumped(1); 380b57cec5SDimitry Andric } 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric /* Return the number of bytes needed to add to SizeInBytes to make it 410b57cec5SDimitry Andric * the result a multiple of 8. 420b57cec5SDimitry Andric */ 430b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes)440b57cec5SDimitry Andric__llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes) { 450b57cec5SDimitry Andric return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t)); 460b57cec5SDimitry Andric } 470b57cec5SDimitry Andric __llvm_profile_get_version(void)480b57cec5SDimitry AndricCOMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_version(void) { 490eae32dcSDimitry Andric return INSTR_PROF_RAW_VERSION_VAR; 500b57cec5SDimitry Andric } 510b57cec5SDimitry Andric __llvm_profile_reset_counters(void)520b57cec5SDimitry AndricCOMPILER_RT_VISIBILITY void __llvm_profile_reset_counters(void) { 5306c3fb27SDimitry Andric if (__llvm_profile_get_version() & VARIANT_MASK_TEMPORAL_PROF) 5406c3fb27SDimitry Andric __llvm_profile_global_timestamp = 1; 5506c3fb27SDimitry Andric 5604eeddc0SDimitry Andric char *I = __llvm_profile_begin_counters(); 5704eeddc0SDimitry Andric char *E = __llvm_profile_end_counters(); 580b57cec5SDimitry Andric 591fd87a68SDimitry Andric char ResetValue = 601fd87a68SDimitry Andric (__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE) ? 0xFF : 0; 611fd87a68SDimitry Andric memset(I, ResetValue, E - I); 620b57cec5SDimitry Andric 63*5f757f3fSDimitry Andric I = __llvm_profile_begin_bitmap(); 64*5f757f3fSDimitry Andric E = __llvm_profile_end_bitmap(); 65*5f757f3fSDimitry Andric memset(I, 0x0, E - I); 66*5f757f3fSDimitry Andric 670b57cec5SDimitry Andric const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); 680b57cec5SDimitry Andric const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); 690b57cec5SDimitry Andric const __llvm_profile_data *DI; 700b57cec5SDimitry Andric for (DI = DataBegin; DI < DataEnd; ++DI) { 710b57cec5SDimitry Andric uint64_t CurrentVSiteCount = 0; 720b57cec5SDimitry Andric uint32_t VKI, i; 730b57cec5SDimitry Andric if (!DI->Values) 740b57cec5SDimitry Andric continue; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric ValueProfNode **ValueCounters = (ValueProfNode **)DI->Values; 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI) 790b57cec5SDimitry Andric CurrentVSiteCount += DI->NumValueSites[VKI]; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric for (i = 0; i < CurrentVSiteCount; ++i) { 82bdd1243dSDimitry Andric ValueProfNode *CurrVNode = ValueCounters[i]; 830b57cec5SDimitry Andric 84bdd1243dSDimitry Andric while (CurrVNode) { 85bdd1243dSDimitry Andric CurrVNode->Count = 0; 86bdd1243dSDimitry Andric CurrVNode = CurrVNode->Next; 870b57cec5SDimitry Andric } 880b57cec5SDimitry Andric } 890b57cec5SDimitry Andric } 905ffd83dbSDimitry Andric lprofSetProfileDumped(0); 910b57cec5SDimitry Andric } 92