参考书:《Getting Started with LLVM Core Libraries》
一、概览图:
整体概述:IR在翻译成目标代码时,图中白色方框Passes表示进一步优化,灰色方框表示翻译过程中的各个阶段。
image.png
- Instruction Selection :(指令选择)该阶段将IR的三地址结构转换为有向无环图(DAG :Directed Acyclic Graph),每一个DAG表示一个 basic block 的计算,所以每个 basic block 关联着不同的DAG。
- Instruction Scheduling : (指令调度)在Instruction Selection 我们能确定使用哪些指令去计算 basic block ,这被编码在类SelectionDAG中。但是DAG并不确定指令执行顺序,这里第一个 Instruction Scheduling 实例(也叫做 Pre-register Allocation(RA) Scheduling ) 对指令排序。
- Register Allocation : (寄存器分配)LLVM IR的寄存器是无限的,这一步翻译成有限的特定目标的寄存器。
- Instruction Scheduling :第二个指令调度实例, 也叫做 Post-register Allocation(RA) Scheduling
- Code Emission : 该阶段将 MachineInstr 转换成 MCInst 实例,更适合汇编器和链接器。
因此,指令表示有四个较清晰的层次:LLVM IR ,SelectionDAG nodes,MachineInstr, and MCInst.
二、使用LLC
llc是我们后端使用的主要工具
// 生成汇编
$ llc sum.bc -o sum.s
// 生成obj文件
$ llc sum.bc -filetype=obj -o sum.o
// 指定后端架构
$ llc -march=mips -filetype=obj sum.bc -o sum.o
三、了解后端代码结构
后端实现分散在LLVM源码树的不同的目录,主要的库在lib目录和子目录(CodeGen, MC, TableGen, and Target)
- CodeGen:常规代码生成算法的实现:instruction selection, scheduler, register allocation, and all analyses needed for them
- MC : 底层功能的实现,比如:the assembler (assembly parser), relaxation algorithm (disassembler), and specific object file idioms such as ELF, COFF, MachO, and so on.
- TableGen : 包含TableGen 工具的完整实现,它用来生成C++代码,基于顶层的tb目标文件描述。
-
如果要写一个新的后端,只需要在 Target 目录下新建一个子目录。
下边我们看一下Target/Sparc目录的组织结构:
image.png
四、了解后端库
llc只有一小部分非共享代码tools/llc/llc.cpp,它的大多数功能实现都是可重用的库。
- 下面是目标无关的代码生成器的库,忽略掉了libLLVM前缀:
• AsmParser.a: This library contains code to parse assembly text and implement an assembler
• AsmPrinter.a: This library contains code to print assembly language and implement a backend that generates assembly files
• CodeGen.a: This library contains the code generation algorithms
• MC.a: This library contains the MCInst class and related ones and is used to
represent the program in the lowest level that LLVM allows
• MCDisassembler.a: This library contains the code to implement a
disassembler that reads object code and decodes bytes to MCInst objects
• MCJIT.a: This library contains the implementation for the just-in-time code
generator
• MCParser.a: This library contains the interface to the MCAsmParser class and is used to implement a component that parses assembly text and performs part of the work of an assembler
• SelectionDAG.a: This library contains SelectionDAG and related classes
• Target.a: This library contains the interfaces that allow the target-independent algorithms to solicit target-dependent functionality, although this functionality per se is implemented in other libraries
(the target-dependent ones)
- 下面是特定目标的库:
• <Target>AsmParser.a: This library contains the target-specific part of the AsmParser library, responsible for implementing an assembler for the target machine
• <Target>AsmPrinter.a: This library contains the functionality to print target instructions and allow the backend to generate assembly language files
• <Target>CodeGen.a: This library contains the majority of the target-dependent functionality of the backend, including specific register handling rules, instruction selection, and scheduling
• <Target>Desc.a: This library contains target-machine information regarding the low-level MC infrastructure and is responsible for registering target-specific MC objects such as MCCodeEmitter
• <Target>Disassembler.a: This library complements the MCDisassembler library with target-dependent functionality to build a system that is able to read bytes and decode them into MCInst target instructions
• <Target>Info.a: This library is responsible for registering the
target in the LLVM code generator system and provides façade classes that allow the target-independent code generator libraries to access target-specific functionality
网友评论