blob: 8b45ea7c4f29f7af1c7d836295faf55eb4752958 [file] [log] [blame]
Chris Larsen3039e382015-08-26 07:54:08 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "intrinsics_mips64.h"
18
19#include "arch/mips64/instruction_set_features_mips64.h"
20#include "art_method.h"
21#include "code_generator_mips64.h"
22#include "entrypoints/quick/quick_entrypoints.h"
23#include "intrinsics.h"
24#include "mirror/array-inl.h"
25#include "mirror/string.h"
26#include "thread.h"
27#include "utils/mips64/assembler_mips64.h"
28#include "utils/mips64/constants_mips64.h"
29
30namespace art {
31
32namespace mips64 {
33
34IntrinsicLocationsBuilderMIPS64::IntrinsicLocationsBuilderMIPS64(CodeGeneratorMIPS64* codegen)
35 : arena_(codegen->GetGraph()->GetArena()) {
36}
37
38Mips64Assembler* IntrinsicCodeGeneratorMIPS64::GetAssembler() {
39 return reinterpret_cast<Mips64Assembler*>(codegen_->GetAssembler());
40}
41
42ArenaAllocator* IntrinsicCodeGeneratorMIPS64::GetAllocator() {
43 return codegen_->GetGraph()->GetArena();
44}
45
Chris Larsen9701c2e2015-09-04 17:22:47 -070046#define __ codegen->GetAssembler()->
47
48static void MoveFromReturnRegister(Location trg,
49 Primitive::Type type,
50 CodeGeneratorMIPS64* codegen) {
51 if (!trg.IsValid()) {
52 DCHECK_EQ(type, Primitive::kPrimVoid);
53 return;
54 }
55
56 DCHECK_NE(type, Primitive::kPrimVoid);
57
58 if (Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) {
59 GpuRegister trg_reg = trg.AsRegister<GpuRegister>();
60 if (trg_reg != V0) {
61 __ Move(V0, trg_reg);
62 }
63 } else {
64 FpuRegister trg_reg = trg.AsFpuRegister<FpuRegister>();
65 if (trg_reg != F0) {
66 if (type == Primitive::kPrimFloat) {
67 __ MovS(F0, trg_reg);
68 } else {
69 __ MovD(F0, trg_reg);
70 }
71 }
72 }
73}
74
75static void MoveArguments(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
76 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
77 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
78}
79
80// Slow-path for fallback (calling the managed code to handle the
81// intrinsic) in an intrinsified call. This will copy the arguments
82// into the positions for a regular call.
83//
84// Note: The actual parameters are required to be in the locations
85// given by the invoke's location summary. If an intrinsic
86// modifies those locations before a slowpath call, they must be
87// restored!
88class IntrinsicSlowPathMIPS64 : public SlowPathCodeMIPS64 {
89 public:
90 explicit IntrinsicSlowPathMIPS64(HInvoke* invoke) : invoke_(invoke) { }
91
92 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
93 CodeGeneratorMIPS64* codegen = down_cast<CodeGeneratorMIPS64*>(codegen_in);
94
95 __ Bind(GetEntryLabel());
96
97 SaveLiveRegisters(codegen, invoke_->GetLocations());
98
99 MoveArguments(invoke_, codegen);
100
101 if (invoke_->IsInvokeStaticOrDirect()) {
102 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(),
103 Location::RegisterLocation(A0));
Chris Larsen9701c2e2015-09-04 17:22:47 -0700104 } else {
Alexey Frunze53afca12015-11-05 16:34:23 -0800105 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), Location::RegisterLocation(A0));
Chris Larsen9701c2e2015-09-04 17:22:47 -0700106 }
Alexey Frunze53afca12015-11-05 16:34:23 -0800107 codegen->RecordPcInfo(invoke_, invoke_->GetDexPc(), this);
Chris Larsen9701c2e2015-09-04 17:22:47 -0700108
109 // Copy the result back to the expected output.
110 Location out = invoke_->GetLocations()->Out();
111 if (out.IsValid()) {
112 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
113 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
114 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
115 }
116
117 RestoreLiveRegisters(codegen, invoke_->GetLocations());
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700118 __ Bc(GetExitLabel());
Chris Larsen9701c2e2015-09-04 17:22:47 -0700119 }
120
121 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathMIPS64"; }
122
123 private:
124 // The instruction where this slow path is happening.
125 HInvoke* const invoke_;
126
127 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathMIPS64);
128};
129
130#undef __
131
Chris Larsen3039e382015-08-26 07:54:08 -0700132bool IntrinsicLocationsBuilderMIPS64::TryDispatch(HInvoke* invoke) {
133 Dispatch(invoke);
134 LocationSummary* res = invoke->GetLocations();
135 return res != nullptr && res->Intrinsified();
136}
137
138#define __ assembler->
139
140static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
141 LocationSummary* locations = new (arena) LocationSummary(invoke,
142 LocationSummary::kNoCall,
143 kIntrinsified);
144 locations->SetInAt(0, Location::RequiresFpuRegister());
145 locations->SetOut(Location::RequiresRegister());
146}
147
148static void MoveFPToInt(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
149 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
150 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
151
152 if (is64bit) {
153 __ Dmfc1(out, in);
154 } else {
155 __ Mfc1(out, in);
156 }
157}
158
159// long java.lang.Double.doubleToRawLongBits(double)
160void IntrinsicLocationsBuilderMIPS64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
161 CreateFPToIntLocations(arena_, invoke);
162}
163
164void IntrinsicCodeGeneratorMIPS64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000165 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700166}
167
168// int java.lang.Float.floatToRawIntBits(float)
169void IntrinsicLocationsBuilderMIPS64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
170 CreateFPToIntLocations(arena_, invoke);
171}
172
173void IntrinsicCodeGeneratorMIPS64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000174 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700175}
176
177static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
178 LocationSummary* locations = new (arena) LocationSummary(invoke,
179 LocationSummary::kNoCall,
180 kIntrinsified);
181 locations->SetInAt(0, Location::RequiresRegister());
182 locations->SetOut(Location::RequiresFpuRegister());
183}
184
185static void MoveIntToFP(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
186 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
187 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
188
189 if (is64bit) {
190 __ Dmtc1(in, out);
191 } else {
192 __ Mtc1(in, out);
193 }
194}
195
196// double java.lang.Double.longBitsToDouble(long)
197void IntrinsicLocationsBuilderMIPS64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
198 CreateIntToFPLocations(arena_, invoke);
199}
200
201void IntrinsicCodeGeneratorMIPS64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000202 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700203}
204
205// float java.lang.Float.intBitsToFloat(int)
206void IntrinsicLocationsBuilderMIPS64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
207 CreateIntToFPLocations(arena_, invoke);
208}
209
210void IntrinsicCodeGeneratorMIPS64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000211 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700212}
213
214static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
215 LocationSummary* locations = new (arena) LocationSummary(invoke,
216 LocationSummary::kNoCall,
217 kIntrinsified);
218 locations->SetInAt(0, Location::RequiresRegister());
219 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
220}
221
222static void GenReverseBytes(LocationSummary* locations,
223 Primitive::Type type,
224 Mips64Assembler* assembler) {
225 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
226 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
227
228 switch (type) {
229 case Primitive::kPrimShort:
230 __ Dsbh(out, in);
231 __ Seh(out, out);
232 break;
233 case Primitive::kPrimInt:
234 __ Rotr(out, in, 16);
235 __ Wsbh(out, out);
236 break;
237 case Primitive::kPrimLong:
238 __ Dsbh(out, in);
239 __ Dshd(out, out);
240 break;
241 default:
242 LOG(FATAL) << "Unexpected size for reverse-bytes: " << type;
243 UNREACHABLE();
244 }
245}
246
247// int java.lang.Integer.reverseBytes(int)
248void IntrinsicLocationsBuilderMIPS64::VisitIntegerReverseBytes(HInvoke* invoke) {
249 CreateIntToIntLocations(arena_, invoke);
250}
251
252void IntrinsicCodeGeneratorMIPS64::VisitIntegerReverseBytes(HInvoke* invoke) {
253 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
254}
255
256// long java.lang.Long.reverseBytes(long)
257void IntrinsicLocationsBuilderMIPS64::VisitLongReverseBytes(HInvoke* invoke) {
258 CreateIntToIntLocations(arena_, invoke);
259}
260
261void IntrinsicCodeGeneratorMIPS64::VisitLongReverseBytes(HInvoke* invoke) {
262 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
263}
264
265// short java.lang.Short.reverseBytes(short)
266void IntrinsicLocationsBuilderMIPS64::VisitShortReverseBytes(HInvoke* invoke) {
267 CreateIntToIntLocations(arena_, invoke);
268}
269
270void IntrinsicCodeGeneratorMIPS64::VisitShortReverseBytes(HInvoke* invoke) {
271 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
272}
273
Chris Larsen81284372015-10-21 15:28:53 -0700274static void GenNumberOfLeadingZeroes(LocationSummary* locations,
275 bool is64bit,
276 Mips64Assembler* assembler) {
Chris Larsen3039e382015-08-26 07:54:08 -0700277 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
278 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
279
280 if (is64bit) {
281 __ Dclz(out, in);
282 } else {
283 __ Clz(out, in);
284 }
285}
286
287// int java.lang.Integer.numberOfLeadingZeros(int i)
288void IntrinsicLocationsBuilderMIPS64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
289 CreateIntToIntLocations(arena_, invoke);
290}
291
292void IntrinsicCodeGeneratorMIPS64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000293 GenNumberOfLeadingZeroes(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700294}
295
296// int java.lang.Long.numberOfLeadingZeros(long i)
297void IntrinsicLocationsBuilderMIPS64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
298 CreateIntToIntLocations(arena_, invoke);
299}
300
301void IntrinsicCodeGeneratorMIPS64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000302 GenNumberOfLeadingZeroes(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen0646da72015-09-22 16:02:40 -0700303}
304
Chris Larsen81284372015-10-21 15:28:53 -0700305static void GenNumberOfTrailingZeroes(LocationSummary* locations,
306 bool is64bit,
307 Mips64Assembler* assembler) {
Chris Larsen0646da72015-09-22 16:02:40 -0700308 Location in = locations->InAt(0);
309 Location out = locations->Out();
310
311 if (is64bit) {
312 __ Dsbh(out.AsRegister<GpuRegister>(), in.AsRegister<GpuRegister>());
313 __ Dshd(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
314 __ Dbitswap(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
315 __ Dclz(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
316 } else {
317 __ Rotr(out.AsRegister<GpuRegister>(), in.AsRegister<GpuRegister>(), 16);
318 __ Wsbh(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
319 __ Bitswap(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
320 __ Clz(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
321 }
322}
323
324// int java.lang.Integer.numberOfTrailingZeros(int i)
325void IntrinsicLocationsBuilderMIPS64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
326 CreateIntToIntLocations(arena_, invoke);
327}
328
329void IntrinsicCodeGeneratorMIPS64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000330 GenNumberOfTrailingZeroes(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen0646da72015-09-22 16:02:40 -0700331}
332
333// int java.lang.Long.numberOfTrailingZeros(long i)
334void IntrinsicLocationsBuilderMIPS64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
335 CreateIntToIntLocations(arena_, invoke);
336}
337
338void IntrinsicCodeGeneratorMIPS64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000339 GenNumberOfTrailingZeroes(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700340}
341
Chris Larsen9aebff22015-09-22 17:54:15 -0700342static void GenRotateRight(HInvoke* invoke,
343 Primitive::Type type,
344 Mips64Assembler* assembler) {
345 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
346
347 LocationSummary* locations = invoke->GetLocations();
348 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
349 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
350
351 if (invoke->InputAt(1)->IsIntConstant()) {
352 uint32_t shift = static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue());
353 if (type == Primitive::kPrimInt) {
354 shift &= 0x1f;
355 __ Rotr(out, in, shift);
356 } else {
357 shift &= 0x3f;
358 if (shift < 32) {
359 __ Drotr(out, in, shift);
360 } else {
361 shift &= 0x1f;
362 __ Drotr32(out, in, shift);
363 }
364 }
365 } else {
366 GpuRegister shamt = locations->InAt(1).AsRegister<GpuRegister>();
367 if (type == Primitive::kPrimInt) {
368 __ Rotrv(out, in, shamt);
369 } else {
370 __ Drotrv(out, in, shamt);
371 }
372 }
373}
374
375// int java.lang.Integer.rotateRight(int i, int distance)
376void IntrinsicLocationsBuilderMIPS64::VisitIntegerRotateRight(HInvoke* invoke) {
377 LocationSummary* locations = new (arena_) LocationSummary(invoke,
378 LocationSummary::kNoCall,
379 kIntrinsified);
380 locations->SetInAt(0, Location::RequiresRegister());
381 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
383}
384
385void IntrinsicCodeGeneratorMIPS64::VisitIntegerRotateRight(HInvoke* invoke) {
386 GenRotateRight(invoke, Primitive::kPrimInt, GetAssembler());
387}
388
Chris Larsen81284372015-10-21 15:28:53 -0700389// long java.lang.Long.rotateRight(long i, int distance)
Chris Larsen9aebff22015-09-22 17:54:15 -0700390void IntrinsicLocationsBuilderMIPS64::VisitLongRotateRight(HInvoke* invoke) {
391 LocationSummary* locations = new (arena_) LocationSummary(invoke,
392 LocationSummary::kNoCall,
393 kIntrinsified);
394 locations->SetInAt(0, Location::RequiresRegister());
395 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
396 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
397}
398
399void IntrinsicCodeGeneratorMIPS64::VisitLongRotateRight(HInvoke* invoke) {
400 GenRotateRight(invoke, Primitive::kPrimLong, GetAssembler());
401}
402
Chris Larsen0f8f8642015-10-02 17:25:58 -0700403static void GenRotateLeft(HInvoke* invoke,
404 Primitive::Type type,
405 Mips64Assembler* assembler) {
406 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
407
408 LocationSummary* locations = invoke->GetLocations();
409 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
410 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
411
412 if (invoke->InputAt(1)->IsIntConstant()) {
413 int32_t shift = -static_cast<int32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue());
414 if (type == Primitive::kPrimInt) {
415 shift &= 0x1f;
416 __ Rotr(out, in, shift);
417 } else {
418 shift &= 0x3f;
419 if (shift < 32) {
420 __ Drotr(out, in, shift);
421 } else {
422 shift &= 0x1f;
423 __ Drotr32(out, in, shift);
424 }
425 }
426 } else {
427 GpuRegister shamt = locations->InAt(1).AsRegister<GpuRegister>();
428 if (type == Primitive::kPrimInt) {
429 __ Subu(TMP, ZERO, shamt);
430 __ Rotrv(out, in, TMP);
431 } else {
432 __ Dsubu(TMP, ZERO, shamt);
433 __ Drotrv(out, in, TMP);
434 }
435 }
436}
437
438// int java.lang.Integer.rotateLeft(int i, int distance)
439void IntrinsicLocationsBuilderMIPS64::VisitIntegerRotateLeft(HInvoke* invoke) {
440 LocationSummary* locations = new (arena_) LocationSummary(invoke,
441 LocationSummary::kNoCall,
442 kIntrinsified);
443 locations->SetInAt(0, Location::RequiresRegister());
444 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
445 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
446}
447
448void IntrinsicCodeGeneratorMIPS64::VisitIntegerRotateLeft(HInvoke* invoke) {
449 GenRotateLeft(invoke, Primitive::kPrimInt, GetAssembler());
450}
451
Chris Larsen81284372015-10-21 15:28:53 -0700452// long java.lang.Long.rotateLeft(long i, int distance)
Chris Larsen0f8f8642015-10-02 17:25:58 -0700453void IntrinsicLocationsBuilderMIPS64::VisitLongRotateLeft(HInvoke* invoke) {
454 LocationSummary* locations = new (arena_) LocationSummary(invoke,
455 LocationSummary::kNoCall,
456 kIntrinsified);
457 locations->SetInAt(0, Location::RequiresRegister());
458 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
459 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
460}
461
462void IntrinsicCodeGeneratorMIPS64::VisitLongRotateLeft(HInvoke* invoke) {
463 GenRotateLeft(invoke, Primitive::kPrimLong, GetAssembler());
464}
465
Chris Larsen3039e382015-08-26 07:54:08 -0700466static void GenReverse(LocationSummary* locations,
467 Primitive::Type type,
468 Mips64Assembler* assembler) {
469 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
470
471 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
472 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
473
474 if (type == Primitive::kPrimInt) {
475 __ Rotr(out, in, 16);
476 __ Wsbh(out, out);
477 __ Bitswap(out, out);
478 } else {
479 __ Dsbh(out, in);
480 __ Dshd(out, out);
481 __ Dbitswap(out, out);
482 }
483}
484
485// int java.lang.Integer.reverse(int)
486void IntrinsicLocationsBuilderMIPS64::VisitIntegerReverse(HInvoke* invoke) {
487 CreateIntToIntLocations(arena_, invoke);
488}
489
490void IntrinsicCodeGeneratorMIPS64::VisitIntegerReverse(HInvoke* invoke) {
491 GenReverse(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
492}
493
494// long java.lang.Long.reverse(long)
495void IntrinsicLocationsBuilderMIPS64::VisitLongReverse(HInvoke* invoke) {
496 CreateIntToIntLocations(arena_, invoke);
497}
498
499void IntrinsicCodeGeneratorMIPS64::VisitLongReverse(HInvoke* invoke) {
500 GenReverse(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
501}
502
Chris Larsen0b7ac982015-09-04 12:54:28 -0700503static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
504 LocationSummary* locations = new (arena) LocationSummary(invoke,
505 LocationSummary::kNoCall,
506 kIntrinsified);
507 locations->SetInAt(0, Location::RequiresFpuRegister());
508 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
509}
510
511static void MathAbsFP(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
512 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
513 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
514
515 if (is64bit) {
516 __ AbsD(out, in);
517 } else {
518 __ AbsS(out, in);
519 }
520}
521
522// double java.lang.Math.abs(double)
523void IntrinsicLocationsBuilderMIPS64::VisitMathAbsDouble(HInvoke* invoke) {
524 CreateFPToFPLocations(arena_, invoke);
525}
526
527void IntrinsicCodeGeneratorMIPS64::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000528 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700529}
530
531// float java.lang.Math.abs(float)
532void IntrinsicLocationsBuilderMIPS64::VisitMathAbsFloat(HInvoke* invoke) {
533 CreateFPToFPLocations(arena_, invoke);
534}
535
536void IntrinsicCodeGeneratorMIPS64::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000537 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700538}
539
540static void CreateIntToInt(ArenaAllocator* arena, HInvoke* invoke) {
541 LocationSummary* locations = new (arena) LocationSummary(invoke,
542 LocationSummary::kNoCall,
543 kIntrinsified);
544 locations->SetInAt(0, Location::RequiresRegister());
545 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
546}
547
548static void GenAbsInteger(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
549 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
550 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
551
552 if (is64bit) {
553 __ Dsra32(AT, in, 31);
554 __ Xor(out, in, AT);
555 __ Dsubu(out, out, AT);
556 } else {
557 __ Sra(AT, in, 31);
558 __ Xor(out, in, AT);
559 __ Subu(out, out, AT);
560 }
561}
562
563// int java.lang.Math.abs(int)
564void IntrinsicLocationsBuilderMIPS64::VisitMathAbsInt(HInvoke* invoke) {
565 CreateIntToInt(arena_, invoke);
566}
567
568void IntrinsicCodeGeneratorMIPS64::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000569 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700570}
571
572// long java.lang.Math.abs(long)
573void IntrinsicLocationsBuilderMIPS64::VisitMathAbsLong(HInvoke* invoke) {
574 CreateIntToInt(arena_, invoke);
575}
576
577void IntrinsicCodeGeneratorMIPS64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000578 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700579}
580
581static void GenMinMaxFP(LocationSummary* locations,
582 bool is_min,
583 bool is_double,
584 Mips64Assembler* assembler) {
585 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
586 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
587 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
588
589 if (is_double) {
590 if (is_min) {
591 __ MinD(out, lhs, rhs);
592 } else {
593 __ MaxD(out, lhs, rhs);
594 }
595 } else {
596 if (is_min) {
597 __ MinS(out, lhs, rhs);
598 } else {
599 __ MaxS(out, lhs, rhs);
600 }
601 }
602}
603
604static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
605 LocationSummary* locations = new (arena) LocationSummary(invoke,
606 LocationSummary::kNoCall,
607 kIntrinsified);
608 locations->SetInAt(0, Location::RequiresFpuRegister());
609 locations->SetInAt(1, Location::RequiresFpuRegister());
610 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
611}
612
613// double java.lang.Math.min(double, double)
614void IntrinsicLocationsBuilderMIPS64::VisitMathMinDoubleDouble(HInvoke* invoke) {
615 CreateFPFPToFPLocations(arena_, invoke);
616}
617
618void IntrinsicCodeGeneratorMIPS64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000619 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700620}
621
622// float java.lang.Math.min(float, float)
623void IntrinsicLocationsBuilderMIPS64::VisitMathMinFloatFloat(HInvoke* invoke) {
624 CreateFPFPToFPLocations(arena_, invoke);
625}
626
627void IntrinsicCodeGeneratorMIPS64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000628 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700629}
630
631// double java.lang.Math.max(double, double)
632void IntrinsicLocationsBuilderMIPS64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
633 CreateFPFPToFPLocations(arena_, invoke);
634}
635
636void IntrinsicCodeGeneratorMIPS64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000637 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700638}
639
640// float java.lang.Math.max(float, float)
641void IntrinsicLocationsBuilderMIPS64::VisitMathMaxFloatFloat(HInvoke* invoke) {
642 CreateFPFPToFPLocations(arena_, invoke);
643}
644
645void IntrinsicCodeGeneratorMIPS64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000646 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700647}
648
649static void GenMinMax(LocationSummary* locations,
650 bool is_min,
651 Mips64Assembler* assembler) {
652 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
653 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
654 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
655
Chris Larsen14500822015-10-01 11:35:18 -0700656 // Some architectures, such as ARM and MIPS (prior to r6), have a
657 // conditional move instruction which only changes the target
658 // (output) register if the condition is true (MIPS prior to r6 had
659 // MOVF, MOVT, and MOVZ). The SELEQZ and SELNEZ instructions always
660 // change the target (output) register. If the condition is true the
661 // output register gets the contents of the "rs" register; otherwise,
662 // the output register is set to zero. One consequence of this is
663 // that to implement something like "rd = c==0 ? rs : rt" MIPS64r6
664 // needs to use a pair of SELEQZ/SELNEZ instructions. After
665 // executing this pair of instructions one of the output registers
666 // from the pair will necessarily contain zero. Then the code ORs the
667 // output registers from the SELEQZ/SELNEZ instructions to get the
668 // final result.
669 //
670 // The initial test to see if the output register is same as the
671 // first input register is needed to make sure that value in the
672 // first input register isn't clobbered before we've finished
673 // computing the output value. The logic in the corresponding else
674 // clause performs the same task but makes sure the second input
675 // register isn't clobbered in the event that it's the same register
676 // as the output register; the else clause also handles the case
677 // where the output register is distinct from both the first, and the
678 // second input registers.
Chris Larsen0b7ac982015-09-04 12:54:28 -0700679 if (out == lhs) {
680 __ Slt(AT, rhs, lhs);
681 if (is_min) {
682 __ Seleqz(out, lhs, AT);
683 __ Selnez(AT, rhs, AT);
684 } else {
685 __ Selnez(out, lhs, AT);
686 __ Seleqz(AT, rhs, AT);
687 }
688 } else {
689 __ Slt(AT, lhs, rhs);
690 if (is_min) {
691 __ Seleqz(out, rhs, AT);
692 __ Selnez(AT, lhs, AT);
693 } else {
694 __ Selnez(out, rhs, AT);
695 __ Seleqz(AT, lhs, AT);
696 }
697 }
698 __ Or(out, out, AT);
699}
700
701static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
702 LocationSummary* locations = new (arena) LocationSummary(invoke,
703 LocationSummary::kNoCall,
704 kIntrinsified);
705 locations->SetInAt(0, Location::RequiresRegister());
706 locations->SetInAt(1, Location::RequiresRegister());
707 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
708}
709
710// int java.lang.Math.min(int, int)
711void IntrinsicLocationsBuilderMIPS64::VisitMathMinIntInt(HInvoke* invoke) {
712 CreateIntIntToIntLocations(arena_, invoke);
713}
714
715void IntrinsicCodeGeneratorMIPS64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000716 GenMinMax(invoke->GetLocations(), /* is_min */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700717}
718
719// long java.lang.Math.min(long, long)
720void IntrinsicLocationsBuilderMIPS64::VisitMathMinLongLong(HInvoke* invoke) {
721 CreateIntIntToIntLocations(arena_, invoke);
722}
723
724void IntrinsicCodeGeneratorMIPS64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000725 GenMinMax(invoke->GetLocations(), /* is_min */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700726}
727
728// int java.lang.Math.max(int, int)
729void IntrinsicLocationsBuilderMIPS64::VisitMathMaxIntInt(HInvoke* invoke) {
730 CreateIntIntToIntLocations(arena_, invoke);
731}
732
733void IntrinsicCodeGeneratorMIPS64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000734 GenMinMax(invoke->GetLocations(), /* is_min */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700735}
736
737// long java.lang.Math.max(long, long)
738void IntrinsicLocationsBuilderMIPS64::VisitMathMaxLongLong(HInvoke* invoke) {
739 CreateIntIntToIntLocations(arena_, invoke);
740}
741
742void IntrinsicCodeGeneratorMIPS64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000743 GenMinMax(invoke->GetLocations(), /* is_min */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700744}
745
746// double java.lang.Math.sqrt(double)
747void IntrinsicLocationsBuilderMIPS64::VisitMathSqrt(HInvoke* invoke) {
748 CreateFPToFPLocations(arena_, invoke);
749}
750
751void IntrinsicCodeGeneratorMIPS64::VisitMathSqrt(HInvoke* invoke) {
752 LocationSummary* locations = invoke->GetLocations();
753 Mips64Assembler* assembler = GetAssembler();
754 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
755 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
756
757 __ SqrtD(out, in);
758}
759
Chris Larsen81284372015-10-21 15:28:53 -0700760static void CreateFPToFP(ArenaAllocator* arena,
761 HInvoke* invoke,
762 Location::OutputOverlap overlaps = Location::kOutputOverlap) {
Chris Larsen0b7ac982015-09-04 12:54:28 -0700763 LocationSummary* locations = new (arena) LocationSummary(invoke,
764 LocationSummary::kNoCall,
765 kIntrinsified);
766 locations->SetInAt(0, Location::RequiresFpuRegister());
Chris Larsen81284372015-10-21 15:28:53 -0700767 locations->SetOut(Location::RequiresFpuRegister(), overlaps);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700768}
769
770// double java.lang.Math.rint(double)
771void IntrinsicLocationsBuilderMIPS64::VisitMathRint(HInvoke* invoke) {
Chris Larsen81284372015-10-21 15:28:53 -0700772 CreateFPToFP(arena_, invoke, Location::kNoOutputOverlap);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700773}
774
775void IntrinsicCodeGeneratorMIPS64::VisitMathRint(HInvoke* invoke) {
776 LocationSummary* locations = invoke->GetLocations();
777 Mips64Assembler* assembler = GetAssembler();
778 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
779 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
780
781 __ RintD(out, in);
782}
783
784// double java.lang.Math.floor(double)
785void IntrinsicLocationsBuilderMIPS64::VisitMathFloor(HInvoke* invoke) {
786 CreateFPToFP(arena_, invoke);
787}
788
Chris Larsen14500822015-10-01 11:35:18 -0700789const constexpr uint16_t kFPLeaveUnchanged = kPositiveZero |
790 kPositiveInfinity |
791 kNegativeZero |
792 kNegativeInfinity |
793 kQuietNaN |
794 kSignalingNaN;
Chris Larsen0b7ac982015-09-04 12:54:28 -0700795
Chris Larsen81284372015-10-21 15:28:53 -0700796enum FloatRoundingMode {
797 kFloor,
798 kCeil,
799};
800
801static void GenRoundingMode(LocationSummary* locations,
802 FloatRoundingMode mode,
803 Mips64Assembler* assembler) {
Chris Larsen0b7ac982015-09-04 12:54:28 -0700804 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
805 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
806
Chris Larsen81284372015-10-21 15:28:53 -0700807 DCHECK_NE(in, out);
808
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700809 Mips64Label done;
Chris Larsen0b7ac982015-09-04 12:54:28 -0700810
Chris Larsen81284372015-10-21 15:28:53 -0700811 // double floor/ceil(double in) {
Chris Larsen0b7ac982015-09-04 12:54:28 -0700812 // if in.isNaN || in.isInfinite || in.isZero {
813 // return in;
814 // }
815 __ ClassD(out, in);
816 __ Dmfc1(AT, out);
Chris Larsen14500822015-10-01 11:35:18 -0700817 __ Andi(AT, AT, kFPLeaveUnchanged); // +0.0 | +Inf | -0.0 | -Inf | qNaN | sNaN
Chris Larsen0b7ac982015-09-04 12:54:28 -0700818 __ MovD(out, in);
819 __ Bnezc(AT, &done);
820
Chris Larsen81284372015-10-21 15:28:53 -0700821 // Long outLong = floor/ceil(in);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700822 // if outLong == Long.MAX_VALUE {
Chris Larsen81284372015-10-21 15:28:53 -0700823 // // floor()/ceil() has almost certainly returned a value
824 // // which can't be successfully represented as a signed
825 // // 64-bit number. Java expects that the input value will
826 // // be returned in these cases.
827 // // There is also a small probability that floor(in)/ceil(in)
828 // // correctly truncates/rounds up the input value to
829 // // Long.MAX_VALUE. In that case, this exception handling
830 // // code still does the correct thing.
Chris Larsen0b7ac982015-09-04 12:54:28 -0700831 // return in;
832 // }
Chris Larsen81284372015-10-21 15:28:53 -0700833 if (mode == kFloor) {
834 __ FloorLD(out, in);
835 } else if (mode == kCeil) {
836 __ CeilLD(out, in);
837 }
Chris Larsen0b7ac982015-09-04 12:54:28 -0700838 __ Dmfc1(AT, out);
839 __ MovD(out, in);
840 __ LoadConst64(TMP, kPrimLongMax);
841 __ Beqc(AT, TMP, &done);
842
843 // double out = outLong;
844 // return out;
845 __ Dmtc1(AT, out);
846 __ Cvtdl(out, out);
847 __ Bind(&done);
848 // }
849}
850
Chris Larsen81284372015-10-21 15:28:53 -0700851void IntrinsicCodeGeneratorMIPS64::VisitMathFloor(HInvoke* invoke) {
852 GenRoundingMode(invoke->GetLocations(), kFloor, GetAssembler());
853}
854
Chris Larsen0b7ac982015-09-04 12:54:28 -0700855// double java.lang.Math.ceil(double)
856void IntrinsicLocationsBuilderMIPS64::VisitMathCeil(HInvoke* invoke) {
857 CreateFPToFP(arena_, invoke);
858}
859
860void IntrinsicCodeGeneratorMIPS64::VisitMathCeil(HInvoke* invoke) {
Chris Larsen81284372015-10-21 15:28:53 -0700861 GenRoundingMode(invoke->GetLocations(), kCeil, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700862}
863
Chris Larsen70fb1f42015-09-04 10:15:27 -0700864// byte libcore.io.Memory.peekByte(long address)
865void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekByte(HInvoke* invoke) {
866 CreateIntToIntLocations(arena_, invoke);
867}
868
869void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekByte(HInvoke* invoke) {
870 Mips64Assembler* assembler = GetAssembler();
871 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
872 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
873
874 __ Lb(out, adr, 0);
875}
876
877// short libcore.io.Memory.peekShort(long address)
878void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekShortNative(HInvoke* invoke) {
879 CreateIntToIntLocations(arena_, invoke);
880}
881
882void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekShortNative(HInvoke* invoke) {
883 Mips64Assembler* assembler = GetAssembler();
884 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
885 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
886
887 __ Lh(out, adr, 0);
888}
889
890// int libcore.io.Memory.peekInt(long address)
891void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekIntNative(HInvoke* invoke) {
892 CreateIntToIntLocations(arena_, invoke);
893}
894
895void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekIntNative(HInvoke* invoke) {
896 Mips64Assembler* assembler = GetAssembler();
897 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
898 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
899
900 __ Lw(out, adr, 0);
901}
902
903// long libcore.io.Memory.peekLong(long address)
904void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekLongNative(HInvoke* invoke) {
905 CreateIntToIntLocations(arena_, invoke);
906}
907
908void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekLongNative(HInvoke* invoke) {
909 Mips64Assembler* assembler = GetAssembler();
910 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
911 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
912
913 __ Ld(out, adr, 0);
914}
915
916static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
917 LocationSummary* locations = new (arena) LocationSummary(invoke,
918 LocationSummary::kNoCall,
919 kIntrinsified);
920 locations->SetInAt(0, Location::RequiresRegister());
921 locations->SetInAt(1, Location::RequiresRegister());
922}
923
924// void libcore.io.Memory.pokeByte(long address, byte value)
925void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeByte(HInvoke* invoke) {
926 CreateIntIntToVoidLocations(arena_, invoke);
927}
928
929void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeByte(HInvoke* invoke) {
930 Mips64Assembler* assembler = GetAssembler();
931 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
932 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
933
934 __ Sb(val, adr, 0);
935}
936
937// void libcore.io.Memory.pokeShort(long address, short value)
938void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeShortNative(HInvoke* invoke) {
939 CreateIntIntToVoidLocations(arena_, invoke);
940}
941
942void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeShortNative(HInvoke* invoke) {
943 Mips64Assembler* assembler = GetAssembler();
944 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
945 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
946
947 __ Sh(val, adr, 0);
948}
949
950// void libcore.io.Memory.pokeInt(long address, int value)
951void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeIntNative(HInvoke* invoke) {
952 CreateIntIntToVoidLocations(arena_, invoke);
953}
954
955void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeIntNative(HInvoke* invoke) {
956 Mips64Assembler* assembler = GetAssembler();
957 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
958 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
959
960 __ Sw(val, adr, 00);
961}
962
963// void libcore.io.Memory.pokeLong(long address, long value)
964void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeLongNative(HInvoke* invoke) {
965 CreateIntIntToVoidLocations(arena_, invoke);
966}
967
968void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeLongNative(HInvoke* invoke) {
969 Mips64Assembler* assembler = GetAssembler();
970 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
971 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
972
973 __ Sd(val, adr, 0);
974}
975
Chris Larsen49e55392015-09-04 16:04:03 -0700976// Thread java.lang.Thread.currentThread()
977void IntrinsicLocationsBuilderMIPS64::VisitThreadCurrentThread(HInvoke* invoke) {
978 LocationSummary* locations = new (arena_) LocationSummary(invoke,
979 LocationSummary::kNoCall,
980 kIntrinsified);
981 locations->SetOut(Location::RequiresRegister());
982}
983
984void IntrinsicCodeGeneratorMIPS64::VisitThreadCurrentThread(HInvoke* invoke) {
985 Mips64Assembler* assembler = GetAssembler();
986 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
987
988 __ LoadFromOffset(kLoadUnsignedWord,
989 out,
990 TR,
991 Thread::PeerOffset<kMips64PointerSize>().Int32Value());
992}
993
Chris Larsen1360ada2015-09-04 23:38:16 -0700994static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
995 LocationSummary* locations = new (arena) LocationSummary(invoke,
996 LocationSummary::kNoCall,
997 kIntrinsified);
998 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
999 locations->SetInAt(1, Location::RequiresRegister());
1000 locations->SetInAt(2, Location::RequiresRegister());
1001 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1002}
1003
1004static void GenUnsafeGet(HInvoke* invoke,
1005 Primitive::Type type,
1006 bool is_volatile,
1007 CodeGeneratorMIPS64* codegen) {
1008 LocationSummary* locations = invoke->GetLocations();
1009 DCHECK((type == Primitive::kPrimInt) ||
1010 (type == Primitive::kPrimLong) ||
1011 (type == Primitive::kPrimNot));
1012 Mips64Assembler* assembler = codegen->GetAssembler();
1013 // Object pointer.
1014 GpuRegister base = locations->InAt(1).AsRegister<GpuRegister>();
1015 // Long offset.
1016 GpuRegister offset = locations->InAt(2).AsRegister<GpuRegister>();
1017 GpuRegister trg = locations->Out().AsRegister<GpuRegister>();
1018
1019 __ Daddu(TMP, base, offset);
1020 if (is_volatile) {
1021 __ Sync(0);
1022 }
1023 switch (type) {
1024 case Primitive::kPrimInt:
1025 __ Lw(trg, TMP, 0);
1026 break;
1027
1028 case Primitive::kPrimNot:
1029 __ Lwu(trg, TMP, 0);
1030 break;
1031
1032 case Primitive::kPrimLong:
1033 __ Ld(trg, TMP, 0);
1034 break;
1035
1036 default:
1037 LOG(FATAL) << "Unsupported op size " << type;
1038 UNREACHABLE();
1039 }
1040}
1041
1042// int sun.misc.Unsafe.getInt(Object o, long offset)
1043void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGet(HInvoke* invoke) {
1044 CreateIntIntIntToIntLocations(arena_, invoke);
1045}
1046
1047void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001048 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001049}
1050
1051// int sun.misc.Unsafe.getIntVolatile(Object o, long offset)
1052void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetVolatile(HInvoke* invoke) {
1053 CreateIntIntIntToIntLocations(arena_, invoke);
1054}
1055
1056void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001057 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001058}
1059
1060// long sun.misc.Unsafe.getLong(Object o, long offset)
1061void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetLong(HInvoke* invoke) {
1062 CreateIntIntIntToIntLocations(arena_, invoke);
1063}
1064
1065void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001066 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001067}
1068
1069// long sun.misc.Unsafe.getLongVolatile(Object o, long offset)
1070void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1071 CreateIntIntIntToIntLocations(arena_, invoke);
1072}
1073
1074void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001075 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001076}
1077
1078// Object sun.misc.Unsafe.getObject(Object o, long offset)
1079void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetObject(HInvoke* invoke) {
1080 CreateIntIntIntToIntLocations(arena_, invoke);
1081}
1082
1083void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001084 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001085}
1086
1087// Object sun.misc.Unsafe.getObjectVolatile(Object o, long offset)
1088void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1089 CreateIntIntIntToIntLocations(arena_, invoke);
1090}
1091
1092void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001093 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001094}
1095
1096static void CreateIntIntIntIntToVoid(ArenaAllocator* arena, HInvoke* invoke) {
1097 LocationSummary* locations = new (arena) LocationSummary(invoke,
1098 LocationSummary::kNoCall,
1099 kIntrinsified);
1100 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1101 locations->SetInAt(1, Location::RequiresRegister());
1102 locations->SetInAt(2, Location::RequiresRegister());
1103 locations->SetInAt(3, Location::RequiresRegister());
1104}
1105
1106static void GenUnsafePut(LocationSummary* locations,
1107 Primitive::Type type,
1108 bool is_volatile,
1109 bool is_ordered,
1110 CodeGeneratorMIPS64* codegen) {
1111 DCHECK((type == Primitive::kPrimInt) ||
1112 (type == Primitive::kPrimLong) ||
1113 (type == Primitive::kPrimNot));
1114 Mips64Assembler* assembler = codegen->GetAssembler();
1115 // Object pointer.
1116 GpuRegister base = locations->InAt(1).AsRegister<GpuRegister>();
1117 // Long offset.
1118 GpuRegister offset = locations->InAt(2).AsRegister<GpuRegister>();
1119 GpuRegister value = locations->InAt(3).AsRegister<GpuRegister>();
1120
1121 __ Daddu(TMP, base, offset);
1122 if (is_volatile || is_ordered) {
1123 __ Sync(0);
1124 }
1125 switch (type) {
1126 case Primitive::kPrimInt:
1127 case Primitive::kPrimNot:
1128 __ Sw(value, TMP, 0);
1129 break;
1130
1131 case Primitive::kPrimLong:
1132 __ Sd(value, TMP, 0);
1133 break;
1134
1135 default:
1136 LOG(FATAL) << "Unsupported op size " << type;
1137 UNREACHABLE();
1138 }
1139 if (is_volatile) {
1140 __ Sync(0);
1141 }
1142
1143 if (type == Primitive::kPrimNot) {
1144 codegen->MarkGCCard(base, value);
1145 }
1146}
1147
1148// void sun.misc.Unsafe.putInt(Object o, long offset, int x)
1149void IntrinsicLocationsBuilderMIPS64::VisitUnsafePut(HInvoke* invoke) {
1150 CreateIntIntIntIntToVoid(arena_, invoke);
1151}
1152
1153void IntrinsicCodeGeneratorMIPS64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001154 GenUnsafePut(invoke->GetLocations(),
1155 Primitive::kPrimInt,
1156 /* is_volatile */ false,
1157 /* is_ordered */ false,
1158 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001159}
1160
1161// void sun.misc.Unsafe.putOrderedInt(Object o, long offset, int x)
1162void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutOrdered(HInvoke* invoke) {
1163 CreateIntIntIntIntToVoid(arena_, invoke);
1164}
1165
1166void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001167 GenUnsafePut(invoke->GetLocations(),
1168 Primitive::kPrimInt,
1169 /* is_volatile */ false,
1170 /* is_ordered */ true,
1171 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001172}
1173
1174// void sun.misc.Unsafe.putIntVolatile(Object o, long offset, int x)
1175void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutVolatile(HInvoke* invoke) {
1176 CreateIntIntIntIntToVoid(arena_, invoke);
1177}
1178
1179void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001180 GenUnsafePut(invoke->GetLocations(),
1181 Primitive::kPrimInt,
1182 /* is_volatile */ true,
1183 /* is_ordered */ false,
1184 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001185}
1186
1187// void sun.misc.Unsafe.putObject(Object o, long offset, Object x)
1188void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutObject(HInvoke* invoke) {
1189 CreateIntIntIntIntToVoid(arena_, invoke);
1190}
1191
1192void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001193 GenUnsafePut(invoke->GetLocations(),
1194 Primitive::kPrimNot,
1195 /* is_volatile */ false,
1196 /* is_ordered */ false,
1197 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001198}
1199
1200// void sun.misc.Unsafe.putOrderedObject(Object o, long offset, Object x)
1201void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1202 CreateIntIntIntIntToVoid(arena_, invoke);
1203}
1204
1205void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001206 GenUnsafePut(invoke->GetLocations(),
1207 Primitive::kPrimNot,
1208 /* is_volatile */ false,
1209 /* is_ordered */ true,
1210 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001211}
1212
1213// void sun.misc.Unsafe.putObjectVolatile(Object o, long offset, Object x)
1214void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1215 CreateIntIntIntIntToVoid(arena_, invoke);
1216}
1217
1218void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001219 GenUnsafePut(invoke->GetLocations(),
1220 Primitive::kPrimNot,
1221 /* is_volatile */ true,
1222 /* is_ordered */ false,
1223 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001224}
1225
1226// void sun.misc.Unsafe.putLong(Object o, long offset, long x)
1227void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutLong(HInvoke* invoke) {
1228 CreateIntIntIntIntToVoid(arena_, invoke);
1229}
1230
1231void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001232 GenUnsafePut(invoke->GetLocations(),
1233 Primitive::kPrimLong,
1234 /* is_volatile */ false,
1235 /* is_ordered */ false,
1236 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001237}
1238
1239// void sun.misc.Unsafe.putOrderedLong(Object o, long offset, long x)
1240void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1241 CreateIntIntIntIntToVoid(arena_, invoke);
1242}
1243
1244void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001245 GenUnsafePut(invoke->GetLocations(),
1246 Primitive::kPrimLong,
1247 /* is_volatile */ false,
1248 /* is_ordered */ true,
1249 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001250}
1251
1252// void sun.misc.Unsafe.putLongVolatile(Object o, long offset, long x)
1253void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1254 CreateIntIntIntIntToVoid(arena_, invoke);
1255}
1256
1257void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001258 GenUnsafePut(invoke->GetLocations(),
1259 Primitive::kPrimLong,
1260 /* is_volatile */ true,
1261 /* is_ordered */ false,
1262 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001263}
1264
Chris Larsen36427492015-10-23 02:19:38 -07001265static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, HInvoke* invoke) {
1266 LocationSummary* locations = new (arena) LocationSummary(invoke,
1267 LocationSummary::kNoCall,
1268 kIntrinsified);
1269 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1270 locations->SetInAt(1, Location::RequiresRegister());
1271 locations->SetInAt(2, Location::RequiresRegister());
1272 locations->SetInAt(3, Location::RequiresRegister());
1273 locations->SetInAt(4, Location::RequiresRegister());
1274
1275 locations->SetOut(Location::RequiresRegister());
1276}
1277
1278static void GenCas(LocationSummary* locations, Primitive::Type type, CodeGeneratorMIPS64* codegen) {
1279 Mips64Assembler* assembler = codegen->GetAssembler();
1280 GpuRegister base = locations->InAt(1).AsRegister<GpuRegister>();
1281 GpuRegister offset = locations->InAt(2).AsRegister<GpuRegister>();
1282 GpuRegister expected = locations->InAt(3).AsRegister<GpuRegister>();
1283 GpuRegister value = locations->InAt(4).AsRegister<GpuRegister>();
1284 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1285
1286 DCHECK_NE(base, out);
1287 DCHECK_NE(offset, out);
1288 DCHECK_NE(expected, out);
1289
1290 // do {
1291 // tmp_value = [tmp_ptr] - expected;
1292 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1293 // result = tmp_value != 0;
1294
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001295 Mips64Label loop_head, exit_loop;
Chris Larsen36427492015-10-23 02:19:38 -07001296 __ Daddu(TMP, base, offset);
1297 __ Sync(0);
1298 __ Bind(&loop_head);
1299 if (type == Primitive::kPrimLong) {
1300 __ Lld(out, TMP);
1301 } else {
Roland Levillain391b8662015-12-18 11:43:38 +00001302 // Note: We will need a read barrier here, when read barrier
1303 // support is added to the MIPS64 back end.
Chris Larsen36427492015-10-23 02:19:38 -07001304 __ Ll(out, TMP);
1305 }
1306 __ Dsubu(out, out, expected); // If we didn't get the 'expected'
1307 __ Sltiu(out, out, 1); // value, set 'out' to false, and
1308 __ Beqzc(out, &exit_loop); // return.
1309 __ Move(out, value); // Use 'out' for the 'store conditional' instruction.
1310 // If we use 'value' directly, we would lose 'value'
1311 // in the case that the store fails. Whether the
1312 // store succeeds, or fails, it will load the
1313 // correct boolean value into the 'out' register.
1314 if (type == Primitive::kPrimLong) {
1315 __ Scd(out, TMP);
1316 } else {
1317 __ Sc(out, TMP);
1318 }
1319 __ Beqzc(out, &loop_head); // If we couldn't do the read-modify-write
1320 // cycle atomically then retry.
1321 __ Bind(&exit_loop);
1322 __ Sync(0);
1323}
1324
1325// boolean sun.misc.Unsafe.compareAndSwapInt(Object o, long offset, int expected, int x)
1326void IntrinsicLocationsBuilderMIPS64::VisitUnsafeCASInt(HInvoke* invoke) {
1327 CreateIntIntIntIntIntToInt(arena_, invoke);
1328}
1329
1330void IntrinsicCodeGeneratorMIPS64::VisitUnsafeCASInt(HInvoke* invoke) {
1331 GenCas(invoke->GetLocations(), Primitive::kPrimInt, codegen_);
1332}
1333
1334// boolean sun.misc.Unsafe.compareAndSwapLong(Object o, long offset, long expected, long x)
1335void IntrinsicLocationsBuilderMIPS64::VisitUnsafeCASLong(HInvoke* invoke) {
1336 CreateIntIntIntIntIntToInt(arena_, invoke);
1337}
1338
1339void IntrinsicCodeGeneratorMIPS64::VisitUnsafeCASLong(HInvoke* invoke) {
1340 GenCas(invoke->GetLocations(), Primitive::kPrimLong, codegen_);
1341}
1342
1343// boolean sun.misc.Unsafe.compareAndSwapObject(Object o, long offset, Object expected, Object x)
1344void IntrinsicLocationsBuilderMIPS64::VisitUnsafeCASObject(HInvoke* invoke) {
1345 CreateIntIntIntIntIntToInt(arena_, invoke);
1346}
1347
1348void IntrinsicCodeGeneratorMIPS64::VisitUnsafeCASObject(HInvoke* invoke) {
1349 GenCas(invoke->GetLocations(), Primitive::kPrimNot, codegen_);
1350}
1351
Chris Larsen9701c2e2015-09-04 17:22:47 -07001352// char java.lang.String.charAt(int index)
1353void IntrinsicLocationsBuilderMIPS64::VisitStringCharAt(HInvoke* invoke) {
1354 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1355 LocationSummary::kCallOnSlowPath,
1356 kIntrinsified);
1357 locations->SetInAt(0, Location::RequiresRegister());
1358 locations->SetInAt(1, Location::RequiresRegister());
1359 locations->SetOut(Location::SameAsFirstInput());
1360}
1361
1362void IntrinsicCodeGeneratorMIPS64::VisitStringCharAt(HInvoke* invoke) {
1363 LocationSummary* locations = invoke->GetLocations();
1364 Mips64Assembler* assembler = GetAssembler();
1365
1366 // Location of reference to data array
1367 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1368 // Location of count
1369 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1370
1371 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1372 GpuRegister idx = locations->InAt(1).AsRegister<GpuRegister>();
1373 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1374
1375 // TODO: Maybe we can support range check elimination. Overall,
1376 // though, I think it's not worth the cost.
1377 // TODO: For simplicity, the index parameter is requested in a
1378 // register, so different from Quick we will not optimize the
1379 // code for constants (which would save a register).
1380
1381 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1382 codegen_->AddSlowPath(slow_path);
1383
1384 // Load the string size
1385 __ Lw(TMP, obj, count_offset);
1386 codegen_->MaybeRecordImplicitNullCheck(invoke);
1387 // Revert to slow path if idx is too large, or negative
1388 __ Bgeuc(idx, TMP, slow_path->GetEntryLabel());
1389
1390 // out = obj[2*idx].
1391 __ Sll(TMP, idx, 1); // idx * 2
1392 __ Daddu(TMP, TMP, obj); // Address of char at location idx
1393 __ Lhu(out, TMP, value_offset); // Load char at location idx
1394
1395 __ Bind(slow_path->GetExitLabel());
1396}
1397
1398// int java.lang.String.compareTo(String anotherString)
1399void IntrinsicLocationsBuilderMIPS64::VisitStringCompareTo(HInvoke* invoke) {
1400 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1401 LocationSummary::kCall,
1402 kIntrinsified);
1403 InvokeRuntimeCallingConvention calling_convention;
1404 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1405 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1406 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1407 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1408}
1409
1410void IntrinsicCodeGeneratorMIPS64::VisitStringCompareTo(HInvoke* invoke) {
1411 Mips64Assembler* assembler = GetAssembler();
1412 LocationSummary* locations = invoke->GetLocations();
1413
1414 // Note that the null check must have been done earlier.
1415 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1416
1417 GpuRegister argument = locations->InAt(1).AsRegister<GpuRegister>();
1418 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1419 codegen_->AddSlowPath(slow_path);
1420 __ Beqzc(argument, slow_path->GetEntryLabel());
1421
1422 __ LoadFromOffset(kLoadDoubleword,
1423 TMP,
1424 TR,
1425 QUICK_ENTRYPOINT_OFFSET(kMips64WordSize,
1426 pStringCompareTo).Int32Value());
1427 __ Jalr(TMP);
1428 __ Nop();
1429 __ Bind(slow_path->GetExitLabel());
1430}
1431
Chris Larsen972d6d72015-10-20 11:29:12 -07001432// boolean java.lang.String.equals(Object anObject)
1433void IntrinsicLocationsBuilderMIPS64::VisitStringEquals(HInvoke* invoke) {
1434 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1435 LocationSummary::kNoCall,
1436 kIntrinsified);
1437 locations->SetInAt(0, Location::RequiresRegister());
1438 locations->SetInAt(1, Location::RequiresRegister());
1439 locations->SetOut(Location::RequiresRegister());
1440
1441 // Temporary registers to store lengths of strings and for calculations.
1442 locations->AddTemp(Location::RequiresRegister());
1443 locations->AddTemp(Location::RequiresRegister());
1444 locations->AddTemp(Location::RequiresRegister());
1445}
1446
1447void IntrinsicCodeGeneratorMIPS64::VisitStringEquals(HInvoke* invoke) {
1448 Mips64Assembler* assembler = GetAssembler();
1449 LocationSummary* locations = invoke->GetLocations();
1450
1451 GpuRegister str = locations->InAt(0).AsRegister<GpuRegister>();
1452 GpuRegister arg = locations->InAt(1).AsRegister<GpuRegister>();
1453 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1454
1455 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
1456 GpuRegister temp2 = locations->GetTemp(1).AsRegister<GpuRegister>();
1457 GpuRegister temp3 = locations->GetTemp(2).AsRegister<GpuRegister>();
1458
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001459 Mips64Label loop;
1460 Mips64Label end;
1461 Mips64Label return_true;
1462 Mips64Label return_false;
Chris Larsen972d6d72015-10-20 11:29:12 -07001463
1464 // Get offsets of count, value, and class fields within a string object.
1465 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1466 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1467 const int32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1468
1469 // Note that the null check must have been done earlier.
1470 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1471
1472 // If the register containing the pointer to "this", and the register
1473 // containing the pointer to "anObject" are the same register then
1474 // "this", and "anObject" are the same object and we can
1475 // short-circuit the logic to a true result.
1476 if (str == arg) {
1477 __ LoadConst64(out, 1);
1478 return;
1479 }
1480
1481 // Check if input is null, return false if it is.
1482 __ Beqzc(arg, &return_false);
1483
1484 // Reference equality check, return true if same reference.
1485 __ Beqc(str, arg, &return_true);
1486
1487 // Instanceof check for the argument by comparing class fields.
1488 // All string objects must have the same type since String cannot be subclassed.
1489 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1490 // If the argument is a string object, its class field must be equal to receiver's class field.
1491 __ Lw(temp1, str, class_offset);
1492 __ Lw(temp2, arg, class_offset);
1493 __ Bnec(temp1, temp2, &return_false);
1494
1495 // Load lengths of this and argument strings.
1496 __ Lw(temp1, str, count_offset);
1497 __ Lw(temp2, arg, count_offset);
1498 // Check if lengths are equal, return false if they're not.
1499 __ Bnec(temp1, temp2, &return_false);
1500 // Return true if both strings are empty.
1501 __ Beqzc(temp1, &return_true);
1502
1503 // Don't overwrite input registers
1504 __ Move(TMP, str);
1505 __ Move(temp3, arg);
1506
1507 // Assertions that must hold in order to compare strings 4 characters at a time.
1508 DCHECK_ALIGNED(value_offset, 8);
1509 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1510
1511 // Loop to compare strings 4 characters at a time starting at the beginning of the string.
1512 // Ok to do this because strings are zero-padded to be 8-byte aligned.
1513 __ Bind(&loop);
1514 __ Ld(out, TMP, value_offset);
1515 __ Ld(temp2, temp3, value_offset);
1516 __ Bnec(out, temp2, &return_false);
1517 __ Daddiu(TMP, TMP, 8);
1518 __ Daddiu(temp3, temp3, 8);
1519 __ Addiu(temp1, temp1, -4);
1520 __ Bgtzc(temp1, &loop);
1521
1522 // Return true and exit the function.
1523 // If loop does not result in returning false, we return true.
1524 __ Bind(&return_true);
1525 __ LoadConst64(out, 1);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001526 __ Bc(&end);
Chris Larsen972d6d72015-10-20 11:29:12 -07001527
1528 // Return false and exit the function.
1529 __ Bind(&return_false);
1530 __ LoadConst64(out, 0);
1531 __ Bind(&end);
1532}
1533
Chris Larsen9701c2e2015-09-04 17:22:47 -07001534static void GenerateStringIndexOf(HInvoke* invoke,
1535 Mips64Assembler* assembler,
1536 CodeGeneratorMIPS64* codegen,
1537 ArenaAllocator* allocator,
1538 bool start_at_zero) {
1539 LocationSummary* locations = invoke->GetLocations();
1540 GpuRegister tmp_reg = start_at_zero ? locations->GetTemp(0).AsRegister<GpuRegister>() : TMP;
1541
1542 // Note that the null check must have been done earlier.
1543 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1544
1545 // Check for code points > 0xFFFF. Either a slow-path check when we
1546 // don't know statically, or directly dispatch if we have a constant.
1547 SlowPathCodeMIPS64* slow_path = nullptr;
1548 if (invoke->InputAt(1)->IsIntConstant()) {
1549 if (!IsUint<16>(invoke->InputAt(1)->AsIntConstant()->GetValue())) {
1550 // Always needs the slow-path. We could directly dispatch to it,
1551 // but this case should be rare, so for simplicity just put the
1552 // full slow-path down and branch unconditionally.
1553 slow_path = new (allocator) IntrinsicSlowPathMIPS64(invoke);
1554 codegen->AddSlowPath(slow_path);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001555 __ Bc(slow_path->GetEntryLabel());
Chris Larsen9701c2e2015-09-04 17:22:47 -07001556 __ Bind(slow_path->GetExitLabel());
1557 return;
1558 }
1559 } else {
1560 GpuRegister char_reg = locations->InAt(1).AsRegister<GpuRegister>();
1561 __ LoadConst32(tmp_reg, std::numeric_limits<uint16_t>::max());
1562 slow_path = new (allocator) IntrinsicSlowPathMIPS64(invoke);
1563 codegen->AddSlowPath(slow_path);
1564 __ Bltuc(tmp_reg, char_reg, slow_path->GetEntryLabel()); // UTF-16 required
1565 }
1566
1567 if (start_at_zero) {
1568 DCHECK_EQ(tmp_reg, A2);
1569 // Start-index = 0.
1570 __ Clear(tmp_reg);
1571 } else {
1572 __ Slt(TMP, A2, ZERO); // if fromIndex < 0
1573 __ Seleqz(A2, A2, TMP); // fromIndex = 0
1574 }
1575
1576 __ LoadFromOffset(kLoadDoubleword,
1577 TMP,
1578 TR,
1579 QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, pIndexOf).Int32Value());
1580 __ Jalr(TMP);
1581 __ Nop();
1582
1583 if (slow_path != nullptr) {
1584 __ Bind(slow_path->GetExitLabel());
1585 }
1586}
1587
1588// int java.lang.String.indexOf(int ch)
1589void IntrinsicLocationsBuilderMIPS64::VisitStringIndexOf(HInvoke* invoke) {
1590 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1591 LocationSummary::kCall,
1592 kIntrinsified);
1593 // We have a hand-crafted assembly stub that follows the runtime
1594 // calling convention. So it's best to align the inputs accordingly.
1595 InvokeRuntimeCallingConvention calling_convention;
1596 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1597 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1598 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1599 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1600
1601 // Need a temp for slow-path codepoint compare, and need to send start-index=0.
1602 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1603}
1604
1605void IntrinsicCodeGeneratorMIPS64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001606 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Chris Larsen9701c2e2015-09-04 17:22:47 -07001607}
1608
1609// int java.lang.String.indexOf(int ch, int fromIndex)
1610void IntrinsicLocationsBuilderMIPS64::VisitStringIndexOfAfter(HInvoke* invoke) {
1611 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1612 LocationSummary::kCall,
1613 kIntrinsified);
1614 // We have a hand-crafted assembly stub that follows the runtime
1615 // calling convention. So it's best to align the inputs accordingly.
1616 InvokeRuntimeCallingConvention calling_convention;
1617 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1618 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1619 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1620 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1621 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1622}
1623
1624void IntrinsicCodeGeneratorMIPS64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001625 GenerateStringIndexOf(
1626 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Chris Larsen9701c2e2015-09-04 17:22:47 -07001627}
1628
1629// java.lang.String.String(byte[] bytes)
1630void IntrinsicLocationsBuilderMIPS64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1631 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1632 LocationSummary::kCall,
1633 kIntrinsified);
1634 InvokeRuntimeCallingConvention calling_convention;
1635 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1636 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1637 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1638 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1639 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1640 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1641}
1642
1643void IntrinsicCodeGeneratorMIPS64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1644 Mips64Assembler* assembler = GetAssembler();
1645 LocationSummary* locations = invoke->GetLocations();
1646
1647 GpuRegister byte_array = locations->InAt(0).AsRegister<GpuRegister>();
1648 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1649 codegen_->AddSlowPath(slow_path);
1650 __ Beqzc(byte_array, slow_path->GetEntryLabel());
1651
1652 __ LoadFromOffset(kLoadDoubleword,
1653 TMP,
1654 TR,
1655 QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, pAllocStringFromBytes).Int32Value());
1656 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1657 __ Jalr(TMP);
1658 __ Nop();
1659 __ Bind(slow_path->GetExitLabel());
1660}
1661
1662// java.lang.String.String(char[] value)
1663void IntrinsicLocationsBuilderMIPS64::VisitStringNewStringFromChars(HInvoke* invoke) {
1664 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1665 LocationSummary::kCall,
1666 kIntrinsified);
1667 InvokeRuntimeCallingConvention calling_convention;
1668 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1669 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1670 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1671 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1672 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1673}
1674
1675void IntrinsicCodeGeneratorMIPS64::VisitStringNewStringFromChars(HInvoke* invoke) {
1676 Mips64Assembler* assembler = GetAssembler();
1677
1678 __ LoadFromOffset(kLoadDoubleword,
1679 TMP,
1680 TR,
1681 QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, pAllocStringFromChars).Int32Value());
1682 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1683 __ Jalr(TMP);
1684 __ Nop();
1685}
1686
1687// java.lang.String.String(String original)
1688void IntrinsicLocationsBuilderMIPS64::VisitStringNewStringFromString(HInvoke* invoke) {
1689 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1690 LocationSummary::kCall,
1691 kIntrinsified);
1692 InvokeRuntimeCallingConvention calling_convention;
1693 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1694 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1695 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1696 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1697 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1698}
1699
1700void IntrinsicCodeGeneratorMIPS64::VisitStringNewStringFromString(HInvoke* invoke) {
1701 Mips64Assembler* assembler = GetAssembler();
1702 LocationSummary* locations = invoke->GetLocations();
1703
1704 GpuRegister string_to_copy = locations->InAt(0).AsRegister<GpuRegister>();
1705 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1706 codegen_->AddSlowPath(slow_path);
1707 __ Beqzc(string_to_copy, slow_path->GetEntryLabel());
1708
1709 __ LoadFromOffset(kLoadDoubleword,
1710 TMP,
1711 TR,
1712 QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, pAllocStringFromString).Int32Value());
1713 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1714 __ Jalr(TMP);
1715 __ Nop();
1716 __ Bind(slow_path->GetExitLabel());
1717}
1718
Chris Larsen3039e382015-08-26 07:54:08 -07001719// Unimplemented intrinsics.
1720
1721#define UNIMPLEMENTED_INTRINSIC(Name) \
1722void IntrinsicLocationsBuilderMIPS64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1723} \
1724void IntrinsicCodeGeneratorMIPS64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1725}
1726
Chris Larsen3039e382015-08-26 07:54:08 -07001727UNIMPLEMENTED_INTRINSIC(MathRoundDouble)
1728UNIMPLEMENTED_INTRINSIC(MathRoundFloat)
Chris Larsen0b7ac982015-09-04 12:54:28 -07001729
Chris Larsen3039e382015-08-26 07:54:08 -07001730UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
1731UNIMPLEMENTED_INTRINSIC(StringGetCharsNoCheck)
1732UNIMPLEMENTED_INTRINSIC(SystemArrayCopyChar)
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001733UNIMPLEMENTED_INTRINSIC(SystemArrayCopy)
Chris Larsen3039e382015-08-26 07:54:08 -07001734
Mark Mendella4f12202015-08-06 15:23:34 -04001735UNIMPLEMENTED_INTRINSIC(MathCos)
1736UNIMPLEMENTED_INTRINSIC(MathSin)
1737UNIMPLEMENTED_INTRINSIC(MathAcos)
1738UNIMPLEMENTED_INTRINSIC(MathAsin)
1739UNIMPLEMENTED_INTRINSIC(MathAtan)
1740UNIMPLEMENTED_INTRINSIC(MathAtan2)
1741UNIMPLEMENTED_INTRINSIC(MathCbrt)
1742UNIMPLEMENTED_INTRINSIC(MathCosh)
1743UNIMPLEMENTED_INTRINSIC(MathExp)
1744UNIMPLEMENTED_INTRINSIC(MathExpm1)
1745UNIMPLEMENTED_INTRINSIC(MathHypot)
1746UNIMPLEMENTED_INTRINSIC(MathLog)
1747UNIMPLEMENTED_INTRINSIC(MathLog10)
1748UNIMPLEMENTED_INTRINSIC(MathNextAfter)
1749UNIMPLEMENTED_INTRINSIC(MathSinh)
1750UNIMPLEMENTED_INTRINSIC(MathTan)
1751UNIMPLEMENTED_INTRINSIC(MathTanh)
1752
Chris Larsen3039e382015-08-26 07:54:08 -07001753#undef UNIMPLEMENTED_INTRINSIC
1754
1755#undef __
1756
1757} // namespace mips64
1758} // namespace art