BUG: Bad Code Is Generated by Global Optimizations When Bitwise Shift Operators Are Used (250887)



The information in this article applies to:

  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0 SP3
  • Microsoft Visual C++, 32-bit Professional Edition 6.0 SP3
  • 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.

MORE INFORMATION

Steps to Reproduce Behavior

//test.cpp
// Compiler options are cl /GX /Og test.cpp.

#include <stdio.h>

//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:<BR/>
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
				

Modification Type:MajorLast Reviewed:11/18/2003
Keywords:kbBug kbCodeGen kbCompiler KB250887