{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/9707","repository_url":"https://api.github.com/repos/dotnet/coreclr","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/9707/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/9707/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/9707/events","html_url":"https://github.com/dotnet/coreclr/issues/9707","id":209303637,"number":9707,"title":"Pre-check doesn't eliminate bounds check","user":{"login":"benaadams","id":1142958,"avatar_url":"https://avatars.githubusercontent.com/u/1142958?v=3","gravatar_id":"","url":"https://api.github.com/users/benaadams","html_url":"https://github.com/benaadams","followers_url":"https://api.github.com/users/benaadams/followers","following_url":"https://api.github.com/users/benaadams/following{/other_user}","gists_url":"https://api.github.com/users/benaadams/gists{/gist_id}","starred_url":"https://api.github.com/users/benaadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benaadams/subscriptions","organizations_url":"https://api.github.com/users/benaadams/orgs","repos_url":"https://api.github.com/users/benaadams/repos","events_url":"https://api.github.com/users/benaadams/events{/privacy}","received_events_url":"https://api.github.com/users/benaadams/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2017-02-21T23:18:38Z","updated_at":"2017-02-22T18:40:55Z","closed_at":null,"body":"I'm experimenting with improvements `List.Add` and noticed that bounds checks are not elimitated where the value is pre-checked e.g.\r\n```csharp\r\npublic void Add(T item)\r\n{\r\n    var array = _items;\r\n    _version++;\r\n    var size = _size;\r\n    if (size < array.Length)\r\n    {\r\n        array[size] = item;\r\n        _size = size + 1;\r\n    }\r\n    else\r\n    {\r\n        AddWithResize(item);\r\n    }\r\n}\r\n```\r\nGenerates\r\n```asm\r\nInlines into 060034D4 List`1:Add(long):this\r\n  [0 IL=0054 TR=000029 060034D5] [FAILED: noinline per IL/cached result] List`1:AddWithResize(long):this\r\nBudget: initialTime=240, finalTime=240, initialBudget=2400, currentBudget=2400\r\nBudget: initialSize=1499, finalSize=1499\r\n; Assembly listing for method List`1:Add(long):this\r\n; Emitting BLENDED_CODE for X64 CPU with SSE2\r\n; optimized code\r\n; rsp based frame\r\n; fully interruptible\r\n; Final local variable assignments\r\n;\r\n;  V00 this         [V00,T00] ( 12,  10.5)     ref  ->  rcx         this\r\n;  V01 arg1         [V01,T02] (  4,   3  )    long  ->  rdx        \r\n;  V02 loc0         [V02,T03] (  4,   3  )     ref  ->  rax        \r\n;  V03 loc1         [V03,T01] (  7,   5  )     int  ->   r8        \r\n;  V04 OutArgs      [V04    ] (  1,   1  )  lclBlk (32) [rsp+0x00]  \r\n;\r\n; Lcl frame size = 40\r\n\r\nG_M51715_IG01:\r\n       4883EC28             sub      rsp, 40\r\n       90                   nop      \r\n\r\nG_M51715_IG02:\r\n       488B4108             mov      rax, gword ptr [rcx+8]\r\n       FF411C               inc      dword ptr [rcx+28]\r\n       448B4118             mov      r8d, dword ptr [rcx+24]\r\n       44394008             cmp      dword ptr [rax+8], r8d  // check\r\n       7E1B                 jle      SHORT G_M51715_IG04     // jump alt path\r\n       443B4008             cmp      r8d, dword ptr [rax+8]  // check\r\n       7323                 jae      SHORT G_M51715_IG06     // jump bounds check fail (unreachable)\r\n       4D63C8               movsxd   r9, r8d\r\n       4A8954C810           mov      qword ptr [rax+8*r9+16], rdx\r\n       41FFC0               inc      r8d\r\n       44894118             mov      dword ptr [rcx+24], r8d\r\n\r\nG_M51715_IG03:\r\n       4883C428             add      rsp, 40\r\n       C3                   ret      \r\n\r\nG_M51715_IG04:\r\n       488D0500000000       lea      rax, [(reloc)]\r\n\r\nG_M51715_IG05:\r\n       4883C428             add      rsp, 40\r\n       48FFE0               rex.jmp  rax\r\n\r\nG_M51715_IG06:\r\n       E800000000           call     CORINFO_HELP_RNGCHKFAIL\r\n       CC                   int3     \r\n\r\n; Total bytes of code 68, prolog size 5 for method List`1:Add(long):this\r\n```\r\nTried various re-orderings; but nothing seems to effect it\r\n\r\n/cc @JosephTremoulet\r\n"},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/281761282","html_url":"https://github.com/dotnet/coreclr/issues/9707#issuecomment-281761282","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/9707","id":281761282,"user":{"login":"benaadams","id":1142958,"avatar_url":"https://avatars.githubusercontent.com/u/1142958?v=3","gravatar_id":"","url":"https://api.github.com/users/benaadams","html_url":"https://github.com/benaadams","followers_url":"https://api.github.com/users/benaadams/followers","following_url":"https://api.github.com/users/benaadams/following{/other_user}","gists_url":"https://api.github.com/users/benaadams/gists{/gist_id}","starred_url":"https://api.github.com/users/benaadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benaadams/subscriptions","organizations_url":"https://api.github.com/users/benaadams/orgs","repos_url":"https://api.github.com/users/benaadams/repos","events_url":"https://api.github.com/users/benaadams/events{/privacy}","received_events_url":"https://api.github.com/users/benaadams/received_events","type":"User","site_admin":false},"created_at":"2017-02-22T18:40:55Z","updated_at":"2017-02-22T18:40:55Z","body":"@mikedn thank you for looking into this"}}
{"actor":{"display_login":"benaadams"}}