001/*
002 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.nodes;
024
025import java.util.*;
026
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.graph.*;
030import com.oracle.graal.nodeinfo.*;
031import com.oracle.graal.nodes.memory.*;
032
033public class ValueNodeUtil {
034
035    public static ValueNode assertKind(Kind kind, ValueNode x) {
036        assert x != null && x.getKind() == kind : "kind=" + kind + ", value=" + x + ((x == null) ? "" : ", value.kind=" + x.getKind());
037        return x;
038    }
039
040    public static RuntimeException shouldNotReachHere(String msg) {
041        throw new InternalError("should not reach here: " + msg);
042    }
043
044    public static RuntimeException shouldNotReachHere() {
045        throw new InternalError("should not reach here");
046    }
047
048    public static ValueNode assertLong(ValueNode x) {
049        assert x != null && (x.getKind() == Kind.Long);
050        return x;
051    }
052
053    public static ValueNode assertInt(ValueNode x) {
054        assert x != null && (x.getKind() == Kind.Int);
055        return x;
056    }
057
058    public static ValueNode assertFloat(ValueNode x) {
059        assert x != null && (x.getKind() == Kind.Float);
060        return x;
061    }
062
063    public static ValueNode assertObject(ValueNode x) {
064        assert x != null && (x.getKind() == Kind.Object);
065        return x;
066    }
067
068    public static ValueNode assertDouble(ValueNode x) {
069        assert x != null && (x.getKind() == Kind.Double);
070        return x;
071    }
072
073    public static void assertHigh(ValueNode x) {
074        assert x == null;
075    }
076
077    @SuppressWarnings("unchecked")
078    public static <T extends Node> Collection<T> filter(Iterable<Node> nodes, Class<T> clazz) {
079        ArrayList<T> phis = new ArrayList<>();
080        for (Node node : nodes) {
081            if (clazz.isInstance(node)) {
082                phis.add((T) node);
083            }
084        }
085        return phis;
086    }
087
088    /**
089     * Converts a given instruction to a value string. The representation of an node as a value is
090     * formed by concatenating the {@linkplain jdk.internal.jvmci.meta.Kind#getTypeChar character}
091     * denoting its {@linkplain ValueNode#getKind kind} and its id. For example, {@code "i13"}.
092     *
093     * @param value the instruction to convert to a value string. If {@code value == null}, then "-"
094     *            is returned.
095     * @return the instruction representation as a string
096     */
097    public static String valueString(ValueNode value) {
098        return (value == null) ? "-" : ("" + value.getKind().getTypeChar() + value.toString(Verbosity.Id));
099    }
100
101    public static ValueNode asNode(MemoryNode node) {
102        if (node == null) {
103            return null;
104        } else {
105            return node.asNode();
106        }
107    }
108}