1 /*
2  * Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
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 
24 
25 package org.graalvm.compiler.nodes;
26 
27 import static java.lang.Character.toLowerCase;
28 
29 import java.util.ArrayList;
30 import java.util.Collection;
31 
32 import org.graalvm.compiler.graph.Node;
33 import org.graalvm.compiler.nodeinfo.Verbosity;
34 import org.graalvm.compiler.nodes.memory.MemoryKill;
35 
36 import jdk.vm.ci.meta.JavaKind;
37 
38 public class ValueNodeUtil {
39 
assertKind(JavaKind kind, ValueNode x)40     public static ValueNode assertKind(JavaKind kind, ValueNode x) {
41         assert x != null && x.getStackKind() == kind : "kind=" + kind + ", value=" + x + ((x == null) ? "" : ", value.kind=" + x.getStackKind());
42         return x;
43     }
44 
shouldNotReachHere(String msg)45     public static RuntimeException shouldNotReachHere(String msg) {
46         throw new InternalError("should not reach here: " + msg);
47     }
48 
shouldNotReachHere()49     public static RuntimeException shouldNotReachHere() {
50         throw new InternalError("should not reach here");
51     }
52 
assertLong(ValueNode x)53     public static ValueNode assertLong(ValueNode x) {
54         assert x != null && (x.getStackKind() == JavaKind.Long);
55         return x;
56     }
57 
assertInt(ValueNode x)58     public static ValueNode assertInt(ValueNode x) {
59         assert x != null && (x.getStackKind() == JavaKind.Int);
60         return x;
61     }
62 
assertFloat(ValueNode x)63     public static ValueNode assertFloat(ValueNode x) {
64         assert x != null && (x.getStackKind() == JavaKind.Float);
65         return x;
66     }
67 
assertObject(ValueNode x)68     public static ValueNode assertObject(ValueNode x) {
69         assert x != null && (x.getStackKind() == JavaKind.Object);
70         return x;
71     }
72 
assertDouble(ValueNode x)73     public static ValueNode assertDouble(ValueNode x) {
74         assert x != null && (x.getStackKind() == JavaKind.Double);
75         return x;
76     }
77 
assertHigh(ValueNode x)78     public static void assertHigh(ValueNode x) {
79         assert x == null;
80     }
81 
82     @SuppressWarnings("unchecked")
filter(Iterable<Node> nodes, Class<T> clazz)83     public static <T extends Node> Collection<T> filter(Iterable<Node> nodes, Class<T> clazz) {
84         ArrayList<T> phis = new ArrayList<>();
85         for (Node node : nodes) {
86             if (clazz.isInstance(node)) {
87                 phis.add((T) node);
88             }
89         }
90         return phis;
91     }
92 
93     /**
94      * Converts a given instruction to a value string. The representation of an node as a value is
95      * formed by concatenating the {@linkplain jdk.vm.ci.meta.JavaKind#getTypeChar character}
96      * denoting its {@linkplain ValueNode#getStackKind kind} and its id. For example, {@code "i13"}.
97      *
98      * @param value the instruction to convert to a value string. If {@code value == null}, then "-"
99      *            is returned.
100      * @return the instruction representation as a string
101      */
valueString(ValueNode value)102     public static String valueString(ValueNode value) {
103         return (value == null) ? "-" : ("" + toLowerCase(value.getStackKind().getTypeChar()) + value.toString(Verbosity.Id));
104     }
105 
asNode(MemoryKill node)106     public static ValueNode asNode(MemoryKill node) {
107         if (node == null) {
108             return null;
109         } else {
110             return node.asNode();
111         }
112     }
113 }
114