This repository has been archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 671
/
reaching_def_analysis.rs
319 lines (285 loc) · 11 KB
/
reaching_def_analysis.rs
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
// Copyright (c) The Diem Core Contributors
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0
// Reaching definition analysis with subsequent copy propagation.
//
// This analysis and transformation only propagates definitions, leaving dead assignments
// in the code. The subsequent livevar_analysis takes care of removing those.
use std::collections::{BTreeMap, BTreeSet};
use itertools::Itertools;
use move_binary_format::file_format::CodeOffset;
use move_model::{ast::TempIndex, model::FunctionEnv};
use crate::{
dataflow_analysis::{DataflowAnalysis, TransferFunctions},
dataflow_domains::{AbstractDomain, JoinResult},
function_target::{FunctionData, FunctionTarget},
function_target_pipeline::{FunctionTargetProcessor, FunctionTargetsHolder},
stackless_bytecode::{AbortAction, BorrowNode, Bytecode, Operation},
stackless_control_flow_graph::StacklessControlFlowGraph,
};
/// The reaching definitions we are capturing. Currently we only capture
/// aliases (assignment).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Def {
Alias(TempIndex),
}
type DefMap = BTreeMap<TempIndex, BTreeSet<Def>>;
type HavocSet = BTreeSet<TempIndex>;
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Default)]
pub struct ReachingDefState {
pub map: DefMap,
pub havoced: HavocSet,
}
/// The annotation for reaching definitions. For each code position, we have a map of local
/// indices to the set of definitions reaching the code position.
#[derive(Default)]
pub struct ReachingDefAnnotation(BTreeMap<CodeOffset, ReachingDefState>);
pub struct ReachingDefProcessor {}
impl ReachingDefProcessor {
pub fn new() -> Box<Self> {
Box::new(ReachingDefProcessor {})
}
/// Returns Some(temp, def) if temp has a unique reaching definition and None otherwise.
fn get_unique_def(
temp: TempIndex,
defs: &BTreeSet<Def>,
havoc_vars: &HavocSet,
) -> Option<(TempIndex, TempIndex)> {
if defs.len() != 1 {
return None;
}
let Def::Alias(def) = defs.iter().next().unwrap();
if havoc_vars.contains(def) {
return None;
}
Some((temp, *def))
}
/// Gets the propagated local resolving aliases using the reaching definitions.
fn get_propagated_local(temp: TempIndex, state: &ReachingDefState) -> TempIndex {
// For being robust, we protect this function against cycles in alias definitions. If
// a cycle is detected, alias resolution stops.
fn get(
temp: TempIndex,
state: &ReachingDefState,
visited: &mut BTreeSet<TempIndex>,
) -> TempIndex {
if let Some(defs) = state.map.get(&temp) {
if let Some((_, def_temp)) =
ReachingDefProcessor::get_unique_def(temp, defs, &state.havoced)
{
if visited.insert(def_temp) {
return get(def_temp, state, visited);
}
}
}
temp
}
let mut visited = BTreeSet::new();
get(temp, state, &mut visited)
}
/// Perform copy propagation based on reaching definitions analysis results.
pub fn copy_propagation(
target: &FunctionTarget<'_>,
code: Vec<Bytecode>,
defs: &ReachingDefAnnotation,
) -> Vec<Bytecode> {
let default_state = ReachingDefState::default();
let mut res = vec![];
for (pc, bytecode) in code.into_iter().enumerate() {
let state = defs.0.get(&(pc as CodeOffset)).unwrap_or(&default_state);
let mut propagate = |local| Self::get_propagated_local(local, state);
res.push(bytecode.remap_src_vars(target, &mut propagate));
}
res
}
/// Compute the set of locals which are borrowed from or which are otherwise used to refer to.
/// We can't alias such locals to other locals because of reference semantics.
fn borrowed_locals(&self, code: &[Bytecode]) -> BTreeSet<TempIndex> {
use Bytecode::*;
code.iter()
.filter_map(|bc| match bc {
Call(_, _, Operation::BorrowLoc, srcs, _) => Some(srcs[0]),
Call(_, _, Operation::WriteBack(BorrowNode::LocalRoot(src), ..), ..)
| Call(_, _, Operation::IsParent(BorrowNode::LocalRoot(src), ..), ..) => Some(*src),
Call(_, _, Operation::WriteBack(BorrowNode::Reference(src), ..), ..)
| Call(_, _, Operation::IsParent(BorrowNode::Reference(src), ..), ..) => Some(*src),
_ => None,
})
.collect()
}
}
impl FunctionTargetProcessor for ReachingDefProcessor {
fn process(
&self,
_targets: &mut FunctionTargetsHolder,
func_env: &FunctionEnv,
mut data: FunctionData,
_scc_opt: Option<&[FunctionEnv]>,
) -> FunctionData {
if !func_env.is_native() {
let cfg = StacklessControlFlowGraph::new_forward(&data.code);
let analyzer = ReachingDefAnalysis {
_target: FunctionTarget::new(func_env, &data),
borrowed_locals: self.borrowed_locals(&data.code),
};
let block_state_map = analyzer.analyze_function(
ReachingDefState {
map: BTreeMap::new(),
havoced: BTreeSet::new(),
},
&data.code,
&cfg,
);
let per_bytecode_state =
analyzer.state_per_instruction(block_state_map, &data.code, &cfg, |before, _| {
before.clone()
});
// Run copy propagation transformation.
let annotations = ReachingDefAnnotation(per_bytecode_state);
let code = std::mem::take(&mut data.code);
let target = FunctionTarget::new(func_env, &data);
let new_code = Self::copy_propagation(&target, code, &annotations);
data.code = new_code;
// Currently we do not need reaching defs after this phase. If so in the future, we
// need to uncomment this statement.
//data.annotations.set(annotations);
}
data
}
fn name(&self) -> String {
"reaching_def_analysis".to_string()
}
}
struct ReachingDefAnalysis<'a> {
_target: FunctionTarget<'a>,
borrowed_locals: BTreeSet<TempIndex>,
}
impl<'a> ReachingDefAnalysis<'a> {}
impl<'a> TransferFunctions for ReachingDefAnalysis<'a> {
type State = ReachingDefState;
const BACKWARD: bool = false;
fn execute(&self, state: &mut ReachingDefState, instr: &Bytecode, _offset: CodeOffset) {
use BorrowNode::*;
use Bytecode::*;
use Operation::*;
match instr {
Assign(_, dest, src, _) => {
state.kill(*dest);
if !self.borrowed_locals.contains(dest) && !self.borrowed_locals.contains(src) {
state.def_alias(*dest, *src);
}
}
Load(_, dest, ..) => {
state.kill(*dest);
}
Call(_, dests, oper, _, on_abort) => {
// generic kills
for dest in dests {
state.kill(*dest);
}
if let Some(AbortAction(_, dest)) = on_abort {
state.kill(*dest);
}
// op-specific actions
match oper {
WriteBack(LocalRoot(local_root), ..) => {
state.kill(*local_root);
}
Havoc(_) => {
state.havoc(dests[0]);
}
_ => (),
}
}
_ => {}
}
}
}
impl<'a> DataflowAnalysis for ReachingDefAnalysis<'a> {}
impl AbstractDomain for ReachingDefState {
fn join(&mut self, other: &Self) -> JoinResult {
let mut result = JoinResult::Unchanged;
for idx in self.map.keys().cloned().collect_vec() {
if let Some(other_defs) = other.map.get(&idx) {
// Union of definitions
let defs = self.map.get_mut(&idx).unwrap();
for d in other_defs {
if defs.insert(d.clone()) {
result = JoinResult::Changed;
}
}
} else {
// Kill this definition as it is not contained in both incoming states.
self.map.remove(&idx);
result = JoinResult::Changed;
}
}
result
}
}
impl ReachingDefState {
fn def_alias(&mut self, dest: TempIndex, src: TempIndex) {
// ensure that the previous def is killed
assert!(!self.map.contains_key(&dest));
// cascade the definition
for defs in self.map.values_mut() {
if defs.contains(&Def::Alias(dest)) {
defs.insert(Def::Alias(src));
}
}
// update the new alias
self.map.entry(dest).or_default().insert(Def::Alias(src));
}
fn kill(&mut self, dest: TempIndex) {
self.map.remove(&dest);
self.havoced.remove(&dest);
}
fn havoc(&mut self, dest: TempIndex) {
self.havoced.insert(dest);
}
}
// =================================================================================================
// Formatting
/// Format a reaching definition annotation.
pub fn format_reaching_def_annotation(
target: &FunctionTarget<'_>,
code_offset: CodeOffset,
) -> Option<String> {
if let Some(ReachingDefAnnotation(map)) =
target.get_annotations().get::<ReachingDefAnnotation>()
{
if let Some(map_at) = map.get(&code_offset) {
let mut res = map_at
.map
.iter()
.map(|(idx, defs)| {
let name = target.get_local_name(*idx);
format!(
"{} -> {{{}}}",
name.display(target.symbol_pool()),
defs.iter()
.map(|def| {
match def {
Def::Alias(a) => {
let local_name = format!(
"{}",
target.get_local_name(*a).display(target.symbol_pool())
);
if map_at.havoced.contains(a) {
format!("{}, {}*", local_name, local_name)
} else {
local_name
}
}
}
})
.join(", ")
)
})
.join(", ");
res.insert_str(0, "reach: ");
return Some(res);
}
}
None
}