1 /*===- InstrProfilingPort.h- Support library for PGO instrumentation ------===*\ 2 |* 3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 |* See https://llvm.org/LICENSE.txt for license information. 5 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 |* 7 \*===----------------------------------------------------------------------===*/ 8 9 /* This header must be included after all others so it can provide fallback 10 definitions for stuff missing in system headers. */ 11 12 #ifndef PROFILE_INSTRPROFILING_PORT_H_ 13 #define PROFILE_INSTRPROFILING_PORT_H_ 14 15 #ifdef _MSC_VER 16 #define COMPILER_RT_ALIGNAS(x) __declspec(align(x)) 17 #define COMPILER_RT_VISIBILITY 18 /* FIXME: selectany does not have the same semantics as weak. */ 19 #define COMPILER_RT_WEAK __declspec(selectany) 20 /* Need to include <windows.h> */ 21 #define COMPILER_RT_ALLOCA _alloca 22 /* Need to include <stdio.h> and <io.h> */ 23 #define COMPILER_RT_FTRUNCATE(f,l) _chsize(_fileno(f),l) 24 #define COMPILER_RT_ALWAYS_INLINE __forceinline 25 #elif __GNUC__ 26 #define COMPILER_RT_ALIGNAS(x) __attribute__((aligned(x))) 27 #define COMPILER_RT_VISIBILITY __attribute__((visibility("hidden"))) 28 #define COMPILER_RT_WEAK __attribute__((weak)) 29 #define COMPILER_RT_ALLOCA __builtin_alloca 30 #define COMPILER_RT_FTRUNCATE(f,l) ftruncate(fileno(f),l) 31 #define COMPILER_RT_ALWAYS_INLINE inline __attribute((always_inline)) 32 #endif 33 34 #if defined(__APPLE__) 35 #define COMPILER_RT_SEG "__DATA," 36 #else 37 #define COMPILER_RT_SEG "" 38 #endif 39 40 #ifdef _MSC_VER 41 #define COMPILER_RT_SECTION(Sect) __declspec(allocate(Sect)) 42 #else 43 #define COMPILER_RT_SECTION(Sect) __attribute__((section(Sect))) 44 #endif 45 46 #define COMPILER_RT_MAX_HOSTLEN 128 47 #ifdef __ORBIS__ 48 #define COMPILER_RT_GETHOSTNAME(Name, Len) ((void)(Name), (void)(Len), (-1)) 49 #else 50 #define COMPILER_RT_GETHOSTNAME(Name, Len) lprofGetHostName(Name, Len) 51 #endif 52 53 #if COMPILER_RT_HAS_ATOMICS == 1 54 #ifdef _MSC_VER 55 #include <windows.h> 56 #if _MSC_VER < 1900 57 #define snprintf _snprintf 58 #endif 59 #if defined(_WIN64) 60 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \ 61 (InterlockedCompareExchange64((LONGLONG volatile *)Ptr, (LONGLONG)NewV, \ 62 (LONGLONG)OldV) == (LONGLONG)OldV) 63 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \ 64 (DomType *)InterlockedExchangeAdd64((LONGLONG volatile *)&PtrVar, \ 65 (LONGLONG)sizeof(DomType) * PtrIncr) 66 #else /* !defined(_WIN64) */ 67 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \ 68 (InterlockedCompareExchange((LONG volatile *)Ptr, (LONG)NewV, (LONG)OldV) == \ 69 (LONG)OldV) 70 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \ 71 (DomType *)InterlockedExchangeAdd((LONG volatile *)&PtrVar, \ 72 (LONG)sizeof(DomType) * PtrIncr) 73 #endif 74 #else /* !defined(_MSC_VER) */ 75 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \ 76 __sync_bool_compare_and_swap(Ptr, OldV, NewV) 77 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \ 78 (DomType *)__sync_fetch_and_add((long *)&PtrVar, sizeof(DomType) * PtrIncr) 79 #endif 80 #else /* COMPILER_RT_HAS_ATOMICS != 1 */ 81 #include "InstrProfilingUtil.h" 82 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \ 83 lprofBoolCmpXchg((void **)Ptr, OldV, NewV) 84 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \ 85 (DomType *)lprofPtrFetchAdd((void **)&PtrVar, sizeof(DomType) * PtrIncr) 86 #endif 87 88 #if defined(_WIN32) 89 #define DIR_SEPARATOR '\\' 90 #define DIR_SEPARATOR_2 '/' 91 #else 92 #define DIR_SEPARATOR '/' 93 #endif 94 95 #ifndef DIR_SEPARATOR_2 96 #define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR) 97 #else /* DIR_SEPARATOR_2 */ 98 #define IS_DIR_SEPARATOR(ch) \ 99 (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2)) 100 #endif /* DIR_SEPARATOR_2 */ 101 102 #define PROF_ERR(Format, ...) \ 103 fprintf(stderr, "LLVM Profile Error: " Format, __VA_ARGS__); 104 105 #define PROF_WARN(Format, ...) \ 106 fprintf(stderr, "LLVM Profile Warning: " Format, __VA_ARGS__); 107 108 #define PROF_NOTE(Format, ...) \ 109 fprintf(stderr, "LLVM Profile Note: " Format, __VA_ARGS__); 110 111 #ifndef MAP_FILE 112 #define MAP_FILE 0 113 #endif 114 115 #ifndef O_BINARY 116 #define O_BINARY 0 117 #endif 118 119 #if defined(__FreeBSD__) 120 121 #include <inttypes.h> 122 #include <sys/types.h> 123 124 #else /* defined(__FreeBSD__) */ 125 126 #include <inttypes.h> 127 #include <stdint.h> 128 129 #endif /* defined(__FreeBSD__) && defined(__i386__) */ 130 131 #endif /* PROFILE_INSTRPROFILING_PORT_H_ */ 132