BUG: Bad Code Generation with Global Optimization (254226)



The information in this article applies to:

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

This article was previously published under Q254226

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 that this is a bug in the Microsoft products that are 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 <stdio.h>

//#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.

Modification Type:MinorLast Reviewed:7/5/2005
Keywords:kbBug kbCodeGen kbCompiler kbpending KB254226