001/*
002 * Copyright (c) 2013, 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.phases;
024
025import static com.oracle.graal.hotspot.replacements.HotSpotReplacementsUtil.*;
026import static com.oracle.graal.nodes.ConstantNode.*;
027import static com.oracle.graal.nodes.NamedLocationIdentity.*;
028import jdk.internal.jvmci.common.*;
029import jdk.internal.jvmci.hotspot.*;
030import jdk.internal.jvmci.hotspot.HotSpotVMConfig.CompressEncoding;
031import jdk.internal.jvmci.meta.*;
032
033import com.oracle.graal.compiler.common.type.*;
034import com.oracle.graal.hotspot.nodes.*;
035import com.oracle.graal.hotspot.nodes.type.*;
036import com.oracle.graal.nodes.*;
037import com.oracle.graal.nodes.memory.*;
038import com.oracle.graal.nodes.memory.address.*;
039import com.oracle.graal.phases.*;
040import com.oracle.graal.phases.common.*;
041import com.oracle.graal.phases.tiers.*;
042
043/**
044 * For AOT compilation we aren't allowed to use a {@link Class} reference ({@code javaMirror})
045 * directly. Instead the {@link Class} reference should be obtained from the {@code Klass} object.
046 * The reason for this is, that in Class Data Sharing (CDS) a {@code Klass} object is mapped to a
047 * fixed address in memory, but the {@code javaMirror} is not (which lives in the Java heap).
048 *
049 * Lowering can introduce new {@link ConstantNode}s containing a {@link Class} reference, thus this
050 * phase must be applied after {@link LoweringPhase}.
051 *
052 * @see AheadOfTimeVerificationPhase
053 */
054public class LoadJavaMirrorWithKlassPhase extends BasePhase<PhaseContext> {
055
056    private final int classMirrorOffset;
057    private final CompressEncoding oopEncoding;
058
059    public LoadJavaMirrorWithKlassPhase(int classMirrorOffset, CompressEncoding oopEncoding) {
060        this.classMirrorOffset = classMirrorOffset;
061        this.oopEncoding = oopEncoding;
062    }
063
064    private ValueNode getClassConstantReplacement(StructuredGraph graph, PhaseContext context, JavaConstant constant) {
065        if (constant instanceof HotSpotObjectConstant) {
066            ConstantReflectionProvider constantReflection = context.getConstantReflection();
067            ResolvedJavaType type = constantReflection.asJavaType(constant);
068            if (type != null) {
069                MetaAccessProvider metaAccess = context.getMetaAccess();
070                Stamp stamp = StampFactory.exactNonNull(metaAccess.lookupJavaType(Class.class));
071
072                if (type instanceof HotSpotResolvedObjectType) {
073                    ConstantNode klass = ConstantNode.forConstant(context.getStampProvider().createHubStamp(true), ((HotSpotResolvedObjectType) type).klass(), metaAccess, graph);
074                    AddressNode address = graph.unique(new OffsetAddressNode(klass, ConstantNode.forLong(classMirrorOffset, graph)));
075                    ValueNode read = graph.unique(new FloatingReadNode(address, CLASS_MIRROR_LOCATION, null, stamp));
076
077                    if (((HotSpotObjectConstant) constant).isCompressed()) {
078                        return CompressionNode.compress(read, oopEncoding);
079                    } else {
080                        return read;
081                    }
082                } else {
083                    /*
084                     * Primitive classes are more difficult since they don't have a corresponding
085                     * Klass* so get them from Class.TYPE for the java box type.
086                     */
087                    HotSpotResolvedPrimitiveType primitive = (HotSpotResolvedPrimitiveType) type;
088                    ResolvedJavaType boxingClass = metaAccess.lookupJavaType(primitive.getKind().toBoxedJavaClass());
089                    ConstantNode clazz = ConstantNode.forConstant(boxingClass.getJavaClass(), metaAccess, graph);
090                    HotSpotResolvedJavaField[] a = (HotSpotResolvedJavaField[]) boxingClass.getStaticFields();
091                    HotSpotResolvedJavaField typeField = null;
092                    for (HotSpotResolvedJavaField f : a) {
093                        if (f.getName().equals("TYPE")) {
094                            typeField = f;
095                            break;
096                        }
097                    }
098                    if (typeField == null) {
099                        throw new JVMCIError("Can't find TYPE field in class");
100                    }
101
102                    if (oopEncoding != null) {
103                        stamp = NarrowOopStamp.compressed((AbstractObjectStamp) stamp, oopEncoding);
104                    }
105                    AddressNode address = graph.unique(new OffsetAddressNode(clazz, ConstantNode.forLong(typeField.offset(), graph)));
106                    ValueNode read = graph.unique(new FloatingReadNode(address, FINAL_LOCATION, null, stamp));
107
108                    if (oopEncoding == null || ((HotSpotObjectConstant) constant).isCompressed()) {
109                        return read;
110                    } else {
111                        return CompressionNode.uncompress(read, oopEncoding);
112                    }
113                }
114            }
115        }
116        return null;
117    }
118
119    @Override
120    protected void run(StructuredGraph graph, PhaseContext context) {
121        for (ConstantNode node : getConstantNodes(graph)) {
122            JavaConstant constant = node.asJavaConstant();
123            ValueNode freadNode = getClassConstantReplacement(graph, context, constant);
124            if (freadNode != null) {
125                node.replace(graph, freadNode);
126            }
127        }
128    }
129}