Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
339 changes: 201 additions & 138 deletions compiler/rustc_mir_transform/src/gvn.rs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,23 @@ impl SsaLocals {
ssa
}

#[inline]
pub(super) fn num_locals(&self) -> usize {
self.assignments.len()
}

#[inline]
pub(super) fn locals(&self) -> impl Iterator<Item = Local> {
self.assignments.indices()
}

#[inline]
pub(super) fn is_ssa(&self, local: Local) -> bool {
matches!(self.assignments[local], Set1::One(_))
}

/// Return the number of uses if a local that are not "Deref".
#[inline]
pub(super) fn num_direct_uses(&self, local: Local) -> u32 {
self.direct_uses[local]
}
Expand Down
56 changes: 26 additions & 30 deletions tests/coverage/closure.cov-map
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,22 @@ Number of file 0 mappings: 66
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
Highest counter ID seen: c1

Function name: closure::main::{closure#0}
Raw bytes (51): 0x[01, 01, 01, 01, 05, 09, 01, 28, 05, 00, 06, 01, 01, 0d, 00, 1a, 01, 00, 1d, 00, 1e, 01, 01, 0c, 00, 14, 05, 00, 15, 02, 0a, 02, 02, 09, 00, 0a, 01, 01, 09, 00, 17, 01, 00, 18, 00, 20, 01, 01, 05, 00, 06]
Function name: closure::main::{closure#0} (unused)
Raw bytes (49): 0x[01, 01, 00, 09, 00, 28, 05, 00, 06, 00, 01, 0d, 00, 1a, 00, 00, 1d, 00, 1e, 00, 01, 0c, 00, 14, 00, 00, 15, 02, 0a, 00, 02, 09, 00, 0a, 00, 01, 09, 00, 17, 00, 00, 18, 00, 20, 00, 01, 05, 00, 06]
Number of files: 1
- file 0 => $DIR/closure.rs
Number of expressions: 1
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
Number of expressions: 0
Number of file 0 mappings: 9
- Code(Counter(0)) at (prev + 40, 5) to (start + 0, 6)
- Code(Counter(0)) at (prev + 1, 13) to (start + 0, 26)
- Code(Counter(0)) at (prev + 0, 29) to (start + 0, 30)
- Code(Counter(0)) at (prev + 1, 12) to (start + 0, 20)
- Code(Counter(1)) at (prev + 0, 21) to (start + 2, 10)
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 0, 10)
= (c0 - c1)
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 23)
- Code(Counter(0)) at (prev + 0, 24) to (start + 0, 32)
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 6)
Highest counter ID seen: c1
- Code(Zero) at (prev + 40, 5) to (start + 0, 6)
- Code(Zero) at (prev + 1, 13) to (start + 0, 26)
- Code(Zero) at (prev + 0, 29) to (start + 0, 30)
- Code(Zero) at (prev + 1, 12) to (start + 0, 20)
- Code(Zero) at (prev + 0, 21) to (start + 2, 10)
- Code(Zero) at (prev + 2, 9) to (start + 0, 10)
- Code(Zero) at (prev + 1, 9) to (start + 0, 23)
- Code(Zero) at (prev + 0, 24) to (start + 0, 32)
- Code(Zero) at (prev + 1, 5) to (start + 0, 6)
Highest counter ID seen: (none)

