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
162
163
use rustc::mir::{Local, Location, Lvalue, Mir};
use rustc::mir::visit::{LvalueContext, MutVisitor, Visitor};
use rustc_data_structures::indexed_vec::IndexVec;
use std::marker::PhantomData;
use std::mem;
pub struct DefUseAnalysis<'tcx> {
info: IndexVec<Local, Info<'tcx>>,
}
#[derive(Clone)]
pub struct Info<'tcx> {
pub defs_and_uses: Vec<Use<'tcx>>,
}
#[derive(Clone)]
pub struct Use<'tcx> {
pub context: LvalueContext<'tcx>,
pub location: Location,
}
impl<'tcx> DefUseAnalysis<'tcx> {
pub fn new(mir: &Mir<'tcx>) -> DefUseAnalysis<'tcx> {
DefUseAnalysis {
info: IndexVec::from_elem_n(Info::new(), mir.local_decls.len()),
}
}
pub fn analyze(&mut self, mir: &Mir<'tcx>) {
let mut finder = DefUseFinder {
info: mem::replace(&mut self.info, IndexVec::new()),
};
finder.visit_mir(mir);
self.info = finder.info
}
pub fn local_info(&self, local: Local) -> &Info<'tcx> {
&self.info[local]
}
pub fn local_info_mut(&mut self, local: Local) -> &mut Info<'tcx> {
&mut self.info[local]
}
fn mutate_defs_and_uses<F>(&self, local: Local, mir: &mut Mir<'tcx>, mut callback: F)
where F: for<'a> FnMut(&'a mut Lvalue<'tcx>,
LvalueContext<'tcx>,
Location) {
for lvalue_use in &self.info[local].defs_and_uses {
MutateUseVisitor::new(local,
&mut callback,
mir).visit_location(mir, lvalue_use.location)
}
}
pub fn replace_all_defs_and_uses_with(&self,
local: Local,
mir: &mut Mir<'tcx>,
new_lvalue: Lvalue<'tcx>) {
self.mutate_defs_and_uses(local, mir, |lvalue, _, _| *lvalue = new_lvalue.clone())
}
}
struct DefUseFinder<'tcx> {
info: IndexVec<Local, Info<'tcx>>,
}
impl<'tcx> DefUseFinder<'tcx> {
fn lvalue_mut_info(&mut self, lvalue: &Lvalue<'tcx>) -> Option<&mut Info<'tcx>> {
let info = &mut self.info;
if let Lvalue::Local(local) = *lvalue {
Some(&mut info[local])
} else {
None
}
}
}
impl<'tcx> Visitor<'tcx> for DefUseFinder<'tcx> {
fn visit_lvalue(&mut self,
lvalue: &Lvalue<'tcx>,
context: LvalueContext<'tcx>,
location: Location) {
if let Some(ref mut info) = self.lvalue_mut_info(lvalue) {
info.defs_and_uses.push(Use {
context: context,
location: location,
})
}
self.super_lvalue(lvalue, context, location)
}
}
impl<'tcx> Info<'tcx> {
fn new() -> Info<'tcx> {
Info {
defs_and_uses: vec![],
}
}
pub fn def_count(&self) -> usize {
self.defs_and_uses.iter().filter(|lvalue_use| lvalue_use.context.is_mutating_use()).count()
}
pub fn def_count_not_including_drop(&self) -> usize {
self.defs_and_uses.iter().filter(|lvalue_use| {
lvalue_use.context.is_mutating_use() && !lvalue_use.context.is_drop()
}).count()
}
pub fn use_count(&self) -> usize {
self.defs_and_uses.iter().filter(|lvalue_use| {
lvalue_use.context.is_nonmutating_use()
}).count()
}
}
struct MutateUseVisitor<'tcx, F> {
query: Local,
callback: F,
phantom: PhantomData<&'tcx ()>,
}
impl<'tcx, F> MutateUseVisitor<'tcx, F> {
fn new(query: Local, callback: F, _: &Mir<'tcx>)
-> MutateUseVisitor<'tcx, F>
where F: for<'a> FnMut(&'a mut Lvalue<'tcx>, LvalueContext<'tcx>, Location) {
MutateUseVisitor {
query: query,
callback: callback,
phantom: PhantomData,
}
}
}
impl<'tcx, F> MutVisitor<'tcx> for MutateUseVisitor<'tcx, F>
where F: for<'a> FnMut(&'a mut Lvalue<'tcx>, LvalueContext<'tcx>, Location) {
fn visit_lvalue(&mut self,
lvalue: &mut Lvalue<'tcx>,
context: LvalueContext<'tcx>,
location: Location) {
if let Lvalue::Local(local) = *lvalue {
if local == self.query {
(self.callback)(lvalue, context, location)
}
}
self.super_lvalue(lvalue, context, location)
}
}