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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use def_use::DefUseAnalysis;
use rustc::mir::{Constant, Local, LocalKind, Location, Lvalue, Mir, Operand, Rvalue, StatementKind};
use rustc::mir::transform::{MirPass, MirSource, Pass};
use rustc::mir::visit::MutVisitor;
use rustc::ty::TyCtxt;
use transform::qualify_consts;
pub struct CopyPropagation;
impl Pass for CopyPropagation {}
impl<'tcx> MirPass<'tcx> for CopyPropagation {
fn run_pass<'a>(&mut self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
source: MirSource,
mir: &mut Mir<'tcx>) {
match source {
MirSource::Const(_) => {
return
}
MirSource::Static(..) | MirSource::Promoted(..) => {
return
}
MirSource::Fn(function_node_id) => {
if qualify_consts::is_const_fn(tcx, tcx.hir.local_def_id(function_node_id)) {
return
}
}
}
if tcx.sess.opts.debugging_opts.mir_opt_level <= 1 {
return;
}
loop {
let mut def_use_analysis = DefUseAnalysis::new(mir);
def_use_analysis.analyze(mir);
let mut changed = false;
for dest_local in mir.local_decls.indices() {
debug!("Considering destination local: {:?}", dest_local);
let action;
let location;
{
let dest_use_info = def_use_analysis.local_info(dest_local);
let dest_def_count = dest_use_info.def_count_not_including_drop();
if dest_def_count == 0 {
debug!(" Can't copy-propagate local: dest {:?} undefined",
dest_local);
continue
}
if dest_def_count > 1 {
debug!(" Can't copy-propagate local: dest {:?} defined {} times",
dest_local,
dest_use_info.def_count());
continue
}
if dest_use_info.use_count() == 0 {
debug!(" Can't copy-propagate local: dest {:?} unused",
dest_local);
continue
}
let dest_lvalue_def = dest_use_info.defs_and_uses.iter().filter(|lvalue_def| {
lvalue_def.context.is_mutating_use() && !lvalue_def.context.is_drop()
}).next().unwrap();
location = dest_lvalue_def.location;
let basic_block = &mir[location.block];
let statement_index = location.statement_index;
let statement = match basic_block.statements.get(statement_index) {
Some(statement) => statement,
None => {
debug!(" Can't copy-propagate local: used in terminator");
continue
}
};
match statement.kind {
StatementKind::Assign(Lvalue::Local(local), Rvalue::Use(ref operand)) if
local == dest_local => {
let maybe_action = match *operand {
Operand::Consume(ref src_lvalue) => {
Action::local_copy(&mir, &def_use_analysis, src_lvalue)
}
Operand::Constant(ref src_constant) => {
Action::constant(src_constant)
}
};
match maybe_action {
Some(this_action) => action = this_action,
None => continue,
}
}
_ => {
debug!(" Can't copy-propagate local: source use is not an \
assignment");
continue
}
}
}
changed = action.perform(mir, &def_use_analysis, dest_local, location) || changed;
break
}
if !changed {
break
}
}
}
}
enum Action<'tcx> {
PropagateLocalCopy(Local),
PropagateConstant(Constant<'tcx>),
}
impl<'tcx> Action<'tcx> {
fn local_copy(mir: &Mir<'tcx>, def_use_analysis: &DefUseAnalysis, src_lvalue: &Lvalue<'tcx>)
-> Option<Action<'tcx>> {
let src_local = if let Lvalue::Local(local) = *src_lvalue {
local
} else {
debug!(" Can't copy-propagate local: source is not a local");
return None;
};
let src_use_info = def_use_analysis.local_info(src_local);
let src_use_count = src_use_info.use_count();
if src_use_count == 0 {
debug!(" Can't copy-propagate local: no uses");
return None
}
if src_use_count != 1 {
debug!(" Can't copy-propagate local: {} uses", src_use_info.use_count());
return None
}
let src_def_count = src_use_info.def_count_not_including_drop();
if src_def_count > 1 ||
(src_def_count == 0 && mir.local_kind(src_local) != LocalKind::Arg) {
debug!(" Can't copy-propagate local: {} defs of src",
src_use_info.def_count_not_including_drop());
return None
}
Some(Action::PropagateLocalCopy(src_local))
}
fn constant(src_constant: &Constant<'tcx>) -> Option<Action<'tcx>> {
Some(Action::PropagateConstant((*src_constant).clone()))
}
fn perform(self,
mir: &mut Mir<'tcx>,
def_use_analysis: &DefUseAnalysis<'tcx>,
dest_local: Local,
location: Location)
-> bool {
match self {
Action::PropagateLocalCopy(src_local) => {
debug!(" Replacing all uses of {:?} with {:?} (local)",
dest_local,
src_local);
for lvalue_use in &def_use_analysis.local_info(dest_local).defs_and_uses {
if lvalue_use.context.is_storage_marker() {
mir.make_statement_nop(lvalue_use.location)
}
}
for lvalue_use in &def_use_analysis.local_info(src_local).defs_and_uses {
if lvalue_use.context.is_storage_marker() {
mir.make_statement_nop(lvalue_use.location)
}
}
let src_lvalue = Lvalue::Local(src_local);
def_use_analysis.replace_all_defs_and_uses_with(dest_local, mir, src_lvalue);
debug!(" Deleting assignment");
mir.make_statement_nop(location);
true
}
Action::PropagateConstant(src_constant) => {
debug!(" Replacing all uses of {:?} with {:?} (constant)",
dest_local,
src_constant);
let dest_local_info = def_use_analysis.local_info(dest_local);
for lvalue_use in &dest_local_info.defs_and_uses {
if lvalue_use.context.is_storage_marker() {
mir.make_statement_nop(lvalue_use.location)
}
}
let mut visitor = ConstantPropagationVisitor::new(dest_local,
src_constant);
for dest_lvalue_use in &dest_local_info.defs_and_uses {
visitor.visit_location(mir, dest_lvalue_use.location)
}
let use_count = dest_local_info.use_count();
if visitor.uses_replaced == use_count {
debug!(" {} of {} use(s) replaced; deleting assignment",
visitor.uses_replaced,
use_count);
mir.make_statement_nop(location);
true
} else if visitor.uses_replaced == 0 {
debug!(" No uses replaced; not deleting assignment");
false
} else {
debug!(" {} of {} use(s) replaced; not deleting assignment",
visitor.uses_replaced,
use_count);
true
}
}
}
}
}
struct ConstantPropagationVisitor<'tcx> {
dest_local: Local,
constant: Constant<'tcx>,
uses_replaced: usize,
}
impl<'tcx> ConstantPropagationVisitor<'tcx> {
fn new(dest_local: Local, constant: Constant<'tcx>)
-> ConstantPropagationVisitor<'tcx> {
ConstantPropagationVisitor {
dest_local: dest_local,
constant: constant,
uses_replaced: 0,
}
}
}
impl<'tcx> MutVisitor<'tcx> for ConstantPropagationVisitor<'tcx> {
fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) {
self.super_operand(operand, location);
match *operand {
Operand::Consume(Lvalue::Local(local)) if local == self.dest_local => {}
_ => return,
}
*operand = Operand::Constant(self.constant.clone());
self.uses_replaced += 1
}
}