“do an assign”
You aren’t specifying what you mean here with regard to LLVM’s terminology. LLVM doesn’t really have “temporaries”. There are Value’s, and there are pointers to the stack or heap.
If you want to create a new “local variable”, you have to allocate stack space by creating an alloca instruction in the entry block. Then you can Store into that pointer, and Load from it.
eg;
$ cat main.c
int main(){
int i=0;
i+=1;
return 0;
}
$ clang -O0 -c main.c -emit-llvm && llvm-dis main.bc && cat main.ll
…
define i32 @main() #0 {
%i = alloca i32, align 4
store i32 0, i32* %i, align 4
%1 = load i32* %i, align 4
%2 = add nsw i32 %1, 1
store i32 %2, i32* %i, align 4
ret i32 0
}
But values don’t have to be written to the stack. All Value* objects are assumed to be stored in a register, preserved by the back-end until they are no longer needed.
$ cat fib.c
unsigned fibonacci(unsigned n){
if (n == 0)
return 0;
if (n == 1)
return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
$ clang -O3 -c fib.c -emit-llvm && llvm-dis fib.bc && cat fib.ll
…
define i32 @fibonacci(i32 %n) #0 {
switch i32 %n, label %2 [
i32 0, label %8
i32 1, label %1
]
; :1 ; preds = %0
br label %8
; :2 ; preds = %0
%3 = add i32 %n, -1
%4 = tail call i32 @fibonacci(i32 %3)
%5 = add i32 %n, -2
%6 = tail call i32 @fibonacci(i32 %5)
%7 = add i32 %6, %4
ret i32 %7
; :8 ; preds = %1, %0
%.0 = phi i32 [ 1, %1 ], [ 0, %0 ]
ret i32 %.0
}
See, not an alloca anywhere. The “temporary” value %7 is calculated from the other value’s %4 & %6, without any explicit store & load from the stack.
Each line like;
%7 = add i32 %6, %4
above, represents a single (Instruction*).
“%7” is it’s name (in this case auto-numbered because it has no explicit name).
“add i32 …” is a textual representation of the specific Instruction.
There is no separate llvm object to represent the " = " string above.
As I said before, Instruction inherits from Value. It represents both the operation to be performed, and the “temporary” result to be passed as arguments to other Instructions.
You may also find this useful to help you understand how to use the C++ API;
$ llc -march=cpp fib.bc
$ cat fib.cpp
…