xref: /openbmc/u-boot/include/linux/compiler-gcc.h (revision fb8ffd7cfc68b3dc44e182356a207d784cb30b34)
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  */
8*fb8ffd7cSMasahiro Yamada #define GCC_VERSION (__GNUC__ * 10000 \
9*fb8ffd7cSMasahiro Yamada 		   + __GNUC_MINOR__ * 100 \
10*fb8ffd7cSMasahiro Yamada 		   + __GNUC_PATCHLEVEL__)
1195ffaba3SGraeme Russ 
1295ffaba3SGraeme Russ 
1395ffaba3SGraeme Russ /* Optimization barrier */
1495ffaba3SGraeme Russ /* The "volatile" is due to gcc bugs */
1595ffaba3SGraeme Russ #define barrier() __asm__ __volatile__("": : :"memory")
1695ffaba3SGraeme Russ 
1795ffaba3SGraeme Russ /*
1895ffaba3SGraeme Russ  * This macro obfuscates arithmetic on a variable address so that gcc
1995ffaba3SGraeme Russ  * shouldn't recognize the original var, and make assumptions about it.
2095ffaba3SGraeme Russ  *
2195ffaba3SGraeme Russ  * This is needed because the C standard makes it undefined to do
2295ffaba3SGraeme Russ  * pointer arithmetic on "objects" outside their boundaries and the
2395ffaba3SGraeme Russ  * gcc optimizers assume this is the case. In particular they
2495ffaba3SGraeme Russ  * assume such arithmetic does not wrap.
2595ffaba3SGraeme Russ  *
2695ffaba3SGraeme Russ  * A miscompilation has been observed because of this on PPC.
2795ffaba3SGraeme Russ  * To work around it we hide the relationship of the pointer and the object
2895ffaba3SGraeme Russ  * using this macro.
2995ffaba3SGraeme Russ  *
3095ffaba3SGraeme Russ  * Versions of the ppc64 compiler before 4.1 had a bug where use of
3195ffaba3SGraeme Russ  * RELOC_HIDE could trash r30. The bug can be worked around by changing
3295ffaba3SGraeme Russ  * the inline assembly constraint from =g to =r, in this particular
3395ffaba3SGraeme Russ  * case either is valid.
3495ffaba3SGraeme Russ  */
3595ffaba3SGraeme Russ #define RELOC_HIDE(ptr, off)					\
3695ffaba3SGraeme Russ   ({ unsigned long __ptr;					\
3795ffaba3SGraeme Russ     __asm__ ("" : "=r"(__ptr) : "0"(ptr));		\
3895ffaba3SGraeme Russ     (typeof(ptr)) (__ptr + (off)); })
3995ffaba3SGraeme Russ 
40*fb8ffd7cSMasahiro Yamada /* Make the optimizer believe the variable can be manipulated arbitrarily. */
41*fb8ffd7cSMasahiro Yamada #define OPTIMIZER_HIDE_VAR(var) __asm__ ("" : "=r" (var) : "0" (var))
42*fb8ffd7cSMasahiro Yamada 
43*fb8ffd7cSMasahiro Yamada #ifdef __CHECKER__
44*fb8ffd7cSMasahiro Yamada #define __must_be_array(arr) 0
45*fb8ffd7cSMasahiro Yamada #else
4695ffaba3SGraeme Russ /* &a[0] degrades to a pointer: a different type from an array */
47*fb8ffd7cSMasahiro Yamada #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
48*fb8ffd7cSMasahiro Yamada #endif
4995ffaba3SGraeme Russ 
5095ffaba3SGraeme Russ /*
5195ffaba3SGraeme Russ  * Force always-inline if the user requests it so via the .config,
5295ffaba3SGraeme Russ  * or if gcc is too old:
5395ffaba3SGraeme Russ  */
5495ffaba3SGraeme Russ #if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) || \
5595ffaba3SGraeme Russ     !defined(CONFIG_OPTIMIZE_INLINING) || (__GNUC__ < 4)
56*fb8ffd7cSMasahiro Yamada # define inline		inline		__attribute__((always_inline)) notrace
57*fb8ffd7cSMasahiro Yamada # define __inline__	__inline__	__attribute__((always_inline)) notrace
58*fb8ffd7cSMasahiro Yamada # define __inline	__inline	__attribute__((always_inline)) notrace
59*fb8ffd7cSMasahiro Yamada #else
60*fb8ffd7cSMasahiro Yamada /* A lot of inline functions can cause havoc with function tracing */
61*fb8ffd7cSMasahiro Yamada # define inline		inline		notrace
62*fb8ffd7cSMasahiro Yamada # define __inline__	__inline__	notrace
63*fb8ffd7cSMasahiro Yamada # define __inline	__inline	notrace
6495ffaba3SGraeme Russ #endif
6595ffaba3SGraeme Russ 
6695ffaba3SGraeme Russ #define __deprecated			__attribute__((deprecated))
6795ffaba3SGraeme Russ #define __packed			__attribute__((packed))
6895ffaba3SGraeme Russ #define __weak				__attribute__((weak))
6995ffaba3SGraeme Russ 
7095ffaba3SGraeme Russ /*
7195ffaba3SGraeme Russ  * it doesn't make sense on ARM (currently the only user of __naked) to trace
7295ffaba3SGraeme Russ  * naked functions because then mcount is called without stack and frame pointer
7395ffaba3SGraeme Russ  * being set up and there is no chance to restore the lr register to the value
7495ffaba3SGraeme Russ  * before mcount was called.
75*fb8ffd7cSMasahiro Yamada  *
76*fb8ffd7cSMasahiro Yamada  * The asm() bodies of naked functions often depend on standard calling conventions,
77*fb8ffd7cSMasahiro Yamada  * therefore they must be noinline and noclone.  GCC 4.[56] currently fail to enforce
78*fb8ffd7cSMasahiro Yamada  * this, so we must do so ourselves.  See GCC PR44290.
7995ffaba3SGraeme Russ  */
80*fb8ffd7cSMasahiro Yamada #define __naked				__attribute__((naked)) noinline __noclone notrace
8195ffaba3SGraeme Russ 
8295ffaba3SGraeme Russ #define __noreturn			__attribute__((noreturn))
8395ffaba3SGraeme Russ 
8495ffaba3SGraeme Russ /*
8595ffaba3SGraeme Russ  * From the GCC manual:
8695ffaba3SGraeme Russ  *
8795ffaba3SGraeme Russ  * Many functions have no effects except the return value and their
8895ffaba3SGraeme Russ  * return value depends only on the parameters and/or global
8995ffaba3SGraeme Russ  * variables.  Such a function can be subject to common subexpression
9095ffaba3SGraeme Russ  * elimination and loop optimization just as an arithmetic operator
9195ffaba3SGraeme Russ  * would be.
9295ffaba3SGraeme Russ  * [...]
9395ffaba3SGraeme Russ  */
9495ffaba3SGraeme Russ #define __pure				__attribute__((pure))
9595ffaba3SGraeme Russ #define __aligned(x)			__attribute__((aligned(x)))
9695ffaba3SGraeme Russ #define __printf(a, b)			__attribute__((format(printf, a, b)))
97*fb8ffd7cSMasahiro Yamada #define __scanf(a, b)			__attribute__((format(scanf, a, b)))
9895ffaba3SGraeme Russ #define  noinline			__attribute__((noinline))
9995ffaba3SGraeme Russ #define __attribute_const__		__attribute__((__const__))
10095ffaba3SGraeme Russ #define __maybe_unused			__attribute__((unused))
10195ffaba3SGraeme Russ #define __always_unused			__attribute__((unused))
10295ffaba3SGraeme Russ 
10395ffaba3SGraeme Russ #define __gcc_header(x) #x
10495ffaba3SGraeme Russ #define _gcc_header(x) __gcc_header(linux/compiler-gcc##x.h)
10595ffaba3SGraeme Russ #define gcc_header(x) _gcc_header(x)
10695ffaba3SGraeme Russ #include gcc_header(__GNUC__)
107*fb8ffd7cSMasahiro Yamada 
108*fb8ffd7cSMasahiro Yamada #if !defined(__noclone)
109*fb8ffd7cSMasahiro Yamada #define __noclone	/* not needed */
110*fb8ffd7cSMasahiro Yamada #endif
111*fb8ffd7cSMasahiro Yamada 
112*fb8ffd7cSMasahiro Yamada /*
113*fb8ffd7cSMasahiro Yamada  * A trick to suppress uninitialized variable warning without generating any
114*fb8ffd7cSMasahiro Yamada  * code
115*fb8ffd7cSMasahiro Yamada  */
116*fb8ffd7cSMasahiro Yamada #define uninitialized_var(x) x = x
117*fb8ffd7cSMasahiro Yamada 
118*fb8ffd7cSMasahiro Yamada #define __always_inline		inline __attribute__((always_inline))
119