public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Optimization flags not working (GCC 4.2.4)
@ 2009-04-03 19:31 slnc
  2009-04-03 21:45 ` John (Eljay) Love-Jensen
  0 siblings, 1 reply; 7+ messages in thread
From: slnc @ 2009-04-03 19:31 UTC (permalink / raw)
  To: gcc-help

I have a file called "main.c":

-- main.c ------------------------------------------
int main(void) {
	int i, j;

	for (i = 0; i < 10; i ++)
		j = 5;

	return 0;
}
----------------------------------------------------

When I compile it with "gcc -O1 -c -o main.o main.c" and I do an 
"objdump -d main.o" I get:

------------------------------------------------------
main.o:     file format elf64-x86-64

Disassembly of section .text:

0000000000000000 <main>:
    0:	31 c0                	xor    %eax,%eax
    2:	c3                   	retq
------------------------------------------------------


But when I try to use just only one optimization flag by compiling with 
"gcc -ftree-dce -c -o main.o main.c" the optimization flag seems to do 
nothing at all:

------------------------------------------------------
main.o:     file format elf64-x86-64

Disassembly of section .text:

0000000000000000 <main>:
    0:	55                   	push   %rbp
    1:	48 89 e5             	mov    %rsp,%rbp
    4:	c7 45 fc 00 00 00 00 	movl   $0x0,-0x4(%rbp)
    b:	eb 0b                	jmp    18 <main+0x18>
    d:	c7 45 f8 05 00 00 00 	movl   $0x5,-0x8(%rbp)
   14:	83 45 fc 01          	addl   $0x1,-0x4(%rbp)
   18:	83 7d fc 09          	cmpl   $0x9,-0x4(%rbp)
   1c:	7e ef                	jle    d <main+0xd>
   1e:	b8 00 00 00 00       	mov    $0x0,%eax
   23:	c9                   	leaveq
   24:	c3                   	retq
------------------------------------------------------


And even if I specify all the optimization flags that are enabled by -O1 
I get the same previous objdump. What am I doing wrong?

Thank you,

-- 
Juan Alonso

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

* RE: Optimization flags not working (GCC 4.2.4)
  2009-04-03 19:31 Optimization flags not working (GCC 4.2.4) slnc
@ 2009-04-03 21:45 ` John (Eljay) Love-Jensen
  2009-04-03 23:55   ` slnc
  0 siblings, 1 reply; 7+ messages in thread
From: John (Eljay) Love-Jensen @ 2009-04-03 21:45 UTC (permalink / raw)
  To: slnc, gcc-help

Hi Juan,

If you are not optimizing (-O0), the optimization -f flags have no effect.

HTH,
--Eljay

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

* Re: Optimization flags not working (GCC 4.2.4)
  2009-04-03 21:45 ` John (Eljay) Love-Jensen
@ 2009-04-03 23:55   ` slnc
  2009-04-04  0:31     ` David Daney
  0 siblings, 1 reply; 7+ messages in thread
From: slnc @ 2009-04-03 23:55 UTC (permalink / raw)
  To: John (Eljay) Love-Jensen; +Cc: gcc-help

Hi John,

Thank you but -O0 makes no difference.

gcc -c -g -O0 -fdefer-pop -fdelayed-branch -fguess-branch-probability 
-fcprop-registers -fif-conversion -fif-conversion2 -ftree-ccp -ftree-dce 
-ftree-dominator-opts -ftree-dse -ftree-ter -ftree-lrs -ftree-sra 
-ftree-copyrename -ftree-fre -ftree-ch -funit-at-a-time 
-fmerge-constants main.c

(those are all gcc 4.2.4 optimization flags for level -O1 according to 
the docs) generates the same unoptimized code. Changing -O0 with -O1 
generates the optimized version.

John (Eljay) Love-Jensen wrote:
> Hi Juan,
> 
> If you are not optimizing (-O0), the optimization -f flags have no effect.
> 
> HTH,
> --Eljay
> 


-- 
Juan Alonso

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

