more cleaned up

This commit is contained in:
Folkert 2022-03-21 22:54:24 +01:00
parent a170f461e0
commit 1b1a7b0385
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
2 changed files with 20 additions and 61 deletions

View file

@ -1,14 +1,10 @@
use crate::annotation::canonicalize_annotation; use crate::annotation::canonicalize_annotation;
use crate::annotation::IntroducedVariables; use crate::annotation::IntroducedVariables;
use crate::env::Env; use crate::env::Env;
use crate::expr::references_from_call_better; use crate::expr::references_from;
use crate::expr::references_from_local_better;
use crate::expr::ClosureData; use crate::expr::ClosureData;
use crate::expr::Expr::{self, *}; use crate::expr::Expr::{self, *};
use crate::expr::{ use crate::expr::{canonicalize_expr, local_successors, Output, Recursive};
canonicalize_expr, local_successors, references_from_call, references_from_local, Output,
Recursive,
};
use crate::pattern::{bindings_from_patterns, canonicalize_pattern, Pattern}; use crate::pattern::{bindings_from_patterns, canonicalize_pattern, Pattern};
use crate::procedure::References; use crate::procedure::References;
use crate::scope::create_alias; use crate::scope::create_alias;
@ -434,11 +430,6 @@ fn find_used_defs(
refs_by_def: &MutMap<Symbol, (Region, References)>, refs_by_def: &MutMap<Symbol, (Region, References)>,
closures: &MutMap<Symbol, References>, closures: &MutMap<Symbol, References>,
) -> References { ) -> References {
// Determine the full set of references by traversing the graph.
let mut visited_symbols = MutSet::default();
let mut output_references = References::default();
// Start with the return expression's referenced locals. They're the only ones that count! // Start with the return expression's referenced locals. They're the only ones that count!
// //
// If I have two defs which reference each other, but neither of them is referenced // If I have two defs which reference each other, but neither of them is referenced
@ -450,27 +441,13 @@ fn find_used_defs(
// def as a whole references both `a` *and* `b`, even though it doesn't // def as a whole references both `a` *and* `b`, even though it doesn't
// directly mention `b` - because `a` depends on `b`. If we didn't traverse a graph here, // directly mention `b` - because `a` depends on `b`. If we didn't traverse a graph here,
// we'd erroneously give a warning that `b` was unused since it wasn't directly referenced. // we'd erroneously give a warning that `b` was unused since it wasn't directly referenced.
for symbol in value_lookups.iter().copied() { let locals = value_lookups
// We only care about local symbols in this analysis. .iter()
if symbol.module_id() == home { .copied()
// Traverse the graph and look up *all* the references for this local symbol. .filter(|symbol| symbol.module_id() == home);
let refs = let calls = calls.iter().copied();
references_from_local_better(symbol, &mut visited_symbols, refs_by_def, closures);
output_references = output_references.union(refs); references_from(locals, calls, refs_by_def, closures)
}
}
for symbol in calls.iter().copied() {
// Traverse the graph and look up *all* the references for this call.
// Reuse the same visited_symbols as before; if we already visited it,
// we won't learn anything new from visiting it again!
let refs = references_from_call_better(symbol, &mut visited_symbols, refs_by_def, closures);
output_references = output_references.union(refs);
}
output_references
} }
#[inline(always)] #[inline(always)]

View file

@ -1153,50 +1153,32 @@ enum ReferencesFrom {
Call(Symbol), Call(Symbol),
} }
pub(crate) fn references_from_local_better<'a, T>( pub(crate) fn references_from<'a, T>(
initial: Symbol, locals: impl IntoIterator<Item = Symbol>,
visited: &'a mut MutSet<Symbol>, calls: impl IntoIterator<Item = Symbol>,
refs_by_def: &'a MutMap<Symbol, (T, References)>, refs_by_def: &'a MutMap<Symbol, (T, References)>,
closures: &'a MutMap<Symbol, References>, closures: &'a MutMap<Symbol, References>,
) -> References ) -> References
where where
T: Debug, T: Debug,
{ {
references_from_help( let mut stack = Vec::new();
ReferencesFrom::Local(initial),
visited,
refs_by_def,
closures,
)
}
pub(crate) fn references_from_call_better<'a, T>( stack.extend(locals.into_iter().map(ReferencesFrom::Local));
initial: Symbol, stack.extend(calls.into_iter().map(ReferencesFrom::Call));
visited: &'a mut MutSet<Symbol>,
refs_by_def: &'a MutMap<Symbol, (T, References)>, references_from_help(stack, refs_by_def, closures)
closures: &'a MutMap<Symbol, References>,
) -> References
where
T: Debug,
{
references_from_help(
ReferencesFrom::Call(initial),
visited,
refs_by_def,
closures,
)
} }
fn references_from_help<'a, T>( fn references_from_help<'a, T>(
initial: ReferencesFrom, mut stack: Vec<ReferencesFrom>,
visited: &'a mut MutSet<Symbol>,
refs_by_def: &'a MutMap<Symbol, (T, References)>, refs_by_def: &'a MutMap<Symbol, (T, References)>,
closures: &'a MutMap<Symbol, References>, closures: &'a MutMap<Symbol, References>,
) -> References ) -> References
where where
T: Debug, T: Debug,
{ {
let mut stack: Vec<ReferencesFrom> = vec![initial]; let mut visited = Vec::new();
let mut result = References::default(); let mut result = References::default();
while let Some(job) = stack.pop() { while let Some(job) = stack.pop() {
@ -1207,7 +1189,7 @@ where
continue; continue;
} }
visited.insert(defined_symbol); visited.push(defined_symbol);
for local in refs.value_lookups.iter() { for local in refs.value_lookups.iter() {
stack.push(ReferencesFrom::Local(*local)); stack.push(ReferencesFrom::Local(*local));
@ -1228,7 +1210,7 @@ where
continue; continue;
} }
visited.insert(call_symbol); visited.push(call_symbol);
result = result.union(references.clone()); result = result.union(references.clone());