Skip to content

Commit 4e7770e

Browse files
committed
Prefer switching on false for boolean switches
This ends up not really mattering because we generate a plain conditional branch in LLVM either way.
1 parent ff167a6 commit 4e7770e

File tree

6 files changed

+21
-24
lines changed

6 files changed

+21
-24
lines changed

src/librustc/middle/const_val.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,13 @@
1111
use syntax::symbol::InternedString;
1212
use syntax::ast;
1313
use std::rc::Rc;
14-
use std::borrow::Cow;
1514
use hir::def_id::DefId;
1615
use rustc_const_math::*;
1716
use self::ConstVal::*;
1817
pub use rustc_const_math::ConstInt;
1918

2019
use std::collections::BTreeMap;
2120

22-
pub static BOOL_SWITCH_TRUE: Cow<'static, [ConstInt]> = Cow::Borrowed(&[ConstInt::Infer(1)]);
23-
2421
#[derive(Clone, Debug, Hash, RustcEncodable, RustcDecodable, Eq, PartialEq)]
2522
pub enum ConstVal {
2623
Float(ConstFloat),

src/librustc/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ pub struct Terminator<'tcx> {
447447
}
448448

449449
/// For use in SwitchInt, for switching on bools.
450-
pub static BOOL_SWITCH_TRUE: Cow<'static, [ConstInt]> = Cow::Borrowed(&[ConstInt::Infer(1)]);
450+
pub static BOOL_SWITCH_FALSE: Cow<'static, [ConstInt]> = Cow::Borrowed(&[ConstInt::Infer(0)]);
451451

452452
#[derive(Clone, RustcEncodable, RustcDecodable)]
453453
pub enum TerminatorKind<'tcx> {

src/librustc_borrowck/borrowck/mir/elaborate_drops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,8 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
846846
self.new_block(c, is_cleanup, TerminatorKind::SwitchInt {
847847
discr: Operand::Consume(flag),
848848
switch_ty: boolty,
849-
values: BOOL_SWITCH_TRUE.clone(),
850-
targets: vec![on_set, on_unset],
849+
values: BOOL_SWITCH_FALSE.clone(),
850+
targets: vec![on_unset, on_set],
851851
})
852852
}
853853
}

src/librustc_mir/build/expr/into.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
7272
this.cfg.terminate(block, source_info, TerminatorKind::SwitchInt {
7373
discr: operand,
7474
switch_ty: this.hir.bool_ty(),
75-
values: BOOL_SWITCH_TRUE.clone(),
76-
targets: vec![then_block, else_block],
75+
values: BOOL_SWITCH_FALSE.clone(),
76+
targets: vec![else_block, then_block],
7777
});
7878

7979
unpack!(then_block = this.into(destination, then_block, then_expr));
@@ -113,22 +113,22 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
113113

114114
let lhs = unpack!(block = this.as_operand(block, lhs));
115115
let blocks = match op {
116-
LogicalOp::And => vec![else_block, false_block],
117-
LogicalOp::Or => vec![true_block, else_block],
116+
LogicalOp::And => vec![false_block, else_block],
117+
LogicalOp::Or => vec![else_block, true_block],
118118
};
119119
this.cfg.terminate(block, source_info, TerminatorKind::SwitchInt {
120120
discr: lhs,
121121
switch_ty: this.hir.bool_ty(),
122-
values: BOOL_SWITCH_TRUE.clone(),
122+
values: BOOL_SWITCH_FALSE.clone(),
123123
targets: blocks,
124124
});
125125

126126
let rhs = unpack!(else_block = this.as_operand(else_block, rhs));
127127
this.cfg.terminate(else_block, source_info, TerminatorKind::SwitchInt {
128128
discr: rhs,
129129
switch_ty: this.hir.bool_ty(),
130-
values: BOOL_SWITCH_TRUE.clone(),
131-
targets: vec![true_block, false_block],
130+
values: BOOL_SWITCH_FALSE.clone(),
131+
targets: vec![false_block, true_block],
132132
});
133133

134134
this.cfg.push_assign_constant(
@@ -191,8 +191,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
191191
TerminatorKind::SwitchInt {
192192
discr: cond,
193193
switch_ty: this.hir.bool_ty(),
194-
values: BOOL_SWITCH_TRUE.clone(),
195-
targets: vec![body_block, exit_block],
194+
values: BOOL_SWITCH_FALSE.clone(),
195+
targets: vec![exit_block, body_block],
196196
});
197197

198198
// if the test is false, there's no `break` to assign `destination`, so

src/librustc_mir/build/matches/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
675675
self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt {
676676
discr: cond,
677677
switch_ty: self.hir.bool_ty(),
678-
values: BOOL_SWITCH_TRUE.clone(),
679-
targets: vec![arm_block, otherwise],
678+
values: BOOL_SWITCH_FALSE.clone(),
679+
targets: vec![otherwise, arm_block],
680680
});
681681
Some(otherwise)
682682
} else {

src/librustc_mir/build/matches/test.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
236236
&ConstVal::Bool(false) => vec![false_bb, true_bb],
237237
v => span_bug!(test.span, "expected boolean value but got {:?}", v)
238238
};
239-
(BOOL_SWITCH_TRUE.clone(), vec![true_bb, false_bb], ret)
239+
(BOOL_SWITCH_FALSE.clone(), vec![false_bb, true_bb], ret)
240240
} else {
241241
// The switch may be inexhaustive so we
242242
// add a catch all block
@@ -326,8 +326,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
326326
self.cfg.terminate(eq_block, source_info, TerminatorKind::SwitchInt {
327327
discr: Operand::Consume(eq_result),
328328
switch_ty: self.hir.bool_ty(),
329-
values: BOOL_SWITCH_TRUE.clone(),
330-
targets: vec![block, fail],
329+
values: BOOL_SWITCH_FALSE.clone(),
330+
targets: vec![fail, block],
331331
});
332332
vec![block, fail]
333333
} else {
@@ -375,8 +375,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
375375
self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt {
376376
discr: Operand::Consume(result),
377377
switch_ty: self.hir.bool_ty(),
378-
values: BOOL_SWITCH_TRUE.clone(),
379-
targets: vec![true_bb, false_bb],
378+
values: BOOL_SWITCH_FALSE.clone(),
379+
targets: vec![false_bb, true_bb],
380380
});
381381
vec![true_bb, false_bb]
382382
}
@@ -403,8 +403,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
403403
self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt {
404404
discr: Operand::Consume(result),
405405
switch_ty: self.hir.bool_ty(),
406-
values: BOOL_SWITCH_TRUE.clone(),
407-
targets: vec![target_block, fail_block]
406+
values: BOOL_SWITCH_FALSE.clone(),
407+
targets: vec![fail_block, target_block]
408408
});
409409
target_block
410410
}

0 commit comments

Comments
 (0)