comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 13995:e455fc531ec2

Truffle: Added API in NodeUtil to count nodes restricted to a Kind. Added API in NodeUtil to print the inlining tree.
author Christian Humer <christian.humer@gmail.com>
date Fri, 21 Feb 2014 02:25:12 +0100
parents f46cab39a9a2
children 655a4fd5038b
comparison
equal deleted inserted replaced
13994:989f58d6a0ca 13995:e455fc531ec2
32 import sun.misc.*; 32 import sun.misc.*;
33 33
34 import com.oracle.truffle.api.*; 34 import com.oracle.truffle.api.*;
35 import com.oracle.truffle.api.nodes.Node.Child; 35 import com.oracle.truffle.api.nodes.Node.Child;
36 import com.oracle.truffle.api.nodes.Node.Children; 36 import com.oracle.truffle.api.nodes.Node.Children;
37 import com.oracle.truffle.api.nodes.NodeInfo.Kind;
37 38
38 /** 39 /**
39 * Utility class that manages the special access methods for node instances. 40 * Utility class that manages the special access methods for node instances.
40 */ 41 */
41 public final class NodeUtil { 42 public final class NodeUtil {
609 }); 610 });
610 return nodeList; 611 return nodeList;
611 } 612 }
612 613
613 public static int countNodes(Node root) { 614 public static int countNodes(Node root) {
614 return countNodes(root, null, false); 615 return countNodes(root, null, null, false);
615 } 616 }
616 617
617 public static int countNodes(Node root, Class<?> clazz, boolean countInlinedCallNodes) { 618 public static int countNodes(Node root, Class<?> clazz, Kind nodeKind, boolean countInlinedCallNodes) {
618 NodeCountVisitor nodeCount = new NodeCountVisitor(root, clazz, countInlinedCallNodes); 619 NodeCountVisitor nodeCount = new NodeCountVisitor(root, clazz, nodeKind, countInlinedCallNodes);
619 root.accept(nodeCount); 620 root.accept(nodeCount);
620 return nodeCount.nodeCount; 621 return nodeCount.nodeCount;
621 } 622 }
622 623
624 public static int countNodes(Node root, Class<?> clazz, boolean countInlinedCallNodes) {
625 return countNodes(root, clazz, null, countInlinedCallNodes);
626 }
627
623 private static final class NodeCountVisitor implements NodeVisitor { 628 private static final class NodeCountVisitor implements NodeVisitor {
624 629
625 private Node root; 630 private Node root;
626 private boolean inspectInlinedCalls; 631 private final boolean inspectInlinedCalls;
627 int nodeCount; 632 int nodeCount;
633 private final Kind kind;
628 private final Class<?> clazz; 634 private final Class<?> clazz;
629 635
630 private NodeCountVisitor(Node root, Class<?> clazz, boolean inspectInlinedCalls) { 636 private NodeCountVisitor(Node root, Class<?> clazz, Kind kind, boolean inspectInlinedCalls) {
631 this.root = root; 637 this.root = root;
632 this.clazz = clazz; 638 this.clazz = clazz;
639 this.kind = kind;
633 this.inspectInlinedCalls = inspectInlinedCalls; 640 this.inspectInlinedCalls = inspectInlinedCalls;
634 } 641 }
635 642
636 @Override 643 @Override
637 public boolean visit(Node node) { 644 public boolean visit(Node node) {
638 if (node instanceof RootNode && node != root) { 645 if (node instanceof RootNode && node != root) {
639 return false; 646 return false;
640 } 647 }
641 648
642 if (clazz == null || clazz.isInstance(node)) { 649 if ((clazz == null || clazz.isInstance(node)) && (kind == null || isKind(node))) {
643 nodeCount++; 650 nodeCount++;
644 } 651 }
645 652
646 if (inspectInlinedCalls && node instanceof CallNode) { 653 if (inspectInlinedCalls && node instanceof CallNode) {
647 CallNode call = (CallNode) node; 654 CallNode call = (CallNode) node;
650 } 657 }
651 } 658 }
652 659
653 return true; 660 return true;
654 } 661 }
662
663 private boolean isKind(Node n) {
664 return kind == n.getKind();
665 }
666 }
667
668 public static void printInliningTree(final PrintStream stream, RootNode root) {
669 printRootNode(stream, 0, root);
670 root.accept(new NodeVisitor() {
671 int depth = 1;
672
673 public boolean visit(Node node) {
674 if (node instanceof CallNode) {
675 RootNode inlinedRoot = ((CallNode) node).getInlinedRoot();
676 if (inlinedRoot != null) {
677 depth++;
678 printRootNode(stream, depth * 2, inlinedRoot);
679 inlinedRoot.accept(this);
680 depth--;
681 }
682 }
683 return true;
684 }
685 });
686 }
687
688 private static void printRootNode(PrintStream stream, int indent, RootNode root) {
689 for (int i = 0; i < indent; i++) {
690 stream.print(" ");
691 }
692 stream.print(root.toString());
693 stream.print(" (");
694 stream.print(countNodes(root));
695 stream.print("/");
696 stream.print(countNodes(root, null, true));
697 stream.println(")");
655 } 698 }
656 699
657 public static String printCompactTreeToString(Node node) { 700 public static String printCompactTreeToString(Node node) {
658 StringWriter out = new StringWriter(); 701 StringWriter out = new StringWriter();
659 printCompactTree(new PrintWriter(out), null, node, 1); 702 printCompactTree(new PrintWriter(out), null, node, 1);