1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use borrow_check::nll::region_infer::values::{PointIndex, RegionValueElements};
use borrow_check::nll::type_check::liveness::liveness_map::{LiveVar, NllLivenessMap};
use rustc::mir::visit::{PlaceContext, Visitor};
use rustc::mir::{Local, Location, Mir};
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use rustc_data_structures::vec_linked_list as vll;
use util::liveness::{categorize, DefUse, LiveVariableMap};
crate struct LocalUseMap<'me> {
liveness_map: &'me NllLivenessMap,
first_def_at: IndexVec<LiveVar, Option<AppearanceIndex>>,
first_use_at: IndexVec<LiveVar, Option<AppearanceIndex>>,
first_drop_at: IndexVec<LiveVar, Option<AppearanceIndex>>,
appearances: IndexVec<AppearanceIndex, Appearance>,
}
struct Appearance {
point_index: PointIndex,
next: Option<AppearanceIndex>,
}
newtype_index! {
pub struct AppearanceIndex { .. }
}
impl vll::LinkElem for Appearance {
type LinkIndex = AppearanceIndex;
fn next(elem: &Self) -> Option<AppearanceIndex> {
elem.next
}
}
impl LocalUseMap<'me> {
crate fn build(
liveness_map: &'me NllLivenessMap,
elements: &RegionValueElements,
mir: &Mir<'_>,
) -> Self {
let nones = IndexVec::from_elem_n(None, liveness_map.num_variables());
let mut local_use_map = LocalUseMap {
liveness_map,
first_def_at: nones.clone(),
first_use_at: nones.clone(),
first_drop_at: nones,
appearances: IndexVec::new(),
};
LocalUseMapBuild {
local_use_map: &mut local_use_map,
elements,
}.visit_mir(mir);
local_use_map
}
crate fn defs(&self, local: LiveVar) -> impl Iterator<Item = PointIndex> + '_ {
vll::iter(self.first_def_at[local], &self.appearances)
.map(move |aa| self.appearances[aa].point_index)
}
crate fn uses(&self, local: LiveVar) -> impl Iterator<Item = PointIndex> + '_ {
vll::iter(self.first_use_at[local], &self.appearances)
.map(move |aa| self.appearances[aa].point_index)
}
crate fn drops(&self, local: LiveVar) -> impl Iterator<Item = PointIndex> + '_ {
vll::iter(self.first_drop_at[local], &self.appearances)
.map(move |aa| self.appearances[aa].point_index)
}
}
struct LocalUseMapBuild<'me, 'map: 'me> {
local_use_map: &'me mut LocalUseMap<'map>,
elements: &'me RegionValueElements,
}
impl LocalUseMapBuild<'_, '_> {
fn insert_def(&mut self, local: LiveVar, location: Location) {
Self::insert(
self.elements,
&mut self.local_use_map.first_def_at[local],
&mut self.local_use_map.appearances,
location,
);
}
fn insert_use(&mut self, local: LiveVar, location: Location) {
Self::insert(
self.elements,
&mut self.local_use_map.first_use_at[local],
&mut self.local_use_map.appearances,
location,
);
}
fn insert_drop(&mut self, local: LiveVar, location: Location) {
Self::insert(
self.elements,
&mut self.local_use_map.first_drop_at[local],
&mut self.local_use_map.appearances,
location,
);
}
fn insert(
elements: &RegionValueElements,
first_appearance: &mut Option<AppearanceIndex>,
appearances: &mut IndexVec<AppearanceIndex, Appearance>,
location: Location,
) {
let point_index = elements.point_from_location(location);
let appearance_index = appearances.push(Appearance {
point_index,
next: *first_appearance,
});
*first_appearance = Some(appearance_index);
}
}
impl Visitor<'tcx> for LocalUseMapBuild<'_, '_> {
fn visit_local(&mut self, &local: &Local, context: PlaceContext<'tcx>, location: Location) {
if let Some(local_with_region) = self.local_use_map.liveness_map.from_local(local) {
match categorize(context) {
Some(DefUse::Def) => self.insert_def(local_with_region, location),
Some(DefUse::Use) => self.insert_use(local_with_region, location),
Some(DefUse::Drop) => self.insert_drop(local_with_region, location),
_ => (),
}
}
}
}