Microsoft KB Archive/250887

= BUG: Bad Code Is Generated by Global Optimizations When Bitwise Shift Operators Are Used =

Article ID: 250887

Article Last Modified on 11/18/2003

-

APPLIES TO


 * Microsoft Visual C++, 32-bit Enterprise Edition 6.0 SP3
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++, 32-bit Learning Edition 6.0 SP3

-



This article was previously published under Q250887



SYMPTOMS
For builds that use global optimizations (/Og), an if statement may never be executed when the bitwise shift operator, >> or <<, is called after the if statement.

NOTE: Both the "maximize speed" (/O2) and "minimize size" (/O1) optimizations are composite optimization switches that include /Og.



CAUSE
The optimizer has incorrectly removed a CMP instruction.



RESOLUTION
To resolve this problem, disable the global optimizations.

Global optimizations can be turned off on a function-by-function basis by using the #pragma optimize("g",off) directive.

Global optimizations can also be turned off by adding /Og- to a source file or to a project's settings.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce Behavior
//test.cpp // Compiler options are cl /GX /Og test.cpp.


 * 1) include 

//Uncomment the following line to turn off global optimizations: //#pragma optimize("g",off) void ShiftValues (const char * pEncoded) {   size_t nCount = 0; size_t nBits = 0; size_t x = 0; while (pEncoded[nCount] != '=') {       nBits += 6; printf("added 6, nBits now %d\n", nBits); if (nBits >= 8) {           printf("%x\n", (0 << (nBits - 8))); nBits -= 8; printf("subtracted 8, nBits now %d\n", nBits); }       nCount++; } } // Uncomment the following line to turn global optimizations back on: //#pragma optimize("g",on)

void main {   ShiftValues ("AA="); }

Correct output with global optimizations turn off: added 6, nBits now 6 added 6, nBits now 12 0 subtracted 8, nBits now 4

Incorrect output with global optimizations turn on: added 6, nBits now 6 0 subtracted 8, nBits now -2 added 6, nBits now 4 0 subtracted 8, nBits now -4

Keywords: kbbug kbcodegen kbcompiler KB250887

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.