Skip to content

Commit

Permalink
Auto merge of #111344 - cjgillot:gvn-simplify, r=<try>
Browse files Browse the repository at this point in the history
Perform opportunistic simplifications during value numbering

Based on #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.
  • Loading branch information
bors committed Dec 30, 2023
2 parents d59f06f + 935e283 commit 01915ea
Show file tree
Hide file tree
Showing 90 changed files with 2,693 additions and 1,094 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
}
}

fn unsize_into(
pub fn unsize_into(
&mut self,
src: &OpTy<'tcx, M::Provenance>,
cast_ty: TyAndLayout<'tcx>,
Expand Down
367 changes: 315 additions & 52 deletions compiler/rustc_mir_transform/src/gvn.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/mir-opt/const_allocation.main.GVN.after.32bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ fn main() -> () {

bb0: {
StorageLive(_1);
nop;
StorageLive(_2);
_2 = const {ALLOC9: &&[(Option<i32>, &[&str])]};
_1 = (*_2);
nop;
StorageDead(_2);
StorageDead(_1);
_0 = const ();
return;
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/const_allocation.main.GVN.after.64bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ fn main() -> () {

bb0: {
StorageLive(_1);
nop;
StorageLive(_2);
_2 = const {ALLOC9: &&[(Option<i32>, &[&str])]};
_1 = (*_2);
nop;
StorageDead(_2);
StorageDead(_1);
_0 = const ();
return;
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/const_allocation2.main.GVN.after.32bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ fn main() -> () {

bb0: {
StorageLive(_1);
nop;
StorageLive(_2);
_2 = const {ALLOC9: &&[(Option<i32>, &[&u8])]};
_1 = (*_2);
nop;
StorageDead(_2);
StorageDead(_1);
_0 = const ();
return;
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/const_allocation2.main.GVN.after.64bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ fn main() -> () {

bb0: {
StorageLive(_1);
nop;
StorageLive(_2);
_2 = const {ALLOC9: &&[(Option<i32>, &[&u8])]};
_1 = (*_2);
nop;
StorageDead(_2);
StorageDead(_1);
_0 = const ();
return;
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/const_allocation3.main.GVN.after.32bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ fn main() -> () {

bb0: {
StorageLive(_1);
nop;
StorageLive(_2);
_2 = const {ALLOC4: &&Packed};
_1 = (*_2);
nop;
StorageDead(_2);
StorageDead(_1);
_0 = const ();
return;
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/const_allocation3.main.GVN.after.64bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ fn main() -> () {

bb0: {
StorageLive(_1);
nop;
StorageLive(_2);
_2 = const {ALLOC2: &&Packed};
_1 = (*_2);
nop;
StorageDead(_2);
StorageDead(_1);
_0 = const ();
return;
Expand Down
6 changes: 2 additions & 4 deletions tests/mir-opt/const_prop/address_of_pair.fn0.GVN.diff
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
bb0: {
StorageLive(_2);
- _2 = (const 1_i32, const false);
- StorageLive(_3);
+ _2 = const (1_i32, false);
+ nop;
StorageLive(_3);
_3 = &raw mut (_2.1: bool);
- _2 = (const 1_i32, const false);
+ _2 = const (1_i32, false);
Expand All @@ -42,9 +41,8 @@
StorageDead(_6);
_0 = _5;
- StorageDead(_5);
- StorageDead(_3);
+ nop;
+ nop;
StorageDead(_3);
StorageDead(_2);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
StorageLive(_3);
_3 = const 2_usize;
_4 = Len(_2);
- _4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind unreachable];
+ _5 = Lt(const 2_usize, _4);
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind unreachable];
+ _4 = const 4_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind unreachable];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
StorageLive(_3);
_3 = const 2_usize;
_4 = Len(_2);
- _4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
+ _5 = Lt(const 2_usize, _4);
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind continue];
+ _4 = const 4_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind continue];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
StorageLive(_3);
_3 = const 2_usize;
_4 = Len(_2);
- _4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind unreachable];
+ _5 = Lt(const 2_usize, _4);
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind unreachable];
+ _4 = const 4_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind unreachable];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
StorageLive(_3);
_3 = const 2_usize;
_4 = Len(_2);
- _4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
+ _5 = Lt(const 2_usize, _4);
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind continue];
+ _4 = const 4_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind continue];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@
}

bb0: {
- StorageLive(_1);
+ nop;
StorageLive(_1);
StorageLive(_2);
- StorageLive(_3);
+ nop;
StorageLive(_3);
_9 = const _;
_3 = &(*_9);
_2 = &raw const (*_3);
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
StorageDead(_2);
- StorageDead(_3);
+ nop;
StorageDead(_3);
StorageLive(_5);
StorageLive(_6);
_6 = const 3_usize;
Expand All @@ -50,8 +47,7 @@
StorageDead(_6);
_0 = const ();
StorageDead(_5);
- StorageDead(_1);
+ nop;
StorageDead(_1);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@
}

bb0: {
- StorageLive(_1);
+ nop;
StorageLive(_1);
StorageLive(_2);
- StorageLive(_3);
+ nop;
StorageLive(_3);
_9 = const _;
_3 = &(*_9);
_2 = &raw const (*_3);
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
StorageDead(_2);
- StorageDead(_3);
+ nop;
StorageDead(_3);
StorageLive(_5);
StorageLive(_6);
_6 = const 3_usize;
Expand All @@ -50,8 +47,7 @@
StorageDead(_6);
_0 = const ();
StorageDead(_5);
- StorageDead(_1);
+ nop;
StorageDead(_1);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@
}

bb0: {
- StorageLive(_1);
+ nop;
StorageLive(_1);
StorageLive(_2);
- StorageLive(_3);
+ nop;
StorageLive(_3);
_9 = const _;
_3 = &(*_9);
_2 = &raw const (*_3);
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
StorageDead(_2);
- StorageDead(_3);
+ nop;
StorageDead(_3);
StorageLive(_5);
StorageLive(_6);
_6 = const 3_usize;
Expand All @@ -50,8 +47,7 @@
StorageDead(_6);
_0 = const ();
StorageDead(_5);
- StorageDead(_1);
+ nop;
StorageDead(_1);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@
}

bb0: {
- StorageLive(_1);
+ nop;
StorageLive(_1);
StorageLive(_2);
- StorageLive(_3);
+ nop;
StorageLive(_3);
_9 = const _;
_3 = &(*_9);
_2 = &raw const (*_3);
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
StorageDead(_2);
- StorageDead(_3);
+ nop;
StorageDead(_3);
StorageLive(_5);
StorageLive(_6);
_6 = const 3_usize;
Expand All @@ -50,8 +47,7 @@
StorageDead(_6);
_0 = const ();
StorageDead(_5);
- StorageDead(_1);
+ nop;
StorageDead(_1);
return;
}
}
Expand Down
7 changes: 3 additions & 4 deletions tests/mir-opt/const_prop/boolean_identities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ pub fn test(x: bool, y: bool) -> bool {
// CHECK-LABEL: fn test(
// CHECK: debug a => [[a:_.*]];
// CHECK: debug b => [[b:_.*]];
// FIXME(cjgillot) simplify algebraic identity
// CHECK-NOT: [[a]] = const true;
// CHECK-NOT: [[b]] = const false;
// CHECK-NOT: _0 = const false;
// CHECK: [[a]] = const true;
// CHECK: [[b]] = const false;
// CHECK: _0 = const false;
let a = (y | true);
let b = (x & false);
a & b
Expand Down
12 changes: 7 additions & 5 deletions tests/mir-opt/const_prop/boolean_identities.test.GVN.diff
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@
StorageLive(_4);
_4 = _2;
- _3 = BitOr(move _4, const true);
+ _3 = BitOr(_2, const true);
+ _3 = const true;
StorageDead(_4);
- StorageLive(_5);
+ nop;
StorageLive(_6);
_6 = _1;
- _5 = BitAnd(move _6, const false);
+ _5 = BitAnd(_1, const false);
+ _5 = const false;
StorageDead(_6);
StorageLive(_7);
_7 = _3;
- _7 = _3;
+ _7 = const true;
StorageLive(_8);
_8 = _5;
- _8 = _5;
- _0 = BitAnd(move _7, move _8);
+ _0 = BitAnd(_3, _5);
+ _8 = const false;
+ _0 = const false;
StorageDead(_8);
StorageDead(_7);
- StorageDead(_5);
Expand Down
9 changes: 6 additions & 3 deletions tests/mir-opt/const_prop/boxes.main.GVN.panic-abort.diff
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

bb0: {
StorageLive(_1);
StorageLive(_2);
- StorageLive(_2);
+ nop;
StorageLive(_3);
- _4 = SizeOf(i32);
- _5 = AlignOf(i32);
Expand All @@ -39,8 +40,10 @@
StorageDead(_7);
_9 = (((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32);
_2 = (*_9);
_1 = Add(move _2, const 0_i32);
StorageDead(_2);
- _1 = Add(move _2, const 0_i32);
- StorageDead(_2);
+ _1 = _2;
+ nop;
drop(_3) -> [return: bb2, unwind unreachable];
}

Expand Down
9 changes: 6 additions & 3 deletions tests/mir-opt/const_prop/boxes.main.GVN.panic-unwind.diff
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

bb0: {
StorageLive(_1);
StorageLive(_2);
- StorageLive(_2);
+ nop;
StorageLive(_3);
- _4 = SizeOf(i32);
- _5 = AlignOf(i32);
Expand All @@ -39,8 +40,10 @@
StorageDead(_7);
_9 = (((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32);
_2 = (*_9);
_1 = Add(move _2, const 0_i32);
StorageDead(_2);
- _1 = Add(move _2, const 0_i32);
- StorageDead(_2);
+ _1 = _2;
+ nop;
drop(_3) -> [return: bb2, unwind: bb3];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/const_prop/boxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
// CHECK: debug x => [[x:_.*]];
// CHECK: (*{{_.*}}) = const 42_i32;
// CHECK: [[tmp:_.*]] = (*{{_.*}});
// CHECK: [[x]] = Add(move [[tmp]], const 0_i32);
// CHECK: [[x]] = [[tmp]];
let x = *(#[rustc_box]
Box::new(42))
+ 0;
Expand Down
Loading

0 comments on commit 01915ea

Please sign in to comment.