From 91c999546c96c21db48fb271fd9b7de44a13d845 Mon Sep 17 00:00:00 2001 From: rzvxa <3788964+rzvxa@users.noreply.github.com> Date: Thu, 6 Jun 2024 05:41:06 +0000 Subject: [PATCH] improvement(semantic/cfg): better control flow for `DoWhileStatement`s. (#3452) similar to #3451 --- crates/oxc_semantic/src/builder.rs | 13 +++++-------- ...gration__cfg__cfg_files@do_while_break.js-2.snap | 12 ++++++------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index a1f7dc229d25d..2ad41b21784ea 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -649,6 +649,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> { self.visit_statement(&stmt.body); /* cfg - condition basic block */ + let after_body_graph_ix = self.cfg.current_node_ix; let start_of_condition_graph_ix = self.cfg.new_basic_block(); /* cfg */ @@ -659,18 +660,14 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> { let end_do_while_graph_ix = self.cfg.new_basic_block(); - // before do while to start of condition basic block - self.cfg.add_edge( - before_do_while_stmt_graph_ix, - start_of_condition_graph_ix, - EdgeType::Normal, - ); + // before do while to start of body basic block + self.cfg.add_edge(before_do_while_stmt_graph_ix, start_body_graph_ix, EdgeType::Normal); // body of do-while to start of condition - self.cfg.add_edge(start_body_graph_ix, start_of_condition_graph_ix, EdgeType::Backedge); + self.cfg.add_edge(after_body_graph_ix, start_of_condition_graph_ix, EdgeType::Normal); // end of condition to after do while self.cfg.add_edge(end_of_condition_graph_ix, end_do_while_graph_ix, EdgeType::Normal); // end of condition to after start of body - self.cfg.add_edge(end_of_condition_graph_ix, start_body_graph_ix, EdgeType::Normal); + self.cfg.add_edge(end_of_condition_graph_ix, start_body_graph_ix, EdgeType::Backedge); self.cfg.restore_state( &statement_state, diff --git a/crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@do_while_break.js-2.snap b/crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@do_while_break.js-2.snap index 6d356b9a651ae..8a92966fcd7f5 100644 --- a/crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@do_while_break.js-2.snap +++ b/crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@do_while_break.js-2.snap @@ -26,18 +26,18 @@ digraph { 2 -> 3 [ label = Normal ] 4 -> 5 [ label = Unreachable , style = "dotted" ] 4 -> 5 [ label = Normal ] - 3 -> 6 [ label = Normal ] - 4 -> 6 [ label = Backedge ] + 3 -> 4 [ label = Normal ] + 5 -> 6 [ label = Normal ] 6 -> 7 [ label = Normal ] - 6 -> 4 [ label = Normal ] + 6 -> 4 [ label = Backedge ] 7 -> 8 [ label = Unreachable , style = "dotted" ] 2 -> 9 [ label = Normal ] 10 -> 11 [ label = Unreachable , style = "dotted" ] 10 -> 11 [ label = Normal ] - 9 -> 12 [ label = Normal ] - 10 -> 12 [ label = Backedge ] + 9 -> 10 [ label = Normal ] + 11 -> 12 [ label = Normal ] 12 -> 13 [ label = Normal ] - 12 -> 10 [ label = Normal ] + 12 -> 10 [ label = Backedge ] 13 -> 14 [ label = Normal ] 0 -> 15 [ label = Normal ] }