This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Loop oddity


> > Here is a loop oddity that I noticed.  The loop 
> 
> >   for (i = 1000000; i; i--) {};
> 
> > I noticed this trying to write a little program to check insn
> > timing.  Thus, I didn't want the loop transformed.
> 
> You cannot rely upon gcc not optimizing it away completely.  Now is
> the time to fix your code.  We suggest accessing a volatile object
> inside the loop.

Well adding a volatile object changes the code, and in particular
introduces unwanted memory accesses with unspecified and variable
timing.  The issue seems to be that the loop pass is doing loop
unrolling when it does strength reduction.  If I turn off strength
reduction, then I get the expected result (a loop that does 1000000
iterations).  I think that it's valid to replace the loop with
"i = 0;" when strength reduction is enabled, but I don't think
changing the number of loop iterations is a valid transformation
of what was written even if the final result is the same.  It's
interesting that this transformation doesn't occur if "i" is global.

Dave
-- 
J. David Anglin                                  dave.anglin@nrc.ca
National Research Council of Canada              (613) 990-0752 (FAX: 952-6605)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]