通过查找方法调用的数量
Pass finding number of methods calls
我是编写 LLVM passes 的新手,我想知道如果我想计算我的主函数调用 printf() 的次数(作为示例),我应该在我的 pass 中添加什么。
说我有这个超级刺激的主线:
#include <stdio.h>
int main() {
printf("hello world\n");
return 0;
}
而且我希望我的通行证能够查看 printf() 是否被调用以及它被调用了多少次(例如,如果它处于某种循环中)。
这是我目前的通行证:
namespace {
struct SkeletonPass : public FunctionPass {
static char ID;
SkeletonPass() : FunctionPass(ID) {}
virtual bool runOnFunction(Function &F) {
errs() << "I saw a function called " << F.getName() << "!\n";
errs() << "It uses printf()" << F. //I'd like to write out how many times printf() is used here
return false;
}
};
}
如果你只想检查printf是否被调用,而不是通过CFG。
llvm 子类 runOnFunction 针对每个函数体运行,您可以遍历函数指令并检查它们是否为 CallInst 并调用特别是 printf。
inst_iterator inst = inst_begin(&F);
inst_iterator instEnd = inst_end(&F);
for(; inst != instEnd; ++inst)
{
if (CallInst* psCall = dyn_cast<CallInst>(&*inst))
{
StringRef name = psCall->getCalledFunction()->getName();
/* check name */
}
}
现在检查它是否在循环中。有一个子类runOnLoop但是LPPassManager接口应该用于更新循环嵌套。(如果你想更新它)
用于控制流搜索,
BasicBlocks 已经以 CFG 方式组织,它们有后继者和前任者,因此您不必构建新图。
你可以使用简单的递归DFS算法来遍历cfg。
看这里
http://eli.thegreenplace.net/2013/09/16/analyzing-function-cfgs-with-llvm
这家伙解释得好多了。
还可以查看 llvm 手册以找到更好的接口和程序。
http://llvm.org/docs/WritingAnLLVMPass.html
我是编写 LLVM passes 的新手,我想知道如果我想计算我的主函数调用 printf() 的次数(作为示例),我应该在我的 pass 中添加什么。
说我有这个超级刺激的主线:
#include <stdio.h>
int main() {
printf("hello world\n");
return 0;
}
而且我希望我的通行证能够查看 printf() 是否被调用以及它被调用了多少次(例如,如果它处于某种循环中)。
这是我目前的通行证:
namespace {
struct SkeletonPass : public FunctionPass {
static char ID;
SkeletonPass() : FunctionPass(ID) {}
virtual bool runOnFunction(Function &F) {
errs() << "I saw a function called " << F.getName() << "!\n";
errs() << "It uses printf()" << F. //I'd like to write out how many times printf() is used here
return false;
}
};
}
如果你只想检查printf是否被调用,而不是通过CFG。
llvm 子类 runOnFunction 针对每个函数体运行,您可以遍历函数指令并检查它们是否为 CallInst 并调用特别是 printf。
inst_iterator inst = inst_begin(&F);
inst_iterator instEnd = inst_end(&F);
for(; inst != instEnd; ++inst)
{
if (CallInst* psCall = dyn_cast<CallInst>(&*inst))
{
StringRef name = psCall->getCalledFunction()->getName();
/* check name */
}
}
现在检查它是否在循环中。有一个子类runOnLoop但是LPPassManager接口应该用于更新循环嵌套。(如果你想更新它)
用于控制流搜索,
BasicBlocks 已经以 CFG 方式组织,它们有后继者和前任者,因此您不必构建新图。 你可以使用简单的递归DFS算法来遍历cfg。
看这里 http://eli.thegreenplace.net/2013/09/16/analyzing-function-cfgs-with-llvm 这家伙解释得好多了。
还可以查看 llvm 手册以找到更好的接口和程序。 http://llvm.org/docs/WritingAnLLVMPass.html