From aecfc21dc36c6705efdfee01ab12edbe03f96ee2 Mon Sep 17 00:00:00 2001
From: rakdver <rakdver@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Fri, 17 Nov 2006 10:29:07 +0000
Subject: [PATCH] 	PR tree-optimization/29801 	* tree-ssa-ccp.c
 (get_symbol_constant_value): New function. 	(get_default_value): Use
 get_symbol_constant_value. 	(set_lattice_value): ICE when the value of the
 constant is 	changed. 	(visit_assignment): Ignore VDEFs of read-only
 variables.

	* gcc.dg/pr29801.c: New test.



git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@118926 138bc75d-0d04-0410-961f-82ee72b054a4
---
 gcc/ChangeLog                  |  9 ++++++++
 gcc/testsuite/ChangeLog        |  5 ++++
 gcc/testsuite/gcc.dg/pr29801.c | 24 +++++++++++++++++++
 gcc/tree-ssa-ccp.c             | 42 ++++++++++++++++++++++++++--------
 4 files changed, 71 insertions(+), 9 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr29801.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index ebdf75ff2bc5..ab9b5526eec0 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,12 @@
+2006-11-17  Zdenek Dvorak <dvorakz@suse.cz>
+
+	PR tree-optimization/29801
+	* tree-ssa-ccp.c (get_symbol_constant_value): New function.
+	(get_default_value): Use get_symbol_constant_value.
+	(set_lattice_value): ICE when the value of the constant is
+	changed.
+	(visit_assignment): Ignore VDEFs of read-only variables.
+
 2006-11-17  Zdenek Dvorak <dvorakz@suse.cz>
 
 	* tree-vect-transform.c (vect_create_epilog_for_reduction): Fix
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 6ba65d40a585..84bce0c911e3 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2006-11-17  Zdenek Dvorak <dvorakz@suse.cz>
+
+	PR tree-optimization/29801
+	* gcc.dg/pr29801.c: New test.
+
 2006-11-17  Jakub Jelinek  <jakub@redhat.com>
 
 	PR middle-end/29584
diff --git a/gcc/testsuite/gcc.dg/pr29801.c b/gcc/testsuite/gcc.dg/pr29801.c
new file mode 100644
index 000000000000..b61ae51425dc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr29801.c
@@ -0,0 +1,24 @@
+/* We used to crash in ccp here, because the initial constant value of 2
+   was changed to 5.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+static const int a = 2;
+
+int test (int param)
+{
+  int *p = (int *) &a;
+
+  if (param)
+    *p = 5;
+
+  return a;
+}
+
+/* Check that we return the correct (unchanged) value.  */
+
+/* { dg-final { scan-tree-dump-times "return 2" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "return 5" 0 "optimized" } } */
+
+/* { dg-final { cleanup-tree-dump "optimized" } } */
diff --git a/gcc/tree-ssa-ccp.c b/gcc/tree-ssa-ccp.c
index 9dd63fe3fd27..2acd62e36915 100644
--- a/gcc/tree-ssa-ccp.c
+++ b/gcc/tree-ssa-ccp.c
@@ -293,6 +293,24 @@ ccp_decl_initial_min_invariant (tree t)
   return true;
 }
 
+/* If SYM is a constant variable with known value, return the value.
+   NULL_TREE is returned otherwise.  */
+
+static tree
+get_symbol_constant_value (tree sym)
+{
+  if (TREE_STATIC (sym)
+      && TREE_READONLY (sym)
+      && !MTAG_P (sym))
+    {
+      tree val = DECL_INITIAL (sym);
+      if (val
+	  && ccp_decl_initial_min_invariant (val))
+	return val;
+    }
+
+  return NULL_TREE;
+}
 
 /* Compute a default value for variable VAR and store it in the
    CONST_VAL array.  The following rules are used to get default
@@ -320,6 +338,7 @@ get_default_value (tree var)
 {
   tree sym = SSA_NAME_VAR (var);
   prop_value_t val = { UNINITIALIZED, NULL_TREE, NULL_TREE };
+  tree cst_val;
   
   if (!do_store_ccp && !is_gimple_reg (var))
     {
@@ -333,16 +352,12 @@ get_default_value (tree var)
       val.lattice_val = CONSTANT;
       val.value = SSA_NAME_VALUE (var);
     }
-  else if (TREE_STATIC (sym)
-	   && TREE_READONLY (sym)
-	   && !MTAG_P (sym)
-	   && DECL_INITIAL (sym)
-	   && ccp_decl_initial_min_invariant (DECL_INITIAL (sym)))
+  else if ((cst_val = get_symbol_constant_value (sym)) != NULL_TREE)
     {
       /* Globals and static variables declared 'const' take their
 	 initial value.  */
       val.lattice_val = CONSTANT;
-      val.value = DECL_INITIAL (sym);
+      val.value = cst_val;
       val.mem_ref = sym;
     }
   else
@@ -415,9 +430,10 @@ set_lattice_value (tree var, prop_value_t new_val)
      value.  If *OLD_VAL and NEW_VAL are the same, return false to
      inform the caller that this was a non-transition.  */
 
-  gcc_assert (old_val->lattice_val <= new_val.lattice_val
+  gcc_assert (old_val->lattice_val < new_val.lattice_val
               || (old_val->lattice_val == new_val.lattice_val
-		  && old_val->value == new_val.value
+		  && ((!old_val->value && !new_val.value)
+		      || operand_equal_p (old_val->value, new_val.value, 0))
 		  && old_val->mem_ref == new_val.mem_ref));
 
   if (old_val->lattice_val != new_val.lattice_val)
@@ -1222,7 +1238,15 @@ visit_assignment (tree stmt, tree *output_p)
       /* Set the value of every VDEF to VAL.  */
       changed = false;
       FOR_EACH_SSA_TREE_OPERAND (vdef, stmt, i, SSA_OP_VIRTUAL_DEFS)
-	changed |= set_lattice_value (vdef, val);
+	{
+	  /* See PR 29801.  We may have VDEFs for read-only variables
+	     (see the handling of unmodifiable variables in
+	     add_virtual_operand); do not attempt to change their value.  */
+	  if (get_symbol_constant_value (SSA_NAME_VAR (vdef)) != NULL_TREE)
+	    continue;
+
+	  changed |= set_lattice_value (vdef, val);
+	}
       
       /* Note that for propagation purposes, we are only interested in
 	 visiting statements that load the exact same memory reference
-- 
GitLab