public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix PR81097
@ 2017-06-20 12:45 Richard Biener
  0 siblings, 0 replies; only message in thread
From: Richard Biener @ 2017-06-20 12:45 UTC (permalink / raw)
  To: gcc-patches


The following fixes PR81097 but not eventually more latent issues
in transforming ~x "back" to -x - 1.  I want a testcase before
fiddling more with this.  The following follows the pattern
of previous fixes to this function, making sure to do negation
in 'type'.

Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to trunk.

Richard.

2017-06-20  Richard Biener  <rguenther@suse.de>

	PR middle-end/81097
	* fold-const.c (split_tree): Fold to type before negating.

	* c-c++-common/ubsan/pr81097.c: New testcase.

Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c	(revision 249397)
+++ gcc/fold-const.c	(working copy)
@@ -853,9 +853,9 @@ split_tree (location_t loc, tree in, tre
 	   && code == PLUS_EXPR)
     {
       /* -X - 1 is folded to ~X, undo that here.  Do _not_ do this
-         when IN is constant.  */
-      *minus_litp = build_one_cst (TREE_TYPE (in));
-      var = negate_expr (TREE_OPERAND (in, 0));
+         when IN is constant.  Convert to TYPE before negating.  */
+      *minus_litp = build_one_cst (type);
+      var = negate_expr (fold_convert_loc (loc, type, TREE_OPERAND (in, 0)));
     }
   else
     var = in;
Index: gcc/testsuite/c-c++-common/ubsan/pr81097.c
===================================================================
--- gcc/testsuite/c-c++-common/ubsan/pr81097.c	(nonexistent)
+++ gcc/testsuite/c-c++-common/ubsan/pr81097.c	(working copy)
@@ -0,0 +1,12 @@
+/* { dg-do run } */
+/* { dg-options "-fsanitize=undefined -fsanitize-undefined-trap-on-error" } */
+
+unsigned int a = 3309568;
+unsigned int b = -1204857327;
+short c = -10871;
+short x;
+int main()
+{
+  x = ((short)(~a) | ~c) +  ((short)(~b) | ~c);
+  return 0;
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-06-20 12:45 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-20 12:45 [PATCH] Fix PR81097 Richard Biener

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).