Skip to content

Commit 69d51fc

Browse files
committed
Simplify operands in unions.
1 parent fc1a4c5 commit 69d51fc

5 files changed

+16
-22
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,12 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
893893
}
894894
}
895895

896+
let fields: Option<Vec<_>> = fields
897+
.iter_mut()
898+
.map(|op| self.simplify_operand(op, location).or_else(|| self.new_opaque()))
899+
.collect();
900+
let fields = fields?;
901+
896902
let (ty, variant_index) = match *kind {
897903
AggregateKind::Array(..) => {
898904
assert!(!fields.is_empty());
@@ -912,12 +918,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
912918
AggregateKind::Adt(_, _, _, _, Some(_)) => return None,
913919
};
914920

915-
let fields: Option<Vec<_>> = fields
916-
.iter_mut()
917-
.map(|op| self.simplify_operand(op, location).or_else(|| self.new_opaque()))
918-
.collect();
919-
let fields = fields?;
920-
921921
if let AggregateTy::Array = ty
922922
&& fields.len() > 4
923923
{

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
StorageLive(_1);
1111
StorageLive(_2);
1212
- _2 = ();
13+
- _1 = Union32 { value: move _2 };
1314
+ _2 = const ();
14-
_1 = Union32 { value: move _2 };
15+
+ _1 = Union32 { value: const () };
1516
StorageDead(_2);
1617
_0 = move _1 as u32 (Transmute);
1718
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
@@ -10,8 +10,9 @@
1010
StorageLive(_1);
1111
StorageLive(_2);
1212
- _2 = ();
13+
- _1 = Union32 { value: move _2 };
1314
+ _2 = const ();
14-
_1 = Union32 { value: move _2 };
15+
+ _1 = Union32 { value: const () };
1516
StorageDead(_2);
1617
_0 = move _1 as u32 (Transmute);
1718
StorageDead(_1);

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,21 @@
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 3 (inlined std::mem::drop::<u32>) {
12-
debug _x => _3;
11+
debug _x => _2;
1312
}
1413
}
1514
scope 2 (inlined val) {
1615
}
1716

1817
bb0: {
1918
StorageLive(_1);
20-
StorageLive(_2);
21-
_2 = const 1_u32;
2219
_1 = Un { us: const 1_u32 };
20+
StorageLive(_2);
21+
_2 = (_1.0: u32);
2322
StorageDead(_2);
24-
StorageLive(_3);
25-
_3 = (_1.0: u32);
26-
StorageDead(_3);
2723
StorageDead(_1);
2824
return;
2925
}

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,21 @@
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 3 (inlined std::mem::drop::<u32>) {
12-
debug _x => _3;
11+
debug _x => _2;
1312
}
1413
}
1514
scope 2 (inlined val) {
1615
}
1716

1817
bb0: {
1918
StorageLive(_1);
20-
StorageLive(_2);
21-
_2 = const 1_u32;
2219
_1 = Un { us: const 1_u32 };
20+
StorageLive(_2);
21+
_2 = (_1.0: u32);
2322
StorageDead(_2);
24-
StorageLive(_3);
25-
_3 = (_1.0: u32);
26-
StorageDead(_3);
2723
StorageDead(_1);
2824
return;
2925
}

0 commit comments

Comments
 (0)