Skip to content

Commit 1d2e1a9

Browse files
committed
Avoid empty "else" blocks
If an "if" expression has no "else", we don't have to create an LLVM basic block either.
1 parent 5df2bb1 commit 1d2e1a9

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/librustc/middle/trans/controlflow.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,8 @@ pub fn trans_if(bcx: block,
6767
expr::trans_to_datum(bcx, cond).to_result();
6868

6969
let then_bcx_in = scope_block(bcx, thn.info(), "then");
70-
let else_bcx_in = scope_block(bcx, els.info(), "else");
7170

7271
let cond_val = bool_to_i1(bcx, cond_val);
73-
CondBr(bcx, cond_val, then_bcx_in.llbb, else_bcx_in.llbb);
74-
75-
debug!("then_bcx_in=%s, else_bcx_in=%s",
76-
then_bcx_in.to_str(), else_bcx_in.to_str());
7772

7873
let then_bcx_out = trans_block(then_bcx_in, thn, dest);
7974
let then_bcx_out = trans_block_cleanups(then_bcx_out,
@@ -83,9 +78,10 @@ pub fn trans_if(bcx: block,
8378
// because trans_expr will create another scope block
8479
// context for the block, but we've already got the
8580
// 'else' context
86-
let else_bcx_out = match els {
81+
let (else_bcx_in, next_bcx) = match els {
8782
Some(elexpr) => {
88-
match elexpr.node {
83+
let else_bcx_in = scope_block(bcx, els.info(), "else");
84+
let else_bcx_out = match elexpr.node {
8985
ast::expr_if(_, _, _) => {
9086
let elseif_blk = ast_util::block_from_expr(elexpr);
9187
trans_block(else_bcx_in, &elseif_blk, dest)
@@ -95,14 +91,25 @@ pub fn trans_if(bcx: block,
9591
}
9692
// would be nice to have a constraint on ifs
9793
_ => bcx.tcx().sess.bug("strange alternative in if")
98-
}
94+
};
95+
let else_bcx_out = trans_block_cleanups(else_bcx_out,
96+
block_cleanups(else_bcx_in));
97+
98+
(else_bcx_in, join_blocks(bcx, [then_bcx_out, else_bcx_out]))
99+
}
100+
_ => {
101+
let next_bcx = sub_block(bcx, "next");
102+
Br(then_bcx_out, next_bcx.llbb);
103+
104+
(next_bcx, next_bcx)
99105
}
100-
_ => else_bcx_in
101106
};
102-
let else_bcx_out = trans_block_cleanups(else_bcx_out,
103-
block_cleanups(else_bcx_in));
104-
return join_blocks(bcx, [then_bcx_out, else_bcx_out]);
105107

108+
debug!("then_bcx_in=%s, else_bcx_in=%s",
109+
then_bcx_in.to_str(), else_bcx_in.to_str());
110+
111+
CondBr(bcx, cond_val, then_bcx_in.llbb, else_bcx_in.llbb);
112+
next_bcx
106113
}
107114

108115
pub fn join_blocks(parent_bcx: block, in_cxs: &[block]) -> block {

0 commit comments

Comments
 (0)