blob: 5578a58bc50084b2f5a5594bd9bb62650b6664c4 [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
Willy Tarreaucc05fba2009-10-27 21:40:18 +010038/* By default, gcc does not inline large chunks of code, but we want it to
39 * respect our choices.
40 */
41#if !defined(forceinline)
42#if __GNUC__ < 3
43#define forceinline inline
44#else
45#define forceinline inline __attribute__((always_inline))
46#endif
47#endif
48
Willy Tarreaudbd25fc2017-11-20 21:22:17 +010049/* silence the "unused" warnings without having to place painful #ifdefs.
50 * For use with variables or functions.
51 */
52#define __maybe_unused __attribute__((unused))
Willy Tarreaucc05fba2009-10-27 21:40:18 +010053
Willy Tarreau8d26f022018-10-15 11:53:34 +020054/* This allows gcc to know that some locations are never reached, for example
55 * after a longjmp() in the Lua code, hence that some errors caught by such
56 * methods cannot propagate further. This is important with gcc versions 6 and
57 * above which can more aggressively detect null dereferences. The builtin
58 * below was introduced in gcc 4.5, and before it we didn't care.
59 */
60#if __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
Willy Tarreau4e7cc332018-10-20 17:45:48 +020061#define my_unreachable() __builtin_unreachable()
Willy Tarreau8d26f022018-10-15 11:53:34 +020062#else
Willy Tarreau4e7cc332018-10-20 17:45:48 +020063#define my_unreachable()
Willy Tarreau8d26f022018-10-15 11:53:34 +020064#endif
65
Willy Tarreau071d4b32018-12-08 15:20:43 +010066/* This macro may be used to block constant propagation that lets the compiler
67 * detect a possible NULL dereference on a variable resulting from an explicit
68 * assignment in an impossible check. Sometimes a function is called which does
69 * safety checks and returns NULL if safe conditions are not met. The place
70 * where it's called cannot hit this condition and dereferencing the pointer
71 * without first checking it will make the compiler emit a warning about a
72 * "potential null pointer dereference" which is hard to work around. This
73 * macro "washes" the pointer and prevents the compiler from emitting tests
74 * branching to undefined instructions. It may only be used when the developer
75 * is absolutely certain that the conditions are guaranteed and that the
76 * pointer passed in argument cannot be NULL by design.
77 */
78#define ALREADY_CHECKED(p) do { asm("" : "=rm"(p) : "0"(p)); } while (0)
79
Willy Tarreauf4016682020-03-14 10:42:26 +010080/* same as above but to be used to pass the input value to the output but
81 * without letting the compiler know about its initial properties.
82 */
83#define DISGUISE(v) ({ typeof(v) __v = (v); ALREADY_CHECKED(__v); __v; })
84
Willy Tarreaucc05fba2009-10-27 21:40:18 +010085/*
Willy Tarreau89ee7982020-02-25 07:14:43 +010086 * Gcc >= 3 provides the ability for the program to give hints to the
Willy Tarreaucc05fba2009-10-27 21:40:18 +010087 * compiler about what branch of an if is most likely to be taken. This
88 * helps the compiler produce the most compact critical paths, which is
89 * generally better for the cache and to reduce the number of jumps.
90 */
91#if !defined(likely)
92#if __GNUC__ < 3
93#define __builtin_expect(x,y) (x)
94#define likely(x) (x)
95#define unlikely(x) (x)
Willy Tarreau89ee7982020-02-25 07:14:43 +010096#else
Willy Tarreaucc05fba2009-10-27 21:40:18 +010097#define likely(x) (__builtin_expect((x) != 0, 1))
98#define unlikely(x) (__builtin_expect((x) != 0, 0))
Willy Tarreaucc05fba2009-10-27 21:40:18 +010099#endif
100#endif
101
Olivier Houchard928fbfa2018-01-24 18:17:06 +0100102#ifndef __GNUC_PREREQ__
103#if defined(__GNUC__) && !defined(__INTEL_COMPILER)
104#define __GNUC_PREREQ__(ma, mi) \
105 (__GNUC__ > (ma) || __GNUC__ == (ma) && __GNUC_MINOR__ >= (mi))
106#else
107#define __GNUC_PREREQ__(ma, mi) 0
108#endif
109#endif
110
111#ifndef offsetof
112#if __GNUC_PREREQ__(4, 1)
113#define offsetof(type, field) __builtin_offsetof(type, field)
114#else
115#define offsetof(type, field) \
Willy Tarreauc03ea402018-07-30 11:47:35 +0200116 ((size_t)(uintptr_t)((const volatile void *)&((type *)0)->field))
Olivier Houchard928fbfa2018-01-24 18:17:06 +0100117#endif
118#endif
119
Willy Tarreau0e268672020-02-21 15:40:58 +0100120/* Some architectures have a double-word CAS, sometimes even dual-8 bytes.
121 * Some architectures support unaligned accesses, others are fine with them
122 * but only for non-atomic operations. Also mention those supporting unaligned
123 * accesses and being little endian, and those where unaligned accesses are
124 * known to be fast (almost as fast as aligned ones).
125 */
126#if defined(__x86_64__)
127#define HA_UNALIGNED
128#define HA_UNALIGNED_LE
129#define HA_UNALIGNED_LE64
130#define HA_UNALIGNED_FAST
131#define HA_UNALIGNED_ATOMIC
132#define HA_HAVE_CAS_DW
133#define HA_CAS_IS_8B
134#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
135#define HA_UNALIGNED
136#define HA_UNALIGNED_LE
137#define HA_UNALIGNED_ATOMIC
138#elif defined (__aarch64__) || defined(__ARM_ARCH_8A)
139#define HA_UNALIGNED
140#define HA_UNALIGNED_LE
141#define HA_UNALIGNED_LE64
142#define HA_UNALIGNED_FAST
143#define HA_HAVE_CAS_DW
144#define HA_CAS_IS_8B
145#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
146#define HA_UNALIGNED
147#define HA_UNALIGNED_LE
148#define HA_UNALIGNED_FAST
149#define HA_HAVE_CAS_DW
150#endif
Willy Tarreaucc05fba2009-10-27 21:40:18 +0100151
Willy Tarreau226ef262020-02-22 15:51:39 +0100152
153/* sets alignment for current field or variable */
154#ifndef ALIGNED
155#define ALIGNED(x) __attribute__((aligned(x)))
156#endif
157
158/* sets alignment only on architectures preventing unaligned atomic accesses */
159#ifndef MAYBE_ALIGNED
160#ifndef HA_UNALIGNED
161#define MAYBE_ALIGNED(x) ALIGNED(x)
162#else
163#define MAYBE_ALIGNED(x)
164#endif
165#endif
166
167/* sets alignment only on architectures preventing unaligned atomic accesses */
168#ifndef ATOMIC_ALIGNED
169#ifndef HA_UNALIGNED_ATOMIC
170#define ATOMIC_ALIGNED(x) ALIGNED(x)
171#else
172#define ATOMIC_ALIGNED(x)
173#endif
174#endif
175
176/* add a mandatory alignment for next fields in a structure */
177#ifndef ALWAYS_ALIGN
178#define ALWAYS_ALIGN(x) union { } ALIGNED(x)
179#endif
180
181/* add an optional alignment for next fields in a structure, only for archs
182 * which do not support unaligned accesses.
183 */
184#ifndef MAYBE_ALIGN
185#ifndef HA_UNALIGNED
186#define MAYBE_ALIGN(x) union { } ALIGNED(x)
187#else
188#define MAYBE_ALIGN(x)
189#endif
190#endif
191
192/* add an optional alignment for next fields in a structure, only for archs
193 * which do not support unaligned accesses for atomic operations.
194 */
195#ifndef ATOMIC_ALIGN
196#ifndef HA_UNALIGNED_ATOMIC
197#define ATOMIC_ALIGN(x) union { } ALIGNED(x)
198#else
199#define ATOMIC_ALIGN(x)
200#endif
201#endif
202
Willy Tarreaucc05fba2009-10-27 21:40:18 +0100203#endif /* _COMMON_COMPILER_H */