Author: snsn1984
Before introducing llvm IR, we need to first understand the structure of llvm. Traditional static compilers are divided into three stages: front-end, optimization, and backend.
The three-phase design of llvm is as follows: to support a new programming language, we only need to implement a new front-end. If we need to support a new hardware device, we only need to implement a new backend. The Optimization phase is aimed at the unified llvm IR, so it is a general stage, whether it supports new programming languages or new hardware devices, you do not need to modify the optimization phase here. So here we can see the role of llvm IR. Llvm IR has three main formats: one is the compilation language in the memory, and the other is the binary intermediate language stored on the hard disk (. at the end of BC), the last is the readable intermediate format (. LL ). The three intermediate formats are completely equal. Llvm IR is the key to optimizing llvm and generating code. Based on the readable IR, we can know what code we have generated before the final generation of the target code. Based on IR, we can choose to use different backend to generate different executable code. At the same time, because of the use of uniform IR, We can reuse llvm optimization functions, even if we are using our own programming language.
If you want to intuitively see what the llvm IR looks like, you can first write a helloworld program named hello. C.
Follow these steps to configure llvm:
Http://clang.llvm.org/get_started.html
Then, use the following command to obtain the binary. BC file:
Clang-emit-llvm-C hello. C-O hello. BC
Run the following command to obtain the corresponding hello. ll file, which is readable and can be opened in a text editor.
Llvm-Dis hello. BC
In this way, we can intuitively get the two formats of IR. As for the format in the memory, we cannot get it through the file format.
We generally view the. ll format, because it is the IR format for people to read.
For llvm IR introduction, llvm has a specific document, the address of the document is: http://llvm.org/docs/LangRef.html