getting started with llvm core libraries读书笔记IR(1st DAY)

basic 指令
To make Clang generate the bitcode, you can use the following command:

$ clang sum.c -emit-llvm -c -o sum.bc

To generate the assembly representation, you can use the following command:

$ clang sum.c -emit-llvm -S -c -o sum.ll

You can also assemble the LLVM IR assembly text, which will create a bitcode:

$ llvm-as sum.ll -o sum.bc

To convert from bitcode to IR assembly, which is the opposite, you can use the
disassembler:

$ llvm-dis sum.bc -o sum.ll

The llvm-extract tool allows the extraction of IR functions, globals, and also the
deletion of globals from the IR module. For instance, extract the sum function from
sum.bc with the following command:

$ llvm-extract -func=sum sum.bc -o sum-fn.bc

Introducing the LLVM IR language syntax
module
1、The module is the LLVM IR top-level data structure.
2、Each module contains a sequence of functions, which contains a sequence of basic blocks that contain a sequence of instructions.
3、The module also contains peripheral entities to support this model, such as global variables, the target data layout, and external function prototypes as well as data structure declarations.

The Module class aggregates all of the data used in the entire translation
unit, which is a synonym for “module” in LLVM terminology. It declares the
Module::iterator typedef as an easy way to iterate across the functions
inside this module.

The Function class contains all objects related to a function definition or
declaration. In the case of a declaration (use the isDeclaration() method
to check whether it is a declaration), it contains only the function prototype.
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.
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, you will iterate
across its basic blocks.

The BasicBlock class encapsulates a sequence of LLVM instructions,
accessible via the begin()/end() idiom. You can directly access its last
instruction using the getTerminator() method, and 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.

The Instruction class represents an atom of computation in the LLVM IR, a
single instruction. It has some methods to access high-level predicates, such as
isAssociative() , isCommutative() , isIdempotent() , or isTerminator() ,
but its exact functionality can be retrieved with getOpcode() , which returns a
member of the llvm::Instruction enumeration, which represents the LLVM
IR opcodes. 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.

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. Function and Instruction are subclasses of both Value and User , while BasicBlock is a subclass of just 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.
For every Value class, you can also access its name through the getName()
method.This models the fact that any LLVM value can have a distinct identifier associated with it. For example, %add1 can identify the result of an add instruction, BB1 can identify a basic block, and myfunc can identify a function. Value also has a powerful method called replaceAllUsesWith(Value *) , which navigates through all of the users of this value and replaces it with some other value.

The User class has the op_begin() and op_end() methods that allows
you to quickly access all of the Value interfaces that it uses. Note that this
represents the use-def chain. You can also use a helper method called
replaceUsesOfWith(Value *From, Value *To) to replace any of its used
values.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值