Skip to content

Commit 73db12a

Browse files
committed
Merge borrowed locals too.
1 parent 308cc76 commit 73db12a

File tree

5 files changed

+84
-112
lines changed

5 files changed

+84
-112
lines changed

compiler/rustc_mir_transform/src/copy_prop.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
111111

112112
fn visit_local(&mut self, local: &mut Local, ctxt: PlaceContext, _: Location) {
113113
let new_local = self.copy_classes[*local];
114-
if self.borrowed_locals.contains(*local) || self.borrowed_locals.contains(new_local) {
114+
// We must not unify two locals that are borrowed. But this is fine if one is borrowed and
115+
// the other is not. We chose to check the original local, and not the target. That way, if
116+
// the original local is borrowed and the target is not, we do not pessimize the whole class.
117+
if self.borrowed_locals.contains(*local) {
115118
return;
116119
}
117120
match ctxt {

tests/mir-opt/copy-prop/borrowed_local.borrowed.CopyProp.panic-abort.diff

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
let mut _3: &u32;
88

99
bb0: {
10-
_2 = _1;
10+
- _2 = _1;
1111
_3 = &_1;
1212
_0 = opaque::<&u32>(_3) -> [return: bb1, unwind unreachable];
1313
}
1414

1515
bb1: {
16-
_0 = opaque::<u32>(_2) -> [return: bb2, unwind unreachable];
16+
- _0 = opaque::<u32>(_2) -> [return: bb2, unwind unreachable];
17+
+ _0 = opaque::<u32>(_1) -> [return: bb2, unwind unreachable];
1718
}
1819

1920
bb2: {

tests/mir-opt/copy-prop/borrowed_local.borrowed.CopyProp.panic-unwind.diff

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
let mut _3: &u32;
88

99
bb0: {
10-
_2 = _1;
10+
- _2 = _1;
1111
_3 = &_1;
1212
_0 = opaque::<&u32>(_3) -> [return: bb1, unwind continue];
1313
}
1414

1515
bb1: {
16-
_0 = opaque::<u32>(_2) -> [return: bb2, unwind continue];
16+
- _0 = opaque::<u32>(_2) -> [return: bb2, unwind continue];
17+
+ _0 = opaque::<u32>(_1) -> [return: bb2, unwind continue];
1718
}
1819

1920
bb2: {

tests/mir-opt/copy-prop/borrowed_local.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ fn compare_address() -> bool {
4646
fn borrowed(x: u32) -> bool {
4747
// CHECK-LABEL: fn borrowed(
4848
// CHECK: bb0: {
49-
// CHECK-NEXT: _2 = _1;
5049
// CHECK-NEXT: _3 = &_1;
5150
// CHECK-NEXT: _0 = opaque::<&u32>(_3)
5251
// CHECK: bb1: {
53-
// CHECK-NEXT: _0 = opaque::<u32>(_2)
52+
// CHECK-NEXT: _0 = opaque::<u32>(_1)
5453
mir!(
5554
{
5655
let a = x;

tests/mir-opt/pre-codegen/slice_filter.variant_a-{closure#0}.PreCodegen.after.mir

+73-105
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ fn variant_a::{closure#0}(_1: &mut {closure@$DIR/slice_filter.rs:7:25: 7:39}, _2
1010
let mut _8: &&usize;
1111
let _9: &usize;
1212
let mut _10: &&usize;
13-
let mut _15: bool;
13+
let mut _13: bool;
14+
let mut _14: &&usize;
15+
let _15: &usize;
1416
let mut _16: &&usize;
15-
let _17: &usize;
16-
let mut _18: &&usize;
17+
let mut _19: bool;
18+
let mut _20: &&usize;
19+
let _21: &usize;
20+
let mut _22: &&usize;
1721
let mut _23: bool;
1822
let mut _24: &&usize;
1923
let _25: &usize;
2024
let mut _26: &&usize;
21-
let mut _29: bool;
22-
let mut _30: &&usize;
23-
let _31: &usize;
24-
let mut _32: &&usize;
2525
scope 1 {
2626
debug a => _4;
2727
debug b => _5;
@@ -30,47 +30,39 @@ fn variant_a::{closure#0}(_1: &mut {closure@$DIR/slice_filter.rs:7:25: 7:39}, _2
3030
scope 2 (inlined std::cmp::impls::<impl PartialOrd for &usize>::le) {
3131
debug self => _8;
3232
debug other => _10;
33-
let mut _11: &usize;
34-
let mut _12: &usize;
3533
scope 3 (inlined std::cmp::impls::<impl PartialOrd for usize>::le) {
36-
debug self => _11;
37-
debug other => _12;
38-
let mut _13: usize;
39-
let mut _14: usize;
34+
debug self => _4;
35+
debug other => _9;
36+
let mut _11: usize;
37+
let mut _12: usize;
4038
}
4139
}
4240
scope 4 (inlined std::cmp::impls::<impl PartialOrd for &usize>::le) {
43-
debug self => _16;
44-
debug other => _18;
45-
let mut _19: &usize;
46-
let mut _20: &usize;
41+
debug self => _14;
42+
debug other => _16;
4743
scope 5 (inlined std::cmp::impls::<impl PartialOrd for usize>::le) {
48-
debug self => _19;
49-
debug other => _20;
50-
let mut _21: usize;
51-
let mut _22: usize;
44+
debug self => _7;
45+
debug other => _15;
46+
let mut _17: usize;
47+
let mut _18: usize;
5248
}
5349
}
5450
scope 6 (inlined std::cmp::impls::<impl PartialOrd for &usize>::le) {
55-
debug self => _24;
56-
debug other => _26;
57-
let mut _27: &usize;
58-
let mut _28: &usize;
51+
debug self => _20;
52+
debug other => _22;
5953
scope 7 (inlined std::cmp::impls::<impl PartialOrd for usize>::le) {
60-
debug self => _27;
61-
debug other => _28;
54+
debug self => _6;
55+
debug other => _21;
6256
}
6357
}
6458
scope 8 (inlined std::cmp::impls::<impl PartialOrd for &usize>::le) {
65-
debug self => _30;
66-
debug other => _32;
67-
let mut _33: &usize;
68-
let mut _34: &usize;
59+
debug self => _24;
60+
debug other => _26;
6961
scope 9 (inlined std::cmp::impls::<impl PartialOrd for usize>::le) {
70-
debug self => _33;
71-
debug other => _34;
72-
let mut _35: usize;
73-
let mut _36: usize;
62+
debug self => _5;
63+
debug other => _25;
64+
let mut _27: usize;
65+
let mut _28: usize;
7466
}
7567
}
7668
}
@@ -81,22 +73,16 @@ fn variant_a::{closure#0}(_1: &mut {closure@$DIR/slice_filter.rs:7:25: 7:39}, _2
8173
_5 = &((*_3).1: usize);
8274
_6 = &((*_3).2: usize);
8375
_7 = &((*_3).3: usize);
84-
StorageLive(_15);
76+
StorageLive(_13);
8577
StorageLive(_8);
8678
_8 = &_4;
8779
StorageLive(_10);
8880
_9 = &((*_3).2: usize);
8981
_10 = &_9;
90-
StorageLive(_11);
91-
StorageLive(_12);
92-
_11 = _4;
93-
_12 = _9;
94-
_13 = ((*_3).0: usize);
95-
_14 = ((*_3).2: usize);
96-
_15 = Le(_13, _14);
97-
StorageDead(_12);
98-
StorageDead(_11);
99-
switchInt(move _15) -> [0: bb1, otherwise: bb2];
82+
_11 = ((*_3).0: usize);
83+
_12 = ((*_3).2: usize);
84+
_13 = Le(_11, _12);
85+
switchInt(move _13) -> [0: bb1, otherwise: bb2];
10086
}
10187

10288
bb1: {
@@ -108,99 +94,81 @@ fn variant_a::{closure#0}(_1: &mut {closure@$DIR/slice_filter.rs:7:25: 7:39}, _2
10894
bb2: {
10995
StorageDead(_10);
11096
StorageDead(_8);
111-
StorageLive(_23);
97+
StorageLive(_19);
98+
StorageLive(_14);
99+
_14 = &_7;
112100
StorageLive(_16);
113-
_16 = &_7;
101+
_15 = &((*_3).1: usize);
102+
_16 = &_15;
103+
StorageLive(_17);
104+
_17 = ((*_3).3: usize);
114105
StorageLive(_18);
115-
_17 = &((*_3).1: usize);
116-
_18 = &_17;
117-
StorageLive(_19);
118-
StorageLive(_20);
119-
_19 = _7;
120-
_20 = _17;
121-
StorageLive(_21);
122-
_21 = ((*_3).3: usize);
123-
StorageLive(_22);
124-
_22 = ((*_3).1: usize);
125-
_23 = Le(move _21, move _22);
126-
StorageDead(_22);
127-
StorageDead(_21);
128-
StorageDead(_20);
129-
StorageDead(_19);
130-
switchInt(move _23) -> [0: bb3, otherwise: bb8];
106+
_18 = ((*_3).1: usize);
107+
_19 = Le(move _17, move _18);
108+
StorageDead(_18);
109+
StorageDead(_17);
110+
switchInt(move _19) -> [0: bb3, otherwise: bb8];
131111
}
132112

133113
bb3: {
134-
StorageDead(_18);
135114
StorageDead(_16);
115+
StorageDead(_14);
136116
goto -> bb4;
137117
}
138118

139119
bb4: {
140-
StorageLive(_29);
141-
StorageLive(_24);
142-
_24 = &_6;
143-
StorageLive(_26);
144-
_25 = &((*_3).0: usize);
145-
_26 = &_25;
146-
StorageLive(_27);
147-
StorageLive(_28);
148-
_27 = _6;
149-
_28 = _25;
150-
_29 = Le(_14, _13);
151-
StorageDead(_28);
152-
StorageDead(_27);
153-
switchInt(move _29) -> [0: bb5, otherwise: bb6];
120+
StorageLive(_23);
121+
StorageLive(_20);
122+
_20 = &_6;
123+
StorageLive(_22);
124+
_21 = &((*_3).0: usize);
125+
_22 = &_21;
126+
_23 = Le(_12, _11);
127+
switchInt(move _23) -> [0: bb5, otherwise: bb6];
154128
}
155129

156130
bb5: {
157-
StorageDead(_26);
158-
StorageDead(_24);
131+
StorageDead(_22);
132+
StorageDead(_20);
159133
_0 = const false;
160134
goto -> bb7;
161135
}
162136

163137
bb6: {
138+
StorageDead(_22);
139+
StorageDead(_20);
140+
StorageLive(_24);
141+
_24 = &_5;
142+
StorageLive(_26);
143+
_25 = &((*_3).3: usize);
144+
_26 = &_25;
145+
StorageLive(_27);
146+
_27 = ((*_3).1: usize);
147+
StorageLive(_28);
148+
_28 = ((*_3).3: usize);
149+
_0 = Le(move _27, move _28);
150+
StorageDead(_28);
151+
StorageDead(_27);
164152
StorageDead(_26);
165153
StorageDead(_24);
166-
StorageLive(_30);
167-
_30 = &_5;
168-
StorageLive(_32);
169-
_31 = &((*_3).3: usize);
170-
_32 = &_31;
171-
StorageLive(_33);
172-
StorageLive(_34);
173-
_33 = _5;
174-
_34 = _31;
175-
StorageLive(_35);
176-
_35 = ((*_3).1: usize);
177-
StorageLive(_36);
178-
_36 = ((*_3).3: usize);
179-
_0 = Le(move _35, move _36);
180-
StorageDead(_36);
181-
StorageDead(_35);
182-
StorageDead(_34);
183-
StorageDead(_33);
184-
StorageDead(_32);
185-
StorageDead(_30);
186154
goto -> bb7;
187155
}
188156

189157
bb7: {
190-
StorageDead(_29);
158+
StorageDead(_23);
191159
goto -> bb9;
192160
}
193161

194162
bb8: {
195-
StorageDead(_18);
196163
StorageDead(_16);
164+
StorageDead(_14);
197165
_0 = const true;
198166
goto -> bb9;
199167
}
200168

201169
bb9: {
202-
StorageDead(_23);
203-
StorageDead(_15);
170+
StorageDead(_19);
171+
StorageDead(_13);
204172
return;
205173
}
206174
}

0 commit comments

Comments
 (0)