[LLVMdev] global question

John Regehr regehr at cs.utah.edu
Sat Mar 7 08:56:33 PST 2009


Below is some C code and its LLVM translation (using a recent rev).  The 
compiler goes ahead and loads from x in main, but it seems clear that a 
constant propagation pass could have inferred that x is 0.  In fact that 
is what happens if x is static, but in this case I cannot see how 
external linkage of x would invalidate that optimization.

This is one of those optimizations that would not be an issue for SPEC 
or whatever, but probably makes a difference for smallish embedded codes 
that tend to have a lot of code in main().

Thanks,

John Regehr


int x;

int main (void)
{
   return x;
}

regehr at john-home:~$ llvm-gcc -O2 -S --emit-llvm test.c -o -

; ModuleID = 'test.c'
target datalayout = 
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"
target triple = "i386-pc-linux-gnu"
@x = common global i32 0		; <i32*> [#uses=1]

define i32 @main() nounwind readonly {
entry:
	%0 = load i32* @x, align 4		; <i32> [#uses=1]
	ret i32 %0
}



More information about the llvm-dev mailing list