{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rust/issues/24489","labels_url":"https://api.github.com/repos/rust-lang/rust/issues/24489/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/24489/comments","events_url":"https://api.github.com/repos/rust-lang/rust/issues/24489/events","html_url":"https://github.com/rust-lang/rust/issues/24489","id":68861001,"number":24489,"title":"Performance of inlined code not as expected","user":{"login":"tbu-","id":6666593,"avatar_url":"https://avatars.githubusercontent.com/u/6666593?v=3","gravatar_id":"","url":"https://api.github.com/users/tbu-","html_url":"https://github.com/tbu-","followers_url":"https://api.github.com/users/tbu-/followers","following_url":"https://api.github.com/users/tbu-/following{/other_user}","gists_url":"https://api.github.com/users/tbu-/gists{/gist_id}","starred_url":"https://api.github.com/users/tbu-/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tbu-/subscriptions","organizations_url":"https://api.github.com/users/tbu-/orgs","repos_url":"https://api.github.com/users/tbu-/repos","events_url":"https://api.github.com/users/tbu-/events{/privacy}","received_events_url":"https://api.github.com/users/tbu-/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-04-16T07:10:38Z","updated_at":"2015-04-16T10:36:10Z","closed_at":null,"body":"I expect the following two snippets to have the same performance:\r\n\r\n    #[inline(always)]\r\n    fn decrement(val: u32) -> Option<u32> {\r\n        if val != 0 {\r\n            Some(val - 1)\r\n        } else {\r\n            None\r\n        }\r\n    }\r\n\r\n    match decrement(a) {\r\n        Some(aa) => /* do something */ {},\r\n        None => /* do something else */ {},\r\n    }\r\n\r\nand\r\n\r\n    if a != 0 {\r\n        a - 1;\r\n        /* do something */\r\n    } else {\r\n        /* do something else */\r\n    }\r\n\r\nThe latter one has better performance in current Rust: [Gist](https://gist.github.com/tbu-/4ed70aa510c40086477f)."},"comment":{"url":"https://api.github.com/repos/rust-lang/rust/issues/comments/93704182","html_url":"https://github.com/rust-lang/rust/issues/24489#issuecomment-93704182","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/24489","id":93704182,"user":{"login":"dotdash","id":230962,"avatar_url":"https://avatars.githubusercontent.com/u/230962?v=3","gravatar_id":"","url":"https://api.github.com/users/dotdash","html_url":"https://github.com/dotdash","followers_url":"https://api.github.com/users/dotdash/followers","following_url":"https://api.github.com/users/dotdash/following{/other_user}","gists_url":"https://api.github.com/users/dotdash/gists{/gist_id}","starred_url":"https://api.github.com/users/dotdash/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotdash/subscriptions","organizations_url":"https://api.github.com/users/dotdash/orgs","repos_url":"https://api.github.com/users/dotdash/repos","events_url":"https://api.github.com/users/dotdash/events{/privacy}","received_events_url":"https://api.github.com/users/dotdash/received_events","type":"User","site_admin":false},"created_at":"2015-04-16T10:36:10Z","updated_at":"2015-04-16T10:36:10Z","body":"That's an interesting case, which I think LLVM should handle, but currently doesn't. Maybe the required analysis is too expensive? No idea. The problem is this:\r\n```llvm\r\nmatch_case:                                       ; preds = %match_case, %match_case.lr.ph\r\n  %.1417 = phi i32 [ %2, %match_case.lr.ph ], [ %.14, %match_case ]\r\n; black_box omitted here\r\n  %5 = icmp eq i32 %4, 0\r\n  %6 = add i32 %4, -1\r\n  %.14 = select i1 %5, i32 0, i32 %6\r\n  br i1 %5, label %clean_ast_57_.loopexit, label %match_case\r\n```\r\n\r\n`%.14` is only used in the PHI node when we are looping. And that is only the case when `%5` is false, which means that `%.i14` will always be equal to `%6`. And indeed, replacing `%.14` by `%6` in the PHI node leads to identical IR and ASM as the fast version.\r\n\r\nI'll try looking around in LLVM for a bit and file a bug if I can't figure out anything."}}