Willy Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 1 | /* |
| 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 Tarreau | f3bfede | 2011-07-25 11:38:17 +0200 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
Willy Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 20 | */ |
| 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 Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 38 | /* 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 Tarreau | dbd25fc | 2017-11-20 21:22:17 +0100 | [diff] [blame] | 49 | /* 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 Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 53 | |
Willy Tarreau | 8d26f02 | 2018-10-15 11:53:34 +0200 | [diff] [blame] | 54 | /* 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 Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 61 | #define my_unreachable() __builtin_unreachable() |
Willy Tarreau | 8d26f02 | 2018-10-15 11:53:34 +0200 | [diff] [blame] | 62 | #else |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 63 | #define my_unreachable() |
Willy Tarreau | 8d26f02 | 2018-10-15 11:53:34 +0200 | [diff] [blame] | 64 | #endif |
| 65 | |
Willy Tarreau | 071d4b3 | 2018-12-08 15:20:43 +0100 | [diff] [blame] | 66 | /* 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 Tarreau | f401668 | 2020-03-14 10:42:26 +0100 | [diff] [blame] | 80 | /* 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 Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 85 | /* |
Willy Tarreau | 89ee798 | 2020-02-25 07:14:43 +0100 | [diff] [blame] | 86 | * Gcc >= 3 provides the ability for the program to give hints to the |
Willy Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 87 | * 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 Tarreau | 89ee798 | 2020-02-25 07:14:43 +0100 | [diff] [blame] | 96 | #else |
Willy Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 97 | #define likely(x) (__builtin_expect((x) != 0, 1)) |
| 98 | #define unlikely(x) (__builtin_expect((x) != 0, 0)) |
Willy Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 99 | #endif |
| 100 | #endif |
| 101 | |
Olivier Houchard | 928fbfa | 2018-01-24 18:17:06 +0100 | [diff] [blame] | 102 | #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 Tarreau | c03ea40 | 2018-07-30 11:47:35 +0200 | [diff] [blame] | 116 | ((size_t)(uintptr_t)((const volatile void *)&((type *)0)->field)) |
Olivier Houchard | 928fbfa | 2018-01-24 18:17:06 +0100 | [diff] [blame] | 117 | #endif |
| 118 | #endif |
| 119 | |
Willy Tarreau | 0e26867 | 2020-02-21 15:40:58 +0100 | [diff] [blame] | 120 | /* 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 Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 151 | |
Willy Tarreau | 226ef26 | 2020-02-22 15:51:39 +0100 | [diff] [blame] | 152 | |
| 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 Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 203 | #endif /* _COMMON_COMPILER_H */ |