Skip to content

Commit b75587b

Browse files
committed
Extend GVN to perform local value numbering.
1 parent e9f9832 commit b75587b

27 files changed

+331
-171
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 121 additions & 54 deletions
Large diffs are not rendered by default.

compiler/rustc_mir_transform/src/ssa.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,23 @@ impl SsaLocals {
9999
ssa
100100
}
101101

102+
#[inline]
102103
pub(super) fn num_locals(&self) -> usize {
103104
self.assignments.len()
104105
}
105106

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

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

114117
/// Return the number of uses if a local that are not "Deref".
118+
#[inline]
115119
pub(super) fn num_direct_uses(&self, local: Local) -> u32 {
116120
self.direct_uses[local]
117121
}

tests/coverage/closure.cov-map

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,22 @@ Number of file 0 mappings: 66
7474
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
7575
Highest counter ID seen: c1
7676

77-
Function name: closure::main::{closure#0}
78-
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]
77+
Function name: closure::main::{closure#0} (unused)
78+
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]
7979
Number of files: 1
8080
- file 0 => $DIR/closure.rs
81-
Number of expressions: 1
82-
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
81+
Number of expressions: 0
8382
Number of file 0 mappings: 9
84-
- Code(Counter(0)) at (prev + 40, 5) to (start + 0, 6)
85-
- Code(Counter(0)) at (prev + 1, 13) to (start + 0, 26)
86-
- Code(Counter(0)) at (prev + 0, 29) to (start + 0, 30)
87-
- Code(Counter(0)) at (prev + 1, 12) to (start + 0, 20)
88-
- Code(Counter(1)) at (prev + 0, 21) to (start + 2, 10)
89-
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 0, 10)
90-
= (c0 - c1)
91-
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 23)
92-
- Code(Counter(0)) at (prev + 0, 24) to (start + 0, 32)
93-
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 6)
94-
Highest counter ID seen: c1
83+
- Code(Zero) at (prev + 40, 5) to (start + 0, 6)
84+
- Code(Zero) at (prev + 1, 13) to (start + 0, 26)
85+
- Code(Zero) at (prev + 0, 29) to (start + 0, 30)
86+
- Code(Zero) at (prev + 1, 12) to (start + 0, 20)
87+
- Code(Zero) at (prev + 0, 21) to (start + 2, 10)
88+
- Code(Zero) at (prev + 2, 9) to (start + 0, 10)
89+
- Code(Zero) at (prev + 1, 9) to (start + 0, 23)
90+
- Code(Zero) at (prev + 0, 24) to (start + 0, 32)
91+
- Code(Zero) at (prev + 1, 5) to (start + 0, 6)
92+
Highest counter ID seen: (none)
9593

9694
Function name: closure::main::{closure#10} (unused)
9795
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]
@@ -199,24 +197,22 @@ Number of file 0 mappings: 7
199197
- Code(Counter(0)) at (prev + 2, 9) to (start + 0, 10)
200198
Highest counter ID seen: c1
201199

202-
Function name: closure::main::{closure#18}
203-
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]
200+
Function name: closure::main::{closure#18} (unused)
201+
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]
204202
Number of files: 1
205203
- file 0 => $DIR/closure.rs
206-
Number of expressions: 1
207-
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
204+
Number of expressions: 0
208205
Number of file 0 mappings: 9
209-
- Code(Counter(0)) at (prev + 25, 13) to (start + 0, 14)
210-
- Code(Counter(0)) at (prev + 1, 21) to (start + 0, 34)
211-
- Code(Counter(0)) at (prev + 0, 37) to (start + 0, 38)
212-
- Code(Counter(0)) at (prev + 1, 20) to (start + 0, 28)
213-
- Code(Counter(1)) at (prev + 0, 29) to (start + 2, 18)
214-
- Code(Expression(0, Sub)) at (prev + 2, 17) to (start + 0, 18)
215-
= (c0 - c1)
216-
- Code(Counter(0)) at (prev + 1, 17) to (start + 0, 31)
217-
- Code(Counter(0)) at (prev + 0, 32) to (start + 0, 40)
218-
- Code(Counter(0)) at (prev + 1, 13) to (start + 0, 14)
219-
Highest counter ID seen: c1
206+
- Code(Zero) at (prev + 25, 13) to (start + 0, 14)
207+
- Code(Zero) at (prev + 1, 21) to (start + 0, 34)
208+
- Code(Zero) at (prev + 0, 37) to (start + 0, 38)
209+
- Code(Zero) at (prev + 1, 20) to (start + 0, 28)
210+
- Code(Zero) at (prev + 0, 29) to (start + 2, 18)
211+
- Code(Zero) at (prev + 2, 17) to (start + 0, 18)
212+
- Code(Zero) at (prev + 1, 17) to (start + 0, 31)
213+
- Code(Zero) at (prev + 0, 32) to (start + 0, 40)
214+
- Code(Zero) at (prev + 1, 13) to (start + 0, 14)
215+
Highest counter ID seen: (none)
220216

