Llvm talk twice a day llvm IR

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.