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
384 changes: 244 additions & 140 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
54 changes: 54 additions & 0 deletions tests/mir-opt/const_prop/borrowed_variable.borrow.GVN.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
- // MIR for `borrow` before GVN
+ // MIR for `borrow` after GVN

fn borrow() -> () {
let mut _0: ();
let mut _1: i32;
let _2: ();
let mut _3: i32;
let _5: ();
let mut _6: i32;
scope 1 {
debug x => _1;
let _4: &i32;
scope 2 {
debug r => _4;
}
}

bb0: {
- StorageLive(_1);
+ nop;
_1 = const 42_i32;
StorageLive(_2);
StorageLive(_3);
- _3 = copy _1;
- _2 = opaque::<i32>(move _3) -> [return: bb1, unwind unreachable];
+ _3 = const 42_i32;
+ _2 = opaque::<i32>(const 42_i32) -> [return: bb1, unwind unreachable];
}

bb1: {
StorageDead(_3);
StorageDead(_2);
StorageLive(_4);
_4 = &_1;
StorageLive(_5);
StorageLive(_6);
- _6 = copy _1;
- _5 = opaque::<i32>(move _6) -> [return: bb2, unwind unreachable];
+ _6 = const 42_i32;
+ _5 = opaque::<i32>(const 42_i32) -> [return: bb2, unwind unreachable];
}

bb2: {
StorageDead(_6);
StorageDead(_5);
_0 = const ();
StorageDead(_4);
- StorageDead(_1);
+ nop;
return;
}
}

50 changes: 50 additions & 0 deletions tests/mir-opt/const_prop/borrowed_variable.constptr.GVN.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
- // MIR for `constptr` before GVN
+ // MIR for `constptr` after GVN

fn constptr() -> () {
let mut _0: ();
let mut _1: i32;
let _2: ();
let mut _3: i32;
let _5: ();
let mut _6: i32;
scope 1 {
debug x => _1;
let _4: *const i32;
scope 2 {
debug r => _4;
}
}

bb0: {
StorageLive(_1);
_1 = const 42_i32;
StorageLive(_2);
StorageLive(_3);
- _3 = copy _1;
- _2 = opaque::<i32>(move _3) -> [return: bb1, unwind unreachable];
+ _3 = const 42_i32;
+ _2 = opaque::<i32>(const 42_i32) -> [return: bb1, unwind unreachable];
}

bb1: {
StorageDead(_3);
StorageDead(_2);
StorageLive(_4);
_4 = &raw const _1;
StorageLive(_5);
StorageLive(_6);
_6 = copy _1;
_5 = opaque::<i32>(move _6) -> [return: bb2, unwind unreachable];
}

bb2: {
StorageDead(_6);
StorageDead(_5);
_0 = const ();
StorageDead(_4);
StorageDead(_1);
return;
}
}

50 changes: 50 additions & 0 deletions tests/mir-opt/const_prop/borrowed_variable.mutborrow.GVN.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
- // MIR for `mutborrow` before GVN
+ // MIR for `mutborrow` after GVN

fn mutborrow() -> () {
let mut _0: ();
let mut _1: i32;
let _2: ();
let mut _3: i32;
let _5: ();
let mut _6: i32;
scope 1 {
debug x => _1;
let _4: &mut i32;
scope 2 {
debug r => _4;
}
}

bb0: {
StorageLive(_1);
_1 = const 42_i32;
StorageLive(_2);
StorageLive(_3);
- _3 = copy _1;
- _2 = opaque::<i32>(move _3) -> [return: bb1, unwind unreachable];
+ _3 = const 42_i32;
+ _2 = opaque::<i32>(const 42_i32) -> [return: bb1, unwind unreachable];
}

bb1: {
StorageDead(_3);
StorageDead(_2);
StorageLive(_4);
_4 = &mut _1;
StorageLive(_5);
StorageLive(_6);
_6 = copy _1;
_5 = opaque::<i32>(move _6) -> [return: bb2, unwind unreachable];
}

bb2: {
StorageDead(_6);
StorageDead(_5);
_0 = const ();
StorageDead(_4);
StorageDead(_1);
return;
}
}

50 changes: 50 additions & 0 deletions tests/mir-opt/const_prop/borrowed_variable.mutptr.GVN.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
- // MIR for `mutptr` before GVN
+ // MIR for `mutptr` after GVN

fn mutptr() -> () {
let mut _0: ();
let mut _1: i32;
let _2: ();
let mut _3: i32;
let _5: ();
let mut _6: i32;
scope 1 {
debug x => _1;
let _4: *mut i32;
scope 2 {
debug r => _4;
}
}

bb0: {
StorageLive(_1);
_1 = const 42_i32;
StorageLive(_2);
StorageLive(_3);
- _3 = copy _1;
- _2 = opaque::<i32>(move _3) -> [return: bb1, unwind unreachable];
+ _3 = const 42_i32;
+ _2 = opaque::<i32>(const 42_i32) -> [return: bb1, unwind unreachable];
}

bb1: {
StorageDead(_3);
StorageDead(_2);
StorageLive(_4);
_4 = &raw mut _1;
StorageLive(_5);
StorageLive(_6);
_6 = copy _1;
_5 = opaque::<i32>(move _6) -> [return: bb2, unwind unreachable];
}

bb2: {
StorageDead(_6);
StorageDead(_5);
_0 = const ();
StorageDead(_4);
StorageDead(_1);
return;
}
}

64 changes: 64 additions & 0 deletions tests/mir-opt/const_prop/borrowed_variable.nonfreeze.GVN.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
- // MIR for `nonfreeze` before GVN
+ // MIR for `nonfreeze` after GVN

fn nonfreeze(_1: T) -> () {
debug x => _1;
let mut _0: ();
let mut _2: T;
let _3: ();
let mut _4: T;
let _6: ();
let mut _7: T;
let _8: ();
let mut _9: T;
scope 1 {
debug y => _2;
let _5: &T;
scope 2 {
debug r => _5;
}
}

bb0: {
- StorageLive(_2);
+ nop;
_2 = copy _1;
StorageLive(_3);
StorageLive(_4);
_4 = copy _2;
- _3 = opaque::<T>(move _4) -> [return: bb1, unwind unreachable];
+ _3 = opaque::<T>(copy _2) -> [return: bb1, unwind unreachable];
}

bb1: {
StorageDead(_4);
StorageDead(_3);
StorageLive(_5);
_5 = &_1;
StorageLive(_6);
StorageLive(_7);
_7 = copy _1;
_6 = opaque::<T>(move _7) -> [return: bb2, unwind unreachable];
}

bb2: {
StorageDead(_7);
StorageDead(_6);
StorageLive(_8);
StorageLive(_9);
_9 = copy _2;
- _8 = opaque::<T>(move _9) -> [return: bb3, unwind unreachable];
+ _8 = opaque::<T>(copy _2) -> [return: bb3, unwind unreachable];
}

bb3: {
StorageDead(_9);
StorageDead(_8);
_0 = const ();
StorageDead(_5);
- StorageDead(_2);
+ nop;
return;
}
}

Loading
Loading