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] Constant fold -1 >> x


Following my recent "0 >> x" constant folding patch, Nathan Sidwell
made the excellent suggestion that a similar optimization could be
performed for arithmetic right shifts of ~0 or -1, i.e. all_onesp.

The following patch implements this suggestion.  It turns out that
this optimization isn't performed at the RTL-level, so being a new
optimization, I've added a test case to the gcc testsuite.  Whilst
I was there I decided to also add checks that "0 >> x" and friends
were also being optimized.

This patch has been tested against the basic improvements branch
with a complete "make bootstrap" and "make -k check", all languages
except Ada and treelang, on i686-pc-linux-gnu with no new
regressions.


Ok for the gcc-3_4-basic-improvements-branch?



2002-10-10  Roger Sayle  <roger@eyesopen.com>
	    Nathan Sidwell <nathan@codesourcery.com>

	* fold-const.c (fold) [RSHIFT_EXPR]: Optimize arithmetic right
	shifts of the form -1 >> x.

	* gcc.c-torture/execute/shiftopt-1.c: New test case.


Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.219.2.4
diff -c -3 -p -r1.219.2.4 fold-const.c
*** fold-const.c	8 Oct 2002 13:44:58 -0000	1.219.2.4
--- fold-const.c	10 Oct 2002 16:00:24 -0000
*************** fold (expr)
*** 5775,5785 ****
      case RROTATE_EXPR:
        if (integer_all_onesp (arg0))
  	return omit_one_operand (type, arg0, arg1);

        /* ... fall through ...  */

      case LSHIFT_EXPR:
!     case RSHIFT_EXPR:
        if (integer_zerop (arg1))
  	return non_lvalue (convert (type, arg0));
        if (integer_zerop (arg0))
--- 5775,5790 ----
      case RROTATE_EXPR:
        if (integer_all_onesp (arg0))
  	return omit_one_operand (type, arg0, arg1);
+       goto shift;

+     case RSHIFT_EXPR:
+       /* Optimize -1 >> x for arithmetic right shifts.  */
+       if (integer_all_onesp (arg0) && ! TREE_UNSIGNED (type))
+ 	return omit_one_operand (type, arg0, arg1);
        /* ... fall through ...  */

      case LSHIFT_EXPR:
!     shift:
        if (integer_zerop (arg1))
  	return non_lvalue (convert (type, arg0));
        if (integer_zerop (arg0))
*** /dev/null	Thu Aug 30 14:30:55 2001
--- gcc.c-torture/execute/shiftopt-1.c	Thu Oct 10 14:54:34 2002
***************
*** 0 ****
--- 1,73 ----
+ /* Copyright (C) 2002  Free Software Foundation
+
+    Check that constant folding of shift operations is working.
+
+    Roger Sayle, 10th October 2002.  */
+
+ extern void abort (void);
+ extern void link_error (void);
+
+ void
+ utest (unsigned int x)
+ {
+   if (x >> 0 != x)
+     link_error ();
+
+   if (x << 0 != x)
+     link_error ();
+
+   if (0 << x != 0)
+     link_error ();
+
+   if (0 >> x != 0)
+     link_error ();
+
+   if (-1 >> x != -1)
+     link_error ();
+
+   if (~0 >> x != ~0)
+     link_error ();
+ }
+
+ void
+ stest (int x)
+ {
+   if (x >> 0 != x)
+     link_error ();
+
+   if (x << 0 != x)
+     link_error ();
+
+   if (0 << x != 0)
+     link_error ();
+
+   if (0 >> x != 0)
+     link_error ();
+
+   if (-1 >> x != -1)
+     link_error ();
+
+   if (~0 >> x != ~0)
+     link_error ();
+ }
+
+ int
+ main ()
+ {
+   utest(9);
+   utest(0);
+
+   stest(9);
+   stest(0);
+
+   return 0;
+ }
+
+ #ifndef __OPTIMIZE__
+ void
+ link_error ()
+ {
+   abort ();
+ }
+ #endif
+


Roger
--
Roger Sayle,                         E-mail: roger@eyesopen.com
OpenEye Scientific Software,         WWW: http://www.eyesopen.com/
Suite 1107, 3600 Cerrillos Road,     Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507.         Fax: (+1) 505-473-0833


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