Insert call to malloc in gimple

Prathamesh Kulkarni
Mon Jan 18 16:51:00 GMT 2016

On 18 January 2016 at 01:44, Cristina Georgiana Opriceana
<> wrote:
> Hello,
> I am trying to recognize array declarations, get their size and
> replace them with dynamic allocation, through malloc and free calls in
> gimple.
> So far I've managed to find arrays with something like:
>   if (TREE_CODE(var) == VAR_DECL &&
>            TREE_CODE(var_type) == ARRAY_TYPE ) { ... }
> But how can I get the size of the array?
DECL_SIZE (var) for size in bits / DECL_SIZE_UNIT (var) for size in
bytes (defined in tree.h).
> Also, supposing that malloc is called at least once, how can I
> construct a new gimple call statement that  refers to malloc?
I assume you want to pass to malloc() the size of the array ?
sth like this should perhaps work (haven't tested):
tree size = DECL_SIZE_UNIT (var);
tree malloc_decl = builtin_decl_explicit (BUILT_IN_MALLOC);
gcall *stmt = gimple_build_call (malloc_decl, 1, size);
This will create stmt, and to insert it in basic-block use one of the
gsi_insert_* () routines.

> Any ideas or examples would help me a lot, as I couldn't find too many.
> Thanks,
> Cristina

More information about the Gcc-help mailing list