> This is OK, but please also mention in the comment that __builtin_assume > generates better code for Clang 8 than __builtin_unreachable does, as that's > the > only reason to put up with all this mess.
Done, as follows: 2020-08-22 Bruno Haible <br...@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. diff --git a/lib/verify.h b/lib/verify.h index d485a02..04bb2df 100644 --- a/lib/verify.h +++ b/lib/verify.h @@ -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)