mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-28 15:43:21 +00:00
Simplify MIR generation for logical ops
This commit is contained in:
parent
1897657ef0
commit
e38e954a0d
@ -126,18 +126,17 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
|
|||||||
ExprKind::LogicalOp { op, lhs, rhs } => {
|
ExprKind::LogicalOp { op, lhs, rhs } => {
|
||||||
// And:
|
// And:
|
||||||
//
|
//
|
||||||
// [block: If(lhs)] -true-> [else_block: If(rhs)] -true-> [true_block]
|
// [block: If(lhs)] -true-> [else_block: dest = (rhs)]
|
||||||
// | | (false)
|
// | (false)
|
||||||
// +----------false-----------+------------------> [false_block]
|
// [shortcurcuit_block: dest = false]
|
||||||
//
|
//
|
||||||
// Or:
|
// Or:
|
||||||
//
|
//
|
||||||
// [block: If(lhs)] -false-> [else_block: If(rhs)] -true-> [true_block]
|
// [block: If(lhs)] -false-> [else_block: dest = (rhs)]
|
||||||
// | (true) | (false)
|
// | (true)
|
||||||
// [true_block] [false_block]
|
// [shortcurcuit_block: dest = true]
|
||||||
|
|
||||||
let (true_block, false_block, mut else_block, join_block) = (
|
let (shortcircuit_block, mut else_block, join_block) = (
|
||||||
this.cfg.start_new_block(),
|
|
||||||
this.cfg.start_new_block(),
|
this.cfg.start_new_block(),
|
||||||
this.cfg.start_new_block(),
|
this.cfg.start_new_block(),
|
||||||
this.cfg.start_new_block(),
|
this.cfg.start_new_block(),
|
||||||
@ -145,47 +144,41 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
|
|||||||
|
|
||||||
let lhs = unpack!(block = this.as_local_operand(block, lhs));
|
let lhs = unpack!(block = this.as_local_operand(block, lhs));
|
||||||
let blocks = match op {
|
let blocks = match op {
|
||||||
LogicalOp::And => (else_block, false_block),
|
LogicalOp::And => (else_block, shortcircuit_block),
|
||||||
LogicalOp::Or => (true_block, else_block),
|
LogicalOp::Or => (shortcircuit_block, else_block),
|
||||||
};
|
};
|
||||||
let term = TerminatorKind::if_(this.hir.tcx(), lhs, blocks.0, blocks.1);
|
let term = TerminatorKind::if_(this.hir.tcx(), lhs, blocks.0, blocks.1);
|
||||||
this.cfg.terminate(block, source_info, term);
|
this.cfg.terminate(block, source_info, term);
|
||||||
|
|
||||||
let rhs = unpack!(else_block = this.as_local_operand(else_block, rhs));
|
|
||||||
let term = TerminatorKind::if_(this.hir.tcx(), rhs, true_block, false_block);
|
|
||||||
this.cfg.terminate(else_block, source_info, term);
|
|
||||||
|
|
||||||
this.cfg.push_assign_constant(
|
this.cfg.push_assign_constant(
|
||||||
true_block,
|
shortcircuit_block,
|
||||||
source_info,
|
source_info,
|
||||||
destination,
|
destination,
|
||||||
Constant {
|
Constant {
|
||||||
span: expr_span,
|
span: expr_span,
|
||||||
ty: this.hir.bool_ty(),
|
ty: this.hir.bool_ty(),
|
||||||
user_ty: None,
|
user_ty: None,
|
||||||
literal: this.hir.true_literal(),
|
literal: match op {
|
||||||
|
LogicalOp::And => this.hir.false_literal(),
|
||||||
|
LogicalOp::Or => this.hir.true_literal(),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
this.cfg.push_assign_constant(
|
|
||||||
false_block,
|
|
||||||
source_info,
|
|
||||||
destination,
|
|
||||||
Constant {
|
|
||||||
span: expr_span,
|
|
||||||
ty: this.hir.bool_ty(),
|
|
||||||
user_ty: None,
|
|
||||||
literal: this.hir.false_literal(),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
this.cfg.terminate(
|
this.cfg.terminate(
|
||||||
true_block,
|
shortcircuit_block,
|
||||||
source_info,
|
source_info,
|
||||||
TerminatorKind::Goto { target: join_block },
|
TerminatorKind::Goto { target: join_block },
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let rhs = unpack!(else_block = this.as_local_operand(else_block, rhs));
|
||||||
|
this.cfg.push_assign(
|
||||||
|
else_block,
|
||||||
|
source_info,
|
||||||
|
destination,
|
||||||
|
Rvalue::Use(rhs),
|
||||||
|
);
|
||||||
this.cfg.terminate(
|
this.cfg.terminate(
|
||||||
false_block,
|
else_block,
|
||||||
source_info,
|
source_info,
|
||||||
TerminatorKind::Goto { target: join_block },
|
TerminatorKind::Goto { target: join_block },
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user