public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* optimizer question
@ 2002-09-20 12:15 Zoltan.2.Varga
  2002-09-20 12:24 ` John Love-Jensen
  2002-09-20 12:29 ` Jeff Law
  0 siblings, 2 replies; 3+ messages in thread
From: Zoltan.2.Varga @ 2002-09-20 12:15 UTC (permalink / raw)
  To: gcc-help


				Hi,

   When confronted with the following code:

void main()
{
	int x = 2;
	int y;

	y = rand();
	y = y / x;
	printf("%d.\n", y);
}

Why does gcc generate a division instead of a shift ? Is this a shortcoming of the optimizer or some feature of C I don't 
understand ? When I replace the 'x' in the division with '2', a shift is generated. I'm using gcc-3.2 -O3 on x86 linux.

				thanks

					Zoltan

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

* Re: optimizer question
  2002-09-20 12:15 optimizer question Zoltan.2.Varga
@ 2002-09-20 12:24 ` John Love-Jensen
  2002-09-20 12:29 ` Jeff Law
  1 sibling, 0 replies; 3+ messages in thread
From: John Love-Jensen @ 2002-09-20 12:24 UTC (permalink / raw)
  To: Zoltan.2.Varga, gcc-help

Hi Zoltan,

Change it to...
    const int x = 2;
...and then the optimizer (-O / -O1, -O2, -O3, -Os) can do something
helpful.

--Eljay

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

* Re: optimizer question
  2002-09-20 12:15 optimizer question Zoltan.2.Varga
  2002-09-20 12:24 ` John Love-Jensen
@ 2002-09-20 12:29 ` Jeff Law
  1 sibling, 0 replies; 3+ messages in thread
From: Jeff Law @ 2002-09-20 12:29 UTC (permalink / raw)
  To: Zoltan.2.Varga; +Cc: gcc-help

In message <214ABAB6BF4DC24EAD9AB1C7461DA6A1045116@buebe002.europe.nokia.com>, 
Zoltan.2.Varga@nokia.com writes:
 >
 >				Hi,
 >
 >   When confronted with the following code:
 >
 >void main()
 >{
 >	int x = 2;
 >	int y;
 >
 >	y = rand();
 >	y = y / x;
 >	printf("%d.\n", y);
 >}
 >
 >Why does gcc generate a division instead of a shift ? Is this a shortcoming o
 >f the optimizer or some feature of C I don't 
 >understand ? When I replace the 'x' in the division with '2', a shift is gene
 >rated. I'm using gcc-3.2 -O3 on x86 linux.
Because conversion of division into shifts happens before constant propagation.

jeff

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

end of thread, other threads:[~2002-09-20 19:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-20 12:15 optimizer question Zoltan.2.Varga
2002-09-20 12:24 ` John Love-Jensen
2002-09-20 12:29 ` Jeff Law

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