# HG changeset patch # User Andreas Woess # Date 1416943619 -3600 # Node ID c2b45b536677a53414d8684f3117fb1192ba8fd5 # Parent 97026ca2a86e513beaf80fb8a7cbffdc76f43f19 Constant fold elements of switch table and enum values() arrays diff -r 97026ca2a86e -r c2b45b536677 graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/GraalOptions.java --- a/graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/GraalOptions.java Tue Nov 25 13:08:57 2014 +0100 +++ b/graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/GraalOptions.java Tue Nov 25 20:26:59 2014 +0100 @@ -274,6 +274,8 @@ public static final OptionValue MatchExpressions = new OptionValue<>(true); @Option(help = "Constant fold final fields with default values.") public static final OptionValue TrustFinalDefaultFields = new OptionValue<>(true); + @Option(help = "Mark well-known stable fields as such.") + public static final OptionValue ImplicitStableValues = new OptionValue<>(true); /** diff -r 97026ca2a86e -r c2b45b536677 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotConstantReflectionProvider.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotConstantReflectionProvider.java Tue Nov 25 13:08:57 2014 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotConstantReflectionProvider.java Tue Nov 25 20:26:59 2014 +0100 @@ -172,10 +172,13 @@ if (receiver == null) { assert hotspotField.isStatic(); - if (hotspotField.isFinal()) { + if (hotspotField.isFinal() || hotspotField.isStable()) { ResolvedJavaType holder = hotspotField.getDeclaringClass(); if (holder.isInitialized() && !holder.getName().equals(SystemClassName) && isEmbeddable(hotspotField)) { - return readFieldValue(field, receiver); + JavaConstant value = readFieldValue(field, receiver); + if (hotspotField.isFinal() || !value.isDefaultForKind()) { + return value; + } } } } else { diff -r 97026ca2a86e -r c2b45b536677 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaFieldImpl.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaFieldImpl.java Tue Nov 25 13:08:57 2014 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaFieldImpl.java Tue Nov 25 20:26:59 2014 +0100 @@ -22,6 +22,7 @@ */ package com.oracle.graal.hotspot.meta; +import static com.oracle.graal.compiler.common.GraalOptions.*; import static com.oracle.graal.hotspot.HotSpotGraalRuntime.*; import static com.oracle.graal.hotspot.meta.HotSpotResolvedObjectTypeImpl.*; @@ -143,6 +144,9 @@ return true; } assert getAnnotation(Stable.class) == null; + if (ImplicitStableValues.getValue() && isImplicitStableField()) { + return true; + } return false; } @@ -165,4 +169,33 @@ return null; } } + + private boolean isArray() { + JavaType fieldType = getType(); + return fieldType instanceof ResolvedJavaType && ((ResolvedJavaType) fieldType).isArray(); + } + + private boolean isImplicitStableField() { + if (isSynthetic()) { + if (isSyntheticImplicitStableField()) { + return true; + } + } + return false; + } + + private boolean isSyntheticImplicitStableField() { + assert this.isSynthetic(); + if (isStatic() && isArray()) { + if (isFinal() && name.equals("$VALUES") || name.equals("ENUM$VALUES")) { + // generated int[] field for EnumClass::values() + return true; + } else if (name.startsWith("$SwitchMap$") || name.startsWith("$SWITCH_TABLE$")) { + // javac and ecj generate a static field in an inner class for a switch on an enum + // named $SwitchMap$p$k$g$EnumClass and $SWITCH_TABLE$p$k$g$EnumClass, respectively + return true; + } + } + return false; + } }