Identifying a pointer to a structure

Richard Biener richard.guenther@gmail.com
Thu Dec 3 10:02:00 GMT 2015


On Thu, Dec 3, 2015 at 8:54 AM, Uday P. Khedker <uday@cse.iitb.ac.in> wrote:
> We are implementing points-to analysis in GCC 4.7.2 and need to distinguish
> between
> pointers to scalars and the pointers to structures. This distinction by
> using the TYPE (TREE_TYPE)
> hierarchy of the tree node of the pointer. We have two questions:
>
> (a) Is it sufficient to check for the presence of RECORD_TYPE in type
> hierarchy?
> (b) Is it safe to assume that the RECORD_TYPE always appears as a leaf node
> in
>     the type description of any pointer to structure?
>
> As an example, the tree nodes of a pointer to an integer (y) and a pointer
> to a structure (f)
> below. It seems to support our hunch.

Yes, your observations are correct with respect to types.

But you can't rely on the pointer type for determining what kind of
object apointer points to.
First because of int *p = &s.i;  with struct { int i; ... } s; points
to an int but it also points
to the start of an aggregate (and so can be trivially casted to a
pointer to s).  Second because
GCCs middle-end (thus GIMPLE) ignores pointer types completely so you can have
an SSA name a_1 of type int *X and a dereference a_1->b.c (thus a_1 points to a
structure object even though it is of type int *).

Richard.

> For example, the tree node of y which is a pointer to an integer is as
> follows:
>
> <var_decl 0x405c8ea0 y
>     type <pointer_type 0x40524a80
>         type <integer_type 0x40524360 int public SI
>             size <integer_cst 0x40514578 constant 32>
>             unit size <integer_cst 0x40514594 constant 4>
>             align 32 symtab 0 alias set 3 canonical type 0x40524360
> precision 32 min <integer_cst 0x405148a4 -2147483648> max <integer_cst
> 0x405148c0 2147483647>
>             pointer_to_this <pointer_type 0x40524a80>>
>         unsigned SI
>         size <integer_cst 0x40514578 constant 32>
>         unit size <integer_cst 0x40514594 constant 4>
>         align 32 symtab 0 alias set 2 canonical type 0x40524a80
>         pointer_to_this <pointer_type 0x405cf300>>
>     used static unsigned SI file struct0.c line 15 col 11
>     size <integer_cst 0x40514578 type <integer_type 0x40524060 bitsizetype>
> constant 32>
>     unit size <integer_cst 0x40514594 type <integer_type 0x40524000
> sizetype> constant 4>
>     align 32 context <translation_unit_decl 0x40529804 D.2319>
>     (mem/f/c:SI (symbol_ref:SI ("y") [flags 0x2]  <var_decl 0x405c8ea0 y>)
> [2 y+0 S4 A32])>
> _
> _ Similarly, the tree node of f which is a pointer to a structure is as
> follows:
>
> <var_decl 0x405c8f60 f
>     type <pointer_type 0x405cf1e0
>         type <*record_type* 0x405cf060 list BLK
>             size <integer_cst 0x40514c08 constant 96>
>             unit size <integer_cst 0x40514c24 constant 12>
>             align 32 symtab 0 alias set 4 canonical type 0x405cf060 fields
> <field_decl 0x405220b8 val> context <translation_unit_decl 0x40529804
> D.2319>
>             pointer_to_this <pointer_type 0x405cf1e0> chain <type_decl
> 0x40529870 list>>
>         public unsigned SI
>         size <integer_cst 0x40514578 constant 32>
>         unit size <integer_cst 0x40514594 constant 4>
>         align 32 symtab 0 alias set 2 canonical type 0x405cf1e0
>         pointer_to_this <pointer_type 0x405cf5a0>>
>     used static unsigned SI file struct0.c line 14 col 25
>     size <integer_cst 0x40514578 type <integer_type 0x40524060 bitsizetype>
> constant 32>
>     unit size <integer_cst 0x40514594 type <integer_type 0x40524000
> sizetype> constant 4>
>     align 32 context <translation_unit_decl 0x40529804 D.2319>
>     (mem/f/c:SI (symbol_ref:SI ("f") [flags 0x2]  <var_decl 0x405c8f60 f>)
> [2 f+0 S4 A32])>
>
> Thanks and regards,
>
> Uday Khedker.
>



More information about the Gcc mailing list