How to increase global variable in LLVM module?

I want to add an instruction at the end of the base unit to increase the value of GlobalVariable (using the C ++ LLVM library). I'm new to LLVM, can I do it directly or does it take to load a global variable, increment it by the desired value and write it back to the global variable?

Even if I load a variable (using the LoadInst constructor), how does the Add command know where the variable is?

For example, look at this IR ocde:

%cell_index = load i32* %cell_index_ptr
%new_cell_index = add i32 1, %cell_index

add command knows which variable to work for (cell_index). But since I will create a load instruction from C ++, I do not know where the variable will be created.

+4
source share
1 answer

, , .

LLVM ++ Instruction Value. LoadInst, . :

IRBuilder<> IR(SomeInsertionPoint);
LoadInst *Load = IR.CreateLoad(MyGlobalVariable);
Value *Inc = IR.CreateAdd(IR.getInt32(1), Load);
StoreInst *Store = IR.CreateStore(Inc, MyGlobalVariable);
+2

All Articles