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