C9202001: Incorrect Code Generated with /Oel or /Ox

ID Number: Q81357

6.00 6.00a 6.00ax | 6.00 6.00a

MS-DOS | OS/2

buglist6.00 buglist6.00a buglist6.00ax fixlist7.00

Summary:

PROBLEM ID: C9202001

SYMPTOMS

When using the Microsoft C Compiler versions 6.0, 6.0a, and 6.0ax,

incorrect code is generated if the /Oel or /Ox option is used and

the loop being optimized is nested.

In the sample code below, the following warning is generated

because of bad code generation:

warning C4200: local variable 'j' used without having been

initialized.

CAUSE

Incorrect code may be generated when trying to optimize a nested

loop using both loop optimizations and global register allocation

(/Oel).

RESOLUTION

Valid ways to work around this problem include:

1. Compile with optimization that does not include both loop

optimization (/Ol) and global register allocation (/Oe).

-or-

2. Use the optimize pragma to disable global register allocation or

the loop_opt pragma to disable loop optimization.

-or-

3. Compile with the /qc (quick compile) option.

STATUS

Microsoft has confirmed this to be a problem in Microsoft

C versions 6.0, 6.0a, and 6.0ax. This problem was corrected in C/C++

version 7.0.

More Information

The sample code below when compiled with /Ox or /Oel incorrectly

optimizes out the initialization of the local variable j. Thus,

the following warning is generated:

warning C4200: local variable 'j' used without having been

initialized

If the local variable j is initialized, the above warning is not

generated. However, the code generated is still incorrect.

An example of the workaround number 2 above is shown in the sample

code below. Uncomment the lines with #pragma directives to eliminate

the error. The advantage to the second solution is that the "e"

optimization will be used for any other functions in the source file.

Sample Code

-----------

/* Compile options needed: /c ( /Ox or /Oel)

*/

struct DATA {

int array [5][5];

int m[5];

};

//#pragma optimize ("e",off)

void test ( struct DATA *ptr)

{

int k, j , i, temptot = 0;

for ( k = 0; k <= 4; k++)

for (j = 0; j <= 4; j++)

for(i = 0; i <= j ; i++ ){

if ( ptr->array[ptr->m[j]+i] [k] ){

temptot = temptot + 1;

}

}

}

//#pragma optimize ("e",on)

Additional reference words: 6.00 6.00a 6.00ax