[cfe-dev] Startling optimization

Robert Purves listrp at gmail.com
Sun Jun 19 21:56:20 PDT 2011


$ cat test.c
int foo( int n ) 
{ 
  int  a = 0, b = 0, c = 0, j; 

  for ( j = 0; j < n; j++ ) 
  { 
    a++; 
    b = a | c; 
    c = a; 
  } 
  return b; 
}  

$ clang -O -S test.c


_foo:
	pushq	%rbp
	movq	%rsp, %rbp
	testl	%edi, %edi
	jle	LBB0_2
	leal	-1(%rdi), %eax
	orl	%edi, %eax
	popq	%rbp
	ret
LBB0_2:
	xorl	%eax, %eax
	popq	%rbp
	ret

Which parts of clang/llvm source should I read to learn how the loop is magically optimized away?

Robert P.





More information about the cfe-dev mailing list