Yuri, Duncan,
Thank you for the replies.
I tried to do a GEP() on %0. The execution asserted, saying some type mismatching.
I agree with Duncan, Compiler generated temporaries (%0) are neither local or global variables, they are actually virtual registers who don’t have address, no types, while the size is determined by the operands of the instruction (E.g., add i32 has a result size of i32, while add i16 has a result size of i16).
// Original C code:
void foo(int x, int y){
volatile int z;
z = x + y;
printf("val : %d\n", z);
}
and its generated LLVM IR:
// LLVM IR Code generated from the C code:
define void @foo(i32 %x, i32 %y) nounwind noinline {
entry:
%z = alloca i32, align 4
%0 = add nsw i32 %y, %x
volatile store i32 %0, i32* %z, align 4
%1 = volatile load i32* %z, align 4
%2 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([10 x i8]* @.str, i32 0, i32 0), i32 %1) nounwind
ret void
}
By piping it to the C Backend, I get
// C Code generated via the C Backend:
void foo(unsigned int llvm_cbe_x, unsigned int llvm_cbe_y) {
unsigned int llvm_cbe_z; /* Address-exposed local */
unsigned int llvm_cbe_tmp__1;
unsigned int llvm_cbe_tmp__2;
*((unsigned int volatile*)(&llvm_cbe_z)) = (((unsigned int )(((unsigned int )llvm_cbe_y) + ((unsigned int )llvm_cbe_x))));
llvm_cbe_tmp__1 = *((unsigned int volatile*)(&llvm_cbe_z));
llvm_cbe_tmp__2 = printf(((&_OC_str.array[((signed int )0u)])), llvm_cbe_tmp__1);
return;
}
Motivated by the C Backend, I think what I can do is to generate a local (stack) variable and store the %0 into it.
E.g.:
%0 = add nsw i32 %y, %x
%tmp_1 = alloca i32, align 4
store i32 %0, *i32 %tmp_1
…
Now, %tmp_1 is a local variable that holds the value of %0, has a known good size and can have its address taken.
Does this make sense?
Thank you
Chuck