Skip to content

Commit 9c523b0

Browse files
committed
Avoid unnecessary CompilerToVM call and IllegalArgumentException when canonicalizing array length read.
1 parent 766ee0d commit 9c523b0

File tree

1 file changed

+10
-8
lines changed
  • compiler/src/org.graalvm.compiler.nodes/src/org/graalvm/compiler/nodes/memory

1 file changed

+10
-8
lines changed

compiler/src/org.graalvm.compiler.nodes/src/org/graalvm/compiler/nodes/memory/ReadNode.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -114,7 +114,15 @@ public static ValueNode canonicalizeRead(ValueNode read, AddressNode address, Lo
114114
if (tool.canonicalizeReads() && address instanceof OffsetAddressNode) {
115115
OffsetAddressNode objAddress = (OffsetAddressNode) address;
116116
ValueNode object = objAddress.getBase();
117-
if (metaAccess != null && object.isConstant() && !object.isNullConstant() && objAddress.getOffset().isConstant()) {
117+
// Note: readConstant cannot be used to read the array length, so in order to avoid an
118+
// unnecessary CompilerToVM.readFieldValue call ending in an IllegalArgumentException,
119+
// check if we are reading the array length location first.
120+
if (locationIdentity.equals(ARRAY_LENGTH_LOCATION)) {
121+
ValueNode length = GraphUtil.arrayLength(object, ArrayLengthProvider.FindLengthMode.CANONICALIZE_READ, tool.getConstantReflection());
122+
if (length != null) {
123+
return length;
124+
}
125+
} else if (metaAccess != null && object.isConstant() && !object.isNullConstant() && objAddress.getOffset().isConstant()) {
118126
long displacement = objAddress.getOffset().asJavaConstant().asLong();
119127
int stableDimension = ((ConstantNode) object).getStableDimension();
120128
if (locationIdentity.isImmutable() || stableDimension > 0) {
@@ -125,12 +133,6 @@ public static ValueNode canonicalizeRead(ValueNode read, AddressNode address, Lo
125133
}
126134
}
127135
}
128-
if (locationIdentity.equals(ARRAY_LENGTH_LOCATION)) {
129-
ValueNode length = GraphUtil.arrayLength(object, ArrayLengthProvider.FindLengthMode.CANONICALIZE_READ, tool.getConstantReflection());
130-
if (length != null) {
131-
return length;
132-
}
133-
}
134136
if (locationIdentity instanceof CanonicalizableLocation) {
135137
CanonicalizableLocation canonicalize = (CanonicalizableLocation) locationIdentity;
136138
ValueNode result = canonicalize.canonicalizeRead(read, address, object, tool);

0 commit comments

Comments
 (0)