Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 701
Neitral Avatar asked May 11 '18 17:05

Neitral


1 Answers

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

like image 79
Lanbosudo Avatar answered Sep 27 '22 23:09

Lanbosudo