/*
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.graal.nodes.java;

import com.oracle.graal.api.meta.*;
import com.oracle.graal.graph.*;
import com.oracle.graal.graph.spi.*;
import com.oracle.graal.nodes.*;
import com.oracle.graal.nodes.spi.*;
import com.oracle.graal.nodes.type.*;

/**
 * The {@code InstanceOfDynamicNode} represents a type check where the type being checked is not
 * known at compile time. This is used, for instance, to intrinsify {@link Class#isInstance(Object)}
 * .
 */
public final class InstanceOfDynamicNode extends LogicNode implements Canonicalizable, Lowerable {

    @Input private ValueNode object;
    @Input private ValueNode mirror;

    /**
     * @param mirror the {@link Class} value representing the target target type of the test
     * @param object the object being tested
     */
    public InstanceOfDynamicNode(ValueNode mirror, ValueNode object) {
        this.mirror = mirror;
        this.object = object;
        assert mirror.getKind() == Kind.Object : mirror.getKind();
        assert StampTool.isExactType(mirror);
        assert StampTool.typeOrNull(mirror).getName().equals("Ljava/lang/Class;");
    }

    @Override
    public void lower(LoweringTool tool) {
        tool.getLowerer().lower(this, tool);
    }

    @Override
    public Node canonical(CanonicalizerTool tool) {
        assert object() != null : this;
        if (mirror().isConstant()) {
            ResolvedJavaType t = tool.getConstantReflection().asJavaType(mirror().asConstant());
            if (t != null) {
                if (t.isPrimitive()) {
                    return LogicConstantNode.contradiction(graph());
                } else {
                    return graph().unique(new InstanceOfNode(t, object(), null));
                }
            }
        }
        return this;
    }

    public ValueNode object() {
        return object;
    }

    public ValueNode mirror() {
        return mirror;
    }
}

@@ -22,17 +22,17 @@
 009, 201
-1
+4
 , Oracle
@@ -1128,41 +1128,8 @@
 .*;%0A
-import com.oracle.graal.graph.*;%0A
 impo
@@ -1566,16 +1566,34 @@
 alizable
+.Binary%3CValueNode%3E
 , Lowera
@@ -2267,38 +2267,24 @@
 ol);%0A    %7D%0A%0A
-    @Override%0A
     public N
@@ -2282,16 +2282,21 @@
  public 
+Value
 Node can
@@ -2328,51 +2328,53 @@
 tool
-) %7B%0A        assert object() != null : this;
+, ValueNode forObject, ValueNode forMirror) %7B
 %0A   
@@ -2382,24 +2382,25 @@
     if (
-m
+forM
 irror
-()
 .isConst
@@ -2481,24 +2481,25 @@
 avaType(
-m
+forM
 irror
-()
 .asConst
@@ -2633,23 +2633,16 @@
 diction(
-graph()
 );%0A     
@@ -2692,23 +2692,8 @@
 urn 
-graph().unique(
 new 
@@ -2714,24 +2714,24 @@
 (t, 
-o
+forO
 bject
-()
 , null)
-)
 ;%0A  
@@ -2920,11 +2920,131 @@
 r;%0A    %7D
+%0A%0A    public ValueNode getX() %7B%0A        return object;%0A    %7D%0A%0A    public ValueNode getY() %7B%0A        return mirror;%0A    %7D
 %0A%7D%0A

make InstanceOfDynamicNode a BinaryOpLogicNode