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 0 >> x.


Whilst recently reading "Optimizer Evaluation" by Preston Briggs, I
thought I'd check that all of the listed algebraic simplifications were
performed by GCC.  The good news is that GCC optimizes all of the listed
algebraic identities.  However 0 << x and 0 >> x are not constant folded
at the tree-level, but are optimized later during the RTL passes.  This
patch add support for this simplification to the earlier constant folding
pass.  This avoids generating RTL that needs to be cleaned up later, and
may enable additional optimizations.

Whilst there, I also thought I'd add the related constant folding
optimizations for rotations of ~0, i.e. all ones.


This patch has been tested by a complete "make bootstrap" and "make
-k check", all languages except Ada and treelang, against mainline
on i686-pc-linux-gnu with no new regressions.  Its also been tested
against the gcc-3_4-basic-improvements branch by bootstrapping on
i686-pc-cygwin, all languages except Ada and treelang.  I've also
confirmed that the patch is working by inspecting .c.00.rtl.


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


2002-10-07  Roger Sayle  <roger@eyesopen.com>

	* fold-const.c (fold) [LROTATE_EXPR, RROTATE_EXPR]: Optimize
	left and right rotates of ~0, i.e. integer_all_onesp (arg0).
	[LSHIFT_EXPR, RSHIFT_EXPR]: Optimize shifts and rotates of zero.


Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.222
diff -c -3 -p -r1.222 fold-const.c
*** fold-const.c	22 Sep 2002 14:09:32 -0000	1.222
--- fold-const.c	7 Oct 2002 22:11:56 -0000
*************** fold (expr)
*** 5710,5721 ****

        goto binary;

-     case LSHIFT_EXPR:
-     case RSHIFT_EXPR:
      case LROTATE_EXPR:
      case RROTATE_EXPR:
        if (integer_zerop (arg1))
  	return non_lvalue (convert (type, arg0));
        /* Since negative shift count is not well-defined,
  	 don't try to compute it in the compiler.  */
        if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
--- 5710,5729 ----

        goto binary;

      case LROTATE_EXPR:
      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))
+ 	return omit_one_operand (type, arg0, arg1);
+
        /* Since negative shift count is not well-defined,
  	 don't try to compute it in the compiler.  */
        if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)

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]