]> Savannah Git Hosting - gnulib.git/commitdiff
verify: Do use __builtin_assume on clang.
authorBruno Haible <bruno@clisp.org>
Sat, 22 Aug 2020 22:52:39 +0000 (00:52 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 22 Aug 2020 23:00:44 +0000 (01:00 +0200)
* lib/verify.h (assume): Use clang’s __builtin_assume, with a temporary
variable in a statement expression.

ChangeLog
lib/verify.h

index e89966ab71b10ba6d712c71dddf219e3671493ff..3d83db226fdf239463c82e5407b368d85cda00ba 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2020-08-22  Bruno Haible  <bruno@clisp.org>
+
+       verify: Do use __builtin_assume on clang.
+       * lib/verify.h (assume): Use clang’s __builtin_assume, with a temporary
+       variable in a statement expression.
+
 2020-08-22  Bruno Haible  <bruno@clisp.org>
 
        sig2str: Add more signals.
index d485a0283a972c4f247580064f07344044bea1bb..04bb2dfc334d36db4bbd69b40036bfb8982e1480 100644 (file)
@@ -246,6 +246,13 @@ template <int w>
 
 /* @assert.h omit start@  */
 
+#if defined __has_builtin
+/* <https://clang.llvm.org/docs/LanguageExtensions.html#builtin-functions> */
+# define _GL_HAS_BUILTIN_ASSUME __has_builtin (__builtin_assume)
+#else
+# define _GL_HAS_BUILTIN_ASSUME 0
+#endif
+
 #if 3 < __GNUC__ + (3 < __GNUC_MINOR__ + (4 <= __GNUC_PATCHLEVEL__))
 # define _GL_HAS_BUILTIN_TRAP 1
 #elif defined __has_builtin
@@ -305,14 +312,21 @@ template <int w>
 
    Although assuming R can help a compiler generate better code or
    diagnostics, performance can suffer if R uses hard-to-optimize
-   features such as function calls not inlined by the compiler.
-
-   Avoid Clang’s __builtin_assume, as clang 9.0.1 -Wassume can
-   generate a bogus diagnostic "the argument to '__builtin_assume' has
-   side effects that will be discarded" even when the argument has no
-   side effects.  */
-
-#if _GL_HAS_BUILTIN_UNREACHABLE
+   features such as function calls not inlined by the compiler.  */
+
+/* Use __builtin_assume in preference to __builtin_unreachable, because
+   in clang versions 8.0.x and older, the definition based on
+   __builtin_assume has an effect on optimizations, whereas the definition
+   based on __builtin_unreachable does not.  (GCC so far has only
+   __builtin_unreachable.)  */
+#if _GL_HAS_BUILTIN_ASSUME
+/* Use a temporary variable, to avoid a clang warning
+   "the argument to '__builtin_assume' has side effects that will be discarded"
+   if R contains invocations of functions not marked as 'const'.  */
+# define assume(R) \
+    ((void) ({ __typeof__ (R) _gl_verify_temp = (R); \
+               __builtin_assume (_gl_verify_temp); }))
+#elif _GL_HAS_BUILTIN_UNREACHABLE
 # define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
 #elif 1200 <= _MSC_VER
 # define assume(R) __assume (R)