MINOR: compiler: drop special cases of likely/unlikely for older compilers
We used to special-case the likely()/unlikely() macros for a series of
early gcc 4.x compilers which used to produce very bad code when using
__builtin_expect(x,1), which basically used to build an integer (0 or 1)
from a condition then compare it to integer 1. This was already fixed in
5.x, but even now, looking at the code produced by various flavors of 4.x
this bad behavior couldn't be witnessed anymore. So let's consider it as
fixed by now, which will allow to get rid of some ugly tricks at some
specific places. A test on 4.7.4 shows that the code shrinks by about 3kB
now, thanks to some tests being inlined closer to the call place and the
unlikely case being moved to real functions. See the link below for more
background on this.
Link: https://www.mail-archive.com/haproxy@formilux.org/msg36392.html
diff --git a/include/common/compiler.h b/include/common/compiler.h
index 179b318..377b54d 100644
--- a/include/common/compiler.h
+++ b/include/common/compiler.h
@@ -109,7 +109,7 @@
#define ALREADY_CHECKED(p) do { asm("" : "=rm"(p) : "0"(p)); } while (0)
/*
- * Gcc >= 3 provides the ability for the programme to give hints to the
+ * Gcc >= 3 provides the ability for the program to give hints to the
* compiler about what branch of an if is most likely to be taken. This
* helps the compiler produce the most compact critical paths, which is
* generally better for the cache and to reduce the number of jumps.
@@ -119,17 +119,9 @@
#define __builtin_expect(x,y) (x)
#define likely(x) (x)
#define unlikely(x) (x)
-#elif __GNUC__ < 4 || __GNUC__ >= 5
-/* gcc 3.x and 5.x do the best job at this */
+#else
#define likely(x) (__builtin_expect((x) != 0, 1))
#define unlikely(x) (__builtin_expect((x) != 0, 0))
-#else
-/* GCC 4.x is stupid, it performs the comparison then compares it to 1,
- * so we cheat in a dirty way to prevent it from doing this. This will
- * only work with ints and booleans though.
- */
-#define likely(x) (x)
-#define unlikely(x) (__builtin_expect((unsigned long)(x), 0))
#endif
#endif