public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "mschiffer@universe-factory.net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/84790] Miscompilation for MIPS16 with -fpic and -Os or -O2
Date: Mon, 27 May 2024 18:20:31 +0000	[thread overview]
Message-ID: <bug-84790-4-9Mh5mlLO1U@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-84790-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #13 from Matthias Schiffer <mschiffer@universe-factory.net> ---
I don't think the register used matters -  changing it may hide the bug in
specific instances, but it does not fix the root cause.

I've now built a simpler reproducer which still seems to exhibit the same issue
with your latest patch (however I've only built a baremetal GCC with your patch
and looked at the generated code, I've not actually run this example on the
affected platforms - I might be overlooking something. Will try to get a full
toolchain build in the next days).

The basic premise of the following code:

In test(), the return value `ret` must be moved from v0 to a different register
temporarily for calling foo(). Using the inline asm, GCC is nudged to use v1 as
this temporary register.

As GCC knows the contents of foo() and bar(), it assumes that the value of v1
is preserved across the call to foo(). This assumption is wrong because the gp
setup code is inserted at the beginning of bar after all optimization and
register allocation has already happened. As mentioned before, this setup code
clobbers v1.

```
unsigned ext(void);

__attribute__((noinline))
static void foo(void) {
        /* Do not let the optimizer remove foo and bar */
        asm volatile("");
}

__attribute__((noinline))
static void bar(void) {
        foo();
}

unsigned test(void)
{
        unsigned ret = ext();

        register unsigned v1 asm("v1") = ret;
        asm volatile("" :: "r"(v1));

        bar();

        return ret;
}
```

`objdump -d -r` output (built using GCC commit
05daf617ea22e1d818295ed2d037456937e23530, with "-Os -mips32r2 -mtune=24kc
-mabicalls -mips16 -fpic"):

```
Disassembly of section .text:

00000000 <foo>:
   0:   e8a0            jrc     ra
   2:   6500            nop

00000004 <bar>:
   4:   f000 6a00       li      v0,0
                        4: R_MIPS16_HI16        _gp_disp
   8:   f000 0b00       la      v1,8 <bar+0x4>
                        8: R_MIPS16_LO16        _gp_disp
   c:   f400 3240       sll     v0,16
  10:   e269            addu    v0,v1
  12:   64c4            save    32,ra
  14:   659a            move    gp,v0
  16:   d204            sw      v0,16(sp)
  18:   675c            move    v0,gp
  1a:   f000 9a40       lw      v0,0(v0)
                        1a: R_MIPS16_GOT16      foo
  1e:   f000 4a00       addiu   v0,0
                        1e: R_MIPS16_LO16       foo
  22:   ea40            jalr    v0
  24:   653a            move    t9,v0
  26:   6444            restore 32,ra
  28:   e8a0            jrc     ra
  2a:   6500            nop

0000002c <test>:
  2c:   f000 6a00       li      v0,0
                        2c: R_MIPS16_HI16       _gp_disp
  30:   f000 0b00       la      v1,30 <test+0x4>
                        30: R_MIPS16_LO16       _gp_disp
  34:   f400 3240       sll     v0,16
  38:   e269            addu    v0,v1
  3a:   659a            move    gp,v0
  3c:   64e4            save    32,ra,s0
  3e:   671c            move    s0,gp
  40:   d204            sw      v0,16(sp)
  42:   f000 9840       lw      v0,0(s0)
                        42: R_MIPS16_CALL16     ext
  46:   ea40            jalr    v0
  48:   653a            move    t9,v0
  4a:   6762            move    v1,v0
  4c:   f000 9800       lw      s0,0(s0)
                        4c: R_MIPS16_GOT16      bar
  50:   f000 4800       addiu   s0,0
                        50: R_MIPS16_LO16       bar
  54:   e840            jalr    s0
  56:   6538            move    t9,s0
  58:   6464            restore 32,ra,s0
  5a:   e820            jr      ra
  5c:   6743            move    v0,v1
  5e:   6500            nop
```

At 4a, the return value is moved to v1. At 5c, it is supposed to be moved back,
but v1 has been clobbered in the mean time.

  parent reply	other threads:[~2024-05-27 18:20 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-84790-4@http.gcc.gnu.org/bugzilla/>
2024-05-22 14:28 ` syq at gcc dot gnu.org
2024-05-25 16:37 ` syq at gcc dot gnu.org
2024-05-26  0:05 ` mschiffer@universe-factory.net
2024-05-26  3:42 ` syq at gcc dot gnu.org
2024-05-26  3:45 ` syq at gcc dot gnu.org
2024-05-26  8:55 ` mschiffer@universe-factory.net
2024-05-27  1:34 ` syq at gcc dot gnu.org
2024-05-27  2:10 ` syq at gcc dot gnu.org
2024-05-27 18:20 ` mschiffer@universe-factory.net [this message]
2024-05-28  7:26 ` syq at gcc dot gnu.org
2024-05-29 17:16 ` cvs-commit at gcc dot gnu.org
2024-05-29 17:17 ` syq at gcc dot gnu.org
2024-05-29 17:44 ` mschiffer@universe-factory.net
2024-05-30  1:48 ` cvs-commit at gcc dot gnu.org
2024-05-30  2:11 ` cvs-commit at gcc dot gnu.org
2024-05-30  2:11 ` cvs-commit at gcc dot gnu.org
2024-05-30  2:17 ` 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-84790-4-9Mh5mlLO1U@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).