Skip to content

Commit 7bfb31f

Browse files
committed
Simplify operands in unions.
1 parent fce6510 commit 7bfb31f

5 files changed

+16
-22
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,12 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
865865
}
866866
}
867867

868+
let fields: Option<Vec<_>> = fields
869+
.iter_mut()
870+
.map(|op| self.simplify_operand(op, location).or_else(|| self.new_opaque()))
871+
.collect();
872+
let fields = fields?;
873+
868874
let (ty, variant_index) = match *kind {
869875
AggregateKind::Array(..) => {
870876
assert!(!fields.is_empty());
@@ -884,12 +890,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
884890
AggregateKind::Adt(_, _, _, _, Some(_)) => return None,
885891
};
886892

887-
let fields: Option<Vec<_>> = fields
888-
.iter_mut()
889-
.map(|op| self.simplify_operand(op, location).or_else(|| self.new_opaque()))
890-
.collect();
891-
let fields = fields?;
892-
893893
if let AggregateTy::Array = ty
894894
&& fields.len() > 4
895895
{

tests/mir-opt/const_prop/transmute.undef_union_as_integer.GVN.32bit.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
StorageLive(_1);
1313
StorageLive(_2);
1414
- _2 = ();
15+
- _1 = Union32 { value: move _2 };
1516
+ _2 = const ();
16-
_1 = Union32 { value: move _2 };
17+
+ _1 = Union32 { value: const () };
1718
StorageDead(_2);
1819
_0 = move _1 as u32 (Transmute);
1920
StorageDead(_1);

tests/mir-opt/const_prop/transmute.undef_union_as_integer.GVN.64bit.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
StorageLive(_1);
1313
StorageLive(_2);
1414
- _2 = ();
15+
- _1 = Union32 { value: move _2 };
1516
+ _2 = const ();
16-
_1 = Union32 { value: move _2 };
17+
+ _1 = Union32 { value: const () };
1718
StorageDead(_2);
1819
_0 = move _1 as u32 (Transmute);
1920
StorageDead(_1);

tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-abort.diff

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,23 @@
55
let mut _0: ();
66
let _1: main::Un;
77
let mut _2: u32;
8-
let mut _3: u32;
98
scope 1 {
109
debug un => _1;
1110
scope 2 {
1211
}
1312
scope 4 (inlined std::mem::drop::<u32>) {
14-
debug _x => _3;
13+
debug _x => _2;
1514
}
1615
}
1716
scope 3 (inlined val) {
1817
}
1918

2019
bb0: {
2120
StorageLive(_1);
22-
StorageLive(_2);
23-
_2 = const 1_u32;
2421
_1 = Un { us: const 1_u32 };
22+
StorageLive(_2);
23+
_2 = (_1.0: u32);
2524
StorageDead(_2);
26-
StorageLive(_3);
27-
_3 = (_1.0: u32);
28-
StorageDead(_3);
2925
StorageDead(_1);
3026
return;
3127
}

tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-unwind.diff

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,23 @@
55
let mut _0: ();
66
let _1: main::Un;
77
let mut _2: u32;
8-
let mut _3: u32;
98
scope 1 {
109
debug un => _1;
1110
scope 2 {
1211
}
1312
scope 4 (inlined std::mem::drop::<u32>) {
14-
debug _x => _3;
13+
debug _x => _2;
1514
}
1615
}
1716
scope 3 (inlined val) {
1817
}
1918

2019
bb0: {
2120
StorageLive(_1);
22-
StorageLive(_2);
23-
_2 = const 1_u32;
2421
_1 = Un { us: const 1_u32 };
22+
StorageLive(_2);
23+
_2 = (_1.0: u32);
2524
StorageDead(_2);
26-
StorageLive(_3);
27-
_3 = (_1.0: u32);
28-
StorageDead(_3);
2925
StorageDead(_1);
3026
return;
3127
}

0 commit comments

Comments
 (0)