< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.printer/src/org/graalvm/compiler/printer/BinaryGraphPrinter.java

Print this page




   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.printer;
  24 
  25 import static org.graalvm.compiler.graph.Edges.Type.Inputs;
  26 import static org.graalvm.compiler.graph.Edges.Type.Successors;
  27 
  28 import java.io.IOException;
  29 import java.nio.channels.WritableByteChannel;
  30 import java.util.Arrays;
  31 import java.util.Collection;
  32 import java.util.Collections;
  33 import java.util.LinkedList;
  34 import java.util.List;
  35 import java.util.Map;
  36 import jdk.vm.ci.meta.ResolvedJavaField;
  37 
  38 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
  39 import org.graalvm.compiler.bytecode.Bytecode;
  40 import org.graalvm.compiler.core.common.cfg.BlockMap;

  41 import org.graalvm.compiler.debug.DebugOptions;
  42 import org.graalvm.compiler.graph.CachedGraph;
  43 import org.graalvm.compiler.graph.Edges;
  44 import org.graalvm.compiler.graph.Graph;
  45 import org.graalvm.compiler.graph.InputEdges;
  46 import org.graalvm.compiler.graph.Node;
  47 import org.graalvm.compiler.graph.NodeClass;
  48 import org.graalvm.compiler.graph.NodeMap;

  49 import org.graalvm.compiler.nodes.AbstractBeginNode;
  50 import org.graalvm.compiler.nodes.AbstractEndNode;
  51 import org.graalvm.compiler.nodes.AbstractMergeNode;
  52 import org.graalvm.compiler.nodes.ConstantNode;
  53 import org.graalvm.compiler.nodes.ControlSinkNode;
  54 import org.graalvm.compiler.nodes.ControlSplitNode;
  55 import org.graalvm.compiler.nodes.FixedNode;
  56 import org.graalvm.compiler.nodes.PhiNode;
  57 import org.graalvm.compiler.nodes.ProxyNode;

  58 import org.graalvm.compiler.nodes.VirtualState;
  59 import org.graalvm.compiler.nodes.cfg.Block;
  60 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
  61 import jdk.vm.ci.meta.ResolvedJavaMethod;
  62 import jdk.vm.ci.meta.ResolvedJavaType;
  63 import jdk.vm.ci.meta.Signature;
  64 import org.graalvm.compiler.debug.DebugContext;
  65 import org.graalvm.compiler.graph.NodeSourcePosition;
  66 import org.graalvm.compiler.nodes.StructuredGraph;
  67 import org.graalvm.compiler.phases.schedule.SchedulePhase;
  68 import org.graalvm.graphio.GraphBlocks;
  69 import org.graalvm.graphio.GraphElements;
  70 import org.graalvm.graphio.GraphOutput;
  71 import org.graalvm.graphio.GraphStructure;
  72 import org.graalvm.graphio.GraphTypes;
  73 





  74 public class BinaryGraphPrinter implements
  75                 GraphStructure<BinaryGraphPrinter.GraphInfo, Node, NodeClass<?>, Edges>,
  76                 GraphBlocks<BinaryGraphPrinter.GraphInfo, Block, Node>,
  77                 GraphElements<ResolvedJavaMethod, ResolvedJavaField, Signature, NodeSourcePosition>,
  78                 GraphTypes, GraphPrinter {
  79     private final SnippetReflectionProvider snippetReflection;
  80     private final GraphOutput<BinaryGraphPrinter.GraphInfo, ResolvedJavaMethod> output;
  81 
  82     public BinaryGraphPrinter(WritableByteChannel channel, SnippetReflectionProvider snippetReflection) throws IOException {
  83         this.output = GraphOutput.newBuilder(this).blocks(this).elements(this).types(this).build(channel);
  84         this.snippetReflection = snippetReflection;
  85     }
  86 
  87     @Override
  88     public SnippetReflectionProvider getSnippetReflectionProvider() {
  89         return snippetReflection;
  90     }
  91 
  92     @Override
  93     public void beginGroup(DebugContext debug, String name, String shortName, ResolvedJavaMethod method, int bci, Map<Object, Object> properties) throws IOException {
  94         output.beginGroup(new GraphInfo(debug, null), name, shortName, method, bci, properties);
  95     }
  96 
  97     @Override
  98     public void endGroup() throws IOException {
  99         output.endGroup();
 100     }
 101 
 102     @Override
 103     public void close() {
 104         output.close();
 105     }
 106 
 107     @Override
 108     public ResolvedJavaMethod method(Object object) {
 109         if (object instanceof Bytecode) {
 110             return ((Bytecode) object).getMethod();
 111         } else if (object instanceof ResolvedJavaMethod) {
 112             return ((ResolvedJavaMethod) object);
 113         } else {
 114             return null;
 115         }
 116     }
 117 
 118     @Override





 119     public NodeClass<?> nodeClass(Object obj) {
 120         if (obj instanceof NodeClass<?>) {
 121             return (NodeClass<?>) obj;
 122         }
 123         if (obj instanceof Node) {
 124             return ((Node) obj).getNodeClass();
 125         }
 126         return null;
 127     }
 128 
 129     @Override





 130     public Object nodeClassType(NodeClass<?> node) {
 131         return node.getJavaClass();
 132     }
 133 
 134     @Override
 135     public String nameTemplate(NodeClass<?> nodeClass) {
 136         return nodeClass.getNameTemplate();
 137     }
 138 
 139     @Override
 140     public final GraphInfo graph(GraphInfo currrent, Object obj) {
 141         if (obj instanceof Graph) {
 142             return new GraphInfo(currrent.debug, (Graph) obj);
 143         } else if (obj instanceof CachedGraph) {
 144             return new GraphInfo(currrent.debug, ((CachedGraph<?>) obj).getReadonlyCopy());
 145         } else {
 146             return null;
 147         }
 148     }
 149 


 239         } else if (node instanceof AbstractMergeNode) {
 240             props.put("category", "merge");
 241         } else if (node instanceof AbstractBeginNode) {
 242             props.put("category", "begin");
 243         } else if (node instanceof AbstractEndNode) {
 244             props.put("category", "end");
 245         } else if (node instanceof FixedNode) {
 246             props.put("category", "fixed");
 247         } else if (node instanceof VirtualState) {
 248             props.put("category", "state");
 249         } else if (node instanceof PhiNode) {
 250             props.put("category", "phi");
 251         } else if (node instanceof ProxyNode) {
 252             props.put("category", "proxy");
 253         } else {
 254             if (node instanceof ConstantNode) {
 255                 ConstantNode cn = (ConstantNode) node;
 256                 updateStringPropertiesForConstant((Map) props, cn);
 257             }
 258             props.put("category", "floating");







 259         }
 260     }
 261 
 262     private Object getBlockForNode(Node node, NodeMap<Block> nodeToBlocks) {
 263         if (nodeToBlocks.isNew(node)) {
 264             return "NEW (not in schedule)";
 265         } else {
 266             Block block = nodeToBlocks.get(node);
 267             if (block != null) {
 268                 return block.getId();
 269             } else if (node instanceof PhiNode) {
 270                 return getBlockForNode(((PhiNode) node).merge(), nodeToBlocks);
 271             }
 272         }
 273         return null;
 274     }
 275 
 276     private static void findExtraNodes(Node node, Collection<? super Node> extraNodes) {
 277         if (node instanceof AbstractMergeNode) {
 278             AbstractMergeNode merge = (AbstractMergeNode) node;




   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.printer;
  24 
  25 import static org.graalvm.compiler.graph.Edges.Type.Inputs;
  26 import static org.graalvm.compiler.graph.Edges.Type.Successors;
  27 
  28 import java.io.IOException;

  29 import java.util.Arrays;
  30 import java.util.Collection;
  31 import java.util.Collections;
  32 import java.util.LinkedList;
  33 import java.util.List;
  34 import java.util.Map;

  35 
  36 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
  37 import org.graalvm.compiler.bytecode.Bytecode;
  38 import org.graalvm.compiler.core.common.cfg.BlockMap;
  39 import org.graalvm.compiler.debug.DebugContext;
  40 import org.graalvm.compiler.debug.DebugOptions;
  41 import org.graalvm.compiler.graph.CachedGraph;
  42 import org.graalvm.compiler.graph.Edges;
  43 import org.graalvm.compiler.graph.Graph;
  44 import org.graalvm.compiler.graph.InputEdges;
  45 import org.graalvm.compiler.graph.Node;
  46 import org.graalvm.compiler.graph.NodeClass;
  47 import org.graalvm.compiler.graph.NodeMap;
  48 import org.graalvm.compiler.graph.NodeSourcePosition;
  49 import org.graalvm.compiler.nodes.AbstractBeginNode;
  50 import org.graalvm.compiler.nodes.AbstractEndNode;
  51 import org.graalvm.compiler.nodes.AbstractMergeNode;
  52 import org.graalvm.compiler.nodes.ConstantNode;
  53 import org.graalvm.compiler.nodes.ControlSinkNode;
  54 import org.graalvm.compiler.nodes.ControlSplitNode;
  55 import org.graalvm.compiler.nodes.FixedNode;
  56 import org.graalvm.compiler.nodes.PhiNode;
  57 import org.graalvm.compiler.nodes.ProxyNode;
  58 import org.graalvm.compiler.nodes.StructuredGraph;
  59 import org.graalvm.compiler.nodes.VirtualState;
  60 import org.graalvm.compiler.nodes.cfg.Block;
  61 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
  62 import org.graalvm.compiler.nodes.util.JavaConstantFormattable;





  63 import org.graalvm.compiler.phases.schedule.SchedulePhase;
  64 import org.graalvm.graphio.GraphBlocks;
  65 import org.graalvm.graphio.GraphElements;
  66 import org.graalvm.graphio.GraphOutput;
  67 import org.graalvm.graphio.GraphStructure;
  68 import org.graalvm.graphio.GraphTypes;
  69 
  70 import jdk.vm.ci.meta.ResolvedJavaField;
  71 import jdk.vm.ci.meta.ResolvedJavaMethod;
  72 import jdk.vm.ci.meta.ResolvedJavaType;
  73 import jdk.vm.ci.meta.Signature;
  74 
  75 public class BinaryGraphPrinter implements
  76                 GraphStructure<BinaryGraphPrinter.GraphInfo, Node, NodeClass<?>, Edges>,
  77                 GraphBlocks<BinaryGraphPrinter.GraphInfo, Block, Node>,
  78                 GraphElements<ResolvedJavaMethod, ResolvedJavaField, Signature, NodeSourcePosition>,
  79                 GraphTypes, GraphPrinter {
  80     private final SnippetReflectionProvider snippetReflection;
  81     private final GraphOutput<BinaryGraphPrinter.GraphInfo, ResolvedJavaMethod> output;
  82 
  83     public BinaryGraphPrinter(DebugContext ctx, SnippetReflectionProvider snippetReflection) throws IOException {
  84         this.output = ctx.buildOutput(GraphOutput.newBuilder(this).protocolVersion(5, 0).blocks(this).elements(this).types(this));
  85         this.snippetReflection = snippetReflection;
  86     }
  87 
  88     @Override
  89     public SnippetReflectionProvider getSnippetReflectionProvider() {
  90         return snippetReflection;
  91     }
  92 
  93     @Override
  94     public void beginGroup(DebugContext debug, String name, String shortName, ResolvedJavaMethod method, int bci, Map<Object, Object> properties) throws IOException {
  95         output.beginGroup(new GraphInfo(debug, null), name, shortName, method, bci, properties);
  96     }
  97 
  98     @Override
  99     public void endGroup() throws IOException {
 100         output.endGroup();
 101     }
 102 
 103     @Override
 104     public void close() {
 105         output.close();
 106     }
 107 
 108     @Override
 109     public ResolvedJavaMethod method(Object object) {
 110         if (object instanceof Bytecode) {
 111             return ((Bytecode) object).getMethod();
 112         } else if (object instanceof ResolvedJavaMethod) {
 113             return ((ResolvedJavaMethod) object);
 114         } else {
 115             return null;
 116         }
 117     }
 118 
 119     @Override
 120     public Node node(Object obj) {
 121         return obj instanceof Node ? (Node) obj : null;
 122     }
 123 
 124     @Override
 125     public NodeClass<?> nodeClass(Object obj) {
 126         if (obj instanceof NodeClass<?>) {
 127             return (NodeClass<?>) obj;
 128         }



 129         return null;
 130     }
 131 
 132     @Override
 133     public NodeClass<?> classForNode(Node node) {
 134         return node.getNodeClass();
 135     }
 136 
 137     @Override
 138     public Object nodeClassType(NodeClass<?> node) {
 139         return node.getJavaClass();
 140     }
 141 
 142     @Override
 143     public String nameTemplate(NodeClass<?> nodeClass) {
 144         return nodeClass.getNameTemplate();
 145     }
 146 
 147     @Override
 148     public final GraphInfo graph(GraphInfo currrent, Object obj) {
 149         if (obj instanceof Graph) {
 150             return new GraphInfo(currrent.debug, (Graph) obj);
 151         } else if (obj instanceof CachedGraph) {
 152             return new GraphInfo(currrent.debug, ((CachedGraph<?>) obj).getReadonlyCopy());
 153         } else {
 154             return null;
 155         }
 156     }
 157 


 247         } else if (node instanceof AbstractMergeNode) {
 248             props.put("category", "merge");
 249         } else if (node instanceof AbstractBeginNode) {
 250             props.put("category", "begin");
 251         } else if (node instanceof AbstractEndNode) {
 252             props.put("category", "end");
 253         } else if (node instanceof FixedNode) {
 254             props.put("category", "fixed");
 255         } else if (node instanceof VirtualState) {
 256             props.put("category", "state");
 257         } else if (node instanceof PhiNode) {
 258             props.put("category", "phi");
 259         } else if (node instanceof ProxyNode) {
 260             props.put("category", "proxy");
 261         } else {
 262             if (node instanceof ConstantNode) {
 263                 ConstantNode cn = (ConstantNode) node;
 264                 updateStringPropertiesForConstant((Map) props, cn);
 265             }
 266             props.put("category", "floating");
 267         }
 268         if (getSnippetReflectionProvider() != null) {
 269             for (Map.Entry<String, Object> prop : props.entrySet()) {
 270                 if (prop.getValue() instanceof JavaConstantFormattable) {
 271                     props.put(prop.getKey(), ((JavaConstantFormattable) prop.getValue()).format(this));
 272                 }
 273             }
 274         }
 275     }
 276 
 277     private Object getBlockForNode(Node node, NodeMap<Block> nodeToBlocks) {
 278         if (nodeToBlocks.isNew(node)) {
 279             return "NEW (not in schedule)";
 280         } else {
 281             Block block = nodeToBlocks.get(node);
 282             if (block != null) {
 283                 return block.getId();
 284             } else if (node instanceof PhiNode) {
 285                 return getBlockForNode(((PhiNode) node).merge(), nodeToBlocks);
 286             }
 287         }
 288         return null;
 289     }
 290 
 291     private static void findExtraNodes(Node node, Collection<? super Node> extraNodes) {
 292         if (node instanceof AbstractMergeNode) {
 293             AbstractMergeNode merge = (AbstractMergeNode) node;


< prev index next >