Compiler.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines several macros, based on the current compiler. This allows
  11. // use of compiler-specific features in a way that remains portable.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_SUPPORT_COMPILER_H
  15. #define LLVM_SUPPORT_COMPILER_H
  16. #include "llvm-config.h"
  17. #ifndef __has_feature
  18. # define __has_feature(x) 0
  19. #endif
  20. #ifndef __has_extension
  21. # define __has_extension(x) 0
  22. #endif
  23. #ifndef __has_attribute
  24. # define __has_attribute(x) 0
  25. #endif
  26. #ifndef __has_builtin
  27. # define __has_builtin(x) 0
  28. #endif
  29. /// \macro LLVM_GNUC_PREREQ
  30. /// \brief Extend the default __GNUC_PREREQ even if glibc's features.h isn't
  31. /// available.
  32. #ifndef LLVM_GNUC_PREREQ
  33. # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
  34. # define LLVM_GNUC_PREREQ(maj, min, patch) \
  35. ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
  36. ((maj) << 20) + ((min) << 10) + (patch))
  37. # elif defined(__GNUC__) && defined(__GNUC_MINOR__)
  38. # define LLVM_GNUC_PREREQ(maj, min, patch) \
  39. ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
  40. # else
  41. # define LLVM_GNUC_PREREQ(maj, min, patch) 0
  42. # endif
  43. #endif
  44. /// \macro LLVM_MSC_PREREQ
  45. /// \brief Is the compiler MSVC of at least the specified version?
  46. /// The common \param version values to check for are:
  47. /// * 1800: Microsoft Visual Studio 2013 / 12.0
  48. /// * 1900: Microsoft Visual Studio 2015 / 14.0
  49. #ifdef _MSC_VER
  50. #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
  51. // We require at least MSVC 2013.
  52. #if !LLVM_MSC_PREREQ(1800)
  53. #error LLVM requires at least MSVC 2013.
  54. #endif
  55. #else
  56. #define LLVM_MSC_PREREQ(version) 0
  57. #endif
  58. #if !defined(_MSC_VER) || defined(__clang__) || LLVM_MSC_PREREQ(1900)
  59. #define LLVM_NOEXCEPT noexcept
  60. #else
  61. #define LLVM_NOEXCEPT throw()
  62. #endif
  63. /// \brief Does the compiler support ref-qualifiers for *this?
  64. ///
  65. /// Sadly, this is separate from just rvalue reference support because GCC
  66. /// and MSVC implemented this later than everything else.
  67. #if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
  68. #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
  69. #else
  70. #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
  71. #endif
  72. /// Expands to '&' if ref-qualifiers for *this are supported.
  73. ///
  74. /// This can be used to provide lvalue/rvalue overrides of member functions.
  75. /// The rvalue override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
  76. #if LLVM_HAS_RVALUE_REFERENCE_THIS
  77. #define LLVM_LVALUE_FUNCTION &
  78. #else
  79. #define LLVM_LVALUE_FUNCTION
  80. #endif
  81. #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
  82. # define LLVM_CONSTEXPR constexpr
  83. #else
  84. # define LLVM_CONSTEXPR
  85. #endif
  86. /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
  87. /// into a shared library, then the class should be private to the library and
  88. /// not accessible from outside it. Can also be used to mark variables and
  89. /// functions, making them private to any shared library they are linked into.
  90. /// On PE/COFF targets, library visibility is the default, so this isn't needed.
  91. #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
  92. !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
  93. #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
  94. #else
  95. #define LLVM_LIBRARY_VISIBILITY
  96. #endif
  97. #if __has_attribute(sentinel) || LLVM_GNUC_PREREQ(3, 0, 0)
  98. #define LLVM_END_WITH_NULL __attribute__((sentinel))
  99. #else
  100. #define LLVM_END_WITH_NULL
  101. #endif
  102. #if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
  103. #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
  104. #else
  105. #define LLVM_ATTRIBUTE_USED
  106. #endif
  107. #if __has_attribute(warn_unused_result) || LLVM_GNUC_PREREQ(3, 4, 0)
  108. #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
  109. #else
  110. #define LLVM_ATTRIBUTE_UNUSED_RESULT
  111. #endif
  112. // Some compilers warn about unused functions. When a function is sometimes
  113. // used or not depending on build settings (e.g. a function only called from
  114. // within "assert"), this attribute can be used to suppress such warnings.
  115. //
  116. // However, it shouldn't be used for unused *variables*, as those have a much
  117. // more portable solution:
  118. // (void)unused_var_name;
  119. // Prefer cast-to-void wherever it is sufficient.
  120. #if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
  121. #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
  122. #else
  123. #define LLVM_ATTRIBUTE_UNUSED
  124. #endif
  125. // FIXME: Provide this for PE/COFF targets.
  126. #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
  127. (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
  128. #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
  129. #else
  130. #define LLVM_ATTRIBUTE_WEAK
  131. #endif
  132. // Prior to clang 3.2, clang did not accept any spelling of
  133. // __has_attribute(const), so assume it is supported.
  134. #if defined(__clang__) || defined(__GNUC__)
  135. // aka 'CONST' but following LLVM Conventions.
  136. #define LLVM_READNONE __attribute__((__const__))
  137. #else
  138. #define LLVM_READNONE
  139. #endif
  140. #if __has_attribute(pure) || defined(__GNUC__)
  141. // aka 'PURE' but following LLVM Conventions.
  142. #define LLVM_READONLY __attribute__((__pure__))
  143. #else
  144. #define LLVM_READONLY
  145. #endif
  146. #if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
  147. #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
  148. #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
  149. #else
  150. #define LLVM_LIKELY(EXPR) (EXPR)
  151. #define LLVM_UNLIKELY(EXPR) (EXPR)
  152. #endif
  153. /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
  154. /// mark a method "not for inlining".
  155. #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
  156. #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
  157. #elif defined(_MSC_VER)
  158. #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
  159. #else
  160. #define LLVM_ATTRIBUTE_NOINLINE
  161. #endif
  162. /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
  163. /// so, mark a method "always inline" because it is performance sensitive. GCC
  164. /// 3.4 supported this but is buggy in various cases and produces unimplemented
  165. /// errors, just use it in GCC 4.0 and later.
  166. #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
  167. #define LLVM_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
  168. #elif defined(_MSC_VER)
  169. #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
  170. #else
  171. #define LLVM_ATTRIBUTE_ALWAYS_INLINE
  172. #endif
  173. #ifdef __GNUC__
  174. #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
  175. #elif defined(_MSC_VER)
  176. #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
  177. #else
  178. #define LLVM_ATTRIBUTE_NORETURN
  179. #endif
  180. #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
  181. #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
  182. #else
  183. #define LLVM_ATTRIBUTE_RETURNS_NONNULL
  184. #endif
  185. /// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
  186. /// pointer that does not alias any other valid pointer.
  187. #ifdef __GNUC__
  188. #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
  189. #elif defined(_MSC_VER)
  190. #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
  191. #else
  192. #define LLVM_ATTRIBUTE_RETURNS_NOALIAS
  193. #endif
  194. /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
  195. /// pedantic diagnostics.
  196. #ifdef __GNUC__
  197. #define LLVM_EXTENSION __extension__
  198. #else
  199. #define LLVM_EXTENSION
  200. #endif
  201. // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
  202. #if __has_feature(attribute_deprecated_with_message)
  203. # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
  204. decl __attribute__((deprecated(message)))
  205. #elif defined(__GNUC__)
  206. # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
  207. decl __attribute__((deprecated))
  208. #elif defined(_MSC_VER)
  209. # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
  210. __declspec(deprecated(message)) decl
  211. #else
  212. # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
  213. decl
  214. #endif
  215. /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
  216. /// to an expression which states that it is undefined behavior for the
  217. /// compiler to reach this point. Otherwise is not defined.
  218. #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
  219. # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
  220. #elif defined(_MSC_VER)
  221. # define LLVM_BUILTIN_UNREACHABLE __assume(false)
  222. #endif
  223. /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
  224. /// which causes the program to exit abnormally.
  225. #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
  226. # define LLVM_BUILTIN_TRAP __builtin_trap()
  227. #elif defined(_MSC_VER)
  228. // The __debugbreak intrinsic is supported by MSVC, does not require forward
  229. // declarations involving platform-specific typedefs (unlike RaiseException),
  230. // results in a call to vectored exception handlers, and encodes to a short
  231. // instruction that still causes the trapping behavior we want.
  232. # define LLVM_BUILTIN_TRAP __debugbreak()
  233. #else
  234. # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
  235. #endif
  236. /// \macro LLVM_ASSUME_ALIGNED
  237. /// \brief Returns a pointer with an assumed alignment.
  238. #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
  239. # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
  240. #elif defined(LLVM_BUILTIN_UNREACHABLE)
  241. // As of today, clang does not support __builtin_assume_aligned.
  242. # define LLVM_ASSUME_ALIGNED(p, a) \
  243. (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
  244. #else
  245. # define LLVM_ASSUME_ALIGNED(p, a) (p)
  246. #endif
  247. /// \macro LLVM_ALIGNAS
  248. /// \brief Used to specify a minimum alignment for a structure or variable. The
  249. /// alignment must be a constant integer. Use LLVM_PTR_SIZE to compute
  250. /// alignments in terms of the size of a pointer.
  251. ///
  252. /// Note that __declspec(align) has special quirks, it's not legal to pass a
  253. /// structure with __declspec(align) as a formal parameter.
  254. #ifdef _MSC_VER
  255. # define LLVM_ALIGNAS(x) __declspec(align(x))
  256. #elif __GNUC__ && !__has_feature(cxx_alignas) && !LLVM_GNUC_PREREQ(4, 8, 0)
  257. # define LLVM_ALIGNAS(x) __attribute__((aligned(x)))
  258. #else
  259. # define LLVM_ALIGNAS(x) alignas(x)
  260. #endif
  261. /// \macro LLVM_PACKED
  262. /// \brief Used to specify a packed structure.
  263. /// LLVM_PACKED(
  264. /// struct A {
  265. /// int i;
  266. /// int j;
  267. /// int k;
  268. /// long long l;
  269. /// });
  270. ///
  271. /// LLVM_PACKED_START
  272. /// struct B {
  273. /// int i;
  274. /// int j;
  275. /// int k;
  276. /// long long l;
  277. /// };
  278. /// LLVM_PACKED_END
  279. #ifdef _MSC_VER
  280. # define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
  281. # define LLVM_PACKED_START __pragma(pack(push, 1))
  282. # define LLVM_PACKED_END __pragma(pack(pop))
  283. #else
  284. # define LLVM_PACKED(d) d __attribute__((packed))
  285. # define LLVM_PACKED_START _Pragma("pack(push, 1)")
  286. # define LLVM_PACKED_END _Pragma("pack(pop)")
  287. #endif
  288. /// \macro LLVM_PTR_SIZE
  289. /// \brief A constant integer equivalent to the value of sizeof(void*).
  290. /// Generally used in combination with LLVM_ALIGNAS or when doing computation in
  291. /// the preprocessor.
  292. #ifdef __SIZEOF_POINTER__
  293. # define LLVM_PTR_SIZE __SIZEOF_POINTER__
  294. #elif defined(_WIN64)
  295. # define LLVM_PTR_SIZE 8
  296. #elif defined(_WIN32)
  297. # define LLVM_PTR_SIZE 4
  298. #elif defined(_MSC_VER)
  299. # error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
  300. #else
  301. # define LLVM_PTR_SIZE sizeof(void *)
  302. #endif
  303. /// \macro LLVM_FUNCTION_NAME
  304. /// \brief Expands to __func__ on compilers which support it. Otherwise,
  305. /// expands to a compiler-dependent replacement.
  306. #if defined(_MSC_VER)
  307. # define LLVM_FUNCTION_NAME __FUNCTION__
  308. #else
  309. # define LLVM_FUNCTION_NAME __func__
  310. #endif
  311. /// \macro LLVM_MEMORY_SANITIZER_BUILD
  312. /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
  313. #if __has_feature(memory_sanitizer)
  314. # define LLVM_MEMORY_SANITIZER_BUILD 1
  315. # include <sanitizer/msan_interface.h>
  316. #else
  317. # define LLVM_MEMORY_SANITIZER_BUILD 0
  318. # define __msan_allocated_memory(p, size)
  319. # define __msan_unpoison(p, size)
  320. #endif
  321. /// \macro LLVM_ADDRESS_SANITIZER_BUILD
  322. /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
  323. #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
  324. # define LLVM_ADDRESS_SANITIZER_BUILD 1
  325. # include <sanitizer/asan_interface.h>
  326. #else
  327. # define LLVM_ADDRESS_SANITIZER_BUILD 0
  328. # define __asan_poison_memory_region(p, size)
  329. # define __asan_unpoison_memory_region(p, size)
  330. #endif
  331. /// \macro LLVM_THREAD_SANITIZER_BUILD
  332. /// \brief Whether LLVM itself is built with ThreadSanitizer instrumentation.
  333. #if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
  334. # define LLVM_THREAD_SANITIZER_BUILD 1
  335. #else
  336. # define LLVM_THREAD_SANITIZER_BUILD 0
  337. #endif
  338. #if LLVM_THREAD_SANITIZER_BUILD
  339. // Thread Sanitizer is a tool that finds races in code.
  340. // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
  341. // tsan detects these exact functions by name.
  342. extern "C" {
  343. void AnnotateHappensAfter(const char *file, int line, const volatile void *cv);
  344. void AnnotateHappensBefore(const char *file, int line, const volatile void *cv);
  345. void AnnotateIgnoreWritesBegin(const char *file, int line);
  346. void AnnotateIgnoreWritesEnd(const char *file, int line);
  347. }
  348. // This marker is used to define a happens-before arc. The race detector will
  349. // infer an arc from the begin to the end when they share the same pointer
  350. // argument.
  351. # define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv)
  352. // This marker defines the destination of a happens-before arc.
  353. # define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv)
  354. // Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
  355. # define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
  356. // Resume checking for racy writes.
  357. # define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
  358. #else
  359. # define TsanHappensBefore(cv)
  360. # define TsanHappensAfter(cv)
  361. # define TsanIgnoreWritesBegin()
  362. # define TsanIgnoreWritesEnd()
  363. #endif
  364. /// \brief Mark debug helper function definitions like dump() that should not be
  365. /// stripped from debug builds.
  366. // FIXME: Move this to a private config.h as it's not usable in public headers.
  367. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  368. #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
  369. #else
  370. #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
  371. #endif
  372. /// \macro LLVM_THREAD_LOCAL
  373. /// \brief A thread-local storage specifier which can be used with globals,
  374. /// extern globals, and static globals.
  375. ///
  376. /// This is essentially an extremely restricted analog to C++11's thread_local
  377. /// support, and uses that when available. However, it falls back on
  378. /// platform-specific or vendor-provided extensions when necessary. These
  379. /// extensions don't support many of the C++11 thread_local's features. You
  380. /// should only use this for PODs that you can statically initialize to
  381. /// some constant value. In almost all circumstances this is most appropriate
  382. /// for use with a pointer, integer, or small aggregation of pointers and
  383. /// integers.
  384. #if LLVM_ENABLE_THREADS
  385. #if __has_feature(cxx_thread_local)
  386. #define LLVM_THREAD_LOCAL thread_local
  387. #elif defined(_MSC_VER)
  388. // MSVC supports this with a __declspec.
  389. #define LLVM_THREAD_LOCAL __declspec(thread)
  390. #else
  391. // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
  392. // we only need the restricted functionality that provides.
  393. #define LLVM_THREAD_LOCAL __thread
  394. #endif
  395. #else // !LLVM_ENABLE_THREADS
  396. // If threading is disabled entirely, this compiles to nothing and you get
  397. // a normal global variable.
  398. #define LLVM_THREAD_LOCAL
  399. #endif
  400. #endif