public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Guenther <rguenther@suse.de>
To: gcc@gcc.gnu.org
Subject: The C++ FE drops qualifiers on pointer dereference
Date: Fri, 17 Jul 2009 13:26:00 -0000	[thread overview]
Message-ID: <alpine.LNX.2.00.0907171512240.16347@zhemvz.fhfr.qr> (raw)


The C++ frontend drops qualifiers when performing pointer dereference:

void foo(int * __restrict p, int * __restrict__ q, int i)
{
  *q = p[i];
}

;; Function void foo(int*, int*, int) (null)
;; enabled by -tree-original


<<cleanup_point <<< Unknown tree: expr_stmt
  (void) (*(int *) q = *((int *) p + (long unsigned int) ((long unsigned 
int) i * 4))) >>>
>>;

while this isn't a big issue for the plain *q (the cast is stripped
as useless during gimplification), the second cast results in the
gimplifier generating an unqualified temporary to do the dereference:

void foo(int*, int*, int) (int * restrict p, int * restrict q, int i)
{
  long unsigned int D.2088;
  long unsigned int D.2089;
  int * D.2090;
  int D.2091;

  D.2088 = (long unsigned int) i;
  D.2089 = D.2088 * 4;
  D.2090 = p + D.2089;
  D.2091 = *D.2090;
  *q = D.2091;
}

The C frontend seems to get away without doing this conversion, the
C++ frontend does it here:

typeck.c:decay_conversion

  /* [basic.lval]

     Non-class rvalues always have cv-unqualified types.  */
  type = TREE_TYPE (exp);
  if (!CLASS_TYPE_P (type) && cp_type_quals (type))
    exp = build_nop (TYPE_MAIN_VARIANT (type), exp);

called from

#1  0x0000000000657c35 in cp_build_indirect_ref (ptr=0x7ffff7edcf30, 
    errorstring=0x120f491 "unary *", complain=3)
    at /space/rguenther/src/svn/trunk/gcc/cp/typeck.c:2493
2490      if (ptr == current_class_ptr)
2491        return current_class_ref;
2492
2493      pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2494                 ? ptr : decay_conversion (ptr));
2495      type = TREE_TYPE (pointer);

The C++ frontend in this way currently pessimizes code by emitting
dereferences of non-restrict qualified pointers where the user
used a restrict qualified pointer.

Where can this be fixed?  I currently have a hack to not strip
restrict qualifiers in decay_conversion, but that feels a little
too hackish.  What's the important piece of decay_conversion
that is necessary for indirect refs?

Thanks,
Richard.

             reply	other threads:[~2009-07-17 13:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-17 13:26 Richard Guenther [this message]
2009-07-17 13:54 ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LNX.2.00.0907171512240.16347@zhemvz.fhfr.qr \
    --to=rguenther@suse.de \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).