Skip to content

Commit 7f3e855

Browse files
committed
Use record_operands_moved more aggresively
1 parent b766abc commit 7f3e855

13 files changed

+120
-233
lines changed

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use rustc_middle::mir::*;
1111
use rustc_middle::ty::{self, Ty, UpvarSubsts};
1212
use rustc_span::Span;
1313

14+
use std::slice;
15+
1416
impl<'a, 'tcx> Builder<'a, 'tcx> {
1517
/// Returns an rvalue suitable for use until the end of the current
1618
/// scope expression.
@@ -117,7 +119,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
117119
block =
118120
this.into(this.hir.tcx().mk_place_deref(Place::from(result)), block, value)
119121
);
120-
block.and(Rvalue::Use(Operand::Move(Place::from(result))))
122+
let result_operand = Operand::Move(Place::from(result));
123+
this.record_operands_moved(slice::from_ref(&result_operand));
124+
block.and(Rvalue::Use(result_operand))
121125
}
122126
ExprKind::Cast { source } => {
123127
let source = unpack!(block = this.as_operand(block, scope, source));
@@ -161,6 +165,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
161165
.map(|f| unpack!(block = this.as_operand(block, scope, f)))
162166
.collect();
163167

168+
this.record_operands_moved(&fields);
164169
block.and(Rvalue::Aggregate(box AggregateKind::Array(el_ty), fields))
165170
}
166171
ExprKind::Tuple { fields } => {
@@ -171,6 +176,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
171176
.map(|f| unpack!(block = this.as_operand(block, scope, f)))
172177
.collect();
173178

179+
this.record_operands_moved(&fields);
174180
block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
175181
}
176182
ExprKind::Closure { closure_id, substs, upvars, movability } => {
@@ -222,6 +228,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
222228
}
223229
UpvarSubsts::Closure(substs) => box AggregateKind::Closure(closure_id, substs),
224230
};
231+
this.record_operands_moved(&operands);
225232
block.and(Rvalue::Aggregate(result, operands))
226233
}
227234
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {

compiler/rustc_mir_build/src/build/expr/into.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use rustc_hir as hir;
1010
use rustc_middle::mir::*;
1111
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation};
1212
use rustc_span::symbol::sym;
13-
1413
use rustc_target::spec::abi::Abi;
1514

