[Bug c/40115] New: -O2 and higher causes wrong label address calculation

sergstesh at yahoo dot com gcc-bugzilla@gcc.gnu.org
Tue May 12 11:29:00 GMT 2009


The following program:
"
cat -n main.c
     1  #include <stdio.h>
     2
     3
     4  int main()
     5    {
     6    __label__ lab3;
     7    __label__ lab4;
     8
     9    int i = 0;
    10
    11    i++;
    12    lab1: fprintf(stderr, "i=%d\n", i);
    13
    14    i++;
    15    lab2: fprintf(stderr, "i=%d\n", i);
    16
    17    i++;
    18    lab3: fprintf(stderr, "i=%d\n", i);
    19
    20    i++;
    21    lab4: fprintf(stderr, "i=%d\n", i);
    22
    23    fprintf(stderr, "&&lab1=%08x\n", (unsigned)&&lab1);
    24    fprintf(stderr, "&&lab2=%08x\n", (unsigned)&&lab2);
    25    fprintf(stderr, "&&lab3=%08x\n", (unsigned)&&lab3);
    26    fprintf(stderr, "&&lab4=%08x\n", (unsigned)&&lab4);
    27
    28    return 0;
    29    }
"

after being compiled as

/mnt/sdb8/sergei/AFSWD_debug/install/gcc-4.3.3/binsh/gcc -O1 -Wall -Wextra
main.c -o main_-O1

produces:

"
 ./main_-O1
i=1
i=2
i=3
i=4
&&lab1=08048435
&&lab2=08048452
&&lab3=0804846f
&&lab4=0804848c
"

which is correct in a sense all the labels have different addresses.

When compiled as

/mnt/sdb8/sergei/AFSWD_debug/install/gcc-4.3.3/binsh/gcc -O2 -Wall -Wextra
main.c -c -o main_-O2.o

it produces

"
 ./main_-O2
i=1
i=2
i=3
i=4
&&lab1=08048430
&&lab2=08048430
&&lab3=08048430
&&lab4=08048430
",

which is wrong in the sense all the addresses are the same.

Please note that labels have intentionally been put against 'fprintf'
statements which are _not_ eliminated by optimization ('objdump' easily proves
this).

I have read pages ## 246 .. 248 in

http://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc.pdf

, they don't mention optimizations (or did I miss it ?).

The problem also exists with gcc-3.4.6.


-- 
           Summary: -O2 and higher causes wrong label address calculation
           Product: gcc
           Version: 4.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sergstesh at yahoo dot com
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


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



More information about the Gcc-bugs mailing list