[cfe-dev] How to increment a Global Variable in a LLVM module?

Reid Kleckner rnk at google.com
Thu May 14 10:33:43 PDT 2015

Load it, add it, store it, and then for good measure, plug it, play it,
burn it, and rip it. :)

You just need to add a store to your IR:
%cell_index = load i32* %cell_index_ptr
%new_cell_index = add i32 1, %cell_index
store i32 %new_cell_index, i32* %cell_index_ptr

On Wed, May 13, 2015 at 9:23 PM, Kolmogorov <e-link at msn.com> wrote:

> 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
> 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. (I do not operate the IR code directly but using
> the C++ it was just for the example
> --
> View this message in context:
> http://clang-developers.42468.n3.nabble.com/How-to-increment-a-Global-Variable-in-a-LLVM-module-tp4045533.html
> Sent from the Clang Developers mailing list archive at Nabble.com.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150514/d2904555/attachment.html>

More information about the cfe-dev mailing list