Skip to content

Commit 405bed9

Browse files
committed
Simplify repeat expressions.
1 parent 2c04a25 commit 405bed9

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+6
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,17 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
491491
ProjectionElem::Field(f, ty)
492492
}
493493
ProjectionElem::Index(idx) => {
494+
if let Value::Repeat(inner, _) = self.get(value) {
495+
return Some(*inner);
496+
}
494497
let idx = self.locals[idx]?;
495498
ProjectionElem::Index(idx)
496499
}
497500
ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
498501
match self.get(value) {
502+
Value::Repeat(inner, _) => {
503+
return Some(*inner);
504+
}
499505
Value::Aggregate(ty, _, operands) if ty.is_array() => {
500506
let offset = if from_end {
501507
operands.len() - offset as usize

tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff

+6-4
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141

4242
bb1: {
4343
- _6 = _3[_7];
44-
+ _6 = _3[0 of 1];
45-
_5 = opaque::<T>(move _6) -> [return: bb2, unwind unreachable];
44+
- _5 = opaque::<T>(move _6) -> [return: bb2, unwind unreachable];
45+
+ _6 = _1;
46+
+ _5 = opaque::<T>(_1) -> [return: bb2, unwind unreachable];
4647
}
4748

4849
bb2: {
@@ -63,8 +64,9 @@
6364

6465
bb3: {
6566
- _11 = _3[_12];
66-
+ _11 = _3[_2];
67-
_10 = opaque::<T>(move _11) -> [return: bb4, unwind unreachable];
67+
- _10 = opaque::<T>(move _11) -> [return: bb4, unwind unreachable];
68+
+ _11 = _1;
69+
+ _10 = opaque::<T>(_1) -> [return: bb4, unwind unreachable];
6870
}
6971

7072
bb4: {

tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff

+6-4
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141

4242
bb1: {
4343
- _6 = _3[_7];
44-
+ _6 = _3[0 of 1];
45-
_5 = opaque::<T>(move _6) -> [return: bb2, unwind continue];
44+
- _5 = opaque::<T>(move _6) -> [return: bb2, unwind continue];
45+
+ _6 = _1;
46+
+ _5 = opaque::<T>(_1) -> [return: bb2, unwind continue];
4647
}
4748

4849
bb2: {
@@ -63,8 +64,9 @@
6364

6465
bb3: {
6566
- _11 = _3[_12];
66-
+ _11 = _3[_2];
67-
_10 = opaque::<T>(move _11) -> [return: bb4, unwind continue];
67+
- _10 = opaque::<T>(move _11) -> [return: bb4, unwind continue];
68+
+ _11 = _1;
69+
+ _10 = opaque::<T>(_1) -> [return: bb4, unwind continue];
6870
}
6971

7072
bb4: {

0 commit comments

Comments
 (0)