{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/6303","repository_url":"https://api.github.com/repos/dotnet/coreclr","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/6303/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/6303/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/6303/events","html_url":"https://github.com/dotnet/coreclr/issues/6303","id":165948818,"number":6303,"title":"RyuJIT can't detect that certain expressions are loop invariant","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/CodeGen","name":"CodeGen","color":"d4c5f9"},{"url":"https://api.github.com/repos/dotnet/coreclr/labels/optimization","name":"optimization","color":"0052cc"}],"state":"open","locked":false,"assignee":{"login":"JosephTremoulet","id":10655216,"avatar_url":"https://avatars.githubusercontent.com/u/10655216?v=3","gravatar_id":"","url":"https://api.github.com/users/JosephTremoulet","html_url":"https://github.com/JosephTremoulet","followers_url":"https://api.github.com/users/JosephTremoulet/followers","following_url":"https://api.github.com/users/JosephTremoulet/following{/other_user}","gists_url":"https://api.github.com/users/JosephTremoulet/gists{/gist_id}","starred_url":"https://api.github.com/users/JosephTremoulet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JosephTremoulet/subscriptions","organizations_url":"https://api.github.com/users/JosephTremoulet/orgs","repos_url":"https://api.github.com/users/JosephTremoulet/repos","events_url":"https://api.github.com/users/JosephTremoulet/events{/privacy}","received_events_url":"https://api.github.com/users/JosephTremoulet/received_events","type":"User","site_admin":false},"assignees":[{"login":"JosephTremoulet","id":10655216,"avatar_url":"https://avatars.githubusercontent.com/u/10655216?v=3","gravatar_id":"","url":"https://api.github.com/users/JosephTremoulet","html_url":"https://github.com/JosephTremoulet","followers_url":"https://api.github.com/users/JosephTremoulet/followers","following_url":"https://api.github.com/users/JosephTremoulet/following{/other_user}","gists_url":"https://api.github.com/users/JosephTremoulet/gists{/gist_id}","starred_url":"https://api.github.com/users/JosephTremoulet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JosephTremoulet/subscriptions","organizations_url":"https://api.github.com/users/JosephTremoulet/orgs","repos_url":"https://api.github.com/users/JosephTremoulet/repos","events_url":"https://api.github.com/users/JosephTremoulet/events{/privacy}","received_events_url":"https://api.github.com/users/JosephTremoulet/received_events","type":"User","site_admin":false}],"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":518,"closed_issues":282,"state":"open","created_at":"2015-09-29T00:26:07Z","updated_at":"2016-08-11T08:26:51Z","due_on":null,"closed_at":null},"comments":7,"created_at":"2016-07-16T21:39:26Z","updated_at":"2016-08-11T21:14:03Z","closed_at":null,"body":"Consider the following example:\r\n```C#\r\nusing System;\r\nusing System.Runtime.CompilerServices;\r\n\r\nclass Program {\r\n    [MethodImpl(MethodImplOptions.NoInlining)]\r\n    static void use(int x) {\r\n    }\r\n    static void Main() {\r\n        var r = new Random();\r\n        int x = r.Next();\r\n        int y = r.Next();\r\n        int z = r.Next();\r\n\r\n        for (int i = 0; i < 10; i++)\r\n            use(x * y * z);\r\n    }\r\n}\r\n```\r\nThe `x*y*z` expression is invariant but it is not hoisted:\r\n```\r\nG_M45811_IG03:\r\n       8BCF                 mov      ecx, edi\r\n       0FAFCB               imul     ecx, ebx\r\n       0FAFCE               imul     ecx, esi\r\n       E892FBFFFF           call     Program:use(int)\r\n       41FFC6               inc      r14d\r\n       4183FE0A             cmp      r14d, 10\r\n       7CEA                 jl       SHORT G_M45811_IG03\r\n```\r\n\r\nAs far as I can tell this happens because the JIT can't detect that `x`, `y` and `z` themselves are invariant. There doesn't seem to exist a VN function application for `Random.Next` calls and without that there seems to be no way to tell where a particular VN was defined, outside the loop in this case."},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/239294621","html_url":"https://github.com/dotnet/coreclr/issues/6303#issuecomment-239294621","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/6303","id":239294621,"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-08-11T21:14:03Z","updated_at":"2016-08-11T21:14:03Z","body":"Indeed, if I put that code in an instance method and access those fields via `this` then it works fine. But only up to a point:\r\n```C#\r\nfor (int n = 0; n < 10; n++)\r\n{\r\n    int k = n + a;\r\n    int l = n + b;\r\n\r\n    for (int i = 0; i < 10; i++)\r\n        use(k * l); // * not hoisted\r\n}\r\n```\r\n"}}
{"actor":{"display_login":"mikedn"}}