001/*
002 * Copyright (c) 2009, 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.java;
024
025import jdk.internal.jvmci.meta.*;
026
027import com.oracle.graal.graph.*;
028import com.oracle.graal.graph.spi.*;
029import com.oracle.graal.nodeinfo.*;
030import com.oracle.graal.nodes.*;
031import com.oracle.graal.nodes.spi.*;
032
033/**
034 * The {@code InstanceOfDynamicNode} represents a type check where the type being checked is not
035 * known at compile time. This is used, for instance, to intrinsify {@link Class#isInstance(Object)}
036 * .
037 */
038@NodeInfo
039public class InstanceOfDynamicNode extends LogicNode implements Canonicalizable.Binary<ValueNode>, Lowerable {
040    public static final NodeClass<InstanceOfDynamicNode> TYPE = NodeClass.create(InstanceOfDynamicNode.class);
041
042    @Input ValueNode object;
043    @Input ValueNode mirror;
044
045    public static LogicNode create(ConstantReflectionProvider constantReflection, ValueNode mirror, ValueNode object) {
046        LogicNode synonym = findSynonym(constantReflection, object, mirror);
047        if (synonym != null) {
048            return synonym;
049        }
050        return new InstanceOfDynamicNode(mirror, object);
051
052    }
053
054    public InstanceOfDynamicNode(ValueNode mirror, ValueNode object) {
055        super(TYPE);
056        this.mirror = mirror;
057        this.object = object;
058        assert mirror.getKind() == Kind.Object : mirror.getKind();
059    }
060
061    @Override
062    public void lower(LoweringTool tool) {
063        tool.getLowerer().lower(this, tool);
064    }
065
066    private static LogicNode findSynonym(ConstantReflectionProvider constantReflection, ValueNode forObject, ValueNode forMirror) {
067        if (forMirror.isConstant()) {
068            ResolvedJavaType t = constantReflection.asJavaType(forMirror.asConstant());
069            if (t != null) {
070                if (t.isPrimitive()) {
071                    return LogicConstantNode.contradiction();
072                } else {
073                    return InstanceOfNode.create(t, forObject, null);
074                }
075            }
076        }
077        return null;
078    }
079
080    public LogicNode canonical(CanonicalizerTool tool, ValueNode forObject, ValueNode forMirror) {
081        LogicNode res = findSynonym(tool.getConstantReflection(), forObject, forMirror);
082        if (res == null) {
083            res = this;
084        }
085        return res;
086    }
087
088    public ValueNode object() {
089        return object;
090    }
091
092    public ValueNode mirror() {
093        return mirror;
094    }
095
096    public ValueNode getX() {
097        return object;
098    }
099
100    public ValueNode getY() {
101        return mirror;
102    }
103}