BUG: /Ol Causes Register Variables to Be Allocated on Stack

ID: Q67040

6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00 1.50

MS-DOS                 | OS/2       | WINDOWS
kbtool kbbuglist

The information in this article applies to:

SYMPTOMS

In most cases, a variable declared with the register storage-class in Microsoft C versions 6.0, 6.0a, 6.0ax, and C/C++ version 7.0 will not be allocated to a register if loop optimization (/Ol) is enabled. Instead, the compiler will place the variable on the stack.

The register storage will be allocated only if the function contains an inline assembly (_asm) block, because the inline assembly takes precedence over optimization. This is reflected by the following warning message, which is generated when /Ol is used on a function with an _asm block:

   warning C4204: in-line assembler precludes global optimizations

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here as it becomes available.

This is not a problem in the 32-bit compilers.

MORE INFORMATION

The sample program below demonstrates both of the situations described above. If the program is compiled as is with /Ol, the register declared variable "i" will not be put in a register; it will be handled exactly like "j", which is not declared with register storage-class.

If the line at the end of main() with the _asm block is uncommented, then "i" will be allocated storage in a register and the C4204 warning will be displayed if the warning level is set at 3 or 4 (/W3 or /W4). To see the difference, you can generate an assembly listing with /Fa, or you can compile and link the program with CodeView information and then view the mixed source and assembly listing in the debugger.

Sample Code

/* Compile options needed: /Ol /Fc /W3
*/ 

int func(void);

void main(void)
{
    register int i = 7;
    int          j = 9;

    while ( i < 10 ) {
       i += func();
       j += func();
    }

    /* Uncomment the following line to have i put in a register */ 
    /* _asm xor i,i */ 
}

int func(void)
{
   return (1);
}

Additional reference words: 1.00 1.50 6.00 6.00a 6.00ax 7.00 8.00 8.00c KBCategory: kbtool kbbuglist KBSubcategory: CLIss Keywords : kb16bitonly

Last Reviewed: July 18, 1997