RFA: Handle unsigned characters from string constants in store-ccp

Daniel Jacobowitz drow@false.org
Tue Jul 24 17:43:00 GMT 2007


The attached test fails on all platforms on various hosts, because
tree-ssa-ccp.c has this bit of code:

	    return build_int_cst (TREE_TYPE (t), (TREE_STRING_POINTER (ctor)
					          [TREE_INT_CST_LOW (idx)]));

TREE_TYPE (t) here is the type of the desired value, unsigned char.
TREE_STRING_POINTER points to a const char *.  So if the character we
load has its high bit set, and char is signed, then we create an
INTEGER_CST whose value is -99 but whose type is unsigned.  VRP later
decides, quite reasonably, that no unsigned char could be equal to -99.

Is this patch OK?

-- 
Daniel Jacobowitz
CodeSourcery

2007-07-24  Daniel Jacobowitz  <dan@codesourcery.com>

	* tree-ssa-ccp.c (fold_const_aggregate_ref): Use fold_convert.

2007-07-24  Daniel Jacobowitz  <dan@codesourcery.com>

	* gcc.c-torture/execute/20070724-1.c: New.

Index: testsuite/gcc.c-torture/execute/20070724-1.c
===================================================================
--- testsuite/gcc.c-torture/execute/20070724-1.c	(revision 0)
+++ testsuite/gcc.c-torture/execute/20070724-1.c	(revision 0)
@@ -0,0 +1,11 @@
+void abort (void);
+
+static unsigned char magic[] = "\235";
+static unsigned char value = '\235';
+
+int main()
+{
+  if (value != magic[0])
+    abort ();
+  return 0;
+}
Index: tree-ssa-ccp.c
===================================================================
--- tree-ssa-ccp.c	(revision 126877)
+++ tree-ssa-ccp.c	(working copy)
@@ -1053,8 +1053,10 @@ fold_const_aggregate_ref (tree t)
 	          == 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 fold_convert (TREE_TYPE (t),
+				 build_int_cst (NULL,
+						(TREE_STRING_POINTER (ctor)
+						 [TREE_INT_CST_LOW (idx)])));
 	  return NULL_TREE;
 	}
 



More information about the Gcc-patches mailing list