Skip to content

Commit

Permalink
more efficient find_may_aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Medowhill committed Feb 22, 2024
1 parent 8a6d68c commit e1e066c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 46 deletions.
32 changes: 7 additions & 25 deletions src/relational/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,41 +224,23 @@ impl<'tcx> Analyzer<'tcx, '_> {
}

pub fn locals_invalidated_by_call(&self, callee: LocalDefId) -> HashSet<(Local, usize)> {
self.prog_info.reachability[&callee]
let fls: Vec<_> = self.prog_info.reachability[&callee]
.iter()
.flat_map(|func| {
self.prog_info.indirect_assigns[func]
.iter()
.flat_map(|local| {
self.prog_info
.alias_graph
.find_may_aliases(*func, *local)
.into_iter()
.filter_map(|alias| {
if alias.function == self.local_def_id {
Some((alias.local, alias.depth))
} else {
None
}
})
})
.map(|local| (*func, *local))
})
.collect()
.collect();
self.prog_info
.alias_graph
.find_may_aliases(&fls, self.local_def_id)
}

pub fn find_may_aliases(&self, local: Local) -> HashSet<(Local, usize)> {
self.prog_info
.alias_graph
.find_may_aliases(self.local_def_id, local)
.into_iter()
.filter_map(|alias| {
if alias.function == self.local_def_id {
Some((alias.local, alias.depth))
} else {
None
}
})
.collect()
.find_may_aliases(&[(self.local_def_id, local)], self.local_def_id)
}

pub fn def_id_to_string(&self, def_id: DefId) -> String {
Expand Down
47 changes: 26 additions & 21 deletions src/steensgaard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,14 @@ impl AnalysisResults {

pub fn get_alias_graph(&self) -> AliasGraph {
let id_to_node = self.vars.clone();
let mut local_to_nodes: HashMap<_, HashSet<_>> = HashMap::new();
for (id, node) in &id_to_node {
let VarId::Local(_, local_def_id, local) = id else { continue };
local_to_nodes
.entry((*local_def_id, *local))
.or_default()
.insert(*node);
}
let node_to_ids = inv(&id_to_node);
let (points_to, points_to_fn): (HashMap<_, _>, HashMap<_, _>) = self
.var_tys
Expand All @@ -651,6 +659,7 @@ impl AnalysisResults {
let pointed_by_fn = inv(&points_to_fn);
AliasGraph {
id_to_node,
local_to_nodes,
node_to_ids,
points_to,
pointed_by,
Expand All @@ -675,45 +684,41 @@ where
#[derive(Debug)]
pub struct AliasGraph {
id_to_node: HashMap<VarId, VarId>,
local_to_nodes: HashMap<(LocalDefId, u32), HashSet<VarId>>,
node_to_ids: HashMap<VarId, HashSet<VarId>>,
points_to: HashMap<VarId, VarId>,
pointed_by: HashMap<VarId, HashSet<VarId>>,
points_to_fn: HashMap<VarId, FnId>,
fn_pointed_by: HashMap<FnId, HashSet<VarId>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MayAlias {
pub function: LocalDefId,
pub local: Local,
pub depth: usize,
}

impl AliasGraph {
pub fn find_may_aliases(&self, f: LocalDefId, l: Local) -> HashSet<MayAlias> {
pub fn find_may_aliases(
&self,
fls: &[(LocalDefId, Local)],
caller: LocalDefId,
) -> HashSet<(Local, usize)> {
let mut aliases = HashSet::new();
let mut done = HashSet::new();
let mut remainings = HashSet::new();

for (id, node) in &self.id_to_node {
let VarId::Local(_, local_def_id, local) = id else { continue };
if *local_def_id == f && *local == l.as_u32() {
remainings.insert((self.points_to[node], 0));
}
}
let mut remainings: HashSet<_> = fls
.iter()
.flat_map(|(local_def_id, local)| {
self.local_to_nodes[&(*local_def_id, local.as_u32())]
.iter()
.map(|node| (self.points_to[node], 0))
})
.collect();

while !remainings.is_empty() {
let mut new_remainings = HashSet::new();
for (node, depth) in remainings {
done.insert(node);
for node in &self.node_to_ids[&node] {
let VarId::Local(_, f, l) = *node else { continue };
let alias = MayAlias {
function: f,
local: Local::from_u32(l),
depth,
};
aliases.insert(alias);
if f == caller {
aliases.insert((Local::from_u32(l), depth));
}
}
if let Some(nodes) = self.pointed_by.get(&node) {
for node in nodes {
Expand Down

0 comments on commit e1e066c

Please sign in to comment.