From: Richard Dawe Newsgroups: comp.os.msdos.djgpp Subject: Re: -O3 doesnt remove dead code Date: Sat, 14 Sep 2002 19:53:00 +0100 Lines: 55 Message-ID: <3D83858C.BED42EB5@phekda.freeserve.co.uk> References: NNTP-Posting-Host: modem-209.iodine.dialup.pol.co.uk Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news5.svr.pol.co.uk 1032034935 8772 62.136.44.209 (14 Sep 2002 20:22:15 GMT) NNTP-Posting-Date: 14 Sep 2002 20:22:15 GMT X-Complaints-To: abuse AT theplanet DOT net X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.2.19 i586) X-Accept-Language: de,fr To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Hello. Rafal 'Raf256' Maj wrote: > > Hans-Bernhard Broeker wrote in > news:alvrup$25r$1 AT nets3 DOT rz DOT RWTH-Aachen DOT DE > > >> int main() { for (long long int i=0; i<500000000; i++) ; return 0; } > >> why this dead-loop is not removed ? > > > Because the GCC people explicitly decided to not do that, IIRC. The > > distinction being that a loop explicitly coded with an empty body is > > probably a timing-critical delay loop, and as such should be left > > alone. If you put something into the loop, and try again, you just > > might see what you expected. > > int a; > for (long long int i=0; i<500000000; i++) a=0; > > gives same result :-/ What does that prove? How do you know that gcc doesn't move the assignment outside the loop, giving you almost the same code as before: int a; for (long long int i=0; i<500000000; i++) ; a=0; > IMHO time-critical loops should be marked as : > > for (volatile long long int i=0; i<500000000; i++) ; > ^^^^^^^^ > this will never be optimized. And all dead codes should be deleted (this > loop is not due to volatile counter) I thought that 'volatile' meant that something external to the program could modify the value (e.g.: some interrupt handler). Note that 'volatile' may have no effect, depending on the C implementation. See point 8 on page 28 of the C99 standard. Also, why should the compiler drop this loop? You've asked it to loop, so why shouldn't it generate code to do that? You can't expect the compiler to do all the optimisation for you! > Meybe I should report somwhere that 'bug' ? I use gcc 2.95 I think you should ask gcc AT gnu DOT org if there is a warning to detect empty bodies in for loops, where the only action is to increase the loop counter. But even then there may not be one. It's probably possible to construct a case where the warning would be bogus. Regards, -- Richard Dawe [ http://www.phekda.freeserve.co.uk/richdawe/ ]