(layout_type...
authorRichard Kenner <kenner@gcc.gnu.org>
Tue, 3 Oct 1995 23:15:52 +0000 (19:15 -0400)
committerRichard Kenner <kenner@gcc.gnu.org>
Tue, 3 Oct 1995 23:15:52 +0000 (19:15 -0400)
(layout_type, case ARRAY_TYPE): Strip MAX_EXPR from upper bound when
computing length if it just protects against negative length.

From-SVN: r10430

gcc/stor-layout.c

index 3156c58..8cdbf86 100644 (file)
@@ -747,10 +747,28 @@ layout_type (type)
        if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
            && TYPE_SIZE (element))
          {
-           tree length
-             = size_binop (PLUS_EXPR, size_one_node,
-                           size_binop (MINUS_EXPR, TYPE_MAX_VALUE (index),
-                                       TYPE_MIN_VALUE (index)));
+           tree ub = TYPE_MAX_VALUE (index);
+           tree lb = TYPE_MIN_VALUE (index);
+           tree length;
+
+           /* If UB is max (lb - 1, x), remove the MAX_EXPR since the
+              test for negative below covers it.  */
+           if (TREE_CODE (ub) == MAX_EXPR
+               && TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR
+               && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1))
+               && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0),
+                                   lb, 0))
+             ub = TREE_OPERAND (ub, 1);
+           else if (TREE_CODE (ub) == MAX_EXPR
+                    && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR
+                    && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1))
+                    && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1),
+                                                      0),
+                                        lb, 0))
+             ub = TREE_OPERAND (ub, 0);
+
+           length = size_binop (PLUS_EXPR, size_one_node,
+                                size_binop (MINUS_EXPR, ub, lb));
 
            /* If neither bound is a constant and sizetype is signed, make
               sure the size is never negative.  We should really do this