This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: __attribute__((optimize("-O2"))) doesn't work
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Chaoran Yang <chaoran at rice dot edu>
- Cc: Oleg Endo <oleg dot endo at t-online dot de>, gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Tue, 23 Dec 2014 08:29:32 +0000
- Subject: Re: __attribute__((optimize("-O2"))) doesn't work
- Authentication-results: sourceware.org; auth=none
- References: <127B0179-8F1C-4CE6-8318-33E8AA76E7A6 at rice dot edu> <1419284351 dot 8915 dot 89 dot camel at yam-132-YW-E178-FTW> <C854C95C-19C9-476A-80CD-7C4E123758F2 at rice dot edu> <1419284923 dot 8915 dot 91 dot camel at yam-132-YW-E178-FTW> <20645553-3EEF-427B-831D-5C082DB67478 at rice dot edu> <CAH6eHdQGYqu68jvtreGqASRCrnYQRNRT+SXhSv1Jn3zA7qBT4g at mail dot gmail dot com> <4368062E-9D29-498B-9353-01209A46BE9C at rice dot edu>
On 23 December 2014 at 00:27, Chaoran Yang <chaoran@rice.edu> wrote:
>> On Dec 22, 2014, at 5:59 PM, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>>
>>> Shouldnât it be optimized to:
>>>
>>> 0x0000000000400530 <+0>: mov $0x2,%eax
>>> 0x0000000000400535 <+5>: retq
>>>
>>> just like I compiled it with "gcc -O2 t.câ?
>>
>> When you compile the whole file with -O2 you also compile main() with
>> -O2, which is not the same as just optimising one function.
>
> Even if I take out the main function, using __attribute__((optimize(âO2â))) is still not the same as -O2. See the following output.
>
> $ cat t.c
> int foo()
> {
> return 2;
> }
> $ gcc -c -O2 t.c
> $ gdb t.o
> (gdb) disas foo
> Dump of assembler code for function foo:
> 0x0000000000000000 <+0>: mov $0x2,%eax <==== this is compiled with -O2
> 0x0000000000000005 <+5>: retq
> End of assembler dump.
>
> $ gcc -c t.c
> $ gdb t.o
> (gdb) disas foo
> Dump of assembler code for function foo:
> 0x0000000000000000 <+0>: push %rbp <==== this is compiled with no optimization
> 0x0000000000000001 <+1>: mov %rsp,%rbp
> 0x0000000000000004 <+4>: mov $0x2,%eax
> 0x0000000000000009 <+9>: pop %rbp
> 0x000000000000000a <+10>: retq
> End of assembler dump.
>
> $ cat t.c
> int foo() __attribute__((optimize("O2"))); <==== Adding the __attribute__
> int foo()
> {
> return 2;
> }
> $ gcc -c t.c
> $ gdb t.o
> (gdb) disas foo
> Dump of assembler code for function foo:
> 0x0000000000000000 <+0>: push %rbp <==== output is the same as compiled with no optimization
> 0x0000000000000001 <+1>: mov $0x2,%eax
> 0x0000000000000006 <+6>: mov %rsp,%rbp
> 0x0000000000000009 <+9>: pop %rbp
> 0x000000000000000a <+10>: retq
> End of assembler dump.
>
>> But I believe unless you use some -Ox option on the command line the
>> optimize attribute has no effect (which is true for optimization
>> options like -finline-functions). You have to enable optimization in
>> the first place before you can control the optimization level.
>
> I donât this this is true, at least not true for -fomit-frame-pointer. I've tested __attribute__((optimize(â-fomit-frame-pointerâ))), which works regardless optimization is turned on or not.
Well then try testing it with something less pointless than "return 2"
N.B. that should be optimize("omit-frame-pointer"), you can grep in
the testsuite to find the correct syntax to use.