Skip to content

Commit 6ce5ecb

Browse files
authoredDec 23, 2018
Rollup merge of rust-lang#56917 - sinkuu:mir_build_logicop, r=davidtwco
Simplify MIR generation for logical operations Reduces one block and one branch from MIR generated for a logical operator.
2 parents 84bc34e + e38e954 commit 6ce5ecb

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed
 

‎src/librustc_mir/build/expr/into.rs

+23-30
Original file line numberDiff line numberDiff line change
@@ -126,66 +126,59 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
126126
ExprKind::LogicalOp { op, lhs, rhs } => {
127127
// And:
128128
//
129-
// [block: If(lhs)] -true-> [else_block: If(rhs)] -true-> [true_block]
130-
// | | (false)
131-
// +----------false-----------+------------------> [false_block]
129+
// [block: If(lhs)] -true-> [else_block: dest = (rhs)]
130+
// | (false)
131+
// [shortcurcuit_block: dest = false]
132132
//
133133
// Or:
134134
//
135-
// [block: If(lhs)] -false-> [else_block: If(rhs)] -true-> [true_block]
136-
// | (true) | (false)
137-
// [true_block] [false_block]
135+
// [block: If(lhs)] -false-> [else_block: dest = (rhs)]
136+
// | (true)
137+
// [shortcurcuit_block: dest = true]
138138

139-
let (true_block, false_block, mut else_block, join_block) = (
140-
this.cfg.start_new_block(),
139+
let (shortcircuit_block, mut else_block, join_block) = (
141140
this.cfg.start_new_block(),
142141
this.cfg.start_new_block(),
143142
this.cfg.start_new_block(),
144143
);
145144

146145
let lhs = unpack!(block = this.as_local_operand(block, lhs));
147146
let blocks = match op {
148-
LogicalOp::And => (else_block, false_block),
149-
LogicalOp::Or => (true_block, else_block),
147+
LogicalOp::And => (else_block, shortcircuit_block),
148+
LogicalOp::Or => (shortcircuit_block, else_block),
150149
};
151150
let term = TerminatorKind::if_(this.hir.tcx(), lhs, blocks.0, blocks.1);
152151
this.cfg.terminate(block, source_info, term);
153152

154-
let rhs = unpack!(else_block = this.as_local_operand(else_block, rhs));
155-
let term = TerminatorKind::if_(this.hir.tcx(), rhs, true_block, false_block);
156-
this.cfg.terminate(else_block, source_info, term);
157-
158153
this.cfg.push_assign_constant(
159-
true_block,
154+
shortcircuit_block,
160155
source_info,
161156
destination,
162157
Constant {
163158
span: expr_span,
164159
ty: this.hir.bool_ty(),
165160
user_ty: None,
166-
literal: this.hir.true_literal(),
161+
literal: match op {
162+
LogicalOp::And => this.hir.false_literal(),
163+
LogicalOp::Or => this.hir.true_literal(),
164+
},
167165
},
168166
);
169-
170-
this.cfg.push_assign_constant(
171-
false_block,
167+
this.cfg.terminate(
168+
shortcircuit_block,
172169
source_info,
173-
destination,
174-
Constant {
175-
span: expr_span,
176-
ty: this.hir.bool_ty(),
177-
user_ty: None,
178-
literal: this.hir.false_literal(),
179-
},
170+
TerminatorKind::Goto { target: join_block },
180171
);
181172

182-
this.cfg.terminate(
183-
true_block,
173+
let rhs = unpack!(else_block = this.as_local_operand(else_block, rhs));
174+
this.cfg.push_assign(
175+
else_block,
184176
source_info,
185-
TerminatorKind::Goto { target: join_block },
177+
destination,
178+
Rvalue::Use(rhs),
186179
);
187180
this.cfg.terminate(
188-
false_block,
181+
else_block,
189182
source_info,
190183
TerminatorKind::Goto { target: join_block },
191184
);

0 commit comments

Comments
 (0)
Please sign in to comment.