From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dan Nicolaescu To: gcc-gnats@gcc.gnu.org Subject: c/2480: aliasing problem with global structures Date: Tue, 03 Apr 2001 16:56:00 -0000 Message-id: <20010403234820.13970.qmail@sourceware.cygnus.com> X-SW-Source: 2001-04/msg00063.html List-Id: >Number: 2480 >Category: c >Synopsis: aliasing problem with global structures >Confidential: no >Severity: serious >Priority: medium >Responsible: unassigned >State: open >Class: pessimizes-code >Submitter-Id: net >Arrival-Date: Tue Apr 03 16:55:59 PDT 2001 >Closed-Date: >Last-Modified: >Originator: Dan Nicolaescu >Release: gcc version 3.1 20010402 (experimental) >Organization: >Environment: sun-sparc-solaris2.7 >Description: The following code: struct example { char a; int b; char c; } *ex1; void bar (void) { ex1->a = 1; ex1->b = 2; ex1->c = 3; } void foo (struct example *ex2) { ex2->a = 1; ex2->b = 2; ex2->c = 3; } When compiled with -O2 -fstrict-aliasing -S on sun-sparc-solaris2.7 with a GCC mainline snapshot from 2000-04-02 (but the same problem occurs on the GCC-3.0 branch and 2.95.2) bar: !#PROLOGUE# 0 !#PROLOGUE# 1 sethi %hi(ex1), %o2 ld [%o2+%lo(ex1)], %o1 mov 1, %o0 stb %o0, [%o1] ld [%o2+%lo(ex1)], %o3 ^^^^^ after the store ex1 is reloaded. true_dependence returns true for these last 2 instructions, it seems that the ex1->a is put in the alias set 0. That is a mistake, but I couldn't find where that is done.... It looks like GCC treats ex1->a as a char*, but that is incorrect, a store to ex1->a cannot alias ex1 mov 3, %o0 mov 2, %o1 st %o0, [%o3+8] ^^^ After this store ex1 is not reloaded, but in this case the struct member is an "int" retl st %o1, [%o3+4] foo: !#PROLOGUE# 0 !#PROLOGUE# 1 mov 3, %o1 st %o1, [%o0+8] mov 1, %o2 mov 2, %o1 stb %o2, [%o0] retl st %o1, [%o0+4] nothing like that happens here, when the pointer to the structure is passed as a parameter. This is an important pessimization. GCC itself contains a lot of global pointers to structures... >How-To-Repeat: Compile the code in the description with -O2 -S -fstrict-aliasing and look at the resulting assembly >Fix: >Release-Note: >Audit-Trail: >Unformatted: