Neitral
Neitral

Reputation: 81

How to identify any type of instruction in LLVM/C++?

I've been asked to do a LLVM function that allows me to find: jump/branch, load/store, call, 'arithmetic and other type of instruction'.

As far as I managed to do, I've been able to find the CallInst, LoadInst, StoreInst and BranchInst doing the following code with dyn_cast:

for (const Function &F : *M) 
    for (const BasicBlock &BB : F) 
        for (const Instruction &I : BB) 
            if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) 
                 count++;;

I'm struggling to find a way of extracting all the arithmetic and 'other type' instructions.

Thanks for your time.

Upvotes: 8

Views: 6319

Answers (1)

Lanbosudo
Lanbosudo

Reputation: 101

You can see here

Use

if (llvm::isa <llvm::LoadInst> (I))

or llvm::StoreInst, etc.

For instruction containing binary operators, llvm::isa <llvm::BinaryOperator> (I) can't differentiate them. Use

if (!strncmp(I.getOpcodeName(), "add", 4))

or

if (I.getOpcode == llvm::Add)

You can find the OpcodeNames and Opcode here and here

Upvotes: 10

Related Questions