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 org.graalvm.compiler.core.common.GraalOptions.ImmutableCode;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.graalvm.compiler.debug.GraalError;
33 import org.graalvm.compiler.graph.NodeClass;
34 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
35 import org.graalvm.compiler.options.OptionValues;
36 import org.graalvm.compiler.replacements.SnippetCounter;
37
38 import jdk.vm.ci.meta.JavaConstant;
39 import jdk.vm.ci.meta.MetaAccessProvider;
40 import jdk.vm.ci.meta.ResolvedJavaField;
41 import jdk.vm.ci.meta.ResolvedJavaType;
42
43 /**
44 * Extends {@link HotSpotConstantFieldProvider} to override the implementation of
45 * {@link #readConstantField} with Graal specific semantics.
46 */
47 public class HotSpotGraalConstantFieldProvider extends HotSpotConstantFieldProvider {
48
49 public HotSpotGraalConstantFieldProvider(GraalHotSpotVMConfig config, MetaAccessProvider metaAccess) {
50 super(config, metaAccess);
51 this.metaAccess = metaAccess;
52 }
53
54 @Override
55 protected boolean isStaticFieldConstant(ResolvedJavaField field, OptionValues options) {
56 return super.isStaticFieldConstant(field, options) && (!ImmutableCode.getValue(options) || isEmbeddableField(field));
57 }
58
59 /**
60 * The set of fields whose values cannot be constant folded in ImmutableCode mode. This is
61 * volatile to support double-checked locking lazy initialization.
62 */
63 private volatile List<ResolvedJavaField> nonEmbeddableFields;
64
65 protected boolean isEmbeddableField(ResolvedJavaField field) {
66 if (nonEmbeddableFields == null) {
67 synchronized (this) {
68 if (nonEmbeddableFields == null) {
69 List<ResolvedJavaField> fields = new ArrayList<>();
70 try {
71 fields.add(metaAccess.lookupJavaField(Boolean.class.getDeclaredField("TRUE")));
72 fields.add(metaAccess.lookupJavaField(Boolean.class.getDeclaredField("FALSE")));
73
74 Class<?> characterCacheClass = Character.class.getDeclaredClasses()[0];
75 assert "java.lang.Character$CharacterCache".equals(characterCacheClass.getName());
76 fields.add(metaAccess.lookupJavaField(characterCacheClass.getDeclaredField("cache")));
77
78 Class<?> byteCacheClass = Byte.class.getDeclaredClasses()[0];
79 assert "java.lang.Byte$ByteCache".equals(byteCacheClass.getName());
80 fields.add(metaAccess.lookupJavaField(byteCacheClass.getDeclaredField("cache")));
81
82 Class<?> shortCacheClass = Short.class.getDeclaredClasses()[0];
83 assert "java.lang.Short$ShortCache".equals(shortCacheClass.getName());
84 fields.add(metaAccess.lookupJavaField(shortCacheClass.getDeclaredField("cache")));
85
86 Class<?> integerCacheClass = Integer.class.getDeclaredClasses()[0];
|
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];
|