Subject: bug#36370: 27.0.50; XFIXNAT called on negative numbers

From: Paul Eggert <[email-masked]>
Date: Thu, 27 Jun 2019 14:13:14 -0700

On 6/27/19 12:56 PM, Pip Cet wrote:

> The eassume tells GCC i is nonnegative, since (!(i >= 0) == !(i >= 0))
> is indeed a constant.

Ah! Thanks, I didn't catch that subtle point. Would the attached patch 
to verify.h address that problem? This patch is for Gnulib, but would 
propagate into Emacs.

I tried this out with Emacs master and although it did change the 
machine code subtly I didn't have the patience to see whether the 
changes were likely to improve performance. The changes did grow the 
Emacs text segment from 2556193 to 2557657 bytes (a 0.06% growth), which 
is not a good sign. This was on Fedora 30 x86-64 with a default Emacs build.

I'll CC: this to bug-gnulib since it's a Gnulib issue. I have not 
installed this patch into Gnulib on savannah.
>From 9a5a83937544e7c127026fcf32030f7dbaa5766c Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Thu, 27 Jun 2019 14:01:53 -0700
Subject: [PATCH] =?UTF-8?q?verify:=20tweak=20=E2=80=98assume=E2=80=99=20pe?=
 =?UTF-8?q?rformance?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Suggested by Pip Cet (Bug#36370#30).
* lib/verify.h (assume): Use __builtin_constant_p to generate
better code in recent GCC.
---
 ChangeLog    | 7 +++++++
 lib/verify.h | 6 ++++--
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5ae108e25..7059f4f2b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2019-06-27  Paul Eggert  <[email-masked]>
+
+	verify: tweak ‘assume’ performance
+	Suggested by Pip Cet (Bug#36370#30).
+	* lib/verify.h (assume): Use __builtin_constant_p to generate
+	better code in recent GCC.
+
 2019-06-26  Paul Eggert  <[email-masked]>
 
 	strverscmp: sync from glibc
diff --git a/lib/verify.h b/lib/verify.h
index f8e4eff02..9b015c693 100644
--- a/lib/verify.h
+++ b/lib/verify.h
@@ -263,9 +263,11 @@ template <int w>
    accordingly.  R should not have side-effects; it may or may not be
    evaluated.  Behavior is undefined if R is false.  */
 
-#if (__has_builtin (__builtin_unreachable) \
+#if ((__has_builtin (__builtin_constant_p) \
+      && __has_builtin (__builtin_unreachable)) \
      || 4 < __GNUC__ + (5 <= __GNUC_MINOR__))
-# define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
+# define assume(R) (!__builtin_constant_p (!(R) == !(R)) || (R) \
+                    ? (void) 0 : __builtin_unreachable ())
 #elif 1200 <= _MSC_VER
 # define assume(R) __assume (R)
 #elif ((defined GCC_LINT || defined lint) \
-- 
2.21.0