Function name: closure::main::{closure#10} (unused)
Raw bytes (25): 0x[01, 01, 00, 04, 00, 9b, 01, 07, 00, 08, 00, 00, 09, 00, 11, 00, 00, 12, 00, 1e, 00, 00, 20, 00, 21]
Expand Down Expand Up @@ -199,24 +197,22 @@ Number of file 0 mappings: 7
- Code(Counter(0)) at (prev + 2, 9) to (start + 0, 10)
Highest counter ID seen: c1

Function name: closure::main::{closure#18}
Raw bytes (51): 0x[01, 01, 01, 01, 05, 09, 01, 19, 0d, 00, 0e, 01, 01, 15, 00, 22, 01, 00, 25, 00, 26, 01, 01, 14, 00, 1c, 05, 00, 1d, 02, 12, 02, 02, 11, 00, 12, 01, 01, 11, 00, 1f, 01, 00, 20, 00, 28, 01, 01, 0d, 00, 0e]
Function name: closure::main::{closure#18} (unused)
Raw bytes (49): 0x[01, 01, 00, 09, 00, 19, 0d, 00, 0e, 00, 01, 15, 00, 22, 00, 00, 25, 00, 26, 00, 01, 14, 00, 1c, 00, 00, 1d, 02, 12, 00, 02, 11, 00, 12, 00, 01, 11, 00, 1f, 00, 00, 20, 00, 28, 00, 01, 0d, 00, 0e]
Number of files: 1
- file 0 => $DIR/closure.rs
Number of expressions: 1
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
Number of expressions: 0
Number of file 0 mappings: 9
- Code(Counter(0)) at (prev + 25, 13) to (start + 0, 14)
- Code(Counter(0)) at (prev + 1, 21) to (start + 0, 34)
- Code(Counter(0)) at (prev + 0, 37) to (start + 0, 38)
- Code(Counter(0)) at (prev + 1, 20) to (start + 0, 28)
- Code(Counter(1)) at (prev + 0, 29) to (start + 2, 18)
- Code(Expression(0, Sub)) at (prev + 2, 17) to (start + 0, 18)
= (c0 - c1)
- Code(Counter(0)) at (prev + 1, 17) to (start + 0, 31)
- Code(Counter(0)) at (prev + 0, 32) to (start + 0, 40)
- Code(Counter(0)) at (prev + 1, 13) to (start + 0, 14)
Highest counter ID seen: c1
- Code(Zero) at (prev + 25, 13) to (start + 0, 14)
- Code(Zero) at (prev + 1, 21) to (start + 0, 34)
- Code(Zero) at (prev + 0, 37) to (start + 0, 38)
- Code(Zero) at (prev + 1, 20) to (start + 0, 28)
- Code(Zero) at (prev + 0, 29) to (start + 2, 18)
- Code(Zero) at (prev + 2, 17) to (start + 0, 18)
- Code(Zero) at (prev + 1, 17) to (start + 0, 31)
- Code(Zero) at (prev + 0, 32) to (start + 0, 40)
- Code(Zero) at (prev + 1, 13) to (start + 0, 14)
Highest counter ID seen: (none)

Function name: closure::main::{closure#19}
Raw bytes (51): 0x[01, 01, 01, 01, 05, 09, 01, 43, 0d, 00, 0e, 01, 01, 15, 00, 22, 01, 00, 25, 00, 26, 01, 01, 14, 00, 1c, 05, 00, 1d, 02, 12, 02, 02, 11, 00, 12, 01, 01, 11, 00, 1f, 01, 00, 20, 00, 28, 01, 01, 0d, 00, 0e]
Expand Down
15 changes: 10 additions & 5 deletions tests/mir-opt/const_prop/boxes.main.GVN.panic-abort.diff
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@
}

bb1: {
StorageLive(_7);
- StorageLive(_7);
+ nop;
_7 = ShallowInitBox(move _6, i32);
_8 = copy ((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
(*_8) = const 42_i32;
_3 = move _7;
StorageDead(_7);
_9 = copy ((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
_2 = copy (*_9);
- _3 = move _7;
- StorageDead(_7);
- _9 = copy ((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
- _2 = copy (*_9);
- _1 = Add(move _2, const 0_i32);
- StorageDead(_2);
+ _3 = copy _7;
+ nop;
+ _9 = copy _8;
+ _2 = copy (*_8);
+ _1 = copy _2;
+ nop;
drop(_3) -> [return: bb2, unwind unreachable];
Expand Down
15 changes: 10 additions & 5 deletions tests/mir-opt/const_prop/boxes.main.GVN.panic-unwind.diff
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@
}

bb1: {
StorageLive(_7);
- StorageLive(_7);
+ nop;
_7 = ShallowInitBox(move _6, i32);
_8 = copy ((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
(*_8) = const 42_i32;
_3 = move _7;
StorageDead(_7);
_9 = copy ((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
_2 = copy (*_9);
- _3 = move _7;
- StorageDead(_7);
- _9 = copy ((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
- _2 = copy (*_9);
- _1 = Add(move _2, const 0_i32);
- StorageDead(_2);
+ _3 = copy _7;
+ nop;
+ _9 = copy _8;
+ _2 = copy (*_8);
+ _1 = copy _2;
+ nop;
drop(_3) -> [return: bb2, unwind: bb3];
Expand Down
9 changes: 6 additions & 3 deletions tests/mir-opt/const_prop/discriminant.main.GVN.32bit.diff
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

bb0: {
StorageLive(_1);
StorageLive(_2);
- StorageLive(_2);
+ nop;
StorageLive(_3);
- _3 = Option::<bool>::Some(const true);
- _4 = discriminant(_3);
Expand All @@ -41,8 +42,10 @@
}

bb4: {
_1 = Add(move _2, const 0_i32);
StorageDead(_2);
- _1 = Add(move _2, const 0_i32);
- StorageDead(_2);
+ _1 = copy _2;
+ nop;
StorageDead(_3);
_0 = const ();
StorageDead(_1);
Expand Down
9 changes: 6 additions & 3 deletions tests/mir-opt/const_prop/discriminant.main.GVN.64bit.diff
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

bb0: {
StorageLive(_1);
StorageLive(_2);
- StorageLive(_2);
+ nop;
StorageLive(_3);
- _3 = Option::<bool>::Some(const true);
- _4 = discriminant(_3);
Expand All @@ -41,8 +42,10 @@
}

bb4: {
_1 = Add(move _2, const 0_i32);
StorageDead(_2);
- _1 = Add(move _2, const 0_i32);
- StorageDead(_2);
+ _1 = copy _2;
+ nop;
StorageDead(_3);
_0 = const ();
StorageDead(_1);
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/const_prop/discriminant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ fn main() {
// CHECK: [[tmp]] = const 10_i32;
// CHECK: goto -> bb4;
// CHECK: bb4: {
// CHECK: {{_.*}} = Add(move [[tmp]], const 0_i32);
// CHECK: {{_.*}} = copy [[tmp]];
let x = (if let Some(true) = Some(true) { 42 } else { 10 }) + 0;
}
3 changes: 2 additions & 1 deletion tests/mir-opt/const_prop/mutable_variable.main.GVN.diff
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
_1 = const 42_i32;
_1 = const 99_i32;
StorageLive(_2);
_2 = copy _1;
- _2 = copy _1;
+ _2 = const 99_i32;
_0 = const ();
StorageDead(_2);
StorageDead(_1);
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/const_prop/mutable_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() {
// CHECK: debug y => [[y:_.*]];
// CHECK: [[x]] = const 42_i32;
// CHECK: [[x]] = const 99_i32;
// CHECK: [[y]] = copy [[x]];
// CHECK: [[y]] = const 99_i32;
let mut x = 42;
x = 99;
let y = x;
Expand Down
12 changes: 8 additions & 4 deletions tests/mir-opt/const_prop/mutable_variable_no_prop.main.GVN.diff
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@
StorageLive(_1);
_1 = const 42_u32;
StorageLive(_2);
StorageLive(_3);
- StorageLive(_3);
+ nop;
StorageLive(_4);
_4 = const {ALLOC0: *mut u32};
_3 = copy (*_4);
_1 = move _3;
StorageDead(_3);
- _1 = move _3;
- StorageDead(_3);
+ _1 = copy _3;
+ nop;
StorageDead(_4);
_2 = const ();
StorageDead(_2);
StorageLive(_5);
_5 = copy _1;
- _5 = copy _1;
+ _5 = copy _3;
_0 = const ();
StorageDead(_5);
StorageDead(_1);
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/const_prop/mutable_variable_no_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ fn main() {
// CHECK: debug y => [[y:_.*]];
// CHECK: [[x]] = const 42_u32;
// CHECK: [[tmp:_.*]] = copy (*{{_.*}});
// CHECK: [[x]] = move [[tmp]];
// CHECK: [[y]] = copy [[x]];
// CHECK: [[x]] = copy [[tmp]];
// CHECK: [[y]] = copy [[tmp]];
let mut x = 42;
unsafe {
x = STATIC;
Expand Down
18 changes: 9 additions & 9 deletions tests/mir-opt/gvn.fn_pointers.GVN.panic-abort.diff
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
let mut _3: fn(u8) -> u8;
let _5: ();
let mut _6: fn(u8) -> u8;
let mut _9: {closure@$DIR/gvn.rs:617:19: 617:21};
let mut _9: {closure@$DIR/gvn.rs:620:19: 620:21};
let _10: ();
let mut _11: fn();
let mut _13: {closure@$DIR/gvn.rs:617:19: 617:21};
let mut _13: {closure@$DIR/gvn.rs:620:19: 620:21};
let _14: ();
let mut _15: fn();
scope 1 {
debug f => _1;
let _4: fn(u8) -> u8;
scope 2 {
debug g => _4;
let _7: {closure@$DIR/gvn.rs:617:19: 617:21};
let _7: {closure@$DIR/gvn.rs:620:19: 620:21};
scope 3 {
debug closure => _7;
let _8: fn();
Expand Down Expand Up @@ -62,16 +62,16 @@
StorageDead(_6);
StorageDead(_5);
- StorageLive(_7);
- _7 = {closure@$DIR/gvn.rs:617:19: 617:21};
- _7 = {closure@$DIR/gvn.rs:620:19: 620:21};
- StorageLive(_8);
+ nop;
+ _7 = const ZeroSized: {closure@$DIR/gvn.rs:617:19: 617:21};
+ _7 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21};
+ nop;
StorageLive(_9);
- _9 = copy _7;
- _8 = move _9 as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
+ _9 = const ZeroSized: {closure@$DIR/gvn.rs:617:19: 617:21};
+ _8 = const ZeroSized: {closure@$DIR/gvn.rs:617:19: 617:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
+ _9 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21};
+ _8 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
StorageDead(_9);
StorageLive(_10);
StorageLive(_11);
Expand All @@ -88,8 +88,8 @@
StorageLive(_13);
- _13 = copy _7;
- _12 = move _13 as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
+ _13 = const ZeroSized: {closure@$DIR/gvn.rs:617:19: 617:21};
+ _12 = const ZeroSized: {closure@$DIR/gvn.rs:617:19: 617:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
+ _13 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21};
+ _12 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
StorageDead(_13);
StorageLive(_14);
StorageLive(_15);
Expand Down
18 changes: 9 additions & 9 deletions tests/mir-opt/gvn.fn_pointers.GVN.panic-unwind.diff
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
let mut _3: fn(u8) -> u8;
let _5: ();
let mut _6: fn(u8) -> u8;
let mut _9: {closure@$DIR/gvn.rs:617:19: 617:21};
let mut _9: {closure@$DIR/gvn.rs:620:19: 620:21};
let _10: ();
let mut _11: fn();
let mut _13: {closure@$DIR/gvn.rs:617:19: 617:21};
let mut _13: {closure@$DIR/gvn.rs:620:19: 620:21};
let _14: ();
let mut _15: fn();
scope 1 {
debug f => _1;
let _4: fn(u8) -> u8;
scope 2 {
debug g => _4;
let _7: {closure@$DIR/gvn.rs:617:19: 617:21};
let _7: {closure@$DIR/gvn.rs:620:19: 620:21};
scope 3 {
debug closure => _7;
let _8: fn();
Expand Down Expand Up @@ -62,16 +62,16 @@
StorageDead(_6);
StorageDead(_5);
- StorageLive(_7);
- _7 = {closure@$DIR/gvn.rs:617:19: 617:21};
- _7 = {closure@$DIR/gvn.rs:620:19: 620:21};
- StorageLive(_8);
+ nop;
+ _7 = const ZeroSized: {closure@$DIR/gvn.rs:617:19: 617:21};
+ _7 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21};
+ nop;
StorageLive(_9);
- _9 = copy _7;
- _8 = move _9 as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
+ _9 = const ZeroSized: {closure@$DIR/gvn.rs:617:19: 617:21};
+ _8 = const ZeroSized: {closure@$DIR/gvn.rs:617:19: 617:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
+ _9 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21};
+ _8 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
StorageDead(_9);
StorageLive(_10);
StorageLive(_11);
Expand All @@ -88,8 +88,8 @@
StorageLive(_13);
- _13 = copy _7;
- _12 = move _13 as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
+ _13 = const ZeroSized: {closure@$DIR/gvn.rs:617:19: 617:21};
+ _12 = const ZeroSized: {closure@$DIR/gvn.rs:617:19: 617:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
+ _13 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21};
+ _12 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
StorageDead(_13);
StorageLive(_14);
StorageLive(_15);
Expand Down
Loading
Loading