数据类型

ConstantInt(整形常量和布尔常量共用)

1
This class represents both boolean and integral constants.

APINT(无符号数类型)

是”unsigned”, “unsigned long” 或者 “uint64_t”的替代,不过功能更加强大,可以支持不同bit wise的数

1
APInt provides a variety of arithmetic operators and methods to manipulate integer values of any bit-width. It supports both the typical integer arithmetic and comparison operations as well as bitwise manipulation.
1
APInt c = val->getValue() - (randA * randX + randB * randY);

需要注意

  • 未初始化时是0
  • 一旦bit width设置了,除了通过截断,符号扩展,或零扩展,都不会改变
  • 所有运算必须在一样的bit width上进行

LoadInst(读取内存)

读取内存的指令

1
LoadInst *opX = new LoadInst(val->getType(), x, "", BI);

使用技巧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool Test::runOnFunction(Function &F){
for(BasicBlock &BB : F){
vector<Instruction*> Inst;
outs() << "\n";
for(Instruction &I: BB){//都是需要用迭代器遍历!
outs() << "this is I " << I << "\n";
outs() << "this is BB " << *BB.begin() << "\n";
Inst.push_back(&I);
}
for(Instruction *I:Inst){
if(BinaryOperator *BI = dyn_cast<BinaryOperator>(I)){
if(BI->getType()->isIntegerTy(32)){
constReplace(I);
}
}
}
}
}

像这些BasicBlock这些数据结构都需要用迭代器来遍历!

函数

Function

getName(返回函数名字)

获取函数的名字

1
F.getName()

判断

isa<X>(判断是否为X类型)

判断isa<X>(xxx)中的xxx是否为X类型

1
if(isa<ConstantInt>(BI->getOperand(i)))

上方的代码,如果BI->getOperand(i)等于ConstantInt的话,返回true

指令

BinaryOperator

getNumOperands(获取操作数数目)

获取操作数的数目

基本块

splitBasicBlock(指定指令处将基本块分割)

1
2
3
4
BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = "",
bool Before = false);
BasicBlock *splitBasicBlock(Instruction *I, const Twine &BBName = "",
bool Before = false) {

指定指令I,以这个指令为基准将block进行分割