LLVM help

Sir I am trying to get the constant 2 in instruction y=x+2;
Its intermediate representaton is like %y=add nsw i32 %x , 2
If I use getOperand(2).getName() then I get null string.
How can I get the value 2 ?
Regards
Rahul Goyal

RAHUL GOYAL wrote:

Sir I am trying to get the constant 2 in instruction y=x+2;
Its intermediate representaton is like %y=add nsw i32 %x , 2
If I use getOperand(2).getName() then I get null string.
How can I get the value 2 ?
Regards
Rahul Goyal

The first thing you have to do is to see if the operand is a ConstantInt. You can do that by using:

ConstantInt * CI = dyn_cast<ConstantInt>(I->getOperand(2));

If CI is non-zero, then the operand is a ConstantInt.

Then all you have to do is use the getZExtValue() or getSExtValue() (depending on whether you want to treat the constant as signed or unsigned) method of ConstantInt:

if (CI)
    unsigned value = CI->getZExtValue();

-- John T.