This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C PATCH] More constistent treatment of C initializers (PR c/83222)


Hi!

In this PR Ian complains that the C FE behaves inconsistently
between static var initializers inside of a function and outside of
functions.  Apparently we've done that earlier, but only at -O1 and above
where decl_constant_value would be called and in initializers inside of
functions would fold to DECL_INITIAL if other conditions are met, but
outside of functions never.  With my recent changes this is the case
also at -O0.

The following patch allows such folding (in initializers only) also
outside of functions.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2017-11-30  Jakub Jelinek  <jakub@redhat.com>

	PR c/83222
	* c-tree.h (decl_constant_value_1): Declare.
	* c-typeck.c (decl_constant_value_1): New function.
	(decl_constant_value): Use it.
	* c-fold.c (c_fully_fold_internal): If in_init, use
	decl_constant_value_1 instead of decl_constant_value.

	* gcc.c-torture/compile/pr83222.c: New test.

--- gcc/c/c-tree.h.jj	2017-11-19 18:06:52.000000000 +0100
+++ gcc/c/c-tree.h	2017-11-30 13:40:02.585208145 +0100
@@ -640,6 +640,7 @@ extern struct c_expr default_function_ar
 							     struct c_expr);
 extern struct c_expr convert_lvalue_to_rvalue (location_t, struct c_expr,
 					       bool, bool);
+extern tree decl_constant_value_1 (tree);
 extern void mark_exp_read (tree);
 extern tree composite_type (tree, tree);
 extern tree build_component_ref (location_t, tree, tree, location_t);
--- gcc/c/c-typeck.c.jj	2017-11-28 22:21:17.000000000 +0100
+++ gcc/c/c-typeck.c	2017-11-30 13:39:41.041475162 +0100
@@ -1832,13 +1832,10 @@ c_size_in_bytes (const_tree type)
 /* Return either DECL or its known constant value (if it has one).  */
 
 tree
-decl_constant_value (tree decl)
+decl_constant_value_1 (tree decl)
 {
-  if (/* Don't change a variable array bound or initial value to a constant
-	 in a place where a variable is invalid.  Note that DECL_INITIAL
-	 isn't valid for a PARM_DECL.  */
-      current_function_decl != NULL_TREE
-      && TREE_CODE (decl) != PARM_DECL
+  if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL.  */
+      TREE_CODE (decl) != PARM_DECL
       && !TREE_THIS_VOLATILE (decl)
       && TREE_READONLY (decl)
       && DECL_INITIAL (decl) != NULL_TREE
@@ -1853,6 +1850,17 @@ decl_constant_value (tree decl)
   return decl;
 }
 
+/* Return either DECL or its known constant value (if it has one).
+   Like the above, but always return decl outside of functions.  */
+
+tree
+decl_constant_value (tree decl)
+{
+  /* Don't change a variable array bound or initial value to a constant
+     in a place where a variable is invalid.  */
+  return current_function_decl ? decl_constant_value_1 (decl) : decl;
+}
+
 /* Convert the array expression EXP to a pointer.  */
 static tree
 array_to_pointer_conversion (location_t loc, tree exp)
--- gcc/c/c-fold.c.jj	2017-11-21 20:23:00.000000000 +0100
+++ gcc/c/c-fold.c	2017-11-30 13:40:53.911571996 +0100
@@ -167,7 +167,10 @@ c_fully_fold_internal (tree expr, bool i
       /* Except for variables which we can optimize to its initializer.  */
       if (VAR_P (expr) && !lval && (optimize || in_init))
 	{
-	  ret = decl_constant_value (expr);
+	  if (in_init)
+	    ret = decl_constant_value_1 (expr);
+	  else
+	    ret = decl_constant_value (expr);
 	  /* Avoid unwanted tree sharing between the initializer and current
 	     function's body where the tree can be modified e.g. by the
 	     gimplifier.  */
--- gcc/testsuite/gcc.c-torture/compile/pr83222.c.jj	2017-11-30 13:53:37.114111959 +0100
+++ gcc/testsuite/gcc.c-torture/compile/pr83222.c	2017-11-30 13:53:19.000000000 +0100
@@ -0,0 +1,21 @@
+/* PR c/83222 */
+
+const char a = 0x42;
+const double b = (double) a;
+const double c = a;
+double d = (double) a;
+double e = a;
+const double f = 1 + (double) a;
+const double g = 1 + a;
+double h = 1 + (double) a;
+double i = 1 + a;
+double j[] = { (double) a, a, 1 + (double) a, 1 + a };
+
+void
+foo (void)
+{
+  static const double k = (double) a;
+  static const double l = a;
+  static const double m = 1 + (double) a;
+  static const double n = 1 + a;
+}

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]