Reputation: 251
I want to add an instruction at the end of a basic block to increment a GlobalVariable (using the LLVM C++ library). I am pretty new to to the LLVM, can I do this directly or does this require loading the global variable, incrementing it by the desired value and writing back to the global variable ?
Even if I load the variable (with LoadInst constructor), How will the "Add" instruction know where is the variable ?
For example, look at this IR ocde :
%cell_index = load i32* %cell_index_ptr
%new_cell_index = add i32 1, %cell_index
the add instruction knows on which variable to operate (cell_index). But since I will create the load instruction from the C++ I don't know where the variable will be created.
Upvotes: 9
Views: 3261
Reputation: 37177
Yes, you'll have to create load, add, and store instructions.
In LLVM's C++ class hierarchy, Instruction
subclasses Value
. When you create your LoadInst
, you can just refer to it directly when creating new instructions. For example:
IRBuilder<> IR(SomeInsertionPoint);
LoadInst *Load = IR.CreateLoad(MyGlobalVariable);
Value *Inc = IR.CreateAdd(IR.getInt32(1), Load);
StoreInst *Store = IR.CreateStore(Inc, MyGlobalVariable);
Upvotes: 9