This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Typecasting information in MEM[...] GIMPLE
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Uday Khedker <uday at cse dot iitb dot ac dot in>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>, Vini Kanvar <vini at cse dot iitb dot ac dot in>
- Date: Mon, 27 Jul 2015 12:20:19 +0200
- Subject: Re: Typecasting information in MEM[...] GIMPLE
- Authentication-results: sourceware.org; auth=none
- References: <1437840060 dot 2559 dot 18 dot camel at laptop> <55B60279 dot 5010707 at cse dot iitb dot ac dot in>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Mon, Jul 27, 2015 at 03:35:45PM +0530, Uday Khedker wrote:
> We are interested in extracting the type of a tree node that appears within
> MEM_REF.
>
> Given a C program:
>
> struct node * * pvar;
> struct node qvar;
> pvar = (struct node * *) malloc (sizeof (struct node *));
> *pvar = &qvar;
>
> It is transformed into the following GIMPLE code:
>
> void * * pvar.1;
> void * pvar.0;
> pvar.0_1 = malloc (4);
> pvar = pvar.0_1;
> MEM[(struct node * *)pvar.0_1] = &qvar;
>
> We wish to discover the type of the argument of MEM[...] in the last
> GIMPLE statement. We can see from the GIMPLE dump that the argument's
> type is "struct node * *". How do we extract this from the tree
> definition of MEM[...]?
>
> We speculate the following solution: Given a variable var (whose tree is
> tree_of_var) and a tree, say t,
>
> if (TREE_CODE(t) is MEM_REF) and (TREE_OPERAND(t, 0) is tree_of_var)
> then
> the type of the expression inside MEM[...] of tree t is
> POINTER_TYPE to TREE_TYPE(t).
>
> Is is correct? It is general enough?
A MEM_REF has 3 possibly distinct types. If TREE_CODE (t) == MEM_REF,
one type, TREE_TYPE (t), is the type of the access, struct node *
in the above case. Another type is one for alias analysis purposes,
stored in TREE_TYPE (TREE_OPERAND (t, 1)), this one will be
struct node ** in your case. And yet another type is the type of the
pointer, TREE_TYPE (TREE_OPERAND (t, 0)), which usually is the same
as pointer to TREE_TYPE (t) initially, but as most of pointer conversions
are regarded as useless, after optimization passes you often can end up
there with very different type.
The type for alias analysis purposes can also differ from pointer to
TREE_TYPE (t), consider e.g.
short *p = ...;
int i = 26;
memcpy (p, &i, sizeof (int));
which is folded (depending on alignment behavior) as MEM_REF[(int *)p] = 26;
and here TREE_TYPE (t) will be int, TREE_TYPE (TREE_OPERAND (t, 0)) likely
short * and TREE_TYPE (TREE_OPERAND (t, 1)) likely char * (ref_all).
Jakub