| 1 |
use super::arena::HandleSet; |
| 2 |
use super::{FunctionMap, ModuleMap}; |
| 3 |
|
| 4 |
pub struct FunctionTracer<'a> { |
| 5 |
pub function: &'a crate::Function, |
| 6 |
pub constants: &'a crate::Arena<crate::Constant>, |
| 7 |
pub overrides: &'a crate::Arena<crate::Override>, |
| 8 |
|
| 9 |
pub types_used: &'a mut HandleSet<crate::Type>, |
| 10 |
pub constants_used: &'a mut HandleSet<crate::Constant>, |
| 11 |
pub overrides_used: &'a mut HandleSet<crate::Override>, |
| 12 |
pub global_expressions_used: &'a mut HandleSet<crate::Expression>, |
| 13 |
|
| 14 |
/// Function-local expressions used. |
| 15 |
pub expressions_used: HandleSet<crate::Expression>, |
| 16 |
} |
| 17 |
|
| 18 |
impl FunctionTracer<'_> { |
| 19 |
pub fn trace(&mut self) { |
| 20 |
for argument in self.function.arguments.iter() { |
| 21 |
self.types_used.insert(argument.ty); |
| 22 |
} |
| 23 |
|
| 24 |
if let Some(ref result) = self.function.result { |
| 25 |
self.types_used.insert(result.ty); |
| 26 |
} |
| 27 |
|
| 28 |
for (_, local) in self.function.local_variables.iter() { |
| 29 |
self.types_used.insert(local.ty); |
| 30 |
if let Some(init) = local.init { |
| 31 |
self.expressions_used.insert(init); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// Treat named expressions as alive, for the sake of our test suite, |
| 36 |
// which uses `let blah = expr;` to exercise lots of things. |
| 37 |
for (&value, _name) in &self.function.named_expressions { |
| 38 |
self.expressions_used.insert(value); |
| 39 |
} |
| 40 |
|
| 41 |
self.trace_block(&self.function.body); |
| 42 |
|
| 43 |
// Given that `trace_block` has marked the expressions used |
| 44 |
// directly by statements, walk the arena to find all |
| 45 |
// expressions used, directly or indirectly. |
| 46 |
self.as_expression().trace_expressions(); |
| 47 |
} |
| 48 |
|
| 49 |
fn as_expression(&mut self) -> super::expressions::ExpressionTracer { |
| 50 |
super::expressions::ExpressionTracer { |
| 51 |
constants: self.constants, |
| 52 |
overrides: self.overrides, |
| 53 |
expressions: &self.function.expressions, |
| 54 |
|
| 55 |
types_used: self.types_used, |
| 56 |
constants_used: self.constants_used, |
| 57 |
overrides_used: self.overrides_used, |
| 58 |
expressions_used: &mut self.expressions_used, |
| 59 |
global_expressions_used: Some(&mut self.global_expressions_used), |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl FunctionMap { |
| 65 |
pub fn compact( |
| 66 |
&self, |
| 67 |
function: &mut crate::Function, |
| 68 |
module_map: &ModuleMap, |
| 69 |
reuse: &mut crate::NamedExpressions, |
| 70 |
) { |
| 71 |
assert!(reuse.is_empty()); |
| 72 |
|
| 73 |
for argument in function.arguments.iter_mut() { |
| 74 |
module_map.types.adjust(&mut argument.ty); |
| 75 |
} |
| 76 |
|
| 77 |
if let Some(ref mut result) = function.result { |
| 78 |
module_map.types.adjust(&mut result.ty); |
| 79 |
} |
| 80 |
|
| 81 |
for (_, local) in function.local_variables.iter_mut() { |
| 82 |
log::trace!("adjusting local variable {:?}", local.name); |
| 83 |
module_map.types.adjust(&mut local.ty); |
| 84 |
if let Some(ref mut init) = local.init { |
| 85 |
self.expressions.adjust(init); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// Drop unused expressions, reusing existing storage. |
| 90 |
function.expressions.retain_mut(|handle, expr| { |
| 91 |
if self.expressions.used(handle) { |
| 92 |
module_map.adjust_expression(expr, &self.expressions); |
| 93 |
true |
| 94 |
} else { |
| 95 |
false |
| 96 |
} |
| 97 |
}); |
| 98 |
|
| 99 |
// Adjust named expressions. |
| 100 |
for (mut handle, name) in function.named_expressions.drain(..) { |
| 101 |
self.expressions.adjust(&mut handle); |
| 102 |
reuse.insert(handle, name); |
| 103 |
} |
| 104 |
core::mem::swap(&mut function.named_expressions, reuse); |
| 105 |
assert!(reuse.is_empty()); |
| 106 |
|
| 107 |
// Adjust statements. |
| 108 |
self.adjust_body(function); |
| 109 |
} |
| 110 |
} |