xref: /linux/include/linux/compiler.h (revision 38938c879eb0c39edf85d5164aa0cffe2874304c)
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 
39f153b821SLinus Torvalds #ifdef __GNUC__
40f153b821SLinus Torvalds #include <linux/compiler-gcc.h>
411da177e4SLinus Torvalds #endif
421da177e4SLinus Torvalds 
4328614889SSteven Rostedt #define notrace __attribute__((no_instrument_function))
4428614889SSteven Rostedt 
451da177e4SLinus Torvalds /* Intel compiler defines __GNUC__. So we will overwrite implementations
461da177e4SLinus Torvalds  * coming from above header files here
471da177e4SLinus Torvalds  */
481da177e4SLinus Torvalds #ifdef __INTEL_COMPILER
491da177e4SLinus Torvalds # include <linux/compiler-intel.h>
501da177e4SLinus Torvalds #endif
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  * Generic compiler-dependent macros required for kernel
541da177e4SLinus Torvalds  * build go below this comment. Actual compiler/compiler version
551da177e4SLinus Torvalds  * specific implementations come from the above header files
561da177e4SLinus Torvalds  */
571da177e4SLinus Torvalds 
582ed84eebSSteven Rostedt struct ftrace_branch_data {
591f0d69a9SSteven Rostedt 	const char *func;
601f0d69a9SSteven Rostedt 	const char *file;
611f0d69a9SSteven Rostedt 	unsigned line;
622bcd521aSSteven Rostedt 	union {
632bcd521aSSteven Rostedt 		struct {
641f0d69a9SSteven Rostedt 			unsigned long correct;
651f0d69a9SSteven Rostedt 			unsigned long incorrect;
661f0d69a9SSteven Rostedt 		};
672bcd521aSSteven Rostedt 		struct {
682bcd521aSSteven Rostedt 			unsigned long miss;
692bcd521aSSteven Rostedt 			unsigned long hit;
702bcd521aSSteven Rostedt 		};
7197e7e4f3SWitold Baryluk 		unsigned long miss_hit[2];
722bcd521aSSteven Rostedt 	};
732bcd521aSSteven Rostedt };
742ed84eebSSteven Rostedt 
752ed84eebSSteven Rostedt /*
762ed84eebSSteven Rostedt  * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
772ed84eebSSteven Rostedt  * to disable branch tracing on a per file basis.
782ed84eebSSteven Rostedt  */
79d9ad8bc0SBart Van Assche #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
80d9ad8bc0SBart Van Assche     && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
812ed84eebSSteven Rostedt void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
821f0d69a9SSteven Rostedt 
831f0d69a9SSteven Rostedt #define likely_notrace(x)	__builtin_expect(!!(x), 1)
841f0d69a9SSteven Rostedt #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
851f0d69a9SSteven Rostedt 
8645b79749SSteven Rostedt #define __branch_check__(x, expect) ({					\
871f0d69a9SSteven Rostedt 			int ______r;					\
882ed84eebSSteven Rostedt 			static struct ftrace_branch_data		\
891f0d69a9SSteven Rostedt 				__attribute__((__aligned__(4)))		\
9045b79749SSteven Rostedt 				__attribute__((section("_ftrace_annotated_branch"))) \
911f0d69a9SSteven Rostedt 				______f = {				\
921f0d69a9SSteven Rostedt 				.func = __func__,			\
931f0d69a9SSteven Rostedt 				.file = __FILE__,			\
941f0d69a9SSteven Rostedt 				.line = __LINE__,			\
951f0d69a9SSteven Rostedt 			};						\
961f0d69a9SSteven Rostedt 			______r = likely_notrace(x);			\
9745b79749SSteven Rostedt 			ftrace_likely_update(&______f, ______r, expect); \
981f0d69a9SSteven Rostedt 			______r;					\
991f0d69a9SSteven Rostedt 		})
1001f0d69a9SSteven Rostedt 
1011f0d69a9SSteven Rostedt /*
1021f0d69a9SSteven Rostedt  * Using __builtin_constant_p(x) to ignore cases where the return
1031f0d69a9SSteven Rostedt  * value is always the same.  This idea is taken from a similar patch
1041f0d69a9SSteven Rostedt  * written by Daniel Walker.
1051f0d69a9SSteven Rostedt  */
1061f0d69a9SSteven Rostedt # ifndef likely
10745b79749SSteven Rostedt #  define likely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1))
1081f0d69a9SSteven Rostedt # endif
1091f0d69a9SSteven Rostedt # ifndef unlikely
11045b79749SSteven Rostedt #  define unlikely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0))
1111f0d69a9SSteven Rostedt # endif
1122bcd521aSSteven Rostedt 
1132bcd521aSSteven Rostedt #ifdef CONFIG_PROFILE_ALL_BRANCHES
1142bcd521aSSteven Rostedt /*
1152bcd521aSSteven Rostedt  * "Define 'is'", Bill Clinton
1162bcd521aSSteven Rostedt  * "Define 'if'", Steven Rostedt
1172bcd521aSSteven Rostedt  */
118ab3c9c68SLinus Torvalds #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
119ab3c9c68SLinus Torvalds #define __trace_if(cond) \
120ab3c9c68SLinus Torvalds 	if (__builtin_constant_p((cond)) ? !!(cond) :			\
1212bcd521aSSteven Rostedt 	({								\
1222bcd521aSSteven Rostedt 		int ______r;						\
1232bcd521aSSteven Rostedt 		static struct ftrace_branch_data			\
1242bcd521aSSteven Rostedt 			__attribute__((__aligned__(4)))			\
1252bcd521aSSteven Rostedt 			__attribute__((section("_ftrace_branch")))	\
1262bcd521aSSteven Rostedt 			______f = {					\
1272bcd521aSSteven Rostedt 				.func = __func__,			\
1282bcd521aSSteven Rostedt 				.file = __FILE__,			\
1292bcd521aSSteven Rostedt 				.line = __LINE__,			\
1302bcd521aSSteven Rostedt 			};						\
1312bcd521aSSteven Rostedt 		______r = !!(cond);					\
13297e7e4f3SWitold Baryluk 		______f.miss_hit[______r]++;					\
1332bcd521aSSteven Rostedt 		______r;						\
1342bcd521aSSteven Rostedt 	}))
1352bcd521aSSteven Rostedt #endif /* CONFIG_PROFILE_ALL_BRANCHES */
1362bcd521aSSteven Rostedt 
1371f0d69a9SSteven Rostedt #else
1381da177e4SLinus Torvalds # define likely(x)	__builtin_expect(!!(x), 1)
1391da177e4SLinus Torvalds # define unlikely(x)	__builtin_expect(!!(x), 0)
1401f0d69a9SSteven Rostedt #endif
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /* Optimization barrier */
1431da177e4SLinus Torvalds #ifndef barrier
1441da177e4SLinus Torvalds # define barrier() __memory_barrier()
1451da177e4SLinus Torvalds #endif
1461da177e4SLinus Torvalds 
147*38938c87SDavid Daney /* Unreachable code */
148*38938c87SDavid Daney #ifndef unreachable
149*38938c87SDavid Daney # define unreachable() do { } while (1)
150*38938c87SDavid Daney #endif
151*38938c87SDavid Daney 
1521da177e4SLinus Torvalds #ifndef RELOC_HIDE
1531da177e4SLinus Torvalds # define RELOC_HIDE(ptr, off)					\
1541da177e4SLinus Torvalds   ({ unsigned long __ptr;					\
1551da177e4SLinus Torvalds      __ptr = (unsigned long) (ptr);				\
1561da177e4SLinus Torvalds     (typeof(ptr)) (__ptr + (off)); })
1571da177e4SLinus Torvalds #endif
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds #endif /* __KERNEL__ */
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds #endif /* __ASSEMBLY__ */
1621da177e4SLinus Torvalds 
1634f79c3ffSDavid Woodhouse #ifdef __KERNEL__
1641da177e4SLinus Torvalds /*
1651da177e4SLinus Torvalds  * Allow us to mark functions as 'deprecated' and have gcc emit a nice
1661da177e4SLinus Torvalds  * warning for each use, in hopes of speeding the functions removal.
1671da177e4SLinus Torvalds  * Usage is:
1681da177e4SLinus Torvalds  * 		int __deprecated foo(void)
1691da177e4SLinus Torvalds  */
1701da177e4SLinus Torvalds #ifndef __deprecated
1711da177e4SLinus Torvalds # define __deprecated		/* unimplemented */
1721da177e4SLinus Torvalds #endif
1731da177e4SLinus Torvalds 
174512345beSPaul E. McKenney #ifdef MODULE
175512345beSPaul E. McKenney #define __deprecated_for_modules __deprecated
176512345beSPaul E. McKenney #else
177512345beSPaul E. McKenney #define __deprecated_for_modules
178512345beSPaul E. McKenney #endif
179512345beSPaul E. McKenney 
1801da177e4SLinus Torvalds #ifndef __must_check
1811da177e4SLinus Torvalds #define __must_check
1821da177e4SLinus Torvalds #endif
1831da177e4SLinus Torvalds 
184cebc04baSAndrew Morton #ifndef CONFIG_ENABLE_MUST_CHECK
185cebc04baSAndrew Morton #undef __must_check
186cebc04baSAndrew Morton #define __must_check
187cebc04baSAndrew Morton #endif
188de488443SJeff Garzik #ifndef CONFIG_ENABLE_WARN_DEPRECATED
189de488443SJeff Garzik #undef __deprecated
190de488443SJeff Garzik #undef __deprecated_for_modules
191de488443SJeff Garzik #define __deprecated
192de488443SJeff Garzik #define __deprecated_for_modules
193de488443SJeff Garzik #endif
194cebc04baSAndrew Morton 
1951da177e4SLinus Torvalds /*
1961da177e4SLinus Torvalds  * Allow us to avoid 'defined but not used' warnings on functions and data,
1971da177e4SLinus Torvalds  * as well as force them to be emitted to the assembly file.
1981da177e4SLinus Torvalds  *
1990d7ebbbcSDavid Rientjes  * As of gcc 3.4, static functions that are not marked with attribute((used))
2000d7ebbbcSDavid Rientjes  * may be elided from the assembly file.  As of gcc 3.4, static data not so
2011da177e4SLinus Torvalds  * marked will not be elided, but this may change in a future gcc version.
2021da177e4SLinus Torvalds  *
2030d7ebbbcSDavid Rientjes  * NOTE: Because distributions shipped with a backported unit-at-a-time
2040d7ebbbcSDavid Rientjes  * compiler in gcc 3.3, we must define __used to be __attribute__((used))
2050d7ebbbcSDavid Rientjes  * for gcc >=3.3 instead of 3.4.
2060d7ebbbcSDavid Rientjes  *
2071da177e4SLinus Torvalds  * In prior versions of gcc, such functions and data would be emitted, but
2081da177e4SLinus Torvalds  * would be warned about except with attribute((unused)).
2090d7ebbbcSDavid Rientjes  *
2100d7ebbbcSDavid Rientjes  * Mark functions that are referenced only in inline assembly as __used so
2110d7ebbbcSDavid Rientjes  * the code is emitted even though it appears to be unreferenced.
2121da177e4SLinus Torvalds  */
2130d7ebbbcSDavid Rientjes #ifndef __used
2140d7ebbbcSDavid Rientjes # define __used			/* unimplemented */
2150d7ebbbcSDavid Rientjes #endif
2160d7ebbbcSDavid Rientjes 
2170d7ebbbcSDavid Rientjes #ifndef __maybe_unused
2180d7ebbbcSDavid Rientjes # define __maybe_unused		/* unimplemented */
2191da177e4SLinus Torvalds #endif
2201da177e4SLinus Torvalds 
221423bc7b2SDavid Woodhouse #ifndef noinline
222423bc7b2SDavid Woodhouse #define noinline
223423bc7b2SDavid Woodhouse #endif
224423bc7b2SDavid Woodhouse 
225735c4fb9SAndrew Morton /*
226735c4fb9SAndrew Morton  * Rather then using noinline to prevent stack consumption, use
227735c4fb9SAndrew Morton  * noinline_for_stack instead.  For documentaiton reasons.
228735c4fb9SAndrew Morton  */
229735c4fb9SAndrew Morton #define noinline_for_stack noinline
230735c4fb9SAndrew Morton 
231423bc7b2SDavid Woodhouse #ifndef __always_inline
232423bc7b2SDavid Woodhouse #define __always_inline inline
233423bc7b2SDavid Woodhouse #endif
234423bc7b2SDavid Woodhouse 
235423bc7b2SDavid Woodhouse #endif /* __KERNEL__ */
236423bc7b2SDavid Woodhouse 
2371da177e4SLinus Torvalds /*
2381da177e4SLinus Torvalds  * From the GCC manual:
2391da177e4SLinus Torvalds  *
2401da177e4SLinus Torvalds  * Many functions do not examine any values except their arguments,
2411da177e4SLinus Torvalds  * and have no effects except the return value.  Basically this is
2421da177e4SLinus Torvalds  * just slightly more strict class than the `pure' attribute above,
2431da177e4SLinus Torvalds  * since function is not allowed to read global memory.
2441da177e4SLinus Torvalds  *
2451da177e4SLinus Torvalds  * Note that a function that has pointer arguments and examines the
2461da177e4SLinus Torvalds  * data pointed to must _not_ be declared `const'.  Likewise, a
2471da177e4SLinus Torvalds  * function that calls a non-`const' function usually must not be
2481da177e4SLinus Torvalds  * `const'.  It does not make sense for a `const' function to return
2491da177e4SLinus Torvalds  * `void'.
2501da177e4SLinus Torvalds  */
2511da177e4SLinus Torvalds #ifndef __attribute_const__
2521da177e4SLinus Torvalds # define __attribute_const__	/* unimplemented */
2531da177e4SLinus Torvalds #endif
2541da177e4SLinus Torvalds 
255a586df06SAndi Kleen /*
256a586df06SAndi Kleen  * Tell gcc if a function is cold. The compiler will assume any path
257a586df06SAndi Kleen  * directly leading to the call is unlikely.
258a586df06SAndi Kleen  */
259a586df06SAndi Kleen 
260a586df06SAndi Kleen #ifndef __cold
261a586df06SAndi Kleen #define __cold
262a586df06SAndi Kleen #endif
263a586df06SAndi Kleen 
264f3fe866dSSam Ravnborg /* Simple shorthand for a section definition */
265f3fe866dSSam Ravnborg #ifndef __section
266f3fe866dSSam Ravnborg # define __section(S) __attribute__ ((__section__(#S)))
267f3fe866dSSam Ravnborg #endif
268f3fe866dSSam Ravnborg 
269d2c123c2SRusty Russell /* Are two types/vars the same type (ignoring qualifiers)? */
270d2c123c2SRusty Russell #ifndef __same_type
271d2c123c2SRusty Russell # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
272d2c123c2SRusty Russell #endif
273d2c123c2SRusty Russell 
2749c3cdc1fSLinus Torvalds /*
2759c3cdc1fSLinus Torvalds  * Prevent the compiler from merging or refetching accesses.  The compiler
2769c3cdc1fSLinus Torvalds  * is also forbidden from reordering successive instances of ACCESS_ONCE(),
2779c3cdc1fSLinus Torvalds  * but only when the compiler is aware of some particular ordering.  One way
2789c3cdc1fSLinus Torvalds  * to make the compiler aware of ordering is to put the two invocations of
2799c3cdc1fSLinus Torvalds  * ACCESS_ONCE() in different C statements.
2809c3cdc1fSLinus Torvalds  *
2819c3cdc1fSLinus Torvalds  * This macro does absolutely -nothing- to prevent the CPU from reordering,
282ded00a56SPaul E. McKenney  * merging, or refetching absolutely anything at any time.  Its main intended
283ded00a56SPaul E. McKenney  * use is to mediate communication between process-level code and irq/NMI
284ded00a56SPaul E. McKenney  * handlers, all running on the same CPU.
2859c3cdc1fSLinus Torvalds  */
2869c3cdc1fSLinus Torvalds #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
2879c3cdc1fSLinus Torvalds 
2881da177e4SLinus Torvalds #endif /* __LINUX_COMPILER_H */
289