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