Skip to content

Commit 01915ea

Browse files
committed
Auto merge of rust-lang#111344 - cjgillot:gvn-simplify, r=<try>
Perform opportunistic simplifications during value numbering Based on rust-lang#109597 Opening mostly for discussion. In its current form, I think this pass does too much. I want to remove the const-propagation part to make it simpler.
2 parents d59f06f + 935e283 commit 01915ea

File tree

90 files changed

+2693
-1094
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+2693
-1094
lines changed

compiler/rustc_const_eval/src/interpret/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
416416
}
417417
}
418418

419-
fn unsize_into(
419+
pub fn unsize_into(
420420
&mut self,
421421
src: &OpTy<'tcx, M::Provenance>,
422422
cast_ty: TyAndLayout<'tcx>,

compiler/rustc_mir_transform/src/gvn.rs

+315-52
Large diffs are not rendered by default.

tests/mir-opt/const_allocation.main.GVN.after.32bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
nop;
10+
StorageLive(_2);
1111
_2 = const {ALLOC9: &&[(Option<i32>, &[&str])]};
1212
_1 = (*_2);
13-
nop;
13+
StorageDead(_2);
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

tests/mir-opt/const_allocation.main.GVN.after.64bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
nop;
10+
StorageLive(_2);
1111
_2 = const {ALLOC9: &&[(Option<i32>, &[&str])]};
1212
_1 = (*_2);
13-
nop;
13+
StorageDead(_2);
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

tests/mir-opt/const_allocation2.main.GVN.after.32bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
nop;
10+
StorageLive(_2);
1111
_2 = const {ALLOC9: &&[(Option<i32>, &[&u8])]};
1212
_1 = (*_2);
13-
nop;
13+
StorageDead(_2);
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

tests/mir-opt/const_allocation2.main.GVN.after.64bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
nop;
10+
StorageLive(_2);
1111
_2 = const {ALLOC9: &&[(Option<i32>, &[&u8])]};
1212
_1 = (*_2);
13-
nop;
13+
StorageDead(_2);
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

tests/mir-opt/const_allocation3.main.GVN.after.32bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
nop;
10+
StorageLive(_2);
1111
_2 = const {ALLOC4: &&Packed};
1212
_1 = (*_2);
13-
nop;
13+
StorageDead(_2);
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

tests/mir-opt/const_allocation3.main.GVN.after.64bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
nop;
10+
StorageLive(_2);
1111
_2 = const {ALLOC2: &&Packed};
1212
_1 = (*_2);
13-
nop;
13+
StorageDead(_2);
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

tests/mir-opt/const_prop/address_of_pair.fn0.GVN.diff

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
bb0: {
2525
StorageLive(_2);
2626
- _2 = (const 1_i32, const false);
27-
- StorageLive(_3);
2827
+ _2 = const (1_i32, false);
29-
+ nop;
28+
StorageLive(_3);
3029
_3 = &raw mut (_2.1: bool);
3130
- _2 = (const 1_i32, const false);
3231
+ _2 = const (1_i32, false);
@@ -42,9 +41,8 @@
4241
StorageDead(_6);
4342
_0 = _5;
4443
- StorageDead(_5);
45-
- StorageDead(_3);
46-
+ nop;
4744
+ nop;
45+
StorageDead(_3);
4846
StorageDead(_2);
4947
return;
5048
}

tests/mir-opt/const_prop/array_index.main.GVN.32bit.panic-abort.diff

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
1919
StorageLive(_3);
2020
_3 = const 2_usize;
21-
_4 = Len(_2);
21+
- _4 = Len(_2);
2222
- _5 = Lt(_3, _4);
2323
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind unreachable];
24-
+ _5 = Lt(const 2_usize, _4);
25-
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind unreachable];
24+
+ _4 = const 4_usize;
25+
+ _5 = const true;
26+
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind unreachable];
2627
}
2728

2829
bb1: {

tests/mir-opt/const_prop/array_index.main.GVN.32bit.panic-unwind.diff

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
1919
StorageLive(_3);
2020
_3 = const 2_usize;
21-
_4 = Len(_2);
21+
- _4 = Len(_2);
2222
- _5 = Lt(_3, _4);
2323
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
24-
+ _5 = Lt(const 2_usize, _4);
25-
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind continue];
24+
+ _4 = const 4_usize;
25+
+ _5 = const true;
26+
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind continue];
2627
}
2728

