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.ValueNode;
  39 import org.graalvm.compiler.nodes.calc.FloatingNode;
  40 import org.graalvm.compiler.nodes.extended.LoadHubNode;
  41 import org.graalvm.compiler.nodes.spi.Lowerable;
  42 import org.graalvm.compiler.nodes.spi.LoweringTool;
  43 
  44 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  45 import jdk.vm.ci.meta.Constant;
  46 import jdk.vm.ci.meta.JavaKind;
  47 import jdk.vm.ci.meta.ResolvedJavaType;
  48 
  49 /**
  50  * Read {@code Klass::_layout_helper} and incorporate any useful stamp information based on any type
  51  * information in {@code klass}.
  52  */
  53 @NodeInfo(cycles = CYCLES_4, size = SIZE_1)
  54 public final class KlassLayoutHelperNode extends FloatingNode implements Canonicalizable, Lowerable {
  55 
  56     public static final NodeClass<KlassLayoutHelperNode> TYPE = NodeClass.create(KlassLayoutHelperNode.class);
  57     @Input protected ValueNode klass;
  58     protected final GraalHotSpotVMConfig config;
  59 
  60     public KlassLayoutHelperNode(@InjectedNodeParameter GraalHotSpotVMConfig config, ValueNode klass) {
  61         super(TYPE, StampFactory.forKind(JavaKind.Int));
  62         this.config = config;
  63         this.klass = klass;
  64     }
  65 
  66     @Override
  67     public boolean inferStamp() {
  68         if (klass instanceof LoadHubNode) {
  69             LoadHubNode hub = (LoadHubNode) klass;
  70             Stamp hubStamp = hub.getValue().stamp();
  71             if (hubStamp instanceof ObjectStamp) {
  72                 ObjectStamp objectStamp = (ObjectStamp) hubStamp;
  73                 ResolvedJavaType type = objectStamp.type();
  74                 if (type != null && !type.isJavaLangObject()) {
  75                     if (!type.isArray() && !type.isInterface()) {
  76                         /*
  77                          * Definitely some form of instance type.
  78                          */
  79                         return updateStamp(StampFactory.forInteger(JavaKind.Int, config.klassLayoutHelperNeutralValue, Integer.MAX_VALUE));
  80                     }
  81                     if (type.isArray()) {
  82                         return updateStamp(StampFactory.forInteger(JavaKind.Int, Integer.MIN_VALUE, config.klassLayoutHelperNeutralValue - 1));
  83                     }
  84                 }
  85             }
  86         }
  87         return false;
  88     }
  89 
  90     @Override
  91     public Node canonical(CanonicalizerTool tool) {
  92         if (tool.allUsagesAvailable() && hasNoUsages()) {
  93             return null;
  94         } else {
  95             if (klass.isConstant()) {
  96                 if (!klass.asConstant().isDefaultForKind()) {
  97                     Constant constant = stamp().readConstant(tool.getConstantReflection().getMemoryAccessProvider(), klass.asConstant(), config.klassLayoutHelperOffset);
  98                     return ConstantNode.forConstant(stamp(), constant, tool.getMetaAccess());
  99                 }
 100             }
 101             if (klass instanceof LoadHubNode) {
 102                 LoadHubNode hub = (LoadHubNode) klass;
 103                 Stamp hubStamp = hub.getValue().stamp();
 104                 if (hubStamp instanceof ObjectStamp) {
 105                     ObjectStamp ostamp = (ObjectStamp) hubStamp;
 106                     HotSpotResolvedObjectType type = (HotSpotResolvedObjectType) ostamp.type();
 107                     if (type != null && type.isArray() && !type.getComponentType().isPrimitive()) {
 108                         // The layout for all object arrays is the same.
 109                         Constant constant = stamp().readConstant(tool.getConstantReflection().getMemoryAccessProvider(), type.klass(), config.klassLayoutHelperOffset);
 110                         return ConstantNode.forConstant(stamp(), constant, tool.getMetaAccess());
 111                     }
 112                 }
 113             }
 114             return this;
 115         }
 116     }
 117 
 118     @Override
 119     public void lower(LoweringTool tool) {
 120         tool.getLowerer().lower(this, tool);
 121     }
 122 
 123     public ValueNode getHub() {
 124         return klass;
 125     }
 126 }