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 | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 80 | /* |
Willy Tarreau | 89ee798 | 2020-02-25 07:14:43 +0100 | [diff] [blame] | 81 | * 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] | 82 | * 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 Tarreau | 89ee798 | 2020-02-25 07:14:43 +0100 | [diff] [blame] | 91 | #else |
Willy Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 92 | #define likely(x) (__builtin_expect((x) != 0, 1)) |
| 93 | #define unlikely(x) (__builtin_expect((x) != 0, 0)) |
Willy Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 94 | #endif |
| 95 | #endif |
| 96 | |
Olivier Houchard | 928fbfa | 2018-01-24 18:17:06 +0100 | [diff] [blame] | 97 | #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 Tarreau | c03ea40 | 2018-07-30 11:47:35 +0200 | [diff] [blame] | 111 | ((size_t)(uintptr_t)((const volatile void *)&((type *)0)->field)) |
Olivier Houchard | 928fbfa | 2018-01-24 18:17:06 +0100 | [diff] [blame] | 112 | #endif |
| 113 | #endif |
| 114 | |
Willy Tarreau | 0e26867 | 2020-02-21 15:40:58 +0100 | [diff] [blame] | 115 | /* 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 Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 146 | |
Willy Tarreau | 226ef26 | 2020-02-22 15:51:39 +0100 | [diff] [blame] | 147 | |
| 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 Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 198 | #endif /* _COMMON_COMPILER_H */ |