001/*
002 * Copyright (c) 2014, 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.hotspot.replacements;
024
025import jdk.internal.jvmci.hotspot.*;
026import jdk.internal.jvmci.meta.*;
027
028import com.oracle.graal.compiler.common.type.*;
029import com.oracle.graal.graph.*;
030import com.oracle.graal.graph.spi.*;
031import com.oracle.graal.nodeinfo.*;
032import com.oracle.graal.nodes.*;
033import com.oracle.graal.nodes.extended.*;
034import com.oracle.graal.nodes.spi.*;
035
036/**
037 * Read {@code Klass::_layout_helper} and incorporate any useful stamp information based on any type
038 * information in {@code klass}.
039 */
040@NodeInfo
041public final class KlassLayoutHelperNode extends FloatingGuardedNode implements Canonicalizable, Lowerable {
042
043    public static final NodeClass<KlassLayoutHelperNode> TYPE = NodeClass.create(KlassLayoutHelperNode.class);
044    @Input protected ValueNode klass;
045    protected final HotSpotVMConfig config;
046
047    public KlassLayoutHelperNode(@InjectedNodeParameter HotSpotVMConfig config, ValueNode klass) {
048        this(config, klass, null);
049    }
050
051    public KlassLayoutHelperNode(@InjectedNodeParameter HotSpotVMConfig config, ValueNode klass, ValueNode guard) {
052        super(TYPE, StampFactory.forKind(Kind.Int), (GuardingNode) guard);
053        this.klass = klass;
054        this.config = config;
055    }
056
057    @Override
058    public boolean inferStamp() {
059        if (klass instanceof LoadHubNode) {
060            LoadHubNode hub = (LoadHubNode) klass;
061            Stamp hubStamp = hub.getValue().stamp();
062            if (hubStamp instanceof ObjectStamp) {
063                ObjectStamp objectStamp = (ObjectStamp) hubStamp;
064                ResolvedJavaType type = objectStamp.type();
065                if (type != null && !type.isJavaLangObject()) {
066                    if (!type.isArray() && !type.isInterface()) {
067                        /*
068                         * Definitely some form of instance type.
069                         */
070                        return updateStamp(StampFactory.forInteger(Kind.Int, config.klassLayoutHelperNeutralValue, Integer.MAX_VALUE));
071                    }
072                    if (type.isArray()) {
073                        return updateStamp(StampFactory.forInteger(Kind.Int, Integer.MIN_VALUE, config.klassLayoutHelperNeutralValue - 1));
074                    }
075                }
076            }
077        }
078        return false;
079    }
080
081    @Override
082    public Node canonical(CanonicalizerTool tool) {
083        if (tool.allUsagesAvailable() && hasNoUsages()) {
084            return null;
085        } else {
086            if (klass.isConstant()) {
087                if (!klass.asConstant().isDefaultForKind()) {
088                    Constant constant = stamp().readConstant(tool.getConstantReflection().getMemoryAccessProvider(), klass.asJavaConstant(), config.klassLayoutHelperOffset);
089                    return ConstantNode.forConstant(stamp(), constant, tool.getMetaAccess());
090                }
091            }
092            if (klass instanceof LoadHubNode) {
093                LoadHubNode hub = (LoadHubNode) klass;
094                Stamp hubStamp = hub.getValue().stamp();
095                if (hubStamp instanceof ObjectStamp) {
096                    ObjectStamp ostamp = (ObjectStamp) hubStamp;
097                    HotSpotResolvedObjectType type = (HotSpotResolvedObjectType) ostamp.type();
098                    if (type != null && type.isArray() && !type.getComponentType().isPrimitive()) {
099                        // The layout for all object arrays is the same.
100                        Constant constant = stamp().readConstant(tool.getConstantReflection().getMemoryAccessProvider(), type.klass(), config.klassLayoutHelperOffset);
101                        return ConstantNode.forConstant(stamp(), constant, tool.getMetaAccess());
102                    }
103                }
104            }
105            return this;
106        }
107    }
108
109    @Override
110    public void lower(LoweringTool tool) {
111        tool.getLowerer().lower(this, tool);
112    }
113
114    public ValueNode getHub() {
115        return klass;
116    }
117}