Home Dashboard Directory Help
Search

Incorrect shift left/right optimization by Tom Lyons


Status: 

Closed
 as Fixed Help for as Fixed


2
0
Sign in
to vote
Type: Bug
ID: 634455
Opened: 1/3/2011 1:53:33 PM
Access Restriction: Public
0
Workaround(s)
view
1
User(s) can reproduce this bug

Description

The following code, when executed as "bug.exe 1 2 3 4", prints "1" in VC10 with "-O1". Without optimization (and in VC7) it prints "5".

#include <stdio.h>

int main(int argc, char **argv)
{
printf("%d\n", ((argc << 29) >> 31) & 5);
}

This is the incorrect generated code:

tag # dumpbin /DISASM bug.obj
Microsoft (R) COFF/PE Dumper Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.


Dump of file bug.obj

File Type: COFF OBJECT

_main:
00000000: 8B 44 24 04        mov         eax,dword ptr [esp+4]
00000004: C1 F8 02         sar         eax,2
00000007: 83 E0 05         and         eax,5
0000000A: 50                 push        eax
0000000B: 68 00 00 00 00     push        offset ??_C@_03PMGGPEJJ@?$CFd?6?$AA@
00000010: E8 00 00 00 00     call        _printf
00000015: 83 C4 08         add         esp,8
00000018: 33 C0             xor         eax,eax
0000001A: C3                 ret

Summary

         10 .debug$F
         68 .debug$S
         2F .drectve
         4 .rdata
         1B .text
Details
Sign in to post a comment.
Posted by Microsoft on 1/7/2011 at 10:53 AM
Thanks for reporting this issue that you've found. We have a fix for this problem. Unfortunately, we're unable to include a fix for this in the upcoming service pack, but a fix will be included in a future release of Visual Studio.

In the meantime you can work around this optimization issue by disabling the optimizer for the function that is exhibiting the incorrect behavior. You can do this by putting the code in a seperate file and compiling without optimizations or use #pragma optimization. See this page: http://msdn.microsoft.com/en-us/library/chh3fb0k(v=VS.100).aspx for more informaiton on using #pragma optimize.

thanks,
VC++ Code Generation and Optimization Team
Posted by Microsoft on 1/7/2011 at 12:27 AM
Thanks for your feedback.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Tom Lyons on 1/5/2011 at 7:18 AM
Our system uses makefiles which run the compiler from the command line, but I just followed the same steps as
you outline to do it from the UI and the output is '1', as verified by the previous commenter. It is necessary that optimization be on in the project. I am using

Microsoft Visual Studio 2010
Version 10.0.30319.1 RTMRel

You can see from the included asm dump in the original report that the code will produce 1, and not 5. Here is the code without optimization, that actually does both shl/sar. These dumps were from .obj files using the /c compiler flag.

tag # dumpbin /DISASM bug.obj
Microsoft (R) COFF/PE Dumper Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.


Dump of file bug.obj

File Type: COFF OBJECT

_main:
00000000: 55                 push        ebp
00000001: 8B EC             mov         ebp,esp
00000003: 8B 45 08         mov         eax,dword ptr [ebp+8]
00000006: C1 E0 1D         shl         eax,1Dh
00000009: C1 F8 1F         sar         eax,1Fh
0000000C: 83 E0 05         and         eax,5
0000000F: 50                 push        eax
00000010: 68 00 00 00 00     push        offset $SG2641
00000015: E8 00 00 00 00     call        _printf
0000001A: 83 C4 08         add         esp,8
0000001D: 33 C0             xor         eax,eax
0000001F: 5D                 pop         ebp
00000020: C3                 ret
Posted by Microsoft on 1/4/2011 at 1:51 AM
Thank you for reporting this issue. Unfortunately, we are unable to reproduce the issue with the steps you provided. We tried to repro with the following steps:

1. Create a CPP empty project and add a source file to it
2. Paste the sample code to the source file
3. Set Property Pages->Configuration properties->Debugging->Command Arguments as "1 2 3 4"
4. Build and Run, and it prints 5

Could you please provide us with the following information?

1. Help to point out the steps misunderstood or omitted.
2. Some screenshots to show this issue, and a vedio can be much better.

It would be greatly appreciated if you could provide us this information as quickly as possible.

Thank you,
Posted by Avery Lee on 1/3/2011 at 8:49 PM
The code isn't portable, but it's perfectly valid for an implementation that defines signed right shifts to sign extend as VC++ does. It is a bug, and apparently one in a new shift/and optimization.

I'm also seeing this bug with VS2010 SP1 beta, btw.
Posted by Mike Danes on 1/3/2011 at 2:43 PM
Fun one. According to the C++ standard right shifting a negative value is implementation dependent so this isn't a bug. But according to the VC++ docs (http://msdn.microsoft.com/en-us/library/336xbhcz.aspx) right shifting should extend the sign.
Posted by Microsoft on 1/3/2011 at 2:23 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.