Common LLVM IR Instructions | ||
Type | Operation | Example(s) |
Data movement |
Stack allocation | alloca |
Momory read | load | |
Memory write | store | |
Type conversion | bitcast, ptrtoint | |
Arithmetic and logic |
Integer arithmetic | add, sub, mul, div, shl, shr |
Floating-point arithmetic |
fadd, fmul | |
Binary logic | and, or, xor, not | |
Boolean logic | icmp | |
Address calculation | getelementptr | |
Control flow |
Unconditional jump | br <location> |
Conditional jump | br <condition>, <true>, <false> | |
Subroutines | call, ret | |
Maintaining SSA form | phi |
// Your Assembly(x86_64) code here
.global _fib
.p2align 4, 0x90
_fib:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
movq %rdi, %rbx
cmpq $2, %rbx
jge LBB0_1
movq %rbx, %rax
jmp LBB0_3
LBB0_1:
leaq -1(%rbx), %rdi
callq _fib
movq %rax, %r14
addq $-2, %rbx
movq %rbx, %rdi
addq %r14, %rax
LBB0_3:
popq %rbx
poqp %r14
popq %rbp
retq
// Your Bash code here
$ clang -O3 fib.c -S -emit-llvm
// fib.c
int64_t fib(int64_t n) {
if (n < 2) return n;
return (fib(n-1) + fib(n-2));
}
// LLVM IR code fib.ll
define i64 @fib(i64) local_unnamed_addr #0 {
%2 = icmp slt i64 %0, 2
br i1 %2, label %9, label %3
; <label>:3: ; preds = %1
%4 = add nsw i64 %0, -1
%5 = tail call i64 @fib(i64 %4)
%6 = add nsw i64 %0, -2
%7 = tail call i64 @fib(i64 %6)
%8 = add nsw i64 %7, %5
ret i64 %8
; <label>:9: ; preds = %1
ret i64 %0
}
Last update: 13-Jan-2025