1 /*
   2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.hotspot.replacements;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_4;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
  27 
  28 import org.graalvm.compiler.core.common.type.ObjectStamp;
  29 import org.graalvm.compiler.core.common.type.Stamp;
  30 import org.graalvm.compiler.core.common.type.StampFactory;
  31 import org.graalvm.compiler.graph.Node;
  32 import org.graalvm.compiler.graph.NodeClass;
  33 import org.graalvm.compiler.graph.spi.Canonicalizable;
  34 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  35 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  36 import org.graalvm.compiler.nodeinfo.NodeInfo;
  37 import org.graalvm.compiler.nodes.ConstantNode;
  38 import org.graalvm.compiler.nodes.FloatingGuardedNode;
  39 import org.graalvm.compiler.nodes.ValueNode;
  40 import org.graalvm.compiler.nodes.extended.GuardingNode;
  41 import org.graalvm.compiler.nodes.extended.LoadHubNode;
  42 import org.graalvm.compiler.nodes.spi.Lowerable;
  43 import org.graalvm.compiler.nodes.spi.LoweringTool;
  44 
  45 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  46 import jdk.vm.ci.meta.Constant;
  47 import jdk.vm.ci.meta.JavaKind;
  48 import jdk.vm.ci.meta.ResolvedJavaType;
  49 
  50 /**
  51  * Read {@code Klass::_layout_helper} and incorporate any useful stamp information based on any type
  52  * information in {@code klass}.
  53  */
  54 @NodeInfo(cycles = CYCLES_4, size = SIZE_1)
  55 public final class KlassLayoutHelperNode extends FloatingGuardedNode implements Canonicalizable, Lowerable {
  56 
  57     public static final NodeClass<KlassLayoutHelperNode> TYPE = NodeClass.create(KlassLayoutHelperNode.class);
  58     @Input protected ValueNode klass;
  59     protected final GraalHotSpotVMConfig config;
  60 
  61     public KlassLayoutHelperNode(@InjectedNodeParameter GraalHotSpotVMConfig config, ValueNode klass) {
  62         this(config, klass, null);
  63     }
  64 
  65     public KlassLayoutHelperNode(@InjectedNodeParameter GraalHotSpotVMConfig config, ValueNode klass, ValueNode guard) {
  66         super(TYPE, StampFactory.forKind(JavaKind.Int), (GuardingNode) guard);
  67         this.klass = klass;
  68         this.config = config;
  69     }
  70 
  71     @Override
  72     public boolean inferStamp() {
  73         if (klass instanceof LoadHubNode) {
  74             LoadHubNode hub = (LoadHubNode) klass;
  75             Stamp hubStamp = hub.getValue().stamp();
  76             if (hubStamp instanceof ObjectStamp) {
  77                 ObjectStamp objectStamp = (ObjectStamp) hubStamp;
  78                 ResolvedJavaType type = objectStamp.type();
  79                 if (type != null && !type.isJavaLangObject()) {
  80                     if (!type.isArray() && !type.isInterface()) {
  81                         /*
  82                          * Definitely some form of instance type.
  83                          */
  84                         return updateStamp(StampFactory.forInteger(JavaKind.Int, config.klassLayoutHelperNeutralValue, Integer.MAX_VALUE));
  85                     }
  86                     if (type.isArray()) {
  87                         return updateStamp(StampFactory.forInteger(JavaKind.Int, Integer.MIN_VALUE, config.klassLayoutHelperNeutralValue - 1));
  88                     }
  89                 }
  90             }
  91         }
  92         return false;
  93     }
  94 
  95     @Override
  96     public Node canonical(CanonicalizerTool tool) {
  97         if (tool.allUsagesAvailable() && hasNoUsages()) {
  98             return null;
  99         } else {
 100             if (klass.isConstant()) {
 101                 if (!klass.asConstant().isDefaultForKind()) {
 102                     Constant constant = stamp().readConstant(tool.getConstantReflection().getMemoryAccessProvider(), klass.asConstant(), config.klassLayoutHelperOffset);
 103                     return ConstantNode.forConstant(stamp(), constant, tool.getMetaAccess());
 104                 }
 105             }
 106             if (klass instanceof LoadHubNode) {
 107                 LoadHubNode hub = (LoadHubNode) klass;
 108                 Stamp hubStamp = hub.getValue().stamp();
 109                 if (hubStamp instanceof ObjectStamp) {
 110                     ObjectStamp ostamp = (ObjectStamp) hubStamp;
 111                     HotSpotResolvedObjectType type = (HotSpotResolvedObjectType) ostamp.type();
 112                     if (type != null && type.isArray() && !type.getComponentType().isPrimitive()) {
 113                         // The layout for all object arrays is the same.
 114                         Constant constant = stamp().readConstant(tool.getConstantReflection().getMemoryAccessProvider(), type.klass(), config.klassLayoutHelperOffset);
 115                         return ConstantNode.forConstant(stamp(), constant, tool.getMetaAccess());
 116                     }
 117                 }
 118             }
 119             return this;
 120         }
 121     }
 122 
 123     @Override
 124     public void lower(LoweringTool tool) {
 125         tool.getLowerer().lower(this, tool);
 126     }
 127 
 128     public ValueNode getHub() {
 129         return klass;
 130     }
 131 }