bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: Pip Cet <pipcet@gmail.com>
Cc: 36370@debbugs.gnu.org, Paul Eggert <eggert@cs.ucla.edu>,
	bug-gnulib@gnu.org
Subject: Re: bug#36370: 27.0.50; XFIXNAT called on negative numbers
Date: Fri, 28 Jun 2019 14:14:14 +0200	[thread overview]
Message-ID: <8979488.cRkkfcT1mV@omega> (raw)
In-Reply-To: <CAOqdjBdt0p8QFwQK8GtO=v25kn_MGFMLhmoBdb1mL2CESea=AQ@mail.gmail.com>

Pip Cet wrote:
> Or, more realistically:
> 
> extern int potentially_inlined_function(int i);
> 
> int main(void)
> {
>   ...
>   eassume(potentially_inlined_function(i));
>   return i >= 0;
> }

OK, I see...

> This makes it safe to use function expressions in eassume, whether the
> function is inlined or not.

By "safe" you mean that you want the function call to not be evaluated.

You are mentioning a limitation:

> eassume(i >= 0 && i < complicated_function ());
> 
> will not "split" the && expression, so it'll behave differently from
> 
> eassume(i >= 0);
> eassume(i < complicated_function ());

And I would mention a regression: When -flto is in use and the expression
invokes an external potentially-inlined function, the old 'assume' would
work fine, i.e. do optimizations across compilation-unit boundaries.
Whereas the new 'assume' does not.

Test case:
================================ foo.c =================================
#include <stdio.h>

#define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
//#define assume(R) (!__builtin_constant_p (!(R) == !(R)) || (R) ? (void) 0 : __builtin_unreachable ())

extern int complicated (int i);
extern int nonnegative (int i);

int f_generic (int i)
{
  printf("%d\n", i & 0x80000000);
  return 0;
}

int f_condition (int i)
{
  if (complicated (i) && i >= 0)
    printf("%d\n", i & 0x80000000);
  return 0;
}

int f_assume (int i)
{
  assume (complicated (i) && i >= 0);
  printf("%d\n", i & 0x80000000);
  return 0;
}
================================= bar.c ================================
int complicated (int i) { return (i & 7) == 3; }
int nonnegative (int i) { return i >= 0; }
========================================================================
$ gcc -O2 -m32 -flto foo.c bar.c -shared -o libfoo.so && objdump --disassemble libfoo.so

With the old 'assume':

000005f0 <f_assume>:
 5f0:   83 ec 10                sub    $0x10,%esp
 5f3:   6a 00                   push   $0x0
 5f5:   68 74 06 00 00          push   $0x674
 5fa:   6a 01                   push   $0x1
 5fc:   e8 fc ff ff ff          call   5fd <f_assume+0xd>
 601:   31 c0                   xor    %eax,%eax
 603:   83 c4 1c                add    $0x1c,%esp
 606:   c3                      ret    
 607:   89 f6                   mov    %esi,%esi
 609:   8d bc 27 00 00 00 00    lea    0x0(%edi,%eiz,1),%edi

With the new 'assume':

00000610 <f_generic>:
 610:   83 ec 10                sub    $0x10,%esp
 613:   8b 44 24 14             mov    0x14(%esp),%eax
 617:   25 00 00 00 80          and    $0x80000000,%eax
 61c:   50                      push   %eax
 61d:   68 48 06 00 00          push   $0x648
 622:   6a 01                   push   $0x1
 624:   e8 fc ff ff ff          call   625 <f_generic+0x15>
 629:   31 c0                   xor    %eax,%eax
 62b:   83 c4 1c                add    $0x1c,%esp
 62e:   c3                      ret    
 62f:   90                      nop

00000630 <f_assume>:
 630:   eb de                   jmp    610 <f_generic>

> But even in those cases, this approach is better than the old approach
> of actually evaluating complicated_function.

I disagree that it is better:
  1. The new 'assume' is worse when -flto is in use.
  2. You recommend to users to split assume(A && B) into assume(A); assume(B);
     which is unnatural.

> At first, I thought it would be better to have a __builtin_assume
> expression at the GCC level, but even that would have to have "either
> evaluate the entire condition expression, or evaluate none of it"
> semantics.

No. At GCC level, it could have a "make the maximum of inferences - across
all optimization phases -, but evaluate none of it" semantics.

Bruno



  reply	other threads:[~2019-06-28 12:32 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAOqdjBcM09RbDv19xNF7HxmykU2oAJ4Vsm45Y65aYXZbOO9u3g@mail.gmail.com>
     [not found] ` <e7d67132-4c2e-5c3a-74ae-78c8d67b8132@cs.ucla.edu>
     [not found]   ` <CAOqdjBct1qJ43dAL5642B52ZXH9M1x_qYOZX3GzJi6YvckoS7Q@mail.gmail.com>
     [not found]     ` <de8a8fa5-176c-f22a-fa56-c5d54fd42352@cs.ucla.edu>
     [not found]       ` <CAOqdjBd7FXkSd=brysRa8bc+o5uHTBshQ2XxQ2ZSyt=naJgp0g@mail.gmail.com>
     [not found]         ` <7ef599ae-0a1d-e86f-2bed-a1503455833f@cs.ucla.edu>
     [not found]           ` <CAOqdjBcyT17XDSAEm2NVtFbJLyEc4m9jj_9sX-nyOUKca2aUwA@mail.gmail.com>
2019-06-27 21:13             ` bug#36370: 27.0.50; XFIXNAT called on negative numbers Paul Eggert
2019-06-27 21:37               ` Pip Cet
2019-06-27 23:45               ` Bruno Haible
2019-06-28  0:04                 ` Paul Eggert
2019-06-28 11:06                 ` Pip Cet
2019-06-28 12:14                   ` Bruno Haible [this message]
2019-06-28 12:29                     ` Bruno Haible
2019-06-28 13:51                     ` Pip Cet
2019-06-28 17:46                       ` Paul Eggert
2019-06-28 19:15                         ` Pip Cet
2019-06-28 19:56                           ` Bruno Haible
2019-06-28 21:08                             ` Pip Cet
2019-06-29  5:41                           ` Paul Eggert
2019-06-29  6:48                             ` Pip Cet
2019-06-29 17:31                               ` Paul Eggert
2019-06-30  9:21                                 ` Pip Cet
2019-06-28 19:11                       ` Bruno Haible
2019-06-28 21:07                         ` Pip Cet
2019-06-28 23:30                           ` Bruno Haible
2019-06-29  5:40                             ` Paul Eggert
2019-06-29  5:44                             ` Pip Cet
2019-06-29 10:31                               ` Bruno Haible
2019-06-29 17:11                                 ` Paul Eggert
2019-06-29 17:48                                   ` Bruno Haible
2019-06-30 15:30                                 ` Pip Cet
2019-06-30 15:45                                   ` Bruno Haible
2019-07-02 23:39                                     ` Paul Eggert
2019-07-01  1:46                                   ` Richard Stallman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8979488.cRkkfcT1mV@omega \
    --to=bruno@clisp.org \
    --cc=36370@debbugs.gnu.org \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    --cc=pipcet@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).