diff options
| author | Björn Steinbrink <bsteinbr@gmail.com> | 2013-07-13 04:10:41 +0200 |
|---|---|---|
| committer | Björn Steinbrink <bsteinbr@gmail.com> | 2013-07-13 13:33:48 +0200 |
| commit | 1d2e1a9ae5cc1affe54e3280cf272197a036beaf (patch) | |
| tree | 1956623d749b90a3612bdcb0b706c52db92bb2d8 | |
| parent | 5df2bb1bccd8394dbd73ffa52dea1151dcd0b0ad (diff) | |
| download | rust-1d2e1a9ae5cc1affe54e3280cf272197a036beaf.tar.gz rust-1d2e1a9ae5cc1affe54e3280cf272197a036beaf.zip | |
Avoid empty "else" blocks
If an "if" expression has no "else", we don't have to create an LLVM basic block either.
| -rw-r--r-- | src/librustc/middle/trans/controlflow.rs | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/src/librustc/middle/trans/controlflow.rs b/src/librustc/middle/trans/controlflow.rs index e9eb7ff086d..81260428f24 100644 --- a/src/librustc/middle/trans/controlflow.rs +++ b/src/librustc/middle/trans/controlflow.rs @@ -67,13 +67,8 @@ pub fn trans_if(bcx: block, expr::trans_to_datum(bcx, cond).to_result(); let then_bcx_in = scope_block(bcx, thn.info(), "then"); - let else_bcx_in = scope_block(bcx, els.info(), "else"); let cond_val = bool_to_i1(bcx, cond_val); - CondBr(bcx, cond_val, then_bcx_in.llbb, else_bcx_in.llbb); - - debug!("then_bcx_in=%s, else_bcx_in=%s", - then_bcx_in.to_str(), else_bcx_in.to_str()); let then_bcx_out = trans_block(then_bcx_in, thn, dest); let then_bcx_out = trans_block_cleanups(then_bcx_out, @@ -83,9 +78,10 @@ pub fn trans_if(bcx: block, // because trans_expr will create another scope block // context for the block, but we've already got the // 'else' context - let else_bcx_out = match els { + let (else_bcx_in, next_bcx) = match els { Some(elexpr) => { - match elexpr.node { + let else_bcx_in = scope_block(bcx, els.info(), "else"); + let else_bcx_out = match elexpr.node { ast::expr_if(_, _, _) => { let elseif_blk = ast_util::block_from_expr(elexpr); trans_block(else_bcx_in, &elseif_blk, dest) @@ -95,14 +91,25 @@ pub fn trans_if(bcx: block, } // would be nice to have a constraint on ifs _ => bcx.tcx().sess.bug("strange alternative in if") - } + }; + let else_bcx_out = trans_block_cleanups(else_bcx_out, + block_cleanups(else_bcx_in)); + + (else_bcx_in, join_blocks(bcx, [then_bcx_out, else_bcx_out])) + } + _ => { + let next_bcx = sub_block(bcx, "next"); + Br(then_bcx_out, next_bcx.llbb); + + (next_bcx, next_bcx) } - _ => else_bcx_in }; - let else_bcx_out = trans_block_cleanups(else_bcx_out, - block_cleanups(else_bcx_in)); - return join_blocks(bcx, [then_bcx_out, else_bcx_out]); + debug!("then_bcx_in=%s, else_bcx_in=%s", + then_bcx_in.to_str(), else_bcx_in.to_str()); + + CondBr(bcx, cond_val, then_bcx_in.llbb, else_bcx_in.llbb); + next_bcx } pub fn join_blocks(parent_bcx: block, in_cxs: &[block]) -> block { |
