diff options
| author | Ariel Ben-Yehuda <ariel.byd@gmail.com> | 2015-05-19 17:38:55 +0300 |
|---|---|---|
| committer | Ariel Ben-Yehuda <ariel.byd@gmail.com> | 2015-05-19 17:42:15 +0300 |
| commit | 3afd760bb3fc8c90adfd9451e1cd8b161301f218 (patch) | |
| tree | 96bfa5b727ac10ab9aabae78e464cf47521dd25a /src | |
| parent | 27d2bd13c357bed328735da8605fce1416acc060 (diff) | |
| download | rust-3afd760bb3fc8c90adfd9451e1cd8b161301f218.tar.gz rust-3afd760bb3fc8c90adfd9451e1cd8b161301f218.zip | |
Fix translation of semi-constant if-statements
Thanks @dotdash
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc_trans/trans/base.rs | 3 | ||||
| -rw-r--r-- | src/librustc_trans/trans/common.rs | 6 | ||||
| -rw-r--r-- | src/librustc_trans/trans/controlflow.rs | 31 |
3 files changed, 13 insertions, 27 deletions
diff --git a/src/librustc_trans/trans/base.rs b/src/librustc_trans/trans/base.rs index 4879975dde6..06841501124 100644 --- a/src/librustc_trans/trans/base.rs +++ b/src/librustc_trans/trans/base.rs @@ -869,8 +869,7 @@ pub fn with_cond<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>, { let _icx = push_ctxt("with_cond"); - if bcx.unreachable.get() || - (common::is_const(val) && common::const_to_uint(val) == 0) { + if bcx.unreachable.get() || common::const_to_opt_uint(val) == Some(0) { return bcx; } diff --git a/src/librustc_trans/trans/common.rs b/src/librustc_trans/trans/common.rs index 03dda57e568..758702f54c0 100644 --- a/src/librustc_trans/trans/common.rs +++ b/src/librustc_trans/trans/common.rs @@ -919,12 +919,6 @@ pub fn const_get_elt(cx: &CrateContext, v: ValueRef, us: &[c_uint]) } } -pub fn is_const(v: ValueRef) -> bool { - unsafe { - llvm::LLVMIsConstant(v) == True - } -} - pub fn const_to_int(v: ValueRef) -> i64 { unsafe { llvm::LLVMConstIntGetSExtValue(v) diff --git a/src/librustc_trans/trans/controlflow.rs b/src/librustc_trans/trans/controlflow.rs index 8cecc39ec39..ab8cfa0ce3b 100644 --- a/src/librustc_trans/trans/controlflow.rs +++ b/src/librustc_trans/trans/controlflow.rs @@ -166,31 +166,24 @@ pub fn trans_if<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, let cond_val = unpack_result!(bcx, expr::trans(bcx, cond).to_llbool()); // Drop branches that are known to be impossible - if is_const(cond_val) && !is_undef(cond_val) { - if const_to_uint(cond_val) == 1 { - match els { - Some(elexpr) => { - let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx }; - trans.visit_expr(&*elexpr); - } - None => {} - } + if let Some(cv) = const_to_opt_uint(cond_val) { + if cv == 1 { // if true { .. } [else { .. }] bcx = trans_block(bcx, &*thn, dest); trans::debuginfo::clear_source_location(bcx.fcx); + + if let Some(elexpr) = els { + let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx }; + trans.visit_expr(&*elexpr); + } } else { - let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx } ; + // if false { .. } [else { .. }] + let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx }; trans.visit_block(&*thn); - match els { - // if false { .. } else { .. } - Some(elexpr) => { - bcx = expr::trans_into(bcx, &*elexpr, dest); - trans::debuginfo::clear_source_location(bcx.fcx); - } - - // if false { .. } - None => { } + if let Some(elexpr) = els { + bcx = expr::trans_into(bcx, &*elexpr, dest); + trans::debuginfo::clear_source_location(bcx.fcx); } } |
