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
use crate::analysis::{reaching_definitions, LocationSet};
use crate::error::*;
use crate::ir;
use std::collections::HashMap;
#[allow(dead_code)]
pub fn use_def<'r, V: ir::Value>(
function: &'r ir::Function<V>,
) -> Result<HashMap<ir::ProgramLocation, LocationSet>> {
let rd = reaching_definitions::reaching_definitions(function)?;
use_def_rd(function, &rd)
}
pub fn use_def_rd<'r, V: ir::Value>(
function: &'r ir::Function<V>,
rd: &HashMap<ir::ProgramLocation, LocationSet>,
) -> Result<HashMap<ir::ProgramLocation, LocationSet>> {
let mut ud: HashMap<ir::ProgramLocation, LocationSet> = HashMap::new();
for (location, _) in rd {
let rpl = location.apply(function)?;
let defs = match rpl.function_location() {
ir::RefFunctionLocation::Instruction(_, instruction) => instruction
.operation()
.variables_read()
.unwrap_or_else(|| Vec::new())
.into_iter()
.fold(LocationSet::new(), |mut defs, variable_read| {
rd[&location].locations().into_iter().for_each(|rd| {
rd.apply(function)
.unwrap()
.instruction()
.unwrap()
.operation()
.variables_written()
.unwrap_or_else(|| Vec::new())
.into_iter()
.for_each(|variable_written| {
if variable_written == variable_read {
defs.insert(rd.clone());
}
})
});
defs
}),
ir::RefFunctionLocation::Edge(ref edge) => {
if let Some(condition_variables) =
edge.condition().map(|condition| condition.variables())
{
condition_variables.into_iter().fold(
LocationSet::new(),
|mut defs, variable_read| {
rd[&location].locations().into_iter().for_each(|rd| {
rd.apply(function)
.unwrap()
.instruction()
.unwrap()
.operation()
.variables_written()
.unwrap_or_else(|| Vec::new())
.into_iter()
.for_each(|variable_written| {
if variable_written == variable_read {
defs.insert(rd.clone());
}
})
});
defs
},
)
} else {
LocationSet::new()
}
}
ir::RefFunctionLocation::EmptyBlock(_) => LocationSet::new(),
};
ud.insert(location.clone(), defs);
}
Ok(ud)
}