【LLVM】《Getting Started with LLVM Core Libararies》读书笔记——IR和几个重要的类

来源:互联网 发布:淘宝 手机 退货地址 编辑:程序博客网 时间:2024/06/05 06:15

【IR中涉及的一些名词解释】

“The target datalayout construct contains information about endianness and type sizes for target triple that is described in target host.”

目标数据架构包括:(1)关于字节序的信息;(2)目标主机中描述的三地址码的类型大小

“The information provided about types is in the format type:<size>:<abi>:<preferred>. In the preceding example, p:64:64:64 represents a pointer that is 64 bits wide in size, with the abi and preferred alignments set to the 64-bit boundary.”

类型形式:type:<size><abi>:<preferred>

Eg:p:64:64:64

type:pointer  <size>:64位 <abi>:64-bit boundary <preferred>:64-bit boundary 对准

“The #0 tag in the function declaration maps to a set of function attributes, also very similar to the ones used in C/C++ functions and methods. The set of attributes is defined at the end of the file:”

函数声明里的“#0”标签映射为函数属性的集合,和C/C++里函数、方法的用法很类似,这个属性集合在文件末尾被定义。



例如,“nounwind”表示程序未抛出异常

“A label relates to a basic block in the same way that a value identifier relates to an instruction”

一个和bb联系的“label”就像一个和指令联系的数据定义。


【在LLVM基础上理解BB】

“A basic block is a sequence of instructions with a single entry point at its first instruction, and a single exit point at its last instruction. In this way, when the code jumps to the label that corresponds to a basic block, we know that it will execute all of the instructions in this basic block until the last instruction, which will change the control flow by jumping to another basic block. Basic blocks and their associated labels need to adhere to the following conditions:
• Each BB needs to end with a terminator instruction, one that jumps to other BBs or returns from the function
• The first BB, called the entry BB, is special in an LLVM function and must not be the target of any branch instructions”

一个BB是由一系列的指令组成的,由第一条指令的entry指针开始,到最后一条指令的exit指针结束。所以一个BB内的指令会从头执行到尾,指导跳到另一个改变控制流的BB。

“Clang uses -O0 (no optimizations) by default, and the unnecessary loads and stores are not removed. If we compile with -O1 instead, the outcome is a much simpler
code.”

-O0指令是缺省的,会得到不必要的loads和stores指令,如果用-O1代替,就会输出更简洁的IR。


【重要的类】

1.Module类

“It declares the Module::iterator typedef as an easy way to iterate across the functions inside this module.”

Module类声明了一个迭代器,可以遍历module中的function

2.Function类

Function对象可能是函数声明,可能是函数定义,获取这两个不同对象的内容可调用不同的函数。

(1)获取函数声明中的参数

“isDeclaration() method to check whether it is a declaration”

使用isDeclaration()方法check对象是否是函数声明,包含函数原型。

“In both cases, it contains a list of the function parameters accessible via the getArgumentList() method or the pair of arg_begin() and arg_end().You can iterate through them using the Function::arg_iterator typedef.”

通过getArgumentList()方法或是用arg_iterator迭代器遍历可获得函数参数。

(2)获取函数中的内容

“If your Function object represents a function definition, and you iterate through its contents via the for (Function::iterator i = function. begin(), e = function.end(); i != e; ++i) idiom,”

如果Function对象代表了一个function定义,可以用以下代码遍历函数中的内容:

3.Basic Block类

“You can directly access its last instruction using the getTerminator()”

BB中包含了一系列指令,可以通过getTerminator()方法获取最后一条指令。

“you also have a few helper methods to navigate the CFG, such as accessing predecessor basic blocks via getSinglePredecessor(), when the basic block has a single predecessor. However, if it does not have a single predecessor, you need to work out the list of predecessors yourself, which is also not difficult if you iterate through basic blocks and check the target of their terminator instructions.”

可以通过CFG,例如通过getSinglePredecessor()方法,访问前一个BB。

4.Instruction类

“but its exact functionality can be retrieved with getOpcode(), which returns a member of the llvm::Instruction enumeration, which represents the LLVM IR opcodes.”

但是它的确切功能可以通过getOpcode()来获取,它返回代表LLVM IR操作码的llvm :: Instruction枚举的成员。

“You can access its operands via the op_begin() and op_end() pair of methods, which are inherited from the User superclass that we will present shortly.”

也可以通过op_begin()和op_end()来获取操作数,这两个方法继承自User类。


【很重要的两个类——Value类和User类】

“In the LLVM in-memory IR, a class that inherits from Value means that it defines a result that can be used by others, whereas a subclass of User means that this entity uses one or more Value interfaces.”

一个继承了Value类的类意味着类内定义了可能被其他类使用的结果,一个继承了User类的类意味着这个实体可能使用了一个或多个Value接口。

1.Value类

“The Value class defines the use_begin() and use_end() methods to allow you to iterate through Users, offering an easy way to access its def-use chain.”

Value类定义了use_begin()和use_end()方法,用于遍历Users,访问def-use链,即访问所有使用了value的user。

















原创粉丝点击