blob: 457e247d6f44993d13d0675246ccab107fe01b74 [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:
David Srbecky9cd6d372016-02-09 15:24:47 +000090 explicit IntrinsicSlowPathMIPS64(HInvoke* invoke)
91 : SlowPathCodeMIPS64(invoke), invoke_(invoke) { }
Chris Larsen9701c2e2015-09-04 17:22:47 -070092
93 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
94 CodeGeneratorMIPS64* codegen = down_cast<CodeGeneratorMIPS64*>(codegen_in);
95
96 __ Bind(GetEntryLabel());
97
98 SaveLiveRegisters(codegen, invoke_->GetLocations());
99
100 MoveArguments(invoke_, codegen);
101
102 if (invoke_->IsInvokeStaticOrDirect()) {
103 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(),
104 Location::RegisterLocation(A0));
Chris Larsen9701c2e2015-09-04 17:22:47 -0700105 } else {
Alexey Frunze53afca12015-11-05 16:34:23 -0800106 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), Location::RegisterLocation(A0));
Chris Larsen9701c2e2015-09-04 17:22:47 -0700107 }
Alexey Frunze53afca12015-11-05 16:34:23 -0800108 codegen->RecordPcInfo(invoke_, invoke_->GetDexPc(), this);
Chris Larsen9701c2e2015-09-04 17:22:47 -0700109
110 // Copy the result back to the expected output.
111 Location out = invoke_->GetLocations()->Out();
112 if (out.IsValid()) {
113 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
114 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
115 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
116 }
117
118 RestoreLiveRegisters(codegen, invoke_->GetLocations());
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700119 __ Bc(GetExitLabel());
Chris Larsen9701c2e2015-09-04 17:22:47 -0700120 }
121
122 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathMIPS64"; }
123
124 private:
125 // The instruction where this slow path is happening.
126 HInvoke* const invoke_;
127
128 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathMIPS64);
129};
130
131#undef __
132
Chris Larsen3039e382015-08-26 07:54:08 -0700133bool IntrinsicLocationsBuilderMIPS64::TryDispatch(HInvoke* invoke) {
134 Dispatch(invoke);
135 LocationSummary* res = invoke->GetLocations();
136 return res != nullptr && res->Intrinsified();
137}
138
139#define __ assembler->
140
141static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
142 LocationSummary* locations = new (arena) LocationSummary(invoke,
143 LocationSummary::kNoCall,
144 kIntrinsified);
145 locations->SetInAt(0, Location::RequiresFpuRegister());
146 locations->SetOut(Location::RequiresRegister());
147}
148
149static void MoveFPToInt(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
150 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
151 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
152
153 if (is64bit) {
154 __ Dmfc1(out, in);
155 } else {
156 __ Mfc1(out, in);
157 }
158}
159
160// long java.lang.Double.doubleToRawLongBits(double)
161void IntrinsicLocationsBuilderMIPS64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
162 CreateFPToIntLocations(arena_, invoke);
163}
164
165void IntrinsicCodeGeneratorMIPS64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000166 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700167}
168
169// int java.lang.Float.floatToRawIntBits(float)
170void IntrinsicLocationsBuilderMIPS64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
171 CreateFPToIntLocations(arena_, invoke);
172}
173
174void IntrinsicCodeGeneratorMIPS64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000175 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700176}
177
178static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
179 LocationSummary* locations = new (arena) LocationSummary(invoke,
180 LocationSummary::kNoCall,
181 kIntrinsified);
182 locations->SetInAt(0, Location::RequiresRegister());
183 locations->SetOut(Location::RequiresFpuRegister());
184}
185
186static void MoveIntToFP(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
187 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
188 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
189
190 if (is64bit) {
191 __ Dmtc1(in, out);
192 } else {
193 __ Mtc1(in, out);
194 }
195}
196
197// double java.lang.Double.longBitsToDouble(long)
198void IntrinsicLocationsBuilderMIPS64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
199 CreateIntToFPLocations(arena_, invoke);
200}
201
202void IntrinsicCodeGeneratorMIPS64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000203 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700204}
205
206// float java.lang.Float.intBitsToFloat(int)
207void IntrinsicLocationsBuilderMIPS64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
208 CreateIntToFPLocations(arena_, invoke);
209}
210
211void IntrinsicCodeGeneratorMIPS64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000212 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700213}
214
215static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
216 LocationSummary* locations = new (arena) LocationSummary(invoke,
217 LocationSummary::kNoCall,
218 kIntrinsified);
219 locations->SetInAt(0, Location::RequiresRegister());
220 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
221}
222
223static void GenReverseBytes(LocationSummary* locations,
224 Primitive::Type type,
225 Mips64Assembler* assembler) {
226 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
227 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
228
229 switch (type) {
230 case Primitive::kPrimShort:
231 __ Dsbh(out, in);
232 __ Seh(out, out);
233 break;
234 case Primitive::kPrimInt:
235 __ Rotr(out, in, 16);
236 __ Wsbh(out, out);
237 break;
238 case Primitive::kPrimLong:
239 __ Dsbh(out, in);
240 __ Dshd(out, out);
241 break;
242 default:
243 LOG(FATAL) << "Unexpected size for reverse-bytes: " << type;
244 UNREACHABLE();
245 }
246}
247
248// int java.lang.Integer.reverseBytes(int)
249void IntrinsicLocationsBuilderMIPS64::VisitIntegerReverseBytes(HInvoke* invoke) {
250 CreateIntToIntLocations(arena_, invoke);
251}
252
253void IntrinsicCodeGeneratorMIPS64::VisitIntegerReverseBytes(HInvoke* invoke) {
254 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
255}
256
257// long java.lang.Long.reverseBytes(long)
258void IntrinsicLocationsBuilderMIPS64::VisitLongReverseBytes(HInvoke* invoke) {
259 CreateIntToIntLocations(arena_, invoke);
260}
261
262void IntrinsicCodeGeneratorMIPS64::VisitLongReverseBytes(HInvoke* invoke) {
263 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
264}
265
266// short java.lang.Short.reverseBytes(short)
267void IntrinsicLocationsBuilderMIPS64::VisitShortReverseBytes(HInvoke* invoke) {
268 CreateIntToIntLocations(arena_, invoke);
269}
270
271void IntrinsicCodeGeneratorMIPS64::VisitShortReverseBytes(HInvoke* invoke) {
272 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
273}
274
Chris Larsen81284372015-10-21 15:28:53 -0700275static void GenNumberOfLeadingZeroes(LocationSummary* locations,
276 bool is64bit,
277 Mips64Assembler* assembler) {
Chris Larsen3039e382015-08-26 07:54:08 -0700278 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
279 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
280
281 if (is64bit) {
282 __ Dclz(out, in);
283 } else {
284 __ Clz(out, in);
285 }
286}
287
288// int java.lang.Integer.numberOfLeadingZeros(int i)
289void IntrinsicLocationsBuilderMIPS64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
290 CreateIntToIntLocations(arena_, invoke);
291}
292
293void IntrinsicCodeGeneratorMIPS64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000294 GenNumberOfLeadingZeroes(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700295}
296
297// int java.lang.Long.numberOfLeadingZeros(long i)
298void IntrinsicLocationsBuilderMIPS64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
299 CreateIntToIntLocations(arena_, invoke);
300}
301
302void IntrinsicCodeGeneratorMIPS64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000303 GenNumberOfLeadingZeroes(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen0646da72015-09-22 16:02:40 -0700304}
305
Chris Larsen81284372015-10-21 15:28:53 -0700306static void GenNumberOfTrailingZeroes(LocationSummary* locations,
307 bool is64bit,
308 Mips64Assembler* assembler) {
Chris Larsen0646da72015-09-22 16:02:40 -0700309 Location in = locations->InAt(0);
310 Location out = locations->Out();
311
312 if (is64bit) {
313 __ Dsbh(out.AsRegister<GpuRegister>(), in.AsRegister<GpuRegister>());
314 __ Dshd(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
315 __ Dbitswap(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
316 __ Dclz(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
317 } else {
318 __ Rotr(out.AsRegister<GpuRegister>(), in.AsRegister<GpuRegister>(), 16);
319 __ Wsbh(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
320 __ Bitswap(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
321 __ Clz(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
322 }
323}
324
325// int java.lang.Integer.numberOfTrailingZeros(int i)
326void IntrinsicLocationsBuilderMIPS64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
327 CreateIntToIntLocations(arena_, invoke);
328}
329
330void IntrinsicCodeGeneratorMIPS64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000331 GenNumberOfTrailingZeroes(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen0646da72015-09-22 16:02:40 -0700332}
333
334// int java.lang.Long.numberOfTrailingZeros(long i)
335void IntrinsicLocationsBuilderMIPS64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
336 CreateIntToIntLocations(arena_, invoke);
337}
338
339void IntrinsicCodeGeneratorMIPS64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000340 GenNumberOfTrailingZeroes(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700341}
342
Chris Larsen9aebff22015-09-22 17:54:15 -0700343static void GenRotateRight(HInvoke* invoke,
344 Primitive::Type type,
345 Mips64Assembler* assembler) {
346 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
347
348 LocationSummary* locations = invoke->GetLocations();
349 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
350 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
351
352 if (invoke->InputAt(1)->IsIntConstant()) {
353 uint32_t shift = static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue());
354 if (type == Primitive::kPrimInt) {
355 shift &= 0x1f;
356 __ Rotr(out, in, shift);
357 } else {
358 shift &= 0x3f;
359 if (shift < 32) {
360 __ Drotr(out, in, shift);
361 } else {
362 shift &= 0x1f;
363 __ Drotr32(out, in, shift);
364 }
365 }
366 } else {
367 GpuRegister shamt = locations->InAt(1).AsRegister<GpuRegister>();
368 if (type == Primitive::kPrimInt) {
369 __ Rotrv(out, in, shamt);
370 } else {
371 __ Drotrv(out, in, shamt);
372 }
373 }
374}
375
376// int java.lang.Integer.rotateRight(int i, int distance)
377void IntrinsicLocationsBuilderMIPS64::VisitIntegerRotateRight(HInvoke* invoke) {
378 LocationSummary* locations = new (arena_) LocationSummary(invoke,
379 LocationSummary::kNoCall,
380 kIntrinsified);
381 locations->SetInAt(0, Location::RequiresRegister());
382 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
383 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
384}
385
386void IntrinsicCodeGeneratorMIPS64::VisitIntegerRotateRight(HInvoke* invoke) {
387 GenRotateRight(invoke, Primitive::kPrimInt, GetAssembler());
388}
389
Chris Larsen81284372015-10-21 15:28:53 -0700390// long java.lang.Long.rotateRight(long i, int distance)
Chris Larsen9aebff22015-09-22 17:54:15 -0700391void IntrinsicLocationsBuilderMIPS64::VisitLongRotateRight(HInvoke* invoke) {
392 LocationSummary* locations = new (arena_) LocationSummary(invoke,
393 LocationSummary::kNoCall,
394 kIntrinsified);
395 locations->SetInAt(0, Location::RequiresRegister());
396 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
397 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
398}
399
400void IntrinsicCodeGeneratorMIPS64::VisitLongRotateRight(HInvoke* invoke) {
401 GenRotateRight(invoke, Primitive::kPrimLong, GetAssembler());
402}
403
Chris Larsen0f8f8642015-10-02 17:25:58 -0700404static void GenRotateLeft(HInvoke* invoke,
405 Primitive::Type type,
406 Mips64Assembler* assembler) {
407 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
408
409 LocationSummary* locations = invoke->GetLocations();
410 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
411 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
412
413 if (invoke->InputAt(1)->IsIntConstant()) {
414 int32_t shift = -static_cast<int32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue());
415 if (type == Primitive::kPrimInt) {
416 shift &= 0x1f;
417 __ Rotr(out, in, shift);
418 } else {
419 shift &= 0x3f;
420 if (shift < 32) {
421 __ Drotr(out, in, shift);
422 } else {
423 shift &= 0x1f;
424 __ Drotr32(out, in, shift);
425 }
426 }
427 } else {
428 GpuRegister shamt = locations->InAt(1).AsRegister<GpuRegister>();
429 if (type == Primitive::kPrimInt) {
430 __ Subu(TMP, ZERO, shamt);
431 __ Rotrv(out, in, TMP);
432 } else {
433 __ Dsubu(TMP, ZERO, shamt);
434 __ Drotrv(out, in, TMP);
435 }
436 }
437}
438
439// int java.lang.Integer.rotateLeft(int i, int distance)
440void IntrinsicLocationsBuilderMIPS64::VisitIntegerRotateLeft(HInvoke* invoke) {
441 LocationSummary* locations = new (arena_) LocationSummary(invoke,
442 LocationSummary::kNoCall,
443 kIntrinsified);
444 locations->SetInAt(0, Location::RequiresRegister());
445 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
446 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
447}
448
449void IntrinsicCodeGeneratorMIPS64::VisitIntegerRotateLeft(HInvoke* invoke) {
450 GenRotateLeft(invoke, Primitive::kPrimInt, GetAssembler());
451}
452
Chris Larsen81284372015-10-21 15:28:53 -0700453// long java.lang.Long.rotateLeft(long i, int distance)
Chris Larsen0f8f8642015-10-02 17:25:58 -0700454void IntrinsicLocationsBuilderMIPS64::VisitLongRotateLeft(HInvoke* invoke) {
455 LocationSummary* locations = new (arena_) LocationSummary(invoke,
456 LocationSummary::kNoCall,
457 kIntrinsified);
458 locations->SetInAt(0, Location::RequiresRegister());
459 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
461}
462
463void IntrinsicCodeGeneratorMIPS64::VisitLongRotateLeft(HInvoke* invoke) {
464 GenRotateLeft(invoke, Primitive::kPrimLong, GetAssembler());
465}
466
Chris Larsen3039e382015-08-26 07:54:08 -0700467static void GenReverse(LocationSummary* locations,
468 Primitive::Type type,
469 Mips64Assembler* assembler) {
470 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
471
472 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
473 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
474
475 if (type == Primitive::kPrimInt) {
476 __ Rotr(out, in, 16);
477 __ Wsbh(out, out);
478 __ Bitswap(out, out);
479 } else {
480 __ Dsbh(out, in);
481 __ Dshd(out, out);
482 __ Dbitswap(out, out);
483 }
484}
485
486// int java.lang.Integer.reverse(int)
487void IntrinsicLocationsBuilderMIPS64::VisitIntegerReverse(HInvoke* invoke) {
488 CreateIntToIntLocations(arena_, invoke);
489}
490
491void IntrinsicCodeGeneratorMIPS64::VisitIntegerReverse(HInvoke* invoke) {
492 GenReverse(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
493}
494
495// long java.lang.Long.reverse(long)
496void IntrinsicLocationsBuilderMIPS64::VisitLongReverse(HInvoke* invoke) {
497 CreateIntToIntLocations(arena_, invoke);
498}
499
500void IntrinsicCodeGeneratorMIPS64::VisitLongReverse(HInvoke* invoke) {
501 GenReverse(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
502}
503
Chris Larsen0b7ac982015-09-04 12:54:28 -0700504static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
505 LocationSummary* locations = new (arena) LocationSummary(invoke,
506 LocationSummary::kNoCall,
507 kIntrinsified);
508 locations->SetInAt(0, Location::RequiresFpuRegister());
509 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
510}
511
512static void MathAbsFP(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
513 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
514 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
515
516 if (is64bit) {
517 __ AbsD(out, in);
518 } else {
519 __ AbsS(out, in);
520 }
521}
522
523// double java.lang.Math.abs(double)
524void IntrinsicLocationsBuilderMIPS64::VisitMathAbsDouble(HInvoke* invoke) {
525 CreateFPToFPLocations(arena_, invoke);
526}
527
528void IntrinsicCodeGeneratorMIPS64::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000529 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700530}
531
532// float java.lang.Math.abs(float)
533void IntrinsicLocationsBuilderMIPS64::VisitMathAbsFloat(HInvoke* invoke) {
534 CreateFPToFPLocations(arena_, invoke);
535}
536
537void IntrinsicCodeGeneratorMIPS64::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000538 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700539}
540
541static void CreateIntToInt(ArenaAllocator* arena, HInvoke* invoke) {
542 LocationSummary* locations = new (arena) LocationSummary(invoke,
543 LocationSummary::kNoCall,
544 kIntrinsified);
545 locations->SetInAt(0, Location::RequiresRegister());
546 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
547}
548
549static void GenAbsInteger(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
550 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
551 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
552
553 if (is64bit) {
554 __ Dsra32(AT, in, 31);
555 __ Xor(out, in, AT);
556 __ Dsubu(out, out, AT);
557 } else {
558 __ Sra(AT, in, 31);
559 __ Xor(out, in, AT);
560 __ Subu(out, out, AT);
561 }
562}
563
564// int java.lang.Math.abs(int)
565void IntrinsicLocationsBuilderMIPS64::VisitMathAbsInt(HInvoke* invoke) {
566 CreateIntToInt(arena_, invoke);
567}
568
569void IntrinsicCodeGeneratorMIPS64::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000570 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700571}
572
573// long java.lang.Math.abs(long)
574void IntrinsicLocationsBuilderMIPS64::VisitMathAbsLong(HInvoke* invoke) {
575 CreateIntToInt(arena_, invoke);
576}
577
578void IntrinsicCodeGeneratorMIPS64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000579 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700580}
581
582static void GenMinMaxFP(LocationSummary* locations,
583 bool is_min,
584 bool is_double,
585 Mips64Assembler* assembler) {
586 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
587 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
588 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
589
590 if (is_double) {
591 if (is_min) {
592 __ MinD(out, lhs, rhs);
593 } else {
594 __ MaxD(out, lhs, rhs);
595 }
596 } else {
597 if (is_min) {
598 __ MinS(out, lhs, rhs);
599 } else {
600 __ MaxS(out, lhs, rhs);
601 }
602 }
603}
604
605static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
606 LocationSummary* locations = new (arena) LocationSummary(invoke,
607 LocationSummary::kNoCall,
608 kIntrinsified);
609 locations->SetInAt(0, Location::RequiresFpuRegister());
610 locations->SetInAt(1, Location::RequiresFpuRegister());
611 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
612}
613
614// double java.lang.Math.min(double, double)
615void IntrinsicLocationsBuilderMIPS64::VisitMathMinDoubleDouble(HInvoke* invoke) {
616 CreateFPFPToFPLocations(arena_, invoke);
617}
618
619void IntrinsicCodeGeneratorMIPS64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000620 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700621}
622
623// float java.lang.Math.min(float, float)
624void IntrinsicLocationsBuilderMIPS64::VisitMathMinFloatFloat(HInvoke* invoke) {
625 CreateFPFPToFPLocations(arena_, invoke);
626}
627
628void IntrinsicCodeGeneratorMIPS64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000629 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700630}
631
632// double java.lang.Math.max(double, double)
633void IntrinsicLocationsBuilderMIPS64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
634 CreateFPFPToFPLocations(arena_, invoke);
635}
636
637void IntrinsicCodeGeneratorMIPS64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000638 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700639}
640
641// float java.lang.Math.max(float, float)
642void IntrinsicLocationsBuilderMIPS64::VisitMathMaxFloatFloat(HInvoke* invoke) {
643 CreateFPFPToFPLocations(arena_, invoke);
644}
645
646void IntrinsicCodeGeneratorMIPS64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000647 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700648}
649
650static void GenMinMax(LocationSummary* locations,
651 bool is_min,
652 Mips64Assembler* assembler) {
653 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
654 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
655 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
656
Chris Larsen14500822015-10-01 11:35:18 -0700657 // Some architectures, such as ARM and MIPS (prior to r6), have a
658 // conditional move instruction which only changes the target
659 // (output) register if the condition is true (MIPS prior to r6 had
660 // MOVF, MOVT, and MOVZ). The SELEQZ and SELNEZ instructions always
661 // change the target (output) register. If the condition is true the
662 // output register gets the contents of the "rs" register; otherwise,
663 // the output register is set to zero. One consequence of this is
664 // that to implement something like "rd = c==0 ? rs : rt" MIPS64r6
665 // needs to use a pair of SELEQZ/SELNEZ instructions. After
666 // executing this pair of instructions one of the output registers
667 // from the pair will necessarily contain zero. Then the code ORs the
668 // output registers from the SELEQZ/SELNEZ instructions to get the
669 // final result.
670 //
671 // The initial test to see if the output register is same as the
672 // first input register is needed to make sure that value in the
673 // first input register isn't clobbered before we've finished
674 // computing the output value. The logic in the corresponding else
675 // clause performs the same task but makes sure the second input
676 // register isn't clobbered in the event that it's the same register
677 // as the output register; the else clause also handles the case
678 // where the output register is distinct from both the first, and the
679 // second input registers.
Chris Larsen0b7ac982015-09-04 12:54:28 -0700680 if (out == lhs) {
681 __ Slt(AT, rhs, lhs);
682 if (is_min) {
683 __ Seleqz(out, lhs, AT);
684 __ Selnez(AT, rhs, AT);
685 } else {
686 __ Selnez(out, lhs, AT);
687 __ Seleqz(AT, rhs, AT);
688 }
689 } else {
690 __ Slt(AT, lhs, rhs);
691 if (is_min) {
692 __ Seleqz(out, rhs, AT);
693 __ Selnez(AT, lhs, AT);
694 } else {
695 __ Selnez(out, rhs, AT);
696 __ Seleqz(AT, lhs, AT);
697 }
698 }
699 __ Or(out, out, AT);
700}
701
702static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
703 LocationSummary* locations = new (arena) LocationSummary(invoke,
704 LocationSummary::kNoCall,
705 kIntrinsified);
706 locations->SetInAt(0, Location::RequiresRegister());
707 locations->SetInAt(1, Location::RequiresRegister());
708 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
709}
710
711// int java.lang.Math.min(int, int)
712void IntrinsicLocationsBuilderMIPS64::VisitMathMinIntInt(HInvoke* invoke) {
713 CreateIntIntToIntLocations(arena_, invoke);
714}
715
716void IntrinsicCodeGeneratorMIPS64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000717 GenMinMax(invoke->GetLocations(), /* is_min */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700718}
719
720// long java.lang.Math.min(long, long)
721void IntrinsicLocationsBuilderMIPS64::VisitMathMinLongLong(HInvoke* invoke) {
722 CreateIntIntToIntLocations(arena_, invoke);
723}
724
725void IntrinsicCodeGeneratorMIPS64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000726 GenMinMax(invoke->GetLocations(), /* is_min */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700727}
728
729// int java.lang.Math.max(int, int)
730void IntrinsicLocationsBuilderMIPS64::VisitMathMaxIntInt(HInvoke* invoke) {
731 CreateIntIntToIntLocations(arena_, invoke);
732}
733
734void IntrinsicCodeGeneratorMIPS64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000735 GenMinMax(invoke->GetLocations(), /* is_min */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700736}
737
738// long java.lang.Math.max(long, long)
739void IntrinsicLocationsBuilderMIPS64::VisitMathMaxLongLong(HInvoke* invoke) {
740 CreateIntIntToIntLocations(arena_, invoke);
741}
742
743void IntrinsicCodeGeneratorMIPS64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000744 GenMinMax(invoke->GetLocations(), /* is_min */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700745}
746
747// double java.lang.Math.sqrt(double)
748void IntrinsicLocationsBuilderMIPS64::VisitMathSqrt(HInvoke* invoke) {
749 CreateFPToFPLocations(arena_, invoke);
750}
751
752void IntrinsicCodeGeneratorMIPS64::VisitMathSqrt(HInvoke* invoke) {
753 LocationSummary* locations = invoke->GetLocations();
754 Mips64Assembler* assembler = GetAssembler();
755 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
756 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
757
758 __ SqrtD(out, in);
759}
760
Chris Larsen81284372015-10-21 15:28:53 -0700761static void CreateFPToFP(ArenaAllocator* arena,
762 HInvoke* invoke,
763 Location::OutputOverlap overlaps = Location::kOutputOverlap) {
Chris Larsen0b7ac982015-09-04 12:54:28 -0700764 LocationSummary* locations = new (arena) LocationSummary(invoke,
765 LocationSummary::kNoCall,
766 kIntrinsified);
767 locations->SetInAt(0, Location::RequiresFpuRegister());
Chris Larsen81284372015-10-21 15:28:53 -0700768 locations->SetOut(Location::RequiresFpuRegister(), overlaps);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700769}
770
771// double java.lang.Math.rint(double)
772void IntrinsicLocationsBuilderMIPS64::VisitMathRint(HInvoke* invoke) {
Chris Larsen81284372015-10-21 15:28:53 -0700773 CreateFPToFP(arena_, invoke, Location::kNoOutputOverlap);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700774}
775
776void IntrinsicCodeGeneratorMIPS64::VisitMathRint(HInvoke* invoke) {
777 LocationSummary* locations = invoke->GetLocations();
778 Mips64Assembler* assembler = GetAssembler();
779 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
780 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
781
782 __ RintD(out, in);
783}
784
785// double java.lang.Math.floor(double)
786void IntrinsicLocationsBuilderMIPS64::VisitMathFloor(HInvoke* invoke) {
787 CreateFPToFP(arena_, invoke);
788}
789
Chris Larsen14500822015-10-01 11:35:18 -0700790const constexpr uint16_t kFPLeaveUnchanged = kPositiveZero |
791 kPositiveInfinity |
792 kNegativeZero |
793 kNegativeInfinity |
794 kQuietNaN |
795 kSignalingNaN;
Chris Larsen0b7ac982015-09-04 12:54:28 -0700796
Chris Larsen81284372015-10-21 15:28:53 -0700797enum FloatRoundingMode {
798 kFloor,
799 kCeil,
800};
801
802static void GenRoundingMode(LocationSummary* locations,
803 FloatRoundingMode mode,
804 Mips64Assembler* assembler) {
Chris Larsen0b7ac982015-09-04 12:54:28 -0700805 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
806 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
807
Chris Larsen81284372015-10-21 15:28:53 -0700808 DCHECK_NE(in, out);
809
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700810 Mips64Label done;
Chris Larsen0b7ac982015-09-04 12:54:28 -0700811
Chris Larsen81284372015-10-21 15:28:53 -0700812 // double floor/ceil(double in) {
Chris Larsen0b7ac982015-09-04 12:54:28 -0700813 // if in.isNaN || in.isInfinite || in.isZero {
814 // return in;
815 // }
816 __ ClassD(out, in);
817 __ Dmfc1(AT, out);
Chris Larsen14500822015-10-01 11:35:18 -0700818 __ Andi(AT, AT, kFPLeaveUnchanged); // +0.0 | +Inf | -0.0 | -Inf | qNaN | sNaN
Chris Larsen0b7ac982015-09-04 12:54:28 -0700819 __ MovD(out, in);
820 __ Bnezc(AT, &done);
821
Chris Larsen81284372015-10-21 15:28:53 -0700822 // Long outLong = floor/ceil(in);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700823 // if outLong == Long.MAX_VALUE {
Chris Larsen81284372015-10-21 15:28:53 -0700824 // // floor()/ceil() has almost certainly returned a value
825 // // which can't be successfully represented as a signed
826 // // 64-bit number. Java expects that the input value will
827 // // be returned in these cases.
828 // // There is also a small probability that floor(in)/ceil(in)
829 // // correctly truncates/rounds up the input value to
830 // // Long.MAX_VALUE. In that case, this exception handling
831 // // code still does the correct thing.
Chris Larsen0b7ac982015-09-04 12:54:28 -0700832 // return in;
833 // }
Chris Larsen81284372015-10-21 15:28:53 -0700834 if (mode == kFloor) {
835 __ FloorLD(out, in);
836 } else if (mode == kCeil) {
837 __ CeilLD(out, in);
838 }
Chris Larsen0b7ac982015-09-04 12:54:28 -0700839 __ Dmfc1(AT, out);
840 __ MovD(out, in);
841 __ LoadConst64(TMP, kPrimLongMax);
842 __ Beqc(AT, TMP, &done);
843
844 // double out = outLong;
845 // return out;
846 __ Dmtc1(AT, out);
847 __ Cvtdl(out, out);
848 __ Bind(&done);
849 // }
850}
851
Chris Larsen81284372015-10-21 15:28:53 -0700852void IntrinsicCodeGeneratorMIPS64::VisitMathFloor(HInvoke* invoke) {
853 GenRoundingMode(invoke->GetLocations(), kFloor, GetAssembler());
854}
855
Chris Larsen0b7ac982015-09-04 12:54:28 -0700856// double java.lang.Math.ceil(double)
857void IntrinsicLocationsBuilderMIPS64::VisitMathCeil(HInvoke* invoke) {
858 CreateFPToFP(arena_, invoke);
859}
860
861void IntrinsicCodeGeneratorMIPS64::VisitMathCeil(HInvoke* invoke) {
Chris Larsen81284372015-10-21 15:28:53 -0700862 GenRoundingMode(invoke->GetLocations(), kCeil, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700863}
864
Chris Larsen70fb1f42015-09-04 10:15:27 -0700865// byte libcore.io.Memory.peekByte(long address)
866void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekByte(HInvoke* invoke) {
867 CreateIntToIntLocations(arena_, invoke);
868}
869
870void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekByte(HInvoke* invoke) {
871 Mips64Assembler* assembler = GetAssembler();
872 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
873 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
874
875 __ Lb(out, adr, 0);
876}
877
878// short libcore.io.Memory.peekShort(long address)
879void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekShortNative(HInvoke* invoke) {
880 CreateIntToIntLocations(arena_, invoke);
881}
882
883void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekShortNative(HInvoke* invoke) {
884 Mips64Assembler* assembler = GetAssembler();
885 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
886 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
887
888 __ Lh(out, adr, 0);
889}
890
891// int libcore.io.Memory.peekInt(long address)
892void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekIntNative(HInvoke* invoke) {
893 CreateIntToIntLocations(arena_, invoke);
894}
895
896void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekIntNative(HInvoke* invoke) {
897 Mips64Assembler* assembler = GetAssembler();
898 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
899 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
900
901 __ Lw(out, adr, 0);
902}
903
904// long libcore.io.Memory.peekLong(long address)
905void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekLongNative(HInvoke* invoke) {
906 CreateIntToIntLocations(arena_, invoke);
907}
908
909void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekLongNative(HInvoke* invoke) {
910 Mips64Assembler* assembler = GetAssembler();
911 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
912 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
913
914 __ Ld(out, adr, 0);
915}
916
917static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
918 LocationSummary* locations = new (arena) LocationSummary(invoke,
919 LocationSummary::kNoCall,
920 kIntrinsified);
921 locations->SetInAt(0, Location::RequiresRegister());
922 locations->SetInAt(1, Location::RequiresRegister());
923}
924
925// void libcore.io.Memory.pokeByte(long address, byte value)
926void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeByte(HInvoke* invoke) {
927 CreateIntIntToVoidLocations(arena_, invoke);
928}
929
930void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeByte(HInvoke* invoke) {
931 Mips64Assembler* assembler = GetAssembler();
932 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
933 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
934
935 __ Sb(val, adr, 0);
936}
937
938// void libcore.io.Memory.pokeShort(long address, short value)
939void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeShortNative(HInvoke* invoke) {
940 CreateIntIntToVoidLocations(arena_, invoke);
941}
942
943void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeShortNative(HInvoke* invoke) {
944 Mips64Assembler* assembler = GetAssembler();
945 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
946 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
947
948 __ Sh(val, adr, 0);
949}
950
951// void libcore.io.Memory.pokeInt(long address, int value)
952void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeIntNative(HInvoke* invoke) {
953 CreateIntIntToVoidLocations(arena_, invoke);
954}
955
956void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeIntNative(HInvoke* invoke) {
957 Mips64Assembler* assembler = GetAssembler();
958 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
959 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
960
961 __ Sw(val, adr, 00);
962}
963
964// void libcore.io.Memory.pokeLong(long address, long value)
965void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeLongNative(HInvoke* invoke) {
966 CreateIntIntToVoidLocations(arena_, invoke);
967}
968
969void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeLongNative(HInvoke* invoke) {
970 Mips64Assembler* assembler = GetAssembler();
971 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
972 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
973
974 __ Sd(val, adr, 0);
975}
976
Chris Larsen49e55392015-09-04 16:04:03 -0700977// Thread java.lang.Thread.currentThread()
978void IntrinsicLocationsBuilderMIPS64::VisitThreadCurrentThread(HInvoke* invoke) {
979 LocationSummary* locations = new (arena_) LocationSummary(invoke,
980 LocationSummary::kNoCall,
981 kIntrinsified);
982 locations->SetOut(Location::RequiresRegister());
983}
984
985void IntrinsicCodeGeneratorMIPS64::VisitThreadCurrentThread(HInvoke* invoke) {
986 Mips64Assembler* assembler = GetAssembler();
987 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
988
989 __ LoadFromOffset(kLoadUnsignedWord,
990 out,
991 TR,
992 Thread::PeerOffset<kMips64PointerSize>().Int32Value());
993}
994
Chris Larsen1360ada2015-09-04 23:38:16 -0700995static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
996 LocationSummary* locations = new (arena) LocationSummary(invoke,
997 LocationSummary::kNoCall,
998 kIntrinsified);
999 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1000 locations->SetInAt(1, Location::RequiresRegister());
1001 locations->SetInAt(2, Location::RequiresRegister());
1002 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1003}
1004
1005static void GenUnsafeGet(HInvoke* invoke,
1006 Primitive::Type type,
1007 bool is_volatile,
1008 CodeGeneratorMIPS64* codegen) {
1009 LocationSummary* locations = invoke->GetLocations();
1010 DCHECK((type == Primitive::kPrimInt) ||
1011 (type == Primitive::kPrimLong) ||
1012 (type == Primitive::kPrimNot));
1013 Mips64Assembler* assembler = codegen->GetAssembler();
1014 // Object pointer.
1015 GpuRegister base = locations->InAt(1).AsRegister<GpuRegister>();
1016 // Long offset.
1017 GpuRegister offset = locations->InAt(2).AsRegister<GpuRegister>();
1018 GpuRegister trg = locations->Out().AsRegister<GpuRegister>();
1019
1020 __ Daddu(TMP, base, offset);
1021 if (is_volatile) {
1022 __ Sync(0);
1023 }
1024 switch (type) {
1025 case Primitive::kPrimInt:
1026 __ Lw(trg, TMP, 0);
1027 break;
1028
1029 case Primitive::kPrimNot:
1030 __ Lwu(trg, TMP, 0);
1031 break;
1032
1033 case Primitive::kPrimLong:
1034 __ Ld(trg, TMP, 0);
1035 break;
1036
1037 default:
1038 LOG(FATAL) << "Unsupported op size " << type;
1039 UNREACHABLE();
1040 }
1041}
1042
1043// int sun.misc.Unsafe.getInt(Object o, long offset)
1044void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGet(HInvoke* invoke) {
1045 CreateIntIntIntToIntLocations(arena_, invoke);
1046}
1047
1048void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001049 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001050}
1051
1052// int sun.misc.Unsafe.getIntVolatile(Object o, long offset)
1053void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetVolatile(HInvoke* invoke) {
1054 CreateIntIntIntToIntLocations(arena_, invoke);
1055}
1056
1057void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001058 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001059}
1060
1061// long sun.misc.Unsafe.getLong(Object o, long offset)
1062void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetLong(HInvoke* invoke) {
1063 CreateIntIntIntToIntLocations(arena_, invoke);
1064}
1065
1066void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001067 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001068}
1069
1070// long sun.misc.Unsafe.getLongVolatile(Object o, long offset)
1071void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1072 CreateIntIntIntToIntLocations(arena_, invoke);
1073}
1074
1075void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001076 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001077}
1078
1079// Object sun.misc.Unsafe.getObject(Object o, long offset)
1080void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetObject(HInvoke* invoke) {
1081 CreateIntIntIntToIntLocations(arena_, invoke);
1082}
1083
1084void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001085 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001086}
1087
1088// Object sun.misc.Unsafe.getObjectVolatile(Object o, long offset)
1089void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1090 CreateIntIntIntToIntLocations(arena_, invoke);
1091}
1092
1093void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001094 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001095}
1096
1097static void CreateIntIntIntIntToVoid(ArenaAllocator* arena, HInvoke* invoke) {
1098 LocationSummary* locations = new (arena) LocationSummary(invoke,
1099 LocationSummary::kNoCall,
1100 kIntrinsified);
1101 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1102 locations->SetInAt(1, Location::RequiresRegister());
1103 locations->SetInAt(2, Location::RequiresRegister());
1104 locations->SetInAt(3, Location::RequiresRegister());
1105}
1106
1107static void GenUnsafePut(LocationSummary* locations,
1108 Primitive::Type type,
1109 bool is_volatile,
1110 bool is_ordered,
1111 CodeGeneratorMIPS64* codegen) {
1112 DCHECK((type == Primitive::kPrimInt) ||
1113 (type == Primitive::kPrimLong) ||
1114 (type == Primitive::kPrimNot));
1115 Mips64Assembler* assembler = codegen->GetAssembler();
1116 // Object pointer.
1117 GpuRegister base = locations->InAt(1).AsRegister<GpuRegister>();
1118 // Long offset.
1119 GpuRegister offset = locations->InAt(2).AsRegister<GpuRegister>();
1120 GpuRegister value = locations->InAt(3).AsRegister<GpuRegister>();
1121
1122 __ Daddu(TMP, base, offset);
1123 if (is_volatile || is_ordered) {
1124 __ Sync(0);
1125 }
1126 switch (type) {
1127 case Primitive::kPrimInt:
1128 case Primitive::kPrimNot:
1129 __ Sw(value, TMP, 0);
1130 break;
1131
1132 case Primitive::kPrimLong:
1133 __ Sd(value, TMP, 0);
1134 break;
1135
1136 default:
1137 LOG(FATAL) << "Unsupported op size " << type;
1138 UNREACHABLE();
1139 }
1140 if (is_volatile) {
1141 __ Sync(0);
1142 }
1143
1144 if (type == Primitive::kPrimNot) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001145 bool value_can_be_null = true; // TODO: Worth finding out this information?
1146 codegen->MarkGCCard(base, value, value_can_be_null);
Chris Larsen1360ada2015-09-04 23:38:16 -07001147 }
1148}
1149
1150// void sun.misc.Unsafe.putInt(Object o, long offset, int x)
1151void IntrinsicLocationsBuilderMIPS64::VisitUnsafePut(HInvoke* invoke) {
1152 CreateIntIntIntIntToVoid(arena_, invoke);
1153}
1154
1155void IntrinsicCodeGeneratorMIPS64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001156 GenUnsafePut(invoke->GetLocations(),
1157 Primitive::kPrimInt,
1158 /* is_volatile */ false,
1159 /* is_ordered */ false,
1160 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001161}
1162
1163// void sun.misc.Unsafe.putOrderedInt(Object o, long offset, int x)
1164void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutOrdered(HInvoke* invoke) {
1165 CreateIntIntIntIntToVoid(arena_, invoke);
1166}
1167
1168void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001169 GenUnsafePut(invoke->GetLocations(),
1170 Primitive::kPrimInt,
1171 /* is_volatile */ false,
1172 /* is_ordered */ true,
1173 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001174}
1175
1176// void sun.misc.Unsafe.putIntVolatile(Object o, long offset, int x)
1177void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutVolatile(HInvoke* invoke) {
1178 CreateIntIntIntIntToVoid(arena_, invoke);
1179}
1180
1181void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001182 GenUnsafePut(invoke->GetLocations(),
1183 Primitive::kPrimInt,
1184 /* is_volatile */ true,
1185 /* is_ordered */ false,
1186 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001187}
1188
1189// void sun.misc.Unsafe.putObject(Object o, long offset, Object x)
1190void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutObject(HInvoke* invoke) {
1191 CreateIntIntIntIntToVoid(arena_, invoke);
1192}
1193
1194void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001195 GenUnsafePut(invoke->GetLocations(),
1196 Primitive::kPrimNot,
1197 /* is_volatile */ false,
1198 /* is_ordered */ false,
1199 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001200}
1201
1202// void sun.misc.Unsafe.putOrderedObject(Object o, long offset, Object x)
1203void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1204 CreateIntIntIntIntToVoid(arena_, invoke);
1205}
1206
1207void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001208 GenUnsafePut(invoke->GetLocations(),
1209 Primitive::kPrimNot,
1210 /* is_volatile */ false,
1211 /* is_ordered */ true,
1212 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001213}
1214
1215// void sun.misc.Unsafe.putObjectVolatile(Object o, long offset, Object x)
1216void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1217 CreateIntIntIntIntToVoid(arena_, invoke);
1218}
1219
1220void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001221 GenUnsafePut(invoke->GetLocations(),
1222 Primitive::kPrimNot,
1223 /* is_volatile */ true,
1224 /* is_ordered */ false,
1225 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001226}
1227
1228// void sun.misc.Unsafe.putLong(Object o, long offset, long x)
1229void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutLong(HInvoke* invoke) {
1230 CreateIntIntIntIntToVoid(arena_, invoke);
1231}
1232
1233void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001234 GenUnsafePut(invoke->GetLocations(),
1235 Primitive::kPrimLong,
1236 /* is_volatile */ false,
1237 /* is_ordered */ false,
1238 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001239}
1240
1241// void sun.misc.Unsafe.putOrderedLong(Object o, long offset, long x)
1242void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1243 CreateIntIntIntIntToVoid(arena_, invoke);
1244}
1245
1246void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001247 GenUnsafePut(invoke->GetLocations(),
1248 Primitive::kPrimLong,
1249 /* is_volatile */ false,
1250 /* is_ordered */ true,
1251 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001252}
1253
1254// void sun.misc.Unsafe.putLongVolatile(Object o, long offset, long x)
1255void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1256 CreateIntIntIntIntToVoid(arena_, invoke);
1257}
1258
1259void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001260 GenUnsafePut(invoke->GetLocations(),
1261 Primitive::kPrimLong,
1262 /* is_volatile */ true,
1263 /* is_ordered */ false,
1264 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001265}
1266
Chris Larsen36427492015-10-23 02:19:38 -07001267static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, HInvoke* invoke) {
1268 LocationSummary* locations = new (arena) LocationSummary(invoke,
1269 LocationSummary::kNoCall,
1270 kIntrinsified);
1271 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1272 locations->SetInAt(1, Location::RequiresRegister());
1273 locations->SetInAt(2, Location::RequiresRegister());
1274 locations->SetInAt(3, Location::RequiresRegister());
1275 locations->SetInAt(4, Location::RequiresRegister());
1276
1277 locations->SetOut(Location::RequiresRegister());
1278}
1279
1280static void GenCas(LocationSummary* locations, Primitive::Type type, CodeGeneratorMIPS64* codegen) {
1281 Mips64Assembler* assembler = codegen->GetAssembler();
1282 GpuRegister base = locations->InAt(1).AsRegister<GpuRegister>();
1283 GpuRegister offset = locations->InAt(2).AsRegister<GpuRegister>();
1284 GpuRegister expected = locations->InAt(3).AsRegister<GpuRegister>();
1285 GpuRegister value = locations->InAt(4).AsRegister<GpuRegister>();
1286 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1287
1288 DCHECK_NE(base, out);
1289 DCHECK_NE(offset, out);
1290 DCHECK_NE(expected, out);
1291
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001292 if (type == Primitive::kPrimNot) {
1293 // Mark card for object assuming new value is stored.
1294 bool value_can_be_null = true; // TODO: Worth finding out this information?
1295 codegen->MarkGCCard(base, value, value_can_be_null);
1296 }
1297
Chris Larsen36427492015-10-23 02:19:38 -07001298 // do {
1299 // tmp_value = [tmp_ptr] - expected;
1300 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1301 // result = tmp_value != 0;
1302
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001303 Mips64Label loop_head, exit_loop;
Chris Larsen36427492015-10-23 02:19:38 -07001304 __ Daddu(TMP, base, offset);
1305 __ Sync(0);
1306 __ Bind(&loop_head);
1307 if (type == Primitive::kPrimLong) {
1308 __ Lld(out, TMP);
1309 } else {
Roland Levillain391b8662015-12-18 11:43:38 +00001310 // Note: We will need a read barrier here, when read barrier
1311 // support is added to the MIPS64 back end.
Chris Larsen36427492015-10-23 02:19:38 -07001312 __ Ll(out, TMP);
1313 }
1314 __ Dsubu(out, out, expected); // If we didn't get the 'expected'
1315 __ Sltiu(out, out, 1); // value, set 'out' to false, and
1316 __ Beqzc(out, &exit_loop); // return.
1317 __ Move(out, value); // Use 'out' for the 'store conditional' instruction.
1318 // If we use 'value' directly, we would lose 'value'
1319 // in the case that the store fails. Whether the
1320 // store succeeds, or fails, it will load the
1321 // correct boolean value into the 'out' register.
1322 if (type == Primitive::kPrimLong) {
1323 __ Scd(out, TMP);
1324 } else {
1325 __ Sc(out, TMP);
1326 }
1327 __ Beqzc(out, &loop_head); // If we couldn't do the read-modify-write
1328 // cycle atomically then retry.
1329 __ Bind(&exit_loop);
1330 __ Sync(0);
1331}
1332
1333// boolean sun.misc.Unsafe.compareAndSwapInt(Object o, long offset, int expected, int x)
1334void IntrinsicLocationsBuilderMIPS64::VisitUnsafeCASInt(HInvoke* invoke) {
1335 CreateIntIntIntIntIntToInt(arena_, invoke);
1336}
1337
1338void IntrinsicCodeGeneratorMIPS64::VisitUnsafeCASInt(HInvoke* invoke) {
1339 GenCas(invoke->GetLocations(), Primitive::kPrimInt, codegen_);
1340}
1341
1342// boolean sun.misc.Unsafe.compareAndSwapLong(Object o, long offset, long expected, long x)
1343void IntrinsicLocationsBuilderMIPS64::VisitUnsafeCASLong(HInvoke* invoke) {
1344 CreateIntIntIntIntIntToInt(arena_, invoke);
1345}
1346
1347void IntrinsicCodeGeneratorMIPS64::VisitUnsafeCASLong(HInvoke* invoke) {
1348 GenCas(invoke->GetLocations(), Primitive::kPrimLong, codegen_);
1349}
1350
1351// boolean sun.misc.Unsafe.compareAndSwapObject(Object o, long offset, Object expected, Object x)
1352void IntrinsicLocationsBuilderMIPS64::VisitUnsafeCASObject(HInvoke* invoke) {
1353 CreateIntIntIntIntIntToInt(arena_, invoke);
1354}
1355
1356void IntrinsicCodeGeneratorMIPS64::VisitUnsafeCASObject(HInvoke* invoke) {
1357 GenCas(invoke->GetLocations(), Primitive::kPrimNot, codegen_);
1358}
1359
Chris Larsen9701c2e2015-09-04 17:22:47 -07001360// char java.lang.String.charAt(int index)
1361void IntrinsicLocationsBuilderMIPS64::VisitStringCharAt(HInvoke* invoke) {
1362 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1363 LocationSummary::kCallOnSlowPath,
1364 kIntrinsified);
1365 locations->SetInAt(0, Location::RequiresRegister());
1366 locations->SetInAt(1, Location::RequiresRegister());
1367 locations->SetOut(Location::SameAsFirstInput());
1368}
1369
1370void IntrinsicCodeGeneratorMIPS64::VisitStringCharAt(HInvoke* invoke) {
1371 LocationSummary* locations = invoke->GetLocations();
1372 Mips64Assembler* assembler = GetAssembler();
1373
1374 // Location of reference to data array
1375 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1376 // Location of count
1377 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1378
1379 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1380 GpuRegister idx = locations->InAt(1).AsRegister<GpuRegister>();
1381 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1382
1383 // TODO: Maybe we can support range check elimination. Overall,
1384 // though, I think it's not worth the cost.
1385 // TODO: For simplicity, the index parameter is requested in a
1386 // register, so different from Quick we will not optimize the
1387 // code for constants (which would save a register).
1388
1389 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1390 codegen_->AddSlowPath(slow_path);
1391
1392 // Load the string size
1393 __ Lw(TMP, obj, count_offset);
1394 codegen_->MaybeRecordImplicitNullCheck(invoke);
1395 // Revert to slow path if idx is too large, or negative
1396 __ Bgeuc(idx, TMP, slow_path->GetEntryLabel());
1397
1398 // out = obj[2*idx].
1399 __ Sll(TMP, idx, 1); // idx * 2
1400 __ Daddu(TMP, TMP, obj); // Address of char at location idx
1401 __ Lhu(out, TMP, value_offset); // Load char at location idx
1402
1403 __ Bind(slow_path->GetExitLabel());
1404}
1405
1406// int java.lang.String.compareTo(String anotherString)
1407void IntrinsicLocationsBuilderMIPS64::VisitStringCompareTo(HInvoke* invoke) {
1408 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1409 LocationSummary::kCall,
1410 kIntrinsified);
1411 InvokeRuntimeCallingConvention calling_convention;
1412 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1413 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1414 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1415 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1416}
1417
1418void IntrinsicCodeGeneratorMIPS64::VisitStringCompareTo(HInvoke* invoke) {
1419 Mips64Assembler* assembler = GetAssembler();
1420 LocationSummary* locations = invoke->GetLocations();
1421
1422 // Note that the null check must have been done earlier.
1423 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1424
1425 GpuRegister argument = locations->InAt(1).AsRegister<GpuRegister>();
1426 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1427 codegen_->AddSlowPath(slow_path);
1428 __ Beqzc(argument, slow_path->GetEntryLabel());
1429
1430 __ LoadFromOffset(kLoadDoubleword,
1431 TMP,
1432 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001433 QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, pStringCompareTo).Int32Value());
Chris Larsen9701c2e2015-09-04 17:22:47 -07001434 __ Jalr(TMP);
1435 __ Nop();
1436 __ Bind(slow_path->GetExitLabel());
1437}
1438
Chris Larsen972d6d72015-10-20 11:29:12 -07001439// boolean java.lang.String.equals(Object anObject)
1440void IntrinsicLocationsBuilderMIPS64::VisitStringEquals(HInvoke* invoke) {
1441 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1442 LocationSummary::kNoCall,
1443 kIntrinsified);
1444 locations->SetInAt(0, Location::RequiresRegister());
1445 locations->SetInAt(1, Location::RequiresRegister());
1446 locations->SetOut(Location::RequiresRegister());
1447
1448 // Temporary registers to store lengths of strings and for calculations.
1449 locations->AddTemp(Location::RequiresRegister());
1450 locations->AddTemp(Location::RequiresRegister());
1451 locations->AddTemp(Location::RequiresRegister());
1452}
1453
1454void IntrinsicCodeGeneratorMIPS64::VisitStringEquals(HInvoke* invoke) {
1455 Mips64Assembler* assembler = GetAssembler();
1456 LocationSummary* locations = invoke->GetLocations();
1457
1458 GpuRegister str = locations->InAt(0).AsRegister<GpuRegister>();
1459 GpuRegister arg = locations->InAt(1).AsRegister<GpuRegister>();
1460 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1461
1462 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
1463 GpuRegister temp2 = locations->GetTemp(1).AsRegister<GpuRegister>();
1464 GpuRegister temp3 = locations->GetTemp(2).AsRegister<GpuRegister>();
1465
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001466 Mips64Label loop;
1467 Mips64Label end;
1468 Mips64Label return_true;
1469 Mips64Label return_false;
Chris Larsen972d6d72015-10-20 11:29:12 -07001470
1471 // Get offsets of count, value, and class fields within a string object.
1472 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1473 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1474 const int32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1475
1476 // Note that the null check must have been done earlier.
1477 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1478
1479 // If the register containing the pointer to "this", and the register
1480 // containing the pointer to "anObject" are the same register then
1481 // "this", and "anObject" are the same object and we can
1482 // short-circuit the logic to a true result.
1483 if (str == arg) {
1484 __ LoadConst64(out, 1);
1485 return;
1486 }
1487
1488 // Check if input is null, return false if it is.
1489 __ Beqzc(arg, &return_false);
1490
1491 // Reference equality check, return true if same reference.
1492 __ Beqc(str, arg, &return_true);
1493
1494 // Instanceof check for the argument by comparing class fields.
1495 // All string objects must have the same type since String cannot be subclassed.
1496 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1497 // If the argument is a string object, its class field must be equal to receiver's class field.
1498 __ Lw(temp1, str, class_offset);
1499 __ Lw(temp2, arg, class_offset);
1500 __ Bnec(temp1, temp2, &return_false);
1501
1502 // Load lengths of this and argument strings.
1503 __ Lw(temp1, str, count_offset);
1504 __ Lw(temp2, arg, count_offset);
1505 // Check if lengths are equal, return false if they're not.
1506 __ Bnec(temp1, temp2, &return_false);
1507 // Return true if both strings are empty.
1508 __ Beqzc(temp1, &return_true);
1509
1510 // Don't overwrite input registers
1511 __ Move(TMP, str);
1512 __ Move(temp3, arg);
1513
1514 // Assertions that must hold in order to compare strings 4 characters at a time.
1515 DCHECK_ALIGNED(value_offset, 8);
1516 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1517
1518 // Loop to compare strings 4 characters at a time starting at the beginning of the string.
1519 // Ok to do this because strings are zero-padded to be 8-byte aligned.
1520 __ Bind(&loop);
1521 __ Ld(out, TMP, value_offset);
1522 __ Ld(temp2, temp3, value_offset);
1523 __ Bnec(out, temp2, &return_false);
1524 __ Daddiu(TMP, TMP, 8);
1525 __ Daddiu(temp3, temp3, 8);
1526 __ Addiu(temp1, temp1, -4);
1527 __ Bgtzc(temp1, &loop);
1528
1529 // Return true and exit the function.
1530 // If loop does not result in returning false, we return true.
1531 __ Bind(&return_true);
1532 __ LoadConst64(out, 1);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001533 __ Bc(&end);
Chris Larsen972d6d72015-10-20 11:29:12 -07001534
1535 // Return false and exit the function.
1536 __ Bind(&return_false);
1537 __ LoadConst64(out, 0);
1538 __ Bind(&end);
1539}
1540
Chris Larsen9701c2e2015-09-04 17:22:47 -07001541static void GenerateStringIndexOf(HInvoke* invoke,
1542 Mips64Assembler* assembler,
1543 CodeGeneratorMIPS64* codegen,
1544 ArenaAllocator* allocator,
1545 bool start_at_zero) {
1546 LocationSummary* locations = invoke->GetLocations();
1547 GpuRegister tmp_reg = start_at_zero ? locations->GetTemp(0).AsRegister<GpuRegister>() : TMP;
1548
1549 // Note that the null check must have been done earlier.
1550 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1551
1552 // Check for code points > 0xFFFF. Either a slow-path check when we
1553 // don't know statically, or directly dispatch if we have a constant.
1554 SlowPathCodeMIPS64* slow_path = nullptr;
1555 if (invoke->InputAt(1)->IsIntConstant()) {
1556 if (!IsUint<16>(invoke->InputAt(1)->AsIntConstant()->GetValue())) {
1557 // Always needs the slow-path. We could directly dispatch to it,
1558 // but this case should be rare, so for simplicity just put the
1559 // full slow-path down and branch unconditionally.
1560 slow_path = new (allocator) IntrinsicSlowPathMIPS64(invoke);
1561 codegen->AddSlowPath(slow_path);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001562 __ Bc(slow_path->GetEntryLabel());
Chris Larsen9701c2e2015-09-04 17:22:47 -07001563 __ Bind(slow_path->GetExitLabel());
1564 return;
1565 }
1566 } else {
1567 GpuRegister char_reg = locations->InAt(1).AsRegister<GpuRegister>();
1568 __ LoadConst32(tmp_reg, std::numeric_limits<uint16_t>::max());
1569 slow_path = new (allocator) IntrinsicSlowPathMIPS64(invoke);
1570 codegen->AddSlowPath(slow_path);
1571 __ Bltuc(tmp_reg, char_reg, slow_path->GetEntryLabel()); // UTF-16 required
1572 }
1573
1574 if (start_at_zero) {
1575 DCHECK_EQ(tmp_reg, A2);
1576 // Start-index = 0.
1577 __ Clear(tmp_reg);
1578 } else {
1579 __ Slt(TMP, A2, ZERO); // if fromIndex < 0
1580 __ Seleqz(A2, A2, TMP); // fromIndex = 0
1581 }
1582
1583 __ LoadFromOffset(kLoadDoubleword,
1584 TMP,
1585 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001586 QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, pIndexOf).Int32Value());
Chris Larsen9701c2e2015-09-04 17:22:47 -07001587 __ Jalr(TMP);
1588 __ Nop();
1589
1590 if (slow_path != nullptr) {
1591 __ Bind(slow_path->GetExitLabel());
1592 }
1593}
1594
1595// int java.lang.String.indexOf(int ch)
1596void IntrinsicLocationsBuilderMIPS64::VisitStringIndexOf(HInvoke* invoke) {
1597 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1598 LocationSummary::kCall,
1599 kIntrinsified);
1600 // We have a hand-crafted assembly stub that follows the runtime
1601 // calling convention. So it's best to align the inputs accordingly.
1602 InvokeRuntimeCallingConvention calling_convention;
1603 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1604 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1605 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1606 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1607
1608 // Need a temp for slow-path codepoint compare, and need to send start-index=0.
1609 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1610}
1611
1612void IntrinsicCodeGeneratorMIPS64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001613 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Chris Larsen9701c2e2015-09-04 17:22:47 -07001614}
1615
1616// int java.lang.String.indexOf(int ch, int fromIndex)
1617void IntrinsicLocationsBuilderMIPS64::VisitStringIndexOfAfter(HInvoke* invoke) {
1618 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1619 LocationSummary::kCall,
1620 kIntrinsified);
1621 // We have a hand-crafted assembly stub that follows the runtime
1622 // calling convention. So it's best to align the inputs accordingly.
1623 InvokeRuntimeCallingConvention calling_convention;
1624 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1625 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1626 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1627 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1628 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1629}
1630
1631void IntrinsicCodeGeneratorMIPS64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001632 GenerateStringIndexOf(
1633 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Chris Larsen9701c2e2015-09-04 17:22:47 -07001634}
1635
1636// java.lang.String.String(byte[] bytes)
1637void IntrinsicLocationsBuilderMIPS64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1638 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1639 LocationSummary::kCall,
1640 kIntrinsified);
1641 InvokeRuntimeCallingConvention calling_convention;
1642 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1643 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1644 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1645 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1646 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1647 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1648}
1649
1650void IntrinsicCodeGeneratorMIPS64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1651 Mips64Assembler* assembler = GetAssembler();
1652 LocationSummary* locations = invoke->GetLocations();
1653
1654 GpuRegister byte_array = locations->InAt(0).AsRegister<GpuRegister>();
1655 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1656 codegen_->AddSlowPath(slow_path);
1657 __ Beqzc(byte_array, slow_path->GetEntryLabel());
1658
1659 __ LoadFromOffset(kLoadDoubleword,
1660 TMP,
1661 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001662 QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize,
1663 pAllocStringFromBytes).Int32Value());
Chris Larsen9701c2e2015-09-04 17:22:47 -07001664 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1665 __ Jalr(TMP);
1666 __ Nop();
1667 __ Bind(slow_path->GetExitLabel());
1668}
1669
1670// java.lang.String.String(char[] value)
1671void IntrinsicLocationsBuilderMIPS64::VisitStringNewStringFromChars(HInvoke* invoke) {
1672 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1673 LocationSummary::kCall,
1674 kIntrinsified);
1675 InvokeRuntimeCallingConvention calling_convention;
1676 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1677 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1678 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1679 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1680 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1681}
1682
1683void IntrinsicCodeGeneratorMIPS64::VisitStringNewStringFromChars(HInvoke* invoke) {
1684 Mips64Assembler* assembler = GetAssembler();
1685
1686 __ LoadFromOffset(kLoadDoubleword,
1687 TMP,
1688 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001689 QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize,
1690 pAllocStringFromChars).Int32Value());
Chris Larsen9701c2e2015-09-04 17:22:47 -07001691 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1692 __ Jalr(TMP);
1693 __ Nop();
1694}
1695
1696// java.lang.String.String(String original)
1697void IntrinsicLocationsBuilderMIPS64::VisitStringNewStringFromString(HInvoke* invoke) {
1698 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1699 LocationSummary::kCall,
1700 kIntrinsified);
1701 InvokeRuntimeCallingConvention calling_convention;
1702 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1703 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1704 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1705 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1706 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1707}
1708
1709void IntrinsicCodeGeneratorMIPS64::VisitStringNewStringFromString(HInvoke* invoke) {
1710 Mips64Assembler* assembler = GetAssembler();
1711 LocationSummary* locations = invoke->GetLocations();
1712
1713 GpuRegister string_to_copy = locations->InAt(0).AsRegister<GpuRegister>();
1714 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1715 codegen_->AddSlowPath(slow_path);
1716 __ Beqzc(string_to_copy, slow_path->GetEntryLabel());
1717
1718 __ LoadFromOffset(kLoadDoubleword,
1719 TMP,
1720 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001721 QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize,
1722 pAllocStringFromString).Int32Value());
Chris Larsen9701c2e2015-09-04 17:22:47 -07001723 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1724 __ Jalr(TMP);
1725 __ Nop();
1726 __ Bind(slow_path->GetExitLabel());
1727}
1728
Chris Larsen3039e382015-08-26 07:54:08 -07001729// Unimplemented intrinsics.
1730
1731#define UNIMPLEMENTED_INTRINSIC(Name) \
1732void IntrinsicLocationsBuilderMIPS64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1733} \
1734void IntrinsicCodeGeneratorMIPS64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1735}
1736
Aart Bik3f67e692016-01-15 14:35:12 -08001737UNIMPLEMENTED_INTRINSIC(IntegerBitCount)
1738UNIMPLEMENTED_INTRINSIC(LongBitCount)
1739
Chris Larsen3039e382015-08-26 07:54:08 -07001740UNIMPLEMENTED_INTRINSIC(MathRoundDouble)
1741UNIMPLEMENTED_INTRINSIC(MathRoundFloat)
Chris Larsen0b7ac982015-09-04 12:54:28 -07001742
Chris Larsen3039e382015-08-26 07:54:08 -07001743UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
1744UNIMPLEMENTED_INTRINSIC(StringGetCharsNoCheck)
1745UNIMPLEMENTED_INTRINSIC(SystemArrayCopyChar)
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001746UNIMPLEMENTED_INTRINSIC(SystemArrayCopy)
Chris Larsen3039e382015-08-26 07:54:08 -07001747
Mark Mendella4f12202015-08-06 15:23:34 -04001748UNIMPLEMENTED_INTRINSIC(MathCos)
1749UNIMPLEMENTED_INTRINSIC(MathSin)
1750UNIMPLEMENTED_INTRINSIC(MathAcos)
1751UNIMPLEMENTED_INTRINSIC(MathAsin)
1752UNIMPLEMENTED_INTRINSIC(MathAtan)
1753UNIMPLEMENTED_INTRINSIC(MathAtan2)
1754UNIMPLEMENTED_INTRINSIC(MathCbrt)
1755UNIMPLEMENTED_INTRINSIC(MathCosh)
1756UNIMPLEMENTED_INTRINSIC(MathExp)
1757UNIMPLEMENTED_INTRINSIC(MathExpm1)
1758UNIMPLEMENTED_INTRINSIC(MathHypot)
1759UNIMPLEMENTED_INTRINSIC(MathLog)
1760UNIMPLEMENTED_INTRINSIC(MathLog10)
1761UNIMPLEMENTED_INTRINSIC(MathNextAfter)
1762UNIMPLEMENTED_INTRINSIC(MathSinh)
1763UNIMPLEMENTED_INTRINSIC(MathTan)
1764UNIMPLEMENTED_INTRINSIC(MathTanh)
1765
Aart Bik59c94542016-01-25 14:20:58 -08001766UNIMPLEMENTED_INTRINSIC(FloatIsInfinite)
1767UNIMPLEMENTED_INTRINSIC(DoubleIsInfinite)
Aart Bik59c94542016-01-25 14:20:58 -08001768
Aart Bik59c94542016-01-25 14:20:58 -08001769UNIMPLEMENTED_INTRINSIC(IntegerHighestOneBit)
1770UNIMPLEMENTED_INTRINSIC(LongHighestOneBit)
1771UNIMPLEMENTED_INTRINSIC(IntegerLowestOneBit)
1772UNIMPLEMENTED_INTRINSIC(LongLowestOneBit)
Aart Bika19616e2016-02-01 18:57:58 -08001773
1774// Handled as HIR instructions.
Aart Bik75a38b22016-02-17 10:41:50 -08001775UNIMPLEMENTED_INTRINSIC(FloatIsNaN)
1776UNIMPLEMENTED_INTRINSIC(DoubleIsNaN)
Aart Bika19616e2016-02-01 18:57:58 -08001777UNIMPLEMENTED_INTRINSIC(IntegerCompare)
1778UNIMPLEMENTED_INTRINSIC(LongCompare)
Aart Bik59c94542016-01-25 14:20:58 -08001779UNIMPLEMENTED_INTRINSIC(IntegerSignum)
1780UNIMPLEMENTED_INTRINSIC(LongSignum)
1781
Chris Larsen3039e382015-08-26 07:54:08 -07001782#undef UNIMPLEMENTED_INTRINSIC
1783
1784#undef __
1785
1786} // namespace mips64
1787} // namespace art