LLVM-- Cheat Sheet

LLVM-- Cheat Sheet#

It is a good idea to have this cheat sheet with you, e.g., by printing it, when writing LLVM-- code.

Description

Example(s)

Integer types

i1, i8, i32, i64

Structure types

{i1, i64 *, i8}

Fixed-size array type

[20 x i64]

Named types

%tuple = type {i64, i64}

Global variables

@x = global i64 10

Global string variables

@s = global [5 x i8] c"Hello"

External function declarations

declare i64 @libfun (i64 %x)

Function declarations

define i64 @foo (i64 %x){%y = add i64 %x, 1 ret i64 %y}

Comparison operators

eq, ne, sle, slt, sge, sgt

Arith/logical operators

add, sub, mul, sdiv, srem, shl, lshr, ashr, and, or, xor

Arith/logical computation

%x = add i64 %y, 42

Allocating on the stack

%ptr = alloca i32

Loading from a pointer

%val = load i32, i32* %ptr

Storing to a pointer

store i32 42, i32* %ptr

Integer comparison

%cmp = icmp eq i32 %val, 10

Calling void functions

call void @myFun(i32 %arg1, i32 %arg2)

Calling non-void functions

%z = call i8 @myFun(i32 %arg1, i32 %arg2)

Casting

%ptr2 = bitcast i32* %ptr to i8*

Address of pointer

%int_ptr = ptrtoint i32* %ptr to i64

Pointer arithmetic

%element_ptr = getelementptr i32, i32* %array, i32 3

Phi node

%res = phi i32 [ %val1, %block1 ], [ %val2, %block2 ]

Conditional branching

br i1 %x, label %L1, label %L2

Unconditional branching

br label %L

Returning a value

ret i64 42

Returning void

ret void

End of block unreachable

unreachable