This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug target/69008] gcc emits unneeded memory access when passing trivial structs by value (ARM)


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

Renlin Li <renlin at gcc dot gnu.org> changed:

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

--- Comment #2 from Renlin Li <renlin at gcc dot gnu.org> ---
This relate to the strict alignment for aarch32 target. The structure is
treated as BLKmode and will be stored in the stack first.

However, I believe that this actually can be optimized by DSE pass, which will
forward the value to the ADD operation directly eliminate the store. However,
It seems it's unable to recognize the opportunities here. 

For example the following modified test case:

struct Trivial {
    short i1;
    short i2;
};

int foo(Trivial t)
{
    return t.i1 + t.i2;
}

The expand will emits the following code, which still stores the structure into
stack first. However, DSE can optimized it and remove insn 2.

(insn 2 4 3 2 (set (mem/c:SI (plus:SI (reg/f:SI 105 virtual-stack-vars)
                (const_int -4 [0xfffffffffffffffc])) [1  S4 A32])
        (reg:SI 0 r0)) test.c:7 -1
     (nil))
(note 3 2 6 2 NOTE_INSN_FUNCTION_BEG)
(insn 6 3 7 2 (set (reg:SI 116)
        (sign_extend:SI (mem/c:HI (plus:SI (reg/f:SI 105 virtual-stack-vars)
                    (const_int -4 [0xfffffffffffffffc])) [2 t.i1+0 S2 A32])))
test.c:8 -1
     (nil))
(insn 7 6 8 2 (set (reg:SI 117)
        (sign_extend:SI (mem/c:HI (plus:SI (reg/f:SI 105 virtual-stack-vars)
                    (const_int -2 [0xfffffffffffffffe])) [2 t.i2+0 S2 A16])))
test.c:8 -1
     (nil))
(insn 8 7 9 2 (set (reg:SI 115)
        (plus:SI (reg:SI 116)
            (reg:SI 117))) test.c:8 -1
     (nil))


On the other hand, if the original test case is compiled with -mabi=apcs-gnu,
it will produce exactly the same code-gen as clang does.
"-mabi=apcs-gnu" will change the target BIGGEST_ALIGNMENT macro to 32.
In this case, the structure will be treated as scalar DImode. It will no longer
stored on the stack any more. The expand will emit different code from the very
beginning.

(insn 6 3 7 2 (set (reg:SI 114)
        (plus:SI (subreg:SI (reg/v:DI 113 [ t ]) 0)
            (subreg:SI (reg/v:DI 113 [ t ]) 4))) new.c:8 -1
     (nil))
(insn 7 6 11 2 (set (reg:SI 112 [ <retval> ])
        (reg:SI 114)) new.c:8 -1
     (nil))
(insn 11 7 12 2 (set (reg/i:SI 0 r0)
        (reg:SI 112 [ <retval> ])) new.c:9 -1
     (nil))
(insn 12 11 0 2 (use (reg/i:SI 0 r0)) new.c:9 -1
     (nil))

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]