public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/69008] gcc emits unneeded memory access when passing trivial structs by value (ARM)
       [not found] <bug-69008-4@http.gcc.gnu.org/bugzilla/>
@ 2020-03-21 22:36 ` david.forgeas at gmail dot com
  2020-06-04 12:41 ` pb at gcc dot gnu.org
  2021-05-04 12:31 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 3+ messages in thread
From: david.forgeas at gmail dot com @ 2020-03-21 22:36 UTC (permalink / raw)
  To: gcc-bugs

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

David Forgeas <david.forgeas at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |david.forgeas at gmail dot com

--- Comment #7 from David Forgeas <david.forgeas at gmail dot com> ---
The use case that made me see this is std::string_view, or equivalently:

namespace my {
        struct string_view {
                const char *data;
                unsigned long length;
                const char & back() const {
                        return data[length - 1];
                }
        };
}
const char *back(my::string_view const sv)
{
        return &sv.back();
}

pi@raspberrypi:~ $ g++ -O3 -pipe -S string_view.cpp -std=c++1z
pi@raspberrypi:~ $ cat string_view.s 
        .arch armv6
        .eabi_attribute 28, 1
        .eabi_attribute 20, 1
        .eabi_attribute 21, 1
        .eabi_attribute 23, 3
        .eabi_attribute 24, 1
        .eabi_attribute 25, 1
        .eabi_attribute 26, 2
        .eabi_attribute 30, 2
        .eabi_attribute 34, 1
        .eabi_attribute 18, 4
        .file   "string_view.cpp"
        .text
        .align  2
        .global _Z4backN2my11string_viewE
        .syntax unified
        .arm
        .fpu vfp
        .type   _Z4backN2my11string_viewE, %function
_Z4backN2my11string_viewE:
        .fnstart
.LFB1:
        @ args = 0, pretend = 0, frame = 8
        @ frame_needed = 0, uses_anonymous_args = 0
        @ link register save eliminated.
        sub     sp, sp, #8
        add     r3, sp, #8
        stmdb   r3, {r0, r1}
        ldm     sp, {r0, r3}
        sub     r3, r3, #1
        add     r0, r0, r3
        add     sp, sp, #8
        @ sp needed
        bx      lr
        .cantunwind
        .fnend
        .size   _Z4backN2my11string_viewE, .-_Z4backN2my11string_viewE
        .ident  "GCC: (Raspbian 6.3.0-18+rpi1+deb9u1) 6.3.0 20170516"


Also seen in a more recent version: https://godbolt.org/z/3LJ9SN

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

* [Bug middle-end/69008] gcc emits unneeded memory access when passing trivial structs by value (ARM)
       [not found] <bug-69008-4@http.gcc.gnu.org/bugzilla/>
  2020-03-21 22:36 ` [Bug middle-end/69008] gcc emits unneeded memory access when passing trivial structs by value (ARM) david.forgeas at gmail dot com
@ 2020-06-04 12:41 ` pb at gcc dot gnu.org
  2021-05-04 12:31 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 3+ messages in thread
From: pb at gcc dot gnu.org @ 2020-06-04 12:41 UTC (permalink / raw)
  To: gcc-bugs

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

Philip Blundell <pb at gcc dot gnu.org> changed:

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

--- Comment #8 from Philip Blundell <pb at gcc dot gnu.org> ---
There seem to be a few different things going on here.

With the current state of master, I no longer get the redundant ldm using the
testcase from comment #7.  But the store is still there:

.LFB1:
        @ args = 0, pretend = 0, frame = 8
        @ frame_needed = 0, uses_anonymous_args = 0
        @ link register save eliminated.
        sub     sp, sp, #8
        add     r3, sp, #8
        stmdb   r3, {r0, r1}
        sub     r3, r1, #1
        add     sp, sp, #8
        add     r0, r0, r3
        @ sp needed
        bx      lr

A brief inspection of the rtl suggests that the way that we wrap multiple
stores in a PARALLEL might be preventing DSE from deleting the dead store.  If
I hack arm.md to disable the define_expand "store_multiple" then the store goes
away:

.LFB1:
        @ args = 0, pretend = 0, frame = 8
        @ frame_needed = 0, uses_anonymous_args = 0
        @ link register save eliminated.
        sub     sp, sp, #8
        sub     r1, r1, #1
        add     sp, sp, #8
        add     r0, r0, r1
        @ sp needed
        bx      lr

But we're still left with the useless stack pointer manipulation.  As Andrew
said in comment #4, what seems to have happened here is that we reserve a stack
slot for the incoming args because they are BLKmode and, even though all the
memory accesses end up being deleted, the space in the stack frame is still
reserved and sp gets bumped to accommodate it.

The memory references to the stack frame have been eliminated before the
prologue and epilogue are generated so presumably it wouldn't be impossible to
detect that the frame is no longer actually needed at this point.  But I don't
know how hard this would be to do in the general case.

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

* [Bug middle-end/69008] gcc emits unneeded memory access when passing trivial structs by value (ARM)
       [not found] <bug-69008-4@http.gcc.gnu.org/bugzilla/>
  2020-03-21 22:36 ` [Bug middle-end/69008] gcc emits unneeded memory access when passing trivial structs by value (ARM) david.forgeas at gmail dot com
  2020-06-04 12:41 ` pb at gcc dot gnu.org
@ 2021-05-04 12:31 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-05-04 12:31 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED

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

end of thread, other threads:[~2021-05-04 12:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-69008-4@http.gcc.gnu.org/bugzilla/>
2020-03-21 22:36 ` [Bug middle-end/69008] gcc emits unneeded memory access when passing trivial structs by value (ARM) david.forgeas at gmail dot com
2020-06-04 12:41 ` pb at gcc dot gnu.org
2021-05-04 12:31 ` rguenth 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).