diff options
| author | Simonas Kazlauskas <git@kazlauskas.me> | 2017-02-02 22:03:23 +0200 |
|---|---|---|
| committer | Simonas Kazlauskas <git@kazlauskas.me> | 2017-02-10 19:44:00 +0200 |
| commit | 8e00d28ff43e04b7cfcfe7445061b73985f9f1b8 (patch) | |
| tree | 747f9c497c29cbcc25531db3e9975be9484807e1 | |
| parent | 4be18488a741b2bf9b6f32c0ae5b21f4c3f6c83e (diff) | |
| download | rust-8e00d28ff43e04b7cfcfe7445061b73985f9f1b8.tar.gz rust-8e00d28ff43e04b7cfcfe7445061b73985f9f1b8.zip | |
Prefer switching on false for boolean switches
This ends up not really mattering because we generate a plain conditional branch in LLVM either way.
| -rw-r--r-- | src/librustc/middle/const_val.rs | 3 | ||||
| -rw-r--r-- | src/librustc/mir/mod.rs | 2 | ||||
| -rw-r--r-- | src/librustc_borrowck/borrowck/mir/elaborate_drops.rs | 4 | ||||
| -rw-r--r-- | src/librustc_mir/build/expr/into.rs | 18 | ||||
| -rw-r--r-- | src/librustc_mir/build/matches/mod.rs | 4 | ||||
| -rw-r--r-- | src/librustc_mir/build/matches/test.rs | 14 |
6 files changed, 21 insertions, 24 deletions
diff --git a/src/librustc/middle/const_val.rs b/src/librustc/middle/const_val.rs index f885a6d9569..11919db479c 100644 --- a/src/librustc/middle/const_val.rs +++ b/src/librustc/middle/const_val.rs @@ -11,7 +11,6 @@ use syntax::symbol::InternedString; use syntax::ast; use std::rc::Rc; -use std::borrow::Cow; use hir::def_id::DefId; use rustc_const_math::*; use self::ConstVal::*; @@ -19,8 +18,6 @@ pub use rustc_const_math::ConstInt; use std::collections::BTreeMap; -pub static BOOL_SWITCH_TRUE: Cow<'static, [ConstInt]> = Cow::Borrowed(&[ConstInt::Infer(1)]); - #[derive(Clone, Debug, Hash, RustcEncodable, RustcDecodable, Eq, PartialEq)] pub enum ConstVal { Float(ConstFloat), diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index 5657ec157e8..98693d469ed 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -447,7 +447,7 @@ pub struct Terminator<'tcx> { } /// For use in SwitchInt, for switching on bools. -pub static BOOL_SWITCH_TRUE: Cow<'static, [ConstInt]> = Cow::Borrowed(&[ConstInt::Infer(1)]); +pub static BOOL_SWITCH_FALSE: Cow<'static, [ConstInt]> = Cow::Borrowed(&[ConstInt::Infer(0)]); #[derive(Clone, RustcEncodable, RustcDecodable)] pub enum TerminatorKind<'tcx> { diff --git a/src/librustc_borrowck/borrowck/mir/elaborate_drops.rs b/src/librustc_borrowck/borrowck/mir/elaborate_drops.rs index 144b0c2203a..44b85c31d86 100644 --- a/src/librustc_borrowck/borrowck/mir/elaborate_drops.rs +++ b/src/librustc_borrowck/borrowck/mir/elaborate_drops.rs @@ -846,8 +846,8 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> { self.new_block(c, is_cleanup, TerminatorKind::SwitchInt { discr: Operand::Consume(flag), switch_ty: boolty, - values: BOOL_SWITCH_TRUE.clone(), - targets: vec![on_set, on_unset], + values: BOOL_SWITCH_FALSE.clone(), + targets: vec![on_unset, on_set], }) } } diff --git a/src/librustc_mir/build/expr/into.rs b/src/librustc_mir/build/expr/into.rs index 537daa4a15b..f61b4a66077 100644 --- a/src/librustc_mir/build/expr/into.rs +++ b/src/librustc_mir/build/expr/into.rs @@ -72,8 +72,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { this.cfg.terminate(block, source_info, TerminatorKind::SwitchInt { discr: operand, switch_ty: this.hir.bool_ty(), - values: BOOL_SWITCH_TRUE.clone(), - targets: vec![then_block, else_block], + values: BOOL_SWITCH_FALSE.clone(), + targets: vec![else_block, then_block], }); unpack!(then_block = this.into(destination, then_block, then_expr)); @@ -113,13 +113,13 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { let lhs = unpack!(block = this.as_operand(block, lhs)); let blocks = match op { - LogicalOp::And => vec![else_block, false_block], - LogicalOp::Or => vec![true_block, else_block], + LogicalOp::And => vec![false_block, else_block], + LogicalOp::Or => vec![else_block, true_block], }; this.cfg.terminate(block, source_info, TerminatorKind::SwitchInt { discr: lhs, switch_ty: this.hir.bool_ty(), - values: BOOL_SWITCH_TRUE.clone(), + values: BOOL_SWITCH_FALSE.clone(), targets: blocks, }); @@ -127,8 +127,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { this.cfg.terminate(else_block, source_info, TerminatorKind::SwitchInt { discr: rhs, switch_ty: this.hir.bool_ty(), - values: BOOL_SWITCH_TRUE.clone(), - targets: vec![true_block, false_block], + values: BOOL_SWITCH_FALSE.clone(), + targets: vec![false_block, true_block], }); this.cfg.push_assign_constant( @@ -191,8 +191,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { TerminatorKind::SwitchInt { discr: cond, switch_ty: this.hir.bool_ty(), - values: BOOL_SWITCH_TRUE.clone(), - targets: vec![body_block, exit_block], + values: BOOL_SWITCH_FALSE.clone(), + targets: vec![exit_block, body_block], }); // if the test is false, there's no `break` to assign `destination`, so diff --git a/src/librustc_mir/build/matches/mod.rs b/src/librustc_mir/build/matches/mod.rs index 885965da1f9..812900b6bec 100644 --- a/src/librustc_mir/build/matches/mod.rs +++ b/src/librustc_mir/build/matches/mod.rs @@ -675,8 +675,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt { discr: cond, switch_ty: self.hir.bool_ty(), - values: BOOL_SWITCH_TRUE.clone(), - targets: vec![arm_block, otherwise], + values: BOOL_SWITCH_FALSE.clone(), + targets: vec![otherwise, arm_block], }); Some(otherwise) } else { diff --git a/src/librustc_mir/build/matches/test.rs b/src/librustc_mir/build/matches/test.rs index d9c2e6bb090..f268eda4c15 100644 --- a/src/librustc_mir/build/matches/test.rs +++ b/src/librustc_mir/build/matches/test.rs @@ -236,7 +236,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { &ConstVal::Bool(false) => vec![false_bb, true_bb], v => span_bug!(test.span, "expected boolean value but got {:?}", v) }; - (BOOL_SWITCH_TRUE.clone(), vec![true_bb, false_bb], ret) + (BOOL_SWITCH_FALSE.clone(), vec![false_bb, true_bb], ret) } else { // The switch may be inexhaustive so we // add a catch all block @@ -326,8 +326,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { self.cfg.terminate(eq_block, source_info, TerminatorKind::SwitchInt { discr: Operand::Consume(eq_result), switch_ty: self.hir.bool_ty(), - values: BOOL_SWITCH_TRUE.clone(), - targets: vec![block, fail], + values: BOOL_SWITCH_FALSE.clone(), + targets: vec![fail, block], }); vec![block, fail] } else { @@ -375,8 +375,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt { discr: Operand::Consume(result), switch_ty: self.hir.bool_ty(), - values: BOOL_SWITCH_TRUE.clone(), - targets: vec![true_bb, false_bb], + values: BOOL_SWITCH_FALSE.clone(), + targets: vec![false_bb, true_bb], }); vec![true_bb, false_bb] } @@ -403,8 +403,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt { discr: Operand::Consume(result), switch_ty: self.hir.bool_ty(), - values: BOOL_SWITCH_TRUE.clone(), - targets: vec![target_block, fail_block] + values: BOOL_SWITCH_FALSE.clone(), + targets: vec![fail_block, target_block] }); target_block } |
