Home Dashboard Directory Help
Search

Wrong comparison instruction selected for loop preamble by A. Kliger


Status: 

Closed
 as Fixed Help for as Fixed


1
1
Sign in
to vote
Type: Bug
ID: 644615
Opened: 2/16/2011 11:43:54 AM
Access Restriction: Public
1
Workaround(s)
view
0
User(s) can reproduce this bug

Description

When generating code for a for-loop preamble after an if-statement, the generated code sometimes avoids oututting a CMP instruction and just outputs a conditional jump instruction reusing the previous values of the flags register.

It does this in /O2 and /O1 when the if-statement and the for-loop end up comparing the same pairs of registers.

However sometimes it gets the condition code wrong. In the attached example, a JGE instruction is selected instead of a JL.

Compiling with /Od produces the expected result. Incorrect behavior is observed in generating x86 and x64 code. Incorrect behavior also present in Visual Studio 2008 SP1.
Details
Sign in to post a comment.
Posted by Microsoft on 5/9/2011 at 4:03 PM
Thank you for submitting the bug. Yes, you are correct; the issue fix was prepared for an upcoming release of the compiler.

Thank you again,
Natalia Glagoleva,
Microsoft
Posted by A. Kliger on 4/21/2011 at 10:46 AM
Thank you for taking the time to work on this issue.

I just want to confirm that Microsoft Visual Studio 2010 Version 10.0.40219.1 SP1Rel continues to generate incorrect code.

I assume "Resolved as Fixed" means that it is fixed in a not-yet-shipping version.

Thanks again,
A. Kliger
Posted by Microsoft on 2/17/2011 at 12:09 AM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for review. We will contact you if we require any additional information.
Posted by Microsoft on 2/16/2011 at 12:00 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.
Posted by A. Kliger on 2/16/2011 at 2:11 PM
There are two classes of workarounds: (1) ones that rely on introducing some code that invalidates the EFLAGS register between the if-statement and the for-loop forcing a second CMP to be generated or (2) ones that resolve that somehow avoid triggering the bug.

In the first class, are workarounds that intoduce a function call (or perhaps an arithmetic expression) after the if-statement:
    if (oldVal >= newVal) {
     ...
    }
    aFunctionCall();
    for (int i = static_cast<int>(oldVal); i < static_cast<int>(newVal); ++i) {
     ...
    }

The second class of workaround is to change the loop to use an unsigned int as the bound:

    for (unsigned int i = oldVal; i < newVal; ++i) {
     ... /* static_cast<int>(i) as needed in the body of the loop. */
    }
File Name Submitted By Submitted On File Size  
twoloopsbug.zip 2/16/2011 825 bytes