# HG changeset patch # User Andreas Woess # Date 1415642586 -3600 # Node ID fe77c26ccde60fe342b94d9a3d11e3c9f4a0762d # Parent 75e72d39582038b0447c71db4557263e7e7b89d4 Truffle: fix stable array canonicalization diff -r 75e72d395820 -r fe77c26ccde6 graal/com.oracle.graal.phases.common/src/com/oracle/graal/phases/common/CanonicalizerPhase.java --- a/graal/com.oracle.graal.phases.common/src/com/oracle/graal/phases/common/CanonicalizerPhase.java Mon Nov 10 20:23:05 2014 +0100 +++ b/graal/com.oracle.graal.phases.common/src/com/oracle/graal/phases/common/CanonicalizerPhase.java Mon Nov 10 19:03:06 2014 +0100 @@ -228,15 +228,15 @@ } public boolean tryCanonicalize(final Node node, NodeClass nodeClass) { - boolean result = baseTryCanonicalize(node, nodeClass); - if (!result && customCanonicalizer != null) { + if (customCanonicalizer != null) { Node canonical = customCanonicalizer.canonicalize(node); - result = performReplacement(node, canonical); - if (!result) { + if (performReplacement(node, canonical)) { + return true; + } else { customCanonicalizer.simplify(node, tool); } } - return result; + return baseTryCanonicalize(node, nodeClass); } private static AutoCloseable getCanonicalizeableContractAssertion(Node node) { diff -r 75e72d395820 -r fe77c26ccde6 graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PartialEvaluatorCanonicalizer.java --- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PartialEvaluatorCanonicalizer.java Mon Nov 10 20:23:05 2014 +0100 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PartialEvaluatorCanonicalizer.java Mon Nov 10 19:03:06 2014 +0100 @@ -27,8 +27,10 @@ import com.oracle.graal.nodes.*; import com.oracle.graal.nodes.java.*; import com.oracle.graal.phases.common.*; -import com.oracle.truffle.api.*; +import com.oracle.graal.truffle.nodes.*; +import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; import com.oracle.truffle.api.nodes.Node.Child; +import com.oracle.truffle.api.nodes.Node.Children; final class PartialEvaluatorCanonicalizer extends CanonicalizerPhase.CustomCanonicalizer { @@ -52,32 +54,56 @@ private Node canonicalizeLoadField(LoadFieldNode loadFieldNode) { if (!loadFieldNode.isStatic() && loadFieldNode.object().isConstant() && !loadFieldNode.object().isNullConstant()) { - if (loadFieldNode.field().isFinal() || (loadFieldNode.getKind() == Kind.Object && loadFieldNode.field().getAnnotation(Child.class) != null) || - loadFieldNode.field().getAnnotation(CompilerDirectives.CompilationFinal.class) != null) { - JavaConstant constant = loadFieldNode.field().readValue(loadFieldNode.object().asJavaConstant()); - assert verifyFieldValue(loadFieldNode.field(), constant); - return ConstantNode.forConstant(constant, metaAccess); + ResolvedJavaField field = loadFieldNode.field(); + JavaType fieldType = field.getType(); + if (field.isFinal() || field.getAnnotation(CompilationFinal.class) != null || + (fieldType.getKind() == Kind.Object && (field.getAnnotation(Child.class) != null || field.getAnnotation(Children.class) != null))) { + JavaConstant constant = field.readValue(loadFieldNode.object().asJavaConstant()); + assert verifyFieldValue(field, constant); + if (constant.isNonNull() && fieldType.getKind() == Kind.Object && fieldType.getComponentType() != null && + (field.getAnnotation(CompilationFinal.class) != null || field.getAnnotation(Children.class) != null)) { + int stableDimensions = getDeclaredArrayDimensions(fieldType); + return StableArrayConstantNode.forStableArrayConstant(constant, stableDimensions, true, metaAccess); + } else { + return ConstantNode.forConstant(constant, metaAccess); + } } } return loadFieldNode; } private Node canonicalizeLoadIndex(LoadIndexedNode loadIndexedNode) { - if (loadIndexedNode.array().isConstant() && loadIndexedNode.index().isConstant()) { + if (loadIndexedNode.array() instanceof StableArrayConstantNode && loadIndexedNode.index().isConstant()) { + StableArrayConstantNode stableArray = (StableArrayConstantNode) loadIndexedNode.array(); int index = loadIndexedNode.index().asJavaConstant().asInt(); JavaConstant constant = constantReflection.readArrayElement(loadIndexedNode.array().asJavaConstant(), index); if (constant != null) { - return ConstantNode.forConstant(constant, metaAccess); + if (stableArray.getStableDimensions() > 1 && constant.isNonNull()) { + return StableArrayConstantNode.forStableArrayConstant(constant, stableArray.getStableDimensions() - 1, stableArray.isCompilationFinal(), metaAccess); + } else if (constant.isNonNull() || stableArray.isCompilationFinal()) { + return ConstantNode.forConstant(constant, metaAccess); + } } } return loadIndexedNode; } + private static int getDeclaredArrayDimensions(JavaType type) { + int dimensions = 0; + JavaType componentType = type; + while ((componentType = componentType.getComponentType()) != null) { + dimensions++; + } + return dimensions; + } + private boolean verifyFieldValue(ResolvedJavaField field, JavaConstant constant) { assert field.getAnnotation(Child.class) == null || constant.isNull() || metaAccess.lookupJavaType(com.oracle.truffle.api.nodes.Node.class).isAssignableFrom(metaAccess.lookupJavaType(constant)) : "@Child field value must be a Node: " + field + ", but was: " + constant; + assert field.getAnnotation(Children.class) == null || constant.isNull() || metaAccess.lookupJavaType(constant).isArray() : "@Children field value must be an array: " + field + ", but was: " + + constant; return true; } } diff -r 75e72d395820 -r fe77c26ccde6 graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/StableArrayConstantNode.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/StableArrayConstantNode.java Mon Nov 10 19:03:06 2014 +0100 @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.truffle.nodes; + +import com.oracle.graal.api.meta.*; +import com.oracle.graal.compiler.common.type.*; +import com.oracle.graal.nodeinfo.*; +import com.oracle.graal.nodes.*; + +/** + * The {@code StableArrayConstantNode} represents a stable or compilation final array constant. + */ +@NodeInfo(shortName = "StableConst", nameTemplate = "StableConst({p#rawvalue})") +public class StableArrayConstantNode extends ConstantNode { + protected final int stableDimensions; + protected final boolean compilationFinal; + + /** + * Constructs a new node representing the specified stable array constant. + * + * @param value the constant + * @param stableDimensions number of array dimensions that are to be considered as stable + * @param compilationFinal if {@code true}, default values are considered constant as well + */ + public static StableArrayConstantNode create(JavaConstant value, Stamp stamp, int stableDimensions, boolean compilationFinal) { + return new StableArrayConstantNode(value, stamp, stableDimensions, compilationFinal); + } + + protected StableArrayConstantNode(JavaConstant value, Stamp stamp, int stableDimensions, boolean compilationFinal) { + super(value, stamp); + assert value.getKind() == Kind.Object && value.isNonNull(); + assert stableDimensions <= 255; + this.stableDimensions = stableDimensions; + this.compilationFinal = compilationFinal; + } + + public static ConstantNode forStableArrayConstant(JavaConstant constant, int stableDimensions, boolean compilationFinal, MetaAccessProvider metaAccess, StructuredGraph graph) { + return graph.unique(forStableArrayConstant(constant, stableDimensions, compilationFinal, metaAccess)); + } + + public static ConstantNode forStableArrayConstant(JavaConstant constant, int stableDimensions, boolean compilationFinal, MetaAccessProvider metaAccess) { + if (constant.getKind() == Kind.Object && constant.isNonNull()) { + Stamp stamp = StampFactory.forConstant(constant, metaAccess); + assert stamp.javaType(metaAccess).isArray(); + return StableArrayConstantNode.create(constant, stamp, stableDimensions, compilationFinal); + } else { + throw new IllegalArgumentException(constant.toString()); + } + } + + public int getStableDimensions() { + return stableDimensions; + } + + public boolean isCompilationFinal() { + return compilationFinal; + } +}