如何正确实现LLVM InstVisitor? [英] How do I correclty implement an LLVM InstVisitor?

查看:336
本文介绍了如何正确实现LLVM InstVisitor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要学习LLVM,我制作了一个ModulePass,它遍历了函数,基本块以及最后的指令.在某些时候,我想深入研究说明并进行分析.在阅读文档时,我遇到了 http://llvm.org/docs/doxygen/html/classllvm_1_1InstVisitor.html ,并且文档建议使用这些结构来有效地遍历IR,而不要执行很多if(auto* I = dyn_cast<>())行.

To learn LLVM I made a ModulePass that runs through the functions, basic blocks, and finally instructions. At some point I want to dig into the instructions and perform analysis. While reading the documentation I came across http://llvm.org/docs/doxygen/html/classllvm_1_1InstVisitor.html and the documentation recommends using these structures to efficiently traverse IR rather than do a lot of if(auto* I = dyn_cast<>()) lines.

我尝试过修改文档示例,但对于BranchInst:

I tried making a variation of the documentation example, but for BranchInst:

struct BranchInstVisitor : public InstVisitor<BranchInst> {                                               
    unsigned Count;                                                                                       
    BranchInstVisitor() : Count(0) {}                                                                     

    void visitBranchInst(BranchInst &BI){                                                                 
        Count++;                                                                                          
        errs() << "BI found! " << Count << "\n";                                                          
    }                                                                                                     

}; // End of BranchInstVisitor

在我的ModulePass中,我创建了访客:

Within my ModulePass, I created the visitor:

for(Module::iterator F = M.begin(), modEnd = M.end(); F != modEnd; ++F){
    BranchInstVisitor BIV; 
    BIV.visit(F);
    ...

不幸的是,编译时我对visit(F)的调用失败:

Unfortunately, my call to visit(F) fails when I compile:

error: invalid static_cast from type ‘llvm::InstVisitor<llvm::BranchInst>* const’ to type ‘llvm::BranchInst*’ static_cast<SubClass*>(this)->visitFunction(F);

如何正确实现LLVM InstVisitor? InstVisitor是否应该在通行证之外运行?如果我错过了文档,请告诉我去哪里.

How do I correctly implement an LLVM InstVisitor? Are InstVisitors supposed to be run outside of passes? If I missed documentation, please let me know where to go.

推荐答案

模板参数应该是您要声明的类型,而不是指令类型,例如:

The template parameter should be the type you're declaring, not a type of instruction, like this:

struct BranchInstVisitor : public InstVisitor<BranchInstVisitor>

每个访问者都可以根据需要覆盖任意多个visit*方法-并不是每个访问者都被一种指令所束缚.那不是很有用.

Each visitor can override as many visit* methods as you want -- it's not like each visitor is tied to one type of instruction. That wouldn't be very useful.

这篇关于如何正确实现LLVM InstVisitor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