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: about unroll loops


"Ö£¬B" <taurus-zheng@163.com> writes:

>    I am a new user of gcc.
>    I have written a small code loop.c like this:
> main()
> {
> 	int a[16];
> 	int i;
> 	for(i=0;i<16;i++)
> 	   a[i]=3;
> 	return;
> }
>    I wanted to unroll the loop, so I compile it use the command 
> "gcc -unroll-all-loops -S loop.c", then I look up the loop.s,
> but the loop is not unroolled. Please tell me the reason.

1) The option you want is spelled "-funroll-all-loops".
2) You must also enable optimization, with the -O or -O2 switches.

When I do that with your test case I get

main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $72, %esp
        andl    $-16, %esp
        leave
        ret

There is no loop because, with optimization on, GCC realizes that the
array 'a' is never used after it's initialized, so it can delete the
initialization.

A slight modification of your test case:

extern int a[16];

void foo()
{
	int i;
	for(i=0;i<16;i++)
	   a[i]=3;
}

compiled with -O -funroll-all-loops, produces

foo:
        pushl   %ebp
        movl    %esp, %ebp
        movl    $3, a
        movl    $3, a+4
        movl    $3, a+8
        movl    $3, a+12
        movl    $3, a+16
        movl    $3, a+20
        movl    $3, a+24
        movl    $3, a+28
        movl    $3, a+32
        movl    $3, a+36
        movl    $3, a+40
        movl    $3, a+44
        movl    $3, a+48
        movl    $3, a+52
        movl    $3, a+56
        movl    $3, a+60
        leave
        ret

which, I believe, is more like what you are looking for.  Here GCC
cannot assume that the stores to the array are dead, because it is
visible outside the function.

zw

p.s. Your message lacks a proper character set label, so I see your
name and your sigfile as gobbledygook.  Please fix this.


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