{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/3439","repository_url":"https://api.github.com/repos/dotnet/coreclr","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/3439/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/3439/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/3439/events","html_url":"https://github.com/dotnet/coreclr/issues/3439","id":137459391,"number":3439,"title":"[RyuJIT] Div then Mod (or viceversa) executes idiv twice instead of using the value in the register","user":{"login":"redknightlois","id":1045411,"avatar_url":"https://avatars.githubusercontent.com/u/1045411?v=3","gravatar_id":"","url":"https://api.github.com/users/redknightlois","html_url":"https://github.com/redknightlois","followers_url":"https://api.github.com/users/redknightlois/followers","following_url":"https://api.github.com/users/redknightlois/following{/other_user}","gists_url":"https://api.github.com/users/redknightlois/gists{/gist_id}","starred_url":"https://api.github.com/users/redknightlois/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/redknightlois/subscriptions","organizations_url":"https://api.github.com/users/redknightlois/orgs","repos_url":"https://api.github.com/users/redknightlois/repos","events_url":"https://api.github.com/users/redknightlois/events{/privacy}","received_events_url":"https://api.github.com/users/redknightlois/received_events","type":"User","site_admin":false},"labels":[{"id":196940911,"url":"https://api.github.com/repos/dotnet/coreclr/labels/CodeGen","name":"CodeGen","color":"d4c5f9","default":false},{"id":190283555,"url":"https://api.github.com/repos/dotnet/coreclr/labels/optimization","name":"optimization","color":"0052cc","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/dotnet/coreclr/milestones/5","html_url":"https://github.com/dotnet/coreclr/milestones/Future","labels_url":"https://api.github.com/repos/dotnet/coreclr/milestones/5/labels","id":1328081,"number":5,"title":"Future","description":"Milestone tracking the issues that have been triaged for after 1.0.0-rtm","creator":{"login":"joshfree","id":1302850,"avatar_url":"https://avatars.githubusercontent.com/u/1302850?v=3","gravatar_id":"","url":"https://api.github.com/users/joshfree","html_url":"https://github.com/joshfree","followers_url":"https://api.github.com/users/joshfree/followers","following_url":"https://api.github.com/users/joshfree/following{/other_user}","gists_url":"https://api.github.com/users/joshfree/gists{/gist_id}","starred_url":"https://api.github.com/users/joshfree/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joshfree/subscriptions","organizations_url":"https://api.github.com/users/joshfree/orgs","repos_url":"https://api.github.com/users/joshfree/repos","events_url":"https://api.github.com/users/joshfree/events{/privacy}","received_events_url":"https://api.github.com/users/joshfree/received_events","type":"User","site_admin":false},"open_issues":631,"closed_issues":344,"state":"open","created_at":"2015-09-29T00:26:07Z","updated_at":"2016-11-11T19:37:45Z","due_on":null,"closed_at":null},"comments":10,"created_at":"2016-03-01T03:43:03Z","updated_at":"2016-11-11T22:08:28Z","closed_at":null,"body":"Found this trying to optimize a very low level memory addressing algorithm. Suddenly I noticed that we were doing the idiv twice. \n\nRepro on rc2-16551:\n\n``` csharp\n  public class Program\n    {\n        public long Dividend;\n\n        [MethodImpl(MethodImplOptions.NoInlining)]\n        public long Run(long parameter)\n        {\n            long div = parameter / Dividend;\n            long mod = parameter % Dividend;\n\n            return div + mod;\n        }\n\n        public static void Main(string[] args)\n        {\n            Random r = new Random();\n\n            var p = new Program();\n            p.Dividend = 10;\n\n            long value = p.Run(203);\n            Console.WriteLine($\"{value}\");\n            Console.ReadLine();\n        }\n    }\n```\n\nwill output (in x64 but will also happen in x86):\n\n``` asm\n00007FF8E8EC3C83  mov         rcx,qword ptr [rcx+8]  \n00007FF8E8EC3C87  mov         rax,r8          // 2 instructions to prepare for idiv. \n00007FF8E8EC3C8A  cqo                    \n00007FF8E8EC3C8C  idiv        rax,rcx         // parameter / Dividend\n00007FF8E8EC3C8F  mov         r9,rax          // r9:= divResult \n00007FF8E8EC3C92  mov         rax,r8          // 2 instructions to prepare for idiv. \n00007FF8E8EC3C95  cqo  \n00007FF8E8EC3C97  idiv        rax,rcx         //  parameter / Dividend\n00007FF8E8EC3C9A  lea         rax,[r9+rdx]    // rax := divResult + modulus\n```\n\nthis could be written as:\n\n``` asm\n00007FF8E8EC3C83  mov         rcx,qword ptr [rcx+8]  \n00007FF8E8EC3C87  mov         rax,r8          // 2 instructions to prepare for idiv. \n00007FF8E8EC3C8A  cqo                    \n00007FF8E8EC3C8C  idiv        rax,rcx         //  parameter / Dividend\n00007FF8E8EC3C92  add         rax,rdx         //  rax := divResult + modulus\n```\n\nThis is pretty common in addressing modes or accessing matrix locations, etc. Everywhere you have a row and a column, you will have such an structure.\n"},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/260067021","html_url":"https://github.com/dotnet/coreclr/issues/3439#issuecomment-260067021","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/3439","id":260067021,"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},"created_at":"2016-11-11T22:08:28Z","updated_at":"2016-11-11T22:08:28Z","body":"Does `BlocksPerPage` change often? I would guess that it doesn't, it's fixed for a given BTree. If that's the case then you could use reciprocal division, it would likely be better than the JIT removing a redundant `idiv`."}}
{"actor":{"display_login":"mikedn"}}