5-t*_*o-9 7 llvm llvm-ir llvm-c++-api
使用ModulePass,我的目标是向上遍历一个SSA图:从一个带有0..2个操作数的语句(大多数操作码都属于那个),我想找出两件事:
例如,假设遵循LLVM IR:
define i32 @mul_add(i32 %x, i32 %y, i32 %z) {
entry:
%tmp = mul i32 %x, %y
%tmp2 = add i32 %tmp, %z
ret i32 %tmp2
}
Run Code Online (Sandbox Code Playgroud)
我将从return语句开始.现在我想知道我要回来的东西:
我如何使用C++ API来实现这些步骤?
解决方案很简单,我将尽可能通用地描述它。
LLVM 中的每个Operandanllvm::Instruction都是 supertype llvm::Value。的一个子类型Value是llvm::Instruction. 这允许通过以下方法进行递归:
bool runOnInstruction(llvm::Instruction *instruction) {
bool flag = false;
for (auto operand = instruction->operands().begin();
operand != instruction->operands().end(); ++operand) {
printOperandStats(operand->get());
flag = runOnOperand(operand->get()) | flag;
}
return flag;
}
bool runOnOperand(llvm::Value *operand) {
operand->printAsOperand(errs(), true);
// ... do something else ...
auto *instruction = dyn_cast<llvm::Instruction>(operand);
if (nullptr != instruction) {
return runOnInstruction(instruction);
} else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
这等于问题所要求的 DFS。每个操作数将被转换为指令并被递归分析。布尔返回值通常用于 LLVM Passes:true 值描述对 IR 的修改。
| 归档时间: |
|
| 查看次数: |
417 次 |
| 最近记录: |