no Loop Invariant Motion?

prashant rawat prashant.rawat86@gmail.com
Sun Jan 3 19:50:00 GMT 2010


Thanks a lot :)
However, what if I make variable a global?
Now it will be there in the assembly code.
#include<stdio.h>
int b, a=0;
int main ()
{
       while (a < 100)
       {
                       b = 10;
                       a = a + 1;
                       printf ("%d\n", a);
       }
       printf ("%d, %d\n", b, a);
       return 0;
}

If there is some LIM performed, then why is it not reflected in the .lim file?
Is it that such inexpensive loop invariant motions are not performed at all?
Are there any testcases on which LIM works?
Thanks in advance.


On Mon, Jan 4, 2010 at 12:34 AM, Bob Plantz <plantz@cds1.net> wrote:
> Forgot to say that I added my own comments to the assembly language
> code.
>
> On Sun, 2010-01-03 at 10:40 -0800, Bob Plantz wrote:
>> On Sun, 2010-01-03 at 11:24 +0530, prashant rawat wrote:
>> > I have a simple C code :
>> >
>> > #include<stdio.h>
>> > int *c;
>> > int main ()
>> > {
>> >         int a=0, b;
>> >         while (a < 100)
>> >         {
>> >                         b = 10;
>> >                         a = a + 1;
>> >         }
>> >         c = &b;
>> >         printf ("%d\n", *c);
>> >         return 0;
>> > }
>> >
>> > I would basically expect the b = 10 statement to be moved out of the
>> > while loop, as it is an invariant.
>> > However, it does not seem to happen in the *.lim dump file.
>> > I am using gcc 4.4.2 to compile, and the cmd line options are
>> > install/bin/gcc -fdump-tree-all -O3 filename.c
>> >
>> > Can anyone explain what went wrong?
>> >
>>
>> My version of gcc (4.4.1) completely eliminates the loop since it it
>> irrelevant. Notice that a is not used beyond the loop. gcc generates the
>> following assembly language:
>>
>>       .file   "loop_invar.c"
>>       .section        .rodata.str1.1,"aMS",@progbits,1
>> .LC0:
>>       .string "%d\n"
>>       .text
>>       .p2align 4,,15
>> .globl main
>>       .type   main, @function
>> main:
>> .LFB22:
>>       .cfi_startproc
>>       subq    $24, %rsp
>>       .cfi_def_cfa_offset 32
>>       movl    $10, %edx      # 3rd argument to printf
>>       movl    $.LC0, %esi    # address of format string
>>       leaq    12(%rsp), %rax # address of b
>>       movl    $1, %edi       # check stack overflow
>>       movl    $10, 12(%rsp)  # b = 10;
>>       movq    %rax, c(%rip)  # c = &b;
>>       xorl    %eax, %eax     # no floats
>>       call    __printf_chk
>>       xorl    %eax, %eax     # return 0;
>>       addq    $24, %rsp
>>       ret
>>       .cfi_endproc
>> .LFE22:
>>       .size   main, .-main
>>       .comm   c,8,8
>>       .ident  "GCC: (Ubuntu 4.4.1-4ubuntu8) 4.4.1"
>>       .section        .note.GNU-stack,"",@progbits
>>
>> which is the equivalent of:
>> #include<stdio.h>
>> int *c;
>> int main ()
>> {
>>         int b;
>>         b = 10;
>>         c = &b;
>>         printf ("%d\n", 10);
>>         return 0;
>> }
>>
>> --Bob
>>
>>
>>
>
>
>



-- 
With Regards,
Prashant SIngh Rawat
MTech, CSE, IIT Bombay



More information about the Gcc-help mailing list