Skip to content

Commit

Permalink
remove cleanup branches to the resume block
Browse files Browse the repository at this point in the history
This improves LLVM performance by 10% lost during the shimmir transition.
  • Loading branch information
Ariel Ben-Yehuda authored and arielb1 committed Apr 22, 2017
1 parent 9cc77d7 commit 3bf0045
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/librustc_mir/transform/simplify.rs
Expand Up @@ -124,6 +124,8 @@ impl<'a, 'tcx: 'a> CfgSimplifier<'a, 'tcx> {
self.collapse_goto_chain(successor, &mut changed);
}

changed |= self.simplify_unwind(&mut terminator);

let mut new_stmts = vec![];
let mut inner_changed = true;
while inner_changed {
Expand Down Expand Up @@ -238,6 +240,38 @@ impl<'a, 'tcx: 'a> CfgSimplifier<'a, 'tcx> {
true
}

// turn an unwind branch to a resume block into a None
fn simplify_unwind(&mut self, terminator: &mut Terminator<'tcx>) -> bool {
let unwind = match terminator.kind {
TerminatorKind::Drop { ref mut unwind, .. } |
TerminatorKind::DropAndReplace { ref mut unwind, .. } |
TerminatorKind::Call { cleanup: ref mut unwind, .. } |
TerminatorKind::Assert { cleanup: ref mut unwind, .. } =>
unwind,
_ => return false
};

if let &mut Some(unwind_block) = unwind {
let is_resume_block = match self.basic_blocks[unwind_block] {
BasicBlockData {
ref statements,
terminator: Some(Terminator {
kind: TerminatorKind::Resume, ..
}), ..
} if statements.is_empty() => true,
_ => false
};
if is_resume_block {
debug!("simplifying unwind to {:?} from {:?}",
unwind_block, terminator.source_info);
*unwind = None;
}
return is_resume_block;
}

false
}

fn strip_nops(&mut self) {
for blk in self.basic_blocks.iter_mut() {
blk.statements.retain(|stmt| if let StatementKind::Nop = stmt.kind {
Expand Down
2 changes: 1 addition & 1 deletion src/test/codegen/drop.rs
Expand Up @@ -36,7 +36,7 @@ pub fn droppy() {
// CHECK-NOT: call{{.*}}drop{{.*}}SomeUniqueName
// CHECK: invoke{{.*}}drop{{.*}}SomeUniqueName
// CHECK: invoke{{.*}}drop{{.*}}SomeUniqueName
// CHECK: invoke{{.*}}drop{{.*}}SomeUniqueName
// CHECK: call{{.*}}drop{{.*}}SomeUniqueName
// CHECK-NOT: {{(call|invoke).*}}drop{{.*}}SomeUniqueName
// The next line checks for the } that ends the function definition
// CHECK-LABEL: {{^[}]}}
Expand Down
1 change: 1 addition & 0 deletions src/test/codegen/personality_lifetimes.rs
Expand Up @@ -37,5 +37,6 @@ pub fn test() {
// CHECK: bitcast{{.*}}personalityslot
// CHECK-NEXT: call void @llvm.lifetime.start
might_unwind();
let _t = S;
might_unwind();
}

0 comments on commit 3bf0045

Please sign in to comment.