Understanding the simplest LLVM IR

zell picture zell · Dec 12, 2014 · Viewed 13.7k times · Source

I transform the simplest C code

#include <stdio.h>

int main()
{
  return 0;
}

to its LLVM IR, using

clang -emit-llvm -S hello.c 

The generated IR is:

define i32 @main() #0 {
  %1 = alloca i32, align 4
  store i32 0, i32* %1
  ret i32 0
}

However, I do not understand this IR. (LLVM doc helps but not that much for beginners)

  1. Why do we have %1 = alloca i32, align 4 ? What does it correspond to in the original code?
  2. Same question for store i32 0, i32* %1
  3. Does alloca mean allocation on the stack (instead of the dynamic allocation)?
  4. What do 'align 4' mean?

Answer

Sean picture Sean · Dec 12, 2014
 define i32 @main() #0

This defines a function called main that returns a 32 bit integer. The #0 means to use the attributes named #0 for the function. For example, there may be something like attributes #0 = { alwaysinline alignstack=4 } in the IR, and these attributes will be applied to main.

%1 = alloca i32, align 4

This allocates a 32 bit integer on the stack. %1 is the name of a pointer to this location on the stack. The align 4 ensures that the address will be a multiple of 4

store i32 0, i32* %1

This sets the 32 bit integer pointed to by %1 to the 32 bit value 0. It's like saying *x = 1 in C++

ret i32 0

This returns from the function with a 32 bit return value of 0

The assignment is odd, considering that you don't have a local variable in main. LLVM uses BasicBlock to represent groups of instructions, and a basic block has an exit point and a list of instructions. My guess would be that the compiler has decided to use the return as the exit from the basic block and has opted to put in at least one instruction into the block. The assignment is basically a no-op.