@@ -11,17 +11,51 @@ public class BBHelper {
1111 */
1212 public static List <BasicBlock > findAllBlocks (BasicBlock root ) {
1313 List <BasicBlock > nodes = new ArrayList <>();
14- postOrderWalk (root , (n ) -> nodes .add (n ), new HashSet <>());
14+ postOrderWalkForwardCFG (root , (n ) -> nodes .add (n ), new HashSet <>());
1515 return nodes ;
1616 }
1717
18- static void postOrderWalk (BasicBlock n , Consumer <BasicBlock > consumer , HashSet <BasicBlock > visited ) {
18+ public static List <BasicBlock > findAllBlocksPostOrderForwardCFG (CompiledFunction function ) {
19+ List <BasicBlock > nodes = new ArrayList <>();
20+ postOrderWalkForwardCFG (function .entry , (n ) -> nodes .add (n ), new HashSet <>());
21+ return nodes ;
22+ }
23+
24+ public static List <BasicBlock > findAllBlocksReversePostOrderForwardCFG (CompiledFunction function ) {
25+ List <BasicBlock > nodes = new ArrayList <>();
26+ postOrderWalkForwardCFG (function .entry , (n ) -> nodes .add (0 ,n ), new HashSet <>());
27+ return nodes ;
28+ }
29+
30+ static void postOrderWalkForwardCFG (BasicBlock n , Consumer <BasicBlock > consumer , HashSet <BasicBlock > visited ) {
1931 visited .add (n );
2032 /* For each successor node */
2133 for (BasicBlock s : n .successors ) {
2234 if (!visited .contains (s ))
23- postOrderWalk (s , consumer , visited );
35+ postOrderWalkForwardCFG (s , consumer , visited );
2436 }
2537 consumer .accept (n );
2638 }
39+
40+ static void postOrderWalkReverseCFG (BasicBlock n , Consumer <BasicBlock > consumer , HashSet <BasicBlock > visited ) {
41+ visited .add (n );
42+ /* For each successor node */
43+ for (BasicBlock s : n .predecessors ) {
44+ if (!visited .contains (s ))
45+ postOrderWalkReverseCFG (s , consumer , visited );
46+ }
47+ consumer .accept (n );
48+ }
49+
50+ public static List <BasicBlock > findAllBlocksPostOrderReverseCFG (CompiledFunction function ) {
51+ List <BasicBlock > nodes = new ArrayList <>();
52+ postOrderWalkReverseCFG (function .exit , (n ) -> nodes .add (n ), new HashSet <>());
53+ return nodes ;
54+ }
55+
56+ public static List <BasicBlock > findAllBlocksReversePostOrderReverseCFG (CompiledFunction function ) {
57+ List <BasicBlock > nodes = new ArrayList <>();
58+ postOrderWalkReverseCFG (function .exit , (n ) -> nodes .add (0 ,n ), new HashSet <>());
59+ return nodes ;
60+ }
2761}
0 commit comments