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 (Node use : getGuard().asNode().usages()) {
115                if (use instanceof PiNode) {
116                    PiNode otherPi = (PiNode) use;
117                    if (object() == otherPi.object() && stamp().equals(otherPi.stamp())) {
118                        /*
119                         * Two PiNodes with the same guard and same result, so return the one with
120                         * the more precise piStamp.
121                         */
122                        Stamp newStamp = piStamp.join(otherPi.piStamp);
123                        if (newStamp.equals(otherPi.piStamp)) {
124                            return otherPi;
125                        }
126                    }
127                }
128            }
129        }
130        return this;
131    }
132
133    @Override
134    public ValueNode getOriginalNode() {
135        return object;
136    }
137
138    /**
139     * Casts an object to have an exact, non-null stamp representing {@link Class}.
140     */
141    public static Class<?> asNonNullClass(Object object) {
142        return asNonNullClassIntrinsic(object, Class.class, true, true);
143    }
144
145    @NodeIntrinsic(PiNode.class)
146    private static native Class<?> asNonNullClassIntrinsic(Object object, @ConstantNodeParameter Class<?> toType, @ConstantNodeParameter boolean exactType, @ConstantNodeParameter boolean nonNull);
147
148    /**
149     * Changes the stamp of an object.
150     */
151    @NodeIntrinsic
152    public static native Object piCast(Object object, @ConstantNodeParameter Stamp stamp);
153
154    /**
155     * Changes the stamp of an object and ensures the newly stamped value does float above a given
156     * anchor.
157     */
158    @NodeIntrinsic
159    public static native Object piCast(Object object, @ConstantNodeParameter Stamp stamp, GuardingNode anchor);
160
161    /**
162     * Changes the stamp of an object to represent a given type and to indicate that the object is
163     * not null.
164     */
165    public static Object piCastNonNull(Object object, @ConstantNodeParameter Class<?> toType) {
166        return piCast(object, toType, false, true);
167    }
168
169    @NodeIntrinsic
170    public static native Object piCast(Object object, @ConstantNodeParameter Class<?> toType, @ConstantNodeParameter boolean exactType, @ConstantNodeParameter boolean nonNull);
171}