public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "gcc at alanwu dot email" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/101868] New: Incorrect reordering in -O2 with LTO
Date: Wed, 11 Aug 2021 23:54:12 +0000	[thread overview]
Message-ID: <bug-101868-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101868

            Bug ID: 101868
           Summary: Incorrect reordering in -O2 with LTO
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at alanwu dot email
  Target Milestone: ---

GCC with LTO seems to be hoisting a memory read to a place too early. It only
seems to reproduce with LTO, so please excuse posting multiple files.

Compile command: gcc -flto -O2 -fno-strict-aliasing one.c two.c three.c four.c

//--------------- one.c --------------------------------
typedef unsigned long VALUE;

__attribute__ ((cold))
void rb_check_type(VALUE, int);

static VALUE
repro(VALUE dummy, VALUE hash)
{
    if (hash == 0) {
        rb_check_type(hash, 1);
    }
    else if (*(long *)hash) {
        rb_check_type(hash, 1);
    }


    return *(long *)hash;
}

static VALUE (*that)(VALUE dummy, VALUE hash) = repro;

int
main(int argc, char **argv)
{
        argc--;
        that(0, argc);

        rb_check_type(argc, argc);

}
//------------ end of one.c ----------------------------

//------------ two.c -----------------------------------
typedef unsigned long VALUE;


__attribute__ ((noreturn)) void rexc_raise(VALUE mesg);

VALUE rb_donothing(VALUE klass);

static void
funexpected_type(VALUE x, int xt, int t)
{
    rexc_raise(rb_donothing(0));
}

__attribute__ ((cold))
void
rb_check_type(VALUE x, int t)
{
    int xt;

    if (x == 0) {
        funexpected_type(x, xt, t);
    }
}
//------------- end of two.c ---------------------------

//------------ three.c ---------------------------------
typedef unsigned long VALUE;

static void thing(void) {}
static void (*ptr)(void) = &thing;

VALUE
rb_donothing(VALUE klass)
{
        ptr();
        return 0;
}
//-------- end of three.c ------------------------------

//-------- four.c --------------------------------------
typedef unsigned long VALUE;

__attribute__((noreturn))
void
rexc_raise(VALUE mesg)
{
        __builtin_exit(42);
}
//------------- end of four.c --------------------------


The code for repo() reads from memory before doing the check for zero: 
   0x00000000004011a0 <+0>:     sub    $0x18,%rsp
=> 0x00000000004011a4 <+4>:     mov    (%rsi),%rax
   0x00000000004011a7 <+7>:     test   %rsi,%rsi
   0x00000000004011aa <+10>:    je     0x401051 <repro.cold>
   0x00000000004011b0 <+16>:    test   %rax,%rax
   0x00000000004011b3 <+19>:    jne    0x401067 <repro.cold+22>
   0x00000000004011b9 <+25>:    add    $0x18,%rsp
   0x00000000004011bd <+29>:    ret

Here is the output of gcc -v. I'm using the 11.2.0 Docker Hub image.

    Using built-in specs.
    COLLECT_GCC=gcc
   
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-linux-gnu/11.2.0/lto-wrapper
    Target: x86_64-linux-gnu
    Configured with: /usr/src/gcc/configure --build=x86_64-linux-gnu
--disable-multilib --enable-languages=c,c++,fortran,go
    Thread model: posix
    Supported LTO compression algorithms: zlib
    gcc version 11.2.0 (GCC)

             reply	other threads:[~2021-08-11 23:54 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-11 23:54 gcc at alanwu dot email [this message]
2021-08-12  0:21 ` [Bug tree-optimization/101868] " pinskia at gcc dot gnu.org
2021-08-12  0:36 ` pinskia at gcc dot gnu.org
2021-08-12  8:25 ` marxin at gcc dot gnu.org
2021-08-12  8:28 ` marxin at gcc dot gnu.org
2021-08-12 15:09 ` [Bug tree-optimization/101868] [9/10/11 Regression] " pinskia at gcc dot gnu.org
2021-08-16  7:32 ` rguenth at gcc dot gnu.org
2021-08-16  7:57 ` marxin at gcc dot gnu.org
2021-08-16 21:18 ` mpolacek at gcc dot gnu.org
2021-08-17  6:24 ` rguenth at gcc dot gnu.org
2021-08-17  6:41 ` rguenth at gcc dot gnu.org
2021-08-17  9:21 ` cvs-commit at gcc dot gnu.org
2021-08-17  9:22 ` [Bug tree-optimization/101868] [9/10 " rguenth at gcc dot gnu.org
2021-08-17  9:24 ` cvs-commit at gcc dot gnu.org
2021-10-13 11:09 ` cvs-commit at gcc dot gnu.org
2021-11-08 14:07 ` [Bug tree-optimization/101868] [9 " cvs-commit at gcc dot gnu.org
2021-11-08 14:08 ` rguenth at gcc dot gnu.org
2022-06-24 20:25 ` cvs-commit at gcc dot gnu.org

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=bug-101868-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).