xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformOther.c (revision fe6060f10f634930ff71b7c50291ddc610da2475)
10b57cec5SDimitry Andric /*===- InstrProfilingPlatformOther.c - Profile data default platform ------===*\
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 #if !defined(__APPLE__) && !defined(__linux__) && !defined(__FreeBSD__) &&     \
100b57cec5SDimitry Andric     !(defined(__sun__) && defined(__svr4__)) && !defined(__NetBSD__) &&        \
110b57cec5SDimitry Andric     !defined(_WIN32)
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include <stdlib.h>
140b57cec5SDimitry Andric #include <stdio.h>
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "InstrProfiling.h"
17*fe6060f1SDimitry Andric #include "InstrProfilingInternal.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric static const __llvm_profile_data *DataFirst = NULL;
200b57cec5SDimitry Andric static const __llvm_profile_data *DataLast = NULL;
210b57cec5SDimitry Andric static const char *NamesFirst = NULL;
220b57cec5SDimitry Andric static const char *NamesLast = NULL;
230b57cec5SDimitry Andric static uint64_t *CountersFirst = NULL;
240b57cec5SDimitry Andric static uint64_t *CountersLast = NULL;
250b57cec5SDimitry Andric static uint32_t *OrderFileFirst = NULL;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric static const void *getMinAddr(const void *A1, const void *A2) {
280b57cec5SDimitry Andric   return A1 < A2 ? A1 : A2;
290b57cec5SDimitry Andric }
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric static const void *getMaxAddr(const void *A1, const void *A2) {
320b57cec5SDimitry Andric   return A1 > A2 ? A1 : A2;
330b57cec5SDimitry Andric }
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric /*!
360b57cec5SDimitry Andric  * \brief Register an instrumented function.
370b57cec5SDimitry Andric  *
380b57cec5SDimitry Andric  * Calls to this are emitted by clang with -fprofile-instr-generate.  Such
390b57cec5SDimitry Andric  * calls are only required (and only emitted) on targets where we haven't
400b57cec5SDimitry Andric  * implemented linker magic to find the bounds of the sections.
410b57cec5SDimitry Andric  */
420b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
430b57cec5SDimitry Andric void __llvm_profile_register_function(void *Data_) {
440b57cec5SDimitry Andric   /* TODO: Only emit this function if we can't use linker magic. */
450b57cec5SDimitry Andric   const __llvm_profile_data *Data = (__llvm_profile_data *)Data_;
460b57cec5SDimitry Andric   if (!DataFirst) {
470b57cec5SDimitry Andric     DataFirst = Data;
480b57cec5SDimitry Andric     DataLast = Data + 1;
490b57cec5SDimitry Andric     CountersFirst = Data->CounterPtr;
500b57cec5SDimitry Andric     CountersLast = (uint64_t *)Data->CounterPtr + Data->NumCounters;
510b57cec5SDimitry Andric     return;
520b57cec5SDimitry Andric   }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   DataFirst = (const __llvm_profile_data *)getMinAddr(DataFirst, Data);
550b57cec5SDimitry Andric   CountersFirst = (uint64_t *)getMinAddr(CountersFirst, Data->CounterPtr);
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   DataLast = (const __llvm_profile_data *)getMaxAddr(DataLast, Data + 1);
580b57cec5SDimitry Andric   CountersLast = (uint64_t *)getMaxAddr(
590b57cec5SDimitry Andric       CountersLast, (uint64_t *)Data->CounterPtr + Data->NumCounters);
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
630b57cec5SDimitry Andric void __llvm_profile_register_names_function(void *NamesStart,
640b57cec5SDimitry Andric                                             uint64_t NamesSize) {
650b57cec5SDimitry Andric   if (!NamesFirst) {
660b57cec5SDimitry Andric     NamesFirst = (const char *)NamesStart;
670b57cec5SDimitry Andric     NamesLast = (const char *)NamesStart + NamesSize;
680b57cec5SDimitry Andric     return;
690b57cec5SDimitry Andric   }
700b57cec5SDimitry Andric   NamesFirst = (const char *)getMinAddr(NamesFirst, NamesStart);
710b57cec5SDimitry Andric   NamesLast =
720b57cec5SDimitry Andric       (const char *)getMaxAddr(NamesLast, (const char *)NamesStart + NamesSize);
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
760b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_begin_data(void) { return DataFirst; }
770b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
780b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_end_data(void) { return DataLast; }
790b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
800b57cec5SDimitry Andric const char *__llvm_profile_begin_names(void) { return NamesFirst; }
810b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
820b57cec5SDimitry Andric const char *__llvm_profile_end_names(void) { return NamesLast; }
830b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
840b57cec5SDimitry Andric uint64_t *__llvm_profile_begin_counters(void) { return CountersFirst; }
850b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
860b57cec5SDimitry Andric uint64_t *__llvm_profile_end_counters(void) { return CountersLast; }
870b57cec5SDimitry Andric /* TODO: correctly set up OrderFileFirst. */
880b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
890b57cec5SDimitry Andric uint32_t *__llvm_profile_begin_orderfile(void) { return OrderFileFirst; }
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
920b57cec5SDimitry Andric ValueProfNode *__llvm_profile_begin_vnodes(void) {
930b57cec5SDimitry Andric   return 0;
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
960b57cec5SDimitry Andric ValueProfNode *__llvm_profile_end_vnodes(void) { return 0; }
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = 0;
990b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = 0;
1000b57cec5SDimitry Andric 
101*fe6060f1SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
102*fe6060f1SDimitry Andric   return 0;
103*fe6060f1SDimitry Andric }
104*fe6060f1SDimitry Andric 
1050b57cec5SDimitry Andric #endif
106