Chethan Ravindranath
Chethan Ravindranath

Reputation: 2051

Getting the operands in an LLVM Instruction

I am writing an optimization for my compiler and I use LLVM IR as my Intermediate Language. I have parsed the input file and converted it to LLVM IR. During optimization, I need to retrieve the operands of the instructions. I am able to find getOpCode() in the Instruction class, but unable to retrieve the operand list. How do I go about that?

Upvotes: 24

Views: 30600

Answers (2)

Alex
Alex

Reputation: 360

For instance, if you have Instruction* I1, I1->getOperand(0) will return the first operand of type Value*. You can go further, using I1->getOperand(0)->getName() that will return the name of the operand. See Value class methods.

Upvotes: 5

user1118743
user1118743

Reputation:

There are lots of operand accessors, usually provided by the class llvm::User, whose doxygen page is: http://llvm.org/doxygen/classllvm_1_1User.html There's getNumOperands() and getOperand(unsigned int), as well as iterator-style accessors op_begin() and op_end().

For example, given Instruction %X = add i32 %a, 2, I->getOperand(0) will return the Value* for %a, and I->getOperand(1) will return the Value* for i32 2 (castable to ConstantInt).

Upvotes: 35

Related Questions