Skip to content

Commit de34763

Browse files
Document the order of traversal for liveness and make it also more explicit in the method names.
1 parent 42e6626 commit de34763

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/BBHelper.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Liveness.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@
2222
* Computing Liveness Sets for SSA-Form Programs
2323
* Florian Brandner, Benoit Boissinot, Alain Darte, Benoît Dupont de Dinechin, Fabrice Rastello
2424
*
25-
* The implementation is the unoptimized simple one.
25+
* The implementation is the unoptimized simple data analysis form.
2626
* However, we have a modification to ensure that if we see a block
2727
* which loops to itself and has Phi cycles, then the Phi is only added to
2828
* PhiDefs.
2929
*/
3030
public class Liveness {
3131

3232
public Liveness(CompiledFunction function) {
33-
List<BasicBlock> blocks = BBHelper.findAllBlocks(function.entry);
33+
// EaC states that it is most efficient to do RPO on reverse CFG.
34+
// The problem is that if there are infinite loops, we will not visit all basic blocks
35+
// if we started at the exit block (this could be solved by adding artificial edges from infinite loop
36+
// to exit block, but we do not do that yet).
37+
// For a forward CFG traversal, we cannot do RPO as this is a backward dataflow
38+
// problem, i.e. successors must be processed first.
39+
List<BasicBlock> blocks = BBHelper.findAllBlocksPostOrderForwardCFG(function);
3440
RegisterPool regPool = function.registerPool;
3541
initBlocks(regPool, blocks);
3642
init(blocks);
@@ -110,7 +116,6 @@ private void computeLiveness(List<BasicBlock> blocks) {
110116
boolean changed = true;
111117
while (changed) {
112118
changed = false;
113-
// TODO we should process in RPO order
114119
for (BasicBlock block : blocks) {
115120
if (recomputeLiveOut(block))
116121
changed = true;

0 commit comments

Comments
 (0)