Search

Code optimizer fails to detect sequential writes to adjacent parts of the same array by Dmitry Me

Closed
as Fixed Help for as Fixed

1
0
Sign in
to vote
Type: Bug
ID: 692745
Opened: 10/3/2011 6:00:27 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
0
User(s) can reproduce this bug
This behavior is observed in cl.exe 16.00.40219.1

I have this code:

int _tmain(int /*argc*/, _TCHAR* /*argv*/[])
{
    char buffer[4];
    memset( buffer, 0, sizeof( buffer ) );
    memset( buffer, 1, sizeof( buffer ) );
    memset( buffer, 1, sizeof( buffer ) / 2 );
    memset( buffer + sizeof( buffer ) / 2, 1, sizeof( buffer ) / 2 );
    buffer[0] && rand();
    return 0;
}

which I compile with /O2 and get the following disassembly:

5: int _tmain(int /*argc*/, _TCHAR* /*argv*/[])
     6: {
00401000 push        ebp
00401001 mov         ebp,esp
00401003 push        ecx
00401004 push        edi
     7:     char buffer[4];
     8:     memset( buffer, 0, sizeof( buffer ) );
     9:     memset( buffer, 1, sizeof( buffer ) );
00401005 mov         eax,1010101h
0040100A lea         edi,[buffer]
0040100D stos        dword ptr es:[edi]
    10:     memset( buffer, 1, sizeof( buffer ) / 2 );
0040100E lea         edi,[buffer]
00401011 stos        word ptr es:[edi]
    11:     memset( buffer + sizeof( buffer ) / 2, 1, sizeof( buffer ) / 2 );
00401013 lea         edi,[ebp-2]
00401016 stos        word ptr es:[edi]
    12:     buffer[0] && rand();
00401018 cmp         byte ptr [buffer],0
0040101C pop         edi
0040101D je         wmain+25h (401025h)
0040101F call        dword ptr [__imp__rand (4020A0h)]
    13:     return 0;
00401025 xor         eax,eax
    14: }
00401027 leave
00401028 ret

The compiler detected that the first memset() is seperceded by the second memset() and successfully eliminated the first memset(). Yet it failed to detect that the third and the fourth memset()s supercede the second one. The compiler should have eliminated the first and the second memset()s and merge the third and the fourth one.
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling version

Visual Studio 2010 SP1

Steps to reproduce

Compile the code above, then inspect the disassembly.

Product Language

English

Operating System

Windows XP

Operating System Language

English

Actual results

Code optimizer fails to recognize subsequent writes to adjacent sections of the same array.

Expected results

Code optimizer should recognize subsequent writes to adjacent sections of the same array.
File Attachments
0 attachments
Sign in to post a comment.
Posted by xor88 on 8/11/2012 at 3:19 PM
And did you find yourself sympathetic?
Posted by Dmitry Me on 12/20/2011 at 11:49 PM
Damn it, I'm talking to myself already.
Posted by Dmitry Me on 12/20/2011 at 5:52 AM
I haven't tried anything newer than cl.exe 16.00.40219.1 so far, so it is not possible at the moment.
Posted by Dmitry Me on 10/4/2011 at 11:16 PM
Thank you. Could you please present the disassembly from the newer version?
Posted by Microsoft on 10/4/2011 at 1:49 PM
Hi,

Thankyou for this bug report. As you point out, the optimizer "understands" the semantics of the memset intrinsic enough to deduce that the second call wholly supersedes the first. Figuring out the next step - that the subsequent two memset calls together span the entire buffer - is quite tricky.

However, I just built this example on an internal Dev11 build: the newest version of the optimizer has more smarts and goes on beyond the first level of optimization.

So I will close this bug as fixed (in a future release).

Thanks,

Jim
Posted by MS-Moderator07 [Feedback Moderator] on 10/3/2011 at 7:46 PM
Thanks for your feedback.

We are rerouting 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 MS-Moderator01 on 10/3/2011 at 6:40 AM
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.