Skip to content

Commit

Permalink
improvement: better is_reachabale detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Jun 1, 2024
1 parent f5ac263 commit 22f7c40
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions crates/oxc_semantic/src/control_flow/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
mod builder;
mod dot;

use itertools::Itertools;
use oxc_span::CompactStr;
use oxc_syntax::operator::{
AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator,
};
use petgraph::{
stable_graph::NodeIndex,
visit::{depth_first_search, Dfs, DfsEvent, Walker},
visit::{depth_first_search, Control, DfsEvent},
Graph,
};

Expand Down Expand Up @@ -192,19 +191,29 @@ impl ControlFlowGraph {
}

pub fn is_reachabale(&self, from: BasicBlockId, to: BasicBlockId) -> bool {
if from == to {
return true;
}
let graph = &self.graph;
let mut dfs = Dfs::empty(graph);
dfs.reset(graph);
dfs.move_to(from);
dfs.iter(graph)
.take_while_inclusive(|it| {
!self
.basic_block(*it)
.instructions()
.iter()
.any(|it| matches!(it, Instruction { kind: InstructionKind::Unreachable, .. }))
})
.any(|x| x == to)
depth_first_search(&self.graph, Some(from), |event| match event {
DfsEvent::TreeEdge(a, b) => {
if b == to {
return Control::Break(true);
}

let test = graph.edges_connecting(a, b).all(|edge| {
!matches!(edge.weight(), EdgeType::NewFunction | EdgeType::Unreachable)
});
if test {
Control::Continue
} else {
Control::Prune
}
}
_ => Control::Continue,
})
.break_value()
.unwrap_or(false)
}

pub fn is_cyclic(&self, node: BasicBlockId) -> bool {
Expand Down

0 comments on commit 22f7c40

Please sign in to comment.