This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix aliasing miscompilation
- To: gcc-patches at gcc dot gnu dot org
- Subject: [PATCH] Fix aliasing miscompilation
- From: Jakub Jelinek <jakub at redhat dot com>
- Date: Tue, 24 Oct 2000 13:44:42 +0200
- Reply-To: Jakub Jelinek <jakub at redhat dot com>
Hi!
The following testcase is miscompiled with -O2 on i386. The reason is that
when the __restrict keyword is applied to the pointer to incomplete struct a,
get_alias_set sets up alias for struct a as whole but
record_component_aliases is never called on that type once it is layout out
fully (the testcase does not fail even without this patch if baz prototype
is moved after struct a full declaration or if the __restrict keyword is
removed from baz prototype).
The patch below seems to fix it. I'm not testing flag_strict_aliasing
because then the alias set is either 0 or -1.
Ok to commit?
2000-10-24 Jakub Jelinek <jakub@redhat.com>
* stor-layout.c (layout_type): Call record_component_aliases if
TYPE_ALIAS_SET was already set on the incomplete type.
* gcc.c-torture/execute/20001024-1.c: New test.
--- gcc/testsuite/gcc.c-torture/execute/20001024-1.c.jj Tue Oct 24 13:32:08 2000
+++ gcc/testsuite/gcc.c-torture/execute/20001024-1.c Tue Oct 24 10:16:57 2000
@@ -0,0 +1,34 @@
+struct a;
+
+extern int baz (struct a *__restrict x);
+
+struct a {
+ long v;
+ long w;
+};
+
+struct b {
+ struct a c;
+ struct a d;
+};
+
+int bar (int x, const struct b *__restrict y, struct b *__restrict z)
+{
+ if (y->c.v || y->c.w != 250000 || y->d.v || y->d.w != 250000)
+ abort();
+}
+
+void foo(void)
+{
+ struct b x;
+ x.c.v = 0;
+ x.c.w = 250000;
+ x.d = x.c;
+ bar(0, &x, ((void *)0));
+}
+
+int main()
+{
+ foo();
+ exit(0);
+}
--- gcc/stor-layout.c.jj Mon Oct 23 15:24:47 2000
+++ gcc/stor-layout.c Tue Oct 24 13:29:49 2000
@@ -1507,6 +1507,13 @@ layout_type (type)
record it so set_sizetype can fix it up. */
if (! sizetype_set)
early_type_list = tree_cons (NULL_TREE, type, early_type_list);
+
+ /* If an alias set has been set for this aggregate when it was incomplete,
+ we need to record alias sets for the fields now. */
+ if (AGGREGATE_TYPE_P (type)
+ && TYPE_ALIAS_SET_KNOWN_P (type)
+ && TYPE_ALIAS_SET (type))
+ record_component_aliases (type);
}
/* Create and return a type for signed integers of PRECISION bits. */
Jakub