BUG: /Og Generates Bad Code for a Compare/BranchID: Q234602
|
When using the /Og compiler switch for a compare/branch that targets another compare/branch such as the following:
if (!count--) return 0;
if (!count) return 1;
The code generated for the second statement above may be incorrectly removed by the compiler.
This is caused by a bug in C2.dll.
if (!count) return 0;
count--;
if (!count) return 1;
The correct code is generated even with /Og optimization.Name | Size | Date | Time | Version# | Platform |
---|---|---|---|---|---|
c2.dll | 721KB | 6/18/99 | 10:14 PM | 12.00.8534.0 | x86 |
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.
The sample code below can be used to reproduce the problem.
int main(int count)
{
do
{
if (count > 10) return -1;
/* start - code snippet */
if (!count--) return 0;
/* Missing instructions for the below statement with /Og */
if (!count) return 1;
/* end - code snippet */
} while (1);
}
When compiled with /Og, the following incorrect assembly instructions are generated:
; 7 : /* start - code snippet */
; 8 :
; 9 : if (!count--) return 0;
0000b 8b c8 mov ecx, eax
0000d 48 dec eax
0000e 85 c9 test ecx, ecx
00010 74 0a je SHORT $L44
; 3 : do
; 4 : {
; 5 : if (count > 10) return -1;
00012 83 f8 0a cmp eax, 10 ; 0000000aH
00015 7e f4 jle SHORT $L33
$L43:
00017 83 c8 ff or eax, -1
; 10 : /* Missing instructions for the below statement with /Og */
; 11 : if (!count) return 1;
; 12 :
; 13 : /* end - code snippet */<BR/>
The assembly instruction for line eleven above is incorrectly removed in the above optimized code.
; 7 : /* start - code snippet */
; 8 :
; 9 : if (!count--) return 0;
0000e 8b 45 08 mov eax, DWORD PTR _count$[ebp]
00011 8b 4d 08 mov ecx, DWORD PTR _count$[ebp]
00014 83 e9 01 sub ecx, 1
00017 89 4d 08 mov DWORD PTR _count$[ebp], ecx
0001a 85 c0 test eax, eax
0001c 75 04 jne SHORT $L37
0001e 33 c0 xor eax, eax
00020 eb 16 jmp SHORT $L35
$L37:
; 10 : /* Missing instructions for the below statement with /Og */
; 11 : if (!count) return 1;
00022 83 7d 08 00 cmp DWORD PTR _count$[ebp], 0
00026 75 07 jne SHORT $L38
00028 b8 01 00 00 00 mov eax, 1
0002d eb 09 jmp SHORT $L35
$L38:
; 12 :
; 13 : /* end - code snippet */
The assembly instructions for line eleven above is not removed, which is the correct behavior.
; 7 : /* start - code snippet */
; 8 :
; 9 : if (!count) return 0;
0000b 85 c0 test eax, eax
0000d 74 0d je SHORT $L44
; 10 : count--; /* workaround */
0000f 48 dec eax
; 11 : /* No missing instructions for the below statement with /Og */
; 12 : if (!count) return 1;
00010 74 0e je SHORT $L45
; 3 : do
; 4 : {
; 5 : if (count > 10) return -1;
00012 83 f8 0a cmp eax, 10 ; 0000000aH
00015 7e f4 jle SHORT $L33
$L43:
00017 83 c8 ff or eax, -1
; 13 :
; 14 : /* end - code snippet */
; 7 : /* start - code snippet */
; 8 :
; 9 : if (!count--) return 0;
0000b 8b c8 mov ecx, eax
0000d 48 dec eax
0000e 85 c9 test ecx, ecx
00010 74 0e je SHORT $L44
; 10 : /* No missing instructions for the below statement with /Og */
; 11 : if (!count) return 1;
00012 85 c0 test eax, eax
00014 74 0e je SHORT $L45
; 3 : do
; 4 : {
; 5 : if (count > 10) return -1;
00016 83 f8 0a cmp eax, 10 ; 0000000aH
00019 7e f0 jle SHORT $L33
$L43:
0001b 83 c8 ff or eax, -1
; 12 :
; 13 : /* end - code snippet */
Additional query words: /Og assembly code removed
Keywords : kbCodeGen kbCompiler kbVC600QFE kbDSupport kbGrpVCCompiler
Version : winnt:6.0
Platform : winnt
Issue type : kbbug
Last Reviewed: July 13, 1999