obtain the address and size of LLVM generated temporaries

I wonder what is the right approach to obtain the address and size of LLVM (compiler) generated temporaries?

E.g.

%0 = %x + %y
store i32 i0, i32 %z, align 4

How can I get the address of %0 (which could be either a stack or heap variable)?

Thank you

Chuck

This is not possible since such value can also be in register.

Yuri

Hi Chuck,

I wonder what is the right approach to obtain the address and size of
LLVM (compiler) generated temporaries?

E.g.

%0 = %x + %y
store i32 i0, i32 %z, align 4

How can I get the address of %0 (which could be either a stack or heap
variable)?

actually %0 is neither a stack or a heap variable, it is an LLVM "virtual
register" and has no address. It has type i32, so storing it writes the
same number of bytes as any i32 store, see getTypeStoreSize.

Ciao, Duncan.

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