1 /*
   2  * Copyright (c) 2016, 2018, 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 
  24 
  25 package org.graalvm.compiler.hotspot.meta;
  26 
  27 import static jdk.vm.ci.services.Services.IS_BUILDING_NATIVE_IMAGE;
  28 import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
  29 import static org.graalvm.compiler.core.common.GraalOptions.ImmutableCode;
  30 
  31 import java.util.ArrayList;
  32 import java.util.List;
  33 
  34 import org.graalvm.compiler.debug.GraalError;
  35 import org.graalvm.compiler.graph.NodeClass;
  36 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  37 import org.graalvm.compiler.options.OptionValues;
  38 import org.graalvm.compiler.replacements.SnippetCounter;
  39 
  40 import jdk.vm.ci.meta.JavaConstant;
  41 import jdk.vm.ci.meta.MetaAccessProvider;
  42 import jdk.vm.ci.meta.ResolvedJavaField;
  43 import jdk.vm.ci.meta.ResolvedJavaType;
  44 
  45 /**
  46  * Extends {@link HotSpotConstantFieldProvider} to override the implementation of
  47  * {@link #readConstantField} with Graal specific semantics.
  48  */
  49 public class HotSpotGraalConstantFieldProvider extends HotSpotConstantFieldProvider {
  50 
  51     public HotSpotGraalConstantFieldProvider(GraalHotSpotVMConfig config, MetaAccessProvider metaAccess) {
  52         super(config, metaAccess);
  53         this.metaAccess = metaAccess;
  54     }
  55 
  56     @Override
  57     protected boolean isStaticFieldConstant(ResolvedJavaField field, OptionValues options) {
  58         return super.isStaticFieldConstant(field, options) && (!ImmutableCode.getValue(options) || isEmbeddableField(field));
  59     }
  60 
  61     /**
  62      * The set of fields whose values cannot be constant folded in ImmutableCode mode. This is
  63      * volatile to support double-checked locking lazy initialization.
  64      */
  65     private volatile List<ResolvedJavaField> nonEmbeddableFields;
  66 
  67     protected boolean isEmbeddableField(ResolvedJavaField field) {
  68         if (!IS_IN_NATIVE_IMAGE && (IS_BUILDING_NATIVE_IMAGE || nonEmbeddableFields == null)) {
  69             synchronized (this) {
  70                 if (nonEmbeddableFields == null) {
  71                     List<ResolvedJavaField> fields = new ArrayList<>();
  72                     try {
  73                         fields.add(metaAccess.lookupJavaField(Boolean.class.getDeclaredField("TRUE")));
  74                         fields.add(metaAccess.lookupJavaField(Boolean.class.getDeclaredField("FALSE")));
  75 
  76                         Class<?> characterCacheClass = Character.class.getDeclaredClasses()[0];
  77                         assert "java.lang.Character$CharacterCache".equals(characterCacheClass.getName());
  78                         fields.add(metaAccess.lookupJavaField(characterCacheClass.getDeclaredField("cache")));
  79 
  80                         Class<?> byteCacheClass = Byte.class.getDeclaredClasses()[0];
  81                         assert "java.lang.Byte$ByteCache".equals(byteCacheClass.getName());
  82                         fields.add(metaAccess.lookupJavaField(byteCacheClass.getDeclaredField("cache")));
  83 
  84                         Class<?> shortCacheClass = Short.class.getDeclaredClasses()[0];
  85                         assert "java.lang.Short$ShortCache".equals(shortCacheClass.getName());
  86                         fields.add(metaAccess.lookupJavaField(shortCacheClass.getDeclaredField("cache")));
  87 
  88                         Class<?> integerCacheClass = Integer.class.getDeclaredClasses()[0];
  89                         assert "java.lang.Integer$IntegerCache".equals(integerCacheClass.getName());
  90                         fields.add(metaAccess.lookupJavaField(integerCacheClass.getDeclaredField("cache")));
  91 
  92                         Class<?> longCacheClass = Long.class.getDeclaredClasses()[0];
  93                         assert "java.lang.Long$LongCache".equals(longCacheClass.getName());
  94                         fields.add(metaAccess.lookupJavaField(longCacheClass.getDeclaredField("cache")));
  95 
  96                         fields.add(metaAccess.lookupJavaField(Throwable.class.getDeclaredField("UNASSIGNED_STACK")));
  97                         fields.add(metaAccess.lookupJavaField(Throwable.class.getDeclaredField("SUPPRESSED_SENTINEL")));
  98                     } catch (SecurityException | NoSuchFieldException e) {
  99                         throw new GraalError(e);
 100                     }
 101                     nonEmbeddableFields = fields;
 102                 }
 103             }
 104         }
 105         return !nonEmbeddableFields.contains(field);
 106     }
 107 
 108     @Override
 109     protected boolean isFinalFieldValueConstant(ResolvedJavaField field, JavaConstant value, ConstantFieldTool<?> tool) {
 110         if (super.isFinalFieldValueConstant(field, value, tool)) {
 111             return true;
 112         }
 113 
 114         if (!field.isStatic()) {
 115             JavaConstant receiver = tool.getReceiver();
 116             if (getSnippetCounterType().isInstance(receiver) || getNodeClassType().isInstance(receiver)) {
 117                 return true;
 118             }
 119         }
 120 
 121         return false;
 122     }
 123 
 124     @Override
 125     protected boolean isStableFieldValueConstant(ResolvedJavaField field, JavaConstant value, ConstantFieldTool<?> tool) {
 126         if (super.isStableFieldValueConstant(field, value, tool)) {
 127             return true;
 128         }
 129 
 130         if (!field.isStatic()) {
 131             JavaConstant receiver = tool.getReceiver();
 132             if (getHotSpotVMConfigType().isInstance(receiver)) {
 133                 return true;
 134             }
 135         }
 136 
 137         return false;
 138     }
 139 
 140     private final MetaAccessProvider metaAccess;
 141 
 142     private ResolvedJavaType cachedHotSpotVMConfigType;
 143     private ResolvedJavaType cachedSnippetCounterType;
 144     private ResolvedJavaType cachedNodeClassType;
 145 
 146     private ResolvedJavaType getHotSpotVMConfigType() {
 147         if (cachedHotSpotVMConfigType == null) {
 148             cachedHotSpotVMConfigType = metaAccess.lookupJavaType(GraalHotSpotVMConfig.class);
 149         }
 150         return cachedHotSpotVMConfigType;
 151     }
 152 
 153     private ResolvedJavaType getSnippetCounterType() {
 154         if (cachedSnippetCounterType == null) {
 155             cachedSnippetCounterType = metaAccess.lookupJavaType(SnippetCounter.class);
 156         }
 157         return cachedSnippetCounterType;
 158     }
 159 
 160     private ResolvedJavaType getNodeClassType() {
 161         if (cachedNodeClassType == null) {
 162             cachedNodeClassType = metaAccess.lookupJavaType(NodeClass.class);
 163         }
 164         return cachedNodeClassType;
 165     }
 166 }