The %n
are virtual registers that will be resolved to actual registers when generating code for the target machine.
The i32
is there for type information. In the original code it was an int
which your compiler took to be 32-bit integer.
alloca
is for allocating space on the stack. In this example it is i32
(32-bit integer) so you can load in the 0 for the return value. align 4
gives this allocation 4 byte alignment i.e. the stack pointer will be on a 4 byte aligned address.
It isn't the most efficient representation but that is not the aim if IR. IR should be portable to different architectures. It is then down to the backend to produce efficient machine code.
LLVM Language Reference Manual
Why alloca
and store
are there is to do with this being the main
function. If you had called this function something else, the IR would just contain ret
as you expected. From examining the assembly produced for main it appears to be related to the stack base pointer
but I don't fully understand why it is there. Time to pull out the C standard I think.
Update: I couln't find anything in the C standard but it seems clang does this for every main function. I don't know the clang code base well enough to track it down though.
Update: See comments with Bill Lynch below. These instuctions are there:
for the possible implicit return 0
that main functions have