9d3b9106f6879d726b3edd05567b1cdcd84c0bf2
[oota-llvm.git] / include / llvm / Support / Compiler.h
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
15 #ifndef LLVM_SUPPORT_COMPILER_H
16 #define LLVM_SUPPORT_COMPILER_H
17
18 #include "llvm/Config/llvm-config.h"
19
20 #ifndef __has_feature
21 # define __has_feature(x) 0
22 #endif
23
24 #ifndef __has_extension
25 # define __has_extension(x) 0
26 #endif
27
28 #ifndef __has_attribute
29 # define __has_attribute(x) 0
30 #endif
31
32 #ifndef __has_builtin
33 # define __has_builtin(x) 0
34 #endif
35
36 /// \macro LLVM_GNUC_PREREQ
37 /// \brief Extend the default __GNUC_PREREQ even if glibc's features.h isn't
38 /// available.
39 #ifndef LLVM_GNUC_PREREQ
40 # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
41 #  define LLVM_GNUC_PREREQ(maj, min, patch) \
42     ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
43      ((maj) << 20) + ((min) << 10) + (patch))
44 # elif defined(__GNUC__) && defined(__GNUC_MINOR__)
45 #  define LLVM_GNUC_PREREQ(maj, min, patch) \
46     ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
47 # else
48 #  define LLVM_GNUC_PREREQ(maj, min, patch) 0
49 # endif
50 #endif
51
52 /// \macro LLVM_MSC_PREREQ
53 /// \brief Is the compiler MSVC of at least the specified version?
54 /// The common \param version values to check for are:
55 ///  * 1800: Microsoft Visual Studio 2013 / 12.0
56 ///  * 1900: Microsoft Visual Studio 2015 / 14.0
57 #ifdef _MSC_VER
58 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
59
60 // We require at least MSVC 2013.
61 #if !LLVM_MSC_PREREQ(1800)
62 #error LLVM requires at least MSVC 2013.
63 #endif
64
65 #else
66 #define LLVM_MSC_PREREQ(version) 0
67 #endif
68
69 #if !defined(_MSC_VER) || defined(__clang__) || LLVM_MSC_PREREQ(1900)
70 #define LLVM_NOEXCEPT noexcept
71 #else
72 #define LLVM_NOEXCEPT
73 #endif
74
75 /// \brief Does the compiler support r-value reference *this?
76 ///
77 /// Sadly, this is separate from just r-value reference support because GCC
78 /// implemented this later than everything else.
79 #if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
80 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
81 #else
82 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
83 #endif
84
85 /// Expands to '&' if r-value references are supported.
86 ///
87 /// This can be used to provide l-value/r-value overrides of member functions.
88 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
89 #if LLVM_HAS_RVALUE_REFERENCE_THIS
90 #define LLVM_LVALUE_FUNCTION &
91 #else
92 #define LLVM_LVALUE_FUNCTION
93 #endif
94
95 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
96 /// Use to mark functions as uncallable. Member functions with this should
97 /// be declared private so that some behavior is kept in C++03 mode.
98 ///
99 /// class DontCopy {
100 /// private:
101 ///   DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
102 ///   DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
103 /// public:
104 ///   ...
105 /// };
106 #if __has_feature(cxx_deleted_functions) || defined(__GXX_EXPERIMENTAL_CXX0X__)
107 #define LLVM_DELETED_FUNCTION = delete
108 #else
109 #define LLVM_DELETED_FUNCTION
110 #endif
111
112 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
113 # define LLVM_CONSTEXPR constexpr
114 #else
115 # define LLVM_CONSTEXPR
116 #endif
117
118 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
119 /// into a shared library, then the class should be private to the library and
120 /// not accessible from outside it.  Can also be used to mark variables and
121 /// functions, making them private to any shared library they are linked into.
122 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
123 #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) &&              \
124     !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
125 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
126 #else
127 #define LLVM_LIBRARY_VISIBILITY
128 #endif
129
130 #if __has_attribute(sentinel) || LLVM_GNUC_PREREQ(3, 0, 0)
131 #define LLVM_END_WITH_NULL __attribute__((sentinel))
132 #else
133 #define LLVM_END_WITH_NULL
134 #endif
135
136 #if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
137 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
138 #else
139 #define LLVM_ATTRIBUTE_USED
140 #endif
141
142 #if __has_attribute(warn_unused_result) || LLVM_GNUC_PREREQ(3, 4, 0)
143 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
144 #else
145 #define LLVM_ATTRIBUTE_UNUSED_RESULT
146 #endif
147
148 // Some compilers warn about unused functions. When a function is sometimes
149 // used or not depending on build settings (e.g. a function only called from
150 // within "assert"), this attribute can be used to suppress such warnings.
151 //
152 // However, it shouldn't be used for unused *variables*, as those have a much
153 // more portable solution:
154 //   (void)unused_var_name;
155 // Prefer cast-to-void wherever it is sufficient.
156 #if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
157 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
158 #else
159 #define LLVM_ATTRIBUTE_UNUSED
160 #endif
161
162 // FIXME: Provide this for PE/COFF targets.
163 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) &&                    \
164     (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
165 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
166 #else
167 #define LLVM_ATTRIBUTE_WEAK
168 #endif
169
170 // Prior to clang 3.2, clang did not accept any spelling of
171 // __has_attribute(const), so assume it is supported.
172 #if defined(__clang__) || defined(__GNUC__)
173 // aka 'CONST' but following LLVM Conventions.
174 #define LLVM_READNONE __attribute__((__const__))
175 #else
176 #define LLVM_READNONE
177 #endif
178
179 #if __has_attribute(pure) || defined(__GNUC__)
180 // aka 'PURE' but following LLVM Conventions.
181 #define LLVM_READONLY __attribute__((__pure__))
182 #else
183 #define LLVM_READONLY
184 #endif
185
186 #if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
187 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
188 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
189 #else
190 #define LLVM_LIKELY(EXPR) (EXPR)
191 #define LLVM_UNLIKELY(EXPR) (EXPR)
192 #endif
193
194 // C++ doesn't support 'extern template' of template specializations.  GCC does,
195 // but requires __extension__ before it.  In the header, use this:
196 //   EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
197 // in the .cpp file, use this:
198 //   TEMPLATE_INSTANTIATION(class foo<bar>);
199 #ifdef __GNUC__
200 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
201 #define TEMPLATE_INSTANTIATION(X) template X
202 #else
203 #define EXTERN_TEMPLATE_INSTANTIATION(X)
204 #define TEMPLATE_INSTANTIATION(X)
205 #endif
206
207 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
208 /// mark a method "not for inlining".
209 #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
210 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
211 #elif defined(_MSC_VER)
212 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
213 #else
214 #define LLVM_ATTRIBUTE_NOINLINE
215 #endif
216
217 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
218 /// so, mark a method "always inline" because it is performance sensitive. GCC
219 /// 3.4 supported this but is buggy in various cases and produces unimplemented
220 /// errors, just use it in GCC 4.0 and later.
221 #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
222 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
223 #elif defined(_MSC_VER)
224 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
225 #else
226 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
227 #endif
228
229 #ifdef __GNUC__
230 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
231 #elif defined(_MSC_VER)
232 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
233 #else
234 #define LLVM_ATTRIBUTE_NORETURN
235 #endif
236
237 #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
238 #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
239 #else
240 #define LLVM_ATTRIBUTE_RETURNS_NONNULL
241 #endif
242
243 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
244 /// pedantic diagnostics.
245 #ifdef __GNUC__
246 #define LLVM_EXTENSION __extension__
247 #else
248 #define LLVM_EXTENSION
249 #endif
250
251 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
252 #if __has_feature(attribute_deprecated_with_message)
253 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
254   decl __attribute__((deprecated(message)))
255 #elif defined(__GNUC__)
256 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
257   decl __attribute__((deprecated))
258 #elif defined(_MSC_VER)
259 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
260   __declspec(deprecated(message)) decl
261 #else
262 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
263   decl
264 #endif
265
266 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
267 /// to an expression which states that it is undefined behavior for the
268 /// compiler to reach this point.  Otherwise is not defined.
269 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
270 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
271 #elif defined(_MSC_VER)
272 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
273 #endif
274
275 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
276 /// which causes the program to exit abnormally.
277 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
278 # define LLVM_BUILTIN_TRAP __builtin_trap()
279 #elif defined(_MSC_VER)
280 // The __debugbreak intrinsic is supported by MSVC, does not require forward
281 // declarations involving platform-specific typedefs (unlike RaiseException),
282 // results in a call to vectored exception handlers, and encodes to a short
283 // instruction that still causes the trapping behavior we want.
284 # define LLVM_BUILTIN_TRAP __debugbreak()
285 #else
286 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
287 #endif
288
289 /// \macro LLVM_ASSUME_ALIGNED
290 /// \brief Returns a pointer with an assumed alignment.
291 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
292 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
293 #elif defined(LLVM_BUILTIN_UNREACHABLE)
294 // As of today, clang does not support __builtin_assume_aligned.
295 # define LLVM_ASSUME_ALIGNED(p, a) \
296            (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
297 #else
298 # define LLVM_ASSUME_ALIGNED(p, a) (p)
299 #endif
300
301 /// \macro LLVM_FUNCTION_NAME
302 /// \brief Expands to __func__ on compilers which support it.  Otherwise,
303 /// expands to a compiler-dependent replacement.
304 #if defined(_MSC_VER)
305 # define LLVM_FUNCTION_NAME __FUNCTION__
306 #else
307 # define LLVM_FUNCTION_NAME __func__
308 #endif
309
310 /// \macro LLVM_MEMORY_SANITIZER_BUILD
311 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
312 #if __has_feature(memory_sanitizer)
313 # define LLVM_MEMORY_SANITIZER_BUILD 1
314 # include <sanitizer/msan_interface.h>
315 #else
316 # define LLVM_MEMORY_SANITIZER_BUILD 0
317 # define __msan_allocated_memory(p, size)
318 # define __msan_unpoison(p, size)
319 #endif
320
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 #else
326 # define LLVM_ADDRESS_SANITIZER_BUILD 0
327 #endif
328
329 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
330 /// \brief Is unaligned memory access fast on the host machine.
331 ///
332 /// Don't specialize on alignment for platforms where unaligned memory accesses
333 /// generates the same code as aligned memory accesses for common types.
334 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
335     defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
336     defined(_X86_) || defined(__i386) || defined(__i386__)
337 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
338 #else
339 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
340 #endif
341
342 /// \brief Does the compiler support generalized initializers (using braced
343 /// lists and std::initializer_list).  While clang may claim it supports general
344 /// initializers, if we're using MSVC's headers, we might not have a usable
345 /// std::initializer list type from the STL.  Disable this for now.
346 #if __has_feature(cxx_generalized_initializers) && !defined(_MSC_VER)
347 #define LLVM_HAS_INITIALIZER_LISTS 1
348 #else
349 #define LLVM_HAS_INITIALIZER_LISTS 0
350 #endif
351
352 /// \brief Mark debug helper function definitions like dump() that should not be
353 /// stripped from debug builds.
354 // FIXME: Move this to a private config.h as it's not usable in public headers.
355 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
356 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
357 #else
358 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
359 #endif
360
361 /// \macro LLVM_THREAD_LOCAL
362 /// \brief A thread-local storage specifier which can be used with globals,
363 /// extern globals, and static globals.
364 ///
365 /// This is essentially an extremely restricted analog to C++11's thread_local
366 /// support, and uses that when available. However, it falls back on
367 /// platform-specific or vendor-provided extensions when necessary. These
368 /// extensions don't support many of the C++11 thread_local's features. You
369 /// should only use this for PODs that you can statically initialize to
370 /// some constant value. In almost all circumstances this is most appropriate
371 /// for use with a pointer, integer, or small aggregation of pointers and
372 /// integers.
373 #if LLVM_ENABLE_THREADS
374 #if __has_feature(cxx_thread_local)
375 #define LLVM_THREAD_LOCAL thread_local
376 #elif defined(_MSC_VER)
377 // MSVC supports this with a __declspec.
378 #define LLVM_THREAD_LOCAL __declspec(thread)
379 #else
380 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
381 // we only need the restricted functionality that provides.
382 #define LLVM_THREAD_LOCAL __thread
383 #endif
384 #else // !LLVM_ENABLE_THREADS
385 // If threading is disabled entirely, this compiles to nothing and you get
386 // a normal global variable.
387 #define LLVM_THREAD_LOCAL
388 #endif
389
390 #endif