Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the opcode of an instruction?

Tags:

llvm

In fact I have found two solutions, and I want to know if there is any difference:

  • using isa, like isa<LoadInst>(i)
  • using getopcode (i.getopcode() method and comparing to Load)

Which one should I use to check the opcode of an instruction?

like image 899
mdrlol Avatar asked Jan 08 '23 09:01

mdrlol


1 Answers

isa is used to check for an existing derived instruction class. class i.getopcode() could help you to get all of the operations' information.

According to the Inheritance diagram for llvm::Instruction,LLVM internally will divide all instructions into several different classes like llvm::BinaryOperator, llvm::CallInst, llvm::CmpInst, etc. But there is no exact operation information for these classes.

However, for Instruction::getOpcode(), it will directly get the operation from the llvm::Instruction object. You could refer to Instruction.def for an idea about definition of each instruction. Basically, the opcode will be the exact operations the instruction intends to do.

Say, for an LLVM IR add, you can use isa<llvm::BinaryOperator> to know that this is a BinaryOperator. But this is only for what the instruction class it is. If you want to know whether it is an ADD or a SUB. i.getopcode() should be used here.

like image 70
Kun Ling Avatar answered Jan 17 '23 12:01

Kun Ling