{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/2596","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/2596/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/2596/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/2596/events","html_url":"https://github.com/dotnet/coreclr/issues/2596","id":125805292,"number":2596,"title":"RyuJIT's loop cloning optimization has questionable CQ and a bug","user":{"login":"mikedn","id":9973420,"avatar_url":"https://avatars.githubusercontent.com/u/9973420?v=3","gravatar_id":"","url":"https://api.github.com/users/mikedn","html_url":"https://github.com/mikedn","followers_url":"https://api.github.com/users/mikedn/followers","following_url":"https://api.github.com/users/mikedn/following{/other_user}","gists_url":"https://api.github.com/users/mikedn/gists{/gist_id}","starred_url":"https://api.github.com/users/mikedn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikedn/subscriptions","organizations_url":"https://api.github.com/users/mikedn/orgs","repos_url":"https://api.github.com/users/mikedn/repos","events_url":"https://api.github.com/users/mikedn/events{/privacy}","received_events_url":"https://api.github.com/users/mikedn/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/coreclr/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/dotnet/coreclr/labels/CodeGen","name":"CodeGen","color":"d4c5f9"},{"url":"https://api.github.com/repos/dotnet/coreclr/labels/optimization","name":"optimization","color":"0052cc"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/dotnet/coreclr/milestones/6","html_url":"https://github.com/dotnet/coreclr/milestones/1.0.0-rc2%20","labels_url":"https://api.github.com/repos/dotnet/coreclr/milestones/6/labels","id":1380513,"number":6,"title":"1.0.0-rc2 ","description":"","creator":{"login":"tzwlai","id":9966807,"avatar_url":"https://avatars.githubusercontent.com/u/9966807?v=3","gravatar_id":"","url":"https://api.github.com/users/tzwlai","html_url":"https://github.com/tzwlai","followers_url":"https://api.github.com/users/tzwlai/followers","following_url":"https://api.github.com/users/tzwlai/following{/other_user}","gists_url":"https://api.github.com/users/tzwlai/gists{/gist_id}","starred_url":"https://api.github.com/users/tzwlai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tzwlai/subscriptions","organizations_url":"https://api.github.com/users/tzwlai/orgs","repos_url":"https://api.github.com/users/tzwlai/repos","events_url":"https://api.github.com/users/tzwlai/events{/privacy}","received_events_url":"https://api.github.com/users/tzwlai/received_events","type":"User","site_admin":false},"open_issues":121,"closed_issues":58,"state":"open","created_at":"2015-10-27T22:42:35Z","updated_at":"2016-01-11T02:47:55Z","due_on":"2016-02-02T08:00:00Z","closed_at":null},"comments":4,"created_at":"2016-01-10T07:15:44Z","updated_at":"2016-01-11T12:38:00Z","closed_at":null,"body":"The following method\r\n```C#\r\n    static void Copy(int[] src, int[] dst, int length)\r\n    {\r\n        for (int i = 0; i < length; i++)\r\n            dst[i] = src[i];\r\n    }\r\n```\r\ngenerates\r\n```\r\nG_M64735_IG01:\r\n       4883EC28             sub      rsp, 40\r\nG_M64735_IG02:\r\n       33C0                 xor      eax, eax\r\n\r\n; check if length is > 0, ok\r\n       4585C0               test     r8d, r8d\r\n       0F8E82000000         jle      G_M64735_IG06\r\n\r\n; check if src and dst are non-null\r\n; not needed because we already know that at least one loop iteration will be executed\r\n       4885C9               test     rcx, rcx\r\n       410F95C1             setne    r9b\r\n       450FB6C9             movzx    r9, r9b\r\n       4885D2               test     rdx, rdx\r\n       410F95C2             setne    r10b\r\n       450FB6D2             movzx    r10, r10b\r\n       4523CA               and      r9d, r10d\r\n       4585C9               test     r9d, r9d\r\n       7445                 je       SHORT G_M64735_IG05\r\n\r\nG_M64735_IG03:\r\n; hoisted bound checks, ok\r\n       44394208             cmp      dword ptr [rdx+8], r8d\r\n       410F9DC1             setge    r9b\r\n       450FB6C9             movzx    r9, r9b\r\n       44394108             cmp      dword ptr [rcx+8], r8d\r\n       410F9DC2             setge    r10b\r\n       450FB6D2             movzx    r10, r10b\r\n\r\n; check if length is >= 0, dubious\r\n; this doesn't seem right, unlike the rest of the code this checks r8 instead of r8d\r\n; and the length was already checked at the start of the method\r\n       4D85C0               test     r8, r8\r\n       410F9DC3             setge    r11b\r\n       450FB6DB             movzx    r11, r11b\r\n       4523D3               and      r10d, r11d\r\n\r\n       4523CA               and      r9d, r10d\r\n       4585C9               test     r9d, r9d\r\n       7417                 je       SHORT G_M64735_IG05\r\n\r\nG_M64735_IG04:\r\n       4C63C8               movsxd   r9d, eax\r\n       468B548910           mov      r10d, dword ptr [rcx+4*r9+16]\r\n       4689548A10           mov      dword ptr [rdx+4*r9+16], r10d\r\n       FFC0                 inc      eax\r\n       413BC0               cmp      eax, r8d\r\n       7CEC                 jl       SHORT G_M64735_IG04\r\n; it's unfortunate that the loop version that is normally executed \r\n; is the one requiring a jmp, IG04 and IG05 should be reversed\r\n       EB20                 jmp      SHORT G_M64735_IG06\r\n\r\nG_M64735_IG05:\r\n       3B4108               cmp      eax, dword ptr [rcx+8]\r\n       731F                 jae      SHORT G_M64735_IG07\r\n       4C63C8               movsxd   r9d, eax\r\n       468B548910           mov      r10d, dword ptr [rcx+4*r9+16]\r\n       3B4208               cmp      eax, dword ptr [rdx+8]\r\n       7312                 jae      SHORT G_M64735_IG07\r\n       4689548A10           mov      dword ptr [rdx+4*r9+16], r10d\r\n       FFC0                 inc      eax\r\n       413BC0               cmp      eax, r8d\r\n       7CE2                 jl       SHORT G_M64735_IG05\r\n\r\nG_M64735_IG06:\r\n       4883C428             add      rsp, 40\r\n       C3                   ret\r\n\r\nG_M64735_IG07:\r\n       E8376AD45E           call     CORINFO_HELP_RNGCHKFAIL\r\n       CC                   int3\r\n```\r\n\r\n@Janiels speculated in #2594 that the null checks may be there so the NullReferenceException will appears as if it was thrown from inside the loop instead of being thrown form hoisted code. But if you pass a null array to this method the debugger will point to the `i < length` loop condition when the exception is thrown, not to `dst[i] = src[i]`."},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/170532201","html_url":"https://github.com/dotnet/coreclr/issues/2596#issuecomment-170532201","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/2596","id":170532201,"user":{"login":"CppStars","id":16410738,"avatar_url":"https://avatars.githubusercontent.com/u/16410738?v=3","gravatar_id":"","url":"https://api.github.com/users/CppStars","html_url":"https://github.com/CppStars","followers_url":"https://api.github.com/users/CppStars/followers","following_url":"https://api.github.com/users/CppStars/following{/other_user}","gists_url":"https://api.github.com/users/CppStars/gists{/gist_id}","starred_url":"https://api.github.com/users/CppStars/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CppStars/subscriptions","organizations_url":"https://api.github.com/users/CppStars/orgs","repos_url":"https://api.github.com/users/CppStars/repos","events_url":"https://api.github.com/users/CppStars/events{/privacy}","received_events_url":"https://api.github.com/users/CppStars/received_events","type":"User","site_admin":false},"created_at":"2016-01-11T12:38:00Z","updated_at":"2016-01-11T12:38:00Z","body":"@mikedn but it is a bug that the code throws an AV, right? Is this CoreCLR or .NET 4.6.1 also?"}}