[support] Add a macro wrapper for alignas and simplify some code.
[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 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
96 # define LLVM_CONSTEXPR constexpr
97 #else
98 # define LLVM_CONSTEXPR
99 #endif
100
101 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
102 /// into a shared library, then the class should be private to the library and
103 /// not accessible from outside it.  Can also be used to mark variables and
104 /// functions, making them private to any shared library they are linked into.
105 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
106 #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) &&              \
107     !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
108 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
109 #else
110 #define LLVM_LIBRARY_VISIBILITY
111 #endif
112
113 #if __has_attribute(sentinel) || LLVM_GNUC_PREREQ(3, 0, 0)
114 #define LLVM_END_WITH_NULL __attribute__((sentinel))
115 #else
116 #define LLVM_END_WITH_NULL
117 #endif
118
119 #if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
120 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
121 #else
122 #define LLVM_ATTRIBUTE_USED
123 #endif
124
125 #if __has_attribute(warn_unused_result) || LLVM_GNUC_PREREQ(3, 4, 0)
126 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
127 #else
128 #define LLVM_ATTRIBUTE_UNUSED_RESULT
129 #endif
130
131 // Some compilers warn about unused functions. When a function is sometimes
132 // used or not depending on build settings (e.g. a function only called from
133 // within "assert"), this attribute can be used to suppress such warnings.
134 //
135 // However, it shouldn't be used for unused *variables*, as those have a much
136 // more portable solution:
137 //   (void)unused_var_name;
138 // Prefer cast-to-void wherever it is sufficient.
139 #if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
140 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
141 #else
142 #define LLVM_ATTRIBUTE_UNUSED
143 #endif
144
145 // FIXME: Provide this for PE/COFF targets.
146 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) &&                    \
147     (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
148 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
149 #else
150 #define LLVM_ATTRIBUTE_WEAK
151 #endif
152
153 // Prior to clang 3.2, clang did not accept any spelling of
154 // __has_attribute(const), so assume it is supported.
155 #if defined(__clang__) || defined(__GNUC__)
156 // aka 'CONST' but following LLVM Conventions.
157 #define LLVM_READNONE __attribute__((__const__))
158 #else
159 #define LLVM_READNONE
160 #endif
161
162 #if __has_attribute(pure) || defined(__GNUC__)
163 // aka 'PURE' but following LLVM Conventions.
164 #define LLVM_READONLY __attribute__((__pure__))
165 #else
166 #define LLVM_READONLY
167 #endif
168
169 #if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
170 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
171 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
172 #else
173 #define LLVM_LIKELY(EXPR) (EXPR)
174 #define LLVM_UNLIKELY(EXPR) (EXPR)
175 #endif
176
177 // C++ doesn't support 'extern template' of template specializations.  GCC does,
178 // but requires __extension__ before it.  In the header, use this:
179 //   EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
180 // in the .cpp file, use this:
181 //   TEMPLATE_INSTANTIATION(class foo<bar>);
182 #ifdef __GNUC__
183 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
184 #define TEMPLATE_INSTANTIATION(X) template X
185 #else
186 #define EXTERN_TEMPLATE_INSTANTIATION(X)
187 #define TEMPLATE_INSTANTIATION(X)
188 #endif
189
190 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
191 /// mark a method "not for inlining".
192 #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
193 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
194 #elif defined(_MSC_VER)
195 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
196 #else
197 #define LLVM_ATTRIBUTE_NOINLINE
198 #endif
199
200 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
201 /// so, mark a method "always inline" because it is performance sensitive. GCC
202 /// 3.4 supported this but is buggy in various cases and produces unimplemented
203 /// errors, just use it in GCC 4.0 and later.
204 #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
205 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
206 #elif defined(_MSC_VER)
207 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
208 #else
209 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
210 #endif
211
212 #ifdef __GNUC__
213 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
214 #elif defined(_MSC_VER)
215 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
216 #else
217 #define LLVM_ATTRIBUTE_NORETURN
218 #endif
219
220 #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
221 #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
222 #else
223 #define LLVM_ATTRIBUTE_RETURNS_NONNULL
224 #endif
225
226 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
227 /// pedantic diagnostics.
228 #ifdef __GNUC__
229 #define LLVM_EXTENSION __extension__
230 #else
231 #define LLVM_EXTENSION
232 #endif
233
234 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
235 #if __has_feature(attribute_deprecated_with_message)
236 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
237   decl __attribute__((deprecated(message)))
238 #elif defined(__GNUC__)
239 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
240   decl __attribute__((deprecated))
241 #elif defined(_MSC_VER)
242 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
243   __declspec(deprecated(message)) decl
244 #else
245 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
246   decl
247 #endif
248
249 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
250 /// to an expression which states that it is undefined behavior for the
251 /// compiler to reach this point.  Otherwise is not defined.
252 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
253 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
254 #elif defined(_MSC_VER)
255 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
256 #endif
257
258 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
259 /// which causes the program to exit abnormally.
260 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
261 # define LLVM_BUILTIN_TRAP __builtin_trap()
262 #elif defined(_MSC_VER)
263 // The __debugbreak intrinsic is supported by MSVC, does not require forward
264 // declarations involving platform-specific typedefs (unlike RaiseException),
265 // results in a call to vectored exception handlers, and encodes to a short
266 // instruction that still causes the trapping behavior we want.
267 # define LLVM_BUILTIN_TRAP __debugbreak()
268 #else
269 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
270 #endif
271
272 /// \macro LLVM_ASSUME_ALIGNED
273 /// \brief Returns a pointer with an assumed alignment.
274 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
275 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
276 #elif defined(LLVM_BUILTIN_UNREACHABLE)
277 // As of today, clang does not support __builtin_assume_aligned.
278 # define LLVM_ASSUME_ALIGNED(p, a) \
279            (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
280 #else
281 # define LLVM_ASSUME_ALIGNED(p, a) (p)
282 #endif
283
284 /// \macro LLVM_ALIGNAS
285 /// \brief Used to specify a minimum alignment for a structure or variable. The
286 /// alignment must be a constant integer.
287 ///
288 /// Note that __declspec(align) has special quirks, it's not legal to pass a
289 /// structure with __declspec(align) as a formal parameter.
290 #ifdef _MSC_VER
291 # define LLVM_ALIGNAS(x) __declspec(align(x))
292 #elif __GNUC__ && !__has_feature(cxx_alignas) && !LLVM_GNUC_PREREQ(4, 8, 0)
293 # define LLVM_ALIGNAS(x) __attribute__((aligned(x)))
294 #else
295 # define LLVM_ALIGNAS(x) alignas(x)
296 #endif
297
298 /// \macro LLVM_FUNCTION_NAME
299 /// \brief Expands to __func__ on compilers which support it.  Otherwise,
300 /// expands to a compiler-dependent replacement.
301 #if defined(_MSC_VER)
302 # define LLVM_FUNCTION_NAME __FUNCTION__
303 #else
304 # define LLVM_FUNCTION_NAME __func__
305 #endif
306
307 /// \macro LLVM_MEMORY_SANITIZER_BUILD
308 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
309 #if __has_feature(memory_sanitizer)
310 # define LLVM_MEMORY_SANITIZER_BUILD 1
311 # include <sanitizer/msan_interface.h>
312 #else
313 # define LLVM_MEMORY_SANITIZER_BUILD 0
314 # define __msan_allocated_memory(p, size)
315 # define __msan_unpoison(p, size)
316 #endif
317
318 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
319 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
320 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
321 # define LLVM_ADDRESS_SANITIZER_BUILD 1
322 #else
323 # define LLVM_ADDRESS_SANITIZER_BUILD 0
324 #endif
325
326 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
327 /// \brief Is unaligned memory access fast on the host machine.
328 ///
329 /// Don't specialize on alignment for platforms where unaligned memory accesses
330 /// generates the same code as aligned memory accesses for common types.
331 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
332     defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
333     defined(_X86_) || defined(__i386) || defined(__i386__)
334 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
335 #else
336 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
337 #endif
338
339 /// \brief Mark debug helper function definitions like dump() that should not be
340 /// stripped from debug builds.
341 // FIXME: Move this to a private config.h as it's not usable in public headers.
342 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
343 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
344 #else
345 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
346 #endif
347
348 /// \macro LLVM_THREAD_LOCAL
349 /// \brief A thread-local storage specifier which can be used with globals,
350 /// extern globals, and static globals.
351 ///
352 /// This is essentially an extremely restricted analog to C++11's thread_local
353 /// support, and uses that when available. However, it falls back on
354 /// platform-specific or vendor-provided extensions when necessary. These
355 /// extensions don't support many of the C++11 thread_local's features. You
356 /// should only use this for PODs that you can statically initialize to
357 /// some constant value. In almost all circumstances this is most appropriate
358 /// for use with a pointer, integer, or small aggregation of pointers and
359 /// integers.
360 #if LLVM_ENABLE_THREADS
361 #if __has_feature(cxx_thread_local)
362 #define LLVM_THREAD_LOCAL thread_local
363 #elif defined(_MSC_VER)
364 // MSVC supports this with a __declspec.
365 #define LLVM_THREAD_LOCAL __declspec(thread)
366 #else
367 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
368 // we only need the restricted functionality that provides.
369 #define LLVM_THREAD_LOCAL __thread
370 #endif
371 #else // !LLVM_ENABLE_THREADS
372 // If threading is disabled entirely, this compiles to nothing and you get
373 // a normal global variable.
374 #define LLVM_THREAD_LOCAL
375 #endif
376
377 #endif