public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jan Hubicka <hubicka@ucw.cz>
To: Jeff Law <jeffreyalaw@gmail.com>
Cc: Richard Biener <rguenther@suse.de>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] Improve DSE to handle stores before __builtin_unreachable ()
Date: Mon, 26 Jun 2023 19:21:03 +0200	[thread overview]
Message-ID: <ZJnI/1HDFDCFo6hs@kam.mff.cuni.cz> (raw)
In-Reply-To: <7bddf09a-1535-8bcb-94da-8ad18b51963f@gmail.com>

Hi,
playing with testcases for path isolation and const function, I noticed
that we do not seem to even try to isolate out of range array accesses:
int a[3]={0,1,2};
test(int i)
{
       if (i > 3)
         return test2(a[i]);
       return a[i];
}

Here call to test2 is dead, since a[i] will access memory past of the
array.  We produce a warning:

t.c:5:24: warning: array subscript 4 is above array bounds of ‘int[3]’ [-Warray-bounds=]

but we still keep the call:

test:
.LFB0:
        .cfi_startproc
        movslq  %edi, %rax
        movl    a(,%rax,4), %eax
        cmpl    $3, %edi
        jg      .L4
        ret
        .p2align 4,,10
        .p2align 3
.L4:
        movl    %eax, %edi
        xorl    %eax, %eax
        jmp     test2

We eventually move the load before conditional, but at path isolation
time it is still quite obvious the conditional being true invokes
undefined behaviour

int test (int i)
{
  int _1;
  int _2;
  int _6;
  int _8;
  
  <bb 2> [local count: 1073741824]:
  if (i_4(D) > 3)
    goto <bb 3>; [20.24%]
  else
    goto <bb 4>; [79.76%]

  <bb 3> [local count: 217325344]:
  _1 = a[i_4(D)];
  _8 = test2 (_1);
  goto <bb 5>; [100.00%]

  <bb 4> [local count: 856416481]:
  _6 = a[i_4(D)];
  
  <bb 5> [local count: 1073741824]:
  # _2 = PHI <_8(3), _6(4)>
  return _2;
} 

Curiously adjusting the testcase:

const int a[3]={0,1,2};
test(int i)
{
        if (i == 3)
                return test2(a[i]);
        return a[i];
}
no longer has undefined behaviour visible at isolate-paths
int test (int i)
{
  int _1;
  int _5;
  int _7;

  <bb 2> [local count: 1073741824]:
  if (i_3(D) == 3)
    goto <bb 3>; [11.56%]
  else
    goto <bb 4>; [88.44%]

  <bb 3> [local count: 124124552]:
  _7 = test2 (0);
  goto <bb 5>; [100.00%]

  <bb 4> [local count: 949617273]:
  _5 = a[i_3(D)];

  <bb 5> [local count: 1073741824]:
  # _1 = PHI <_7(3), _5(4)>
  return _1;
}
since we fold the load to 0.  It would perhaps help optimizers to keep info on undefined behaviour happening there.

Honza

  parent reply	other threads:[~2023-06-26 17:21 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20230620070009.C11983858D1E@sourceware.org>
2023-06-20 13:27 ` Jeff Law
2023-06-21  6:41   ` Richard Biener
2023-06-21  9:55     ` Jan Hubicka
2023-06-21 14:04     ` Jeff Law
2023-06-22  6:31       ` Richard Biener
2023-06-22 13:36         ` Jeff Law
2023-06-22 13:42           ` Jan Hubicka
2023-06-24 14:24             ` Jeff Law
2023-06-25 16:33               ` Jan Hubicka
2023-06-26 17:21   ` Jan Hubicka [this message]
2023-06-26 22:37     ` Jeff Law
2023-06-20  6:59 Richard Biener

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

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

  git send-email \
    --in-reply-to=ZJnI/1HDFDCFo6hs@kam.mff.cuni.cz \
    --to=hubicka@ucw.cz \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jeffreyalaw@gmail.com \
    --cc=rguenther@suse.de \
    /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).