46344
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, 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 |
package org.graalvm.compiler.replacements;
|
|
24 |
|
|
25 |
import static org.graalvm.compiler.replacements.SnippetTemplate.DEFAULT_REPLACER;
|
|
26 |
|
|
27 |
import org.graalvm.compiler.api.replacements.Snippet;
|
|
28 |
import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
|
|
29 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
|
|
30 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
31 |
import org.graalvm.compiler.nodes.spi.LoweringTool;
|
|
32 |
import org.graalvm.compiler.options.OptionValues;
|
|
33 |
import org.graalvm.compiler.phases.util.Providers;
|
|
34 |
import org.graalvm.compiler.replacements.SnippetTemplate.AbstractTemplates;
|
|
35 |
import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
|
|
36 |
import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo;
|
|
37 |
import org.graalvm.compiler.replacements.nodes.ExplodeLoopNode;
|
|
38 |
|
|
39 |
import jdk.vm.ci.code.TargetDescription;
|
|
40 |
import sun.misc.Unsafe;
|
|
41 |
|
|
42 |
public class ConstantStringIndexOfSnippets implements Snippets {
|
|
43 |
public static class Templates extends AbstractTemplates {
|
|
44 |
|
|
45 |
private final SnippetInfo indexOfConstant = snippet(ConstantStringIndexOfSnippets.class, "indexOfConstant");
|
|
46 |
|
|
47 |
public Templates(OptionValues options, Providers providers, SnippetReflectionProvider snippetReflection, TargetDescription target) {
|
|
48 |
super(options, providers, snippetReflection, target);
|
|
49 |
}
|
|
50 |
|
|
51 |
public void lower(SnippetLowerableMemoryNode stringIndexOf, LoweringTool tool) {
|
|
52 |
StructuredGraph graph = stringIndexOf.graph();
|
|
53 |
Arguments args = new Arguments(indexOfConstant, graph.getGuardsStage(), tool.getLoweringStage());
|
|
54 |
args.add("source", stringIndexOf.getArgument(0));
|
|
55 |
args.add("sourceOffset", stringIndexOf.getArgument(1));
|
|
56 |
args.add("sourceCount", stringIndexOf.getArgument(2));
|
|
57 |
args.addConst("target", stringIndexOf.getArgument(3));
|
|
58 |
args.add("targetOffset", stringIndexOf.getArgument(4));
|
|
59 |
args.add("targetCount", stringIndexOf.getArgument(5));
|
|
60 |
args.add("origFromIndex", stringIndexOf.getArgument(6));
|
|
61 |
char[] targetCharArray = snippetReflection.asObject(char[].class, stringIndexOf.getArgument(3).asJavaConstant());
|
|
62 |
args.addConst("md2", md2(targetCharArray));
|
|
63 |
args.addConst("cache", computeCache(targetCharArray));
|
|
64 |
template(args).instantiate(providers.getMetaAccess(), stringIndexOf, DEFAULT_REPLACER, args);
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
|
68 |
static int md2(char[] target) {
|
|
69 |
int c = target.length;
|
|
70 |
if (c == 0) {
|
|
71 |
return 0;
|
|
72 |
}
|
|
73 |
char lastChar = target[c - 1];
|
|
74 |
int md2 = c;
|
|
75 |
for (int i = 0; i < c - 1; i++) {
|
|
76 |
if (target[i] == lastChar) {
|
|
77 |
md2 = (c - 1) - i;
|
|
78 |
}
|
|
79 |
}
|
|
80 |
return md2;
|
|
81 |
}
|
|
82 |
|
|
83 |
static long computeCache(char[] s) {
|
|
84 |
int c = s.length;
|
|
85 |
int cache = 0;
|
|
86 |
int i;
|
|
87 |
for (i = 0; i < c - 1; i++) {
|
|
88 |
cache |= (1 << (s[i] & 63));
|
|
89 |
}
|
|
90 |
return cache;
|
|
91 |
}
|
|
92 |
|
|
93 |
@Snippet
|
|
94 |
public static int indexOfConstant(char[] source, int sourceOffset, int sourceCount,
|
|
95 |
@ConstantParameter char[] target, int targetOffset, int targetCount,
|
|
96 |
int origFromIndex, @ConstantParameter int md2, @ConstantParameter long cache) {
|
|
97 |
int fromIndex = origFromIndex;
|
|
98 |
if (fromIndex >= sourceCount) {
|
|
99 |
return (targetCount == 0 ? sourceCount : -1);
|
|
100 |
}
|
|
101 |
if (fromIndex < 0) {
|
|
102 |
fromIndex = 0;
|
|
103 |
}
|
|
104 |
if (targetCount == 0) {
|
|
105 |
return fromIndex;
|
|
106 |
}
|
|
107 |
|
|
108 |
int targetCountLess1 = targetCount - 1;
|
|
109 |
int sourceEnd = sourceCount - targetCountLess1;
|
|
110 |
|
|
111 |
long base = Unsafe.ARRAY_CHAR_BASE_OFFSET;
|
|
112 |
int lastChar = UnsafeAccess.UNSAFE.getChar(target, base + targetCountLess1 * 2);
|
|
113 |
|
|
114 |
outer_loop: for (long i = sourceOffset + fromIndex; i < sourceEnd;) {
|
|
115 |
int src = UnsafeAccess.UNSAFE.getChar(source, base + (i + targetCountLess1) * 2);
|
|
116 |
if (src == lastChar) {
|
|
117 |
// With random strings and a 4-character alphabet,
|
|
118 |
// reverse matching at this point sets up 0.8% fewer
|
|
119 |
// frames, but (paradoxically) makes 0.3% more probes.
|
|
120 |
// Since those probes are nearer the lastChar probe,
|
|
121 |
// there is may be a net D$ win with reverse matching.
|
|
122 |
// But, reversing loop inhibits unroll of inner loop
|
|
123 |
// for unknown reason. So, does running outer loop from
|
|
124 |
// (sourceOffset - targetCountLess1) to (sourceOffset + sourceCount)
|
|
125 |
if (targetCount <= 8) {
|
|
126 |
ExplodeLoopNode.explodeLoop();
|
|
127 |
}
|
|
128 |
for (long j = 0; j < targetCountLess1; j++) {
|
|
129 |
char sourceChar = UnsafeAccess.UNSAFE.getChar(source, base + (i + j) * 2);
|
|
130 |
if (UnsafeAccess.UNSAFE.getChar(target, base + (targetOffset + j) * 2) != sourceChar) {
|
|
131 |
if ((cache & (1 << sourceChar)) == 0) {
|
|
132 |
if (md2 < j + 1) {
|
|
133 |
i += j + 1;
|
|
134 |
continue outer_loop;
|
|
135 |
}
|
|
136 |
}
|
|
137 |
i += md2;
|
|
138 |
continue outer_loop;
|
|
139 |
}
|
|
140 |
}
|
|
141 |
return (int) (i - sourceOffset);
|
|
142 |
}
|
|
143 |
if ((cache & (1 << src)) == 0) {
|
|
144 |
i += targetCountLess1;
|
|
145 |
}
|
|
146 |
i++;
|
|
147 |
}
|
|
148 |
return -1;
|
|
149 |
}
|
|
150 |
}
|