xref: /openbmc/u-boot/include/linux/compiler-gcc.h (revision cf033e04)
195ffaba3SGraeme Russ #ifndef __LINUX_COMPILER_H
295ffaba3SGraeme Russ #error "Please don't include <linux/compiler-gcc.h> directly, include <linux/compiler.h> instead."
395ffaba3SGraeme Russ #endif
495ffaba3SGraeme Russ 
595ffaba3SGraeme Russ /*
695ffaba3SGraeme Russ  * Common definitions for all gcc versions go here.
795ffaba3SGraeme Russ  */
8fb8ffd7cSMasahiro Yamada #define GCC_VERSION (__GNUC__ * 10000		\
9fb8ffd7cSMasahiro Yamada 		     + __GNUC_MINOR__ * 100	\
10fb8ffd7cSMasahiro Yamada 		     + __GNUC_PATCHLEVEL__)
1195ffaba3SGraeme Russ 
1295ffaba3SGraeme Russ /* Optimization barrier */
139b2c282bSTom Rini 
1495ffaba3SGraeme Russ /* The "volatile" is due to gcc bugs */
15*07bc873cSSimon Glass #define barrier() \
16*07bc873cSSimon Glass 	__asm__ __volatile__("": : :"memory")
179b2c282bSTom Rini /*
189b2c282bSTom Rini  * This version is i.e. to prevent dead stores elimination on @ptr
199b2c282bSTom Rini  * where gcc and llvm may behave differently when otherwise using
209b2c282bSTom Rini  * normal barrier(): while gcc behavior gets along with a normal
219b2c282bSTom Rini  * barrier(), llvm needs an explicit input variable to be assumed
229b2c282bSTom Rini  * clobbered. The issue is as follows: while the inline asm might
239b2c282bSTom Rini  * access any memory it wants, the compiler could have fit all of
249b2c282bSTom Rini  * @ptr into memory registers instead, and since @ptr never escaped
259b2c282bSTom Rini  * from that, it proofed that the inline asm wasn't touching any of
269b2c282bSTom Rini  * it. This version works well with both compilers, i.e. we're telling
279b2c282bSTom Rini  * the compiler that the inline asm absolutely may see the contents
289b2c282bSTom Rini  * of @ptr. See also: https://llvm.org/bugs/show_bug.cgi?id=15495
299b2c282bSTom Rini  */
30*07bc873cSSimon Glass #define barrier_data(ptr) \
31*07bc873cSSimon Glass 	__asm__ __volatile__("": :"r"(ptr) :"memory")
3295ffaba3SGraeme Russ 
3395ffaba3SGraeme Russ /*
3495ffaba3SGraeme Russ  * This macro obfuscates arithmetic on a variable address so that gcc
3595ffaba3SGraeme Russ  * shouldn't recognize the original var, and make assumptions about it.
3695ffaba3SGraeme Russ  *
3795ffaba3SGraeme Russ  * This is needed because the C standard makes it undefined to do
3895ffaba3SGraeme Russ  * pointer arithmetic on "objects" outside their boundaries and the
3995ffaba3SGraeme Russ  * gcc optimizers assume this is the case. In particular they
4095ffaba3SGraeme Russ  * assume such arithmetic does not wrap.
4195ffaba3SGraeme Russ  *
4295ffaba3SGraeme Russ  * A miscompilation has been observed because of this on PPC.
4395ffaba3SGraeme Russ  * To work around it we hide the relationship of the pointer and the object
4495ffaba3SGraeme Russ  * using this macro.
4595ffaba3SGraeme Russ  *
4695ffaba3SGraeme Russ  * Versions of the ppc64 compiler before 4.1 had a bug where use of
4795ffaba3SGraeme Russ  * RELOC_HIDE could trash r30. The bug can be worked around by changing
4895ffaba3SGraeme Russ  * the inline assembly constraint from =g to =r, in this particular
4995ffaba3SGraeme Russ  * case either is valid.
5095ffaba3SGraeme Russ  */
5195ffaba3SGraeme Russ #define RELOC_HIDE(ptr, off)						\
529b2c282bSTom Rini ({									\
539b2c282bSTom Rini 	unsigned long __ptr;						\
5495ffaba3SGraeme Russ 	__asm__ ("" : "=r"(__ptr) : "0"(ptr));				\
559b2c282bSTom Rini 	(typeof(ptr)) (__ptr + (off));					\
569b2c282bSTom Rini })
5795ffaba3SGraeme Russ 
58fb8ffd7cSMasahiro Yamada /* Make the optimizer believe the variable can be manipulated arbitrarily. */
599b2c282bSTom Rini #define OPTIMIZER_HIDE_VAR(var)						\
609b2c282bSTom Rini 	__asm__ ("" : "=r" (var) : "0" (var))
61fb8ffd7cSMasahiro Yamada 
62fb8ffd7cSMasahiro Yamada #ifdef __CHECKER__
639b2c282bSTom Rini #define __must_be_array(a)	0
64fb8ffd7cSMasahiro Yamada #else
6595ffaba3SGraeme Russ /* &a[0] degrades to a pointer: a different type from an array */
66fb8ffd7cSMasahiro Yamada #define __must_be_array(a)	BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
67fb8ffd7cSMasahiro Yamada #endif
6895ffaba3SGraeme Russ 
6995ffaba3SGraeme Russ /*
7095ffaba3SGraeme Russ  * Force always-inline if the user requests it so via the .config,
7195ffaba3SGraeme Russ  * or if gcc is too old:
7295ffaba3SGraeme Russ  */
7395ffaba3SGraeme Russ #if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) ||		\
7495ffaba3SGraeme Russ     !defined(CONFIG_OPTIMIZE_INLINING) || (__GNUC__ < 4)
75fb8ffd7cSMasahiro Yamada #define inline		inline		__attribute__((always_inline)) notrace
76fb8ffd7cSMasahiro Yamada #define __inline__	__inline__	__attribute__((always_inline)) notrace
77fb8ffd7cSMasahiro Yamada #define __inline	__inline	__attribute__((always_inline)) notrace
78fb8ffd7cSMasahiro Yamada #else
79fb8ffd7cSMasahiro Yamada /* A lot of inline functions can cause havoc with function tracing */
80fb8ffd7cSMasahiro Yamada #define inline		inline		notrace
81fb8ffd7cSMasahiro Yamada #define __inline__	__inline__	notrace
82fb8ffd7cSMasahiro Yamada #define __inline	__inline	notrace
8395ffaba3SGraeme Russ #endif
8495ffaba3SGraeme Russ 
859b2c282bSTom Rini #define __always_inline	inline __attribute__((always_inline))
869b2c282bSTom Rini #define  noinline	__attribute__((noinline))
879b2c282bSTom Rini 
8895ffaba3SGraeme Russ #define __deprecated	__attribute__((deprecated))
8995ffaba3SGraeme Russ #define __packed	__attribute__((packed))
9095ffaba3SGraeme Russ #define __weak		__attribute__((weak))
919b2c282bSTom Rini #define __alias(symbol)	__attribute__((alias(#symbol)))
9295ffaba3SGraeme Russ 
9395ffaba3SGraeme Russ /*
949b2c282bSTom Rini  * it doesn't make sense on ARM (currently the only user of __naked)
959b2c282bSTom Rini  * to trace naked functions because then mcount is called without
969b2c282bSTom Rini  * stack and frame pointer being set up and there is no chance to
979b2c282bSTom Rini  * restore the lr register to the value before mcount was called.
98fb8ffd7cSMasahiro Yamada  *
999b2c282bSTom Rini  * The asm() bodies of naked functions often depend on standard calling
1009b2c282bSTom Rini  * conventions, therefore they must be noinline and noclone.
1019b2c282bSTom Rini  *
1029b2c282bSTom Rini  * GCC 4.[56] currently fail to enforce this, so we must do so ourselves.
1039b2c282bSTom Rini  * See GCC PR44290.
10495ffaba3SGraeme Russ  */
105fb8ffd7cSMasahiro Yamada #define __naked		__attribute__((naked)) noinline __noclone notrace
10695ffaba3SGraeme Russ 
10795ffaba3SGraeme Russ #define __noreturn	__attribute__((noreturn))
10895ffaba3SGraeme Russ 
10995ffaba3SGraeme Russ /*
11095ffaba3SGraeme Russ  * From the GCC manual:
11195ffaba3SGraeme Russ  *
11295ffaba3SGraeme Russ  * Many functions have no effects except the return value and their
11395ffaba3SGraeme Russ  * return value depends only on the parameters and/or global
11495ffaba3SGraeme Russ  * variables.  Such a function can be subject to common subexpression
11595ffaba3SGraeme Russ  * elimination and loop optimization just as an arithmetic operator
11695ffaba3SGraeme Russ  * would be.
11795ffaba3SGraeme Russ  * [...]
11895ffaba3SGraeme Russ  */
11995ffaba3SGraeme Russ #define __pure			__attribute__((pure))
12095ffaba3SGraeme Russ #define __aligned(x)		__attribute__((aligned(x)))
12195ffaba3SGraeme Russ #define __printf(a, b)		__attribute__((format(printf, a, b)))
122fb8ffd7cSMasahiro Yamada #define __scanf(a, b)		__attribute__((format(scanf, a, b)))
12395ffaba3SGraeme Russ #define __attribute_const__	__attribute__((__const__))
12495ffaba3SGraeme Russ #define __maybe_unused		__attribute__((unused))
12595ffaba3SGraeme Russ #define __always_unused		__attribute__((unused))
12695ffaba3SGraeme Russ 
1279b2c282bSTom Rini /* gcc version specific checks */
1289b2c282bSTom Rini 
1299b2c282bSTom Rini #if GCC_VERSION < 30200
1309b2c282bSTom Rini # error Sorry, your compiler is too old - please upgrade it.
1319b2c282bSTom Rini #endif
1329b2c282bSTom Rini 
1339b2c282bSTom Rini #if GCC_VERSION < 30300
1349b2c282bSTom Rini # define __used			__attribute__((__unused__))
1359b2c282bSTom Rini #else
1369b2c282bSTom Rini # define __used			__attribute__((__used__))
1379b2c282bSTom Rini #endif
1389b2c282bSTom Rini 
1399b2c282bSTom Rini #ifdef CONFIG_GCOV_KERNEL
1409b2c282bSTom Rini # if GCC_VERSION < 30400
1419b2c282bSTom Rini #   error "GCOV profiling support for gcc versions below 3.4 not included"
1429b2c282bSTom Rini # endif /* __GNUC_MINOR__ */
1439b2c282bSTom Rini #endif /* CONFIG_GCOV_KERNEL */
1449b2c282bSTom Rini 
1459b2c282bSTom Rini #if GCC_VERSION >= 30400
1469b2c282bSTom Rini #define __must_check		__attribute__((warn_unused_result))
1479b2c282bSTom Rini #endif
1489b2c282bSTom Rini 
1499b2c282bSTom Rini #if GCC_VERSION >= 40000
1509b2c282bSTom Rini 
1519b2c282bSTom Rini /* GCC 4.1.[01] miscompiles __weak */
1529b2c282bSTom Rini #ifdef __KERNEL__
1539b2c282bSTom Rini # if GCC_VERSION >= 40100 &&  GCC_VERSION <= 40101
1549b2c282bSTom Rini #  error Your version of gcc miscompiles the __weak directive
1559b2c282bSTom Rini # endif
1569b2c282bSTom Rini #endif
1579b2c282bSTom Rini 
1589b2c282bSTom Rini #define __used			__attribute__((__used__))
1599b2c282bSTom Rini #define __compiler_offsetof(a, b)					\
1609b2c282bSTom Rini 	__builtin_offsetof(a, b)
1619b2c282bSTom Rini 
1629b2c282bSTom Rini #if GCC_VERSION >= 40100 && GCC_VERSION < 40600
1639b2c282bSTom Rini # define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
1649b2c282bSTom Rini #endif
1659b2c282bSTom Rini 
1669b2c282bSTom Rini #if GCC_VERSION >= 40300
1679b2c282bSTom Rini /* Mark functions as cold. gcc will assume any path leading to a call
1689b2c282bSTom Rini  * to them will be unlikely.  This means a lot of manual unlikely()s
1699b2c282bSTom Rini  * are unnecessary now for any paths leading to the usual suspects
1709b2c282bSTom Rini  * like BUG(), printk(), panic() etc. [but let's keep them for now for
1719b2c282bSTom Rini  * older compilers]
1729b2c282bSTom Rini  *
1739b2c282bSTom Rini  * Early snapshots of gcc 4.3 don't support this and we can't detect this
1749b2c282bSTom Rini  * in the preprocessor, but we can live with this because they're unreleased.
1759b2c282bSTom Rini  * Maketime probing would be overkill here.
1769b2c282bSTom Rini  *
1779b2c282bSTom Rini  * gcc also has a __attribute__((__hot__)) to move hot functions into
1789b2c282bSTom Rini  * a special section, but I don't see any sense in this right now in
1799b2c282bSTom Rini  * the kernel context
1809b2c282bSTom Rini  */
1819b2c282bSTom Rini #define __cold			__attribute__((__cold__))
1829b2c282bSTom Rini 
1839b2c282bSTom Rini #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
1849b2c282bSTom Rini 
1859b2c282bSTom Rini #ifndef __CHECKER__
1869b2c282bSTom Rini # define __compiletime_warning(message) __attribute__((warning(message)))
1879b2c282bSTom Rini # define __compiletime_error(message) __attribute__((error(message)))
1889b2c282bSTom Rini #endif /* __CHECKER__ */
1899b2c282bSTom Rini #endif /* GCC_VERSION >= 40300 */
1909b2c282bSTom Rini 
1919b2c282bSTom Rini #if GCC_VERSION >= 40500
1929b2c282bSTom Rini /*
1939b2c282bSTom Rini  * Mark a position in code as unreachable.  This can be used to
1949b2c282bSTom Rini  * suppress control flow warnings after asm blocks that transfer
1959b2c282bSTom Rini  * control elsewhere.
1969b2c282bSTom Rini  *
1979b2c282bSTom Rini  * Early snapshots of gcc 4.5 don't support this and we can't detect
1989b2c282bSTom Rini  * this in the preprocessor, but we can live with this because they're
1999b2c282bSTom Rini  * unreleased.  Really, we need to have autoconf for the kernel.
2009b2c282bSTom Rini  */
2019b2c282bSTom Rini #define unreachable() __builtin_unreachable()
2029b2c282bSTom Rini 
2039b2c282bSTom Rini /* Mark a function definition as prohibited from being cloned. */
2049b2c282bSTom Rini #define __noclone	__attribute__((__noclone__))
2059b2c282bSTom Rini 
2069b2c282bSTom Rini #endif /* GCC_VERSION >= 40500 */
2079b2c282bSTom Rini 
2089b2c282bSTom Rini #if GCC_VERSION >= 40600
2099b2c282bSTom Rini /*
2109b2c282bSTom Rini  * When used with Link Time Optimization, gcc can optimize away C functions or
2119b2c282bSTom Rini  * variables which are referenced only from assembly code.  __visible tells the
2129b2c282bSTom Rini  * optimizer that something else uses this function or variable, thus preventing
2139b2c282bSTom Rini  * this.
2149b2c282bSTom Rini  */
2159b2c282bSTom Rini #define __visible	__attribute__((externally_visible))
2169b2c282bSTom Rini #endif
2179b2c282bSTom Rini 
2189b2c282bSTom Rini 
2199b2c282bSTom Rini #if GCC_VERSION >= 40900 && !defined(__CHECKER__)
2209b2c282bSTom Rini /*
2219b2c282bSTom Rini  * __assume_aligned(n, k): Tell the optimizer that the returned
2229b2c282bSTom Rini  * pointer can be assumed to be k modulo n. The second argument is
2239b2c282bSTom Rini  * optional (default 0), so we use a variadic macro to make the
2249b2c282bSTom Rini  * shorthand.
2259b2c282bSTom Rini  *
2269b2c282bSTom Rini  * Beware: Do not apply this to functions which may return
2279b2c282bSTom Rini  * ERR_PTRs. Also, it is probably unwise to apply it to functions
2289b2c282bSTom Rini  * returning extra information in the low bits (but in that case the
2299b2c282bSTom Rini  * compiler should see some alignment anyway, when the return value is
2309b2c282bSTom Rini  * massaged by 'flags = ptr & 3; ptr &= ~3;').
2319b2c282bSTom Rini  */
2329b2c282bSTom Rini #define __assume_aligned(a, ...) __attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
2339b2c282bSTom Rini #endif
2349b2c282bSTom Rini 
2359b2c282bSTom Rini /*
2369b2c282bSTom Rini  * GCC 'asm goto' miscompiles certain code sequences:
2379b2c282bSTom Rini  *
2389b2c282bSTom Rini  *   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58670
2399b2c282bSTom Rini  *
2409b2c282bSTom Rini  * Work it around via a compiler barrier quirk suggested by Jakub Jelinek.
2419b2c282bSTom Rini  *
2429b2c282bSTom Rini  * (asm goto is automatically volatile - the naming reflects this.)
2439b2c282bSTom Rini  */
2449b2c282bSTom Rini #define asm_volatile_goto(x...)	do { asm goto(x); asm (""); } while (0)
2459b2c282bSTom Rini 
2469b2c282bSTom Rini #ifdef CONFIG_ARCH_USE_BUILTIN_BSWAP
2479b2c282bSTom Rini #if GCC_VERSION >= 40400
2489b2c282bSTom Rini #define __HAVE_BUILTIN_BSWAP32__
2499b2c282bSTom Rini #define __HAVE_BUILTIN_BSWAP64__
2509b2c282bSTom Rini #endif
2519b2c282bSTom Rini #if GCC_VERSION >= 40800 || (defined(__powerpc__) && GCC_VERSION >= 40600)
2529b2c282bSTom Rini #define __HAVE_BUILTIN_BSWAP16__
2539b2c282bSTom Rini #endif
2549b2c282bSTom Rini #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
2559b2c282bSTom Rini 
2569b2c282bSTom Rini #if GCC_VERSION >= 50000
2579b2c282bSTom Rini #define KASAN_ABI_VERSION 4
2589b2c282bSTom Rini #elif GCC_VERSION >= 40902
2599b2c282bSTom Rini #define KASAN_ABI_VERSION 3
2609b2c282bSTom Rini #endif
2619b2c282bSTom Rini 
2629b2c282bSTom Rini #if GCC_VERSION >= 40902
2639b2c282bSTom Rini /*
2649b2c282bSTom Rini  * Tell the compiler that address safety instrumentation (KASAN)
2659b2c282bSTom Rini  * should not be applied to that function.
2669b2c282bSTom Rini  * Conflicts with inlining: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
2679b2c282bSTom Rini  */
2689b2c282bSTom Rini #define __no_sanitize_address __attribute__((no_sanitize_address))
2699b2c282bSTom Rini #endif
2709b2c282bSTom Rini 
2719b2c282bSTom Rini #endif	/* gcc version >= 40000 specific checks */
272fb8ffd7cSMasahiro Yamada 
273fb8ffd7cSMasahiro Yamada #if !defined(__noclone)
274fb8ffd7cSMasahiro Yamada #define __noclone	/* not needed */
275fb8ffd7cSMasahiro Yamada #endif
276fb8ffd7cSMasahiro Yamada 
2779b2c282bSTom Rini #if !defined(__no_sanitize_address)
2789b2c282bSTom Rini #define __no_sanitize_address
2799b2c282bSTom Rini #endif
2809b2c282bSTom Rini 
281fb8ffd7cSMasahiro Yamada /*
282fb8ffd7cSMasahiro Yamada  * A trick to suppress uninitialized variable warning without generating any
283fb8ffd7cSMasahiro Yamada  * code
284fb8ffd7cSMasahiro Yamada  */
285fb8ffd7cSMasahiro Yamada #define uninitialized_var(x) x = x
286