blob: e5fcf38c9d2440c01689a353440eedc3d2c6b839 [file] [log] [blame]
Willy Tarreaucc05fba2009-10-27 21:40:18 +01001/*
2 * include/common/compiler.h
3 * This files contains some compiler-specific settings.
4 *
5 * Copyright (C) 2000-2009 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
Willy Tarreauf3bfede2011-07-25 11:38:17 +020019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Willy Tarreaucc05fba2009-10-27 21:40:18 +010020 */
21
22#ifndef _COMMON_COMPILER_H
23#define _COMMON_COMPILER_H
24
25
26/*
27 * Gcc before 3.0 needs [0] to declare a variable-size array
28 */
29#ifndef VAR_ARRAY
30#if __GNUC__ < 3
31#define VAR_ARRAY 0
32#else
33#define VAR_ARRAY
34#endif
35#endif
36
37
38/* Support passing function parameters in registers. For this, the
39 * CONFIG_REGPARM macro has to be set to the maximal number of registers
40 * allowed. Some functions have intentionally received a regparm lower than
41 * their parameter count, it is in order to avoid register clobbering where
42 * they are called.
43 */
44#ifndef REGPRM1
45#if CONFIG_REGPARM >= 1 && __GNUC__ >= 3
46#define REGPRM1 __attribute__((regparm(1)))
47#else
48#define REGPRM1
49#endif
50#endif
51
52#ifndef REGPRM2
53#if CONFIG_REGPARM >= 2 && __GNUC__ >= 3
54#define REGPRM2 __attribute__((regparm(2)))
55#else
56#define REGPRM2 REGPRM1
57#endif
58#endif
59
60#ifndef REGPRM3
61#if CONFIG_REGPARM >= 3 && __GNUC__ >= 3
62#define REGPRM3 __attribute__((regparm(3)))
63#else
64#define REGPRM3 REGPRM2
65#endif
66#endif
67
68
69/* By default, gcc does not inline large chunks of code, but we want it to
70 * respect our choices.
71 */
72#if !defined(forceinline)
73#if __GNUC__ < 3
74#define forceinline inline
75#else
76#define forceinline inline __attribute__((always_inline))
77#endif
78#endif
79
Willy Tarreaudbd25fc2017-11-20 21:22:17 +010080/* silence the "unused" warnings without having to place painful #ifdefs.
81 * For use with variables or functions.
82 */
83#define __maybe_unused __attribute__((unused))
Willy Tarreaucc05fba2009-10-27 21:40:18 +010084
Willy Tarreau8d26f022018-10-15 11:53:34 +020085/* This allows gcc to know that some locations are never reached, for example
86 * after a longjmp() in the Lua code, hence that some errors caught by such
87 * methods cannot propagate further. This is important with gcc versions 6 and
88 * above which can more aggressively detect null dereferences. The builtin
89 * below was introduced in gcc 4.5, and before it we didn't care.
90 */
91#if __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
Willy Tarreau4e7cc332018-10-20 17:45:48 +020092#define my_unreachable() __builtin_unreachable()
Willy Tarreau8d26f022018-10-15 11:53:34 +020093#else
Willy Tarreau4e7cc332018-10-20 17:45:48 +020094#define my_unreachable()
Willy Tarreau8d26f022018-10-15 11:53:34 +020095#endif
96
Willy Tarreau071d4b32018-12-08 15:20:43 +010097/* This macro may be used to block constant propagation that lets the compiler
98 * detect a possible NULL dereference on a variable resulting from an explicit
99 * assignment in an impossible check. Sometimes a function is called which does
100 * safety checks and returns NULL if safe conditions are not met. The place
101 * where it's called cannot hit this condition and dereferencing the pointer
102 * without first checking it will make the compiler emit a warning about a
103 * "potential null pointer dereference" which is hard to work around. This
104 * macro "washes" the pointer and prevents the compiler from emitting tests
105 * branching to undefined instructions. It may only be used when the developer
106 * is absolutely certain that the conditions are guaranteed and that the
107 * pointer passed in argument cannot be NULL by design.
108 */
109#define ALREADY_CHECKED(p) do { asm("" : "=rm"(p) : "0"(p)); } while (0)
110
Willy Tarreaucc05fba2009-10-27 21:40:18 +0100111/*
112 * Gcc >= 3 provides the ability for the programme to give hints to the
113 * compiler about what branch of an if is most likely to be taken. This
114 * helps the compiler produce the most compact critical paths, which is
115 * generally better for the cache and to reduce the number of jumps.
116 */
117#if !defined(likely)
118#if __GNUC__ < 3
119#define __builtin_expect(x,y) (x)
120#define likely(x) (x)
121#define unlikely(x) (x)
Willy Tarreauc9398352017-10-08 22:26:03 +0200122#elif __GNUC__ < 4 || __GNUC__ >= 5
123/* gcc 3.x and 5.x do the best job at this */
Willy Tarreaucc05fba2009-10-27 21:40:18 +0100124#define likely(x) (__builtin_expect((x) != 0, 1))
125#define unlikely(x) (__builtin_expect((x) != 0, 0))
126#else
127/* GCC 4.x is stupid, it performs the comparison then compares it to 1,
128 * so we cheat in a dirty way to prevent it from doing this. This will
129 * only work with ints and booleans though.
130 */
131#define likely(x) (x)
132#define unlikely(x) (__builtin_expect((unsigned long)(x), 0))
133#endif
134#endif
135
Olivier Houchard928fbfa2018-01-24 18:17:06 +0100136#ifndef __GNUC_PREREQ__
137#if defined(__GNUC__) && !defined(__INTEL_COMPILER)
138#define __GNUC_PREREQ__(ma, mi) \
139 (__GNUC__ > (ma) || __GNUC__ == (ma) && __GNUC_MINOR__ >= (mi))
140#else
141#define __GNUC_PREREQ__(ma, mi) 0
142#endif
143#endif
144
145#ifndef offsetof
146#if __GNUC_PREREQ__(4, 1)
147#define offsetof(type, field) __builtin_offsetof(type, field)
148#else
149#define offsetof(type, field) \
Willy Tarreauc03ea402018-07-30 11:47:35 +0200150 ((size_t)(uintptr_t)((const volatile void *)&((type *)0)->field))
Olivier Houchard928fbfa2018-01-24 18:17:06 +0100151#endif
152#endif
153
Willy Tarreau1c9cace2020-02-21 15:40:58 +0100154/* Some architectures have a double-word CAS, sometimes even dual-8 bytes.
155 * Some architectures support unaligned accesses, others are fine with them
156 * but only for non-atomic operations. Also mention those supporting unaligned
157 * accesses and being little endian, and those where unaligned accesses are
158 * known to be fast (almost as fast as aligned ones).
159 */
160#if defined(__x86_64__)
161#define HA_UNALIGNED
162#define HA_UNALIGNED_LE
163#define HA_UNALIGNED_LE64
164#define HA_UNALIGNED_FAST
165#define HA_UNALIGNED_ATOMIC
166#define HA_HAVE_CAS_DW
167#define HA_CAS_IS_8B
168#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
169#define HA_UNALIGNED
170#define HA_UNALIGNED_LE
171#define HA_UNALIGNED_ATOMIC
172#elif defined (__aarch64__) || defined(__ARM_ARCH_8A)
173#define HA_UNALIGNED
174#define HA_UNALIGNED_LE
175#define HA_UNALIGNED_LE64
176#define HA_UNALIGNED_FAST
177#define HA_HAVE_CAS_DW
178#define HA_CAS_IS_8B
179#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
180#define HA_UNALIGNED
181#define HA_UNALIGNED_LE
182#define HA_UNALIGNED_FAST
183#define HA_HAVE_CAS_DW
184#endif
Willy Tarreaucc05fba2009-10-27 21:40:18 +0100185
Willy Tarreaued0832b2020-02-22 15:51:39 +0100186
187/* sets alignment for current field or variable */
188#ifndef ALIGNED
189#define ALIGNED(x) __attribute__((aligned(x)))
190#endif
191
192/* sets alignment only on architectures preventing unaligned atomic accesses */
193#ifndef MAYBE_ALIGNED
194#ifndef HA_UNALIGNED
195#define MAYBE_ALIGNED(x) ALIGNED(x)
196#else
197#define MAYBE_ALIGNED(x)
198#endif
199#endif
200
201/* sets alignment only on architectures preventing unaligned atomic accesses */
202#ifndef ATOMIC_ALIGNED
203#ifndef HA_UNALIGNED_ATOMIC
204#define ATOMIC_ALIGNED(x) ALIGNED(x)
205#else
206#define ATOMIC_ALIGNED(x)
207#endif
208#endif
209
210/* add a mandatory alignment for next fields in a structure */
211#ifndef ALWAYS_ALIGN
212#define ALWAYS_ALIGN(x) union { } ALIGNED(x)
213#endif
214
215/* add an optional alignment for next fields in a structure, only for archs
216 * which do not support unaligned accesses.
217 */
218#ifndef MAYBE_ALIGN
219#ifndef HA_UNALIGNED
220#define MAYBE_ALIGN(x) union { } ALIGNED(x)
221#else
222#define MAYBE_ALIGN(x)
223#endif
224#endif
225
226/* add an optional alignment for next fields in a structure, only for archs
227 * which do not support unaligned accesses for atomic operations.
228 */
229#ifndef ATOMIC_ALIGN
230#ifndef HA_UNALIGNED_ATOMIC
231#define ATOMIC_ALIGN(x) union { } ALIGNED(x)
232#else
233#define ATOMIC_ALIGN(x)
234#endif
235#endif
236
Willy Tarreaucc05fba2009-10-27 21:40:18 +0100237#endif /* _COMMON_COMPILER_H */