1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef __LINUX_COMPILER_H
31da177e4SLinus Torvalds #define __LINUX_COMPILER_H
41da177e4SLinus Torvalds
5d1515582SWill Deacon #include <linux/compiler_types.h>
6d1515582SWill Deacon
71da177e4SLinus Torvalds #ifndef __ASSEMBLY__
81da177e4SLinus Torvalds
91da177e4SLinus Torvalds #ifdef __KERNEL__
101da177e4SLinus Torvalds
112ed84eebSSteven Rostedt /*
122ed84eebSSteven Rostedt * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
132ed84eebSSteven Rostedt * to disable branch tracing on a per file basis.
142ed84eebSSteven Rostedt */
15134e6a03SSteven Rostedt (VMware) void ftrace_likely_update(struct ftrace_likely_data *f, int val,
16d45ae1f7SSteven Rostedt (VMware) int expect, int is_constant);
17a18ef64fSArnd Bergmann #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
18a18ef64fSArnd Bergmann && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
191f0d69a9SSteven Rostedt #define likely_notrace(x) __builtin_expect(!!(x), 1)
201f0d69a9SSteven Rostedt #define unlikely_notrace(x) __builtin_expect(!!(x), 0)
211f0d69a9SSteven Rostedt
22d45ae1f7SSteven Rostedt (VMware) #define __branch_check__(x, expect, is_constant) ({ \
232026d357SMikulas Patocka long ______r; \
24134e6a03SSteven Rostedt (VMware) static struct ftrace_likely_data \
25e04462fbSMiguel Ojeda __aligned(4) \
2633def849SJoe Perches __section("_ftrace_annotated_branch") \
271f0d69a9SSteven Rostedt ______f = { \
28134e6a03SSteven Rostedt (VMware) .data.func = __func__, \
29134e6a03SSteven Rostedt (VMware) .data.file = __FILE__, \
30134e6a03SSteven Rostedt (VMware) .data.line = __LINE__, \
311f0d69a9SSteven Rostedt }; \
32d45ae1f7SSteven Rostedt (VMware) ______r = __builtin_expect(!!(x), expect); \
33d45ae1f7SSteven Rostedt (VMware) ftrace_likely_update(&______f, ______r, \
34d45ae1f7SSteven Rostedt (VMware) expect, is_constant); \
351f0d69a9SSteven Rostedt ______r; \
361f0d69a9SSteven Rostedt })
371f0d69a9SSteven Rostedt
381f0d69a9SSteven Rostedt /*
391f0d69a9SSteven Rostedt * Using __builtin_constant_p(x) to ignore cases where the return
401f0d69a9SSteven Rostedt * value is always the same. This idea is taken from a similar patch
411f0d69a9SSteven Rostedt * written by Daniel Walker.
421f0d69a9SSteven Rostedt */
431f0d69a9SSteven Rostedt # ifndef likely
44d45ae1f7SSteven Rostedt (VMware) # define likely(x) (__branch_check__(x, 1, __builtin_constant_p(x)))
451f0d69a9SSteven Rostedt # endif
461f0d69a9SSteven Rostedt # ifndef unlikely
47d45ae1f7SSteven Rostedt (VMware) # define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x)))
481f0d69a9SSteven Rostedt # endif
492bcd521aSSteven Rostedt
502bcd521aSSteven Rostedt #ifdef CONFIG_PROFILE_ALL_BRANCHES
512bcd521aSSteven Rostedt /*
522bcd521aSSteven Rostedt * "Define 'is'", Bill Clinton
532bcd521aSSteven Rostedt * "Define 'if'", Steven Rostedt
542bcd521aSSteven Rostedt */
55a15fd609SLinus Torvalds #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
56a15fd609SLinus Torvalds
57a15fd609SLinus Torvalds #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
58a15fd609SLinus Torvalds
59a15fd609SLinus Torvalds #define __trace_if_value(cond) ({ \
602bcd521aSSteven Rostedt static struct ftrace_branch_data \
61e04462fbSMiguel Ojeda __aligned(4) \
6233def849SJoe Perches __section("_ftrace_branch") \
63a15fd609SLinus Torvalds __if_trace = { \
642bcd521aSSteven Rostedt .func = __func__, \
652bcd521aSSteven Rostedt .file = __FILE__, \
662bcd521aSSteven Rostedt .line = __LINE__, \
672bcd521aSSteven Rostedt }; \
68a15fd609SLinus Torvalds (cond) ? \
69a15fd609SLinus Torvalds (__if_trace.miss_hit[1]++,1) : \
70a15fd609SLinus Torvalds (__if_trace.miss_hit[0]++,0); \
71a15fd609SLinus Torvalds })
72a15fd609SLinus Torvalds
732bcd521aSSteven Rostedt #endif /* CONFIG_PROFILE_ALL_BRANCHES */
742bcd521aSSteven Rostedt
751f0d69a9SSteven Rostedt #else
761da177e4SLinus Torvalds # define likely(x) __builtin_expect(!!(x), 1)
771da177e4SLinus Torvalds # define unlikely(x) __builtin_expect(!!(x), 0)
782f0df49cSSteven Rostedt (VMware) # define likely_notrace(x) likely(x)
792f0df49cSSteven Rostedt (VMware) # define unlikely_notrace(x) unlikely(x)
801f0d69a9SSteven Rostedt #endif
811da177e4SLinus Torvalds
821da177e4SLinus Torvalds /* Optimization barrier */
831da177e4SLinus Torvalds #ifndef barrier
843347acc6SArvind Sankar /* The "volatile" is due to gcc bugs */
853347acc6SArvind Sankar # define barrier() __asm__ __volatile__("": : :"memory")
861da177e4SLinus Torvalds #endif
871da177e4SLinus Torvalds
887829fb09SDaniel Borkmann #ifndef barrier_data
893347acc6SArvind Sankar /*
903347acc6SArvind Sankar * This version is i.e. to prevent dead stores elimination on @ptr
913347acc6SArvind Sankar * where gcc and llvm may behave differently when otherwise using
923347acc6SArvind Sankar * normal barrier(): while gcc behavior gets along with a normal
933347acc6SArvind Sankar * barrier(), llvm needs an explicit input variable to be assumed
943347acc6SArvind Sankar * clobbered. The issue is as follows: while the inline asm might
953347acc6SArvind Sankar * access any memory it wants, the compiler could have fit all of
963347acc6SArvind Sankar * @ptr into memory registers instead, and since @ptr never escaped
973347acc6SArvind Sankar * from that, it proved that the inline asm wasn't touching any of
983347acc6SArvind Sankar * it. This version works well with both compilers, i.e. we're telling
993347acc6SArvind Sankar * the compiler that the inline asm absolutely may see the contents
1003347acc6SArvind Sankar * of @ptr. See also: https://llvm.org/bugs/show_bug.cgi?id=15495
1013347acc6SArvind Sankar */
1023347acc6SArvind Sankar # define barrier_data(ptr) __asm__ __volatile__("": :"r"(ptr) :"memory")
1037829fb09SDaniel Borkmann #endif
1047829fb09SDaniel Borkmann
105173a3efdSArnd Bergmann /* workaround for GCC PR82365 if needed */
106173a3efdSArnd Bergmann #ifndef barrier_before_unreachable
107173a3efdSArnd Bergmann # define barrier_before_unreachable() do { } while (0)
108173a3efdSArnd Bergmann #endif
109173a3efdSArnd Bergmann
11038938c87SDavid Daney /* Unreachable code */
11103f16cd0SJosh Poimboeuf #ifdef CONFIG_OBJTOOL
11287b512deSJosh Poimboeuf /* Annotate a C jump table to allow objtool to follow the code flow */
11373cfc53cSArd Biesheuvel #define __annotate_jump_table __section(".data.rel.ro.c_jump_table")
11403f16cd0SJosh Poimboeuf #else /* !CONFIG_OBJTOOL */
11587b512deSJosh Poimboeuf #define __annotate_jump_table
11603f16cd0SJosh Poimboeuf #endif /* CONFIG_OBJTOOL */
117649ea4d5SJosh Poimboeuf
118c837de38SPeter Zijlstra /*
119c837de38SPeter Zijlstra * Mark a position in code as unreachable. This can be used to
120c837de38SPeter Zijlstra * suppress control flow warnings after asm blocks that transfer
121c837de38SPeter Zijlstra * control elsewhere.
122c837de38SPeter Zijlstra */
123fe0640ebS[email protected] #define unreachable() do { \
124c837de38SPeter Zijlstra barrier_before_unreachable(); \
125fe0640ebS[email protected] __builtin_unreachable(); \
126fe0640ebS[email protected] } while (0)
12738938c87SDavid Daney
128b67067f1SNicholas Piggin /*
129b67067f1SNicholas Piggin * KENTRY - kernel entry point
130b67067f1SNicholas Piggin * This can be used to annotate symbols (functions or data) that are used
131b67067f1SNicholas Piggin * without their linker symbol being referenced explicitly. For example,
132b67067f1SNicholas Piggin * interrupt vector handlers, or functions in the kernel image that are found
133b67067f1SNicholas Piggin * programatically.
134b67067f1SNicholas Piggin *
135b67067f1SNicholas Piggin * Not required for symbols exported with EXPORT_SYMBOL, or initcalls. Those
136b67067f1SNicholas Piggin * are handled in their own way (with KEEP() in linker scripts).
137b67067f1SNicholas Piggin *
138b67067f1SNicholas Piggin * KENTRY can be avoided if the symbols in question are marked as KEEP() in the
139b67067f1SNicholas Piggin * linker script. For example an architecture could KEEP() its entire
140b67067f1SNicholas Piggin * boot/exception vector code rather than annotate each function and data.
141b67067f1SNicholas Piggin */
142b67067f1SNicholas Piggin #ifndef KENTRY
143b67067f1SNicholas Piggin # define KENTRY(sym) \
144b67067f1SNicholas Piggin extern typeof(sym) sym; \
145b67067f1SNicholas Piggin static const unsigned long __kentry_##sym \
146b67067f1SNicholas Piggin __used \
147a25c13b3SNick Desaulniers __attribute__((__section__("___kentry+" #sym))) \
148b67067f1SNicholas Piggin = (unsigned long)&sym;
149b67067f1SNicholas Piggin #endif
150b67067f1SNicholas Piggin
1511da177e4SLinus Torvalds #ifndef RELOC_HIDE
1521da177e4SLinus Torvalds # define RELOC_HIDE(ptr, off) \
1531da177e4SLinus Torvalds ({ unsigned long __ptr; \
1541da177e4SLinus Torvalds __ptr = (unsigned long) (ptr); \
1551da177e4SLinus Torvalds (typeof(ptr)) (__ptr + (off)); })
1561da177e4SLinus Torvalds #endif
1571da177e4SLinus Torvalds
158f6b5f1a5SGuenter Roeck #define absolute_pointer(val) RELOC_HIDE((void *)(val), 0)
159f6b5f1a5SGuenter Roeck
160fe8c8a12SCesar Eduardo Barros #ifndef OPTIMIZER_HIDE_VAR
1613e2ffd65SMichael S. Tsirkin /* Make the optimizer believe the variable can be manipulated arbitrarily. */
1623e2ffd65SMichael S. Tsirkin #define OPTIMIZER_HIDE_VAR(var) \
1633e2ffd65SMichael S. Tsirkin __asm__ ("" : "=r" (var) : "0" (var))
164fe8c8a12SCesar Eduardo Barros #endif
165fe8c8a12SCesar Eduardo Barros
166a8306f2dSNick Desaulniers #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
1676f33d587SRusty Russell
16837d1a04bSThomas Gleixner /**
16937d1a04bSThomas Gleixner * data_race - mark an expression as containing intentional data races
17037d1a04bSThomas Gleixner *
17137d1a04bSThomas Gleixner * This data_race() macro is useful for situations in which data races
17237d1a04bSThomas Gleixner * should be forgiven. One example is diagnostic code that accesses
17337d1a04bSThomas Gleixner * shared variables but is not a part of the core synchronization design.
174020e6c22SPaul E. McKenney * For example, if accesses to a given variable are protected by a lock,
175020e6c22SPaul E. McKenney * except for diagnostic code, then the accesses under the lock should
176020e6c22SPaul E. McKenney * be plain C-language accesses and those in the diagnostic code should
177020e6c22SPaul E. McKenney * use data_race(). This way, KCSAN will complain if buggy lockless
178020e6c22SPaul E. McKenney * accesses to that variable are introduced, even if the buggy accesses
179020e6c22SPaul E. McKenney * are protected by READ_ONCE() or WRITE_ONCE().
18037d1a04bSThomas Gleixner *
18137d1a04bSThomas Gleixner * This macro *does not* affect normal code generation, but is a hint
182020e6c22SPaul E. McKenney * to tooling that data races here are to be ignored. If the access must
183020e6c22SPaul E. McKenney * be atomic *and* KCSAN should ignore the access, use both data_race()
184020e6c22SPaul E. McKenney * and READ_ONCE(), for example, data_race(READ_ONCE(x)).
18537d1a04bSThomas Gleixner */
18637d1a04bSThomas Gleixner #define data_race(expr) \
187d976441fSAndrey Ryabinin ({ \
18837d1a04bSThomas Gleixner __kcsan_disable_current(); \
1897c812814SAlexey Dobriyan __auto_type __v = (expr); \
19037d1a04bSThomas Gleixner __kcsan_enable_current(); \
19137d1a04bSThomas Gleixner __v; \
192d976441fSAndrey Ryabinin })
193230fa253SChristian Borntraeger
194cb7380deSKees Cook #ifdef __CHECKER__
195cb7380deSKees Cook #define __BUILD_BUG_ON_ZERO_MSG(e, msg) (0)
196cb7380deSKees Cook #else /* __CHECKER__ */
197cb7380deSKees Cook #define __BUILD_BUG_ON_ZERO_MSG(e, msg) ((int)sizeof(struct {_Static_assert(!(e), msg);}))
198cb7380deSKees Cook #endif /* __CHECKER__ */
199cb7380deSKees Cook
200cb7380deSKees Cook /* &a[0] degrades to a pointer: a different type from an array */
20120e5cc26SKees Cook #define __is_array(a) (!__same_type((a), &(a)[0]))
20220e5cc26SKees Cook #define __must_be_array(a) __BUILD_BUG_ON_ZERO_MSG(!__is_array(a), \
20320e5cc26SKees Cook "must be array")
20420e5cc26SKees Cook
20520e5cc26SKees Cook #define __is_byte_array(a) (__is_array(a) && sizeof((a)[0]) == 1)
20620e5cc26SKees Cook #define __must_be_byte_array(a) __BUILD_BUG_ON_ZERO_MSG(!__is_byte_array(a), \
20720e5cc26SKees Cook "must be byte array")
208cb7380deSKees Cook
209cb7380deSKees Cook /*
210cb7380deSKees Cook * If the "nonstring" attribute isn't available, we have to return true
211cb7380deSKees Cook * so the __must_*() checks pass when "nonstring" isn't supported.
212cb7380deSKees Cook */
213*ac053946SUros Bizjak #if __has_attribute(__nonstring__) && defined(__annotated)
214*ac053946SUros Bizjak #define __is_cstr(a) (!__annotated(a, nonstring))
215*ac053946SUros Bizjak #define __is_noncstr(a) (__annotated(a, nonstring))
216*ac053946SUros Bizjak #else
217*ac053946SUros Bizjak #define __is_cstr(a) (true)
218*ac053946SUros Bizjak #define __is_noncstr(a) (true)
219*ac053946SUros Bizjak #endif
220*ac053946SUros Bizjak
221*ac053946SUros Bizjak /* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */
222*ac053946SUros Bizjak #define __must_be_cstr(p) \
223*ac053946SUros Bizjak __BUILD_BUG_ON_ZERO_MSG(!__is_cstr(p), \
224*ac053946SUros Bizjak "must be C-string (NUL-terminated)")
225*ac053946SUros Bizjak #define __must_be_noncstr(p) \
226*ac053946SUros Bizjak __BUILD_BUG_ON_ZERO_MSG(!__is_noncstr(p), \
227*ac053946SUros Bizjak "must be non-C-string (not NUL-terminated)")
228*ac053946SUros Bizjak
229*ac053946SUros Bizjak /*
230*ac053946SUros Bizjak * Use __typeof_unqual__() when available.
231*ac053946SUros Bizjak *
232*ac053946SUros Bizjak * XXX: Remove test for __CHECKER__ once
2331da177e4SLinus Torvalds * sparse learns about __typeof_unqual__().
2341da177e4SLinus Torvalds */
2357290d580SArd Biesheuvel #if CC_HAS_TYPEOF_UNQUAL && !defined(__CHECKER__)
2367290d580SArd Biesheuvel # define USE_TYPEOF_UNQUAL 1
2377290d580SArd Biesheuvel #endif
2387290d580SArd Biesheuvel
2397290d580SArd Biesheuvel /*
2407290d580SArd Biesheuvel * Define TYPEOF_UNQUAL() to use __typeof_unqual__() as typeof
2417290d580SArd Biesheuvel * operator when available, to return an unqualified type of the exp.
2427290d580SArd Biesheuvel */
2437290d580SArd Biesheuvel #if defined(USE_TYPEOF_UNQUAL)
2441da177e4SLinus Torvalds # define TYPEOF_UNQUAL(exp) __typeof_unqual__(exp)
2451da177e4SLinus Torvalds #else
2460ef8047bSJuergen Gross # define TYPEOF_UNQUAL(exp) __typeof__(exp)
2470ef8047bSJuergen Gross #endif
2480ef8047bSJuergen Gross
2490ef8047bSJuergen Gross #endif /* __KERNEL__ */
2500ef8047bSJuergen Gross
2510ef8047bSJuergen Gross #if defined(CONFIG_CFI_CLANG) && !defined(__DISABLE_EXPORTS) && !defined(BUILD_VDSO)
2520ef8047bSJuergen Gross /*
2530ef8047bSJuergen Gross * Force a reference to the external symbol so the compiler generates
2540ef8047bSJuergen Gross * __kcfi_typid.
2550ef8047bSJuergen Gross */
2560ef8047bSJuergen Gross #define KCFI_REFERENCE(sym) __ADDRESSABLE(sym)
2570ef8047bSJuergen Gross #else
2580ef8047bSJuergen Gross #define KCFI_REFERENCE(sym)
2590ef8047bSJuergen Gross #endif
2600ef8047bSJuergen Gross
2610ef8047bSJuergen Gross /**
2620ef8047bSJuergen Gross * offset_to_ptr - convert a relative memory offset to an absolute pointer
2630ef8047bSJuergen Gross * @off: the address of the 32-bit offset value
2640ef8047bSJuergen Gross */
offset_to_ptr(const int * off)2650ef8047bSJuergen Gross static inline void *offset_to_ptr(const int *off)
2660ef8047bSJuergen Gross {
2670ef8047bSJuergen Gross return (void *)((unsigned long)off + *off);
2680ef8047bSJuergen Gross }
2690ef8047bSJuergen Gross
2700ef8047bSJuergen Gross #endif /* __ASSEMBLY__ */
2710ef8047bSJuergen Gross
2720ef8047bSJuergen Gross #ifdef CONFIG_64BIT
273a9a3ed1eSBorislav Petkov #define ARCH_SEL(a,b) a
274598f0ac1SDavid Laight #else
275598f0ac1SDavid Laight #define ARCH_SEL(a,b) b
276598f0ac1SDavid Laight #endif
277c3b9a398SKees Cook
278c3b9a398SKees Cook /*
279c3b9a398SKees Cook * Force the compiler to emit 'sym' as a symbol, so that we can reference
280c3b9a398SKees Cook * it from inline assembler. Necessary in case 'sym' could be inlined
281c3b9a398SKees Cook * otherwise, or eliminated entirely due to lack of references that are
282c3b9a398SKees Cook * visible to the compiler.
283c3b9a398SKees Cook */
284c3b9a398SKees Cook #define ___ADDRESSABLE(sym, __attrs) \
285c3b9a398SKees Cook static void * __used __attrs \
286c3b9a398SKees Cook __UNIQUE_ID(__PASTE(__addressable_,sym)) = (void *)(uintptr_t)&sym;
287c3b9a398SKees Cook
288c3b9a398SKees Cook #define __ADDRESSABLE(sym) \
289c3b9a398SKees Cook ___ADDRESSABLE(sym, __section(".discard.addressable"))
290c3b9a398SKees Cook
291c3b9a398SKees Cook #define __ADDRESSABLE_ASM(sym) \
292c3b9a398SKees Cook .pushsection .discard.addressable,"aw"; \
293c3b9a398SKees Cook .align ARCH_SEL(8,4); \
294c3b9a398SKees Cook ARCH_SEL(.quad, .long) __stringify(sym); \
295c3b9a398SKees Cook .popsection;
296c3b9a398SKees Cook
297c3b9a398SKees Cook #define __ADDRESSABLE_ASM_STR(sym) __stringify(__ADDRESSABLE_ASM(sym))
298c3b9a398SKees Cook
299c3b9a398SKees Cook /*
300c3b9a398SKees Cook * This returns a constant expression while determining if an argument is
301c3b9a398SKees Cook * a constant expression, most importantly without evaluating the argument.
302c3b9a398SKees Cook * Glory to Martin Uecker <[email protected]>
303c3b9a398SKees Cook *
304c3b9a398SKees Cook * Details:
305d7a62d0aSThorsten Blum * - sizeof() return an integer constant expression, and does not evaluate
306c3b9a398SKees Cook * the value of its operand; it only examines the type of its operand.
307c3b9a398SKees Cook * - The results of comparing two integer constant expressions is also
308c3b9a398SKees Cook * an integer constant expression.
309c3b9a398SKees Cook * - The first literal "8" isn't important. It could be any literal value.
310c3b9a398SKees Cook * - The second literal "8" is to avoid warnings about unaligned pointers;
311c3b9a398SKees Cook * this could otherwise just be "1".
312c3b9a398SKees Cook * - (long)(x) is used to avoid warnings about 64-bit types on 32-bit
313c3b9a398SKees Cook * architectures.
314c3b9a398SKees Cook * - The C Standard defines "null pointer constant", "(void *)0", as
315c3b9a398SKees Cook * distinct from other void pointers.
316598f0ac1SDavid Laight * - If (x) is an integer constant expression, then the "* 0l" resolves
317598f0ac1SDavid Laight * it into an integer constant expression of value 0. Since it is cast to
318598f0ac1SDavid Laight * "void *", this makes the second operand a null pointer constant.
319598f0ac1SDavid Laight * - If (x) is not an integer constant expression, then the second operand
320598f0ac1SDavid Laight * resolves to a void pointer (but not a null pointer constant: the value
321dcf8e563SBart Van Assche * is not an integer constant 0).
322dcf8e563SBart Van Assche * - The conditional operator's third operand, "(int *)8", is an object
323dcf8e563SBart Van Assche * pointer (to type "int").
324dcf8e563SBart Van Assche * - The behavior (including the return type) of the conditional operator
3254b21d25bSKees Cook * ("operand1 ? operand2 : operand3") depends on the kind of expressions
326dcf8e563SBart Van Assche * given for the second and third operands. This is the central mechanism
327dcf8e563SBart Van Assche * of the macro:
32822f54687SLinus Torvalds * - When one operand is a null pointer constant (i.e. when x is an integer
32922f54687SLinus Torvalds * constant expression) and the other is an object pointer (i.e. our
33022f54687SLinus Torvalds * third operand), the conditional operator returns the type of the
33122f54687SLinus Torvalds * object pointer operand (i.e. "int *"). Here, within the sizeof(), we
33222f54687SLinus Torvalds * would then get:
33322f54687SLinus Torvalds * sizeof(*((int *)(...)) == sizeof(int) == 4
33422f54687SLinus Torvalds * - When one operand is a void pointer (i.e. when x is not an integer
33522f54687SLinus Torvalds * constant expression) and the other is an object pointer (i.e. our
33622f54687SLinus Torvalds * third operand), the conditional operator returns a "void *" type.
3374f3d1be4SVincent Mailhol * Here, within the sizeof(), we would then get:
3384f3d1be4SVincent Mailhol * sizeof(*((void *)(...)) == sizeof(void) == 1
3394f3d1be4SVincent Mailhol * - The equality comparison to "sizeof(int)" therefore depends on (x):
3404f3d1be4SVincent Mailhol * sizeof(int) == sizeof(int) (x) was a constant expression
3414f3d1be4SVincent Mailhol * sizeof(int) != sizeof(void) (x) was not a constant expression
3424f3d1be4SVincent Mailhol */
3434f3d1be4SVincent Mailhol #define __is_constexpr(x) \
3444f3d1be4SVincent Mailhol (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
3454f3d1be4SVincent Mailhol
3464f3d1be4SVincent Mailhol /*
3474f3d1be4SVincent Mailhol * Whether 'type' is a signed type or an unsigned type. Supports scalar types,
3484f3d1be4SVincent Mailhol * bool and also pointer types.
3494f3d1be4SVincent Mailhol */
3504f3d1be4SVincent Mailhol #define is_signed_type(type) (((type)(-1)) < (__force type)1)
3514f3d1be4SVincent Mailhol #define is_unsigned_type(type) (!is_signed_type(type))
3524f3d1be4SVincent Mailhol
3534f3d1be4SVincent Mailhol /*
3544f3d1be4SVincent Mailhol * Useful shorthand for "is this condition known at compile-time?"
3554f3d1be4SVincent Mailhol *
3564f3d1be4SVincent Mailhol * Note that the condition may involve non-constant values,
3574f3d1be4SVincent Mailhol * but the compiler may know enough about the details of the
3584f3d1be4SVincent Mailhol * values to determine that the condition is statically true.
359a9a3ed1eSBorislav Petkov */
360a9a3ed1eSBorislav Petkov #define statically_true(x) (__builtin_constant_p(x) && (x))
361a9a3ed1eSBorislav Petkov
362a9a3ed1eSBorislav Petkov /*
363a9a3ed1eSBorislav Petkov * Similar to statically_true() but produces a constant expression
364e506ea45SWill Deacon *
365e506ea45SWill Deacon * To be used in conjunction with macros, such as BUILD_BUG_ON_ZERO(),
3661da177e4SLinus Torvalds * which require their input to be a constant expression and for which
367 * statically_true() would otherwise fail.
368 *
369 * This is a trade-off: const_true() requires all its operands to be
370 * compile time constants. Else, it would always returns false even on
371 * the most trivial cases like:
372 *
373 * true || non_const_var
374 *
375 * On the opposite, statically_true() is able to fold more complex
376 * tautologies and will return true on expressions such as:
377 *
378 * !(non_const_var * 8 % 4)
379 *
380 * For the general case, statically_true() is better.
381 */
382 #define const_true(x) __builtin_choose_expr(__is_constexpr(x), x, false)
383
384 /*
385 * This is needed in functions which generate the stack canary, see
386 * arch/x86/kernel/smpboot.c::start_secondary() for an example.
387 */
388 #define prevent_tail_call_optimization() mb()
389
390 #include <asm/rwonce.h>
391
392 #endif /* __LINUX_COMPILER_H */
393