diff mbox

[c-family] Fix PR middle-end/50266

Message ID 201109061401.55969.ebotcazou@adacore.com
State New
Headers show

Commit Message

Eric Botcazou Sept. 6, 2011, 12:01 p.m. UTC
Hi,

this is a regression present on the mainline and 4.6 branch caused by the 
constructor uniquization patch.  The tree_output_constant_def routine rejects 
offsetof-like computations that can be written in the C family of languages.
While the C compiler folds most of them early, it doesn't if an intermediate 
folding step is required.

The proposed fix is to make c_fully_fold also do the offsetof folding.

Tested on x86_64-suse-linux, OK for mainline and 4.6 branch?


2011-09-06  Eric Botcazou  <ebotcazou@adacore.com>

	PR middle-end/50266
	* c-common.c (c_fully_fold_internal) <ADDR_EXPR>: Fold offsetof-like
	computations.


2011-09-06  Eric Botcazou  <ebotcazou@adacore.com>

	* gcc.dg/init-offsetof-1.c: New test.

Comments

Joseph Myers Sept. 6, 2011, 12:25 p.m. UTC | #1
On Tue, 6 Sep 2011, Eric Botcazou wrote:

> Hi,
> 
> this is a regression present on the mainline and 4.6 branch caused by the 
> constructor uniquization patch.  The tree_output_constant_def routine rejects 
> offsetof-like computations that can be written in the C family of languages.
> While the C compiler folds most of them early, it doesn't if an intermediate 
> folding step is required.
> 
> The proposed fix is to make c_fully_fold also do the offsetof folding.
> 
> Tested on x86_64-suse-linux, OK for mainline and 4.6 branch?

OK with a comment explaining the need to fold in this case and with the 
testcase moved to gcc.c-torture/compile rather than having dg-options 
specifying particular options from the set that would be passed in 
gcc.c-torture/compile.  But in general we want to *reduce* the amount of 
folding done in the front ends and move more of it to gimplification time 
or later, except where required for the front end to meet standard 
requirements for diagnostics relating to constant expressions or avoid 
unnecessary warnings in various cases relating to conversions etc. (and 
there may be better ways to avoid those warnings without early 
optimization).
diff mbox

Patch

Index: c-common.c
===================================================================
--- c-common.c	(revision 178488)
+++ c-common.c	(working copy)
@@ -1264,7 +1264,18 @@  c_fully_fold_internal (tree expr, bool i
       STRIP_TYPE_NOPS (op0);
       if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
 	op0 = decl_constant_value_for_optimization (op0);
-      if (op0 != orig_op0 || in_init)
+      if (op0 != orig_op0
+	  && code == ADDR_EXPR
+	  && (op1 = get_base_address (op0)) != NULL_TREE
+	  && TREE_CODE (op1) == INDIRECT_REF
+	  && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
+	{
+	  tree offset = fold_offsetof (op0, op1);
+	  op1
+	    = fold_convert_loc (loc, TREE_TYPE (expr), TREE_OPERAND (op1, 0));
+	  ret = fold_build_pointer_plus_loc (loc, op1, offset);
+	}
+      else if (op0 != orig_op0 || in_init)
 	ret = in_init
 	  ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
 	  : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);