bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

improve clang support (5)


From: Bruno Haible
Subject: improve clang support (5)
Date: Fri, 07 Aug 2020 02:14:23 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-186-generic; KDE/5.18.0; x86_64; ; )

clang (in version >= 4.0.0 at least) a '__builtin_assume' primitive [1],
that is more useful for implementing 'assume' than '__builtin_unreachable'
in clang versions < 9.0.0. Namely, in these versions of clang,
'__builtin_unreachable' has no effect on optimizations, whereas
'__builtin_assume' has. See:

============================= foo.c ===========================
int foo1 (int x)
{
  __builtin_assume (x > 10);
  return x > 7 ? x + 42 : 3 * x;
}

int foo2 (int x)
{
  (x > 10 ? (void) 0 : __builtin_unreachable ());
  return x > 7 ? x + 42 : 3 * x;
}
===============================================================

$ clang-8.0 -S -O foo.c && cat foo.s
        .text
        .file   "foo.c"
        .globl  foo1                    # -- Begin function foo1
        .p2align        4, 0x90
        .type   foo1,@function
foo1:                                   # @foo1
        .cfi_startproc
# %bb.0:
                                        # kill: def $edi killed $edi def $rdi
        leal    42(%rdi), %eax
        retq
.Lfunc_end0:
        .size   foo1, .Lfunc_end0-foo1
        .cfi_endproc
                                        # -- End function
        .globl  foo2                    # -- Begin function foo2
        .p2align        4, 0x90
        .type   foo2,@function
foo2:                                   # @foo2
        .cfi_startproc
# %bb.0:
                                        # kill: def $edi killed $edi def $rdi
        leal    42(%rdi), %ecx
        cmpl    $7, %edi
        leal    (%rdi,%rdi,2), %eax
        cmovgl  %ecx, %eax
        retq
.Lfunc_end1:
        .size   foo2, .Lfunc_end1-foo2
        .cfi_endproc
                                        # -- End function

        .ident  "clang version 8.0.0 (tags/RELEASE_800/final)"
        .section        ".note.GNU-stack","",@progbits
        .addrsig


[1] https://clang.llvm.org/docs/LanguageExtensions.html#builtin-functions


2020-08-06  Bruno Haible  <bruno@clisp.org>

        Use __builtin_assume with clang.
        * lib/verify.h (_GL_HAS_BUILTIN_ASSUME): New macro.
        (assume): Use __builtin_assume when available.

diff --git a/lib/verify.h b/lib/verify.h
index f109761..58172f3 100644
--- a/lib/verify.h
+++ b/lib/verify.h
@@ -233,6 +233,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
@@ -294,7 +301,9 @@ template <int w>
    diagnostics, performance can suffer if R uses hard-to-optimize
    features such as function calls not inlined by the compiler.  */
 
-#if _GL_HAS_BUILTIN_UNREACHABLE
+#if _GL_HAS_BUILTIN_ASSUME
+# define assume(R) __builtin_assume (R)
+#elif _GL_HAS_BUILTIN_UNREACHABLE
 # define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
 #elif 1200 <= _MSC_VER
 # define assume(R) __assume (R)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]