Insert call to malloc in gimple

Cristina Georgiana Opriceana cristina.opriceana@gmail.com
Mon Feb 8 14:23:00 GMT 2016


On Mon, Jan 18, 2016 at 6:51 PM, Prathamesh Kulkarni
<prathamesh.kulkarni@linaro.org> wrote:
> On 18 January 2016 at 01:44, Cristina Georgiana Opriceana
> <cristina.opriceana@gmail.com> 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.
>
This worked indeed, thanks a lot!

Now the only problem left is to compute where to insert the free() call.

I found that BIND_EXPR could give me some information about the
scope of the variable, but I don't know how can I access the statement
list of the BIND_EXPR

Also, what is the difference between BIND_EXPR and GIMPLE_BIND?
I iterated over the basic blocks and on all the gimple_statement in these
blocks, but none has the GIMPLE_BIND code.

Thanks,
Cristina

> Regards,
> Prathamesh
>>
>> 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