In the following C code, the order of loads and stores is messed up, leading to
wrong code:

extern void abort (void) __attribute__((noreturn));

union setconflict
{
  short a[20];
  int b[10];
};

int
main ()
{
  int sum = 0;
  {
    union setconflict a;
    short *c;
    c = a.a;
    asm ("": "=r" (c):"0" (c));
    *c = 0;
    asm ("": "=r" (c):"0" (c));
    sum += *c;
  }
  {
    union setconflict a;
    int *c;
    c = a.b;
    asm ("": "=r" (c):"0" (c));
    *c = 1;
    asm ("": "=r" (c):"0" (c));
    sum += *c;
  }

  printf ("%d\n",sum);
  if (sum != 1)
    abort();
  return 0;
}



        .file   "t.c"
# GNU C version 4.2.0 20060101 (experimental) (x86_64-unknown-linux-gnu)
#       compiled by GNU C version 4.0.2 20050901 (prerelease) (SUSE Linux).
# GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
# options passed:  -iprefix -isystem -m32 -march=pentiumpro -auxbase -O2
# -fdump-tree-vars -fomit-frame-pointer -fverbose-asm
# options enabled:  -falign-loops -fargument-alias -fbranch-count-reg
# -fcaller-saves -fcommon -fcprop-registers -fcrossjumping
# -fcse-follow-jumps -fcse-skip-blocks -fdefer-pop
# -fdelete-null-pointer-checks -fearly-inlining
# -feliminate-unused-debug-types -fexpensive-optimizations -ffunction-cse
# -fgcse -fgcse-lm -fguess-branch-probability -fident -fif-conversion
# -fif-conversion2 -finline-functions-called-once -fipa-pure-const
# -fipa-reference -fipa-type-escape -fivopts -fkeep-static-consts
# -fleading-underscore -floop-optimize -floop-optimize2 -fmath-errno
# -fmerge-constants -fomit-frame-pointer -foptimize-register-move
# -foptimize-sibling-calls -fpcc-struct-return -fpeephole -fpeephole2
# -fregmove -freorder-blocks -freorder-functions -frerun-cse-after-loop
# -frerun-loop-opt -fsched-interblock -fsched-spec
# -fsched-stalled-insns-dep -fschedule-insns2 -fshow-column
# -fsplit-ivs-in-unroller -fstrength-reduce -fstrict-aliasing
# -fthread-jumps -ftrapping-math -ftree-ccp -ftree-ch -ftree-copy-prop
# -ftree-copyrename -ftree-dce -ftree-dominator-opts -ftree-dse -ftree-fre
# -ftree-loop-im -ftree-loop-ivcanon -ftree-loop-optimize -ftree-lrs
# -ftree-pre -ftree-salias -ftree-sink -ftree-sra -ftree-store-ccp
# -ftree-store-copy-prop -ftree-ter -ftree-vect-loop-version -ftree-vrp
# -funit-at-a-time -fverbose-asm -fzero-initialized-in-bss -m32 -m80387
# -m96bit-long-double -maccumulate-outgoing-args -malign-stringops
# -mfancy-math-387 -mfp-ret-in-387 -mieee-fp -mno-red-zone -mpush-args
# -mtls-direct-seg-refs

# Compiler executable checksum: 6d90f1c30ff8027bc6976ab2dbfe2320

        .section        .rodata.str1.1,"aMS",@progbits,1
.LC0:
        .string "%d\n"
        .text
        .p2align 4,,15
.globl main
        .type   main, @function
main:
        leal    4(%esp), %ecx   #,
        andl    $-16, %esp      #,
        pushl   -4(%ecx)        #
        subl    $76, %esp       #,
        leal    28(%esp), %eax  #, tmp64
        movl    %ecx, 68(%esp)  #,
        movl    %eax, %edx      # tmp64, c
        movl    %ebx, 72(%esp)  #,
        movw    $0, (%edx)      #,* c
        movl    $1, (%eax)      #,* c
        movswl  (%edx),%ebx     #* c, sum
        movl    (%eax), %edx    #* c,
        movl    $.LC0, (%esp)   #,
        addl    %edx, %ebx      #, sum
        movl    %ebx, 4(%esp)   # sum,
        call    printf  #
        decl    %ebx    # sum
        jne     .L6     #,
        movl    68(%esp), %ecx  #,
        xorl    %eax, %eax      # <result>
        movl    72(%esp), %ebx  #,
        addl    $76, %esp       #,
        leal    -4(%ecx), %esp  #,
        ret
.L6:
        call    abort   #
        .size   main, .-main
        .ident  "GCC: (GNU) 4.2.0 20060101 (experimental)"
        .section        .note.GNU-stack,"",@progbits

See also the thread on the gcc@ mailing list starting here:
http://gcc.gnu.org/ml/gcc/2006-01/msg00008.html.


-- 
           Summary: [4.0/4.1/4.2 Regression] RTL alias analysis unprepared
                    to handle stack slot sharing
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Keywords: wrong-code, alias
          Severity: major
          Priority: P3
         Component: rtl-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: steven at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25654


Reply via email to