* Re: Optimization flags not working (GCC 4.2.4)
  2009-04-03 23:55   ` slnc
@ 2009-04-04  0:31     ` David Daney
  2009-04-04  0:47       ` slnc
  0 siblings, 1 reply; 7+ messages in thread
From: David Daney @ 2009-04-04  0:31 UTC (permalink / raw)
  To: slnc; +Cc: John (Eljay) Love-Jensen, gcc-help

slnc wrote:
> Hi John,
> 
> Thank you but -O0 makes no difference.
> 

That's what he just said.  If you use -O0 (the implied default) none of 
the optimizations are done.  Use -O1 (or -O2 or -O3) if you want 
optimizations done.

David Daney

> gcc -c -g -O0 -fdefer-pop -fdelayed-branch -fguess-branch-probability 
> -fcprop-registers -fif-conversion -fif-conversion2 -ftree-ccp -ftree-dce 
> -ftree-dominator-opts -ftree-dse -ftree-ter -ftree-lrs -ftree-sra 
> -ftree-copyrename -ftree-fre -ftree-ch -funit-at-a-time 
> -fmerge-constants main.c
> 
> (those are all gcc 4.2.4 optimization flags for level -O1 according to 
> the docs) generates the same unoptimized code. Changing -O0 with -O1 
> generates the optimized version.
> 
> John (Eljay) Love-Jensen wrote:
>> Hi Juan,
>>
>> If you are not optimizing (-O0), the optimization -f flags have no 
>> effect.
>>
>> HTH,
>> --Eljay
>>
> 
> 

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

* Re: Optimization flags not working (GCC 4.2.4)
  2009-04-04  0:31     ` David Daney
@ 2009-04-04  0:47       ` slnc
  2009-04-04  0:58         ` Robert William Fuller
  2009-04-04  1:01         ` Ian Lance Taylor
  0 siblings, 2 replies; 7+ messages in thread
From: slnc @ 2009-04-04  0:47 UTC (permalink / raw)
  To: David Daney; +Cc: John (Eljay) Love-Jensen, gcc-help

David Daney wrote:
> slnc wrote:
>> Hi John,
>>
>> Thank you but -O0 makes no difference.
>>
> 
> That's what he just said.  If you use -O0 (the implied default) none of 
> the optimizations are done.  Use -O1 (or -O2 or -O3) if you want 
> optimizations done.

Oh, I misunderstood him, sorry. It also doesn't work if I omit 
-O<anything>. So to sum it up, there is no way to compile a program in 
gcc enabling only one specific optimization flag. The only option is to 
use the "pre-packaged" sets of flags (-O1, -O2 or -O3) right?

I have tried disabling all -O1 flags just to check if I could select 
only one by disabling all the others but:

gcc -c -O1 -fno-defer-pop -fno-delayed-branch 
-fno-guess-branch-probability -fno-cprop-registers -fno-if-conversion 
-fno-if-conversion2 -fno-tree-ccp -fno-tree-dce -fno-tree-dominator-opts 
-fno-tree-dse -fno-tree-ter -fno-tree-lrs -fno-tree-sra 
-fno-tree-copyrename -fno-tree-fre -fno-tree-ch -fno-unit-at-a-time 
-fno-merge-constants main.c
objdump -d dce.o

generates an optimized version. So why are the flags there if they 
cannot be selected individually? (I feel like I am missing something 
completely obvious..)

Thank you,
-- 
slnc

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

* Re: Optimization flags not working (GCC 4.2.4)
  2009-04-04  0:47       ` slnc
@ 2009-04-04  0:58         ` Robert William Fuller
  2009-04-04  1:01         ` Ian Lance Taylor
  1 sibling, 0 replies; 7+ messages in thread
From: Robert William Fuller @ 2009-04-04  0:58 UTC (permalink / raw)
  To: slnc; +Cc: David Daney, John (Eljay) Love-Jensen, gcc-help

