This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Fix bug with self-referencing constructor in Ada
- From: Eric Botcazou <ebotcazou at adacore dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Thu, 28 Sep 2006 21:15:40 +0200
- Subject: Fix bug with self-referencing constructor in Ada
This is a regression from the 3.x series present on all platforms.
The gimplifier features a sophisticated machinery to detect constructors on
the rhs that overlap with the lhs, e.g.
type R is record
I1 : Integer;
I2 : Integer;
end record;
R := (R.I1 - R.I2, R.I1 + R.I2);
If overlap is detected, the element will first be preevaluated in a
temporary variable and then the result assigned to the component. The
function in charge is gimplify_init_ctor_preeval.
Now there are these lines in gimplify_init_ctor_preeval:
/* We can't preevaluate if the type contains a placeholder. */
if (type_contains_placeholder_p (TREE_TYPE (*expr_p)))
return;
And
/* Return true if any part of the computation of TYPE involves a
PLACEHOLDER_EXPR. This includes size, bounds, qualifiers
(for QUAL_UNION_TYPE) and field positions. */
static bool
type_contains_placeholder_1 (tree type)
{
/* If the size contains a placeholder or the parent type (component type in
the case of arrays) type involves a placeholder, this type does. */
if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
|| CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
|| (TREE_TYPE (type) != 0
&& type_contains_placeholder_p (TREE_TYPE (type))))
return true;
Note that type_contains_placeholder_1 goes to the pointed-to type for
pointers. This means that gimplify_init_ctor_preeval will punt for records
that contains pointers to unconstrained types, like in the attached testcase,
thus causing a violation of the Write-After-Read dependency in the code.
The problematic lines are there to guard a further gimplification:
/* Gimplify the constructor element to something appropriate for the rhs
of a MODIFY_EXPR. Given that we know the lhs is an aggregate, we know
the gimplifier will consider this a store to memory. Doing this
gimplification now means that we won't have to deal with complicated
language-specific trees, nor trees like SAVE_EXPR that can induce
exponential search behavior. */
one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
that is to say, if you remove them you end up with unmatched PLACEHOLDER_EXPRs
during this gimplification. It turns out that this problem is already solved
in 2 other places in the gimplifier (gimplify_arg, gimplify_modify_expr) with
/* If this is a variable sized type, we must remember the size. */
maybe_with_size_expr (expr_p);
Bootstrapped/regtested on x86_64-suse-linux, applied to mainline as obvious.
2006-09-28 Eric Botcazou <ebotcazou@adacore.com>
* gimplify.c (gimplify_init_ctor_preeval): Call maybe_with_size_expr
on the element before gimplifying it, instead of punting if it is of
variable size.
2006-09-28 Eric Botcazou <ebotcazou@adacore.com>
* gnat.dg/self_aggregate_with_pointer.adb: New test.
--
Eric Botcazou
Index: gimplify.c
===================================================================
--- gimplify.c (revision 117190)
+++ gimplify.c (working copy)
@@ -2670,9 +2670,8 @@ gimplify_init_ctor_preeval (tree *expr_p
return;
}
- /* We can't preevaluate if the type contains a placeholder. */
- if (type_contains_placeholder_p (TREE_TYPE (*expr_p)))
- return;
+ /* If this is a variable sized type, we must remember the size. */
+ maybe_with_size_expr (expr_p);
/* Gimplify the constructor element to something appropriate for the rhs
of a MODIFY_EXPR. Given that we know the lhs is an aggregate, we know
-- { dg-do run }
procedure self_aggregate_with_pointer is
type Arr is array (Natural range <>) of Integer;
type Rec (N : Natural) is record
A : Arr (1..N);
end record;
type Acc_Rec is access all Rec;
type SRec is record
A : Acc_Rec;
I1, I2, I3, I4, I5, I6, I7: Integer;
end record;
R : aliased Rec (1);
S : Srec := (A => R'Access, others => 0);
begin
S := (A => S.A, others => 0);
if S.A /= R'Access then
raise Program_Error;
end if;
end;