Skip to content

Avoid translation of unreachable arms in lazy binops #21985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/librustc_trans/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,23 @@ fn trans_lazy_binop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
return immediate_rvalue_bcx(past_lhs, lhs, binop_ty).to_expr_datumblock();
}

// Check if we can statically determine that we'll always take the left or right side of the
// binop, and if so, don't bother to generate the conditional branch and just translate the arm
// that will actually be executed
if is_const(lhs) && !is_undef(lhs) {
match (&op, const_to_uint(lhs)) {
(&lazy_and, 0) | (&lazy_or, 1) => {
return immediate_rvalue_bcx(past_lhs, lhs, binop_ty).to_expr_datumblock();
}
(&lazy_and, 1) | (&lazy_or, 0) => {
let DatumBlock {bcx: past_rhs, datum: rhs} = trans(past_lhs, b);
let rhs = rhs.to_llscalarish(past_rhs);
return immediate_rvalue_bcx(past_rhs, rhs, binop_ty).to_expr_datumblock();
}
(_, _) => {}
}
}

let join = fcx.new_id_block("join", binop_expr.id);
let before_rhs = fcx.new_id_block("before_rhs", b.id);

Expand Down