1 /*
   2  * Copyright (c) 2009, 2016, 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.nodes.java;
  24 
  25 import static org.graalvm.compiler.graph.iterators.NodePredicates.isNotA;
  26 
  27 import org.graalvm.compiler.core.common.type.Stamp;
  28 import org.graalvm.compiler.core.common.type.StampFactory;
  29 import org.graalvm.compiler.core.common.type.StampPair;
  30 import org.graalvm.compiler.graph.NodeClass;
  31 import org.graalvm.compiler.graph.spi.Canonicalizable;
  32 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  33 import org.graalvm.compiler.nodeinfo.NodeInfo;
  34 import org.graalvm.compiler.nodes.ConstantNode;
  35 import org.graalvm.compiler.nodes.DeoptimizeNode;
  36 import org.graalvm.compiler.nodes.PhiNode;
  37 import org.graalvm.compiler.nodes.ValueNode;
  38 import org.graalvm.compiler.nodes.ValuePhiNode;
  39 import org.graalvm.compiler.nodes.spi.UncheckedInterfaceProvider;
  40 import org.graalvm.compiler.nodes.spi.Virtualizable;
  41 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  42 import org.graalvm.compiler.nodes.type.StampTool;
  43 import org.graalvm.compiler.nodes.util.ConstantFoldUtil;
  44 import org.graalvm.compiler.nodes.virtual.VirtualInstanceNode;
  45 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  46 
  47 import jdk.vm.ci.meta.Assumptions;
  48 import jdk.vm.ci.meta.DeoptimizationAction;
  49 import jdk.vm.ci.meta.DeoptimizationReason;
  50 import jdk.vm.ci.meta.JavaConstant;
  51 import jdk.vm.ci.meta.JavaKind;
  52 import jdk.vm.ci.meta.MetaAccessProvider;
  53 import jdk.vm.ci.meta.ResolvedJavaField;
  54 
  55 /**
  56  * The {@code LoadFieldNode} represents a read of a static or instance field.
  57  */
  58 @NodeInfo(nameTemplate = "LoadField#{p#field/s}")
  59 public final class LoadFieldNode extends AccessFieldNode implements Canonicalizable.Unary<ValueNode>, Virtualizable, UncheckedInterfaceProvider {
  60 
  61     public static final NodeClass<LoadFieldNode> TYPE = NodeClass.create(LoadFieldNode.class);
  62 
  63     private final Stamp uncheckedStamp;
  64 
  65     protected LoadFieldNode(StampPair stamp, ValueNode object, ResolvedJavaField field) {
  66         super(TYPE, stamp.getTrustedStamp(), object, field);
  67         this.uncheckedStamp = stamp.getUncheckedStamp();
  68     }
  69 
  70     public static LoadFieldNode create(Assumptions assumptions, ValueNode object, ResolvedJavaField field) {
  71         return new LoadFieldNode(StampFactory.forDeclaredType(assumptions, field.getType(), false), object, field);
  72     }
  73 
  74     public static LoadFieldNode createOverrideStamp(StampPair stamp, ValueNode object, ResolvedJavaField field) {
  75         return new LoadFieldNode(stamp, object, field);
  76     }
  77 
  78     @Override
  79     public ValueNode getValue() {
  80         return object();
  81     }
  82 
  83     @Override
  84     public ValueNode canonical(CanonicalizerTool tool, ValueNode forObject) {
  85         if (tool.allUsagesAvailable() && hasNoUsages() && !isVolatile() && (isStatic() || StampTool.isPointerNonNull(forObject.stamp()))) {
  86             return null;
  87         }
  88         MetaAccessProvider metaAccess = tool.getMetaAccess();
  89         if (tool.canonicalizeReads() && metaAccess != null) {
  90             ConstantNode constant = asConstant(tool, forObject);
  91             if (constant != null) {
  92                 return constant;
  93             }
  94             if (tool.allUsagesAvailable()) {
  95                 PhiNode phi = asPhi(tool, forObject);
  96                 if (phi != null) {
  97                     return phi;
  98                 }
  99             }
 100         }
 101         if (!isStatic() && forObject.isNullConstant()) {
 102             return new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.NullCheckException);
 103         }
 104         return this;
 105     }
 106 
 107     /**
 108      * Gets a constant value for this load if possible.
 109      */
 110     public ConstantNode asConstant(CanonicalizerTool tool, ValueNode forObject) {
 111         if (isStatic()) {
 112             return ConstantFoldUtil.tryConstantFold(tool.getConstantFieldProvider(), tool.getConstantReflection(), tool.getMetaAccess(), field(), null, getOptions());
 113         } else if (forObject.isConstant() && !forObject.isNullConstant()) {
 114             return ConstantFoldUtil.tryConstantFold(tool.getConstantFieldProvider(), tool.getConstantReflection(), tool.getMetaAccess(), field(), forObject.asJavaConstant(), getOptions());
 115         }
 116         return null;
 117     }
 118 
 119     public ConstantNode asConstant(CanonicalizerTool tool, JavaConstant constant) {
 120         return ConstantFoldUtil.tryConstantFold(tool.getConstantFieldProvider(), tool.getConstantReflection(), tool.getMetaAccess(), field(), constant, getOptions());
 121     }
 122 
 123     private PhiNode asPhi(CanonicalizerTool tool, ValueNode forObject) {
 124         if (!isStatic() && field.isFinal() && forObject instanceof ValuePhiNode && ((ValuePhiNode) forObject).values().filter(isNotA(ConstantNode.class)).isEmpty()) {
 125             PhiNode phi = (PhiNode) forObject;
 126             ConstantNode[] constantNodes = new ConstantNode[phi.valueCount()];
 127             for (int i = 0; i < phi.valueCount(); i++) {
 128                 ConstantNode constant = ConstantFoldUtil.tryConstantFold(tool.getConstantFieldProvider(), tool.getConstantReflection(), tool.getMetaAccess(), field(), phi.valueAt(i).asJavaConstant(),
 129                                 getOptions());
 130                 if (constant == null) {
 131                     return null;
 132                 }
 133                 constantNodes[i] = constant;
 134             }
 135             return new ValuePhiNode(stamp(), phi.merge(), constantNodes);
 136         }
 137         return null;
 138     }
 139 
 140     @Override
 141     public void virtualize(VirtualizerTool tool) {
 142         ValueNode alias = tool.getAlias(object());
 143         if (alias instanceof VirtualObjectNode) {
 144             int fieldIndex = ((VirtualInstanceNode) alias).fieldIndex(field());
 145             if (fieldIndex != -1) {
 146                 ValueNode entry = tool.getEntry((VirtualObjectNode) alias, fieldIndex);
 147                 if (stamp.isCompatible(entry.stamp())) {
 148                     tool.replaceWith(entry);
 149                 } else {
 150                     assert stamp().getStackKind() == JavaKind.Int && (entry.stamp().getStackKind() == JavaKind.Long || entry.getStackKind() == JavaKind.Double ||
 151                                     entry.getStackKind() == JavaKind.Illegal) : "Can only allow different stack kind two slot marker writes on one stot fields.";
 152                 }
 153             }
 154         }
 155     }
 156 
 157     @Override
 158     public Stamp uncheckedStamp() {
 159         return uncheckedStamp;
 160     }
 161 
 162     public void setObject(ValueNode newObject) {
 163         this.updateUsages(object, newObject);
 164         this.object = newObject;
 165     }
 166 }