如何在LLVM中dyn_cast以识别StoreInst?

How to dyn_cast in LLVM to identify StoreInst?

本文关键字:cast 识别 StoreInst dyn LLVM      更新时间:2023-10-16

我正在尝试识别StoreInst.我阅读了LLVM手册,并尝试使用dyn_cast来做到这一点。但是下面的程序返回非常奇怪的结果。

bool runOnFunction(Function &F) override{
for (const BasicBlock &BB : F){
for (const Instruction &I : BB){
const char *s = I.getOpcodeName();
std::string str(s);
errs()<<"at the instruction of "<<str<<"n";
if (const StoreInst *SI = dyn_cast<StoreInst>(&I))
errs()<<"FOUND STOREn";
}
}
return true;
}

结果如下。 当指令实际上是 CallInst 时,dyn_cast以某种方式返回 true。有人知道为什么会这样吗?我该如何解决它?顺便说一句,我已经在同一台机器上安装了一些旧版本的 LLVM,但我认为我在 LLVM-7.0.0 下编译了通行证,并通过clang-7 -O0 -S -emit-llvm HelloWorld.cpp使用 clang-7.0.0 获取 .ll 文件。以前安装的版本会影响此版本吗?谢谢!!

at the instruction of call
at the instruction of call
at the instruction of ret
at the instruction of alloca
FOUND STORE
at the instruction of alloca
FOUND STORE
at the instruction of alloca
FOUND STORE 
at the instruction of store
at the instruction of store
at the instruction of store
at the instruction of call
at the instruction of call
at the instruction of ret
at the instruction of call
at the instruction of ret

我怀疑,由于您的传递是FunctionPass,它在多个函数上并行运行,因此您的日志消息打印顺序不正确。