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
Subject: [PATCH] Fix folding of -(unsigned)(a * -b)
Date: Thu, 07 Jul 2011 13:22:00 -0000	[thread overview]
Message-ID: <alpine.LNX.2.00.1107071514100.810@zhemvz.fhfr.qr> (raw)


Folding of $subject is currently broken (noticed that when playing
with types in pointer_int_sum).  We happily ignore the fact
that the negate operates on an unsigned type and change it to
operate on a signed one - which may cause new undefined overflow.
Seen with the testcase below which aborts with current trunk.

The fix is to not strip sign-changing conversions as already
done for ABS_EXPR.

Bootstrapped on x86_64-unknown-linux-gnu, testing in progress.

Richard.

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

	* fold-const.c (fold_unary_loc): Do not strip sign-changes
	for NEGATE_EXPR.

	* gcc.dg/ftrapv-3.c: New testcase.

Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c	(revision 175962)
+++ gcc/fold-const.c	(working copy)
@@ -7561,7 +7561,7 @@ fold_unary_loc (location_t loc, enum tre
   if (arg0)
     {
       if (CONVERT_EXPR_CODE_P (code)
-	  || code == FLOAT_EXPR || code == ABS_EXPR)
+	  || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
 	{
 	  /* Don't use STRIP_NOPS, because signedness of argument type
 	     matters.  */
Index: gcc/testsuite/gcc.dg/ftrapv-3.c
===================================================================
--- gcc/testsuite/gcc.dg/ftrapv-3.c	(revision 0)
+++ gcc/testsuite/gcc.dg/ftrapv-3.c	(revision 0)
@@ -0,0 +1,16 @@
+/* { dg-do run } */
+/* { dg-options "-ftrapv" } */
+
+extern void abort (void);
+unsigned long
+foo (long i, long j)
+{
+  /* We may not fold this to (unsigned long)(i * j).  */
+  return -(unsigned long)(i * -j);
+}
+int main()
+{
+  if (foo (-__LONG_MAX__ - 1, -1) != -(unsigned long)(-__LONG_MAX__ - 1))
+    abort ();
+  return 0;
+}

             reply	other threads:[~2011-07-07 13:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-07 13:22 Richard Guenther [this message]
2011-07-07 15:48 ` Michael Matz

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.1107071514100.810@zhemvz.fhfr.qr \
    --to=rguenther@suse.de \
    --cc=gcc-patches@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).