[vox-tech] another gcc question

Gabriel Rosa vox-tech@lists.lugod.org
Wed, 27 Feb 2002 09:42:40 -0800 (PST)


On Wed, 27 Feb 2002, Gabriel Rosa wrote:

> On Wed, 27 Feb 2002, Peter Jay Salzman wrote:
>
> > another optimization question:
> >
> >    int n = 5;
> >    for (i=0; i<n; ++i)
> >
> > can gcc unroll this loop the way it can (for instance)
> >
> >    #define N 5
> >    for (i=0; i<N; ++i)
>
> Refer to previous answer.
> if you use -funroll-all-loops, yes.
>
> > if it can't, what about
> >
> >    const int n = 5;
> > 	for (i=0; i<n; ++i)
> >
>
> const int still gets allocated at run/load time, so I'd imagine
> -funroll-loops would not unroll this.
>
> -Gabe
>
>

Ok, maybe I answered in haste.

This is what GCC (Gabe C Compiler) would do.
However, the GNU C Compiler _should_ be smart enough to figure
out that it can replace the const int loop with an unrolled version.

However, i would think it wouldn't, because that would be dangerous
bahaviour, afterall there are ways around const.

Come to think of it, even in the first example it might choose to unroll
if it sees that the value of N couldn't possibly change before the loop.
Again, risky if you're running multiple threads.

-Gabe