public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Guenther <rguenther@suse.de>
To: gcc-patches@gcc.gnu.org
Cc: "Joseph S. Myers" <joseph@codesourcery.com>
Subject: [PATCH][C] Fixup pointer-int-sum
Date: Thu, 07 Jul 2011 11:44:00 -0000	[thread overview]
Message-ID: <alpine.LNX.2.00.1107071332580.810@zhemvz.fhfr.qr> (raw)


This tries to make sense of the comments and code in the code
doing the index - size multiplication in pointer-int-sum.  It
also fixes a bogus integer-constant conversion which results
in not properly canonicalized integer constants.

The comment in the code claims the index - size multiplication
is carried out signed, which doesn't match the code which does
it unsigned (the commend dates back to rev. 6733 where we _did_
carry out the multiplication in a signed type, using
c_common_type_for_size (TYPE_PRECISION (sizetype), 0)).  The
following patch makes us preserve the signedness of intop
so that for signed intop the multiplication will be known to
not overflow (what is actually the C semantics - is the
multiplication allowed to overflow for unsigned intop?  If not
I guess the orginal code of always choosing a signed type was
more correct and we should go back to it instead?)

The comment also claims there is a sign-extension of t
to sizetype - that's not true either, it's just a sign-change.

Joseph, do we want an unconditional (un-)signed multiplication
(before the patch it's unsigned), or what the patch does?

Bootstrapped and tested on x86_64-unknown-linux-gnu.  I'll also
test the unconditionally signed variant.

Thanks,
Richard.

2011-07-07  Richard Guenther  <rguenther@suse.de>

	* c-common.c (pointer_int_sum): Do the index times size
	multiplication in the signedness of index.  Properly
	strip overflow flags.

Index: gcc/c-family/c-common.c
===================================================================
--- gcc/c-family/c-common.c	(revision 175962)
+++ gcc/c-family/c-common.c	(working copy)
@@ -3737,23 +3737,22 @@ pointer_int_sum (location_t loc, enum tr
 
   /* Convert the integer argument to a type the same size as sizetype
      so the multiply won't overflow spuriously.  */
-  if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
-      || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
+  if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
     intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
-					     TYPE_UNSIGNED (sizetype)), intop);
+					     TYPE_UNSIGNED (TREE_TYPE (intop))),
+		     intop);
 
   /* Replace the integer argument with a suitable product by the object size.
-     Do this multiplication as signed, then convert to the appropriate type
-     for the pointer operation and disregard an overflow that occured only
-     because of the sign-extension change in the latter conversion.  */
+     Do this multiplication in a widened intop type, then convert to the
+     appropriate type for the pointer operation and disregard an overflow
+     that occured only because of the sign-change in the latter conversion.  */
   {
     tree t = build_binary_op (loc,
 			      MULT_EXPR, intop,
 			      convert (TREE_TYPE (intop), size_exp), 1);
     intop = convert (sizetype, t);
     if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
-      intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
-				  TREE_INT_CST_HIGH (intop));
+      intop = double_int_to_tree (sizetype, tree_to_double_int (intop));
   }
 
   /* Create the sum or difference.  */

             reply	other threads:[~2011-07-07 11:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-07 11:44 Richard Guenther [this message]
2011-07-07 14:56 ` Joseph S. Myers
2011-07-07 15:02   ` Richard Guenther

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.1107071332580.810@zhemvz.fhfr.qr \
    --to=rguenther@suse.de \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    /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).