2829
bb1: {

tests/mir-opt/const_prop/array_index.main.GVN.64bit.panic-abort.diff

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
1919
StorageLive(_3);
2020
_3 = const 2_usize;
21-
_4 = Len(_2);
21+
- _4 = Len(_2);
2222
- _5 = Lt(_3, _4);
2323
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind unreachable];
24-
+ _5 = Lt(const 2_usize, _4);
25-
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind unreachable];
24+
+ _4 = const 4_usize;
25+
+ _5 = const true;
26+
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind unreachable];
2627
}
2728

2829
bb1: {

tests/mir-opt/const_prop/array_index.main.GVN.64bit.panic-unwind.diff

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
1919
StorageLive(_3);
2020
_3 = const 2_usize;
21-
_4 = Len(_2);
21+
- _4 = Len(_2);
2222
- _5 = Lt(_3, _4);
2323
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
24-
+ _5 = Lt(const 2_usize, _4);
25-
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind continue];
24+
+ _4 = const 4_usize;
25+
+ _5 = const true;
26+
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind continue];
2627
}
2728

2829
bb1: {

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.GVN.32bit.panic-abort.diff

+4-8
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222
}
2323

2424
bb0: {
25-
- StorageLive(_1);
26-
+ nop;
25+
StorageLive(_1);
2726
StorageLive(_2);
28-
- StorageLive(_3);
29-
+ nop;
27+
StorageLive(_3);
3028
_9 = const _;
3129
_3 = &(*_9);
3230
_2 = &raw const (*_3);
3331
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
3432
StorageDead(_2);
35-
- StorageDead(_3);
36-
+ nop;
33+
StorageDead(_3);
3734
StorageLive(_5);
3835
StorageLive(_6);
3936
_6 = const 3_usize;
@@ -50,8 +47,7 @@
5047
StorageDead(_6);
5148
_0 = const ();
5249
StorageDead(_5);
53-
- StorageDead(_1);
54-
+ nop;
50+
StorageDead(_1);
5551
return;
5652
}
5753
}

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.GVN.32bit.panic-unwind.diff

+4-8
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222
}
2323

2424
bb0: {
25-
- StorageLive(_1);
26-
+ nop;
25+
StorageLive(_1);
2726
StorageLive(_2);
28-
- StorageLive(_3);
29-
+ nop;
27+
StorageLive(_3);
3028
_9 = const _;
3129
_3 = &(*_9);
3230
_2 = &raw const (*_3);
3331
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
3432
StorageDead(_2);
35-
- StorageDead(_3);
36-
+ nop;
33+
StorageDead(_3);
3734
StorageLive(_5);
3835
StorageLive(_6);
3936
_6 = const 3_usize;
@@ -50,8 +47,7 @@
5047
StorageDead(_6);
5148
_0 = const ();
5249
StorageDead(_5);
53-
- StorageDead(_1);
54-
+ nop;
50+
StorageDead(_1);
5551
return;
5652
}
5753
}

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.GVN.64bit.panic-abort.diff

+4-8
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222
}
2323

2424
bb0: {
25-
- StorageLive(_1);
26-
+ nop;
25+
StorageLive(_1);
2726
StorageLive(_2);
28-
- StorageLive(_3);
29-
+ nop;
27+
StorageLive(_3);
3028
_9 = const _;
3129
_3 = &(*_9);
3230
_2 = &raw const (*_3);
3331
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
3432
StorageDead(_2);
35-
- StorageDead(_3);
36-
+ nop;
33+
StorageDead(_3);
3734
StorageLive(_5);
3835
StorageLive(_6);
3936
_6 = const 3_usize;
@@ -50,8 +47,7 @@
5047
StorageDead(_6);
5148
_0 = const ();
5249
StorageDead(_5);
53-
- StorageDead(_1);
54-
+ nop;
50+
StorageDead(_1);
5551
return;
5652
}
5753
}

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.GVN.64bit.panic-unwind.diff

+4-8
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222
}
2323

2424
bb0: {
25-
- StorageLive(_1);
26-
+ nop;
25+
StorageLive(_1);
2726
StorageLive(_2);
28-
- StorageLive(_3);
29-
+ nop;
27+
StorageLive(_3);
3028
_9 = const _;
3129
_3 = &(*_9);
3230
_2 = &raw const (*_3);
3331
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
3432
StorageDead(_2);
35-
- StorageDead(_3);
36-
+ nop;
33+
StorageDead(_3);
3734
StorageLive(_5);
3835
StorageLive(_6);
3936
_6 = const 3_usize;
@@ -50,8 +47,7 @@
5047
StorageDead(_6);
5148
_0 = const ();
5249
StorageDead(_5);
53-
- StorageDead(_1);
54-
+ nop;
50+
StorageDead(_1);
5551
return;
5652
}
5753
}

