public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/94173] New: [RISCV] Superfluous stackpointer manipulation
@ 2020-03-14  9:04 gcc at gms dot tf
  2020-03-16 22:21 ` [Bug target/94173] " wilson at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: gcc at gms dot tf @ 2020-03-14  9:04 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94173
           Summary: [RISCV] Superfluous stackpointer manipulation
           Product: gcc
           Version: 9.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at gms dot tf
  Target Milestone: ---

Created attachment 48033
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48033&action=edit
demonstrate how bar function yields superfluous stackpointer instructions

GCC emits stackpointer decrement/increment instructions in some functions where
they are superfluous.

Example:

struct Pair {
    char *s;
    char *t;
};
typedef struct Pair Pair;

Pair bar(char *s, char *t)
{
    return (Pair){s, t};
}

Expected assembly:

0000000000000002 <bar>:
   2:   8082                    ret


Actual assembly:

0000000000000002 <bar>:
   2:   1141                    addi    sp,sp,-16
   4:   0141                    addi    sp,sp,16
   6:   8082                    ret


Notes:

In an example where the function just returns one value the assembly code
actually just contain the expected assenbly code:


char *foo(char *s, char *t)
{
    return s;
}

Note that per the RISC-V calling-conventions up to 2 integer like values are
returned via the a0/a1 registers, i.e. the same registers where also the first
2 function arguments are placed.


FWIW, Clang 9.0.1 generates for both examples the expected code without
superfluous stackpointer adjustments.


Example code is attched.

I tested with:

riscv64-linux-gnu-gcc -O3 -c -o pair.o pair.c

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

* [Bug target/94173] [RISCV] Superfluous stackpointer manipulation
  2020-03-14  9:04 [Bug target/94173] New: [RISCV] Superfluous stackpointer manipulation gcc at gms dot tf
@ 2020-03-16 22:21 ` wilson at gcc dot gnu.org
  2020-03-16 22:21 ` wilson at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: wilson at gcc dot gnu.org @ 2020-03-16 22:21 UTC (permalink / raw)
  To: gcc-bugs

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

Jim Wilson <wilson at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |wilson at gcc dot gnu.org

--- Comment #1 from Jim Wilson <wilson at gcc dot gnu.org> ---
struct Pair has size 8 and align 4, and we have no unaligned load/store
support, so we are not able to allocate the temporary local variable to a
register.  It must be allocated a stack slot.  The RTL optimizer is able to
figure out that the stack stores and loads don't alias anything and hence are
not necessary and optimizes them away.  However, we don't have any support to
unallocate a stack slot after it has already been allocated, so we end up with
the unnecessary stack pointer increment and decrement.  In a degenerate case
like this, where there are no longer any stack loads/stores, we may be able to
notice that and get rid of the stack pointer manipulation.  But in a more
complicated case where there are multiple stack slots, and references to all
but one is optimized away, then we would still need the stack pointer change,
though we would just be wasting stack space in this case with larger
decrements/increments than needed.

If you change the type to
truct Pair {
    char *s;
    char *t;
} __attribute__ ((aligned(8)));
then you get the result you want.  That isn't a practical solution, but it
demonstrates that this is a size/alignment/strict-alignment problem.

This is more of a middle end problem than a RISC-V backend problem.  It should
be possible to reproduce on any target with similar strict alignment
constraints, and similar calling conventions that allow returning the structure
in registers, though I don't know if there are any offhand.

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

* [Bug target/94173] [RISCV] Superfluous stackpointer manipulation
  2020-03-14  9:04 [Bug target/94173] New: [RISCV] Superfluous stackpointer manipulation gcc at gms dot tf
  2020-03-16 22:21 ` [Bug target/94173] " wilson at gcc dot gnu.org
@ 2020-03-16 22:21 ` wilson at gcc dot gnu.org
  2020-03-16 22:27 ` andrew at sifive dot com
  2020-03-16 22:34 ` wilson at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: wilson at gcc dot gnu.org @ 2020-03-16 22:21 UTC (permalink / raw)
  To: gcc-bugs

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

Jim Wilson <wilson at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2020-03-16

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

* [Bug target/94173] [RISCV] Superfluous stackpointer manipulation
  2020-03-14  9:04 [Bug target/94173] New: [RISCV] Superfluous stackpointer manipulation gcc at gms dot tf
  2020-03-16 22:21 ` [Bug target/94173] " wilson at gcc dot gnu.org
  2020-03-16 22:21 ` wilson at gcc dot gnu.org
@ 2020-03-16 22:27 ` andrew at sifive dot com
  2020-03-16 22:34 ` wilson at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: andrew at sifive dot com @ 2020-03-16 22:27 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Waterman <andrew at sifive dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrew at sifive dot com

--- Comment #2 from Andrew Waterman <andrew at sifive dot com> ---
The same thing happens for MIPS N64, at least with the older version of GCC I
have installed (GCC 8.0.0 20170509).

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

* [Bug target/94173] [RISCV] Superfluous stackpointer manipulation
  2020-03-14  9:04 [Bug target/94173] New: [RISCV] Superfluous stackpointer manipulation gcc at gms dot tf
                   ` (2 preceding siblings ...)
  2020-03-16 22:27 ` andrew at sifive dot com
@ 2020-03-16 22:34 ` wilson at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: wilson at gcc dot gnu.org @ 2020-03-16 22:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jim Wilson <wilson at gcc dot gnu.org> ---
I was looking at the rv32 output.  For the rv64 compiler, you need to use
aligned(16).

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

end of thread, other threads:[~2020-03-16 22:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-14  9:04 [Bug target/94173] New: [RISCV] Superfluous stackpointer manipulation gcc at gms dot tf
2020-03-16 22:21 ` [Bug target/94173] " wilson at gcc dot gnu.org
2020-03-16 22:21 ` wilson at gcc dot gnu.org
2020-03-16 22:27 ` andrew at sifive dot com
2020-03-16 22:34 ` wilson 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).