This is the mail archive of the gcc-patches@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: [doc] empty loops


On Thu, 25 Nov 2004 16:58:44 +0100, Zdenek Dvorak
<rakdver@atrey.karlin.mff.cuni.cz> wrote:
> Hello,
> 
> 
> 
> > Also, playing with empty loops, I was shocked after looking at the code
> > created for the recursive variant of
> >
> > void foo(void)
> > {
> >         for (int i=0; i<1000; ++i)
> >                 ;
> > }
> >
> > namely:
> >
> > static inline void foo3(int i)
> > {
> >         if (!(i<1000))
> >                 return;
> >         ++i;
> >         foo3(i);
> > }
> > void foo4(void)
> > {
> >         foo3(0);
> > }
> >
> > I thought Zednek contributed a patch for optimizing these
> > tail-recursive functions
> > some time ago.
> 
> could you please explain what exactlu shocks you here?  Tail recursion
> elimination on this example works perfectly fine for me.
> 
> > Even for a standard n! implementation we suck.
> 
> Not that much; iv optimizations could do a bit better job in
> this case, but the code that we produce basically is
> 
> int fact(int n)
> {
>   i = 0;
>   m = 1;
> 
>   while (n - i != 0)
>     {
>       m *= n - i;
>       i++;
>     }
> }
> 
> which is not that bad.

Ah, I had -funroll-loops enabled, which confused my reading of the
assembly output.
I guess in the unrolled case (where we "skip" iterations in the for()
loop case) we
somehow loose track of the fact that the loop is empty and the iteration counter
unused.  I guess exchanging loop and tail-recursion optimization could
improve the testcase.

Richard.


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