This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] Optimize empty class copies within a C++ return statement


While looking at PR65284, I was confused by the gimple we generate for returns of empty classes by value:

class obj {
  public:
   obj(int);
};

obj funky(){
    return obj(555);
}

For the above snippet, we generate:

obj funky() ()
{
  struct obj D.2248;
  struct obj D.2246;

  obj::obj (&D.2246, 555);
  try
    {
      return D.2248;
    }
  finally
    {
      D.2246 = {CLOBBER};
    }
}

Particularly confusing is the return of uninitialized D.2248. Jason tried to beat into me today the fact that it doesn't matter because there is no value to initialize since the class is empty. I still think it's weird, however...

What led us to the above gimple was the fact that we lowered into:

	return retval = D.2248 = D.2246

which was later optimized by cp_gimplify_expr into "return D.2248" because the C++ gimplifier hook notices that the copy is unnecessary.

Jason suggested that it would be nice to remove D.2248 altogether. With the attached patch we notice the superfluous copy in the return value and optimize it away. After some hoops we now get:

  obj::obj (&D.2246, 555);
  try
    {
      return <retval>;
    }
  finally
    {
      D.2246 = {CLOBBER};
    }

Tested on x86-64 Linux.

OK?

Attachment: curr
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]