Until now, we have understood what LLVM IR (human readable form) is and how it can be used to represent a high-level language. Now, we will take a look at some of the tools that LLVM provides so that we can play around with this IR converting to other formats and back again to the original form. Let's take a look at these tools one by one along with examples.
add.ll
as an example to convert it into bitcode. To know more about the LLVM Bitcode file format refer to http://llvm.org/docs/BitCodeFormat.html$ llvm-as add.ll –o add.bc
To view the content of this bitcode file, a tool such as hexdump
can be used.
$ hexdump –c add.bc
$ llvm-dis add.bc –o add.ll
If you check add.ll
and compare it with the previous version, it will be the same as the previous one.
main.c
file that calls the function in the add.c
file.$ cat main.c #include<stdio.h> extern int add(int); int main() { int a = add(2); printf("%d ",a); return 0; }
Convert the C source code to LLVM bitcode format using the following command.
$ clang -emit-llvm -c main.c
Now link main.bc
and add.bc
to generate output.bc
.
$ llvm-link main.bc add.bc -o output.bc
$ lli output.bc 14
output.bc
file generated by llvm-link and generates the assembly file output.s
.$ llc output.bc –o output.s
Let's look at the content of the output.s
assembly, specifically the two functions of the generated code, which is very similar to what a native assembler would have generated.
Function main: .type main,@function main: # @main .cfi_startproc # BB#0: pushq %rbp .Ltmp0: .cfi_def_cfa_offset 16 .Ltmp1: .cfi_offset %rbp, -16 movq %rsp, %rbp .Ltmp2: .cfi_def_cfa_register %rbp subq $16, %rsp movl $0, -4(%rbp) movl $2, %edi callq add movl %eax, %ecx movl %ecx, -8(%rbp) movl $.L.str, %edi xorl %eax, %eax movl %ecx, %esi callq printf xorl %eax, %eax addq $16, %rsp popq %rbp retq .Lfunc_end0: Function: add add: # @add .cfi_startproc # BB#0: pushq %rbp .Ltmp3: .cfi_def_cfa_offset 16 .Ltmp4: .cfi_offset %rbp, -16 movq %rsp, %rbp .Ltmp5: .cfi_def_cfa_register %rbp movl %edi, -4(%rbp) addl globvar(%rip), %edi movl %edi, %eax popq %rbp retq .Lfunc_end1:
opt [options] [input file name]
When the –analyze
option is provided it performs various analysis on the input. There is a set of analysis options already provided that can be specified through command line or else one can write down their own analysis pass and provide the library to that analysis pass. Some of the useful analysis passes that can be specified using the following command line arguments are:
When the –analyze
option is not passed, the opt tool does the actual optimization work and tries to optimize the code depending upon the command-line options passed. Similarly to the preceding case, you can use some of the optimization passes already present or write your own pass for optimization. Some of the useful optimization passes that can be specified using the following command-line arguments are:
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
52.15.47.218