xref: /linux/include/linux/compiler.h (revision 6f33d58794ef4cef4b2c706029810f9688bd3026)
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)))
8e0fdb0e0SRusty Russell # define __kernel	__attribute__((address_space(0)))
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)
18e0fdb0e0SRusty Russell # define __percpu	__attribute__((noderef, address_space(3)))
19ca5ecddfSPaul E. McKenney #ifdef CONFIG_SPARSE_RCU_POINTER
20ca5ecddfSPaul E. McKenney # define __rcu		__attribute__((noderef, address_space(4)))
21ca5ecddfSPaul E. McKenney #else
2271d1d5c7SPaul E. McKenney # define __rcu
23ca5ecddfSPaul E. McKenney #endif
24c47ffe3dSAl Viro extern void __chk_user_ptr(const volatile void __user *);
25c47ffe3dSAl Viro extern void __chk_io_ptr(const volatile void __iomem *);
261da177e4SLinus Torvalds #else
271da177e4SLinus Torvalds # define __user
281da177e4SLinus Torvalds # define __kernel
291da177e4SLinus Torvalds # define __safe
301da177e4SLinus Torvalds # define __force
311da177e4SLinus Torvalds # define __nocast
321da177e4SLinus Torvalds # define __iomem
331da177e4SLinus Torvalds # define __chk_user_ptr(x) (void)0
341da177e4SLinus Torvalds # define __chk_io_ptr(x) (void)0
351da177e4SLinus Torvalds # define __builtin_warning(x, y...) (1)
361da177e4SLinus Torvalds # define __acquires(x)
371da177e4SLinus Torvalds # define __releases(x)
381da177e4SLinus Torvalds # define __acquire(x) (void)0
391da177e4SLinus Torvalds # define __release(x) (void)0
40dcc8e559SJosh Triplett # define __cond_lock(x,c) (c)
41e0fdb0e0SRusty Russell # define __percpu
4271d1d5c7SPaul E. McKenney # define __rcu
431da177e4SLinus Torvalds #endif
441da177e4SLinus Torvalds 
45*6f33d587SRusty Russell /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
46*6f33d587SRusty Russell #define ___PASTE(a,b) a##b
47*6f33d587SRusty Russell #define __PASTE(a,b) ___PASTE(a,b)
48*6f33d587SRusty Russell 
491da177e4SLinus Torvalds #ifdef __KERNEL__
501da177e4SLinus Torvalds 
51f153b821SLinus Torvalds #ifdef __GNUC__
52f153b821SLinus Torvalds #include <linux/compiler-gcc.h>
531da177e4SLinus Torvalds #endif
541da177e4SLinus Torvalds 
5528614889SSteven Rostedt #define notrace __attribute__((no_instrument_function))
5628614889SSteven Rostedt 
571da177e4SLinus Torvalds /* Intel compiler defines __GNUC__. So we will overwrite implementations
581da177e4SLinus Torvalds  * coming from above header files here
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds #ifdef __INTEL_COMPILER
611da177e4SLinus Torvalds # include <linux/compiler-intel.h>
621da177e4SLinus Torvalds #endif
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds  * Generic compiler-dependent macros required for kernel
661da177e4SLinus Torvalds  * build go below this comment. Actual compiler/compiler version
671da177e4SLinus Torvalds  * specific implementations come from the above header files
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds 
702ed84eebSSteven Rostedt struct ftrace_branch_data {
711f0d69a9SSteven Rostedt 	const char *func;
721f0d69a9SSteven Rostedt 	const char *file;
731f0d69a9SSteven Rostedt 	unsigned line;
742bcd521aSSteven Rostedt 	union {
752bcd521aSSteven Rostedt 		struct {
761f0d69a9SSteven Rostedt 			unsigned long correct;
771f0d69a9SSteven Rostedt 			unsigned long incorrect;
781f0d69a9SSteven Rostedt 		};
792bcd521aSSteven Rostedt 		struct {
802bcd521aSSteven Rostedt 			unsigned long miss;
812bcd521aSSteven Rostedt 			unsigned long hit;
822bcd521aSSteven Rostedt 		};
8397e7e4f3SWitold Baryluk 		unsigned long miss_hit[2];
842bcd521aSSteven Rostedt 	};
852bcd521aSSteven Rostedt };
862ed84eebSSteven Rostedt 
872ed84eebSSteven Rostedt /*
882ed84eebSSteven Rostedt  * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
892ed84eebSSteven Rostedt  * to disable branch tracing on a per file basis.
902ed84eebSSteven Rostedt  */
91d9ad8bc0SBart Van Assche #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
92d9ad8bc0SBart Van Assche     && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
932ed84eebSSteven Rostedt void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
941f0d69a9SSteven Rostedt 
951f0d69a9SSteven Rostedt #define likely_notrace(x)	__builtin_expect(!!(x), 1)
961f0d69a9SSteven Rostedt #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
971f0d69a9SSteven Rostedt 
9845b79749SSteven Rostedt #define __branch_check__(x, expect) ({					\
991f0d69a9SSteven Rostedt 			int ______r;					\
1002ed84eebSSteven Rostedt 			static struct ftrace_branch_data		\
1011f0d69a9SSteven Rostedt 				__attribute__((__aligned__(4)))		\
10245b79749SSteven Rostedt 				__attribute__((section("_ftrace_annotated_branch"))) \
1031f0d69a9SSteven Rostedt 				______f = {				\
1041f0d69a9SSteven Rostedt 				.func = __func__,			\
1051f0d69a9SSteven Rostedt 				.file = __FILE__,			\
1061f0d69a9SSteven Rostedt 				.line = __LINE__,			\
1071f0d69a9SSteven Rostedt 			};						\
1081f0d69a9SSteven Rostedt 			______r = likely_notrace(x);			\
10945b79749SSteven Rostedt 			ftrace_likely_update(&______f, ______r, expect); \
1101f0d69a9SSteven Rostedt 			______r;					\
1111f0d69a9SSteven Rostedt 		})
1121f0d69a9SSteven Rostedt 
1131f0d69a9SSteven Rostedt /*
1141f0d69a9SSteven Rostedt  * Using __builtin_constant_p(x) to ignore cases where the return
1151f0d69a9SSteven Rostedt  * value is always the same.  This idea is taken from a similar patch
1161f0d69a9SSteven Rostedt  * written by Daniel Walker.
1171f0d69a9SSteven Rostedt  */
1181f0d69a9SSteven Rostedt # ifndef likely
11945b79749SSteven Rostedt #  define likely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1))
1201f0d69a9SSteven Rostedt # endif
1211f0d69a9SSteven Rostedt # ifndef unlikely
12245b79749SSteven Rostedt #  define unlikely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0))
1231f0d69a9SSteven Rostedt # endif
1242bcd521aSSteven Rostedt 
1252bcd521aSSteven Rostedt #ifdef CONFIG_PROFILE_ALL_BRANCHES
1262bcd521aSSteven Rostedt /*
1272bcd521aSSteven Rostedt  * "Define 'is'", Bill Clinton
1282bcd521aSSteven Rostedt  * "Define 'if'", Steven Rostedt
1292bcd521aSSteven Rostedt  */
130ab3c9c68SLinus Torvalds #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
131ab3c9c68SLinus Torvalds #define __trace_if(cond) \
132ab3c9c68SLinus Torvalds 	if (__builtin_constant_p((cond)) ? !!(cond) :			\
1332bcd521aSSteven Rostedt 	({								\
1342bcd521aSSteven Rostedt 		int ______r;						\
1352bcd521aSSteven Rostedt 		static struct ftrace_branch_data			\
1362bcd521aSSteven Rostedt 			__attribute__((__aligned__(4)))			\
1372bcd521aSSteven Rostedt 			__attribute__((section("_ftrace_branch")))	\
1382bcd521aSSteven Rostedt 			______f = {					\
1392bcd521aSSteven Rostedt 				.func = __func__,			\
1402bcd521aSSteven Rostedt 				.file = __FILE__,			\
1412bcd521aSSteven Rostedt 				.line = __LINE__,			\
1422bcd521aSSteven Rostedt 			};						\
1432bcd521aSSteven Rostedt 		______r = !!(cond);					\
14497e7e4f3SWitold Baryluk 		______f.miss_hit[______r]++;					\
1452bcd521aSSteven Rostedt 		______r;						\
1462bcd521aSSteven Rostedt 	}))
1472bcd521aSSteven Rostedt #endif /* CONFIG_PROFILE_ALL_BRANCHES */
1482bcd521aSSteven Rostedt 
1491f0d69a9SSteven Rostedt #else
1501da177e4SLinus Torvalds # define likely(x)	__builtin_expect(!!(x), 1)
1511da177e4SLinus Torvalds # define unlikely(x)	__builtin_expect(!!(x), 0)
1521f0d69a9SSteven Rostedt #endif
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds /* Optimization barrier */
1551da177e4SLinus Torvalds #ifndef barrier
1561da177e4SLinus Torvalds # define barrier() __memory_barrier()
1571da177e4SLinus Torvalds #endif
1581da177e4SLinus Torvalds 
15938938c87SDavid Daney /* Unreachable code */
16038938c87SDavid Daney #ifndef unreachable
16138938c87SDavid Daney # define unreachable() do { } while (1)
16238938c87SDavid Daney #endif
16338938c87SDavid Daney 
1641da177e4SLinus Torvalds #ifndef RELOC_HIDE
1651da177e4SLinus Torvalds # define RELOC_HIDE(ptr, off)					\
1661da177e4SLinus Torvalds   ({ unsigned long __ptr;					\
1671da177e4SLinus Torvalds      __ptr = (unsigned long) (ptr);				\
1681da177e4SLinus Torvalds     (typeof(ptr)) (__ptr + (off)); })
1691da177e4SLinus Torvalds #endif
1701da177e4SLinus Torvalds 
171*6f33d587SRusty Russell /* Not-quite-unique ID. */
172*6f33d587SRusty Russell #ifndef __UNIQUE_ID
173*6f33d587SRusty Russell # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
174*6f33d587SRusty Russell #endif
175*6f33d587SRusty Russell 
1761da177e4SLinus Torvalds #endif /* __KERNEL__ */
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds #endif /* __ASSEMBLY__ */
1791da177e4SLinus Torvalds 
1804f79c3ffSDavid Woodhouse #ifdef __KERNEL__
1811da177e4SLinus Torvalds /*
1821da177e4SLinus Torvalds  * Allow us to mark functions as 'deprecated' and have gcc emit a nice
1831da177e4SLinus Torvalds  * warning for each use, in hopes of speeding the functions removal.
1841da177e4SLinus Torvalds  * Usage is:
1851da177e4SLinus Torvalds  * 		int __deprecated foo(void)
1861da177e4SLinus Torvalds  */
1871da177e4SLinus Torvalds #ifndef __deprecated
1881da177e4SLinus Torvalds # define __deprecated		/* unimplemented */
1891da177e4SLinus Torvalds #endif
1901da177e4SLinus Torvalds 
191512345beSPaul E. McKenney #ifdef MODULE
192512345beSPaul E. McKenney #define __deprecated_for_modules __deprecated
193512345beSPaul E. McKenney #else
194512345beSPaul E. McKenney #define __deprecated_for_modules
195512345beSPaul E. McKenney #endif
196512345beSPaul E. McKenney 
1971da177e4SLinus Torvalds #ifndef __must_check
1981da177e4SLinus Torvalds #define __must_check
1991da177e4SLinus Torvalds #endif
2001da177e4SLinus Torvalds 
201cebc04baSAndrew Morton #ifndef CONFIG_ENABLE_MUST_CHECK
202cebc04baSAndrew Morton #undef __must_check
203cebc04baSAndrew Morton #define __must_check
204cebc04baSAndrew Morton #endif
205de488443SJeff Garzik #ifndef CONFIG_ENABLE_WARN_DEPRECATED
206de488443SJeff Garzik #undef __deprecated
207de488443SJeff Garzik #undef __deprecated_for_modules
208de488443SJeff Garzik #define __deprecated
209de488443SJeff Garzik #define __deprecated_for_modules
210de488443SJeff Garzik #endif
211cebc04baSAndrew Morton 
2121da177e4SLinus Torvalds /*
2131da177e4SLinus Torvalds  * Allow us to avoid 'defined but not used' warnings on functions and data,
2141da177e4SLinus Torvalds  * as well as force them to be emitted to the assembly file.
2151da177e4SLinus Torvalds  *
2160d7ebbbcSDavid Rientjes  * As of gcc 3.4, static functions that are not marked with attribute((used))
2170d7ebbbcSDavid Rientjes  * may be elided from the assembly file.  As of gcc 3.4, static data not so
2181da177e4SLinus Torvalds  * marked will not be elided, but this may change in a future gcc version.
2191da177e4SLinus Torvalds  *
2200d7ebbbcSDavid Rientjes  * NOTE: Because distributions shipped with a backported unit-at-a-time
2210d7ebbbcSDavid Rientjes  * compiler in gcc 3.3, we must define __used to be __attribute__((used))
2220d7ebbbcSDavid Rientjes  * for gcc >=3.3 instead of 3.4.
2230d7ebbbcSDavid Rientjes  *
2241da177e4SLinus Torvalds  * In prior versions of gcc, such functions and data would be emitted, but
2251da177e4SLinus Torvalds  * would be warned about except with attribute((unused)).
2260d7ebbbcSDavid Rientjes  *
2270d7ebbbcSDavid Rientjes  * Mark functions that are referenced only in inline assembly as __used so
2280d7ebbbcSDavid Rientjes  * the code is emitted even though it appears to be unreferenced.
2291da177e4SLinus Torvalds  */
2300d7ebbbcSDavid Rientjes #ifndef __used
2310d7ebbbcSDavid Rientjes # define __used			/* unimplemented */
2320d7ebbbcSDavid Rientjes #endif
2330d7ebbbcSDavid Rientjes 
2340d7ebbbcSDavid Rientjes #ifndef __maybe_unused
2350d7ebbbcSDavid Rientjes # define __maybe_unused		/* unimplemented */
2361da177e4SLinus Torvalds #endif
2371da177e4SLinus Torvalds 
2387b2a3513SLi Zefan #ifndef __always_unused
2397b2a3513SLi Zefan # define __always_unused	/* unimplemented */
2407b2a3513SLi Zefan #endif
2417b2a3513SLi Zefan 
242423bc7b2SDavid Woodhouse #ifndef noinline
243423bc7b2SDavid Woodhouse #define noinline
244423bc7b2SDavid Woodhouse #endif
245423bc7b2SDavid Woodhouse 
246735c4fb9SAndrew Morton /*
247735c4fb9SAndrew Morton  * Rather then using noinline to prevent stack consumption, use
248e6be0c9eSAlexander Stein  * noinline_for_stack instead.  For documentation reasons.
249735c4fb9SAndrew Morton  */
250735c4fb9SAndrew Morton #define noinline_for_stack noinline
251735c4fb9SAndrew Morton 
252423bc7b2SDavid Woodhouse #ifndef __always_inline
253423bc7b2SDavid Woodhouse #define __always_inline inline
254423bc7b2SDavid Woodhouse #endif
255423bc7b2SDavid Woodhouse 
256423bc7b2SDavid Woodhouse #endif /* __KERNEL__ */
257423bc7b2SDavid Woodhouse 
2581da177e4SLinus Torvalds /*
2591da177e4SLinus Torvalds  * From the GCC manual:
2601da177e4SLinus Torvalds  *
2611da177e4SLinus Torvalds  * Many functions do not examine any values except their arguments,
2621da177e4SLinus Torvalds  * and have no effects except the return value.  Basically this is
2631da177e4SLinus Torvalds  * just slightly more strict class than the `pure' attribute above,
2641da177e4SLinus Torvalds  * since function is not allowed to read global memory.
2651da177e4SLinus Torvalds  *
2661da177e4SLinus Torvalds  * Note that a function that has pointer arguments and examines the
2671da177e4SLinus Torvalds  * data pointed to must _not_ be declared `const'.  Likewise, a
2681da177e4SLinus Torvalds  * function that calls a non-`const' function usually must not be
2691da177e4SLinus Torvalds  * `const'.  It does not make sense for a `const' function to return
2701da177e4SLinus Torvalds  * `void'.
2711da177e4SLinus Torvalds  */
2721da177e4SLinus Torvalds #ifndef __attribute_const__
2731da177e4SLinus Torvalds # define __attribute_const__	/* unimplemented */
2741da177e4SLinus Torvalds #endif
2751da177e4SLinus Torvalds 
276a586df06SAndi Kleen /*
277a586df06SAndi Kleen  * Tell gcc if a function is cold. The compiler will assume any path
278a586df06SAndi Kleen  * directly leading to the call is unlikely.
279a586df06SAndi Kleen  */
280a586df06SAndi Kleen 
281a586df06SAndi Kleen #ifndef __cold
282a586df06SAndi Kleen #define __cold
283a586df06SAndi Kleen #endif
284a586df06SAndi Kleen 
285f3fe866dSSam Ravnborg /* Simple shorthand for a section definition */
286f3fe866dSSam Ravnborg #ifndef __section
287f3fe866dSSam Ravnborg # define __section(S) __attribute__ ((__section__(#S)))
288f3fe866dSSam Ravnborg #endif
289f3fe866dSSam Ravnborg 
2909a858dc7SAndi Kleen #ifndef __visible
2919a858dc7SAndi Kleen #define __visible
2929a858dc7SAndi Kleen #endif
2939a858dc7SAndi Kleen 
294d2c123c2SRusty Russell /* Are two types/vars the same type (ignoring qualifiers)? */
295d2c123c2SRusty Russell #ifndef __same_type
296d2c123c2SRusty Russell # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
297d2c123c2SRusty Russell #endif
298d2c123c2SRusty Russell 
2999f0cf4adSArjan van de Ven /* Compile time object size, -1 for unknown */
3009f0cf4adSArjan van de Ven #ifndef __compiletime_object_size
3019f0cf4adSArjan van de Ven # define __compiletime_object_size(obj) -1
3029f0cf4adSArjan van de Ven #endif
3034a312769SArjan van de Ven #ifndef __compiletime_warning
3044a312769SArjan van de Ven # define __compiletime_warning(message)
3054a312769SArjan van de Ven #endif
30663312b6aSArjan van de Ven #ifndef __compiletime_error
30763312b6aSArjan van de Ven # define __compiletime_error(message)
30863312b6aSArjan van de Ven #endif
3091399ff86SDavid Daney #ifndef __linktime_error
3101399ff86SDavid Daney # define __linktime_error(message)
3111399ff86SDavid Daney #endif
3129c3cdc1fSLinus Torvalds /*
3139c3cdc1fSLinus Torvalds  * Prevent the compiler from merging or refetching accesses.  The compiler
3149c3cdc1fSLinus Torvalds  * is also forbidden from reordering successive instances of ACCESS_ONCE(),
3159c3cdc1fSLinus Torvalds  * but only when the compiler is aware of some particular ordering.  One way
3169c3cdc1fSLinus Torvalds  * to make the compiler aware of ordering is to put the two invocations of
3179c3cdc1fSLinus Torvalds  * ACCESS_ONCE() in different C statements.
3189c3cdc1fSLinus Torvalds  *
3199c3cdc1fSLinus Torvalds  * This macro does absolutely -nothing- to prevent the CPU from reordering,
320ded00a56SPaul E. McKenney  * merging, or refetching absolutely anything at any time.  Its main intended
321ded00a56SPaul E. McKenney  * use is to mediate communication between process-level code and irq/NMI
322ded00a56SPaul E. McKenney  * handlers, all running on the same CPU.
3239c3cdc1fSLinus Torvalds  */
3249c3cdc1fSLinus Torvalds #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
3259c3cdc1fSLinus Torvalds 
3261da177e4SLinus Torvalds #endif /* __LINUX_COMPILER_H */
327