* fold-const.c (size_binop): In the fast-paths for X+0, 0+X, X-0 and
authorsayle <sayle@138bc75d-0d04-0410-961f-82ee72b054a4>
Sun, 28 Jan 2007 03:48:41 +0000 (03:48 +0000)
committersayle <sayle@138bc75d-0d04-0410-961f-82ee72b054a4>
Sun, 28 Jan 2007 03:48:41 +0000 (03:48 +0000)
1*X check that the constant hasn't overflowed, to preserve the
TREE_OVERFLOW bit.
(round_up): Provide an efficient implementation when rouding-up an
INTEGER_CST to a power-of-two.

* gcc-dg/large-size-array-3.c: New test case.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@121252 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/large-size-array-3.c [new file with mode: 0644]

index 7d566d2..2f0b439 100644 (file)
@@ -1,3 +1,11 @@
+2007-01-27  Roger Sayle  <roger@eyesopen.com>
+
+       * fold-const.c (size_binop): In the fast-paths for X+0, 0+X, X-0 and
+       1*X check that the constant hasn't overflowed, to preserve the
+       TREE_OVERFLOW bit.
+       (round_up): Provide an efficient implementation when rouding-up an
+       INTEGER_CST to a power-of-two.
+
 2007-01-28  Ralf Wildenhues  <Ralf.Wildenhues@gmx.de>
 
        * doc/sourcebuild.texi: Add comma for clarity.
index cd4d684..b606587 100644 (file)
@@ -1815,13 +1815,23 @@ size_binop (enum tree_code code, tree arg0, tree arg1)
   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
     {
       /* And some specific cases even faster than that.  */
-      if (code == PLUS_EXPR && integer_zerop (arg0))
-       return arg1;
-      else if ((code == MINUS_EXPR || code == PLUS_EXPR)
-              && integer_zerop (arg1))
-       return arg0;
-      else if (code == MULT_EXPR && integer_onep (arg0))
-       return arg1;
+      if (code == PLUS_EXPR)
+       {
+         if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
+           return arg1;
+         if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
+           return arg0;
+       }
+      else if (code == MINUS_EXPR)
+       {
+         if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
+           return arg0;
+       }
+      else if (code == MULT_EXPR)
+       {
+         if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
+           return arg1;
+       }
 
       /* Handle general case of two integer constants.  */
       return int_const_binop (code, arg0, arg1, 0);
@@ -13505,10 +13515,36 @@ round_up (tree value, int divisor)
     {
       tree t;
 
-      t = build_int_cst (TREE_TYPE (value), divisor - 1);
-      value = size_binop (PLUS_EXPR, value, t);
-      t = build_int_cst (TREE_TYPE (value), -divisor);
-      value = size_binop (BIT_AND_EXPR, value, t);
+      if (TREE_CODE (value) == INTEGER_CST)
+       {
+         unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
+         HOST_WIDE_INT high;
+
+         if ((low & (divisor - 1)) == 0)
+           return value;
+
+         high = TREE_INT_CST_HIGH (value);
+         low &= ~(divisor - 1);
+         low += divisor;
+         if (low == 0)
+           high++;
+
+         t = build_int_cst_wide_type (TREE_TYPE (value), low, high);
+         if ((TREE_OVERFLOW (value) || integer_zerop (t))
+             && !TREE_OVERFLOW (t))
+           {
+             t = copy_node (t);
+             TREE_OVERFLOW (t) = 1;
+           }
+         return t;
+       }
+      else
+       {
+         t = build_int_cst (TREE_TYPE (value), divisor - 1);
+         value = size_binop (PLUS_EXPR, value, t);
+         t = build_int_cst (TREE_TYPE (value), -divisor);
+         value = size_binop (BIT_AND_EXPR, value, t);
+       }
     }
   else
     {
index ce96d1a..9ed1d06 100644 (file)
@@ -1,5 +1,9 @@
 2007-01-27  Roger Sayle  <roger@eyesopen.com>
 
+       * gcc-dg/large-size-array-3.c: New test case.
+
+2007-01-27  Roger Sayle  <roger@eyesopen.com>
+
        * gfortran.dg/forall_7.f90: New test case.
 
 2007-01-27  Paul Thomas  <pault@gcc.gnu.org>
diff --git a/gcc/testsuite/gcc.dg/large-size-array-3.c b/gcc/testsuite/gcc.dg/large-size-array-3.c
new file mode 100644 (file)
index 0000000..954e28e
--- /dev/null
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+
+#ifdef __LP64__
+#define DIM UINT_MAX>>1
+#else
+#define DIM 65536
+#endif
+
+int
+sub (int *a)
+{
+  return a[0];
+}
+
+int
+main (void)
+{
+  int a[DIM][DIM];  /* { dg-error "size of array 'a' is too large" } */
+  return sub (&a[0][0]);
+}