1 /*
   2  * Copyright (c) 2015, 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.hotspot.meta;
  24 
  25 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
  26 import static org.graalvm.compiler.core.common.GraalOptions.ImmutableCode;
  27 import static org.graalvm.compiler.hotspot.meta.HotSpotGraalConstantFieldProvider.FieldReadEnabledInImmutableCode;
  28 
  29 import org.graalvm.compiler.core.common.type.StampPair;
  30 import org.graalvm.compiler.nodes.ConstantNode;
  31 import org.graalvm.compiler.nodes.ValueNode;
  32 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  33 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderTool;
  34 import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
  35 import org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin;
  36 import org.graalvm.compiler.nodes.graphbuilderconf.TypePlugin;
  37 import org.graalvm.compiler.nodes.util.ConstantFoldUtil;
  38 import org.graalvm.compiler.replacements.WordOperationPlugin;
  39 import org.graalvm.compiler.word.Word;
  40 
  41 import jdk.vm.ci.meta.JavaConstant;
  42 import jdk.vm.ci.meta.JavaKind;
  43 import jdk.vm.ci.meta.JavaType;
  44 import jdk.vm.ci.meta.JavaTypeProfile;
  45 import jdk.vm.ci.meta.ResolvedJavaField;
  46 import jdk.vm.ci.meta.ResolvedJavaMethod;
  47 import jdk.vm.ci.meta.ResolvedJavaType;
  48 
  49 /**
  50  * This plugin handles the HotSpot-specific customizations of bytecode parsing:
  51  * <p>
  52  * {@link Word}-type rewriting for {@link GraphBuilderContext#parsingIntrinsic intrinsic} functions
  53  * (snippets and method substitutions), by forwarding to the {@link WordOperationPlugin}. Note that
  54  * we forward the {@link NodePlugin} and {@link TypePlugin} methods, but not the
  55  * {@link InlineInvokePlugin} methods implemented by {@link WordOperationPlugin}. The latter is not
  56  * necessary because HotSpot only uses the {@link Word} type in methods that are force-inlined,
  57  * i.e., there are never non-inlined invokes that involve the {@link Word} type.
  58  * <p>
  59  * Constant folding of field loads.
  60  */
  61 public final class HotSpotNodePlugin implements NodePlugin, TypePlugin {
  62     protected final WordOperationPlugin wordOperationPlugin;
  63 
  64     public HotSpotNodePlugin(WordOperationPlugin wordOperationPlugin) {
  65         this.wordOperationPlugin = wordOperationPlugin;
  66     }
  67 
  68     @Override
  69     public boolean canChangeStackKind(GraphBuilderContext b) {
  70         if (b.parsingIntrinsic()) {
  71             return wordOperationPlugin.canChangeStackKind(b);
  72         }
  73         return false;
  74     }
  75 
  76     @Override
  77     public StampPair interceptType(GraphBuilderTool b, JavaType declaredType, boolean nonNull) {
  78         if (b.parsingIntrinsic()) {
  79             return wordOperationPlugin.interceptType(b, declaredType, nonNull);
  80         }
  81         return null;
  82     }
  83 
  84     @Override
  85     public boolean handleInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
  86         if (b.parsingIntrinsic() && wordOperationPlugin.handleInvoke(b, method, args)) {
  87             return true;
  88         }
  89         return false;
  90     }
  91 
  92     @Override
  93     public boolean handleLoadField(GraphBuilderContext b, ValueNode object, ResolvedJavaField field) {
  94         if (!ImmutableCode.getValue() || b.parsingIntrinsic()) {
  95             if (object.isConstant()) {
  96                 JavaConstant asJavaConstant = object.asJavaConstant();
  97                 if (tryReadField(b, field, asJavaConstant)) {
  98                     return true;
  99                 }
 100             }
 101         }
 102         if (b.parsingIntrinsic() && wordOperationPlugin.handleLoadField(b, object, field)) {
 103             return true;
 104         }
 105         return false;
 106     }
 107 
 108     @Override
 109     public boolean handleLoadStaticField(GraphBuilderContext b, ResolvedJavaField field) {
 110         if (!ImmutableCode.getValue() || b.parsingIntrinsic()) {
 111             if (tryReadField(b, field, null)) {
 112                 return true;
 113             }
 114         }
 115         if (GeneratePIC.getValue()) {
 116             if (field.isSynthetic() && field.getName().startsWith("$assertionsDisabled")) {
 117                 return tryReadField(b, field, null);
 118             }
 119         }
 120         if (b.parsingIntrinsic() && wordOperationPlugin.handleLoadStaticField(b, field)) {
 121             return true;
 122         }
 123         return false;
 124     }
 125 
 126     private static boolean tryReadField(GraphBuilderContext b, ResolvedJavaField field, JavaConstant object) {
 127         // FieldReadEnabledInImmutableCode is non null only if assertions are enabled
 128         if (FieldReadEnabledInImmutableCode != null && ImmutableCode.getValue()) {
 129             FieldReadEnabledInImmutableCode.set(Boolean.TRUE);
 130             try {
 131                 return tryConstantFold(b, field, object);
 132             } finally {
 133                 FieldReadEnabledInImmutableCode.set(null);
 134             }
 135         } else {
 136             return tryConstantFold(b, field, object);
 137         }
 138     }
 139 
 140     private static boolean tryConstantFold(GraphBuilderContext b, ResolvedJavaField field, JavaConstant object) {
 141         ConstantNode result = ConstantFoldUtil.tryConstantFold(b.getConstantFieldProvider(), b.getConstantReflection(), b.getMetaAccess(), field, object);
 142         if (result != null) {
 143             result = b.getGraph().unique(result);
 144             b.push(field.getJavaKind(), result);
 145             return true;
 146         }
 147         return false;
 148     }
 149 
 150     @Override
 151     public boolean handleStoreField(GraphBuilderContext b, ValueNode object, ResolvedJavaField field, ValueNode value) {
 152         if (b.parsingIntrinsic() && wordOperationPlugin.handleStoreField(b, object, field, value)) {
 153             return true;
 154         }
 155         return false;
 156     }
 157 
 158     @Override
 159     public boolean handleStoreStaticField(GraphBuilderContext b, ResolvedJavaField field, ValueNode value) {
 160         if (b.parsingIntrinsic() && wordOperationPlugin.handleStoreStaticField(b, field, value)) {
 161             return true;
 162         }
 163         return false;
 164     }
 165 
 166     @Override
 167     public boolean handleLoadIndexed(GraphBuilderContext b, ValueNode array, ValueNode index, JavaKind elementKind) {
 168         if (b.parsingIntrinsic() && wordOperationPlugin.handleLoadIndexed(b, array, index, elementKind)) {
 169             return true;
 170         }
 171         return false;
 172     }
 173 
 174     @Override
 175     public boolean handleStoreIndexed(GraphBuilderContext b, ValueNode array, ValueNode index, JavaKind elementKind, ValueNode value) {
 176         if (b.parsingIntrinsic() && wordOperationPlugin.handleStoreIndexed(b, array, index, elementKind, value)) {
 177             return true;
 178         }
 179         return false;
 180     }
 181 
 182     @Override
 183     public boolean handleCheckCast(GraphBuilderContext b, ValueNode object, ResolvedJavaType type, JavaTypeProfile profile) {
 184         if (b.parsingIntrinsic() && wordOperationPlugin.handleCheckCast(b, object, type, profile)) {
 185             return true;
 186         }
 187         return false;
 188     }
 189 
 190     @Override
 191     public boolean handleInstanceOf(GraphBuilderContext b, ValueNode object, ResolvedJavaType type, JavaTypeProfile profile) {
 192         if (b.parsingIntrinsic() && wordOperationPlugin.handleInstanceOf(b, object, type, profile)) {
 193             return true;
 194         }
 195         return false;
 196     }
 197 }