tests/mir-opt/const_prop/boolean_identities.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ pub fn test(x: bool, y: bool) -> bool {
55
// CHECK-LABEL: fn test(
66
// CHECK: debug a => [[a:_.*]];
77
// CHECK: debug b => [[b:_.*]];
8-
// FIXME(cjgillot) simplify algebraic identity
9-
// CHECK-NOT: [[a]] = const true;
10-
// CHECK-NOT: [[b]] = const false;
11-
// CHECK-NOT: _0 = const false;
8+
// CHECK: [[a]] = const true;
9+
// CHECK: [[b]] = const false;
10+
// CHECK: _0 = const false;
1211
let a = (y | true);
1312
let b = (x & false);
1413
a & b

tests/mir-opt/const_prop/boolean_identities.test.GVN.diff

+7-5
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,23 @@
2424
StorageLive(_4);
2525
_4 = _2;
2626
- _3 = BitOr(move _4, const true);
27-
+ _3 = BitOr(_2, const true);
27+
+ _3 = const true;
2828
StorageDead(_4);
2929
- StorageLive(_5);
3030
+ nop;
3131
StorageLive(_6);
3232
_6 = _1;
3333
- _5 = BitAnd(move _6, const false);
34-
+ _5 = BitAnd(_1, const false);
34+
+ _5 = const false;
3535
StorageDead(_6);
3636
StorageLive(_7);
37-
_7 = _3;
37+
- _7 = _3;
38+
+ _7 = const true;
3839
StorageLive(_8);
39-
_8 = _5;
40+
- _8 = _5;
4041
- _0 = BitAnd(move _7, move _8);
41-
+ _0 = BitAnd(_3, _5);
42+
+ _8 = const false;
43+
+ _0 = const false;
4244
StorageDead(_8);
4345
StorageDead(_7);
4446
- StorageDead(_5);

tests/mir-opt/const_prop/boxes.main.GVN.panic-abort.diff

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
bb0: {
2222
StorageLive(_1);
23-
StorageLive(_2);
23+
- StorageLive(_2);
24+
+ nop;
2425
StorageLive(_3);
2526
- _4 = SizeOf(i32);
2627
- _5 = AlignOf(i32);
@@ -39,8 +40,10 @@
3940
StorageDead(_7);
4041
_9 = (((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32);
4142
_2 = (*_9);
42-
_1 = Add(move _2, const 0_i32);
43-
StorageDead(_2);
43+
- _1 = Add(move _2, const 0_i32);
44+
- StorageDead(_2);
45+
+ _1 = _2;
46+
+ nop;
4447
drop(_3) -> [return: bb2, unwind unreachable];
4548
}
4649

tests/mir-opt/const_prop/boxes.main.GVN.panic-unwind.diff

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
bb0: {
2222
StorageLive(_1);
23-
StorageLive(_2);
23+
- StorageLive(_2);
24+
+ nop;
2425
StorageLive(_3);
2526
- _4 = SizeOf(i32);
2627
- _5 = AlignOf(i32);
@@ -39,8 +40,10 @@
3940
StorageDead(_7);
4041
_9 = (((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32);
4142
_2 = (*_9);
42-
_1 = Add(move _2, const 0_i32);
43-
StorageDead(_2);
43+
- _1 = Add(move _2, const 0_i32);
44+
- StorageDead(_2);
45+
+ _1 = _2;
46+
+ nop;
4447
drop(_3) -> [return: bb2, unwind: bb3];
4548
}
4649

tests/mir-opt/const_prop/boxes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn main() {
1212
// CHECK: debug x => [[x:_.*]];
1313
// CHECK: (*{{_.*}}) = const 42_i32;
1414
// CHECK: [[tmp:_.*]] = (*{{_.*}});
15-
// CHECK: [[x]] = Add(move [[tmp]], const 0_i32);
15+
// CHECK: [[x]] = [[tmp]];
1616
let x = *(#[rustc_box]
1717
Box::new(42))
1818
+ 0;

0 commit comments

Comments
 (0)