[PATCH] Fix PR52134

Richard Guenther rguenther@suse.de
Tue Mar 13 13:44:00 GMT 2012


We fail to fold round_up generated expressions during size expression
folding.

Fixed as follows, bootstrapped and tested on x86_64-unknwon-linux-gnu,
applied to trunk.

Richard.

2012-03-13  Richard Guenther  <rguenther@suse.de>

	PR middle-end/52134
	* fold-const.c (fold_binary_loc): Fold (X * Y) & -(1 << CST) to X * Y
	if Y is a constant multiple of 1 << CST.

	* gcc.dg/pr52134.c: New testcase.

Index: gcc/fold-const.c
===================================================================
*** gcc/fold-const.c	(revision 185322)
--- gcc/fold-const.c	(working copy)
*************** fold_binary_loc (location_t loc,
*** 11398,11403 ****
--- 11401,11420 ----
  			      fold_convert_loc (loc, type, arg0));
  	}
  
+       /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
+          multiple of 1 << CST.  */
+       if (TREE_CODE (arg1) == INTEGER_CST)
+ 	{
+ 	  double_int cst1 = tree_to_double_int (arg1);
+ 	  double_int ncst1 = double_int_ext (double_int_neg (cst1),
+ 					     TYPE_PRECISION (TREE_TYPE (arg1)),
+ 					     TYPE_UNSIGNED (TREE_TYPE (arg1)));
+ 	  if (double_int_equal_p (double_int_and (cst1, ncst1), ncst1)
+ 	      && multiple_of_p (type, arg0,
+ 				double_int_to_tree (TREE_TYPE (arg1), ncst1)))
+ 	    return fold_convert_loc (loc, type, arg0);
+ 	}
+ 
        /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
  	 ((A & N) + B) & M -> (A + B) & M
  	 Similarly if (N & M) == 0,
Index: gcc/testsuite/gcc.dg/pr52134.c
===================================================================
*** gcc/testsuite/gcc.dg/pr52134.c	(revision 0)
--- gcc/testsuite/gcc.dg/pr52134.c	(revision 0)
***************
*** 0 ****
--- 1,14 ----
+ /* { dg-do compile } */
+ /* { dg-options "-fdump-tree-original" } */
+ 
+ unsigned f(unsigned t)
+ {
+     return (t*4)&-4;
+ }
+ int f1(int t)
+ {
+     return (t*4)&-4;
+ }
+ 
+ /* { dg-final { scan-tree-dump-not "\\\&" "original" } } */
+ /* { dg-final { cleanup-tree-dump "original" } } */



More information about the Gcc-patches mailing list