simplify local_successors

This commit is contained in:
Folkert 2022-03-21 23:16:07 +01:00
parent cee1a787c9
commit 14b53c0ccf
No known key found for this signature in database
GPG Key ID: 1F17F6FFD112B97C
2 changed files with 10 additions and 21 deletions

View File

@ -438,11 +438,9 @@ pub fn sort_can_defs(
}
let mut defined_symbols: Vec<Symbol> = Vec::new();
let mut defined_symbols_set: ImSet<Symbol> = ImSet::default();
for symbol in can_defs_by_symbol.keys() {
defined_symbols.push(*symbol);
defined_symbols_set.insert(*symbol);
}
// Use topological sort to reorder the defs based on their dependencies to one another.
@ -490,7 +488,7 @@ pub fn sort_can_defs(
}
// remove anything that is not defined in the current block
loc_succ.retain(|key| defined_symbols_set.contains(key));
loc_succ.retain(|key| defined_symbols.contains(key));
loc_succ
}
@ -533,7 +531,7 @@ pub fn sort_can_defs(
}
// remove anything that is not defined in the current block
loc_succ.retain(|key| defined_symbols_set.contains(key));
loc_succ.retain(|key| defined_symbols.contains(key));
loc_succ
}
@ -552,7 +550,7 @@ pub fn sort_can_defs(
// NOTE: if the symbol is a closure we DONT look into its body
// remove anything that is not defined in the current block
loc_succ.retain(|key| defined_symbols_set.contains(key));
loc_succ.retain(|key| defined_symbols.contains(key));
// NOTE: direct recursion does matter here: `x = x` is invalid recursion!

View File

@ -1117,34 +1117,25 @@ pub fn local_successors<'a>(
references: &'a References,
closures: &'a MutMap<Symbol, References>,
) -> ImSet<Symbol> {
let mut answer = references.value_lookups.clone();
let mut answer: Vec<_> = references.value_lookups.iter().copied().collect();
for call_symbol in references.calls.iter() {
answer = answer.union(call_successors(*call_symbol, closures));
}
let mut stack: Vec<_> = references.calls.iter().copied().collect();
let mut seen = Vec::new();
answer
}
fn call_successors(call_symbol: Symbol, closures: &MutMap<Symbol, References>) -> ImSet<Symbol> {
let mut answer = ImSet::default();
let mut seen = MutSet::default();
let mut queue = vec![call_symbol];
while let Some(symbol) = queue.pop() {
while let Some(symbol) = stack.pop() {
if seen.contains(&symbol) {
continue;
}
if let Some(references) = closures.get(&symbol) {
answer.extend(references.value_lookups.iter().copied());
queue.extend(references.calls.iter().copied());
stack.extend(references.calls.iter().copied());
seen.insert(symbol);
seen.push(symbol);
}
}
answer
answer.into_iter().collect()
}
enum CanonicalizeRecordProblem {