compiler-gcc: integrate the various compiler-gcc[345].h files
[cascardo/linux.git] / include / linux / compiler-gcc.h
1 #ifndef __LINUX_COMPILER_H
2 #error "Please don't include <linux/compiler-gcc.h> directly, include <linux/compiler.h> instead."
3 #endif
4
5 /*
6  * Common definitions for all gcc versions go here.
7  */
8
9
10 /* Optimization barrier */
11 /* The "volatile" is due to gcc bugs */
12 #define barrier() __asm__ __volatile__("": : :"memory")
13
14 /*
15  * This macro obfuscates arithmetic on a variable address so that gcc
16  * shouldn't recognize the original var, and make assumptions about it.
17  *
18  * This is needed because the C standard makes it undefined to do
19  * pointer arithmetic on "objects" outside their boundaries and the
20  * gcc optimizers assume this is the case. In particular they
21  * assume such arithmetic does not wrap.
22  *
23  * A miscompilation has been observed because of this on PPC.
24  * To work around it we hide the relationship of the pointer and the object
25  * using this macro.
26  *
27  * Versions of the ppc64 compiler before 4.1 had a bug where use of
28  * RELOC_HIDE could trash r30. The bug can be worked around by changing
29  * the inline assembly constraint from =g to =r, in this particular
30  * case either is valid.
31  */
32 #define RELOC_HIDE(ptr, off)                                    \
33   ({ unsigned long __ptr;                                       \
34     __asm__ ("" : "=r"(__ptr) : "0"(ptr));              \
35     (typeof(ptr)) (__ptr + (off)); })
36
37 #ifdef __CHECKER__
38 #define __must_be_array(arr) 0
39 #else
40 /* &a[0] degrades to a pointer: a different type from an array */
41 #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
42 #endif
43
44 /*
45  * Force always-inline if the user requests it so via the .config,
46  * or if gcc is too old:
47  */
48 #if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) || \
49     !defined(CONFIG_OPTIMIZE_INLINING) || (__GNUC__ < 4)
50 # define inline         inline          __attribute__((always_inline))
51 # define __inline__     __inline__      __attribute__((always_inline))
52 # define __inline       __inline        __attribute__((always_inline))
53 #else
54 /* A lot of inline functions can cause havoc with function tracing */
55 # define inline         inline          notrace
56 # define __inline__     __inline__      notrace
57 # define __inline       __inline        notrace
58 #endif
59
60 #define __deprecated                    __attribute__((deprecated))
61 #define __packed                        __attribute__((packed))
62 #define __weak                          __attribute__((weak))
63
64 /*
65  * it doesn't make sense on ARM (currently the only user of __naked) to trace
66  * naked functions because then mcount is called without stack and frame pointer
67  * being set up and there is no chance to restore the lr register to the value
68  * before mcount was called.
69  *
70  * The asm() bodies of naked functions often depend on standard calling conventions,
71  * therefore they must be noinline and noclone.  GCC 4.[56] currently fail to enforce
72  * this, so we must do so ourselves.  See GCC PR44290.
73  */
74 #define __naked                         __attribute__((naked)) noinline __noclone notrace
75
76 #define __noreturn                      __attribute__((noreturn))
77
78 /*
79  * From the GCC manual:
80  *
81  * Many functions have no effects except the return value and their
82  * return value depends only on the parameters and/or global
83  * variables.  Such a function can be subject to common subexpression
84  * elimination and loop optimization just as an arithmetic operator
85  * would be.
86  * [...]
87  */
88 #define __pure                          __attribute__((pure))
89 #define __aligned(x)                    __attribute__((aligned(x)))
90 #define __printf(a, b)                  __attribute__((format(printf, a, b)))
91 #define __scanf(a, b)                   __attribute__((format(scanf, a, b)))
92 #define  noinline                       __attribute__((noinline))
93 #define __attribute_const__             __attribute__((__const__))
94 #define __maybe_unused                  __attribute__((unused))
95 #define __always_unused                 __attribute__((unused))
96
97 /* gcc version specific checks */
98
99 #if GCC_VERSION < 30200
100 # error Sorry, your compiler is too old - please upgrade it.
101 #endif
102
103 #if GCC_VERSION < 30300
104 # define __used                 __attribute__((__unused__))
105 #else
106 # define __used                 __attribute__((__used__))
107 #endif
108
109 #ifdef CONFIG_GCOV_KERNEL
110 # if GCC_VERSION < 30400
111 #   error "GCOV profiling support for gcc versions below 3.4 not included"
112 # endif /* __GNUC_MINOR__ */
113 #endif /* CONFIG_GCOV_KERNEL */
114
115 #if GCC_VERSION >= 30400
116 #define __must_check            __attribute__((warn_unused_result))
117 #endif
118
119 #if GCC_VERSION >= 40000
120
121 /* GCC 4.1.[01] miscompiles __weak */
122 #ifdef __KERNEL__
123 # if GCC_VERSION >= 40100 &&  GCC_VERSION <= 40101
124 #  error Your version of gcc miscompiles the __weak directive
125 # endif
126 #endif
127
128 #define __used                  __attribute__((__used__))
129 #define __compiler_offsetof(a, b)                                       \
130         __builtin_offsetof(a, b)
131
132 #if GCC_VERSION >= 40100 && GCC_VERSION < 40600
133 # define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
134 #endif
135
136 #if GCC_VERSION >= 40300
137 /* Mark functions as cold. gcc will assume any path leading to a call
138  * to them will be unlikely.  This means a lot of manual unlikely()s
139  * are unnecessary now for any paths leading to the usual suspects
140  * like BUG(), printk(), panic() etc. [but let's keep them for now for
141  * older compilers]
142  *
143  * Early snapshots of gcc 4.3 don't support this and we can't detect this
144  * in the preprocessor, but we can live with this because they're unreleased.
145  * Maketime probing would be overkill here.
146  *
147  * gcc also has a __attribute__((__hot__)) to move hot functions into
148  * a special section, but I don't see any sense in this right now in
149  * the kernel context
150  */
151 #define __cold                  __attribute__((__cold__))
152
153 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
154
155 #ifndef __CHECKER__
156 # define __compiletime_warning(message) __attribute__((warning(message)))
157 # define __compiletime_error(message) __attribute__((error(message)))
158 #endif /* __CHECKER__ */
159 #endif /* GCC_VERSION >= 40300 */
160
161 #if GCC_VERSION >= 40500
162 /*
163  * Mark a position in code as unreachable.  This can be used to
164  * suppress control flow warnings after asm blocks that transfer
165  * control elsewhere.
166  *
167  * Early snapshots of gcc 4.5 don't support this and we can't detect
168  * this in the preprocessor, but we can live with this because they're
169  * unreleased.  Really, we need to have autoconf for the kernel.
170  */
171 #define unreachable() __builtin_unreachable()
172
173 /* Mark a function definition as prohibited from being cloned. */
174 #define __noclone       __attribute__((__noclone__))
175
176 #endif /* GCC_VERSION >= 40500 */
177
178 #if GCC_VERSION >= 40600
179 /*
180  * Tell the optimizer that something else uses this function or variable.
181  */
182 #define __visible       __attribute__((externally_visible))
183 #endif
184
185 /*
186  * GCC 'asm goto' miscompiles certain code sequences:
187  *
188  *   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58670
189  *
190  * Work it around via a compiler barrier quirk suggested by Jakub Jelinek.
191  *
192  * (asm goto is automatically volatile - the naming reflects this.)
193  */
194 #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
195
196 #ifdef CONFIG_ARCH_USE_BUILTIN_BSWAP
197 #if GCC_VERSION >= 40400
198 #define __HAVE_BUILTIN_BSWAP32__
199 #define __HAVE_BUILTIN_BSWAP64__
200 #endif
201 #if GCC_VERSION >= 40800 || (defined(__powerpc__) && GCC_VERSION >= 40600)
202 #define __HAVE_BUILTIN_BSWAP16__
203 #endif
204 #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
205
206 #if GCC_VERSION >= 50000
207 #define KASAN_ABI_VERSION 4
208 #elif GCC_VERSION >= 40902
209 #define KASAN_ABI_VERSION 3
210 #endif
211
212 #endif  /* gcc version >= 40000 specific checks */
213
214 #if !defined(__noclone)
215 #define __noclone       /* not needed */
216 #endif
217
218 /*
219  * A trick to suppress uninitialized variable warning without generating any
220  * code
221  */
222 #define uninitialized_var(x) x = x
223
224 #define __always_inline         inline __attribute__((always_inline))