Getting the operands in an LLVM Instruction

Chethan Ravindranath picture Chethan Ravindranath · Dec 28, 2011 · Viewed 17k times · Source

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?

Answer

user1118743 picture user1118743 · Dec 28, 2011

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).