This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Use __builtin_unreachable() in gcc_assert() if not ENABLE_ASSERT_CHECKING.
- From: David Daney <david dot s dot daney at gmail dot com>
- To: gcc-patches at gcc dot gnu dot org
- Cc: David Daney <ddaney at caviumnetworks dot com>
- Date: Wed, 08 Jul 2009 00:11:34 -0700
- Subject: [PATCH] Use __builtin_unreachable() in gcc_assert() if not ENABLE_ASSERT_CHECKING.
This is a follow-on to my previous patches that added
__builtin_unreachable().
It depends on this still unreviewed patch
<http://gcc.gnu.org/ml/gcc-patches/2009-06/msg01267.html>, without
which, we ICE in stage 2.
In conjunction with that patch and this one
<http://gcc.gnu.org/ml/gcc-patches/2009-06/msg01978.html> (also
unreviewed), we get considerable code size reduction in cc1 when
configured with --disable-checking:
$ size native-clean/gcc/cc1 native-trunk/gcc/cc1
text data bss dec hex filename
9297789 563728 648280 10509797 a05de5 native-clean/gcc/cc1
9280536 563728 648280 10492544 a01a80 native-trunk/gcc/cc1
17253 bytes saved (at the standard -O2) or a whopping 17253/9297789 =
0.19%, I also ran CSiBE, but could discern no real change in compile
times before and after the patch.
Although the savings are not great, and most sane people would never use
a --disable-checking compiler, I think there are a couple of other good
reasons to apply the patch:
* The principle of eating our own dog food. If we add a feature, we
should also use it.
* A couple of bugs with the original __builtin_unreachable() patch were
discovered while testing this patch. If we use it in GCC we will get
much better testing of __builtin_unreachable().
Tested (in conjunction with the two other patches mentioned above) on
x86_64-pc-linux-gnu with --disable-checking --enable-languages=all,ada
both -m64 and -m32 with no regressions found.
OK to commit (after those other two patches are approved/committed)?
2009-07-08 David Daney <ddaney@caviumnetworks.com>
* system.h (gcc_assert): Invoke __builtin_unreachable() instead of
fancy_abort() if !ENABLE_ASSERT_CHECKING.
(gcc_unreachable): Invoke __builtin_unreachable() if
!ENABLE_ASSERT_CHECKING.
Index: gcc/system.h
===================================================================
--- gcc/system.h (revision 149312)
+++ gcc/system.h (working copy)
@@ -576,6 +576,9 @@ extern void fancy_abort (const char *, i
#if ENABLE_ASSERT_CHECKING
#define gcc_assert(EXPR) \
((void)(!(EXPR) ? fancy_abort (__FILE__, __LINE__, __FUNCTION__), 0 : 0))
+#elif (GCC_VERSION >= 4005)
+#define gcc_assert(EXPR) \
+ ((void)(__builtin_expect(!(EXPR), 0) ? __builtin_unreachable(), 0 : 0))
#else
/* Include EXPR, so that unused variable warnings do not occur. */
#define gcc_assert(EXPR) ((void)(0 && (EXPR)))
@@ -583,7 +586,11 @@ extern void fancy_abort (const char *, i
/* Use gcc_unreachable() to mark unreachable locations (like an
unreachable default case of a switch. Do not use gcc_assert(0). */
+#if (GCC_VERSION >= 4005) && !ENABLE_ASSERT_CHECKING
+#define gcc_unreachable() __builtin_unreachable()
+#else
#define gcc_unreachable() (fancy_abort (__FILE__, __LINE__, __FUNCTION__))
+#endif
/* Provide a fake boolean type. We make no attempt to use the
C99 _Bool, as it may not be available in the bootstrap compiler,