[LLVMdev] Counting load and stores of variables
mats at planetcatfish.com
Mon Mar 9 05:08:38 PDT 2015
The complexity of the solution depends on
int x; // our variable of interest
int func(int &a)
a++; // a++ actually updates `x` because it's passed in below.
We can of course make MUCH more complex variants of the above, but
there is no way the compiler, whilst compiling `func` can know if the
variable is `x` or not - until it optimizes and inlines `func`, at
The simple C++ solution would be to replace the simple variable with
an object that has a "counter" applied to all accesses of the content
- but it will affect code-gen and it will affect the "compatibility"
of the variable (it's no longer a regular `int` or `double`). Aliasing
gets challenging here.
Using the compiler's profiling options will tell you which blocks are
hit how many times, and if you analyse which blocks contain your
variable [and how many times the variable is updated] - this assumes
your variable isn't aliased.
Another thought that comes to my mind is to use a virtual machine, and
place those variables in a special section of memory, that can then be
attributed with "read_only" or "no access" and have the VM catch the
access, update the value and then continue. But that's not an easy
If the number of variables is small, you could use debug functionality
for memory acesss breakpoints (register a "break on read" and "break
on write" for a certain variable - in the handler for this, count up
the relevant counter and continue). This is probably doable even if
the number of available hardware breakpoints is smaller than the
number of variables you want to monitor, by running the program
multiple times (assuming your program is repeatable with the same
result every time - if it's some sort of random number generation with
new seed every time you run it, or if it is based on some real-time
events (stock market, traffic, weather, live video etc), this won't
Hardware solutions (either virtual machine or memory access
breakpoints) are the only reasonable ways to solve the aliasing
If you have plenty of time, you could also use a processor emulator,
for example qemu - and let it catch writes to certain addresses and
count how many times. Not practical for most applications tho', since
the processor emulators run much slower than real hardware (100-1000x
slower in a good case)
It gets even more interesting if we consider the actual machine
instructions. The `a++` above could in say x86 be implemented as `inc
a` or `mov a, reg; inc reg; move reg, a` - both will indeed read,
modify the value and write it back - but one is explicitly
loading/storing the value, the other is not. How do you account for
that? Is it one or two accesses?
On 9 March 2015 at 11:24, MKT <taram.mohammad at gmail.com> wrote:
> Hi all,
> I’m working on my thesis in which I need to count the number of memory
> access (i.e. load and stores) of each variable of program. how can I do
> that? these are some approaches came to my mind :
> I need to know during the execution of the program how many load and store
> instructions are executed on a specific variable, hence simply counting
> static load and store instructions doesn’t help.
> I also cannot write an IR pass to inject some instructions to count memory
> accesses of variables (instrumenting) , because in IR optimization I’m not
> aware of what will happen in register allocation.
> And if I write a machine pass to inject machine instructions, I guess maybe
> I loose some informations( e.g. variable names) during the optimizations.
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
More information about the llvm-dev