Prefer switching on false for boolean switches
This ends up not really mattering because we generate a plain conditional branch in LLVM either way.
This commit is contained in:
parent
4be18488a7
commit
8e00d28ff4
@ -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),
|
||||
|
@ -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> {
|
||||
|
@ -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],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user