slnc wrote:
> David Daney wrote:
>> slnc wrote:
>>> Hi John,
>>>
>>> Thank you but -O0 makes no difference.
>>>
>>
>> That's what he just said.  If you use -O0 (the implied default) none
>> of the optimizations are done.  Use -O1 (or -O2 or -O3) if you want
>> optimizations done.
> 
> Oh, I misunderstood him, sorry. It also doesn't work if I omit
> -O<anything>. So to sum it up, there is no way to compile a program in
> gcc enabling only one specific optimization flag. The only option is to
> use the "pre-packaged" sets of flags (-O1, -O2 or -O3) right?
> 
> I have tried disabling all -O1 flags just to check if I could select
> only one by disabling all the others but:
> 
> gcc -c -O1 -fno-defer-pop -fno-delayed-branch
> -fno-guess-branch-probability -fno-cprop-registers -fno-if-conversion
> -fno-if-conversion2 -fno-tree-ccp -fno-tree-dce -fno-tree-dominator-opts
> -fno-tree-dse -fno-tree-ter -fno-tree-lrs -fno-tree-sra
> -fno-tree-copyrename -fno-tree-fre -fno-tree-ch -fno-unit-at-a-time
> -fno-merge-constants main.c
> objdump -d dce.o
> 
> generates an optimized version. So why are the flags there if they
> cannot be selected individually? (I feel like I am missing something
> completely obvious..)
> 
> Thank you,

The conceptual model is that the flags allow you to tweak the
optimizations selected by various optimization regimes (aka optimization
levels.) First, you specify an optimization regime such as -O1, -O2, or
-O3. Then, if you do not like a specific optimization, such as
-fomit-frame-pointer, which often makes it more difficult to debug
programs, then you can remove it with -fno-omit-frame-pointer.
Conversely, if you want a more aggressive optimization, without
increasing the optimization regime to a more aggressive regime, you can
add that specific optimization. So, if I wanted an optimization from
-O2, but did not want to increase my optimization regime to -O2 from
-O1, I can specify just that optimization. However, you cannot tweak the
optimizations selected by an optimization regime if you do not tell the
compiler to optimize!

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

* Re: Optimization flags not working (GCC 4.2.4)
  2009-04-04  0:47       ` slnc
  2009-04-04  0:58         ` Robert William Fuller
@ 2009-04-04  1:01         ` Ian Lance Taylor
  1 sibling, 0 replies; 7+ messages in thread
From: Ian Lance Taylor @ 2009-04-04  1:01 UTC (permalink / raw)
  To: slnc; +Cc: David Daney, John (Eljay) Love-Jensen, gcc-help

slnc <s@slnc.me> writes:

> I have tried disabling all -O1 flags just to check if I could select
> only one by disabling all the others but:
>
> gcc -c -O1 -fno-defer-pop -fno-delayed-branch
> -fno-guess-branch-probability -fno-cprop-registers -fno-if-conversion
> -fno-if-conversion2 -fno-tree-ccp -fno-tree-dce
> -fno-tree-dominator-opts -fno-tree-dse -fno-tree-ter -fno-tree-lrs
> -fno-tree-sra -fno-tree-copyrename -fno-tree-fre -fno-tree-ch
> -fno-unit-at-a-time -fno-merge-constants main.c
> objdump -d dce.o
>
> generates an optimized version. So why are the flags there if they
> cannot be selected individually? (I feel like I am missing something
> completely obvious..)

One view is that they can be selected individually.  However, there are
many optimizations which are not controlled by any flag.  And if you
don't turn on optimization, then gcc won't build the datastructures
needed for most of the optimization passes, so they won't be run even if
you explicitly ask for them.

Another view is that optimizations are not independent.  There is basic
work that has to be done for every optimization, like build the CFG, go
into SSA form, build the dataflow information, etc.  The main difference
between -O0 and -O1 is whether or not this basic work is done.

Ian

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

end of thread, other threads:[~2009-04-04  1:01 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-03 19:31 Optimization flags not working (GCC 4.2.4) slnc
2009-04-03 21:45 ` John (Eljay) Love-Jensen
2009-04-03 23:55   ` slnc
2009-04-04  0:31     ` David Daney
2009-04-04  0:47       ` slnc
2009-04-04  0:58         ` Robert William Fuller
2009-04-04  1:01         ` Ian Lance Taylor

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