43972
|
1 |
/*
|
46344
|
2 |
* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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.lir.sparc;
|
|
24 |
|
46344
|
25 |
import static jdk.vm.ci.code.ValueUtil.asRegister;
|
|
26 |
import static jdk.vm.ci.sparc.SPARC.g0;
|
|
27 |
import static jdk.vm.ci.sparc.SPARCKind.WORD;
|
43972
|
28 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.BPCC;
|
|
29 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.Annul.ANNUL;
|
|
30 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.Annul.NOT_ANNUL;
|
|
31 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.BranchPredict.PREDICT_NOT_TAKEN;
|
|
32 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.BranchPredict.PREDICT_TAKEN;
|
|
33 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.CC.Xcc;
|
|
34 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.Equal;
|
|
35 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.Less;
|
|
36 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.NotEqual;
|
|
37 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
|
|
38 |
|
|
39 |
import org.graalvm.compiler.asm.Label;
|
|
40 |
import org.graalvm.compiler.asm.sparc.SPARCAddress;
|
|
41 |
import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler;
|
|
42 |
import org.graalvm.compiler.core.common.LIRKind;
|
|
43 |
import org.graalvm.compiler.lir.LIRInstructionClass;
|
|
44 |
import org.graalvm.compiler.lir.Opcode;
|
|
45 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
|
|
46 |
import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
|
|
47 |
|
|
48 |
import jdk.vm.ci.code.Register;
|
46344
|
49 |
import jdk.vm.ci.meta.AllocatableValue;
|
43972
|
50 |
import jdk.vm.ci.meta.JavaKind;
|
|
51 |
import jdk.vm.ci.sparc.SPARCKind;
|
|
52 |
|
|
53 |
/**
|
|
54 |
* Emits code which compares two arrays of the same length.
|
|
55 |
*/
|
|
56 |
@Opcode("ARRAY_EQUALS")
|
|
57 |
public final class SPARCArrayEqualsOp extends SPARCLIRInstruction {
|
|
58 |
public static final LIRInstructionClass<SPARCArrayEqualsOp> TYPE = LIRInstructionClass.create(SPARCArrayEqualsOp.class);
|
|
59 |
public static final SizeEstimate SIZE = SizeEstimate.create(32);
|
|
60 |
|
|
61 |
private final JavaKind kind;
|
|
62 |
private final int arrayBaseOffset;
|
|
63 |
private final int arrayIndexScale;
|
|
64 |
|
46344
|
65 |
@Def({REG}) protected AllocatableValue resultValue;
|
|
66 |
@Alive({REG}) protected AllocatableValue array1Value;
|
|
67 |
@Alive({REG}) protected AllocatableValue array2Value;
|
|
68 |
@Alive({REG}) protected AllocatableValue lengthValue;
|
|
69 |
@Temp({REG}) protected AllocatableValue temp1;
|
|
70 |
@Temp({REG}) protected AllocatableValue temp2;
|
|
71 |
@Temp({REG}) protected AllocatableValue temp3;
|
|
72 |
@Temp({REG}) protected AllocatableValue temp4;
|
|
73 |
@Temp({REG}) protected AllocatableValue temp5;
|
43972
|
74 |
|
46344
|
75 |
public SPARCArrayEqualsOp(LIRGeneratorTool tool, JavaKind kind, AllocatableValue result, AllocatableValue array1, AllocatableValue array2, AllocatableValue length) {
|
43972
|
76 |
super(TYPE, SIZE);
|
46963
|
77 |
|
|
78 |
assert !kind.isNumericFloat() : "Float arrays comparison (bitwise_equal || both_NaN) isn't supported";
|
43972
|
79 |
this.kind = kind;
|
|
80 |
|
48861
|
81 |
this.arrayBaseOffset = tool.getProviders().getArrayOffsetProvider().arrayBaseOffset(kind);
|
|
82 |
this.arrayIndexScale = tool.getProviders().getArrayOffsetProvider().arrayScalingFactor(kind);
|
43972
|
83 |
|
|
84 |
this.resultValue = result;
|
|
85 |
this.array1Value = array1;
|
|
86 |
this.array2Value = array2;
|
|
87 |
this.lengthValue = length;
|
|
88 |
|
|
89 |
// Allocate some temporaries.
|
|
90 |
this.temp1 = tool.newVariable(LIRKind.unknownReference(tool.target().arch.getWordKind()));
|
|
91 |
this.temp2 = tool.newVariable(LIRKind.unknownReference(tool.target().arch.getWordKind()));
|
|
92 |
this.temp3 = tool.newVariable(LIRKind.value(tool.target().arch.getWordKind()));
|
|
93 |
this.temp4 = tool.newVariable(LIRKind.value(tool.target().arch.getWordKind()));
|
|
94 |
this.temp5 = tool.newVariable(LIRKind.value(tool.target().arch.getWordKind()));
|
|
95 |
}
|
|
96 |
|
|
97 |
@Override
|
|
98 |
public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
|
|
99 |
Register result = asRegister(resultValue);
|
|
100 |
Register array1 = asRegister(temp1);
|
|
101 |
Register array2 = asRegister(temp2);
|
|
102 |
Register length = asRegister(temp3);
|
|
103 |
|
|
104 |
Label trueLabel = new Label();
|
|
105 |
Label falseLabel = new Label();
|
|
106 |
Label done = new Label();
|
|
107 |
|
|
108 |
// Load array base addresses.
|
|
109 |
masm.add(asRegister(array1Value), arrayBaseOffset, array1);
|
|
110 |
masm.add(asRegister(array2Value), arrayBaseOffset, array2);
|
|
111 |
|
|
112 |
// Get array length in bytes.
|
|
113 |
masm.mulx(asRegister(lengthValue, WORD), arrayIndexScale, length);
|
|
114 |
masm.mov(length, result); // copy
|
|
115 |
|
|
116 |
emit8ByteCompare(masm, result, array1, array2, length, trueLabel, falseLabel);
|
|
117 |
emitTailCompares(masm, result, array1, array2, trueLabel, falseLabel);
|
|
118 |
|
|
119 |
// Return true
|
|
120 |
masm.bind(trueLabel);
|
|
121 |
masm.mov(1, result);
|
|
122 |
masm.jmp(done);
|
|
123 |
|
|
124 |
// Return false
|
|
125 |
masm.bind(falseLabel);
|
|
126 |
masm.mov(g0, result);
|
|
127 |
|
|
128 |
// That's it
|
|
129 |
masm.bind(done);
|
|
130 |
}
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Vector size used in {@link #emit8ByteCompare}.
|
|
134 |
*/
|
|
135 |
private static final int VECTOR_SIZE = 8;
|
|
136 |
|
|
137 |
/**
|
|
138 |
* Emits code that uses 8-byte vector compares.
|
|
139 |
*/
|
|
140 |
private void emit8ByteCompare(SPARCMacroAssembler masm, Register result, Register array1, Register array2, Register length, Label trueLabel, Label falseLabel) {
|
|
141 |
assert lengthValue.getPlatformKind().equals(SPARCKind.WORD);
|
|
142 |
Label loop = new Label();
|
|
143 |
Label compareTail = new Label();
|
|
144 |
Label compareTailCorrectVectorEnd = new Label();
|
|
145 |
|
|
146 |
Register tempReg1 = asRegister(temp4);
|
|
147 |
Register tempReg2 = asRegister(temp5);
|
|
148 |
|
|
149 |
masm.sra(length, 0, length);
|
|
150 |
masm.and(result, VECTOR_SIZE - 1, result); // tail count (in bytes)
|
|
151 |
masm.andcc(length, ~(VECTOR_SIZE - 1), length); // vector count (in bytes)
|
|
152 |
BPCC.emit(masm, Xcc, Equal, NOT_ANNUL, PREDICT_NOT_TAKEN, compareTail);
|
|
153 |
|
|
154 |
masm.sub(length, VECTOR_SIZE, length); // Delay slot
|
|
155 |
masm.add(array1, length, array1);
|
|
156 |
masm.add(array2, length, array2);
|
|
157 |
masm.sub(g0, length, length);
|
|
158 |
|
|
159 |
// Compare the last element first
|
|
160 |
masm.ldx(new SPARCAddress(array1, 0), tempReg1);
|
|
161 |
masm.ldx(new SPARCAddress(array2, 0), tempReg2);
|
|
162 |
masm.compareBranch(tempReg1, tempReg2, NotEqual, Xcc, falseLabel, PREDICT_NOT_TAKEN, null);
|
|
163 |
masm.compareBranch(length, 0, Equal, Xcc, compareTailCorrectVectorEnd, PREDICT_NOT_TAKEN, null);
|
|
164 |
|
|
165 |
// Load the first value from array 1 (Later done in back branch delay-slot)
|
|
166 |
masm.ldx(new SPARCAddress(array1, length), tempReg1);
|
|
167 |
masm.bind(loop);
|
|
168 |
masm.ldx(new SPARCAddress(array2, length), tempReg2);
|
|
169 |
masm.cmp(tempReg1, tempReg2);
|
|
170 |
|
|
171 |
BPCC.emit(masm, Xcc, NotEqual, NOT_ANNUL, PREDICT_NOT_TAKEN, falseLabel);
|
|
172 |
// Delay slot, not annul, add for next iteration
|
|
173 |
masm.addcc(length, VECTOR_SIZE, length);
|
|
174 |
// Annul, to prevent access past the array
|
|
175 |
BPCC.emit(masm, Xcc, NotEqual, ANNUL, PREDICT_TAKEN, loop);
|
|
176 |
masm.ldx(new SPARCAddress(array1, length), tempReg1); // Load in delay slot
|
|
177 |
|
|
178 |
// Tail count zero, therefore we can go to the end
|
|
179 |
masm.compareBranch(result, 0, Equal, Xcc, trueLabel, PREDICT_TAKEN, null);
|
|
180 |
|
|
181 |
masm.bind(compareTailCorrectVectorEnd);
|
|
182 |
// Correct the array pointers
|
|
183 |
masm.add(array1, VECTOR_SIZE, array1);
|
|
184 |
masm.add(array2, VECTOR_SIZE, array2);
|
|
185 |
|
|
186 |
masm.bind(compareTail);
|
|
187 |
}
|
|
188 |
|
|
189 |
/**
|
|
190 |
* Emits code to compare the remaining 1 to 4 bytes.
|
|
191 |
*/
|
|
192 |
private void emitTailCompares(SPARCMacroAssembler masm, Register result, Register array1, Register array2, Label trueLabel, Label falseLabel) {
|
|
193 |
Label compare2Bytes = new Label();
|
|
194 |
Label compare1Byte = new Label();
|
|
195 |
|
|
196 |
Register tempReg1 = asRegister(temp3);
|
|
197 |
Register tempReg2 = asRegister(temp4);
|
|
198 |
|
|
199 |
if (kind.getByteCount() <= 4) {
|
|
200 |
// Compare trailing 4 bytes, if any.
|
|
201 |
masm.compareBranch(result, 4, Less, Xcc, compare2Bytes, PREDICT_NOT_TAKEN, null);
|
|
202 |
|
|
203 |
masm.lduw(new SPARCAddress(array1, 0), tempReg1);
|
|
204 |
masm.lduw(new SPARCAddress(array2, 0), tempReg2);
|
|
205 |
masm.compareBranch(tempReg1, tempReg2, NotEqual, Xcc, falseLabel, PREDICT_NOT_TAKEN, null);
|
|
206 |
|
|
207 |
if (kind.getByteCount() <= 2) {
|
|
208 |
// Move array pointers forward.
|
|
209 |
masm.add(array1, 4, array1);
|
|
210 |
masm.add(array2, 4, array2);
|
|
211 |
masm.sub(result, 4, result);
|
|
212 |
|
|
213 |
// Compare trailing 2 bytes, if any.
|
|
214 |
masm.bind(compare2Bytes);
|
|
215 |
|
|
216 |
masm.compareBranch(result, 2, Less, Xcc, compare1Byte, PREDICT_TAKEN, null);
|
|
217 |
|
|
218 |
masm.lduh(new SPARCAddress(array1, 0), tempReg1);
|
|
219 |
masm.lduh(new SPARCAddress(array2, 0), tempReg2);
|
|
220 |
|
|
221 |
masm.compareBranch(tempReg1, tempReg2, NotEqual, Xcc, falseLabel, PREDICT_TAKEN, null);
|
|
222 |
|
|
223 |
// The one-byte tail compare is only required for boolean and byte arrays.
|
|
224 |
if (kind.getByteCount() <= 1) {
|
|
225 |
// Move array pointers forward before we compare the last trailing byte.
|
|
226 |
masm.add(array1, 2, array1);
|
|
227 |
masm.add(array2, 2, array2);
|
|
228 |
masm.sub(result, 2, result);
|
|
229 |
|
|
230 |
// Compare trailing byte, if any.
|
|
231 |
masm.bind(compare1Byte);
|
|
232 |
masm.compareBranch(result, 1, NotEqual, Xcc, trueLabel, PREDICT_TAKEN, null);
|
|
233 |
|
|
234 |
masm.ldub(new SPARCAddress(array1, 0), tempReg1);
|
|
235 |
masm.ldub(new SPARCAddress(array2, 0), tempReg2);
|
|
236 |
masm.compareBranch(tempReg1, tempReg2, NotEqual, Xcc, falseLabel, PREDICT_TAKEN, null);
|
|
237 |
} else {
|
|
238 |
masm.bind(compare1Byte);
|
|
239 |
}
|
|
240 |
} else {
|
|
241 |
masm.bind(compare2Bytes);
|
|
242 |
}
|
|
243 |
}
|
|
244 |
}
|
|
245 |
}
|