xref: /linux/include/linux/compiler.h (revision 2bcd521a684cc94befbe2ce7d5b613c841b0d304)
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 
622ed84eebSSteven Rostedt struct ftrace_branch_data {
631f0d69a9SSteven Rostedt 	const char *func;
641f0d69a9SSteven Rostedt 	const char *file;
651f0d69a9SSteven Rostedt 	unsigned line;
66*2bcd521aSSteven Rostedt 	union {
67*2bcd521aSSteven Rostedt 		struct {
681f0d69a9SSteven Rostedt 			unsigned long correct;
691f0d69a9SSteven Rostedt 			unsigned long incorrect;
701f0d69a9SSteven Rostedt 		};
71*2bcd521aSSteven Rostedt 		struct {
72*2bcd521aSSteven Rostedt 			unsigned long miss;
73*2bcd521aSSteven Rostedt 			unsigned long hit;
74*2bcd521aSSteven Rostedt 		};
75*2bcd521aSSteven Rostedt 	};
76*2bcd521aSSteven Rostedt };
772ed84eebSSteven Rostedt 
782ed84eebSSteven Rostedt /*
792ed84eebSSteven Rostedt  * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
802ed84eebSSteven Rostedt  * to disable branch tracing on a per file basis.
812ed84eebSSteven Rostedt  */
822ed84eebSSteven Rostedt #if defined(CONFIG_TRACE_BRANCH_PROFILING) && !defined(DISABLE_BRANCH_PROFILING)
832ed84eebSSteven Rostedt void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
841f0d69a9SSteven Rostedt 
851f0d69a9SSteven Rostedt #define likely_notrace(x)	__builtin_expect(!!(x), 1)
861f0d69a9SSteven Rostedt #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
871f0d69a9SSteven Rostedt 
8845b79749SSteven Rostedt #define __branch_check__(x, expect) ({					\
891f0d69a9SSteven Rostedt 			int ______r;					\
902ed84eebSSteven Rostedt 			static struct ftrace_branch_data		\
911f0d69a9SSteven Rostedt 				__attribute__((__aligned__(4)))		\
9245b79749SSteven Rostedt 				__attribute__((section("_ftrace_annotated_branch"))) \
931f0d69a9SSteven Rostedt 				______f = {				\
941f0d69a9SSteven Rostedt 				.func = __func__,			\
951f0d69a9SSteven Rostedt 				.file = __FILE__,			\
961f0d69a9SSteven Rostedt 				.line = __LINE__,			\
971f0d69a9SSteven Rostedt 			};						\
981f0d69a9SSteven Rostedt 			______r = likely_notrace(x);			\
9945b79749SSteven Rostedt 			ftrace_likely_update(&______f, ______r, expect); \
1001f0d69a9SSteven Rostedt 			______r;					\
1011f0d69a9SSteven Rostedt 		})
1021f0d69a9SSteven Rostedt 
1031f0d69a9SSteven Rostedt /*
1041f0d69a9SSteven Rostedt  * Using __builtin_constant_p(x) to ignore cases where the return
1051f0d69a9SSteven Rostedt  * value is always the same.  This idea is taken from a similar patch
1061f0d69a9SSteven Rostedt  * written by Daniel Walker.
1071f0d69a9SSteven Rostedt  */
1081f0d69a9SSteven Rostedt # ifndef likely
10945b79749SSteven Rostedt #  define likely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1))
1101f0d69a9SSteven Rostedt # endif
1111f0d69a9SSteven Rostedt # ifndef unlikely
11245b79749SSteven Rostedt #  define unlikely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0))
1131f0d69a9SSteven Rostedt # endif
114*2bcd521aSSteven Rostedt 
115*2bcd521aSSteven Rostedt #ifdef CONFIG_PROFILE_ALL_BRANCHES
116*2bcd521aSSteven Rostedt /*
117*2bcd521aSSteven Rostedt  * "Define 'is'", Bill Clinton
118*2bcd521aSSteven Rostedt  * "Define 'if'", Steven Rostedt
119*2bcd521aSSteven Rostedt  */
120*2bcd521aSSteven Rostedt #define if(cond) if (__builtin_constant_p((cond)) ? !!(cond) :		\
121*2bcd521aSSteven Rostedt 	({								\
122*2bcd521aSSteven Rostedt 		int ______r;						\
123*2bcd521aSSteven Rostedt 		static struct ftrace_branch_data			\
124*2bcd521aSSteven Rostedt 			__attribute__((__aligned__(4)))			\
125*2bcd521aSSteven Rostedt 			__attribute__((section("_ftrace_branch")))	\
126*2bcd521aSSteven Rostedt 			______f = {					\
127*2bcd521aSSteven Rostedt 				.func = __func__,			\
128*2bcd521aSSteven Rostedt 				.file = __FILE__,			\
129*2bcd521aSSteven Rostedt 				.line = __LINE__,			\
130*2bcd521aSSteven Rostedt 			};						\
131*2bcd521aSSteven Rostedt 		______r = !!(cond);					\
132*2bcd521aSSteven Rostedt 		if (______r)						\
133*2bcd521aSSteven Rostedt 			______f.hit++;					\
134*2bcd521aSSteven Rostedt 		else							\
135*2bcd521aSSteven Rostedt 			______f.miss++;					\
136*2bcd521aSSteven Rostedt 		______r;						\
137*2bcd521aSSteven Rostedt 	}))
138*2bcd521aSSteven Rostedt #endif /* CONFIG_PROFILE_ALL_BRANCHES */
139*2bcd521aSSteven Rostedt 
1401f0d69a9SSteven Rostedt #else
1411da177e4SLinus Torvalds # define likely(x)	__builtin_expect(!!(x), 1)
1421da177e4SLinus Torvalds # define unlikely(x)	__builtin_expect(!!(x), 0)
1431f0d69a9SSteven Rostedt #endif
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /* Optimization barrier */
1461da177e4SLinus Torvalds #ifndef barrier
1471da177e4SLinus Torvalds # define barrier() __memory_barrier()
1481da177e4SLinus Torvalds #endif
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds #ifndef RELOC_HIDE
1511da177e4SLinus Torvalds # define RELOC_HIDE(ptr, off)					\
1521da177e4SLinus Torvalds   ({ unsigned long __ptr;					\
1531da177e4SLinus Torvalds      __ptr = (unsigned long) (ptr);				\
1541da177e4SLinus Torvalds     (typeof(ptr)) (__ptr + (off)); })
1551da177e4SLinus Torvalds #endif
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds #endif /* __KERNEL__ */
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds #endif /* __ASSEMBLY__ */
1601da177e4SLinus Torvalds 
1614f79c3ffSDavid Woodhouse #ifdef __KERNEL__
1621da177e4SLinus Torvalds /*
1631da177e4SLinus Torvalds  * Allow us to mark functions as 'deprecated' and have gcc emit a nice
1641da177e4SLinus Torvalds  * warning for each use, in hopes of speeding the functions removal.
1651da177e4SLinus Torvalds  * Usage is:
1661da177e4SLinus Torvalds  * 		int __deprecated foo(void)
1671da177e4SLinus Torvalds  */
1681da177e4SLinus Torvalds #ifndef __deprecated
1691da177e4SLinus Torvalds # define __deprecated		/* unimplemented */
1701da177e4SLinus Torvalds #endif
1711da177e4SLinus Torvalds 
172512345beSPaul E. McKenney #ifdef MODULE
173512345beSPaul E. McKenney #define __deprecated_for_modules __deprecated
174512345beSPaul E. McKenney #else
175512345beSPaul E. McKenney #define __deprecated_for_modules
176512345beSPaul E. McKenney #endif
177512345beSPaul E. McKenney 
1781da177e4SLinus Torvalds #ifndef __must_check
1791da177e4SLinus Torvalds #define __must_check
1801da177e4SLinus Torvalds #endif
1811da177e4SLinus Torvalds 
182cebc04baSAndrew Morton #ifndef CONFIG_ENABLE_MUST_CHECK
183cebc04baSAndrew Morton #undef __must_check
184cebc04baSAndrew Morton #define __must_check
185cebc04baSAndrew Morton #endif
186de488443SJeff Garzik #ifndef CONFIG_ENABLE_WARN_DEPRECATED
187de488443SJeff Garzik #undef __deprecated
188de488443SJeff Garzik #undef __deprecated_for_modules
189de488443SJeff Garzik #define __deprecated
190de488443SJeff Garzik #define __deprecated_for_modules
191de488443SJeff Garzik #endif
192cebc04baSAndrew Morton 
1931da177e4SLinus Torvalds /*
1941da177e4SLinus Torvalds  * Allow us to avoid 'defined but not used' warnings on functions and data,
1951da177e4SLinus Torvalds  * as well as force them to be emitted to the assembly file.
1961da177e4SLinus Torvalds  *
1970d7ebbbcSDavid Rientjes  * As of gcc 3.4, static functions that are not marked with attribute((used))
1980d7ebbbcSDavid Rientjes  * may be elided from the assembly file.  As of gcc 3.4, static data not so
1991da177e4SLinus Torvalds  * marked will not be elided, but this may change in a future gcc version.
2001da177e4SLinus Torvalds  *
2010d7ebbbcSDavid Rientjes  * NOTE: Because distributions shipped with a backported unit-at-a-time
2020d7ebbbcSDavid Rientjes  * compiler in gcc 3.3, we must define __used to be __attribute__((used))
2030d7ebbbcSDavid Rientjes  * for gcc >=3.3 instead of 3.4.
2040d7ebbbcSDavid Rientjes  *
2051da177e4SLinus Torvalds  * In prior versions of gcc, such functions and data would be emitted, but
2061da177e4SLinus Torvalds  * would be warned about except with attribute((unused)).
2070d7ebbbcSDavid Rientjes  *
2080d7ebbbcSDavid Rientjes  * Mark functions that are referenced only in inline assembly as __used so
2090d7ebbbcSDavid Rientjes  * the code is emitted even though it appears to be unreferenced.
2101da177e4SLinus Torvalds  */
2110d7ebbbcSDavid Rientjes #ifndef __used
2120d7ebbbcSDavid Rientjes # define __used			/* unimplemented */
2130d7ebbbcSDavid Rientjes #endif
2140d7ebbbcSDavid Rientjes 
2150d7ebbbcSDavid Rientjes #ifndef __maybe_unused
2160d7ebbbcSDavid Rientjes # define __maybe_unused		/* unimplemented */
2171da177e4SLinus Torvalds #endif
2181da177e4SLinus Torvalds 
219423bc7b2SDavid Woodhouse #ifndef noinline
220423bc7b2SDavid Woodhouse #define noinline
221423bc7b2SDavid Woodhouse #endif
222423bc7b2SDavid Woodhouse 
223735c4fb9SAndrew Morton /*
224735c4fb9SAndrew Morton  * Rather then using noinline to prevent stack consumption, use
225735c4fb9SAndrew Morton  * noinline_for_stack instead.  For documentaiton reasons.
226735c4fb9SAndrew Morton  */
227735c4fb9SAndrew Morton #define noinline_for_stack noinline
228735c4fb9SAndrew Morton 
229423bc7b2SDavid Woodhouse #ifndef __always_inline
230423bc7b2SDavid Woodhouse #define __always_inline inline
231423bc7b2SDavid Woodhouse #endif
232423bc7b2SDavid Woodhouse 
233423bc7b2SDavid Woodhouse #endif /* __KERNEL__ */
234423bc7b2SDavid Woodhouse 
2351da177e4SLinus Torvalds /*
2361da177e4SLinus Torvalds  * From the GCC manual:
2371da177e4SLinus Torvalds  *
2381da177e4SLinus Torvalds  * Many functions do not examine any values except their arguments,
2391da177e4SLinus Torvalds  * and have no effects except the return value.  Basically this is
2401da177e4SLinus Torvalds  * just slightly more strict class than the `pure' attribute above,
2411da177e4SLinus Torvalds  * since function is not allowed to read global memory.
2421da177e4SLinus Torvalds  *
2431da177e4SLinus Torvalds  * Note that a function that has pointer arguments and examines the
2441da177e4SLinus Torvalds  * data pointed to must _not_ be declared `const'.  Likewise, a
2451da177e4SLinus Torvalds  * function that calls a non-`const' function usually must not be
2461da177e4SLinus Torvalds  * `const'.  It does not make sense for a `const' function to return
2471da177e4SLinus Torvalds  * `void'.
2481da177e4SLinus Torvalds  */
2491da177e4SLinus Torvalds #ifndef __attribute_const__
2501da177e4SLinus Torvalds # define __attribute_const__	/* unimplemented */
2511da177e4SLinus Torvalds #endif
2521da177e4SLinus Torvalds 
253a586df06SAndi Kleen /*
254a586df06SAndi Kleen  * Tell gcc if a function is cold. The compiler will assume any path
255a586df06SAndi Kleen  * directly leading to the call is unlikely.
256a586df06SAndi Kleen  */
257a586df06SAndi Kleen 
258a586df06SAndi Kleen #ifndef __cold
259a586df06SAndi Kleen #define __cold
260a586df06SAndi Kleen #endif
261a586df06SAndi Kleen 
262f3fe866dSSam Ravnborg /* Simple shorthand for a section definition */
263f3fe866dSSam Ravnborg #ifndef __section
264f3fe866dSSam Ravnborg # define __section(S) __attribute__ ((__section__(#S)))
265f3fe866dSSam Ravnborg #endif
266f3fe866dSSam Ravnborg 
2679c3cdc1fSLinus Torvalds /*
2689c3cdc1fSLinus Torvalds  * Prevent the compiler from merging or refetching accesses.  The compiler
2699c3cdc1fSLinus Torvalds  * is also forbidden from reordering successive instances of ACCESS_ONCE(),
2709c3cdc1fSLinus Torvalds  * but only when the compiler is aware of some particular ordering.  One way
2719c3cdc1fSLinus Torvalds  * to make the compiler aware of ordering is to put the two invocations of
2729c3cdc1fSLinus Torvalds  * ACCESS_ONCE() in different C statements.
2739c3cdc1fSLinus Torvalds  *
2749c3cdc1fSLinus Torvalds  * This macro does absolutely -nothing- to prevent the CPU from reordering,
275ded00a56SPaul E. McKenney  * merging, or refetching absolutely anything at any time.  Its main intended
276ded00a56SPaul E. McKenney  * use is to mediate communication between process-level code and irq/NMI
277ded00a56SPaul E. McKenney  * handlers, all running on the same CPU.
2789c3cdc1fSLinus Torvalds  */
2799c3cdc1fSLinus Torvalds #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
2809c3cdc1fSLinus Torvalds 
2811da177e4SLinus Torvalds #endif /* __LINUX_COMPILER_H */
282