1. LLVM各工具使用

  • Convert C to IR
1
clang -emit-llvm -S multiply.c -o multiply.ll

Or

1
clang -cc1 -emit-llvm testfile.c -o testfile.ll

Or

1
clang test.c -S -emit-llvm -o test.ll
  • Convert IR to bc
1
llvm-as test.ll –o test.bc
  • Converting LLVM bitcode to target machine assembly
1
llc test.bc –o test.s

Or

1
clang -S test.bc -o test.s
  • Converting bc to IR
1
llvm-dis test.bc –o test.ll
  • Transforming LLVM IR
1
opt –passname input.ll –o output.ll
  • Linking llvm bc
1
llvm-link test1.bc test2.bc –o output.bc
  • Executing llvm bc
1
lli output.bc

2. IRBuilder

可以批量的插入指令

  • 在之前前面插入指令
1
2
3
4
5
Instruction *pi = ...;
IRBuilder<> Builder(pi);
CallInst* callOne = Builder.CreateCall(...);
CallInst* callTwo = Builder.CreateCall(...);
Value* result = Builder.CreateMul(callOne, callTwo);
  • 在基本块后面插入指令
1
2
3
4
5
BasicBlock *pb = ...;
IRBuilder<> Builder(pb);
CallInst* callOne = Builder.CreateCall(...);
CallInst* callTwo = Builder.CreateCall(...);
Value* result = Builder.CreateMul(callOne, callTwo);

但IRBuilder方法对于常数的指令会被优化掉

3. 遍历Funciton或BasicBlock

除了使用

1
for(auto F=M.begin(),E=M.end();F!=E;F++)

还可以使用

1
for(Module::iterator F = M.begin(), E = M.end();F!=E;++F)

注意,这块要使用比要得到的高一级的迭代器,即如果得到Function,则使用Module::iterator;

如果得到BasicBlock,则使用Function::iterator

如果遍历Instruction,可使用BasicBlock::iterator,或

如果遍历Instruction的参数,可使用Instrunction::op_iterator

  • Function 中有一个函数size(),可以得到该函数中基本块的数量

4. LLvm编译DEBUG版本的命令

1
2
3
4
5
6
7
8
9
export CC=gcc

export CXX=g++

./configure --prefix=/home/haomeng/.local --sysconfdir=/etc --enable-shared --enable-libffi --enable-targets=all --disable-expensive-checks --disable-assertions --with-binutils-include=/usr/include --with-python=/usr/bin/python2 --disable-optimized --enable-debug-runtime

make REQUIRES_RTTI=1 -j 60

make install

5. Pass

在LLVM框架中,Pass用于对LLVM IR进行优化,对IR进行处理与分析,生成优化后的代码。opt命令可以用来运行pass对IR进行处理。

优化类型

  • opt 自带的优化

步骤如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
clang -S -O0 -emit-llvm example.c

opt -O0 -S example.ll

opt -O1 -S example.ll

opt -O2 -S example.ll

opt -O3 -S example.ll

为了能够看到opt所调用的优化Pass,可以加入参数

--debug-pass=Structure

参考:http://llvm.org/docs/CommandGuide/opt.html

  • 自己写Pass进行优化

参考:http://llvm.org/docs/WritingAnLLVMPass.html

  • 在一个Pass里面调用别的Pass

getAnalysis函数

  • 通过Pass manager 对Pass进行管理
  • Analysis Pass 分析IR但不对IR进行修改,其结果可以在多个Pass中使用,直到IR改变

没有更改IR内容就返回false

6. LLVM的Use-Def获取

Alternatively, it’s common to have an instance of the User Class and need to know what Values are used by it. The list of all Values used by a User is known as a use-def chain. Instances of class Instruction are common User s, so we might want to iterate over all of the values that a particular instruction uses (that is, the operands of the particular Instruction):

1
2
3
4
5
6
Instruction *pi = ...;

for (Use &U : pi->operands()) {
Value *v = U.get();
// ...
}

7. LLVM中Def-Use获取

Frequently, we might have an instance of the Value Class and we want to determine which Users use the Value. The list of all Users of a particular Value is called a def-use chain.

  • 函数的def-use
1
2
3
4
5
6
7
Function *F = ...;

for (User *U : F->users()) {
if (Instruction *Inst = dyn_cast<Instruction>(U)) {
errs() << "F is used in instruction:\n";
errs() << *Inst << "\n";
}

或者

1
2
3
4
5
6
Function *F = ...;
for (Value::use_iterator U = F->use_begin(), e = F->use_end(); U != e; ++U) {
if (Instruction *Inst = dyn_cast<Instruction>(&*(U->getUser()))) {
errs() << "F is used in instruction:\n";
errs() << *Inst << "\n";
}
  • 指令的Def-Use
1
2
3
4
5
6
7
Instruction *A = ...;

for (User *U : A->users()) {
if (Instruction *Inst = dyn_cast<Instruction>(U)) {
errs() << "A is used in instruction:\n";
errs() << *Inst << "\n";
}

1
2
3
4
5
6
Instruction *A = ...;
for (Value::use_iterator i = A->use_begin(), e = A->use_end(); i != e; ++i) {
if (Instruction *U = dyn_cast<Instruction>(&*(i->getUser()))) {
...
}
}

8. 遍历LLVM IR指令的操作数

通过User中提供的op_iterator迭代器来遍历Instruction中的操作数

1
2
3
4
5
6
Instruction* V = ...
for (User::op_iterator op = V->op_begin(), e = V->op_end(); op != e; ++op){
if (Instruction *U = dyn_cast<Instruction>(op->get())) {
...
}
}

9. 获取Basic Block的前驱基本块

LLVM已经提供了遍历基本块的所有前驱的函数

1
2
3
4
5
6
BasicBlock* B = ...
for (auto it = pred_begin(B), et = pred_end(B); it != et; ++it)
{
BasicBlock* predecessor = *it;
...
}