public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/101868] New: Incorrect reordering in -O2 with LTO
@ 2021-08-11 23:54 gcc at alanwu dot email
  2021-08-12  0:21 ` [Bug tree-optimization/101868] " pinskia at gcc dot gnu.org
                   ` (16 more replies)
  0 siblings, 17 replies; 18+ messages in thread
From: gcc at alanwu dot email @ 2021-08-11 23:54 UTC (permalink / raw)
  To: gcc-bugs

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)

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2022-06-24 20:25 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-11 23:54 [Bug c/101868] New: Incorrect reordering in -O2 with LTO gcc at alanwu dot email
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

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).