public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Semantics of OBJ_TYPE_REF
@ 2021-06-18 13:01 Erick Ochoa
  2021-06-18 13:15 ` Richard Biener
  2021-06-22 21:58 ` Martin Jambor
  0 siblings, 2 replies; 4+ messages in thread
From: Erick Ochoa @ 2021-06-18 13:01 UTC (permalink / raw)
  To: gcc

Hi,

I am having some trouble understanding the semantics of OBJ_TYPE_REF.
I understand that it is made of three operands:

1. OBJ_TYPE_REF_EXPR: An expression that evaluates the value to use.
2. OBJ_TYPE_REF_OBJECT: Is the object on whose behalf the lookup is
being performed
3. OBJ_TYPE_REF_TOKEN: An integer index to the virtual method table.

The language here is very general and makes it hard for me to understand. Is

1. OBJ_TYPE_REF_EXPR: The virtual function
2. OBJ_TYPE_REF_OBJECT: The "this" object

?

Why is the index needed if it looks like the virtual function is
already pointed to by the first operand? I am reading Hubicka's post
on devirtualization and Part II
(https://hubicka.blogspot.com/2014/01/devirtualization-in-c-part-2-low-level.html)
seems to agree with me on the example below. There is also no mention
of OBJ_TYPE_REF on part II, but there is on part III.

Hubicka's example:

int test() ()
{
  int (*__vtbl_ptr_type) () *tmp1;
  int (*__vtbl_ptr_type) () tmp2;
  struct A a;
    struct A * b;


  _ZN1AC2Ev (&a);
  b = &a;

  tmp1 = b->_vptr.A;
  tmp2 = *tmp1;

  // Notice no OBJ_TYPE_REF below.
  return tmp2 (b);
}

My example:

#include <cstdio>

class A
{
public:
  virtual void foo() { printf("hello\n"); };
  virtual void bar() { printf("world\n"); };
};

int
main(int argc, char**argv )
{
        class A* a = new A();
        a->foo();
        a->bar();
        return 0;
}

// Relevant gimple:

    a = D.2922;
    _1 = a->_vptr.A;
    _2 = *_1; // first element of the virtual table?

    OBJ_TYPE_REF(_2;(struct A)a->0) (a);
    // wouldn't this just be equivalent to
    // _2 (a)

    _3 = a->_vptr.A;
    _4 = _3 + 8;

    _5 = *_4; // second element of the virtual table
    OBJ_TYPE_REF(_5;(struct A)a->1) (a);
    // same
    // _5 (a)

On part III Hubicka says that OBJ_TYPE_REF is a wrapper and that it
represents the type and token. (My guess is that these are the
arguments 2 and 3). I fail to understand why these are needed if we
have the function already.

Thanks! Any help is appreciated.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Semantics of OBJ_TYPE_REF
  2021-06-18 13:01 Semantics of OBJ_TYPE_REF Erick Ochoa
@ 2021-06-18 13:15 ` Richard Biener
  2021-06-22 21:58 ` Martin Jambor
  1 sibling, 0 replies; 4+ messages in thread
From: Richard Biener @ 2021-06-18 13:15 UTC (permalink / raw)
  To: Erick Ochoa, Jan Hubicka; +Cc: GCC Development

On Fri, Jun 18, 2021 at 3:03 PM Erick Ochoa via Gcc <gcc@gcc.gnu.org> wrote:
>
> Hi,
>
> I am having some trouble understanding the semantics of OBJ_TYPE_REF.
> I understand that it is made of three operands:
>
> 1. OBJ_TYPE_REF_EXPR: An expression that evaluates the value to use.
> 2. OBJ_TYPE_REF_OBJECT: Is the object on whose behalf the lookup is
> being performed
> 3. OBJ_TYPE_REF_TOKEN: An integer index to the virtual method table.
>
> The language here is very general and makes it hard for me to understand. Is
>
> 1. OBJ_TYPE_REF_EXPR: The virtual function
> 2. OBJ_TYPE_REF_OBJECT: The "this" object
>
> ?
>
> Why is the index needed if it looks like the virtual function is
> already pointed to by the first operand? I am reading Hubicka's post
> on devirtualization and Part II
> (https://hubicka.blogspot.com/2014/01/devirtualization-in-c-part-2-low-level.html)
> seems to agree with me on the example below. There is also no mention
> of OBJ_TYPE_REF on part II, but there is on part III.
>
> Hubicka's example:
>
> int test() ()
> {
>   int (*__vtbl_ptr_type) () *tmp1;
>   int (*__vtbl_ptr_type) () tmp2;
>   struct A a;
>     struct A * b;
>
>
>   _ZN1AC2Ev (&a);
>   b = &a;
>
>   tmp1 = b->_vptr.A;
>   tmp2 = *tmp1;
>
>   // Notice no OBJ_TYPE_REF below.
>   return tmp2 (b);
> }
>
> My example:
>
> #include <cstdio>
>
> class A
> {
> public:
>   virtual void foo() { printf("hello\n"); };
>   virtual void bar() { printf("world\n"); };
> };
>
> int
> main(int argc, char**argv )
> {
>         class A* a = new A();
>         a->foo();
>         a->bar();
>         return 0;
> }
>
> // Relevant gimple:
>
>     a = D.2922;
>     _1 = a->_vptr.A;
>     _2 = *_1; // first element of the virtual table?
>
>     OBJ_TYPE_REF(_2;(struct A)a->0) (a);
>     // wouldn't this just be equivalent to
>     // _2 (a)
>
>     _3 = a->_vptr.A;
>     _4 = _3 + 8;
>
>     _5 = *_4; // second element of the virtual table
>     OBJ_TYPE_REF(_5;(struct A)a->1) (a);
>     // same
>     // _5 (a)
>
> On part III Hubicka says that OBJ_TYPE_REF is a wrapper and that it
> represents the type and token. (My guess is that these are the
> arguments 2 and 3). I fail to understand why these are needed if we
> have the function already.
>
> Thanks! Any help is appreciated.

For correctness we don't need the OBJ_TYPE_REF but could
indeed just use its first operand.  But we can use OBJ_TYPE_REF
for devirtualization, maybe Honza can explain.

Richard.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Semantics of OBJ_TYPE_REF
  2021-06-18 13:01 Semantics of OBJ_TYPE_REF Erick Ochoa
  2021-06-18 13:15 ` Richard Biener
@ 2021-06-22 21:58 ` Martin Jambor
  2021-06-23  7:59   ` Erick Ochoa
  1 sibling, 1 reply; 4+ messages in thread
From: Martin Jambor @ 2021-06-22 21:58 UTC (permalink / raw)
  To: Erick Ochoa, gcc; +Cc: Jan Hubicka

Hi,

On Fri, Jun 18 2021, Erick Ochoa via Gcc wrote:
> Hi,
>
> I am having some trouble understanding the semantics of OBJ_TYPE_REF.
> I understand that it is made of three operands:
>
> 1. OBJ_TYPE_REF_EXPR: An expression that evaluates the value to use.
> 2. OBJ_TYPE_REF_OBJECT: Is the object on whose behalf the lookup is
> being performed
> 3. OBJ_TYPE_REF_TOKEN: An integer index to the virtual method table.
>
> The language here is very general and makes it hard for me to understand. Is
>
> 1. OBJ_TYPE_REF_EXPR: The virtual function

well, it is never the actual function as in a FUNCTION_DECL, it is
always a pointer to an unknown function.

> 2. OBJ_TYPE_REF_OBJECT: The "this" object
>
> ?
>
> Why is the index needed if it looks like the virtual function is
> already pointed to by the first operand?

As I wrote above, the first operand is just a pointer.  Sometimes other
optimizations can figure out where it leads but often not (or not early
enough for inlining).

In some situations we can get at the actual DECL (and subsequently have
a direct call) using type based devirtualization and for that we use the
other two arguments of OBJ_TYPE_REF.  See the big leading comment in
ipa-devirt.c.


> I am reading Hubicka's post
> on devirtualization and Part II
> (https://hubicka.blogspot.com/2014/01/devirtualization-in-c-part-2-low-level.html)
> seems to agree with me on the example below. There is also no mention
> of OBJ_TYPE_REF on part II, but there is on part III.

I believe the gimple quoted there has been simplified to make it easier
for the reader, since it does not describe the type-based
devirtualization but how other optimizations can together deduce the
call target, and so OBJ_TYPE_REF has been omitted not to confuse anyone.

If you dump the IL for th example yourself, you will see OBJ_TYPE_REF in
the earliest dumps and you will see that it disappears as soon as the
call becomes direct.

Hope this helps,

Martin

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Semantics of OBJ_TYPE_REF
  2021-06-22 21:58 ` Martin Jambor
@ 2021-06-23  7:59   ` Erick Ochoa
  0 siblings, 0 replies; 4+ messages in thread
From: Erick Ochoa @ 2021-06-23  7:59 UTC (permalink / raw)
  To: Martin Jambor; +Cc: gcc, Jan Hubicka

Cool, thanks! I think I understand now.

On Tue, 22 Jun 2021 at 23:58, Martin Jambor <mjambor@suse.cz> wrote:
>
> Hi,
>
> On Fri, Jun 18 2021, Erick Ochoa via Gcc wrote:
> > Hi,
> >
> > I am having some trouble understanding the semantics of OBJ_TYPE_REF.
> > I understand that it is made of three operands:
> >
> > 1. OBJ_TYPE_REF_EXPR: An expression that evaluates the value to use.
> > 2. OBJ_TYPE_REF_OBJECT: Is the object on whose behalf the lookup is
> > being performed
> > 3. OBJ_TYPE_REF_TOKEN: An integer index to the virtual method table.
> >
> > The language here is very general and makes it hard for me to understand. Is
> >
> > 1. OBJ_TYPE_REF_EXPR: The virtual function
>
> well, it is never the actual function as in a FUNCTION_DECL, it is
> always a pointer to an unknown function.
>
> > 2. OBJ_TYPE_REF_OBJECT: The "this" object
> >
> > ?
> >
> > Why is the index needed if it looks like the virtual function is
> > already pointed to by the first operand?
>
> As I wrote above, the first operand is just a pointer.  Sometimes other
> optimizations can figure out where it leads but often not (or not early
> enough for inlining).
>
> In some situations we can get at the actual DECL (and subsequently have
> a direct call) using type based devirtualization and for that we use the
> other two arguments of OBJ_TYPE_REF.  See the big leading comment in
> ipa-devirt.c.
>
>
> > I am reading Hubicka's post
> > on devirtualization and Part II
> > (https://hubicka.blogspot.com/2014/01/devirtualization-in-c-part-2-low-level.html)
> > seems to agree with me on the example below. There is also no mention
> > of OBJ_TYPE_REF on part II, but there is on part III.
>
> I believe the gimple quoted there has been simplified to make it easier
> for the reader, since it does not describe the type-based
> devirtualization but how other optimizations can together deduce the
> call target, and so OBJ_TYPE_REF has been omitted not to confuse anyone.
>
> If you dump the IL for th example yourself, you will see OBJ_TYPE_REF in
> the earliest dumps and you will see that it disappears as soon as the
> call becomes direct.
>
> Hope this helps,
>
> Martin

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-06-23  7:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-18 13:01 Semantics of OBJ_TYPE_REF Erick Ochoa
2021-06-18 13:15 ` Richard Biener
2021-06-22 21:58 ` Martin Jambor
2021-06-23  7:59   ` Erick Ochoa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).