Search code examples
c++inheritancellvmparent-childllvm-c++-api

Correct way to use PtrUseVisitor Class in LLVM


So I found the InstVisitor class in LLVM, which was refreshing to traverse through the function and see instructions of my interest. A straightforward implementation that I was able to get it working is as follows:

class MyInstVisitor : public InstVisitor <MyInstVisitor> {
    public:

    void visitLoadInst(Instruction &I) {
        errs() << "Load:\t" << I << "\n";
    }
};

Afterward, the usage case is as follows:

void visitor(Function &F) {
    MyInstVisitor MAV;
    MAV.visit(F);
    for (auto &I : F) {
        errs() << I << "\n"; // this traverses a function through for loop
    }
}

After looking around further, I found a child class of InstVisitor, a PtrUseVisitor (https://llvm.org/doxygen/classllvm_1_1PtrUseVisitor.html) that I wanted to use (as I wish to visit all of the users of a pointer value after locating the llvm::LoadInst).

However, I am stuck trying to find a way to use this correctly.

I have tried a variety of things, to summarize:

  1. PtrUseVisitor cannot be instantiated like InstVisitor
  2. as the template looks like this: llvm::PtrUseVisitor<DerivedT> I have also tried doing something like llvm::PtrUseVisitor<Instruction>::visitPtr(), which is incorrect.
  3. I think I'm missing something regarding how to use the child class of inheritance, but examples I could find are related to elementary things (such as inheriting from class Animal, for instance)

And many more... just a bit lost at the moment.

My main goal is to use the following member function: PtrUseVisitor::visitPtr(). Can anyone help with providing me with an example of how to use this?

I appreciate any help you can provide.


Solution

  • Take a look at the SROA pass, which defines a class AllocaSlices::SliceBuilder which inherits from PtrUseVisitor. If you look in that class for calls to Base:: methods, those are making using of PtrUseVisitor.