re PR tree-optimization/22303 (CCP does not handle STRING_CSTs)
authorRichard Guenther <rguenther@suse.de>
Tue, 16 May 2006 15:34:12 +0000 (15:34 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Tue, 16 May 2006 15:34:12 +0000 (15:34 +0000)
2006-05-16  Richard Guenther  <rguenther@suse.de>

PR tree-optimization/22303
* tree-ssa-ccp.c (fold_const_aggregate_ref): Handle reads
from STRING_CSTs.
(evaluate_stmt): Fall back to fold_const_aggregate_ref, if
ccp_fold did not simplify the statement.

* gcc.dg/tree-ssa/ssa-ccp-13.c: New testcase.

From-SVN: r113826

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-13.c [new file with mode: 0644]
gcc/tree-ssa-ccp.c

index e5a58791f5ed4c80c90bd895f4b24ea1280abb94..c7bfa59c0ec4aae9081e15d5fc48dd7871fcf413 100644 (file)
@@ -1,3 +1,11 @@
+2006-05-16  Richard Guenther  <rguenther@suse.de>
+
+       PR tree-optimization/22303
+       * tree-ssa-ccp.c (fold_const_aggregate_ref): Handle reads
+       from STRING_CSTs.
+       (evaluate_stmt): Fall back to fold_const_aggregate_ref, if
+       ccp_fold did not simplify the statement.
+
 2006-05-16  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR driver/26885
index 8e94d149213a86f5c4478cdcbadf68304f1acb45..ec6b784ffd1118eb5b6dcdf94913eabdcd3c4bb4 100644 (file)
@@ -1,3 +1,8 @@
+2006-05-16  Richard Guenther  <rguenther@suse.de>
+
+       PR tree-optimization/22303
+       * gcc.dg/tree-ssa/ssa-ccp-13.c: New testcase.
+
 2006-05-16  Jakub Jelinek  <jakub@redhat.com>
 
        PR middle-end/27573
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-13.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-13.c
new file mode 100644 (file)
index 0000000..d35adee
--- /dev/null
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+static const char f[3] = "?";
+
+int foo()
+{
+  int i = 0;
+  return f[i] != '?';
+}
+
+/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
+
index 83dce72770b12518951d22ece553668f8dc5cdae..53050e68429ab9a75177e209feb5487a542c41cd 100644 (file)
@@ -1011,7 +1011,8 @@ fold_const_aggregate_ref (tree t)
        }
 
       if (ctor == NULL_TREE
-         || TREE_CODE (ctor) != CONSTRUCTOR
+         || (TREE_CODE (ctor) != CONSTRUCTOR
+             && TREE_CODE (ctor) != STRING_CST)
          || !TREE_STATIC (ctor))
        return NULL_TREE;
 
@@ -1036,6 +1037,20 @@ fold_const_aggregate_ref (tree t)
          return NULL_TREE;
        }
 
+      /* Fold read from constant string.  */
+      if (TREE_CODE (ctor) == STRING_CST)
+       {
+         if ((TYPE_MODE (TREE_TYPE (t))
+              == TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor))))
+             && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor))))
+                 == MODE_INT)
+             && GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor)))) == 1
+             && compare_tree_int (idx, TREE_STRING_LENGTH (ctor)) < 0)
+           return build_int_cst (TREE_TYPE (t), (TREE_STRING_POINTER (ctor)
+                                                 [TREE_INT_CST_LOW (idx)]));
+         return NULL_TREE;
+       }
+
       /* Whoo-hoo!  I'll fold ya baby.  Yeah!  */
       FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
        if (tree_int_cst_equal (cfield, idx))
@@ -1104,7 +1119,7 @@ static prop_value_t
 evaluate_stmt (tree stmt)
 {
   prop_value_t val;
-  tree simplified;
+  tree simplified = NULL_TREE;
   ccp_lattice_t likelyvalue = likely_value (stmt);
 
   val.mem_ref = NULL_TREE;
@@ -1115,14 +1130,14 @@ evaluate_stmt (tree stmt)
     simplified = ccp_fold (stmt);
   /* If the statement is likely to have a VARYING result, then do not
      bother folding the statement.  */
-  else if (likelyvalue == VARYING)
+  if (likelyvalue == VARYING)
     simplified = get_rhs (stmt);
   /* If the statement is an ARRAY_REF or COMPONENT_REF into constant
      aggregates, extract the referenced constant.  Otherwise the
      statement is likely to have an UNDEFINED value, and there will be
      nothing to do.  Note that fold_const_aggregate_ref returns
      NULL_TREE if the first case does not match.  */
-  else
+  else if (!simplified)
     simplified = fold_const_aggregate_ref (get_rhs (stmt));
 
   if (simplified && is_gimple_min_invariant (simplified))