Microsoft KB Archive/236119

= FIX: Optimization Causes Code Generation Error in a Conditional Statement =

Article ID: 236119

Article Last Modified on 12/1/2003

-

APPLIES TO


 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q236119



SYMPTOMS
The optimizer may generate incorrect code for an if statement that contains identical expressions in both conditional statements.



CAUSE
The optimizer makes a mistake when trying to move common sub-expressions to a single location.



RESOLUTION
There are three potential ways to work around this bug:


 * 1) Turn off global optimization for that function.
 * 2) Make the function inline.
 * 3) Move the common expression before the if block.



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

This bug was corrected in Visual Studio 6 Service Pack 4.



MORE INFORMATION
The following code demonstrates the problem and resolutions 1 and 3.

// compiler options: cl /Og


 * 1) include

static long llll = 0;

double dTry1(double d) { if (d < 0.0) { llll++; return(0.0); } else { llll++; return(100.0); } }

double dTry2(double d) {
 * 1) pragma optimize("g",off)

if (d < 0.0) { llll++; return(0.0); } else { llll++; return(100.0); } }
 * 1) pragma optimize("",on)

double dTry3(double d) {

llll++; if (d < 0.0) return(0.0); else return(100.0); }

void main{ using namespace std; cout << "Result with Optimizer: " << dTry1(100.0) <<endl; cout << "Result without Optimizer: " << dTry2(100.0) <<endl; cout << "Result with moved sub-expression: " << dTry3(100.0) <<endl;

}

Keywords: kbbug kbfix kbnoupdate kbcodegen KB236119

-

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

© Microsoft Corporation. All rights reserved.