public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Optimization difference between C and C++
@ 2019-07-17  9:09 BCartier
  2019-07-18 10:34 ` Andrew Haley
  0 siblings, 1 reply; 2+ messages in thread
From: BCartier @ 2019-07-17  9:09 UTC (permalink / raw)
  To: gcc-help

I  noticed a optimization difference between compiling a simple source code
with ARM GCC in C and C++. The C++ version seems to optimize a lot less
stack usage.

To demonstrate this problem, I compiled the following code with
arm-none-eabi-gcc version gcc-arm-none-eabi-8-2018-q4-major-win32, first
only -O2, then with -O2 -x c++ :

#include <stdio.h>
#include <stdint.h>

struct TestStruct
{
    uint32_t field1;
    uint32_t field2;
    uint32_t field3;
    uint32_t field4;
} ;

struct TestStruct initStructure(uint32_t f1, uint32_t f2, uint32_t f3,
uint32_t f4)
{
    struct TestStruct myStruct;
    myStruct.field1 = f1;
    myStruct.field2 = f2;
    myStruct.field3 = f3;
    
    myStruct.field4 = f4;

    printf("Temp Address %lx", &myStruct);

    return myStruct;
}

void doStuff(struct TestStruct myStruct)
{
    printf("f1 = %d, f2 = %d, f3 = %d, f4 = %d", myStruct.field1,
myStruct.field2, myStruct.field3, myStruct.field4);
}

int main(void)
{
    doStuff(initStructure(1,2,3,4));
    doStuff(initStructure(11,22,33,44));
    doStuff(initStructure(11,12,13,14));
    doStuff(initStructure(21,22,23,24));
}

Here are link to demonstrate the problem :

https://godbolt.org/z/PGCp2y Pure C

Line 44, stack usage is
sub sp, sp, #28

https://godbolt.org/z/7XfsQR C++

Line 37, stack usage is
sub sp, sp, #76

Adding multiple calls to the doStuff function in C does not change stack
usage, but in C++ it does.
I am curious on why this difference in optimization exists. Is it a bug or
is there C++ specification against such optimization ? Or is there an
optimization option I am not aware of that would change this behavior ?

Thanks in advance,
BCartier




--
Sent from: http://gcc.1065356.n8.nabble.com/gcc-Help-f629689.html

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

* Re: Optimization difference between C and C++
  2019-07-17  9:09 Optimization difference between C and C++ BCartier
@ 2019-07-18 10:34 ` Andrew Haley
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Haley @ 2019-07-18 10:34 UTC (permalink / raw)
  To: BCartier, gcc-help

On 7/17/19 10:09 AM, BCartier wrote:

> Adding multiple calls to the doStuff function in C does not change stack
> usage, but in C++ it does.
> I am curious on why this difference in optimization exists. Is it a bug or
> is there C++ specification against such optimization ? Or is there an
> optimization option I am not aware of that would change this behavior ?

It's a missed-optimization bug.

-- 
Andrew Haley  (he/him)
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
https://keybase.io/andrewhaley
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671

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

end of thread, other threads:[~2019-07-18 10:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-17  9:09 Optimization difference between C and C++ BCartier
2019-07-18 10:34 ` Andrew Haley

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