DOCUMENT:Q254226 03-MAY-2001 [visualc] TITLE :BUG: Bad Code Generation with Global Optimization PRODUCT :Microsoft C Compiler PROD/VER:winnt:6.0 OPER/SYS: KEYWORDS:kbCodeGen kbCompiler kbVC600bug ====================================================================== ------------------------------------------------------------------------------- The information in this article applies to: - The C/C++ Compiler (CL.EXE), included with: - Microsoft Visual C++, 32-bit Enterprise Edition, version 6.0 - Microsoft Visual C++, 32-bit Professional Edition, version 6.0 - Microsoft Visual C++, 32-bit Learning Edition, version 6.0 ------------------------------------------------------------------------------- SYMPTOMS ======== The global optimizer may generate incorrect code if two if statements have identical conditional clauses that create side effects. Please refer to the sample code in the "More Information" section for details. RESOLUTION ========== Following are two possible ways to work around this problem: 1. Use the #pragma optimize("g",off) method to turn off global optimization at the function level. By making the function inline and compiling with the /Ob1 or /Ob2 compiler switches also alleviates this problem. 2. Modify the code to avoid the construct in workaround number one. For example, when you change the second if statement to else if this change generates the correct code. STATUS ====== Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. MORE INFORMATION ================ Steps to Reproduce Behavior --------------------------- The following sample code demonstrates the bug: //bug.cpp /* compiler option: cl /Og */ #include //#pragma optimize("g",off) // Uncomment this line to workaround 1. // inline void funcopt(int i) // Uncomment this line to workaround 2 and comment the function funcopt() definition. void funcopt(int i) { if(i--==0) { printf("Zero\n"); } if(i--==0) { printf("One\n"); } return; } int main() { funcopt(1); return 0; } The expected output is 1. Additional query words: ====================================================================== Keywords : kbCodeGen kbCompiler kbVC600bug Technology : kbVCsearch kbAudDeveloper kbCVCComp Version : winnt:6.0 Issue type : kbbug Solution Type : kbpending ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 2001.