Skip to content

Commit

Permalink
cfg: add logging decorator for cfg traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
davleopo committed Mar 4, 2024
1 parent 0b609cb commit 7ace794
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import jdk.graal.compiler.debug.DebugContext;
import jdk.graal.compiler.debug.GraalError;
import jdk.graal.compiler.debug.MemUseTrackerKey;
import jdk.graal.compiler.debug.TTY;
import jdk.graal.compiler.graph.Node;
import jdk.graal.compiler.graph.NodeMap;
import jdk.graal.compiler.graph.iterators.NodeIterable;
Expand Down Expand Up @@ -219,6 +220,44 @@ public void updateCachedLocalLoopFrequency(LoopBeginNode lb, Function<LoopFreque
localLoopFrequencyData.put(lb, updater.apply(localLoopFrequencyData.get(lb)));
}

/**
* Debug only decorator for {@link RecursiveVisitor} to log all basic blocks how they are
* visited one by one.
*/
public static class LoggingCFGDecorator implements ControlFlowGraph.RecursiveVisitor<HIRBlock> {
private final ControlFlowGraph.RecursiveVisitor<HIRBlock> visitor;
private String indent = "";

public LoggingCFGDecorator(ControlFlowGraph.RecursiveVisitor<HIRBlock> visitor, ControlFlowGraph cfg) {
this.visitor = visitor;
TTY.printf("DomTree for %s%n", cfg.graph);
printDomTree(cfg.getStartBlock(), "");
}

private static void printDomTree(HIRBlock cur, String indent) {
TTY.printf("%s%s [dom %s, post dom %s]%n", indent, cur, cur.getDominator(), cur.getPostdominator());
HIRBlock dominated = cur.getFirstDominated();
while (dominated != null) {
printDomTree(dominated, indent + "\t");
dominated = dominated.getDominatedSibling();
}
}

@Override
public HIRBlock enter(HIRBlock b) {
TTY.printf("%sEnter block %s for %s%n", indent, b, visitor);
indent += "\t";
return visitor.enter(b);
}

@Override
public void exit(HIRBlock b, HIRBlock value) {
indent = indent.substring(0, indent.length() - 1);
TTY.printf("%sExit block %s with value %s for %s%n", indent, b, value, visitor);
visitor.exit(b, value);
}
}

@SuppressWarnings("unchecked")
public <V> void visitDominatorTreeDefault(RecursiveVisitor<V> visitor) {

Expand Down

0 comments on commit 7ace794

Please sign in to comment.