Mos Moh
Mos Moh

Reputation: 317

How can i insert an add instruction in IR file llvm?

I want to insert an add instruction in LLVM IR format, something like x = x + 1, where x is global variable. I have tried this:

GlobalVariable* x = new GlobalVariable(mod,Type::getInt32Ty(Context),false,GlobalValue::CommonLinkage,0,"xCounter");

Value one =  ConstantInt::get(Type::getInt32Ty(Context),1);
newInst = BinaryOperator::Create(Instruction::Add, , one ,"counter", insertPos);

But an error occurs, it does not accept type GlobalVariable.

How can I define a global variable and set its value?

Upvotes: 0

Views: 1215

Answers (1)

Oak
Oak

Reputation: 26898

Global variables are always pointers - in your case, its type will be i32*. You need to load from it first before you can add it with anything. You'll then have to store the new value, using the global variable as the store address.

It's the same with local variables, by the way - alloca values are always pointers.

Upvotes: 1

Related Questions