{"action":"created","comment":{"url":"https://api.github.com/repos/rust-lang/rust/pulls/comments/47251671","id":47251671,"diff_hunk":"@@ -65,25 +44,70 @@ impl SimplifyCfg {\n             Some(target)\n         }\n \n+        let mut predecessor_map = util::build_predecessor_map(mir);\n+\n         let mut changed = false;\n-        for bb in mir.all_basic_blocks() {\n+        let mut seen = vec![false; mir.basic_blocks.len()];\n+        let mut worklist = vec![START_BLOCK];\n+        while let Some(bb) = worklist.pop() {\n             // Temporarily swap out the terminator we're modifying to keep borrowck happy\n             let mut terminator = Terminator::Diverge;\n             mem::swap(&mut terminator, &mut mir.basic_block_data_mut(bb).terminator);\n \n+            // Shortcut chains of empty blocks that just jump from one to the next\n             for target in terminator.successors_mut() {\n                 let new_target = match final_target(mir, *target) {\n                     Some(new_target) => new_target,\n                     None if mir.basic_block_data(bb).statements.is_empty() => bb,\n                     None => continue\n                 };\n-                changed |= *target != new_target;\n-                *target = new_target;\n+\n+                if *target != new_target {\n+                    changed = true;\n+                    predecessor_map.remove_predecessor(*target);\n+                    predecessor_map.add_predecessor(new_target);\n+                    *target = new_target;\n+                }\n             }\n \n-            mir.basic_block_data_mut(bb).terminator = terminator;\n+            // See if we can merge the target block into this one\n+            match terminator {\n+                Terminator::Goto { target } if target.index() > DIVERGE_BLOCK.index() &&\n+                        predecessor_map.num_predecessors(target) == 1 => {\n+                    changed = true;\n+                    let mut other_data = BasicBlockData {\n+                        statements: Vec::new(),\n+                        terminator: Terminator::Goto { target: target}\n+                    };\n+                    mem::swap(&mut other_data, mir.basic_block_data_mut(target));\n+\n+                    // target used to have 1 predecessor (bb), and still has only one (itself)\n+                    // All the successors of target have had target replaced by bb in their\n+                    // list of predecessors, keeping the number the same.\n+\n+                    let data = mir.basic_block_data_mut(bb);\n+                    data.statements.append(&mut other_data.statements);\n+                    mem::swap(&mut data.terminator, &mut other_data.terminator);\n+                }\n+                _ => mir.basic_block_data_mut(bb).terminator = terminator","path":"src/librustc_mir/transform/simplify_cfg.rs","position":83,"original_position":83,"commit_id":"156c061bc5e92a828ea9408a3c2cefb4b0c70da2","original_commit_id":"156c061bc5e92a828ea9408a3c2cefb4b0c70da2","user":{"login":"nikomatsakis","id":155238,"avatar_url":"https://avatars.githubusercontent.com/u/155238?v=3","gravatar_id":"","url":"https://api.github.com/users/nikomatsakis","html_url":"https://github.com/nikomatsakis","followers_url":"https://api.github.com/users/nikomatsakis/followers","following_url":"https://api.github.com/users/nikomatsakis/following{/other_user}","gists_url":"https://api.github.com/users/nikomatsakis/gists{/gist_id}","starred_url":"https://api.github.com/users/nikomatsakis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nikomatsakis/subscriptions","organizations_url":"https://api.github.com/users/nikomatsakis/orgs","repos_url":"https://api.github.com/users/nikomatsakis/repos","events_url":"https://api.github.com/users/nikomatsakis/events{/privacy}","received_events_url":"https://api.github.com/users/nikomatsakis/received_events","type":"User","site_admin":false},"body":"Nit: might be a little clearer if we move the assignment here after the match, so that it's clear that we restore the `terminator` on both paths (this took me a bit of puzzling out). But I'm not sure, and I guess it's a bit less efficient that way. Maybe just add a comment like `// restore the terminator we swapped out` on both branches?","created_at":"2015-12-10T16:50:26Z","updated_at":"2015-12-10T16:50:26Z","html_url":"https://github.com/rust-lang/rust/pull/30238#discussion_r47251671","pull_request_url":"https://api.github.com/repos/rust-lang/rust/pulls/30238","_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/comments/47251671"},"html":{"href":"https://github.com/rust-lang/rust/pull/30238#discussion_r47251671"},"pull_request":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/30238"}}},"pull_request":{"url":"https://api.github.com/repos/rust-lang/rust/pulls/30238","id":52783332,"html_url":"https://github.com/rust-lang/rust/pull/30238","diff_url":"https://github.com/rust-lang/rust/pull/30238.diff","patch_url":"https://github.com/rust-lang/rust/pull/30238.patch","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/30238","number":30238,"state":"open","locked":false,"title":"[MIR] Enhance the SimplifyCfg pass to merge consecutive blocks","user":{"login":"gereeter","id":2328242,"avatar_url":"https://avatars.githubusercontent.com/u/2328242?v=3","gravatar_id":"","url":"https://api.github.com/users/gereeter","html_url":"https://github.com/gereeter","followers_url":"https://api.github.com/users/gereeter/followers","following_url":"https://api.github.com/users/gereeter/following{/other_user}","gists_url":"https://api.github.com/users/gereeter/gists{/gist_id}","starred_url":"https://api.github.com/users/gereeter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gereeter/subscriptions","organizations_url":"https://api.github.com/users/gereeter/orgs","repos_url":"https://api.github.com/users/gereeter/repos","events_url":"https://api.github.com/users/gereeter/events{/privacy}","received_events_url":"https://api.github.com/users/gereeter/received_events","type":"User","site_admin":false},"body":"This is a rebase of @dotdash's #29838. I'm not sure that I like the last commit, as it seems a lot harder to tell that predecessors are tracked correctly when what predecessors are changing isn't written down.","created_at":"2015-12-06T16:11:41Z","updated_at":"2015-12-10T16:50:26Z","closed_at":null,"merged_at":null,"merge_commit_sha":"accdf9df1dadaf33ad2e01cf023417ed56d5de76","assignee":{"login":"nikomatsakis","id":155238,"avatar_url":"https://avatars.githubusercontent.com/u/155238?v=3","gravatar_id":"","url":"https://api.github.com/users/nikomatsakis","html_url":"https://github.com/nikomatsakis","followers_url":"https://api.github.com/users/nikomatsakis/followers","following_url":"https://api.github.com/users/nikomatsakis/following{/other_user}","gists_url":"https://api.github.com/users/nikomatsakis/gists{/gist_id}","starred_url":"https://api.github.com/users/nikomatsakis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nikomatsakis/subscriptions","organizations_url":"https://api.github.com/users/nikomatsakis/orgs","repos_url":"https://api.github.com/users/nikomatsakis/repos","events_url":"https://api.github.com/users/nikomatsakis/events{/privacy}","received_events_url":"https://api.github.com/users/nikomatsakis/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/rust-lang/rust/pulls/30238/commits","review_comments_url":"https://api.github.com/repos/rust-lang/rust/pulls/30238/comments","review_comment_url":"https://api.github.com/repos/rust-lang/rust/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/30238/comments","statuses_url":"https://api.github.com/repos/rust-lang/rust/statuses/156c061bc5e92a828ea9408a3c2cefb4b0c70da2","head":{"label":"gereeter:merge-simplify-cfg","ref":"merge-simplify-cfg","sha":"156c061bc5e92a828ea9408a3c2cefb4b0c70da2","user":{"login":"gereeter","id":2328242,"avatar_url":"https://avatars.githubusercontent.com/u/2328242?v=3","gravatar_id":"","url":"https://api.github.com/users/gereeter","html_url":"https://github.com/gereeter","followers_url":"https://api.github.com/users/gereeter/followers","following_url":"https://api.github.com/users/gereeter/following{/other_user}","gists_url":"https://api.github.com/users/gereeter/gists{/gist_id}","starred_url":"https://api.github.com/users/gereeter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gereeter/subscriptions","organizations_url":"https://api.github.com/users/gereeter/orgs","repos_url":"https://api.github.com/users/gereeter/repos","events_url":"https://api.github.com/users/gereeter/events{/privacy}","received_events_url":"https://api.github.com/users/gereeter/received_events","type":"User","site_admin":false},"repo":{"id":17634944,"name":"rust","full_name":"gereeter/rust","owner":{"login":"gereeter","id":2328242,"avatar_url":"https://avatars.githubusercontent.com/u/2328242?v=3","gravatar_id":"","url":"https://api.github.com/users/gereeter","html_url":"https://github.com/gereeter","followers_url":"https://api.github.com/users/gereeter/followers","following_url":"https://api.github.com/users/gereeter/following{/other_user}","gists_url":"https://api.github.com/users/gereeter/gists{/gist_id}","starred_url":"https://api.github.com/users/gereeter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gereeter/subscriptions","organizations_url":"https://api.github.com/users/gereeter/orgs","repos_url":"https://api.github.com/users/gereeter/repos","events_url":"https://api.github.com/users/gereeter/events{/privacy}","received_events_url":"https://api.github.com/users/gereeter/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/gereeter/rust","description":"a safe, concurrent, practical language","fork":true,"url":"https://api.github.com/repos/gereeter/rust","forks_url":"https://api.github.com/repos/gereeter/rust/forks","keys_url":"https://api.github.com/repos/gereeter/rust/keys{/key_id}","collaborators_url":"https://api.github.com/repos/gereeter/rust/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/gereeter/rust/teams","hooks_url":"https://api.github.com/repos/gereeter/rust/hooks","issue_events_url":"https://api.github.com/repos/gereeter/rust/issues/events{/number}","events_url":"https://api.github.com/repos/gereeter/rust/events","assignees_url":"https://api.github.com/repos/gereeter/rust/assignees{/user}","branches_url":"https://api.github.com/repos/gereeter/rust/branches{/branch}","tags_url":"https://api.github.com/repos/gereeter/rust/tags","blobs_url":"https://api.github.com/repos/gereeter/rust/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/gereeter/rust/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/gereeter/rust/git/refs{/sha}","trees_url":"https://api.github.com/repos/gereeter/rust/git/trees{/sha}","statuses_url":"https://api.github.com/repos/gereeter/rust/statuses/{sha}","languages_url":"https://api.github.com/repos/gereeter/rust/languages","stargazers_url":"https://api.github.com/repos/gereeter/rust/stargazers","contributors_url":"https://api.github.com/repos/gereeter/rust/contributors","subscribers_url":"https://api.github.com/repos/gereeter/rust/subscribers","subscription_url":"https://api.github.com/repos/gereeter/rust/subscription","commits_url":"https://api.github.com/repos/gereeter/rust/commits{/sha}","git_commits_url":"https://api.github.com/repos/gereeter/rust/git/commits{/sha}","comments_url":"https://api.github.com/repos/gereeter/rust/comments{/number}","issue_comment_url":"https://api.github.com/repos/gereeter/rust/issues/comments{/number}","contents_url":"https://api.github.com/repos/gereeter/rust/contents/{+path}","compare_url":"https://api.github.com/repos/gereeter/rust/compare/{base}...{head}","merges_url":"https://api.github.com/repos/gereeter/rust/merges","archive_url":"https://api.github.com/repos/gereeter/rust/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/gereeter/rust/downloads","issues_url":"https://api.github.com/repos/gereeter/rust/issues{/number}","pulls_url":"https://api.github.com/repos/gereeter/rust/pulls{/number}","milestones_url":"https://api.github.com/repos/gereeter/rust/milestones{/number}","notifications_url":"https://api.github.com/repos/gereeter/rust/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/gereeter/rust/labels{/name}","releases_url":"https://api.github.com/repos/gereeter/rust/releases{/id}","created_at":"2014-03-11T15:13:15Z","updated_at":"2015-04-05T16:53:23Z","pushed_at":"2015-12-06T19:20:21Z","git_url":"git://github.com/gereeter/rust.git","ssh_url":"git@github.com:gereeter/rust.git","clone_url":"https://github.com/gereeter/rust.git","svn_url":"https://github.com/gereeter/rust","homepage":"http://www.rust-lang.org","size":330771,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rust-lang:master","ref":"master","sha":"85b031b3bc78b673cdbbf578112028ec7b798443","user":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"repo":{"id":724712,"name":"rust","full_name":"rust-lang/rust","owner":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rust-lang/rust","description":"A safe, concurrent, practical language.","fork":false,"url":"https://api.github.com/repos/rust-lang/rust","forks_url":"https://api.github.com/repos/rust-lang/rust/forks","keys_url":"https://api.github.com/repos/rust-lang/rust/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rust-lang/rust/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rust-lang/rust/teams","hooks_url":"https://api.github.com/repos/rust-lang/rust/hooks","issue_events_url":"https://api.github.com/repos/rust-lang/rust/issues/events{/number}","events_url":"https://api.github.com/repos/rust-lang/rust/events","assignees_url":"https://api.github.com/repos/rust-lang/rust/assignees{/user}","branches_url":"https://api.github.com/repos/rust-lang/rust/branches{/branch}","tags_url":"https://api.github.com/repos/rust-lang/rust/tags","blobs_url":"https://api.github.com/repos/rust-lang/rust/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rust-lang/rust/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rust-lang/rust/git/refs{/sha}","trees_url":"https://api.github.com/repos/rust-lang/rust/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rust-lang/rust/statuses/{sha}","languages_url":"https://api.github.com/repos/rust-lang/rust/languages","stargazers_url":"https://api.github.com/repos/rust-lang/rust/stargazers","contributors_url":"https://api.github.com/repos/rust-lang/rust/contributors","subscribers_url":"https://api.github.com/repos/rust-lang/rust/subscribers","subscription_url":"https://api.github.com/repos/rust-lang/rust/subscription","commits_url":"https://api.github.com/repos/rust-lang/rust/commits{/sha}","git_commits_url":"https://api.github.com/repos/rust-lang/rust/git/commits{/sha}","comments_url":"https://api.github.com/repos/rust-lang/rust/comments{/number}","issue_comment_url":"https://api.github.com/repos/rust-lang/rust/issues/comments{/number}","contents_url":"https://api.github.com/repos/rust-lang/rust/contents/{+path}","compare_url":"https://api.github.com/repos/rust-lang/rust/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rust-lang/rust/merges","archive_url":"https://api.github.com/repos/rust-lang/rust/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rust-lang/rust/downloads","issues_url":"https://api.github.com/repos/rust-lang/rust/issues{/number}","pulls_url":"https://api.github.com/repos/rust-lang/rust/pulls{/number}","milestones_url":"https://api.github.com/repos/rust-lang/rust/milestones{/number}","notifications_url":"https://api.github.com/repos/rust-lang/rust/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rust-lang/rust/labels{/name}","releases_url":"https://api.github.com/repos/rust-lang/rust/releases{/id}","created_at":"2010-06-16T20:39:03Z","updated_at":"2015-12-10T16:16:04Z","pushed_at":"2015-12-10T16:47:36Z","git_url":"git://github.com/rust-lang/rust.git","ssh_url":"git@github.com:rust-lang/rust.git","clone_url":"https://github.com/rust-lang/rust.git","svn_url":"https://github.com/rust-lang/rust","homepage":"http://www.rust-lang.org","size":392731,"stargazers_count":14501,"watchers_count":14501,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":2829,"mirror_url":null,"open_issues_count":2342,"forks":2829,"open_issues":2342,"watchers":14501,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/30238"},"html":{"href":"https://github.com/rust-lang/rust/pull/30238"},"issue":{"href":"https://api.github.com/repos/rust-lang/rust/issues/30238"},"comments":{"href":"https://api.github.com/repos/rust-lang/rust/issues/30238/comments"},"review_comments":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/30238/comments"},"review_comment":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/30238/commits"},"statuses":{"href":"https://api.github.com/repos/rust-lang/rust/statuses/156c061bc5e92a828ea9408a3c2cefb4b0c70da2"}}}}