Skip to content

Commit caf9f95

Browse files
committed
If is now always a SwitchInt in MIR
1 parent c68e963 commit caf9f95

File tree

18 files changed

+76
-101
lines changed

18 files changed

+76
-101
lines changed

src/librustc/mir/mod.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,6 @@ pub enum TerminatorKind<'tcx> {
453453
target: BasicBlock,
454454
},
455455

456-
/// jump to branch 0 if this lvalue evaluates to true
457-
If {
458-
cond: Operand<'tcx>,
459-
targets: (BasicBlock, BasicBlock),
460-
},
461-
462456
/// lvalue evaluates to some enum; jump depending on the branch
463457
Switch {
464458
discr: Lvalue<'tcx>,
@@ -470,7 +464,7 @@ pub enum TerminatorKind<'tcx> {
470464
/// to one of the targets, and otherwise fallback to `otherwise`
471465
SwitchInt {
472466
/// discriminant value being tested
473-
discr: Lvalue<'tcx>,
467+
discr: Operand<'tcx>,
474468

475469
/// type of value being tested
476470
switch_ty: Ty<'tcx>,
@@ -550,7 +544,6 @@ impl<'tcx> TerminatorKind<'tcx> {
550544
use self::TerminatorKind::*;
551545
match *self {
552546
Goto { target: ref b } => slice::ref_slice(b).into_cow(),
553-
If { targets: (b1, b2), .. } => vec![b1, b2].into_cow(),
554547
Switch { targets: ref b, .. } => b[..].into_cow(),
555548
SwitchInt { targets: ref b, .. } => b[..].into_cow(),
556549
Resume => (&[]).into_cow(),
@@ -580,7 +573,6 @@ impl<'tcx> TerminatorKind<'tcx> {
580573
use self::TerminatorKind::*;
581574
match *self {
582575
Goto { target: ref mut b } => vec![b],
583-
If { targets: (ref mut b1, ref mut b2), .. } => vec![b1, b2],
584576
Switch { targets: ref mut b, .. } => b.iter_mut().collect(),
585577
SwitchInt { targets: ref mut b, .. } => b.iter_mut().collect(),
586578
Resume => Vec::new(),
@@ -659,7 +651,6 @@ impl<'tcx> TerminatorKind<'tcx> {
659651
use self::TerminatorKind::*;
660652
match *self {
661653
Goto { .. } => write!(fmt, "goto"),
662-
If { cond: ref lv, .. } => write!(fmt, "if({:?})", lv),
663654
Switch { discr: ref lv, .. } => write!(fmt, "switch({:?})", lv),
664655
SwitchInt { discr: ref lv, .. } => write!(fmt, "switchInt({:?})", lv),
665656
Return => write!(fmt, "return"),
@@ -710,7 +701,6 @@ impl<'tcx> TerminatorKind<'tcx> {
710701
match *self {
711702
Return | Resume | Unreachable => vec![],
712703
Goto { .. } => vec!["".into()],
713-
If { .. } => vec!["true".into(), "false".into()],
714704
Switch { ref adt_def, .. } => {
715705
adt_def.variants
716706
.iter()

src/librustc/mir/visit.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use ty::subst::Substs;
1414
use ty::{ClosureSubsts, Region, Ty};
1515
use mir::*;
1616
use rustc_const_math::ConstUsize;
17-
use rustc_data_structures::tuple_slice::TupleSlice;
1817
use rustc_data_structures::indexed_vec::Idx;
1918
use syntax_pos::Span;
2019

@@ -363,14 +362,6 @@ macro_rules! make_mir_visitor {
363362
self.visit_branch(block, target);
364363
}
365364

366-
TerminatorKind::If { ref $($mutability)* cond,
367-
ref $($mutability)* targets } => {
368-
self.visit_operand(cond, source_location);
369-
for &target in targets.as_slice() {
370-
self.visit_branch(block, target);
371-
}
372-
}
373-
374365
TerminatorKind::Switch { ref $($mutability)* discr,
375366
adt_def: _,
376367
ref targets } => {
@@ -384,7 +375,7 @@ macro_rules! make_mir_visitor {
384375
ref $($mutability)* switch_ty,
385376
ref $($mutability)* values,
386377
ref targets } => {
387-
self.visit_lvalue(discr, LvalueContext::Inspect, source_location);
378+
self.visit_operand(discr, source_location);
388379
self.visit_ty(switch_ty);
389380
for value in values {
390381
self.visit_const_val(value, source_location);

src/librustc_borrowck/borrowck/mir/dataflow/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,6 @@ impl<'a, 'tcx: 'a, D> DataflowAnalysis<'a, 'tcx, D>
454454
self.propagate_bits_into_entry_set_for(in_out, changed, target);
455455
self.propagate_bits_into_entry_set_for(in_out, changed, unwind);
456456
}
457-
mir::TerminatorKind::If { ref targets, .. } => {
458-
self.propagate_bits_into_entry_set_for(in_out, changed, &targets.0);
459-
self.propagate_bits_into_entry_set_for(in_out, changed, &targets.1);
460-
}
461457
mir::TerminatorKind::Switch { ref targets, .. } |
462458
mir::TerminatorKind::SwitchInt { ref targets, .. } => {
463459
for target in targets {

src/librustc_borrowck/borrowck/mir/elaborate_drops.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -813,9 +813,12 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
813813
(true, false) => on_set,
814814
(true, true) => {
815815
let flag = self.drop_flag(c.path).unwrap();
816-
self.new_block(c, is_cleanup, TerminatorKind::If {
817-
cond: Operand::Consume(flag),
818-
targets: (on_set, on_unset)
816+
let boolty = self.tcx.types.bool;
817+
self.new_block(c, is_cleanup, TerminatorKind::SwitchInt {
818+
discr: Operand::Consume(flag),
819+
switch_ty: boolty,
820+
values: vec![ConstVal::Bool(true)],
821+
targets: vec![on_set, on_unset],
819822
})
820823
}
821824
}

src/librustc_borrowck/borrowck/mir/gather_moves.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,6 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
464464
self.gather_move(loc, &Lvalue::Local(RETURN_POINTER));
465465
}
466466

467-
TerminatorKind::If { .. } |
468467
TerminatorKind::Assert { .. } |
469468
TerminatorKind::SwitchInt { .. } |
470469
TerminatorKind::Switch { .. } => {

src/librustc_mir/build/expr/into.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use build::expr::category::{Category, RvalueFunc};
1515
use hair::*;
1616
use rustc::ty;
1717
use rustc::mir::*;
18+
use rustc::middle::const_val::ConstVal;
1819

1920
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
2021
/// Compile `expr`, storing the result into `destination`, which
@@ -69,9 +70,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
6970

7071
let mut then_block = this.cfg.start_new_block();
7172
let mut else_block = this.cfg.start_new_block();
72-
this.cfg.terminate(block, source_info, TerminatorKind::If {
73-
cond: operand,
74-
targets: (then_block, else_block)
73+
this.cfg.terminate(block, source_info, TerminatorKind::SwitchInt {
74+
discr: operand,
75+
switch_ty: this.hir.bool_ty(),
76+
values: vec![ConstVal::Bool(true)],
77+
targets: vec![then_block, else_block],
7578
});
7679

7780
unpack!(then_block = this.into(destination, then_block, then_expr));
@@ -111,16 +114,22 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
111114

112115
let lhs = unpack!(block = this.as_operand(block, lhs));
113116
let blocks = match op {
114-
LogicalOp::And => (else_block, false_block),
115-
LogicalOp::Or => (true_block, else_block),
117+
LogicalOp::And => vec![else_block, false_block],
118+
LogicalOp::Or => vec![true_block, else_block],
116119
};
117-
this.cfg.terminate(block, source_info,
118-
TerminatorKind::If { cond: lhs, targets: blocks });
120+
this.cfg.terminate(block, source_info, TerminatorKind::SwitchInt {
121+
discr: lhs,
122+
switch_ty: this.hir.bool_ty(),
123+
values: vec![ConstVal::Bool(true)],
124+
targets: blocks,
125+
});
119126

120127
let rhs = unpack!(else_block = this.as_operand(else_block, rhs));
121-
this.cfg.terminate(else_block, source_info, TerminatorKind::If {
122-
cond: rhs,
123-
targets: (true_block, false_block)
128+
this.cfg.terminate(else_block, source_info, TerminatorKind::SwitchInt {
129+
discr: rhs,
130+
switch_ty: this.hir.bool_ty(),
131+
values: vec![ConstVal::Bool(true)],
132+
targets: vec![true_block, false_block],
124133
});
125134

126135
this.cfg.push_assign_constant(
@@ -180,9 +189,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
180189
loop_block_end = this.as_operand(loop_block, cond_expr));
181190
body_block = this.cfg.start_new_block();
182191
this.cfg.terminate(loop_block_end, source_info,
183-
TerminatorKind::If {
184-
cond: cond,
185-
targets: (body_block, exit_block)
192+
TerminatorKind::SwitchInt {
193+
discr: cond,
194+
switch_ty: this.hir.bool_ty(),
195+
values: vec![ConstVal::Bool(true)],
196+
targets: vec![body_block, exit_block],
186197
});
187198

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

src/librustc_mir/build/matches/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,12 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
672672
let source_info = self.source_info(guard.span);
673673
let cond = unpack!(block = self.as_operand(block, guard));
674674
let otherwise = self.cfg.start_new_block();
675-
self.cfg.terminate(block, source_info,
676-
TerminatorKind::If { cond: cond,
677-
targets: (arm_block, otherwise)});
675+
self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt {
676+
discr: cond,
677+
switch_ty: self.hir.bool_ty(),
678+
values: vec![ConstVal::Bool(true)],
679+
targets: vec![arm_block, otherwise],
680+
});
678681
Some(otherwise)
679682
} else {
680683
let source_info = self.source_info(candidate.span);

src/librustc_mir/build/matches/test.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
221221
v => span_bug!(test.span, "expected boolean value but got {:?}", v)
222222
};
223223

224-
(targets,
225-
TerminatorKind::If {
226-
cond: Operand::Consume(lvalue.clone()),
227-
targets: (true_bb, else_bb)
224+
(targets, TerminatorKind::SwitchInt {
225+
discr: Operand::Consume(lvalue.clone()),
226+
switch_ty: self.hir.bool_ty(),
227+
values: vec![ConstVal::Bool(true)],
228+
targets: vec![true_bb, else_bb]
228229
})
229230

230231
}
@@ -240,7 +241,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
240241

241242
(targets.clone(),
242243
TerminatorKind::SwitchInt {
243-
discr: lvalue.clone(),
244+
discr: Operand::Consume(lvalue.clone()),
244245
switch_ty: switch_ty,
245246
values: options.clone(),
246247
targets: targets
@@ -314,9 +315,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
314315

315316
// check the result
316317
let block = self.cfg.start_new_block();
317-
self.cfg.terminate(eq_block, source_info, TerminatorKind::If {
318-
cond: Operand::Consume(eq_result),
319-
targets: (block, fail),
318+
self.cfg.terminate(eq_block, source_info, TerminatorKind::SwitchInt {
319+
discr: Operand::Consume(eq_result),
320+
switch_ty: self.hir.bool_ty(),
321+
values: vec![ConstVal::Bool(true)],
322+
targets: vec![block, fail],
320323
});
321324

322325
vec![block, fail]
@@ -362,9 +365,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
362365
// branch based on result
363366
let target_blocks: Vec<_> = vec![self.cfg.start_new_block(),
364367
self.cfg.start_new_block()];
365-
self.cfg.terminate(block, source_info, TerminatorKind::If {
366-
cond: Operand::Consume(result),
367-
targets: (target_blocks[0], target_blocks[1])
368+
self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt {
369+
discr: Operand::Consume(result),
370+
switch_ty: self.hir.bool_ty(),
371+
values: vec![ConstVal::Bool(true)],
372+
targets: target_blocks.clone(),
368373
});
369374

370375
target_blocks
@@ -389,9 +394,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
389394

390395
// branch based on result
391396
let target_block = self.cfg.start_new_block();
392-
self.cfg.terminate(block, source_info, TerminatorKind::If {
393-
cond: Operand::Consume(result),
394-
targets: (target_block, fail_block)
397+
self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt {
398+
discr: Operand::Consume(result),
399+
switch_ty: self.hir.bool_ty(),
400+
values: vec![ConstVal::Bool(true)],
401+
targets: vec![target_block, fail_block]
395402
});
396403

397404
target_block

src/librustc_mir/hair/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ pub enum ExprKind<'tcx> {
134134
op: LogicalOp,
135135
lhs: ExprRef<'tcx>,
136136
rhs: ExprRef<'tcx>,
137-
},
137+
}, // NOT overloaded!
138+
// LogicalOp is distinct from BinaryOp because of lazy evaluation of the operands.
138139
Unary {
139140
op: UnOp,
140141
arg: ExprRef<'tcx>,

src/librustc_mir/transform/no_landing_pads.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ impl<'tcx> MutVisitor<'tcx> for NoLandingPads {
2828
TerminatorKind::Resume |
2929
TerminatorKind::Return |
3030
TerminatorKind::Unreachable |
31-
TerminatorKind::If { .. } |
3231
TerminatorKind::Switch { .. } |
3332
TerminatorKind::SwitchInt { .. } => {
3433
/* nothing to do */

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
394394
return Qualif::empty();
395395
}
396396

397-
TerminatorKind::If {..} |
398397
TerminatorKind::Switch {..} |
399398
TerminatorKind::SwitchInt {..} |
400399
TerminatorKind::DropAndReplace { .. } |

src/librustc_mir/transform/simplify.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ impl<'a, 'tcx: 'a> CfgSimplifier<'a, 'tcx> {
209209
// turn a branch with all successors identical to a goto
210210
fn simplify_branch(&mut self, terminator: &mut Terminator<'tcx>) -> bool {
211211
match terminator.kind {
212-
TerminatorKind::If { .. } |
213212
TerminatorKind::Switch { .. } |
214213
TerminatorKind::SwitchInt { .. } => {},
215214
_ => return false

src/librustc_mir/transform/simplify_branches.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ impl<'l, 'tcx> MirPass<'tcx> for SimplifyBranches<'l> {
3030
for block in mir.basic_blocks_mut() {
3131
let terminator = block.terminator_mut();
3232
terminator.kind = match terminator.kind {
33-
TerminatorKind::If { ref targets, cond: Operand::Constant(Constant {
34-
literal: Literal::Value {
35-
value: ConstVal::Bool(cond)
36-
}, ..
37-
}) } => {
38-
if cond {
39-
TerminatorKind::Goto { target: targets.0 }
40-
} else {
41-
TerminatorKind::Goto { target: targets.1 }
42-
}
43-
}
33+
// TerminatorKind::If { ref targets, cond: Operand::Constant(Constant {
34+
// literal: Literal::Value {
35+
// value: ConstVal::Bool(cond)
36+
// }, ..
37+
// }) } => {
38+
// if cond {
39+
// TerminatorKind::Goto { target: targets.0 }
40+
// } else {
41+
// TerminatorKind::Goto { target: targets.1 }
42+
// }
43+
// }
4444

4545
TerminatorKind::Assert { target, cond: Operand::Constant(Constant {
4646
literal: Literal::Value {

src/librustc_mir/transform/type_check.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -423,18 +423,8 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
423423
lv_ty, rv_ty, terr);
424424
}
425425
}
426-
427-
TerminatorKind::If { ref cond, .. } => {
428-
let cond_ty = cond.ty(mir, tcx);
429-
match cond_ty.sty {
430-
ty::TyBool => {}
431-
_ => {
432-
span_mirbug!(self, term, "bad If ({:?}, not bool", cond_ty);
433-
}
434-
}
435-
}
436426
TerminatorKind::SwitchInt { ref discr, switch_ty, .. } => {
437-
let discr_ty = discr.ty(mir, tcx).to_ty(tcx);
427+
let discr_ty = discr.ty(mir, tcx);
438428
if let Err(terr) = self.sub_types(discr_ty, switch_ty) {
439429
span_mirbug!(self, term, "bad SwitchInt ({:?} on {:?}): {:?}",
440430
switch_ty, discr_ty, terr);
@@ -603,10 +593,6 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
603593
match block.terminator().kind {
604594
TerminatorKind::Goto { target } =>
605595
self.assert_iscleanup(mir, block, target, is_cleanup),
606-
TerminatorKind::If { targets: (on_true, on_false), .. } => {
607-
self.assert_iscleanup(mir, block, on_true, is_cleanup);
608-
self.assert_iscleanup(mir, block, on_false, is_cleanup);
609-
}
610596
TerminatorKind::Switch { ref targets, .. } |
611597
TerminatorKind::SwitchInt { ref targets, .. } => {
612598
for target in targets {

src/librustc_passes/mir_stats.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> {
148148
self.record("TerminatorKind", kind);
149149
self.record(match *kind {
150150
TerminatorKind::Goto { .. } => "TerminatorKind::Goto",
151-
TerminatorKind::If { .. } => "TerminatorKind::If",
152151
TerminatorKind::Switch { .. } => "TerminatorKind::Switch",
153152
TerminatorKind::SwitchInt { .. } => "TerminatorKind::SwitchInt",
154153
TerminatorKind::Resume => "TerminatorKind::Resume",

src/librustc_trans/mir/analyze.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ pub fn cleanup_kinds<'a, 'tcx>(mir: &mir::Mir<'tcx>) -> IndexVec<mir::BasicBlock
204204
TerminatorKind::Resume |
205205
TerminatorKind::Return |
206206
TerminatorKind::Unreachable |
207-
TerminatorKind::If { .. } |
208207
TerminatorKind::Switch { .. } |
209208
TerminatorKind::SwitchInt { .. } => {
210209
/* nothing to do */

0 commit comments

Comments
 (0)