InstrProfilingPort.h 5.2 KB

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