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 comparisons against UINT_MAX




I thought that since this patch fixes PR optimization/3640, it
might still be a candidate for 3.1 even during the code freeze.

It transpires there is a missing case in the comparison against
maximum and minimum integer values section in fold().  This is
the simple case of unsigned comparisons against the constant ~0
of the appropriate size (e.g. UINT_MAX).

As shown in PR opt/3640 (extracted from GMP), this comparison is
eventually constant folded in the RTL, but delaying this transformation
that late inhibits several other optimizations that are applied
earlier.


The cases in the two switch statements should be identical to each
other and the equivalent signed comparison clause a few lines earlier.
There are two separate comparison conditions to cleanly avoid overflowing
the HOST_WIDE_INT.  Hopefully, the patch itself is "obvious".


Tested by a "make bootstrap" and "make -k check" (without ada) on
i686-pc-linux-gnu with no new regressions.  Alas, no simple testcase
as this is a code quality issue.


Still ok for 3.1?



2002-01-15  Roger Sayle  <roger@eyesopen.com>
	* fold-const.c (fold): Optimize unsigned comparisons against
	UINT_MAX (and similar unsigned constants).  Fixes PR opt/3640.


*** gcc/gcc/fold-const.c	Sun Dec 16 21:19:08 2001
--- patch8/gcc/fold-const.c	Mon Jan 14 22:48:27 2002
***************
*** 1,6 ****
  /* Fold a constant sub-tree into a single node for C-compiler
     Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
!    1999, 2000, 2001 Free Software Foundation, Inc.

  This file is part of GCC.

--- 1,6 ----
  /* Fold a constant sub-tree into a single node for C-compiler
     Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
!    1999, 2000, 2001, 2002 Free Software Foundation, Inc.

  This file is part of GCC.

*************** fold (expr)
*** 6753,6758 ****
--- 6753,6812 ----
  		default:
  		  break;
  		}
+
+             else if (TREE_INT_CST_HIGH (arg1) == 0
+                      && width < HOST_BITS_PER_WIDE_INT
+                      && (TREE_INT_CST_LOW (arg1)
+                          == ((unsigned HOST_WIDE_INT) 1 << width) - 1)
+                      && TREE_UNSIGNED (TREE_TYPE (arg1)))
+               switch (TREE_CODE (t))
+                 {
+                 case GT_EXPR:
+                   return omit_one_operand (type,
+                                            convert (type, integer_zero_node),
+                                            arg0);
+                 case GE_EXPR:
+                   TREE_SET_CODE (t, EQ_EXPR);
+                   break;
+
+                 case LE_EXPR:
+                   return omit_one_operand (type,
+                                            convert (type, integer_one_node),
+                                            arg0);
+                 case LT_EXPR:
+                   TREE_SET_CODE (t, NE_EXPR);
+                   break;
+
+                 default:
+                   break;
+                 }
+
+             else if (TREE_INT_CST_HIGH (arg1) == 0
+                      && width == HOST_BITS_PER_WIDE_INT
+                      && (TREE_INT_CST_LOW (arg1)
+                          == ((unsigned HOST_WIDE_INT) ~0))
+                      && TREE_UNSIGNED (TREE_TYPE (arg1)))
+               switch (TREE_CODE (t))
+                 {
+                 case GT_EXPR:
+                   return omit_one_operand (type,
+                                            convert (type, integer_zero_node),
+                                            arg0);
+                 case GE_EXPR:
+                   TREE_SET_CODE (t, EQ_EXPR);
+                   break;
+
+                 case LE_EXPR:
+                   return omit_one_operand (type,
+                                            convert (type, integer_one_node),
+                                            arg0);
+                 case LT_EXPR:
+                   TREE_SET_CODE (t, NE_EXPR);
+                   break;
+
+                 default:
+                   break;
+                 }
  	  }
        }


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-438-3470


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