001/*
002 * Copyright (c) 2012, 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
025//JaCoCo Exclude
026
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.compiler.common.type.*;
030import com.oracle.graal.graph.*;
031import com.oracle.graal.graph.spi.*;
032import com.oracle.graal.nodeinfo.*;
033import com.oracle.graal.nodes.extended.*;
034import com.oracle.graal.nodes.spi.*;
035import com.oracle.graal.nodes.type.*;
036import com.oracle.graal.nodes.virtual.*;
037
038/**
039 * A node that changes the type of its input, usually narrowing it. For example, a {@link PiNode}
040 * refines the type of a receiver during type-guarded inlining to be the type tested by the guard.
041 *
042 * In contrast to a {@link GuardedValueNode}, a {@link PiNode} is useless as soon as the type of its
043 * input is as narrow or narrower than the {@link PiNode}'s type. The {@link PiNode}, and therefore
044 * also the scheduling restriction enforced by the anchor, will go away.
045 */
046@NodeInfo
047public class PiNode extends FloatingGuardedNode implements LIRLowerable, Virtualizable, IterableNodeType, Canonicalizable, ValueProxy {
048
049    public static final NodeClass<PiNode> TYPE = NodeClass.create(PiNode.class);
050    @Input ValueNode object;
051    protected final Stamp piStamp;
052
053    public ValueNode object() {
054        return object;
055    }
056
057    protected PiNode(NodeClass<? extends PiNode> c, ValueNode object, Stamp stamp) {
058        super(c, stamp, null);
059        this.object = object;
060        this.piStamp = stamp;
061    }
062
063    public PiNode(ValueNode object, Stamp stamp) {
064        this(object, stamp, null);
065    }
066
067    public PiNode(ValueNode object, Stamp stamp, ValueNode anchor) {
068        super(TYPE, stamp, (GuardingNode) anchor);
069        this.object = object;
070        this.piStamp = stamp;
071    }
072
073    public PiNode(ValueNode object, ResolvedJavaType toType, boolean exactType, boolean nonNull) {
074        this(object, StampFactory.object(toType, exactType, nonNull || StampTool.isPointerNonNull(object.stamp()), true));
075    }
076
077    @Override
078    public void generate(NodeLIRBuilderTool generator) {
079        if (object.getKind() != Kind.Void && object.getKind() != Kind.Illegal) {
080            generator.setResult(this, generator.operand(object));
081        }
082    }
083
084    @Override
085    public boolean inferStamp() {
086        if (piStamp == StampFactory.forNodeIntrinsic()) {
087            return false;
088        }
089        return updateStamp(piStamp.improveWith(object().stamp()));
090    }
091
092    @Override
093    public void virtualize(VirtualizerTool tool) {
094        ValueNode alias = tool.getAlias(object());
095        if (alias instanceof VirtualObjectNode) {
096            VirtualObjectNode virtual = (VirtualObjectNode) alias;
097            if (StampTool.typeOrNull(this) != null && StampTool.typeOrNull(this).isAssignableFrom(virtual.type())) {
098                tool.replaceWithVirtual(virtual);
099            }
100        }
101    }
102
103    @Override
104    public Node canonical(CanonicalizerTool tool) {
105        if (stamp() == StampFactory.forNodeIntrinsic()) {
106            /* The actual stamp has not been set yet. */
107            return this;
108        }
109        inferStamp();
110        if (stamp().equals(object().stamp())) {
111            return object();
112        }
113        if (getGuard() != null) {
114            for (PiNode otherPi : getGuard().asNode().usages().filter(PiNode.class)) {
115                if (object() == otherPi.object() && stamp().equals(otherPi.stamp())) {
116                    /*
117                     * Two PiNodes with the same guard and same result, so return the one with the
118                     * more precise piStamp.
119                     */
120                    Stamp newStamp = piStamp.join(otherPi.piStamp);
121                    if (newStamp.equals(otherPi.piStamp)) {
122                        return otherPi;
123                    }
124                }
125            }
126        }
127        return this;
128    }
129
130    @Override
131    public ValueNode getOriginalNode() {
132        return object;
133    }
134
135    /**
136     * Casts an object to have an exact, non-null stamp representing {@link Class}.
137     */
138    public static Class<?> asNonNullClass(Object object) {
139        return asNonNullClassIntrinsic(object, Class.class, true, true);
140    }
141
142    @NodeIntrinsic(PiNode.class)
143    private static native Class<?> asNonNullClassIntrinsic(Object object, @ConstantNodeParameter Class<?> toType, @ConstantNodeParameter boolean exactType, @ConstantNodeParameter boolean nonNull);
144
145    /**
146     * Changes the stamp of an object.
147     */
148    @NodeIntrinsic
149    public static native Object piCast(Object object, @ConstantNodeParameter Stamp stamp);
150
151    /**
152     * Changes the stamp of an object and ensures the newly stamped value does float above a given
153     * anchor.
154     */
155    @NodeIntrinsic
156    public static native Object piCast(Object object, @ConstantNodeParameter Stamp stamp, GuardingNode anchor);
157
158    /**
159     * Changes the stamp of an object to represent a given type and to indicate that the object is
160     * not null.
161     */
162    public static Object piCastNonNull(Object object, @ConstantNodeParameter Class<?> toType) {
163        return piCast(object, toType, false, true);
164    }
165
166    @NodeIntrinsic
167    public static native Object piCast(Object object, @ConstantNodeParameter Class<?> toType, @ConstantNodeParameter boolean exactType, @ConstantNodeParameter boolean nonNull);
168}