C++ VS2010: bug in generating optimized code with a loop - by varaev

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 740985 Comments
Status Closed Workarounds
Type Bug Repros 2
Opened 5/7/2012 12:44:34 PM
Access Restriction Public
Moderator Decision Sent to Engineering Team for consideration


I originally assumed this is the same bug mentioned here

however after some regression testing it turned out that this issue is new to 2010 and cannot be reproduced in 2005 or 2008, while 737176 problem can be traced back at least to 2005, so this seems to be a different, even though possible related issue.

We've recently transitioned to 2010 from 2005 and this problem quickly became a blocker for us: with over 1M codeline base we cannot practically go through all our loop code and disable optimization there with #pragma optimize(). On the other hand, disabling optimization across the product has quite severe performance repercussions, with up to 100% performance hit, since many operations in our product are highly computational intensive. Would you have any other suggestions as to how to deal with this problem?

Thanks in advance!

Ilya Varaev & Michael Yakubovsky
Sign in to post a comment.
Posted by Yang Xiaohui on 5/20/2012 at 7:28 AM
Is "will be fixed in the VS11 release" a satisfied ansower?
What if I won't upgrade?
No service pack planned?
Posted by Yang Xiaohui on 5/16/2012 at 9:11 PM
Your workaround is not acceptible, vs11 does not support Windows XP.
Posted by Microsoft on 5/16/2012 at 8:39 AM
Hi, thanks for the bug report. This is a bug in the compiler optimizer, and will be fixed in the VS11 release. If you need a workaround, your best bet is to turn off optimizations around the affected functions using:

#pragma optimize("", off)
... your funciton ...
#pragma optimize("", on)

The bug only manifests when two specific optimizations kick in. One optimization turns this code:

int x;
if (x < 0 || x > 300)


int x;
unsigned int tmp = (unsigned int) x;
if (x > 300)

The second optimization that is required for this bug to manifest changes the induction variable of loops to have fewer operations in the loop body. It turns this code:

for (int i=L; i<U; i++)
if (i*3 > 300)

into this:

for (int i=L; i<U; i+=3)
if (i > 100)

The second optimization is buggy (when it rewrites the "i*3 > 100" it loses the fact that i*3 may have been made unsigned by the first optimization). I have fixed this in the VS11 release.

Both of these optimizations are required for the bug to manifest. This is more information than you probably want to know, but it can guide you to where your existing code might be susceptible. For what it's worth, having these two optimizations kick is in extremely rare.

Eric Brumer
Microsoft Visual C++
Posted by MS-Moderator08 [Feedback Moderator] on 5/8/2012 at 12:54 AM
Thank you for submitting feedback on Visual Studio 11 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 MS-Moderator01 on 5/7/2012 at 1:43 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)