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] Fix PR27116, negate_expr bug


This fixes PR27116 by not allowing -~a -> a + 1 transformation for
non-wrapping types.

Bootstrapped and tested on x86_64-unknown-linux-gnu.

Ok for mainline?

Thanks,
Richard.

:ADDPATCH middle-end:

2006-06-08  Richard Guenther  <rguenther@suse.de>

	PR middle-end/27116
	* fold-const.c (negate_expr_p): We can negate BIT_NOT_EXPR
	only, if overflow is defined and not trapping.
	(negate_expr): Likewise.

	* gcc.dg/torture/pr27116.c: New testcase.
	* gcc.dg/pr15785-1.c: Remove test for invalid transformation.

Index: fold-const.c
===================================================================
--- fold-const.c	(revision 114387)
+++ fold-const.c	(working copy)
@@ -945,7 +945,9 @@
       /* Check that -CST will not overflow type.  */
       return may_negate_without_overflow_p (t);
     case BIT_NOT_EXPR:
-       return INTEGRAL_TYPE_P (type);
+       return INTEGRAL_TYPE_P (type)
+       	      && (TYPE_UNSIGNED (type)
+	      	  || (flag_wrapv && !flag_trapv));
 
     case REAL_CST:
     case NEGATE_EXPR:
@@ -1047,7 +1049,9 @@
     {
     /* Convert - (~A) to A + 1.  */
     case BIT_NOT_EXPR:
-      if (INTEGRAL_TYPE_P (type))
+      if (INTEGRAL_TYPE_P (type)
+      	  && (TYPE_UNSIGNED (type)
+	      || (flag_wrapv && !flag_trapv)))
         return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
                             build_int_cst (type, 1));
       break;
Index: testsuite/gcc.dg/torture/pr27116.c
===================================================================
*** testsuite/gcc.dg/torture/pr27116.c	(revision 0)
--- testsuite/gcc.dg/torture/pr27116.c	(revision 0)
***************
*** 0 ****
--- 1,15 ----
+ /* { dg-do run } */
+ 
+ extern void abort(void);
+ 
+ int f(int a, int b)
+ {
+   return (-1 - a) / (-b);
+ }
+ 
+ int main()
+ {
+   if (f(__INT_MAX__, 2) != __INT_MAX__/2 + 1)
+     abort ();
+   return 0;
+ }
Index: testsuite/gcc.dg/pr15785-1.c
===================================================================
*** testsuite/gcc.dg/pr15785-1.c	(revision 114462)
--- testsuite/gcc.dg/pr15785-1.c	(working copy)
*************** void b (int x) {
*** 11,21 ****
  		link_error ();
  }
  
- void c (int x) {
- 	if (!(- (~x) - x))
- 		link_error ();
- }
- 
  void d (int x) {
  	if (!(~ (-x) - x))
  		link_error ();
--- 11,16 ----
*************** void f (int x) {
*** 34,40 ****
  int main (int argc, char *argv[]) {
  	a(argc);
  	b(argc);
- 	c(argc);
  	d(argc);
  	e(argc);
  	f(argc);
--- 29,34 ----


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