[PATCH] Fix PR35390, wrong-code with ~(unsigned)~n

Richard Guenther rguenther@suse.de
Wed Feb 27 13:43:00 GMT 2008


Introduced by myself.  And thus fixed.

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

I'll apply this to the 4.3 branch as well, after testing there (together
with the PR34971 fix, as I'm now touching the branch anyway).

Richard.

2008-02-27  Richard Guenther  <rguenther@suse.de>

	PR middle-end/25290
	* fold-const.c (fold_unary): Return the correct argument,
	converted to the result type.

	* gcc.c-torture/execute/pr35390.c: New testcase.

Index: fold-const.c
===================================================================
*** fold-const.c	(revision 132706)
--- fold-const.c	(working copy)
*************** fold_unary (enum tree_code code, tree ty
*** 8340,8346 ****
        if (TREE_CODE (arg0) == INTEGER_CST)
          return fold_not_const (arg0, type);
        else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
! 	return TREE_OPERAND (op0, 0);
        /* Convert ~ (-A) to A - 1.  */
        else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
  	return fold_build2 (MINUS_EXPR, type,
--- 8340,8346 ----
        if (TREE_CODE (arg0) == INTEGER_CST)
          return fold_not_const (arg0, type);
        else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
! 	return fold_convert (type, TREE_OPERAND (arg0, 0));
        /* Convert ~ (-A) to A - 1.  */
        else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
  	return fold_build2 (MINUS_EXPR, type,
Index: testsuite/gcc.c-torture/execute/pr35390.c
===================================================================
*** testsuite/gcc.c-torture/execute/pr35390.c	(revision 0)
--- testsuite/gcc.c-torture/execute/pr35390.c	(revision 0)
***************
*** 0 ****
--- 1,13 ----
+ extern void abort (void);
+ 
+ unsigned int foo (int n)
+ {
+   return ~((unsigned int)~n);
+ }
+ 
+ int main()
+ {
+   if (foo(0) != 0)
+     abort ();
+   return 0;
+ }



More information about the Gcc-patches mailing list