Exercising basic tools to manipulate the IR formats
We mention that the LLVM IR can be stored on disk in two formats: bitcode and assembly text. We will now learn how to use them. Consider the sum.c
source code:
int sum(int a, int b) { return a+b; }
To make Clang generate the bitcode, you can use the following command:
$ clang sum.c -emit-llvm -c -o sum.bc
To generate the assembly representation, you can use the following command:
$ clang sum.c -emit-llvm -S -c -o sum.ll
You can also assemble the LLVM IR assembly text, which will create a bitcode:
$ llvm-as sum.ll -o sum.bc
To convert from bitcode to IR assembly, which is the opposite, you can use the disassembler:
$ llvm-dis sum.bc -o sum.ll
The llvm-extract
tool allows the extraction of IR functions, globals, and also the deletion of globals from the IR module. For instance, extract the sum
function from sum.bc
with the following command:
$ llvm-extract -func=sum sum.bc -o sum-fn.bc
Nothing changes between sum.bc
and sum-fn.bc
in this...