xref: /linux/include/linux/compiler.h (revision 1f0d69a9fc815db82f15722bf05227190b1d714d)
11da177e4SLinus Torvalds #ifndef __LINUX_COMPILER_H
21da177e4SLinus Torvalds #define __LINUX_COMPILER_H
31da177e4SLinus Torvalds 
41da177e4SLinus Torvalds #ifndef __ASSEMBLY__
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #ifdef __CHECKER__
71da177e4SLinus Torvalds # define __user		__attribute__((noderef, address_space(1)))
81da177e4SLinus Torvalds # define __kernel	/* default address space */
91da177e4SLinus Torvalds # define __safe		__attribute__((safe))
101da177e4SLinus Torvalds # define __force	__attribute__((force))
111da177e4SLinus Torvalds # define __nocast	__attribute__((nocast))
121da177e4SLinus Torvalds # define __iomem	__attribute__((noderef, address_space(2)))
13c902e0a0SJosh Triplett # define __acquires(x)	__attribute__((context(x,0,1)))
14c902e0a0SJosh Triplett # define __releases(x)	__attribute__((context(x,1,0)))
15c902e0a0SJosh Triplett # define __acquire(x)	__context__(x,1)
16c902e0a0SJosh Triplett # define __release(x)	__context__(x,-1)
17dcc8e559SJosh Triplett # define __cond_lock(x,c)	((c) ? ({ __acquire(x); 1; }) : 0)
18c47ffe3dSAl Viro extern void __chk_user_ptr(const volatile void __user *);
19c47ffe3dSAl Viro extern void __chk_io_ptr(const volatile void __iomem *);
201da177e4SLinus Torvalds #else
211da177e4SLinus Torvalds # define __user
221da177e4SLinus Torvalds # define __kernel
231da177e4SLinus Torvalds # define __safe
241da177e4SLinus Torvalds # define __force
251da177e4SLinus Torvalds # define __nocast
261da177e4SLinus Torvalds # define __iomem
271da177e4SLinus Torvalds # define __chk_user_ptr(x) (void)0
281da177e4SLinus Torvalds # define __chk_io_ptr(x) (void)0
291da177e4SLinus Torvalds # define __builtin_warning(x, y...) (1)
301da177e4SLinus Torvalds # define __acquires(x)
311da177e4SLinus Torvalds # define __releases(x)
321da177e4SLinus Torvalds # define __acquire(x) (void)0
331da177e4SLinus Torvalds # define __release(x) (void)0
34dcc8e559SJosh Triplett # define __cond_lock(x,c) (c)
351da177e4SLinus Torvalds #endif
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #ifdef __KERNEL__
381da177e4SLinus Torvalds 
3921124a82SAndi Kleen #if __GNUC__ >= 4
401da177e4SLinus Torvalds # include <linux/compiler-gcc4.h>
4153569ab7SAlistair John Strachan #elif __GNUC__ == 3 && __GNUC_MINOR__ >= 2
421da177e4SLinus Torvalds # include <linux/compiler-gcc3.h>
431da177e4SLinus Torvalds #else
441da177e4SLinus Torvalds # error Sorry, your compiler is too old/not recognized.
451da177e4SLinus Torvalds #endif
461da177e4SLinus Torvalds 
4728614889SSteven Rostedt #define notrace __attribute__((no_instrument_function))
4828614889SSteven Rostedt 
491da177e4SLinus Torvalds /* Intel compiler defines __GNUC__. So we will overwrite implementations
501da177e4SLinus Torvalds  * coming from above header files here
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds #ifdef __INTEL_COMPILER
531da177e4SLinus Torvalds # include <linux/compiler-intel.h>
541da177e4SLinus Torvalds #endif
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /*
571da177e4SLinus Torvalds  * Generic compiler-dependent macros required for kernel
581da177e4SLinus Torvalds  * build go below this comment. Actual compiler/compiler version
591da177e4SLinus Torvalds  * specific implementations come from the above header files
601da177e4SLinus Torvalds  */
611da177e4SLinus Torvalds 
62*1f0d69a9SSteven Rostedt #ifdef CONFIG_TRACE_UNLIKELY_PROFILE
63*1f0d69a9SSteven Rostedt struct ftrace_likely_data {
64*1f0d69a9SSteven Rostedt 	const char *func;
65*1f0d69a9SSteven Rostedt 	const char *file;
66*1f0d69a9SSteven Rostedt 	unsigned line;
67*1f0d69a9SSteven Rostedt 	unsigned long correct;
68*1f0d69a9SSteven Rostedt 	unsigned long incorrect;
69*1f0d69a9SSteven Rostedt };
70*1f0d69a9SSteven Rostedt void ftrace_likely_update(struct ftrace_likely_data *f, int val, int expect);
71*1f0d69a9SSteven Rostedt 
72*1f0d69a9SSteven Rostedt #define likely_notrace(x)	__builtin_expect(!!(x), 1)
73*1f0d69a9SSteven Rostedt #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
74*1f0d69a9SSteven Rostedt 
75*1f0d69a9SSteven Rostedt #define likely_check(x) ({						\
76*1f0d69a9SSteven Rostedt 			int ______r;					\
77*1f0d69a9SSteven Rostedt 			static struct ftrace_likely_data		\
78*1f0d69a9SSteven Rostedt 				__attribute__((__aligned__(4)))		\
79*1f0d69a9SSteven Rostedt 				__attribute__((section("_ftrace_likely"))) \
80*1f0d69a9SSteven Rostedt 				______f = {				\
81*1f0d69a9SSteven Rostedt 				.func = __func__,			\
82*1f0d69a9SSteven Rostedt 				.file = __FILE__,			\
83*1f0d69a9SSteven Rostedt 				.line = __LINE__,			\
84*1f0d69a9SSteven Rostedt 			};						\
85*1f0d69a9SSteven Rostedt 			______f.line = __LINE__;			\
86*1f0d69a9SSteven Rostedt 			______r = likely_notrace(x);			\
87*1f0d69a9SSteven Rostedt 			ftrace_likely_update(&______f, ______r, 1);	\
88*1f0d69a9SSteven Rostedt 			______r;					\
89*1f0d69a9SSteven Rostedt 		})
90*1f0d69a9SSteven Rostedt #define unlikely_check(x) ({						\
91*1f0d69a9SSteven Rostedt 			int ______r;					\
92*1f0d69a9SSteven Rostedt 			static struct ftrace_likely_data		\
93*1f0d69a9SSteven Rostedt 				__attribute__((__aligned__(4)))		\
94*1f0d69a9SSteven Rostedt 				__attribute__((section("_ftrace_unlikely"))) \
95*1f0d69a9SSteven Rostedt 				______f = {				\
96*1f0d69a9SSteven Rostedt 				.func = __func__,			\
97*1f0d69a9SSteven Rostedt 				.file = __FILE__,			\
98*1f0d69a9SSteven Rostedt 				.line = __LINE__,			\
99*1f0d69a9SSteven Rostedt 			};						\
100*1f0d69a9SSteven Rostedt 			______f.line = __LINE__;			\
101*1f0d69a9SSteven Rostedt 			______r = unlikely_notrace(x);			\
102*1f0d69a9SSteven Rostedt 			ftrace_likely_update(&______f, ______r, 0);	\
103*1f0d69a9SSteven Rostedt 			______r;					\
104*1f0d69a9SSteven Rostedt 		})
105*1f0d69a9SSteven Rostedt 
106*1f0d69a9SSteven Rostedt /*
107*1f0d69a9SSteven Rostedt  * Using __builtin_constant_p(x) to ignore cases where the return
108*1f0d69a9SSteven Rostedt  * value is always the same.  This idea is taken from a similar patch
109*1f0d69a9SSteven Rostedt  * written by Daniel Walker.
110*1f0d69a9SSteven Rostedt  */
111*1f0d69a9SSteven Rostedt # ifndef likely
112*1f0d69a9SSteven Rostedt #  define likely(x)	(__builtin_constant_p(x) ? !!(x) : likely_check(x))
113*1f0d69a9SSteven Rostedt # endif
114*1f0d69a9SSteven Rostedt # ifndef unlikely
115*1f0d69a9SSteven Rostedt #  define unlikely(x)	(__builtin_constant_p(x) ? !!(x) : unlikely_check(x))
116*1f0d69a9SSteven Rostedt # endif
117*1f0d69a9SSteven Rostedt #else
1181da177e4SLinus Torvalds # define likely(x)	__builtin_expect(!!(x), 1)
1191da177e4SLinus Torvalds # define unlikely(x)	__builtin_expect(!!(x), 0)
120*1f0d69a9SSteven Rostedt #endif
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /* Optimization barrier */
1231da177e4SLinus Torvalds #ifndef barrier
1241da177e4SLinus Torvalds # define barrier() __memory_barrier()
1251da177e4SLinus Torvalds #endif
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds #ifndef RELOC_HIDE
1281da177e4SLinus Torvalds # define RELOC_HIDE(ptr, off)					\
1291da177e4SLinus Torvalds   ({ unsigned long __ptr;					\
1301da177e4SLinus Torvalds      __ptr = (unsigned long) (ptr);				\
1311da177e4SLinus Torvalds     (typeof(ptr)) (__ptr + (off)); })
1321da177e4SLinus Torvalds #endif
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds #endif /* __KERNEL__ */
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds #endif /* __ASSEMBLY__ */
1371da177e4SLinus Torvalds 
1384f79c3ffSDavid Woodhouse #ifdef __KERNEL__
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds  * Allow us to mark functions as 'deprecated' and have gcc emit a nice
1411da177e4SLinus Torvalds  * warning for each use, in hopes of speeding the functions removal.
1421da177e4SLinus Torvalds  * Usage is:
1431da177e4SLinus Torvalds  * 		int __deprecated foo(void)
1441da177e4SLinus Torvalds  */
1451da177e4SLinus Torvalds #ifndef __deprecated
1461da177e4SLinus Torvalds # define __deprecated		/* unimplemented */
1471da177e4SLinus Torvalds #endif
1481da177e4SLinus Torvalds 
149512345beSPaul E. McKenney #ifdef MODULE
150512345beSPaul E. McKenney #define __deprecated_for_modules __deprecated
151512345beSPaul E. McKenney #else
152512345beSPaul E. McKenney #define __deprecated_for_modules
153512345beSPaul E. McKenney #endif
154512345beSPaul E. McKenney 
1551da177e4SLinus Torvalds #ifndef __must_check
1561da177e4SLinus Torvalds #define __must_check
1571da177e4SLinus Torvalds #endif
1581da177e4SLinus Torvalds 
159cebc04baSAndrew Morton #ifndef CONFIG_ENABLE_MUST_CHECK
160cebc04baSAndrew Morton #undef __must_check
161cebc04baSAndrew Morton #define __must_check
162cebc04baSAndrew Morton #endif
163de488443SJeff Garzik #ifndef CONFIG_ENABLE_WARN_DEPRECATED
164de488443SJeff Garzik #undef __deprecated
165de488443SJeff Garzik #undef __deprecated_for_modules
166de488443SJeff Garzik #define __deprecated
167de488443SJeff Garzik #define __deprecated_for_modules
168de488443SJeff Garzik #endif
169cebc04baSAndrew Morton 
1701da177e4SLinus Torvalds /*
1711da177e4SLinus Torvalds  * Allow us to avoid 'defined but not used' warnings on functions and data,
1721da177e4SLinus Torvalds  * as well as force them to be emitted to the assembly file.
1731da177e4SLinus Torvalds  *
1740d7ebbbcSDavid Rientjes  * As of gcc 3.4, static functions that are not marked with attribute((used))
1750d7ebbbcSDavid Rientjes  * may be elided from the assembly file.  As of gcc 3.4, static data not so
1761da177e4SLinus Torvalds  * marked will not be elided, but this may change in a future gcc version.
1771da177e4SLinus Torvalds  *
1780d7ebbbcSDavid Rientjes  * NOTE: Because distributions shipped with a backported unit-at-a-time
1790d7ebbbcSDavid Rientjes  * compiler in gcc 3.3, we must define __used to be __attribute__((used))
1800d7ebbbcSDavid Rientjes  * for gcc >=3.3 instead of 3.4.
1810d7ebbbcSDavid Rientjes  *
1821da177e4SLinus Torvalds  * In prior versions of gcc, such functions and data would be emitted, but
1831da177e4SLinus Torvalds  * would be warned about except with attribute((unused)).
1840d7ebbbcSDavid Rientjes  *
1850d7ebbbcSDavid Rientjes  * Mark functions that are referenced only in inline assembly as __used so
1860d7ebbbcSDavid Rientjes  * the code is emitted even though it appears to be unreferenced.
1871da177e4SLinus Torvalds  */
1880d7ebbbcSDavid Rientjes #ifndef __used
1890d7ebbbcSDavid Rientjes # define __used			/* unimplemented */
1900d7ebbbcSDavid Rientjes #endif
1910d7ebbbcSDavid Rientjes 
1920d7ebbbcSDavid Rientjes #ifndef __maybe_unused
1930d7ebbbcSDavid Rientjes # define __maybe_unused		/* unimplemented */
1941da177e4SLinus Torvalds #endif
1951da177e4SLinus Torvalds 
196423bc7b2SDavid Woodhouse #ifndef noinline
197423bc7b2SDavid Woodhouse #define noinline
198423bc7b2SDavid Woodhouse #endif
199423bc7b2SDavid Woodhouse 
200735c4fb9SAndrew Morton /*
201735c4fb9SAndrew Morton  * Rather then using noinline to prevent stack consumption, use
202735c4fb9SAndrew Morton  * noinline_for_stack instead.  For documentaiton reasons.
203735c4fb9SAndrew Morton  */
204735c4fb9SAndrew Morton #define noinline_for_stack noinline
205735c4fb9SAndrew Morton 
206423bc7b2SDavid Woodhouse #ifndef __always_inline
207423bc7b2SDavid Woodhouse #define __always_inline inline
208423bc7b2SDavid Woodhouse #endif
209423bc7b2SDavid Woodhouse 
210423bc7b2SDavid Woodhouse #endif /* __KERNEL__ */
211423bc7b2SDavid Woodhouse 
2121da177e4SLinus Torvalds /*
2131da177e4SLinus Torvalds  * From the GCC manual:
2141da177e4SLinus Torvalds  *
2151da177e4SLinus Torvalds  * Many functions do not examine any values except their arguments,
2161da177e4SLinus Torvalds  * and have no effects except the return value.  Basically this is
2171da177e4SLinus Torvalds  * just slightly more strict class than the `pure' attribute above,
2181da177e4SLinus Torvalds  * since function is not allowed to read global memory.
2191da177e4SLinus Torvalds  *
2201da177e4SLinus Torvalds  * Note that a function that has pointer arguments and examines the
2211da177e4SLinus Torvalds  * data pointed to must _not_ be declared `const'.  Likewise, a
2221da177e4SLinus Torvalds  * function that calls a non-`const' function usually must not be
2231da177e4SLinus Torvalds  * `const'.  It does not make sense for a `const' function to return
2241da177e4SLinus Torvalds  * `void'.
2251da177e4SLinus Torvalds  */
2261da177e4SLinus Torvalds #ifndef __attribute_const__
2271da177e4SLinus Torvalds # define __attribute_const__	/* unimplemented */
2281da177e4SLinus Torvalds #endif
2291da177e4SLinus Torvalds 
230a586df06SAndi Kleen /*
231a586df06SAndi Kleen  * Tell gcc if a function is cold. The compiler will assume any path
232a586df06SAndi Kleen  * directly leading to the call is unlikely.
233a586df06SAndi Kleen  */
234a586df06SAndi Kleen 
235a586df06SAndi Kleen #ifndef __cold
236a586df06SAndi Kleen #define __cold
237a586df06SAndi Kleen #endif
238a586df06SAndi Kleen 
239f3fe866dSSam Ravnborg /* Simple shorthand for a section definition */
240f3fe866dSSam Ravnborg #ifndef __section
241f3fe866dSSam Ravnborg # define __section(S) __attribute__ ((__section__(#S)))
242f3fe866dSSam Ravnborg #endif
243f3fe866dSSam Ravnborg 
2449c3cdc1fSLinus Torvalds /*
2459c3cdc1fSLinus Torvalds  * Prevent the compiler from merging or refetching accesses.  The compiler
2469c3cdc1fSLinus Torvalds  * is also forbidden from reordering successive instances of ACCESS_ONCE(),
2479c3cdc1fSLinus Torvalds  * but only when the compiler is aware of some particular ordering.  One way
2489c3cdc1fSLinus Torvalds  * to make the compiler aware of ordering is to put the two invocations of
2499c3cdc1fSLinus Torvalds  * ACCESS_ONCE() in different C statements.
2509c3cdc1fSLinus Torvalds  *
2519c3cdc1fSLinus Torvalds  * This macro does absolutely -nothing- to prevent the CPU from reordering,
252ded00a56SPaul E. McKenney  * merging, or refetching absolutely anything at any time.  Its main intended
253ded00a56SPaul E. McKenney  * use is to mediate communication between process-level code and irq/NMI
254ded00a56SPaul E. McKenney  * handlers, all running on the same CPU.
2559c3cdc1fSLinus Torvalds  */
2569c3cdc1fSLinus Torvalds #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
2579c3cdc1fSLinus Torvalds 
2581da177e4SLinus Torvalds #endif /* __LINUX_COMPILER_H */
259