15+
use std::slice;
16+
1617
impl<'a, 'tcx> Builder<'a, 'tcx> {
1718
/// Compile `expr`, storing the result into `destination`, which
1819
/// is assumed to be uninitialized.
@@ -271,7 +272,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
271272

272273
let field_names = this.hir.all_fields(adt_def, variant_index);
273274

274-
let fields = if let Some(FruInfo { base, field_types }) = base {
275+
let fields: Vec<_> = if let Some(FruInfo { base, field_types }) = base {
275276
let base = unpack!(block = this.as_place(block, base));
276277

277278
// MIR does not natively support FRU, so for each
@@ -306,6 +307,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
306307
user_ty,
307308
active_field_index,
308309
);
310+
this.record_operands_moved(&fields);
309311
this.cfg.push_assign(
310312
block,
311313
source_info,
@@ -432,6 +434,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
432434
let scope = this.local_scope();
433435
let value = unpack!(block = this.as_operand(block, scope, value));
434436
let resume = this.cfg.start_new_block();
437+
this.record_operands_moved(slice::from_ref(&value));
435438
this.cfg.terminate(
436439
block,
437440
source_info,

compiler/rustc_mir_build/src/build/expr/stmt.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
33
use crate::thir::*;
44
use rustc_middle::middle::region;
55
use rustc_middle::mir::*;
6+
use std::slice;
67

78
impl<'a, 'tcx> Builder<'a, 'tcx> {
89
/// Builds a block of MIR statements to evaluate the THIR `expr`.
@@ -46,6 +47,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4647
if this.hir.needs_drop(lhs.ty) {
4748
let rhs = unpack!(block = this.as_local_operand(block, rhs));
4849
let lhs = unpack!(block = this.as_place(block, lhs));
50+
this.record_operands_moved(slice::from_ref(&rhs));
4951
unpack!(block = this.build_drop_and_replace(block, lhs_span, lhs, rhs));
5052
} else {
5153
let rhs = unpack!(block = this.as_local_rvalue(block, rhs));

compiler/rustc_mir_build/src/build/scope.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ that contains only loops and breakable blocks. It tracks where a `break`,
8383

8484
use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder, CFG};
8585
use crate::thir::{Expr, ExprRef, LintLevel};
86-
use rustc_data_structures::fx::FxHashMap;
86+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8787
use rustc_hir as hir;
8888
use rustc_index::vec::IndexVec;
8989
use rustc_middle::middle::region;
@@ -1379,7 +1379,7 @@ impl<'tcx> DropTreeBuilder<'tcx> for Unwind {
13791379
| TerminatorKind::Yield { .. }
13801380
| TerminatorKind::GeneratorDrop
13811381
| TerminatorKind::FalseEdge { .. }
1382-
| TerminatorKind::InlineAsm {.. } => {
1382+
| TerminatorKind::InlineAsm { .. } => {
13831383
span_bug!(term.source_info.span, "cannot unwind from {:?}", term.kind)
13841384
}
13851385
}

src/test/mir-opt/basic_assignment.main.SimplifyCfg-initial.after.mir

+10-18
Original file line numberDiff line numberDiff line change
@@ -41,44 +41,36 @@ fn main() -> () {
4141
StorageLive(_5); // scope 3 at $DIR/basic_assignment.rs:19:9: 19:15
4242
StorageLive(_6); // scope 4 at $DIR/basic_assignment.rs:23:14: 23:20
4343
_6 = move _4; // scope 4 at $DIR/basic_assignment.rs:23:14: 23:20
44-
replace(_5 <- move _6) -> [return: bb1, unwind: bb5]; // scope 4 at $DIR/basic_assignment.rs:23:5: 23:11
44+
replace(_5 <- move _6) -> [return: bb1, unwind: bb4]; // scope 4 at $DIR/basic_assignment.rs:23:5: 23:11
4545
}
4646

4747
bb1: {
48-
drop(_6) -> [return: bb2, unwind: bb6]; // scope 4 at $DIR/basic_assignment.rs:23:19: 23:20
49-
}
50-
51-
bb2: {
5248
StorageDead(_6); // scope 4 at $DIR/basic_assignment.rs:23:19: 23:20
5349
_0 = const (); // scope 0 at $DIR/basic_assignment.rs:10:11: 24:2
54-
drop(_5) -> [return: bb3, unwind: bb7]; // scope 3 at $DIR/basic_assignment.rs:24:1: 24:2
50+
drop(_5) -> [return: bb2, unwind: bb5]; // scope 3 at $DIR/basic_assignment.rs:24:1: 24:2
5551
}
5652

57-
bb3: {
53+
bb2: {
5854
StorageDead(_5); // scope 3 at $DIR/basic_assignment.rs:24:1: 24:2
59-
drop(_4) -> [return: bb4, unwind: bb8]; // scope 2 at $DIR/basic_assignment.rs:24:1: 24:2
55+
drop(_4) -> [return: bb3, unwind: bb6]; // scope 2 at $DIR/basic_assignment.rs:24:1: 24:2
6056
}
6157

62-
bb4: {
58+
bb3: {
6359
StorageDead(_4); // scope 2 at $DIR/basic_assignment.rs:24:1: 24:2
6460
StorageDead(_2); // scope 1 at $DIR/basic_assignment.rs:24:1: 24:2
6561
StorageDead(_1); // scope 0 at $DIR/basic_assignment.rs:24:1: 24:2
6662
return; // scope 0 at $DIR/basic_assignment.rs:24:2: 24:2
6763
}
6864

69-
bb5 (cleanup): {
70-
drop(_6) -> bb6; // scope 4 at $DIR/basic_assignment.rs:23:19: 23:20
71-
}
72-
73-
bb6 (cleanup): {
74-
drop(_5) -> bb7; // scope 3 at $DIR/basic_assignment.rs:24:1: 24:2
65+
bb4 (cleanup): {
66+
drop(_5) -> bb5; // scope 3 at $DIR/basic_assignment.rs:24:1: 24:2
7567
}
7668

77-
bb7 (cleanup): {
78-
drop(_4) -> bb8; // scope 2 at $DIR/basic_assignment.rs:24:1: 24:2
69+
bb5 (cleanup): {
70+
drop(_4) -> bb6; // scope 2 at $DIR/basic_assignment.rs:24:1: 24:2
7971
}
8072

81-
bb8 (cleanup): {
73+
bb6 (cleanup): {
8274
resume; // scope 0 at $DIR/basic_assignment.rs:10:1: 24:2
8375
}
8476
}

src/test/mir-opt/box_expr.main.ElaborateDrops.before.mir

+10-18
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,45 @@ fn main() -> () {
1414
StorageLive(_1); // scope 0 at $DIR/box_expr.rs:7:9: 7:10
1515
StorageLive(_2); // scope 0 at $DIR/box_expr.rs:7:13: 7:25
1616
_2 = Box(S); // scope 0 at $DIR/box_expr.rs:7:13: 7:25
17-
(*_2) = S::new() -> [return: bb1, unwind: bb7]; // scope 0 at $DIR/box_expr.rs:7:17: 7:25
17+
(*_2) = S::new() -> [return: bb1, unwind: bb5]; // scope 0 at $DIR/box_expr.rs:7:17: 7:25
1818
// mir::Constant
1919
// + span: $DIR/box_expr.rs:7:17: 7:23
2020
// + literal: Const { ty: fn() -> S {S::new}, val: Value(Scalar(<ZST>)) }
2121
}
2222

2323
bb1: {
2424
_1 = move _2; // scope 0 at $DIR/box_expr.rs:7:13: 7:25
25-
drop(_2) -> bb2; // scope 0 at $DIR/box_expr.rs:7:24: 7:25
26-
}
27-
28-
bb2: {
2925
StorageDead(_2); // scope 0 at $DIR/box_expr.rs:7:24: 7:25
3026
StorageLive(_3); // scope 1 at $DIR/box_expr.rs:8:5: 8:12
3127
StorageLive(_4); // scope 1 at $DIR/box_expr.rs:8:10: 8:11
3228
_4 = move _1; // scope 1 at $DIR/box_expr.rs:8:10: 8:11
33-
_3 = std::mem::drop::<Box<S>>(move _4) -> [return: bb3, unwind: bb5]; // scope 1 at $DIR/box_expr.rs:8:5: 8:12
29+
_3 = std::mem::drop::<Box<S>>(move _4) -> [return: bb2, unwind: bb4]; // scope 1 at $DIR/box_expr.rs:8:5: 8:12
3430
// mir::Constant
3531
// + span: $DIR/box_expr.rs:8:5: 8:9
3632
// + literal: Const { ty: fn(std::boxed::Box<S>) {std::mem::drop::<std::boxed::Box<S>>}, val: Value(Scalar(<ZST>)) }
3733
}
3834

39-
bb3: {
35+
bb2: {
4036
StorageDead(_4); // scope 1 at $DIR/box_expr.rs:8:11: 8:12
4137
StorageDead(_3); // scope 1 at $DIR/box_expr.rs:8:12: 8:13
4238
_0 = const (); // scope 0 at $DIR/box_expr.rs:6:11: 9:2
43-
drop(_1) -> bb4; // scope 0 at $DIR/box_expr.rs:9:1: 9:2
39+
drop(_1) -> bb3; // scope 0 at $DIR/box_expr.rs:9:1: 9:2
4440
}
4541

46-
bb4: {
42+
bb3: {
4743
StorageDead(_1); // scope 0 at $DIR/box_expr.rs:9:1: 9:2
4844
return; // scope 0 at $DIR/box_expr.rs:9:2: 9:2
4945
}
5046

51-
bb5 (cleanup): {
52-
drop(_4) -> bb6; // scope 1 at $DIR/box_expr.rs:8:11: 8:12
53-
}
54-
55-
bb6 (cleanup): {
56-
drop(_1) -> bb8; // scope 0 at $DIR/box_expr.rs:9:1: 9:2
47+
bb4 (cleanup): {
48+
drop(_1) -> bb6; // scope 0 at $DIR/box_expr.rs:9:1: 9:2
5749
}
5850

59-
bb7 (cleanup): {
60-
drop(_2) -> bb8; // scope 0 at $DIR/box_expr.rs:7:24: 7:25
51+
bb5 (cleanup): {
52+
drop(_2) -> bb6; // scope 0 at $DIR/box_expr.rs:7:24: 7:25
6153
}
6254

63-
bb8 (cleanup): {
55+
bb6 (cleanup): {
6456
resume; // scope 0 at $DIR/box_expr.rs:6:1: 9:2
6557
}
6658
}

src/test/mir-opt/issue_41110.main.ElaborateDrops.after.mir

+3-24
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,44 @@ fn main() -> () {
66
let mut _2: S; // in scope 0 at $DIR/issue-41110.rs:8:13: 8:14
77
let mut _3: S; // in scope 0 at $DIR/issue-41110.rs:8:21: 8:27
88
let mut _4: S; // in scope 0 at $DIR/issue-41110.rs:8:21: 8:22
9-
let mut _5: bool; // in scope 0 at $DIR/issue-41110.rs:8:27: 8:28
109
scope 1 {
1110
debug x => _1; // in scope 1 at $DIR/issue-41110.rs:8:9: 8:10
1211
}
1312

1413
bb0: {
15-
_5 = const false; // scope 0 at $DIR/issue-41110.rs:8:9: 8:10
1614
StorageLive(_1); // scope 0 at $DIR/issue-41110.rs:8:9: 8:10
1715
StorageLive(_2); // scope 0 at $DIR/issue-41110.rs:8:13: 8:14
18-
_5 = const true; // scope 0 at $DIR/issue-41110.rs:8:13: 8:14
1916
_2 = S; // scope 0 at $DIR/issue-41110.rs:8:13: 8:14
2017
StorageLive(_3); // scope 0 at $DIR/issue-41110.rs:8:21: 8:27
2118
StorageLive(_4); // scope 0 at $DIR/issue-41110.rs:8:21: 8:22
2219
_4 = S; // scope 0 at $DIR/issue-41110.rs:8:21: 8:22
23-
_3 = S::id(move _4) -> [return: bb1, unwind: bb4]; // scope 0 at $DIR/issue-41110.rs:8:21: 8:27
20+
_3 = S::id(move _4) -> [return: bb1, unwind: bb3]; // scope 0 at $DIR/issue-41110.rs:8:21: 8:27
2421
// mir::Constant
2522
// + span: $DIR/issue-41110.rs:8:23: 8:25
2623
// + literal: Const { ty: fn(S) -> S {S::id}, val: Value(Scalar(<ZST>)) }
2724
}
2825

2926
bb1: {
3027
StorageDead(_4); // scope 0 at $DIR/issue-41110.rs:8:26: 8:27
31-
_5 = const false; // scope 0 at $DIR/issue-41110.rs:8:13: 8:28
32-
_1 = S::other(move _2, move _3) -> [return: bb2, unwind: bb3]; // scope 0 at $DIR/issue-41110.rs:8:13: 8:28
28+
_1 = S::other(move _2, move _3) -> bb2; // scope 0 at $DIR/issue-41110.rs:8:13: 8:28
3329
// mir::Constant
3430
// + span: $DIR/issue-41110.rs:8:15: 8:20
3531
// + literal: Const { ty: fn(S, S) {S::other}, val: Value(Scalar(<ZST>)) }
3632
}
3733

3834
bb2: {
3935
StorageDead(_3); // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
40-
_5 = const false; // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
4136
StorageDead(_2); // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
4237
_0 = const (); // scope 0 at $DIR/issue-41110.rs:7:11: 9:2
4338
StorageDead(_1); // scope 0 at $DIR/issue-41110.rs:9:1: 9:2
4439
return; // scope 0 at $DIR/issue-41110.rs:9:2: 9:2
4540
}
4641

4742
bb3 (cleanup): {
48-
goto -> bb5; // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
43+
drop(_2) -> bb4; // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
4944
}
5045

5146
bb4 (cleanup): {
52-
goto -> bb5; // scope 0 at $DIR/issue-41110.rs:8:26: 8:27
53-
}
54-
55-
bb5 (cleanup): {
56-
goto -> bb8; // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
57-
}
58-
59-
bb6 (cleanup): {
6047
resume; // scope 0 at $DIR/issue-41110.rs:7:1: 9:2
6148
}
62-
63-
bb7 (cleanup): {
64-
drop(_2) -> bb6; // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
65-
}
66-
67-
bb8 (cleanup): {
68-
switchInt(_5) -> [false: bb6, otherwise: bb7]; // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
69-
}
7049
}

src/test/mir-opt/issue_41110.test.ElaborateDrops.after.mir

+18-30
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn test() -> () {
2525
StorageLive(_3); // scope 2 at $DIR/issue-41110.rs:17:5: 17:12
2626
StorageLive(_4); // scope 2 at $DIR/issue-41110.rs:17:10: 17:11
2727
_4 = move _2; // scope 2 at $DIR/issue-41110.rs:17:10: 17:11
28-
_3 = std::mem::drop::<S>(move _4) -> [return: bb1, unwind: bb7]; // scope 2 at $DIR/issue-41110.rs:17:5: 17:12
28+
_3 = std::mem::drop::<S>(move _4) -> [return: bb1, unwind: bb5]; // scope 2 at $DIR/issue-41110.rs:17:5: 17:12
2929
// mir::Constant
3030
// + span: $DIR/issue-41110.rs:17:5: 17:9
3131
// + literal: Const { ty: fn(S) {std::mem::drop::<S>}, val: Value(Scalar(<ZST>)) }
@@ -37,65 +37,53 @@ fn test() -> () {
3737
StorageLive(_5); // scope 2 at $DIR/issue-41110.rs:18:9: 18:10
3838
_6 = const false; // scope 2 at $DIR/issue-41110.rs:18:9: 18:10
3939
_5 = move _1; // scope 2 at $DIR/issue-41110.rs:18:9: 18:10
40-
goto -> bb12; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6
40+
goto -> bb9; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6
4141
}
4242

4343
bb2: {
44-
goto -> bb3; // scope 2 at $DIR/issue-41110.rs:18:9: 18:10
45-
}
46-
47-
bb3: {
4844
StorageDead(_5); // scope 2 at $DIR/issue-41110.rs:18:9: 18:10
4945
_0 = const (); // scope 0 at $DIR/issue-41110.rs:14:15: 19:2
50-
drop(_2) -> [return: bb4, unwind: bb9]; // scope 1 at $DIR/issue-41110.rs:19:1: 19:2
46+
drop(_2) -> [return: bb3, unwind: bb6]; // scope 1 at $DIR/issue-41110.rs:19:1: 19:2
5147
}
5248

53-
bb4: {
49+
bb3: {
5450
StorageDead(_2); // scope 1 at $DIR/issue-41110.rs:19:1: 19:2
55-
goto -> bb5; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2
51+
goto -> bb4; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2
5652
}
5753

58-
bb5: {
54+
bb4: {
5955
_6 = const false; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2
6056
StorageDead(_1); // scope 0 at $DIR/issue-41110.rs:19:1: 19:2
6157
return; // scope 0 at $DIR/issue-41110.rs:19:2: 19:2
6258
}
6359

64-
bb6 (cleanup): {
65-
goto -> bb8; // scope 2 at $DIR/issue-41110.rs:18:9: 18:10
66-
}
67-
68-
bb7 (cleanup): {
69-
goto -> bb8; // scope 2 at $DIR/issue-41110.rs:17:11: 17:12
70-
}
71-
72-
bb8 (cleanup): {
73-
goto -> bb9; // scope 1 at $DIR/issue-41110.rs:19:1: 19:2
60+
bb5 (cleanup): {
61+
goto -> bb6; // scope 1 at $DIR/issue-41110.rs:19:1: 19:2
7462
}
7563

76-
bb9 (cleanup): {
77-
goto -> bb14; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2
64+
bb6 (cleanup): {
65+
goto -> bb11; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2
7866
}
7967

80-
bb10 (cleanup): {
68+
bb7 (cleanup): {
8169
resume; // scope 0 at $DIR/issue-41110.rs:14:1: 19:2
8270
}
8371

84-
bb11 (cleanup): {
72+
bb8 (cleanup): {
8573
_2 = move _5; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6
86-
goto -> bb6; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6
74+
goto -> bb5; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6
8775
}
8876

89-
bb12: {
77+
bb9: {
9078
_2 = move _5; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6
9179
goto -> bb2; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6
9280
}
9381

94-
bb13 (cleanup): {
95-
drop(_1) -> bb10; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2
82+
bb10 (cleanup): {
83+
drop(_1) -> bb7; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2
9684
}
9785

98-
bb14 (cleanup): {
99-
switchInt(_6) -> [false: bb10, otherwise: bb13]; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2
86+
bb11 (cleanup): {
87+
switchInt(_6) -> [false: bb7, otherwise: bb10]; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2
10088
}
10189
}

0 commit comments

Comments
 (0)