1 /* 2 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 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.replacements.amd64; 26 27 import static org.graalvm.compiler.api.directives.GraalDirectives.LIKELY_PROBABILITY; 28 import static org.graalvm.compiler.api.directives.GraalDirectives.UNLIKELY_PROBABILITY; 29 import static org.graalvm.compiler.api.directives.GraalDirectives.SLOWPATH_PROBABILITY; 30 import static org.graalvm.compiler.api.directives.GraalDirectives.injectBranchProbability; 31 32 import org.graalvm.compiler.api.replacements.ClassSubstitution; 33 import org.graalvm.compiler.api.replacements.Fold; 34 import org.graalvm.compiler.api.replacements.Fold.InjectedParameter; 35 import org.graalvm.compiler.api.replacements.MethodSubstitution; 36 import org.graalvm.compiler.nodes.DeoptimizeNode; 37 import org.graalvm.compiler.replacements.nodes.ArrayCompareToNode; 38 import org.graalvm.compiler.replacements.nodes.ArrayRegionEqualsNode; 39 import org.graalvm.compiler.word.Word; 40 import jdk.internal.vm.compiler.word.Pointer; 41 42 import jdk.vm.ci.meta.DeoptimizationAction; 43 import jdk.vm.ci.meta.DeoptimizationReason; 44 import jdk.vm.ci.meta.JavaKind; 45 import jdk.vm.ci.meta.MetaAccessProvider; 46 47 // JaCoCo Exclude 48 49 /** 50 * Substitutions for {@code java.lang.StringLatin1} methods. 51 * 52 * Since JDK 9. 53 */ 54 @ClassSubstitution(className = "java.lang.StringLatin1", optional = true) 55 public class AMD64StringLatin1Substitutions { 56 57 @Fold byteArrayBaseOffset(@njectedParameter MetaAccessProvider metaAccess)58 static int byteArrayBaseOffset(@InjectedParameter MetaAccessProvider metaAccess) { 59 return metaAccess.getArrayBaseOffset(JavaKind.Byte); 60 } 61 62 @Fold byteArrayIndexScale(@njectedParameter MetaAccessProvider metaAccess)63 static int byteArrayIndexScale(@InjectedParameter MetaAccessProvider metaAccess) { 64 return metaAccess.getArrayIndexScale(JavaKind.Byte); 65 } 66 67 @Fold charArrayBaseOffset(@njectedParameter MetaAccessProvider metaAccess)68 static int charArrayBaseOffset(@InjectedParameter MetaAccessProvider metaAccess) { 69 return metaAccess.getArrayBaseOffset(JavaKind.Char); 70 } 71 72 @Fold charArrayIndexScale(@njectedParameter MetaAccessProvider metaAccess)73 static int charArrayIndexScale(@InjectedParameter MetaAccessProvider metaAccess) { 74 return metaAccess.getArrayIndexScale(JavaKind.Char); 75 } 76 77 /** Marker value for the {@link InjectedParameter} injected parameter. */ 78 static final MetaAccessProvider INJECTED = null; 79 80 /** 81 * @param value is byte[] 82 * @param other is byte[] 83 */ 84 @MethodSubstitution compareTo(byte[] value, byte[] other)85 public static int compareTo(byte[] value, byte[] other) { 86 return ArrayCompareToNode.compareTo(value, other, value.length, other.length, JavaKind.Byte, JavaKind.Byte); 87 } 88 89 /** 90 * @param value is byte[] 91 * @param other is char[] 92 */ 93 @MethodSubstitution compareToUTF16(byte[] value, byte[] other)94 public static int compareToUTF16(byte[] value, byte[] other) { 95 return ArrayCompareToNode.compareTo(value, other, value.length, other.length, JavaKind.Byte, JavaKind.Char); 96 } 97 pointer(byte[] target)98 private static Word pointer(byte[] target) { 99 return Word.objectToTrackedPointer(target).add(byteArrayBaseOffset(INJECTED)); 100 } 101 byteOffsetPointer(byte[] source, int offset)102 private static Word byteOffsetPointer(byte[] source, int offset) { 103 return pointer(source).add(offset * byteArrayIndexScale(INJECTED)); 104 } 105 106 @MethodSubstitution indexOf(byte[] value, int ch, int origFromIndex)107 public static int indexOf(byte[] value, int ch, int origFromIndex) { 108 int fromIndex = origFromIndex; 109 if (injectBranchProbability(UNLIKELY_PROBABILITY, ch >>> 8 != 0)) { 110 // search value must be a byte value 111 return -1; 112 } 113 int length = value.length; 114 if (injectBranchProbability(UNLIKELY_PROBABILITY, fromIndex < 0)) { 115 fromIndex = 0; 116 } else if (injectBranchProbability(UNLIKELY_PROBABILITY, fromIndex >= length)) { 117 // Note: fromIndex might be near -1>>>1. 118 return -1; 119 } 120 return AMD64ArrayIndexOf.indexOf1Byte(value, length, fromIndex, (byte) ch); 121 } 122 123 @MethodSubstitution indexOf(byte[] source, int sourceCount, byte[] target, int targetCount, int origFromIndex)124 public static int indexOf(byte[] source, int sourceCount, byte[] target, int targetCount, int origFromIndex) { 125 int fromIndex = origFromIndex; 126 if (injectBranchProbability(UNLIKELY_PROBABILITY, fromIndex >= sourceCount)) { 127 return (targetCount == 0 ? sourceCount : -1); 128 } 129 if (injectBranchProbability(UNLIKELY_PROBABILITY, fromIndex < 0)) { 130 fromIndex = 0; 131 } 132 if (injectBranchProbability(UNLIKELY_PROBABILITY, targetCount == 0)) { 133 // The empty string is in every string. 134 return fromIndex; 135 } 136 if (injectBranchProbability(UNLIKELY_PROBABILITY, sourceCount - fromIndex < targetCount)) { 137 // The empty string contains nothing except the empty string. 138 return -1; 139 } 140 if (injectBranchProbability(UNLIKELY_PROBABILITY, targetCount == 1)) { 141 return AMD64ArrayIndexOf.indexOf1Byte(source, sourceCount, fromIndex, target[0]); 142 } else { 143 int haystackLength = sourceCount - (targetCount - 2); 144 int offset = fromIndex; 145 while (injectBranchProbability(LIKELY_PROBABILITY, offset < haystackLength)) { 146 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveBytes(source, haystackLength, offset, target[0], target[1]); 147 if (injectBranchProbability(UNLIKELY_PROBABILITY, indexOfResult < 0)) { 148 return -1; 149 } 150 offset = indexOfResult; 151 if (injectBranchProbability(UNLIKELY_PROBABILITY, targetCount == 2)) { 152 return offset; 153 } else { 154 Pointer cmpSourcePointer = byteOffsetPointer(source, offset); 155 Pointer targetPointer = pointer(target); 156 if (injectBranchProbability(UNLIKELY_PROBABILITY, ArrayRegionEqualsNode.regionEquals(cmpSourcePointer, targetPointer, targetCount, JavaKind.Byte))) { 157 return offset; 158 } 159 } 160 offset++; 161 } 162 return -1; 163 } 164 } 165 166 /** 167 * Intrinsic for {@code java.lang.StringLatin1.inflate([BI[CII)V}. 168 * 169 * <pre> 170 * @HotSpotIntrinsicCandidate 171 * public static void inflate(byte[] src, int src_indx, char[] dst, int dst_indx, int len) 172 * </pre> 173 */ 174 @MethodSubstitution inflate(byte[] src, int srcIndex, char[] dest, int destIndex, int len)175 public static void inflate(byte[] src, int srcIndex, char[] dest, int destIndex, int len) { 176 if (injectBranchProbability(SLOWPATH_PROBABILITY, len < 0) || 177 injectBranchProbability(SLOWPATH_PROBABILITY, srcIndex < 0) || 178 injectBranchProbability(SLOWPATH_PROBABILITY, srcIndex + len > src.length) || 179 injectBranchProbability(SLOWPATH_PROBABILITY, destIndex < 0) || 180 injectBranchProbability(SLOWPATH_PROBABILITY, destIndex + len > dest.length)) { 181 DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.BoundsCheckException); 182 } 183 184 // Offset calc. outside of the actual intrinsic. 185 Pointer srcPointer = Word.objectToTrackedPointer(src).add(byteArrayBaseOffset(INJECTED)).add(srcIndex * byteArrayIndexScale(INJECTED)); 186 Pointer destPointer = Word.objectToTrackedPointer(dest).add(charArrayBaseOffset(INJECTED)).add(destIndex * charArrayIndexScale(INJECTED)); 187 AMD64StringLatin1InflateNode.inflate(srcPointer, destPointer, len, JavaKind.Char); 188 } 189 190 /** 191 * Intrinsic for {@code }java.lang.StringLatin1.inflate([BI[BII)V}. 192 * 193 * <pre> 194 * @HotSpotIntrinsicCandidate 195 * public static void inflate(byte[] src, int src_indx, byte[] dst, int dst_indx, int len) 196 * </pre> 197 * 198 * In this variant {@code dest} refers to a byte array containing 2 byte per char so 199 * {@code destIndex} and {@code len} are in terms of char elements and have to be scaled by 2 200 * when referring to {@code dest} 201 */ 202 @MethodSubstitution inflate(byte[] src, int srcIndex, byte[] dest, int destIndex, int len)203 public static void inflate(byte[] src, int srcIndex, byte[] dest, int destIndex, int len) { 204 if (injectBranchProbability(SLOWPATH_PROBABILITY, len < 0) || 205 injectBranchProbability(SLOWPATH_PROBABILITY, srcIndex < 0) || 206 injectBranchProbability(SLOWPATH_PROBABILITY, srcIndex + len > src.length) || 207 injectBranchProbability(SLOWPATH_PROBABILITY, destIndex < 0) || 208 injectBranchProbability(SLOWPATH_PROBABILITY, destIndex * 2 + len * 2 > dest.length)) { 209 DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.BoundsCheckException); 210 } 211 212 // Offset calc. outside of the actual intrinsic. 213 Pointer srcPointer = Word.objectToTrackedPointer(src).add(byteArrayBaseOffset(INJECTED)).add(srcIndex * byteArrayIndexScale(INJECTED)); 214 Pointer destPointer = Word.objectToTrackedPointer(dest).add(byteArrayBaseOffset(INJECTED)).add(destIndex * 2 * byteArrayIndexScale(INJECTED)); 215 AMD64StringLatin1InflateNode.inflate(srcPointer, destPointer, len, JavaKind.Byte); 216 } 217 218 } 219