xref: /linux/include/linux/compiler.h (revision 2ed84eeb8808cf3c9f039213ca137ffd7d753f0e)
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*2ed84eebSSteven Rostedt struct ftrace_branch_data {
631f0d69a9SSteven Rostedt 	const char *func;
641f0d69a9SSteven Rostedt 	const char *file;
651f0d69a9SSteven Rostedt 	unsigned line;
661f0d69a9SSteven Rostedt 	unsigned long correct;
671f0d69a9SSteven Rostedt 	unsigned long incorrect;
681f0d69a9SSteven Rostedt };
69*2ed84eebSSteven Rostedt 
70*2ed84eebSSteven Rostedt /*
71*2ed84eebSSteven Rostedt  * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
72*2ed84eebSSteven Rostedt  * to disable branch tracing on a per file basis.
73*2ed84eebSSteven Rostedt  */
74*2ed84eebSSteven Rostedt #if defined(CONFIG_TRACE_BRANCH_PROFILING) && !defined(DISABLE_BRANCH_PROFILING)
75*2ed84eebSSteven Rostedt void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
761f0d69a9SSteven Rostedt 
771f0d69a9SSteven Rostedt #define likely_notrace(x)	__builtin_expect(!!(x), 1)
781f0d69a9SSteven Rostedt #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
791f0d69a9SSteven Rostedt 
801f0d69a9SSteven Rostedt #define likely_check(x) ({						\
811f0d69a9SSteven Rostedt 			int ______r;					\
82*2ed84eebSSteven Rostedt 			static struct ftrace_branch_data		\
831f0d69a9SSteven Rostedt 				__attribute__((__aligned__(4)))		\
841f0d69a9SSteven Rostedt 				__attribute__((section("_ftrace_likely"))) \
851f0d69a9SSteven Rostedt 				______f = {				\
861f0d69a9SSteven Rostedt 				.func = __func__,			\
871f0d69a9SSteven Rostedt 				.file = __FILE__,			\
881f0d69a9SSteven Rostedt 				.line = __LINE__,			\
891f0d69a9SSteven Rostedt 			};						\
901f0d69a9SSteven Rostedt 			______f.line = __LINE__;			\
911f0d69a9SSteven Rostedt 			______r = likely_notrace(x);			\
921f0d69a9SSteven Rostedt 			ftrace_likely_update(&______f, ______r, 1);	\
931f0d69a9SSteven Rostedt 			______r;					\
941f0d69a9SSteven Rostedt 		})
951f0d69a9SSteven Rostedt #define unlikely_check(x) ({						\
961f0d69a9SSteven Rostedt 			int ______r;					\
97*2ed84eebSSteven Rostedt 			static struct ftrace_branch_data		\
981f0d69a9SSteven Rostedt 				__attribute__((__aligned__(4)))		\
991f0d69a9SSteven Rostedt 				__attribute__((section("_ftrace_unlikely"))) \
1001f0d69a9SSteven Rostedt 				______f = {				\
1011f0d69a9SSteven Rostedt 				.func = __func__,			\
1021f0d69a9SSteven Rostedt 				.file = __FILE__,			\
1031f0d69a9SSteven Rostedt 				.line = __LINE__,			\
1041f0d69a9SSteven Rostedt 			};						\
1051f0d69a9SSteven Rostedt 			______f.line = __LINE__;			\
1061f0d69a9SSteven Rostedt 			______r = unlikely_notrace(x);			\
1071f0d69a9SSteven Rostedt 			ftrace_likely_update(&______f, ______r, 0);	\
1081f0d69a9SSteven Rostedt 			______r;					\
1091f0d69a9SSteven Rostedt 		})
1101f0d69a9SSteven Rostedt 
1111f0d69a9SSteven Rostedt /*
1121f0d69a9SSteven Rostedt  * Using __builtin_constant_p(x) to ignore cases where the return
1131f0d69a9SSteven Rostedt  * value is always the same.  This idea is taken from a similar patch
1141f0d69a9SSteven Rostedt  * written by Daniel Walker.
1151f0d69a9SSteven Rostedt  */
1161f0d69a9SSteven Rostedt # ifndef likely
1171f0d69a9SSteven Rostedt #  define likely(x)	(__builtin_constant_p(x) ? !!(x) : likely_check(x))
1181f0d69a9SSteven Rostedt # endif
1191f0d69a9SSteven Rostedt # ifndef unlikely
1201f0d69a9SSteven Rostedt #  define unlikely(x)	(__builtin_constant_p(x) ? !!(x) : unlikely_check(x))
1211f0d69a9SSteven Rostedt # endif
1221f0d69a9SSteven Rostedt #else
1231da177e4SLinus Torvalds # define likely(x)	__builtin_expect(!!(x), 1)
1241da177e4SLinus Torvalds # define unlikely(x)	__builtin_expect(!!(x), 0)
1251f0d69a9SSteven Rostedt #endif
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /* Optimization barrier */
1281da177e4SLinus Torvalds #ifndef barrier
1291da177e4SLinus Torvalds # define barrier() __memory_barrier()
1301da177e4SLinus Torvalds #endif
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds #ifndef RELOC_HIDE
1331da177e4SLinus Torvalds # define RELOC_HIDE(ptr, off)					\
1341da177e4SLinus Torvalds   ({ unsigned long __ptr;					\
1351da177e4SLinus Torvalds      __ptr = (unsigned long) (ptr);				\
1361da177e4SLinus Torvalds     (typeof(ptr)) (__ptr + (off)); })
1371da177e4SLinus Torvalds #endif
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds #endif /* __KERNEL__ */
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds #endif /* __ASSEMBLY__ */
1421da177e4SLinus Torvalds 
1434f79c3ffSDavid Woodhouse #ifdef __KERNEL__
1441da177e4SLinus Torvalds /*
1451da177e4SLinus Torvalds  * Allow us to mark functions as 'deprecated' and have gcc emit a nice
1461da177e4SLinus Torvalds  * warning for each use, in hopes of speeding the functions removal.
1471da177e4SLinus Torvalds  * Usage is:
1481da177e4SLinus Torvalds  * 		int __deprecated foo(void)
1491da177e4SLinus Torvalds  */
1501da177e4SLinus Torvalds #ifndef __deprecated
1511da177e4SLinus Torvalds # define __deprecated		/* unimplemented */
1521da177e4SLinus Torvalds #endif
1531da177e4SLinus Torvalds 
154512345beSPaul E. McKenney #ifdef MODULE
155512345beSPaul E. McKenney #define __deprecated_for_modules __deprecated
156512345beSPaul E. McKenney #else
157512345beSPaul E. McKenney #define __deprecated_for_modules
158512345beSPaul E. McKenney #endif
159512345beSPaul E. McKenney 
1601da177e4SLinus Torvalds #ifndef __must_check
1611da177e4SLinus Torvalds #define __must_check
1621da177e4SLinus Torvalds #endif
1631da177e4SLinus Torvalds 
164cebc04baSAndrew Morton #ifndef CONFIG_ENABLE_MUST_CHECK
165cebc04baSAndrew Morton #undef __must_check
166cebc04baSAndrew Morton #define __must_check
167cebc04baSAndrew Morton #endif
168de488443SJeff Garzik #ifndef CONFIG_ENABLE_WARN_DEPRECATED
169de488443SJeff Garzik #undef __deprecated
170de488443SJeff Garzik #undef __deprecated_for_modules
171de488443SJeff Garzik #define __deprecated
172de488443SJeff Garzik #define __deprecated_for_modules
173de488443SJeff Garzik #endif
174cebc04baSAndrew Morton 
1751da177e4SLinus Torvalds /*
1761da177e4SLinus Torvalds  * Allow us to avoid 'defined but not used' warnings on functions and data,
1771da177e4SLinus Torvalds  * as well as force them to be emitted to the assembly file.
1781da177e4SLinus Torvalds  *
1790d7ebbbcSDavid Rientjes  * As of gcc 3.4, static functions that are not marked with attribute((used))
1800d7ebbbcSDavid Rientjes  * may be elided from the assembly file.  As of gcc 3.4, static data not so
1811da177e4SLinus Torvalds  * marked will not be elided, but this may change in a future gcc version.
1821da177e4SLinus Torvalds  *
1830d7ebbbcSDavid Rientjes  * NOTE: Because distributions shipped with a backported unit-at-a-time
1840d7ebbbcSDavid Rientjes  * compiler in gcc 3.3, we must define __used to be __attribute__((used))
1850d7ebbbcSDavid Rientjes  * for gcc >=3.3 instead of 3.4.
1860d7ebbbcSDavid Rientjes  *
1871da177e4SLinus Torvalds  * In prior versions of gcc, such functions and data would be emitted, but
1881da177e4SLinus Torvalds  * would be warned about except with attribute((unused)).
1890d7ebbbcSDavid Rientjes  *
1900d7ebbbcSDavid Rientjes  * Mark functions that are referenced only in inline assembly as __used so
1910d7ebbbcSDavid Rientjes  * the code is emitted even though it appears to be unreferenced.
1921da177e4SLinus Torvalds  */
1930d7ebbbcSDavid Rientjes #ifndef __used
1940d7ebbbcSDavid Rientjes # define __used			/* unimplemented */
1950d7ebbbcSDavid Rientjes #endif
1960d7ebbbcSDavid Rientjes 
1970d7ebbbcSDavid Rientjes #ifndef __maybe_unused
1980d7ebbbcSDavid Rientjes # define __maybe_unused		/* unimplemented */
1991da177e4SLinus Torvalds #endif
2001da177e4SLinus Torvalds 
201423bc7b2SDavid Woodhouse #ifndef noinline
202423bc7b2SDavid Woodhouse #define noinline
203423bc7b2SDavid Woodhouse #endif
204423bc7b2SDavid Woodhouse 
205735c4fb9SAndrew Morton /*
206735c4fb9SAndrew Morton  * Rather then using noinline to prevent stack consumption, use
207735c4fb9SAndrew Morton  * noinline_for_stack instead.  For documentaiton reasons.
208735c4fb9SAndrew Morton  */
209735c4fb9SAndrew Morton #define noinline_for_stack noinline
210735c4fb9SAndrew Morton 
211423bc7b2SDavid Woodhouse #ifndef __always_inline
212423bc7b2SDavid Woodhouse #define __always_inline inline
213423bc7b2SDavid Woodhouse #endif
214423bc7b2SDavid Woodhouse 
215423bc7b2SDavid Woodhouse #endif /* __KERNEL__ */
216423bc7b2SDavid Woodhouse 
2171da177e4SLinus Torvalds /*
2181da177e4SLinus Torvalds  * From the GCC manual:
2191da177e4SLinus Torvalds  *
2201da177e4SLinus Torvalds  * Many functions do not examine any values except their arguments,
2211da177e4SLinus Torvalds  * and have no effects except the return value.  Basically this is
2221da177e4SLinus Torvalds  * just slightly more strict class than the `pure' attribute above,
2231da177e4SLinus Torvalds  * since function is not allowed to read global memory.
2241da177e4SLinus Torvalds  *
2251da177e4SLinus Torvalds  * Note that a function that has pointer arguments and examines the
2261da177e4SLinus Torvalds  * data pointed to must _not_ be declared `const'.  Likewise, a
2271da177e4SLinus Torvalds  * function that calls a non-`const' function usually must not be
2281da177e4SLinus Torvalds  * `const'.  It does not make sense for a `const' function to return
2291da177e4SLinus Torvalds  * `void'.
2301da177e4SLinus Torvalds  */
2311da177e4SLinus Torvalds #ifndef __attribute_const__
2321da177e4SLinus Torvalds # define __attribute_const__	/* unimplemented */
2331da177e4SLinus Torvalds #endif
2341da177e4SLinus Torvalds 
235a586df06SAndi Kleen /*
236a586df06SAndi Kleen  * Tell gcc if a function is cold. The compiler will assume any path
237a586df06SAndi Kleen  * directly leading to the call is unlikely.
238a586df06SAndi Kleen  */
239a586df06SAndi Kleen 
240a586df06SAndi Kleen #ifndef __cold
241a586df06SAndi Kleen #define __cold
242a586df06SAndi Kleen #endif
243a586df06SAndi Kleen 
244f3fe866dSSam Ravnborg /* Simple shorthand for a section definition */
245f3fe866dSSam Ravnborg #ifndef __section
246f3fe866dSSam Ravnborg # define __section(S) __attribute__ ((__section__(#S)))
247f3fe866dSSam Ravnborg #endif
248f3fe866dSSam Ravnborg 
2499c3cdc1fSLinus Torvalds /*
2509c3cdc1fSLinus Torvalds  * Prevent the compiler from merging or refetching accesses.  The compiler
2519c3cdc1fSLinus Torvalds  * is also forbidden from reordering successive instances of ACCESS_ONCE(),
2529c3cdc1fSLinus Torvalds  * but only when the compiler is aware of some particular ordering.  One way
2539c3cdc1fSLinus Torvalds  * to make the compiler aware of ordering is to put the two invocations of
2549c3cdc1fSLinus Torvalds  * ACCESS_ONCE() in different C statements.
2559c3cdc1fSLinus Torvalds  *
2569c3cdc1fSLinus Torvalds  * This macro does absolutely -nothing- to prevent the CPU from reordering,
257ded00a56SPaul E. McKenney  * merging, or refetching absolutely anything at any time.  Its main intended
258ded00a56SPaul E. McKenney  * use is to mediate communication between process-level code and irq/NMI
259ded00a56SPaul E. McKenney  * handlers, all running on the same CPU.
2609c3cdc1fSLinus Torvalds  */
2619c3cdc1fSLinus Torvalds #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
2629c3cdc1fSLinus Torvalds 
2631da177e4SLinus Torvalds #endif /* __LINUX_COMPILER_H */
264