[LLVMdev] Determine whether a stored variable is local or global in the code of LLVM 2.7

Renato Golin rengolin at systemcall.org
Mon Aug 9 01:25:21 PDT 2010


On 9 August 2010 04:32, Nayden Nedev <ndn.nedev at gmail.com> wrote:
> I would like to know something different than that. How I can determine if
> the address at which data is stored is part of the heap or part of the
> stack.

Hi Nayden,

Apart from the fact that this is implementation dependent (ABI issues
aside), an AllocaInst means that the variable was allocated in the
pool where all local variables are. If that's the stack in your case,
there you have it.

GlobalVariable is somewhat more complex, since it can be external,
static, const, zero-initialized and that changes where it's going to
be stored (not to mention linker scripts and scattered images).

About heap variables, well, the pointer has to be locally allocated
(or global), so there will be an AllocaInst (or GlobalVariable) to
point to that region in (what we're calling) the heap. If the
AllocaInst is a pointer and point to a local variable (another
AllocaInst) or a random region in memory (heap? source? data?), it up
to the program and it's not always possible to determine just by
looking at the pointer itself.

For that case, you'll have to do some deeper analysis or keep track
whenever you call malloc-like functions to make sure they're pointing
to heap, rather than stack, source or data. There will be a time-frame
when the pointer will be assigned with malloc and not by anything else
(any other assignment will invalidate the assumption, unless the
assignee is also a malloc'ed variable itself), etc.

I'm not sure LLVM has something like this but wouldn't be too hard to
implement, I guess.

cheers,
--renato



More information about the llvm-dev mailing list