From 452fee36d88161a591d3fa3081445220f1a08442 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 23 Aug 2020 00:52:39 +0200 Subject: [PATCH] verify: Do use __builtin_assume on clang. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * lib/verify.h (assume): Use clang’s __builtin_assume, with a temporary variable in a statement expression. --- ChangeLog | 6 ++++++ lib/verify.h | 30 ++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index e89966ab71..3d83db226f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2020-08-22 Bruno Haible + + 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 sig2str: Add more signals. diff --git a/lib/verify.h b/lib/verify.h index d485a0283a..04bb2dfc33 100644 --- a/lib/verify.h +++ b/lib/verify.h @@ -246,6 +246,13 @@ template /* @assert.h omit start@ */ +#if defined __has_builtin +/* */ +# 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 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) -- 2.39.5