221217
Function name: closure::main::{closure#19}
222218
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]

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,21 @@
3030
}
3131

3232
bb1: {
33-
StorageLive(_7);
33+
- StorageLive(_7);
34+
+ nop;
3435
_7 = ShallowInitBox(move _6, i32);
3536
_8 = copy ((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
3637
(*_8) = const 42_i32;
37-
_3 = move _7;
38-
StorageDead(_7);
39-
_9 = copy ((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
40-
_2 = copy (*_9);
38+
- _3 = move _7;
39+
- StorageDead(_7);
40+
- _9 = copy ((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
41+
- _2 = copy (*_9);
4142
- _1 = Add(move _2, const 0_i32);
4243
- StorageDead(_2);
44+
+ _3 = copy _7;
45+
+ nop;
46+
+ _9 = copy _8;
47+
+ _2 = copy (*_8);
4348
+ _1 = copy _2;
4449
+ nop;
4550
drop(_3) -> [return: bb2, unwind unreachable];

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,21 @@
3030
}
3131

3232
bb1: {
33-
StorageLive(_7);
33+
- StorageLive(_7);
34+
+ nop;
3435
_7 = ShallowInitBox(move _6, i32);
3536
_8 = copy ((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
3637
(*_8) = const 42_i32;
37-
_3 = move _7;
38-
StorageDead(_7);
39-
_9 = copy ((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
40-
_2 = copy (*_9);
38+
- _3 = move _7;
39+
- StorageDead(_7);
40+
- _9 = copy ((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
41+
- _2 = copy (*_9);
4142
- _1 = Add(move _2, const 0_i32);
4243
- StorageDead(_2);
44+
+ _3 = copy _7;
45+
+ nop;
46+
+ _9 = copy _8;
47+
+ _2 = copy (*_8);
4348
+ _1 = copy _2;
4449
+ nop;
4550
drop(_3) -> [return: bb2, unwind: bb3];

tests/mir-opt/const_prop/discriminant.main.GVN.32bit.diff

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
bb0: {
1717
StorageLive(_1);
18-
StorageLive(_2);
18+
- StorageLive(_2);
19+
+ nop;
1920
StorageLive(_3);
2021
- _3 = Option::<bool>::Some(const true);
2122
- _4 = discriminant(_3);
@@ -41,8 +42,10 @@
4142
}
4243

4344
bb4: {
44-
_1 = Add(move _2, const 0_i32);
45-
StorageDead(_2);
45+
- _1 = Add(move _2, const 0_i32);
46+
- StorageDead(_2);
47+
+ _1 = copy _2;
48+
+ nop;
4649
StorageDead(_3);
4750
_0 = const ();
4851
StorageDead(_1);

tests/mir-opt/const_prop/discriminant.main.GVN.64bit.diff

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
bb0: {
1717
StorageLive(_1);
18-
StorageLive(_2);
18+
- StorageLive(_2);
19+
+ nop;
1920
StorageLive(_3);
2021
- _3 = Option::<bool>::Some(const true);
2122
- _4 = discriminant(_3);
@@ -41,8 +42,10 @@
4142
}
4243

4344
bb4: {
44-
_1 = Add(move _2, const 0_i32);
45-
StorageDead(_2);
45+
- _1 = Add(move _2, const 0_i32);
46+
- StorageDead(_2);
47+
+ _1 = copy _2;
48+
+ nop;
4649
StorageDead(_3);
4750
_0 = const ();
4851
StorageDead(_1);

tests/mir-opt/const_prop/discriminant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ fn main() {
2020
// CHECK: [[tmp]] = const 10_i32;
2121
// CHECK: goto -> bb4;
2222
// CHECK: bb4: {
23-
// CHECK: {{_.*}} = Add(move [[tmp]], const 0_i32);
23+
// CHECK: {{_.*}} = copy [[tmp]];
2424
let x = (if let Some(true) = Some(true) { 42 } else { 10 }) + 0;
2525
}

tests/mir-opt/const_prop/mutable_variable.main.GVN.diff

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
_1 = const 42_i32;
1818
_1 = const 99_i32;
1919
StorageLive(_2);
20-
_2 = copy _1;
20+
- _2 = copy _1;
21+
+ _2 = const 99_i32;
2122
_0 = const ();
2223
StorageDead(_2);
2324
StorageDead(_1);

tests/mir-opt/const_prop/mutable_variable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn main() {
77
// CHECK: debug y => [[y:_.*]];
88
// CHECK: [[x]] = const 42_i32;
99
// CHECK: [[x]] = const 99_i32;
10-
// CHECK: [[y]] = copy [[x]];
10+
// CHECK: [[y]] = const 99_i32;
1111
let mut x = 42;
1212
x = 99;
1313
let y = x;

0 commit comments

Comments
 (0)