+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.
/* @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
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)