blob: 7b086e310c454e034023a9d4ffa0a2d61924ad49 [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 Tarreaucc05fba2009-10-27 21:40:18 +010080/*
Willy Tarreau89ee7982020-02-25 07:14:43 +010081 * Gcc >= 3 provides the ability for the program to give hints to the
Willy Tarreaucc05fba2009-10-27 21:40:18 +010082 * compiler about what branch of an if is most likely to be taken. This
83 * helps the compiler produce the most compact critical paths, which is
84 * generally better for the cache and to reduce the number of jumps.
85 */
86#if !defined(likely)
87#if __GNUC__ < 3
88#define __builtin_expect(x,y) (x)
89#define likely(x) (x)
90#define unlikely(x) (x)
Willy Tarreau89ee7982020-02-25 07:14:43 +010091#else
Willy Tarreaucc05fba2009-10-27 21:40:18 +010092#define likely(x) (__builtin_expect((x) != 0, 1))
93#define unlikely(x) (__builtin_expect((x) != 0, 0))
Willy Tarreaucc05fba2009-10-27 21:40:18 +010094#endif
95#endif
96
Olivier Houchard928fbfa2018-01-24 18:17:06 +010097#ifndef __GNUC_PREREQ__
98#if defined(__GNUC__) && !defined(__INTEL_COMPILER)
99#define __GNUC_PREREQ__(ma, mi) \
100 (__GNUC__ > (ma) || __GNUC__ == (ma) && __GNUC_MINOR__ >= (mi))
101#else
102#define __GNUC_PREREQ__(ma, mi) 0
103#endif
104#endif
105
106#ifndef offsetof
107#if __GNUC_PREREQ__(4, 1)
108#define offsetof(type, field) __builtin_offsetof(type, field)
109#else
110#define offsetof(type, field) \
Willy Tarreauc03ea402018-07-30 11:47:35 +0200111 ((size_t)(uintptr_t)((const volatile void *)&((type *)0)->field))
Olivier Houchard928fbfa2018-01-24 18:17:06 +0100112#endif
113#endif
114
Willy Tarreau0e268672020-02-21 15:40:58 +0100115/* Some architectures have a double-word CAS, sometimes even dual-8 bytes.
116 * Some architectures support unaligned accesses, others are fine with them
117 * but only for non-atomic operations. Also mention those supporting unaligned
118 * accesses and being little endian, and those where unaligned accesses are
119 * known to be fast (almost as fast as aligned ones).
120 */
121#if defined(__x86_64__)
122#define HA_UNALIGNED
123#define HA_UNALIGNED_LE
124#define HA_UNALIGNED_LE64
125#define HA_UNALIGNED_FAST
126#define HA_UNALIGNED_ATOMIC
127#define HA_HAVE_CAS_DW
128#define HA_CAS_IS_8B
129#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
130#define HA_UNALIGNED
131#define HA_UNALIGNED_LE
132#define HA_UNALIGNED_ATOMIC
133#elif defined (__aarch64__) || defined(__ARM_ARCH_8A)
134#define HA_UNALIGNED
135#define HA_UNALIGNED_LE
136#define HA_UNALIGNED_LE64
137#define HA_UNALIGNED_FAST
138#define HA_HAVE_CAS_DW
139#define HA_CAS_IS_8B
140#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
141#define HA_UNALIGNED
142#define HA_UNALIGNED_LE
143#define HA_UNALIGNED_FAST
144#define HA_HAVE_CAS_DW
145#endif
Willy Tarreaucc05fba2009-10-27 21:40:18 +0100146
Willy Tarreau226ef262020-02-22 15:51:39 +0100147
148/* sets alignment for current field or variable */
149#ifndef ALIGNED
150#define ALIGNED(x) __attribute__((aligned(x)))
151#endif
152
153/* sets alignment only on architectures preventing unaligned atomic accesses */
154#ifndef MAYBE_ALIGNED
155#ifndef HA_UNALIGNED
156#define MAYBE_ALIGNED(x) ALIGNED(x)
157#else
158#define MAYBE_ALIGNED(x)
159#endif
160#endif
161
162/* sets alignment only on architectures preventing unaligned atomic accesses */
163#ifndef ATOMIC_ALIGNED
164#ifndef HA_UNALIGNED_ATOMIC
165#define ATOMIC_ALIGNED(x) ALIGNED(x)
166#else
167#define ATOMIC_ALIGNED(x)
168#endif
169#endif
170
171/* add a mandatory alignment for next fields in a structure */
172#ifndef ALWAYS_ALIGN
173#define ALWAYS_ALIGN(x) union { } ALIGNED(x)
174#endif
175
176/* add an optional alignment for next fields in a structure, only for archs
177 * which do not support unaligned accesses.
178 */
179#ifndef MAYBE_ALIGN
180#ifndef HA_UNALIGNED
181#define MAYBE_ALIGN(x) union { } ALIGNED(x)
182#else
183#define MAYBE_ALIGN(x)
184#endif
185#endif
186
187/* add an optional alignment for next fields in a structure, only for archs
188 * which do not support unaligned accesses for atomic operations.
189 */
190#ifndef ATOMIC_ALIGN
191#ifndef HA_UNALIGNED_ATOMIC
192#define ATOMIC_ALIGN(x) union { } ALIGNED(x)
193#else
194#define ATOMIC_ALIGN(x)
195#endif
196#endif
197
Willy Tarreaucc05fba2009-10-27 21:40:18 +0100198#endif /* _COMMON_COMPILER_H */