Date: Mon, 21 Oct 1996 11:24:34 GMT From: kvhk AT ivs4 DOT barco DOT com (Koen Van Herck) Message-Id: <9610211124.AA19264@ivs4.> To: bug-gcc AT prep DOT ai DOT mit DOT edu Subject: Optimization bug in gcc2.7.2.1 ? Cc: djgpp AT delorie DOT com Hello, I think I have found a bug in gcc 2.7.2.1. The simple program generates incorrect output when it is compiled with -O2. I have reported this bug for gcc 2.7.2, but I was told it would be fixed in gcc 2.7.2.1. As I have just tested, this seems not true. I have used gcc 2.7.2.1 which comes with djgpp v2.1. I have found that the bug did not appear with gcc 2.6.3 (DJGPP V1.?) I have included the assembler listings. It seems that the error is caused by the assignment at 0x0070 in bug.lis. When comparing the two listings (with -O1 and -O2), it seems that the -O1 version is more efficient than the -O2. Maybe this is also a bug. -O2 should be better (or at least as good) as -O1, shouldn't it ? But since I'm not a compiler or assembler specialist, I could be completely wrong in this. I would be very glad hearing your opinion, though. If you need more information, please ask. Best regards, Koen Van Herck. System description ------------------ PC running MS-DOS 6.22 & 4dos 5.0 Intel 486DX2-66 DJGPP V2.1, which includes GCC 2.7.2.1 Test program - bug.c -------------------- #include void main(void) { static int i, j, k, l, m; i = 12; j = 34; k = 56; l = 78; printf("j=%d\n", j); m = i * j % 179 * k % 179; i = j; j = k; printf("&i=%x\n", (void *)&i); k = i; printf("k=%d\n", k); } Description of the bug ---------------------- > gcc -v Reading specs from d:/djgpp/lib\specs gcc version 2.7.2.1 > gcc -O2 -g -Wa,-alhdn -o bug bug.c > bug.lis > gcc -O1 -g -Wa,-alhdn -o nobug bug.c > nobug.lis > nobug j=34 &i=8810 k=34 This is the expected output. (k==j==34) > bug j=34 &i=8810 k=56 This is an error. (k==56 != j==34) Here follow the 2 generated assembler listings: Assembler output nobug.lis -------------------------- 1 .file "bug.c" 2 gcc2_compiled.: 3 ___gnu_compiled_c: 4 .text 18 .lcomm _i.2,4 19 .lcomm _j.3,4 20 .lcomm _k.4,4 21 .lcomm _l.5,4 22 .lcomm _m.6,4 23 LC0: 24 0000 6A3D2564 .ascii "j=%d\12\0" 24 0A00 25 LC1: 26 0006 26693D25 .ascii "&i=%x\12\0" 26 780A00 27 LC2: 28 000d 6B3D2564 .ascii "k=%d\12\0" 28 0A00 29 0013 90 .align 2 31 .globl _main 32 _main: 34 0014 55 pushl %ebp 35 0015 89E5 movl %esp,%ebp 36 0017 E8E4FFFF call ___main 36 FF 1:bug.c **** #include 2:bug.c **** 3:bug.c **** void main(void) 4:bug.c **** { 5:bug.c **** static int i, j, k, l, m; 6:bug.c **** 7:bug.c **** i = 12; 44 001c C705B000 movl $12,_i.2 44 00000C00 44 0000 8:bug.c **** j = 34; 46 0026 C705B400 movl $34,_j.3 46 00002200 46 0000 9:bug.c **** k = 56; 48 0030 C705B800 movl $56,_k.4 48 00003800 48 0000 10:bug.c **** l = 78; 50 003a C705BC00 movl $78,_l.5 50 00004E00 50 0000 11:bug.c **** 12:bug.c **** printf("j=%d\n", j); 52 0044 6A22 pushl $34 53 0046 68000000 pushl $LC0 53 00 54 004b E8B0FFFF call _printf 54 FF 13:bug.c **** 14:bug.c **** m = i * j % 179 * k % 179; 56 0050 A1B00000 movl _i.2,%eax 56 00 57 0055 0FAF05B4 imull _j.3,%eax 57 000000 58 005c B9B30000 movl $179,%ecx 58 00 59 0061 99 cltd 60 0062 F7F9 idivl %ecx 61 0064 89D0 movl %edx,%eax 62 0066 0FAF05B8 imull _k.4,%eax 62 000000 63 006d 99 cltd 64 006e F7F9 idivl %ecx 65 0070 8915C000 movl %edx,_m.6 65 0000 15:bug.c **** i = j; 67 0076 A1B40000 movl _j.3,%eax 67 00 68 007b A3B00000 movl %eax,_i.2 68 00 16:bug.c **** j = k; 70 0080 A1B80000 movl _k.4,%eax 70 00 71 0085 A3B40000 movl %eax,_j.3 71 00 17:bug.c **** 18:bug.c **** printf("&i=%x\n", (void *)&i); 73 008a 68B00000 pushl $_i.2 73 00 74 008f 68060000 pushl $LC1 74 00 75 0094 E867FFFF call _printf 75 FF 19:bug.c **** k = i; 77 0099 A1B00000 movl _i.2,%eax 77 00 78 009e A3B80000 movl %eax,_k.4 78 00 20:bug.c **** 21:bug.c **** printf("k=%d\n", k); 80 00a3 50 pushl %eax 81 00a4 680D0000 pushl $LC2 81 00 82 00a9 E852FFFF call _printf 82 FF 22:bug.c **** } 84 00ae C9 leave 85 00af C3 ret Assembler output bug.lis ------------------------ 1 .file "bug.c" 2 gcc2_compiled.: 3 ___gnu_compiled_c: 4 .text 18 .lcomm _i.2,4 19 .lcomm _j.3,4 20 .lcomm _k.4,4 21 .lcomm _l.5,4 22 .lcomm _m.6,4 23 LC0: 24 0000 6A3D2564 .ascii "j=%d\12\0" 24 0A00 25 LC1: 26 0006 26693D25 .ascii "&i=%x\12\0" 26 780A00 27 LC2: 28 000d 6B3D2564 .ascii "k=%d\12\0" 28 0A00 29 0013 90 .align 2 31 .globl _main 32 _main: 34 0014 55 pushl %ebp 35 0015 89E5 movl %esp,%ebp 36 0017 56 pushl %esi 37 0018 53 pushl %ebx 38 0019 E8E2FFFF call ___main 38 FF 1:bug.c **** #include 2:bug.c **** 3:bug.c **** void main(void) 4:bug.c **** { 5:bug.c **** static int i, j, k, l, m; 6:bug.c **** 7:bug.c **** i = 12; 46 001e C705BC00 movl $12,_i.2 46 00000C00 46 0000 8:bug.c **** j = 34; 48 0028 C705C000 movl $34,_j.3 48 00002200 48 0000 9:bug.c **** k = 56; 50 0032 C705C400 movl $56,_k.4 50 00003800 50 0000 10:bug.c **** l = 78; 52 003c C705C800 movl $78,_l.5 52 00004E00 52 0000 11:bug.c **** 12:bug.c **** printf("j=%d\n", j); 54 0046 6A22 pushl $34 55 0048 68000000 pushl $LC0 55 00 56 004d E8AEFFFF call _printf 56 FF 13:bug.c **** 14:bug.c **** m = i * j % 179 * k % 179; 58 0052 8B0DC000 movl _j.3,%ecx 58 0000 59 0058 0FAF0DBC imull _i.2,%ecx 59 000000 60 005f 89C8 movl %ecx,%eax 61 0061 BBB30000 movl $179,%ebx 61 00 62 0066 99 cltd 63 0067 F7FB idivl %ebx 64 0069 89D1 movl %edx,%ecx 65 006b A1C40000 movl _k.4,%eax 65 00 66 0070 A3C00000 movl %eax,_j.3 66 00 67 0075 0FAFC8 imull %eax,%ecx 68 0078 89C8 movl %ecx,%eax 69 007a 99 cltd 70 007b F7FB idivl %ebx 71 007d 89D6 movl %edx,%esi 72 007f 8935CC00 movl %esi,_m.6 72 0000 15:bug.c **** i = j; 74 0085 8B1DC000 movl _j.3,%ebx 74 0000 75 008b 891DBC00 movl %ebx,_i.2 75 0000 16:bug.c **** j = k; 17:bug.c **** 18:bug.c **** printf("&i=%x\n", (void *)&i); 77 0091 68BC0000 pushl $_i.2 77 00 78 0096 68060000 pushl $LC1 78 00 79 009b E860FFFF call _printf 79 FF 19:bug.c **** k = i; 81 00a0 A1BC0000 movl _i.2,%eax 81 00 82 00a5 A3C40000 movl %eax,_k.4 82 00 20:bug.c **** 21:bug.c **** printf("k=%d\n", k); 84 00aa 50 pushl %eax 85 00ab 680D0000 pushl $LC2 85 00 86 00b0 E84BFFFF call _printf 86 FF 22:bug.c **** } 89 00b5 8D65F8 leal -8(%ebp),%esp 90 00b8 5B popl %ebx 91 00b9 5E popl %esi 92 00ba C9 leave 93 00bb C3 ret ---- Koen Van Herck Electronic Design Engineer E-mail: Koen DOT VanHerck AT barco DOT com BBBB AAA RRRR CCC OOO B A R C O V I S U A L S Y S T E M S B B A A R R C O O A division of Barco Projection Systems B BB A AAA R RR C OO O OO B B A A R R C O O Noordlaan 5 Tel +32 (0)56 36 85 71 BBBB A A R R CCC OOO B-8520 Kuurne Fax +32 (0)56 36 83 55