Skip to content

Commit db8f2d5

Browse files
committed
Avoid unnecessary codegen in with_cond()
Currently "k / 2" generates one (k: uint) or two (k: int) "br false, ..." instructions and the corresponding basic blocks, producing quite some noise and making the code unnecessarily hard to read. Additionally we can skip translation if the code would end up unreachable anyway.
1 parent ac134f7 commit db8f2d5

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/librustc_trans/trans/_match.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,11 +889,13 @@ fn compile_guard<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
889889
}
890890
}
891891

892+
for (_, &binding_info) in &data.bindings_map {
893+
bcx.fcx.lllocals.borrow_mut().remove(&binding_info.id);
894+
}
895+
892896
with_cond(bcx, Not(bcx, val, guard_expr.debug_loc()), |bcx| {
893-
// Guard does not match: remove all bindings from the lllocals table
894897
for (_, &binding_info) in &data.bindings_map {
895898
call_lifetime_end(bcx, binding_info.llmatch);
896-
bcx.fcx.lllocals.borrow_mut().remove(&binding_info.id);
897899
}
898900
match chk {
899901
// If the default arm is the only one left, move on to the next

src/librustc_trans/trans/base.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,12 @@ pub fn with_cond<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
10791079
F: FnOnce(Block<'blk, 'tcx>) -> Block<'blk, 'tcx>,
10801080
{
10811081
let _icx = push_ctxt("with_cond");
1082+
1083+
if bcx.unreachable.get() ||
1084+
(common::is_const(val) && common::const_to_uint(val) == 0) {
1085+
return bcx;
1086+
}
1087+
10821088
let fcx = bcx.fcx;
10831089
let next_cx = fcx.new_temp_block("next");
10841090
let cond_cx = fcx.new_temp_block("cond");

0 commit comments

Comments
 (0)