blob: 0b520376bc3e2bc14462ebff51e06a3bdd5c3636 [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
343static void GenReverse(LocationSummary* locations,
344 Primitive::Type type,
345 Mips64Assembler* assembler) {
346 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
347
348 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
349 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
350
351 if (type == Primitive::kPrimInt) {
352 __ Rotr(out, in, 16);
353 __ Wsbh(out, out);
354 __ Bitswap(out, out);
355 } else {
356 __ Dsbh(out, in);
357 __ Dshd(out, out);
358 __ Dbitswap(out, out);
359 }
360}
361
362// int java.lang.Integer.reverse(int)
363void IntrinsicLocationsBuilderMIPS64::VisitIntegerReverse(HInvoke* invoke) {
364 CreateIntToIntLocations(arena_, invoke);
365}
366
367void IntrinsicCodeGeneratorMIPS64::VisitIntegerReverse(HInvoke* invoke) {
368 GenReverse(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
369}
370
371// long java.lang.Long.reverse(long)
372void IntrinsicLocationsBuilderMIPS64::VisitLongReverse(HInvoke* invoke) {
373 CreateIntToIntLocations(arena_, invoke);
374}
375
376void IntrinsicCodeGeneratorMIPS64::VisitLongReverse(HInvoke* invoke) {
377 GenReverse(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
378}
379
Chris Larsen0b7ac982015-09-04 12:54:28 -0700380static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
381 LocationSummary* locations = new (arena) LocationSummary(invoke,
382 LocationSummary::kNoCall,
383 kIntrinsified);
384 locations->SetInAt(0, Location::RequiresFpuRegister());
385 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
386}
387
Chris Larsen7fda7852016-04-21 16:00:36 -0700388static void GenBitCount(LocationSummary* locations,
389 const Primitive::Type type,
390 Mips64Assembler* assembler) {
391 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
392 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
393
394 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
395
396 // https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
397 //
398 // A generalization of the best bit counting method to integers of
399 // bit-widths up to 128 (parameterized by type T) is this:
400 //
401 // v = v - ((v >> 1) & (T)~(T)0/3); // temp
402 // v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); // temp
403 // v = (v + (v >> 4)) & (T)~(T)0/255*15; // temp
404 // c = (T)(v * ((T)~(T)0/255)) >> (sizeof(T) - 1) * BITS_PER_BYTE; // count
405 //
406 // For comparison, for 32-bit quantities, this algorithm can be executed
407 // using 20 MIPS instructions (the calls to LoadConst32() generate two
408 // machine instructions each for the values being used in this algorithm).
409 // A(n unrolled) loop-based algorithm requires 25 instructions.
410 //
411 // For a 64-bit operand this can be performed in 24 instructions compared
412 // to a(n unrolled) loop based algorithm which requires 38 instructions.
413 //
414 // There are algorithms which are faster in the cases where very few
415 // bits are set but the algorithm here attempts to minimize the total
416 // number of instructions executed even when a large number of bits
417 // are set.
418
419 if (type == Primitive::kPrimInt) {
420 __ Srl(TMP, in, 1);
421 __ LoadConst32(AT, 0x55555555);
422 __ And(TMP, TMP, AT);
423 __ Subu(TMP, in, TMP);
424 __ LoadConst32(AT, 0x33333333);
425 __ And(out, TMP, AT);
426 __ Srl(TMP, TMP, 2);
427 __ And(TMP, TMP, AT);
428 __ Addu(TMP, out, TMP);
429 __ Srl(out, TMP, 4);
430 __ Addu(out, out, TMP);
431 __ LoadConst32(AT, 0x0F0F0F0F);
432 __ And(out, out, AT);
433 __ LoadConst32(TMP, 0x01010101);
434 __ MulR6(out, out, TMP);
435 __ Srl(out, out, 24);
436 } else if (type == Primitive::kPrimLong) {
437 __ Dsrl(TMP, in, 1);
438 __ LoadConst64(AT, 0x5555555555555555L);
439 __ And(TMP, TMP, AT);
440 __ Dsubu(TMP, in, TMP);
441 __ LoadConst64(AT, 0x3333333333333333L);
442 __ And(out, TMP, AT);
443 __ Dsrl(TMP, TMP, 2);
444 __ And(TMP, TMP, AT);
445 __ Daddu(TMP, out, TMP);
446 __ Dsrl(out, TMP, 4);
447 __ Daddu(out, out, TMP);
448 __ LoadConst64(AT, 0x0F0F0F0F0F0F0F0FL);
449 __ And(out, out, AT);
450 __ LoadConst64(TMP, 0x0101010101010101L);
451 __ Dmul(out, out, TMP);
452 __ Dsrl32(out, out, 24);
453 }
454}
455
456// int java.lang.Integer.bitCount(int)
457void IntrinsicLocationsBuilderMIPS64::VisitIntegerBitCount(HInvoke* invoke) {
458 CreateIntToIntLocations(arena_, invoke);
459}
460
461void IntrinsicCodeGeneratorMIPS64::VisitIntegerBitCount(HInvoke* invoke) {
462 GenBitCount(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
463}
464
465// int java.lang.Long.bitCount(long)
466void IntrinsicLocationsBuilderMIPS64::VisitLongBitCount(HInvoke* invoke) {
467 CreateIntToIntLocations(arena_, invoke);
468}
469
470void IntrinsicCodeGeneratorMIPS64::VisitLongBitCount(HInvoke* invoke) {
471 GenBitCount(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
472}
473
Chris Larsen0b7ac982015-09-04 12:54:28 -0700474static void MathAbsFP(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
475 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
476 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
477
478 if (is64bit) {
479 __ AbsD(out, in);
480 } else {
481 __ AbsS(out, in);
482 }
483}
484
485// double java.lang.Math.abs(double)
486void IntrinsicLocationsBuilderMIPS64::VisitMathAbsDouble(HInvoke* invoke) {
487 CreateFPToFPLocations(arena_, invoke);
488}
489
490void IntrinsicCodeGeneratorMIPS64::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000491 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700492}
493
494// float java.lang.Math.abs(float)
495void IntrinsicLocationsBuilderMIPS64::VisitMathAbsFloat(HInvoke* invoke) {
496 CreateFPToFPLocations(arena_, invoke);
497}
498
499void IntrinsicCodeGeneratorMIPS64::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000500 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700501}
502
503static void CreateIntToInt(ArenaAllocator* arena, HInvoke* invoke) {
504 LocationSummary* locations = new (arena) LocationSummary(invoke,
505 LocationSummary::kNoCall,
506 kIntrinsified);
507 locations->SetInAt(0, Location::RequiresRegister());
508 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
509}
510
511static void GenAbsInteger(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
512 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
513 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
514
515 if (is64bit) {
516 __ Dsra32(AT, in, 31);
517 __ Xor(out, in, AT);
518 __ Dsubu(out, out, AT);
519 } else {
520 __ Sra(AT, in, 31);
521 __ Xor(out, in, AT);
522 __ Subu(out, out, AT);
523 }
524}
525
526// int java.lang.Math.abs(int)
527void IntrinsicLocationsBuilderMIPS64::VisitMathAbsInt(HInvoke* invoke) {
528 CreateIntToInt(arena_, invoke);
529}
530
531void IntrinsicCodeGeneratorMIPS64::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000532 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700533}
534
535// long java.lang.Math.abs(long)
536void IntrinsicLocationsBuilderMIPS64::VisitMathAbsLong(HInvoke* invoke) {
537 CreateIntToInt(arena_, invoke);
538}
539
540void IntrinsicCodeGeneratorMIPS64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000541 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700542}
543
544static void GenMinMaxFP(LocationSummary* locations,
545 bool is_min,
Chris Larsenb74353a2015-11-20 09:07:09 -0800546 Primitive::Type type,
Chris Larsen0b7ac982015-09-04 12:54:28 -0700547 Mips64Assembler* assembler) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800548 FpuRegister a = locations->InAt(0).AsFpuRegister<FpuRegister>();
549 FpuRegister b = locations->InAt(1).AsFpuRegister<FpuRegister>();
Chris Larsen0b7ac982015-09-04 12:54:28 -0700550 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
551
Chris Larsenb74353a2015-11-20 09:07:09 -0800552 Mips64Label noNaNs;
553 Mips64Label done;
554 FpuRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
555
556 // When Java computes min/max it prefers a NaN to a number; the
557 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
558 // the inputs is a NaN and the other is a valid number, the MIPS
559 // instruction will return the number; Java wants the NaN value
560 // returned. This is why there is extra logic preceding the use of
561 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
562 // NaN, return the NaN, otherwise return the min/max.
563 if (type == Primitive::kPrimDouble) {
564 __ CmpUnD(FTMP, a, b);
565 __ Bc1eqz(FTMP, &noNaNs);
566
567 // One of the inputs is a NaN
568 __ CmpEqD(ftmp, a, a);
569 // If a == a then b is the NaN, otherwise a is the NaN.
570 __ SelD(ftmp, a, b);
571
572 if (ftmp != out) {
573 __ MovD(out, ftmp);
574 }
575
576 __ Bc(&done);
577
578 __ Bind(&noNaNs);
579
Chris Larsen0b7ac982015-09-04 12:54:28 -0700580 if (is_min) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800581 __ MinD(out, a, b);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700582 } else {
Chris Larsenb74353a2015-11-20 09:07:09 -0800583 __ MaxD(out, a, b);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700584 }
585 } else {
Chris Larsenb74353a2015-11-20 09:07:09 -0800586 DCHECK_EQ(type, Primitive::kPrimFloat);
587 __ CmpUnS(FTMP, a, b);
588 __ Bc1eqz(FTMP, &noNaNs);
589
590 // One of the inputs is a NaN
591 __ CmpEqS(ftmp, a, a);
592 // If a == a then b is the NaN, otherwise a is the NaN.
593 __ SelS(ftmp, a, b);
594
595 if (ftmp != out) {
596 __ MovS(out, ftmp);
597 }
598
599 __ Bc(&done);
600
601 __ Bind(&noNaNs);
602
Chris Larsen0b7ac982015-09-04 12:54:28 -0700603 if (is_min) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800604 __ MinS(out, a, b);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700605 } else {
Chris Larsenb74353a2015-11-20 09:07:09 -0800606 __ MaxS(out, a, b);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700607 }
608 }
Chris Larsenb74353a2015-11-20 09:07:09 -0800609
610 __ Bind(&done);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700611}
612
613static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
614 LocationSummary* locations = new (arena) LocationSummary(invoke,
615 LocationSummary::kNoCall,
616 kIntrinsified);
617 locations->SetInAt(0, Location::RequiresFpuRegister());
618 locations->SetInAt(1, Location::RequiresFpuRegister());
619 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
620}
621
622// double java.lang.Math.min(double, double)
623void IntrinsicLocationsBuilderMIPS64::VisitMathMinDoubleDouble(HInvoke* invoke) {
624 CreateFPFPToFPLocations(arena_, invoke);
625}
626
627void IntrinsicCodeGeneratorMIPS64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800628 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, Primitive::kPrimDouble, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700629}
630
631// float java.lang.Math.min(float, float)
632void IntrinsicLocationsBuilderMIPS64::VisitMathMinFloatFloat(HInvoke* invoke) {
633 CreateFPFPToFPLocations(arena_, invoke);
634}
635
636void IntrinsicCodeGeneratorMIPS64::VisitMathMinFloatFloat(HInvoke* invoke) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800637 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, Primitive::kPrimFloat, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700638}
639
640// double java.lang.Math.max(double, double)
641void IntrinsicLocationsBuilderMIPS64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
642 CreateFPFPToFPLocations(arena_, invoke);
643}
644
645void IntrinsicCodeGeneratorMIPS64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800646 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, Primitive::kPrimDouble, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700647}
648
649// float java.lang.Math.max(float, float)
650void IntrinsicLocationsBuilderMIPS64::VisitMathMaxFloatFloat(HInvoke* invoke) {
651 CreateFPFPToFPLocations(arena_, invoke);
652}
653
654void IntrinsicCodeGeneratorMIPS64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800655 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, Primitive::kPrimFloat, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700656}
657
658static void GenMinMax(LocationSummary* locations,
659 bool is_min,
660 Mips64Assembler* assembler) {
661 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
662 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
663 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
664
Chris Larsenb74353a2015-11-20 09:07:09 -0800665 if (lhs == rhs) {
666 if (out != lhs) {
667 __ Move(out, lhs);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700668 }
669 } else {
Chris Larsenb74353a2015-11-20 09:07:09 -0800670 // Some architectures, such as ARM and MIPS (prior to r6), have a
671 // conditional move instruction which only changes the target
672 // (output) register if the condition is true (MIPS prior to r6 had
673 // MOVF, MOVT, and MOVZ). The SELEQZ and SELNEZ instructions always
674 // change the target (output) register. If the condition is true the
675 // output register gets the contents of the "rs" register; otherwise,
676 // the output register is set to zero. One consequence of this is
677 // that to implement something like "rd = c==0 ? rs : rt" MIPS64r6
678 // needs to use a pair of SELEQZ/SELNEZ instructions. After
679 // executing this pair of instructions one of the output registers
680 // from the pair will necessarily contain zero. Then the code ORs the
681 // output registers from the SELEQZ/SELNEZ instructions to get the
682 // final result.
683 //
684 // The initial test to see if the output register is same as the
685 // first input register is needed to make sure that value in the
686 // first input register isn't clobbered before we've finished
687 // computing the output value. The logic in the corresponding else
688 // clause performs the same task but makes sure the second input
689 // register isn't clobbered in the event that it's the same register
690 // as the output register; the else clause also handles the case
691 // where the output register is distinct from both the first, and the
692 // second input registers.
693 if (out == lhs) {
694 __ Slt(AT, rhs, lhs);
695 if (is_min) {
696 __ Seleqz(out, lhs, AT);
697 __ Selnez(AT, rhs, AT);
698 } else {
699 __ Selnez(out, lhs, AT);
700 __ Seleqz(AT, rhs, AT);
701 }
Chris Larsen0b7ac982015-09-04 12:54:28 -0700702 } else {
Chris Larsenb74353a2015-11-20 09:07:09 -0800703 __ Slt(AT, lhs, rhs);
704 if (is_min) {
705 __ Seleqz(out, rhs, AT);
706 __ Selnez(AT, lhs, AT);
707 } else {
708 __ Selnez(out, rhs, AT);
709 __ Seleqz(AT, lhs, AT);
710 }
Chris Larsen0b7ac982015-09-04 12:54:28 -0700711 }
Chris Larsenb74353a2015-11-20 09:07:09 -0800712 __ Or(out, out, AT);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700713 }
Chris Larsen0b7ac982015-09-04 12:54:28 -0700714}
715
716static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
717 LocationSummary* locations = new (arena) LocationSummary(invoke,
718 LocationSummary::kNoCall,
719 kIntrinsified);
720 locations->SetInAt(0, Location::RequiresRegister());
721 locations->SetInAt(1, Location::RequiresRegister());
722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
723}
724
725// int java.lang.Math.min(int, int)
726void IntrinsicLocationsBuilderMIPS64::VisitMathMinIntInt(HInvoke* invoke) {
727 CreateIntIntToIntLocations(arena_, invoke);
728}
729
730void IntrinsicCodeGeneratorMIPS64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000731 GenMinMax(invoke->GetLocations(), /* is_min */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700732}
733
734// long java.lang.Math.min(long, long)
735void IntrinsicLocationsBuilderMIPS64::VisitMathMinLongLong(HInvoke* invoke) {
736 CreateIntIntToIntLocations(arena_, invoke);
737}
738
739void IntrinsicCodeGeneratorMIPS64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000740 GenMinMax(invoke->GetLocations(), /* is_min */ true, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700741}
742
743// int java.lang.Math.max(int, int)
744void IntrinsicLocationsBuilderMIPS64::VisitMathMaxIntInt(HInvoke* invoke) {
745 CreateIntIntToIntLocations(arena_, invoke);
746}
747
748void IntrinsicCodeGeneratorMIPS64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000749 GenMinMax(invoke->GetLocations(), /* is_min */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700750}
751
752// long java.lang.Math.max(long, long)
753void IntrinsicLocationsBuilderMIPS64::VisitMathMaxLongLong(HInvoke* invoke) {
754 CreateIntIntToIntLocations(arena_, invoke);
755}
756
757void IntrinsicCodeGeneratorMIPS64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000758 GenMinMax(invoke->GetLocations(), /* is_min */ false, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700759}
760
761// double java.lang.Math.sqrt(double)
762void IntrinsicLocationsBuilderMIPS64::VisitMathSqrt(HInvoke* invoke) {
763 CreateFPToFPLocations(arena_, invoke);
764}
765
766void IntrinsicCodeGeneratorMIPS64::VisitMathSqrt(HInvoke* invoke) {
767 LocationSummary* locations = invoke->GetLocations();
768 Mips64Assembler* assembler = GetAssembler();
769 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
770 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
771
772 __ SqrtD(out, in);
773}
774
Chris Larsen81284372015-10-21 15:28:53 -0700775static void CreateFPToFP(ArenaAllocator* arena,
776 HInvoke* invoke,
777 Location::OutputOverlap overlaps = Location::kOutputOverlap) {
Chris Larsen0b7ac982015-09-04 12:54:28 -0700778 LocationSummary* locations = new (arena) LocationSummary(invoke,
779 LocationSummary::kNoCall,
780 kIntrinsified);
781 locations->SetInAt(0, Location::RequiresFpuRegister());
Chris Larsen81284372015-10-21 15:28:53 -0700782 locations->SetOut(Location::RequiresFpuRegister(), overlaps);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700783}
784
785// double java.lang.Math.rint(double)
786void IntrinsicLocationsBuilderMIPS64::VisitMathRint(HInvoke* invoke) {
Chris Larsen81284372015-10-21 15:28:53 -0700787 CreateFPToFP(arena_, invoke, Location::kNoOutputOverlap);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700788}
789
790void IntrinsicCodeGeneratorMIPS64::VisitMathRint(HInvoke* invoke) {
791 LocationSummary* locations = invoke->GetLocations();
792 Mips64Assembler* assembler = GetAssembler();
793 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
794 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
795
796 __ RintD(out, in);
797}
798
799// double java.lang.Math.floor(double)
800void IntrinsicLocationsBuilderMIPS64::VisitMathFloor(HInvoke* invoke) {
801 CreateFPToFP(arena_, invoke);
802}
803
Chris Larsen14500822015-10-01 11:35:18 -0700804const constexpr uint16_t kFPLeaveUnchanged = kPositiveZero |
805 kPositiveInfinity |
806 kNegativeZero |
807 kNegativeInfinity |
808 kQuietNaN |
809 kSignalingNaN;
Chris Larsen0b7ac982015-09-04 12:54:28 -0700810
Chris Larsen81284372015-10-21 15:28:53 -0700811enum FloatRoundingMode {
812 kFloor,
813 kCeil,
814};
815
816static void GenRoundingMode(LocationSummary* locations,
817 FloatRoundingMode mode,
818 Mips64Assembler* assembler) {
Chris Larsen0b7ac982015-09-04 12:54:28 -0700819 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
820 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
821
Chris Larsen81284372015-10-21 15:28:53 -0700822 DCHECK_NE(in, out);
823
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700824 Mips64Label done;
Chris Larsen0b7ac982015-09-04 12:54:28 -0700825
Chris Larsen81284372015-10-21 15:28:53 -0700826 // double floor/ceil(double in) {
Chris Larsen0b7ac982015-09-04 12:54:28 -0700827 // if in.isNaN || in.isInfinite || in.isZero {
828 // return in;
829 // }
830 __ ClassD(out, in);
831 __ Dmfc1(AT, out);
Chris Larsen14500822015-10-01 11:35:18 -0700832 __ Andi(AT, AT, kFPLeaveUnchanged); // +0.0 | +Inf | -0.0 | -Inf | qNaN | sNaN
Chris Larsen0b7ac982015-09-04 12:54:28 -0700833 __ MovD(out, in);
834 __ Bnezc(AT, &done);
835
Chris Larsen81284372015-10-21 15:28:53 -0700836 // Long outLong = floor/ceil(in);
Goran Jakovljevic716d0732017-04-07 11:18:59 +0200837 // if (outLong == Long.MAX_VALUE) || (outLong == Long.MIN_VALUE) {
Chris Larsen81284372015-10-21 15:28:53 -0700838 // // floor()/ceil() has almost certainly returned a value
839 // // which can't be successfully represented as a signed
840 // // 64-bit number. Java expects that the input value will
841 // // be returned in these cases.
842 // // There is also a small probability that floor(in)/ceil(in)
843 // // correctly truncates/rounds up the input value to
Goran Jakovljevic716d0732017-04-07 11:18:59 +0200844 // // Long.MAX_VALUE or Long.MIN_VALUE. In these cases, this
845 // // exception handling code still does the correct thing.
Chris Larsen0b7ac982015-09-04 12:54:28 -0700846 // return in;
847 // }
Chris Larsen81284372015-10-21 15:28:53 -0700848 if (mode == kFloor) {
849 __ FloorLD(out, in);
850 } else if (mode == kCeil) {
851 __ CeilLD(out, in);
852 }
Chris Larsen0b7ac982015-09-04 12:54:28 -0700853 __ Dmfc1(AT, out);
854 __ MovD(out, in);
Goran Jakovljevic716d0732017-04-07 11:18:59 +0200855 __ Daddiu(TMP, AT, 1);
856 __ Dati(TMP, 0x8000); // TMP = AT + 0x8000 0000 0000 0001
857 // or AT - 0x7FFF FFFF FFFF FFFF.
858 // IOW, TMP = 1 if AT = Long.MIN_VALUE
859 // or TMP = 0 if AT = Long.MAX_VALUE.
860 __ Dsrl(TMP, TMP, 1); // TMP = 0 if AT = Long.MIN_VALUE
861 // or AT = Long.MAX_VALUE.
862 __ Beqzc(TMP, &done);
Chris Larsen0b7ac982015-09-04 12:54:28 -0700863
864 // double out = outLong;
865 // return out;
866 __ Dmtc1(AT, out);
867 __ Cvtdl(out, out);
868 __ Bind(&done);
869 // }
870}
871
Chris Larsen81284372015-10-21 15:28:53 -0700872void IntrinsicCodeGeneratorMIPS64::VisitMathFloor(HInvoke* invoke) {
873 GenRoundingMode(invoke->GetLocations(), kFloor, GetAssembler());
874}
875
Chris Larsen0b7ac982015-09-04 12:54:28 -0700876// double java.lang.Math.ceil(double)
877void IntrinsicLocationsBuilderMIPS64::VisitMathCeil(HInvoke* invoke) {
878 CreateFPToFP(arena_, invoke);
879}
880
881void IntrinsicCodeGeneratorMIPS64::VisitMathCeil(HInvoke* invoke) {
Chris Larsen81284372015-10-21 15:28:53 -0700882 GenRoundingMode(invoke->GetLocations(), kCeil, GetAssembler());
Chris Larsen0b7ac982015-09-04 12:54:28 -0700883}
884
Chris Larsen7adaab02016-04-21 14:49:20 -0700885static void GenRound(LocationSummary* locations, Mips64Assembler* assembler, Primitive::Type type) {
886 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
887 FpuRegister half = locations->GetTemp(0).AsFpuRegister<FpuRegister>();
888 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
889
890 DCHECK(type == Primitive::kPrimFloat || type == Primitive::kPrimDouble);
891
892 Mips64Label done;
893 Mips64Label finite;
894 Mips64Label add;
895
896 // if (in.isNaN) {
897 // return 0;
898 // }
899 //
900 // out = floor(in);
901 //
902 // /*
903 // * TODO: Amend this code when emulator FCSR.NAN2008=1 bug is fixed.
904 // *
905 // * Starting with MIPSR6, which always sets FCSR.NAN2008=1, negative
906 // * numbers which are too large to be represented in a 32-/64-bit
907 // * signed integer will be processed by floor.X.Y to output
908 // * Integer.MIN_VALUE/Long.MIN_VALUE, and will no longer be
909 // * processed by this "if" statement.
910 // *
911 // * However, this bug in the 64-bit MIPS emulator causes the
912 // * behavior of floor.X.Y to be the same as pre-R6 implementations
913 // * of MIPS64. When that bug is fixed this logic should be amended.
914 // */
915 // if (out == MAX_VALUE) {
916 // TMP = (in < 0.0) ? 1 : 0;
917 // /*
918 // * If TMP is 1, then adding it to out will wrap its value from
919 // * MAX_VALUE to MIN_VALUE.
920 // */
921 // return out += TMP;
922 // }
923 //
924 // /*
925 // * For negative values not handled by the previous "if" statement the
926 // * test here will correctly set the value of TMP.
927 // */
928 // TMP = ((in - out) >= 0.5) ? 1 : 0;
929 // return out += TMP;
930
931 // Test for NaN.
932 if (type == Primitive::kPrimDouble) {
933 __ CmpUnD(FTMP, in, in);
934 } else {
935 __ CmpUnS(FTMP, in, in);
936 }
937
938 // Return zero for NaN.
939 __ Move(out, ZERO);
940 __ Bc1nez(FTMP, &done);
941
942 // out = floor(in);
943 if (type == Primitive::kPrimDouble) {
944 __ FloorLD(FTMP, in);
945 __ Dmfc1(out, FTMP);
946 } else {
947 __ FloorWS(FTMP, in);
948 __ Mfc1(out, FTMP);
949 }
950
951 // TMP = (out = java.lang.Integer.MAX_VALUE) ? 1 : 0;
952 if (type == Primitive::kPrimDouble) {
953 __ LoadConst64(AT, std::numeric_limits<int64_t>::max());
954 } else {
955 __ LoadConst32(AT, std::numeric_limits<int32_t>::max());
956 }
957 __ Bnec(AT, out, &finite);
958
959 if (type == Primitive::kPrimDouble) {
960 __ Dmtc1(ZERO, FTMP);
961 __ CmpLtD(FTMP, in, FTMP);
962 __ Dmfc1(AT, FTMP);
963 } else {
964 __ Mtc1(ZERO, FTMP);
965 __ CmpLtS(FTMP, in, FTMP);
966 __ Mfc1(AT, FTMP);
967 }
968
969 __ Bc(&add);
970
971 __ Bind(&finite);
972
973 // TMP = (0.5 <= (in - out)) ? -1 : 0;
974 if (type == Primitive::kPrimDouble) {
975 __ Cvtdl(FTMP, FTMP); // Convert output of floor.l.d back to "double".
976 __ LoadConst64(AT, bit_cast<int64_t, double>(0.5));
977 __ SubD(FTMP, in, FTMP);
978 __ Dmtc1(AT, half);
979 __ CmpLeD(FTMP, half, FTMP);
980 __ Dmfc1(AT, FTMP);
981 } else {
982 __ Cvtsw(FTMP, FTMP); // Convert output of floor.w.s back to "float".
983 __ LoadConst32(AT, bit_cast<int32_t, float>(0.5f));
984 __ SubS(FTMP, in, FTMP);
985 __ Mtc1(AT, half);
986 __ CmpLeS(FTMP, half, FTMP);
987 __ Mfc1(AT, FTMP);
988 }
989
990 __ Bind(&add);
991
992 // Return out -= TMP.
993 if (type == Primitive::kPrimDouble) {
994 __ Dsubu(out, out, AT);
995 } else {
996 __ Subu(out, out, AT);
997 }
998
999 __ Bind(&done);
1000}
1001
1002// int java.lang.Math.round(float)
1003void IntrinsicLocationsBuilderMIPS64::VisitMathRoundFloat(HInvoke* invoke) {
1004 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1005 LocationSummary::kNoCall,
1006 kIntrinsified);
1007 locations->SetInAt(0, Location::RequiresFpuRegister());
1008 locations->AddTemp(Location::RequiresFpuRegister());
1009 locations->SetOut(Location::RequiresRegister());
1010}
1011
1012void IntrinsicCodeGeneratorMIPS64::VisitMathRoundFloat(HInvoke* invoke) {
1013 GenRound(invoke->GetLocations(), GetAssembler(), Primitive::kPrimFloat);
1014}
1015
1016// long java.lang.Math.round(double)
1017void IntrinsicLocationsBuilderMIPS64::VisitMathRoundDouble(HInvoke* invoke) {
1018 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1019 LocationSummary::kNoCall,
1020 kIntrinsified);
1021 locations->SetInAt(0, Location::RequiresFpuRegister());
1022 locations->AddTemp(Location::RequiresFpuRegister());
1023 locations->SetOut(Location::RequiresRegister());
1024}
1025
1026void IntrinsicCodeGeneratorMIPS64::VisitMathRoundDouble(HInvoke* invoke) {
1027 GenRound(invoke->GetLocations(), GetAssembler(), Primitive::kPrimDouble);
1028}
1029
Chris Larsen70fb1f42015-09-04 10:15:27 -07001030// byte libcore.io.Memory.peekByte(long address)
1031void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekByte(HInvoke* invoke) {
1032 CreateIntToIntLocations(arena_, invoke);
1033}
1034
1035void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekByte(HInvoke* invoke) {
1036 Mips64Assembler* assembler = GetAssembler();
1037 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
1038 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
1039
1040 __ Lb(out, adr, 0);
1041}
1042
1043// short libcore.io.Memory.peekShort(long address)
1044void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1045 CreateIntToIntLocations(arena_, invoke);
1046}
1047
1048void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1049 Mips64Assembler* assembler = GetAssembler();
1050 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
1051 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
1052
1053 __ Lh(out, adr, 0);
1054}
1055
1056// int libcore.io.Memory.peekInt(long address)
1057void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1058 CreateIntToIntLocations(arena_, invoke);
1059}
1060
1061void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1062 Mips64Assembler* assembler = GetAssembler();
1063 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
1064 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
1065
1066 __ Lw(out, adr, 0);
1067}
1068
1069// long libcore.io.Memory.peekLong(long address)
1070void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1071 CreateIntToIntLocations(arena_, invoke);
1072}
1073
1074void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1075 Mips64Assembler* assembler = GetAssembler();
1076 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
1077 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
1078
1079 __ Ld(out, adr, 0);
1080}
1081
1082static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1083 LocationSummary* locations = new (arena) LocationSummary(invoke,
1084 LocationSummary::kNoCall,
1085 kIntrinsified);
1086 locations->SetInAt(0, Location::RequiresRegister());
1087 locations->SetInAt(1, Location::RequiresRegister());
1088}
1089
1090// void libcore.io.Memory.pokeByte(long address, byte value)
1091void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeByte(HInvoke* invoke) {
1092 CreateIntIntToVoidLocations(arena_, invoke);
1093}
1094
1095void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeByte(HInvoke* invoke) {
1096 Mips64Assembler* assembler = GetAssembler();
1097 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
1098 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
1099
1100 __ Sb(val, adr, 0);
1101}
1102
1103// void libcore.io.Memory.pokeShort(long address, short value)
1104void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1105 CreateIntIntToVoidLocations(arena_, invoke);
1106}
1107
1108void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1109 Mips64Assembler* assembler = GetAssembler();
1110 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
1111 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
1112
1113 __ Sh(val, adr, 0);
1114}
1115
1116// void libcore.io.Memory.pokeInt(long address, int value)
1117void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1118 CreateIntIntToVoidLocations(arena_, invoke);
1119}
1120
1121void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1122 Mips64Assembler* assembler = GetAssembler();
1123 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
1124 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
1125
1126 __ Sw(val, adr, 00);
1127}
1128
1129// void libcore.io.Memory.pokeLong(long address, long value)
1130void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1131 CreateIntIntToVoidLocations(arena_, invoke);
1132}
1133
1134void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1135 Mips64Assembler* assembler = GetAssembler();
1136 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
1137 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
1138
1139 __ Sd(val, adr, 0);
1140}
1141
Chris Larsen49e55392015-09-04 16:04:03 -07001142// Thread java.lang.Thread.currentThread()
1143void IntrinsicLocationsBuilderMIPS64::VisitThreadCurrentThread(HInvoke* invoke) {
1144 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1145 LocationSummary::kNoCall,
1146 kIntrinsified);
1147 locations->SetOut(Location::RequiresRegister());
1148}
1149
1150void IntrinsicCodeGeneratorMIPS64::VisitThreadCurrentThread(HInvoke* invoke) {
1151 Mips64Assembler* assembler = GetAssembler();
1152 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
1153
1154 __ LoadFromOffset(kLoadUnsignedWord,
1155 out,
1156 TR,
1157 Thread::PeerOffset<kMips64PointerSize>().Int32Value());
1158}
1159
Alexey Frunze15958152017-02-09 19:08:30 -08001160static void CreateIntIntIntToIntLocations(ArenaAllocator* arena,
1161 HInvoke* invoke,
1162 Primitive::Type type) {
1163 bool can_call = kEmitCompilerReadBarrier &&
1164 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1165 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Chris Larsen1360ada2015-09-04 23:38:16 -07001166 LocationSummary* locations = new (arena) LocationSummary(invoke,
Alexey Frunze15958152017-02-09 19:08:30 -08001167 (can_call
1168 ? LocationSummary::kCallOnSlowPath
1169 : LocationSummary::kNoCall),
Chris Larsen1360ada2015-09-04 23:38:16 -07001170 kIntrinsified);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001171 if (can_call && kUseBakerReadBarrier) {
1172 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1173 }
Chris Larsen1360ada2015-09-04 23:38:16 -07001174 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1175 locations->SetInAt(1, Location::RequiresRegister());
1176 locations->SetInAt(2, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08001177 locations->SetOut(Location::RequiresRegister(),
1178 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
1179 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1180 // We need a temporary register for the read barrier marking slow
1181 // path in InstructionCodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier.
1182 locations->AddTemp(Location::RequiresRegister());
1183 }
Chris Larsen1360ada2015-09-04 23:38:16 -07001184}
1185
Alexey Frunze15958152017-02-09 19:08:30 -08001186// Note that the caller must supply a properly aligned memory address.
1187// If they do not, the behavior is undefined (atomicity not guaranteed, exception may occur).
Chris Larsen1360ada2015-09-04 23:38:16 -07001188static void GenUnsafeGet(HInvoke* invoke,
1189 Primitive::Type type,
1190 bool is_volatile,
1191 CodeGeneratorMIPS64* codegen) {
1192 LocationSummary* locations = invoke->GetLocations();
1193 DCHECK((type == Primitive::kPrimInt) ||
1194 (type == Primitive::kPrimLong) ||
Alexey Frunze15958152017-02-09 19:08:30 -08001195 (type == Primitive::kPrimNot)) << type;
Chris Larsen1360ada2015-09-04 23:38:16 -07001196 Mips64Assembler* assembler = codegen->GetAssembler();
Alexey Frunze15958152017-02-09 19:08:30 -08001197 // Target register.
1198 Location trg_loc = locations->Out();
1199 GpuRegister trg = trg_loc.AsRegister<GpuRegister>();
Chris Larsen1360ada2015-09-04 23:38:16 -07001200 // Object pointer.
Alexey Frunze15958152017-02-09 19:08:30 -08001201 Location base_loc = locations->InAt(1);
1202 GpuRegister base = base_loc.AsRegister<GpuRegister>();
Chris Larsen1360ada2015-09-04 23:38:16 -07001203 // Long offset.
Alexey Frunze15958152017-02-09 19:08:30 -08001204 Location offset_loc = locations->InAt(2);
1205 GpuRegister offset = offset_loc.AsRegister<GpuRegister>();
Chris Larsen1360ada2015-09-04 23:38:16 -07001206
Alexey Frunze15958152017-02-09 19:08:30 -08001207 if (!(kEmitCompilerReadBarrier && kUseBakerReadBarrier && (type == Primitive::kPrimNot))) {
1208 __ Daddu(TMP, base, offset);
Chris Larsen1360ada2015-09-04 23:38:16 -07001209 }
Alexey Frunze15958152017-02-09 19:08:30 -08001210
Chris Larsen1360ada2015-09-04 23:38:16 -07001211 switch (type) {
Alexey Frunze15958152017-02-09 19:08:30 -08001212 case Primitive::kPrimLong:
1213 __ Ld(trg, TMP, 0);
1214 if (is_volatile) {
1215 __ Sync(0);
1216 }
1217 break;
1218
Chris Larsen1360ada2015-09-04 23:38:16 -07001219 case Primitive::kPrimInt:
1220 __ Lw(trg, TMP, 0);
Alexey Frunze15958152017-02-09 19:08:30 -08001221 if (is_volatile) {
1222 __ Sync(0);
1223 }
Chris Larsen1360ada2015-09-04 23:38:16 -07001224 break;
1225
1226 case Primitive::kPrimNot:
Alexey Frunze15958152017-02-09 19:08:30 -08001227 if (kEmitCompilerReadBarrier) {
1228 if (kUseBakerReadBarrier) {
1229 Location temp = locations->GetTemp(0);
1230 codegen->GenerateReferenceLoadWithBakerReadBarrier(invoke,
1231 trg_loc,
1232 base,
1233 /* offset */ 0U,
1234 /* index */ offset_loc,
1235 TIMES_1,
1236 temp,
1237 /* needs_null_check */ false);
1238 if (is_volatile) {
1239 __ Sync(0);
1240 }
1241 } else {
1242 __ Lwu(trg, TMP, 0);
1243 if (is_volatile) {
1244 __ Sync(0);
1245 }
1246 codegen->GenerateReadBarrierSlow(invoke,
1247 trg_loc,
1248 trg_loc,
1249 base_loc,
1250 /* offset */ 0U,
1251 /* index */ offset_loc);
1252 }
1253 } else {
1254 __ Lwu(trg, TMP, 0);
1255 if (is_volatile) {
1256 __ Sync(0);
1257 }
1258 __ MaybeUnpoisonHeapReference(trg);
1259 }
Chris Larsen1360ada2015-09-04 23:38:16 -07001260 break;
1261
1262 default:
1263 LOG(FATAL) << "Unsupported op size " << type;
1264 UNREACHABLE();
1265 }
1266}
1267
1268// int sun.misc.Unsafe.getInt(Object o, long offset)
1269void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGet(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001270 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Chris Larsen1360ada2015-09-04 23:38:16 -07001271}
1272
1273void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001274 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001275}
1276
1277// int sun.misc.Unsafe.getIntVolatile(Object o, long offset)
1278void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001279 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Chris Larsen1360ada2015-09-04 23:38:16 -07001280}
1281
1282void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001283 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001284}
1285
1286// long sun.misc.Unsafe.getLong(Object o, long offset)
1287void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetLong(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001288 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
Chris Larsen1360ada2015-09-04 23:38:16 -07001289}
1290
1291void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001292 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001293}
1294
1295// long sun.misc.Unsafe.getLongVolatile(Object o, long offset)
1296void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001297 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
Chris Larsen1360ada2015-09-04 23:38:16 -07001298}
1299
1300void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001301 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001302}
1303
1304// Object sun.misc.Unsafe.getObject(Object o, long offset)
1305void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetObject(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001306 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Chris Larsen1360ada2015-09-04 23:38:16 -07001307}
1308
1309void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001310 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001311}
1312
1313// Object sun.misc.Unsafe.getObjectVolatile(Object o, long offset)
1314void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001315 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Chris Larsen1360ada2015-09-04 23:38:16 -07001316}
1317
1318void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001319 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001320}
1321
1322static void CreateIntIntIntIntToVoid(ArenaAllocator* arena, HInvoke* invoke) {
1323 LocationSummary* locations = new (arena) LocationSummary(invoke,
1324 LocationSummary::kNoCall,
1325 kIntrinsified);
1326 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1327 locations->SetInAt(1, Location::RequiresRegister());
1328 locations->SetInAt(2, Location::RequiresRegister());
1329 locations->SetInAt(3, Location::RequiresRegister());
1330}
1331
Alexey Frunze15958152017-02-09 19:08:30 -08001332// Note that the caller must supply a properly aligned memory address.
1333// If they do not, the behavior is undefined (atomicity not guaranteed, exception may occur).
Chris Larsen1360ada2015-09-04 23:38:16 -07001334static void GenUnsafePut(LocationSummary* locations,
1335 Primitive::Type type,
1336 bool is_volatile,
1337 bool is_ordered,
1338 CodeGeneratorMIPS64* codegen) {
1339 DCHECK((type == Primitive::kPrimInt) ||
1340 (type == Primitive::kPrimLong) ||
1341 (type == Primitive::kPrimNot));
1342 Mips64Assembler* assembler = codegen->GetAssembler();
1343 // Object pointer.
1344 GpuRegister base = locations->InAt(1).AsRegister<GpuRegister>();
1345 // Long offset.
1346 GpuRegister offset = locations->InAt(2).AsRegister<GpuRegister>();
1347 GpuRegister value = locations->InAt(3).AsRegister<GpuRegister>();
1348
1349 __ Daddu(TMP, base, offset);
1350 if (is_volatile || is_ordered) {
1351 __ Sync(0);
1352 }
1353 switch (type) {
1354 case Primitive::kPrimInt:
1355 case Primitive::kPrimNot:
Alexey Frunzec061de12017-02-14 13:27:23 -08001356 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1357 __ PoisonHeapReference(AT, value);
1358 __ Sw(AT, TMP, 0);
1359 } else {
1360 __ Sw(value, TMP, 0);
1361 }
Chris Larsen1360ada2015-09-04 23:38:16 -07001362 break;
1363
1364 case Primitive::kPrimLong:
1365 __ Sd(value, TMP, 0);
1366 break;
1367
1368 default:
1369 LOG(FATAL) << "Unsupported op size " << type;
1370 UNREACHABLE();
1371 }
1372 if (is_volatile) {
1373 __ Sync(0);
1374 }
1375
1376 if (type == Primitive::kPrimNot) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001377 bool value_can_be_null = true; // TODO: Worth finding out this information?
1378 codegen->MarkGCCard(base, value, value_can_be_null);
Chris Larsen1360ada2015-09-04 23:38:16 -07001379 }
1380}
1381
1382// void sun.misc.Unsafe.putInt(Object o, long offset, int x)
1383void IntrinsicLocationsBuilderMIPS64::VisitUnsafePut(HInvoke* invoke) {
1384 CreateIntIntIntIntToVoid(arena_, invoke);
1385}
1386
1387void IntrinsicCodeGeneratorMIPS64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001388 GenUnsafePut(invoke->GetLocations(),
1389 Primitive::kPrimInt,
1390 /* is_volatile */ false,
1391 /* is_ordered */ false,
1392 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001393}
1394
1395// void sun.misc.Unsafe.putOrderedInt(Object o, long offset, int x)
1396void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutOrdered(HInvoke* invoke) {
1397 CreateIntIntIntIntToVoid(arena_, invoke);
1398}
1399
1400void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001401 GenUnsafePut(invoke->GetLocations(),
1402 Primitive::kPrimInt,
1403 /* is_volatile */ false,
1404 /* is_ordered */ true,
1405 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001406}
1407
1408// void sun.misc.Unsafe.putIntVolatile(Object o, long offset, int x)
1409void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutVolatile(HInvoke* invoke) {
1410 CreateIntIntIntIntToVoid(arena_, invoke);
1411}
1412
1413void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001414 GenUnsafePut(invoke->GetLocations(),
1415 Primitive::kPrimInt,
1416 /* is_volatile */ true,
1417 /* is_ordered */ false,
1418 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001419}
1420
1421// void sun.misc.Unsafe.putObject(Object o, long offset, Object x)
1422void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutObject(HInvoke* invoke) {
1423 CreateIntIntIntIntToVoid(arena_, invoke);
1424}
1425
1426void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001427 GenUnsafePut(invoke->GetLocations(),
1428 Primitive::kPrimNot,
1429 /* is_volatile */ false,
1430 /* is_ordered */ false,
1431 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001432}
1433
1434// void sun.misc.Unsafe.putOrderedObject(Object o, long offset, Object x)
1435void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1436 CreateIntIntIntIntToVoid(arena_, invoke);
1437}
1438
1439void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001440 GenUnsafePut(invoke->GetLocations(),
1441 Primitive::kPrimNot,
1442 /* is_volatile */ false,
1443 /* is_ordered */ true,
1444 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001445}
1446
1447// void sun.misc.Unsafe.putObjectVolatile(Object o, long offset, Object x)
1448void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1449 CreateIntIntIntIntToVoid(arena_, invoke);
1450}
1451
1452void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001453 GenUnsafePut(invoke->GetLocations(),
1454 Primitive::kPrimNot,
1455 /* is_volatile */ true,
1456 /* is_ordered */ false,
1457 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001458}
1459
1460// void sun.misc.Unsafe.putLong(Object o, long offset, long x)
1461void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutLong(HInvoke* invoke) {
1462 CreateIntIntIntIntToVoid(arena_, invoke);
1463}
1464
1465void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001466 GenUnsafePut(invoke->GetLocations(),
1467 Primitive::kPrimLong,
1468 /* is_volatile */ false,
1469 /* is_ordered */ false,
1470 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001471}
1472
1473// void sun.misc.Unsafe.putOrderedLong(Object o, long offset, long x)
1474void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1475 CreateIntIntIntIntToVoid(arena_, invoke);
1476}
1477
1478void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001479 GenUnsafePut(invoke->GetLocations(),
1480 Primitive::kPrimLong,
1481 /* is_volatile */ false,
1482 /* is_ordered */ true,
1483 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001484}
1485
1486// void sun.misc.Unsafe.putLongVolatile(Object o, long offset, long x)
1487void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1488 CreateIntIntIntIntToVoid(arena_, invoke);
1489}
1490
1491void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001492 GenUnsafePut(invoke->GetLocations(),
1493 Primitive::kPrimLong,
1494 /* is_volatile */ true,
1495 /* is_ordered */ false,
1496 codegen_);
Chris Larsen1360ada2015-09-04 23:38:16 -07001497}
1498
Alexey Frunze15958152017-02-09 19:08:30 -08001499static void CreateIntIntIntIntIntToIntPlusTemps(ArenaAllocator* arena, HInvoke* invoke) {
1500 bool can_call = kEmitCompilerReadBarrier &&
1501 kUseBakerReadBarrier &&
1502 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Chris Larsen36427492015-10-23 02:19:38 -07001503 LocationSummary* locations = new (arena) LocationSummary(invoke,
Alexey Frunze15958152017-02-09 19:08:30 -08001504 (can_call
1505 ? LocationSummary::kCallOnSlowPath
1506 : LocationSummary::kNoCall),
Chris Larsen36427492015-10-23 02:19:38 -07001507 kIntrinsified);
1508 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1509 locations->SetInAt(1, Location::RequiresRegister());
1510 locations->SetInAt(2, Location::RequiresRegister());
1511 locations->SetInAt(3, Location::RequiresRegister());
1512 locations->SetInAt(4, Location::RequiresRegister());
Chris Larsen36427492015-10-23 02:19:38 -07001513 locations->SetOut(Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08001514
1515 // Temporary register used in CAS by (Baker) read barrier.
1516 if (can_call) {
1517 locations->AddTemp(Location::RequiresRegister());
1518 }
Chris Larsen36427492015-10-23 02:19:38 -07001519}
1520
Alexey Frunze15958152017-02-09 19:08:30 -08001521// Note that the caller must supply a properly aligned memory address.
1522// If they do not, the behavior is undefined (atomicity not guaranteed, exception may occur).
1523static void GenCas(HInvoke* invoke, Primitive::Type type, CodeGeneratorMIPS64* codegen) {
Chris Larsen36427492015-10-23 02:19:38 -07001524 Mips64Assembler* assembler = codegen->GetAssembler();
Alexey Frunze15958152017-02-09 19:08:30 -08001525 LocationSummary* locations = invoke->GetLocations();
Chris Larsen36427492015-10-23 02:19:38 -07001526 GpuRegister base = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08001527 Location offset_loc = locations->InAt(2);
1528 GpuRegister offset = offset_loc.AsRegister<GpuRegister>();
Chris Larsen36427492015-10-23 02:19:38 -07001529 GpuRegister expected = locations->InAt(3).AsRegister<GpuRegister>();
1530 GpuRegister value = locations->InAt(4).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08001531 Location out_loc = locations->Out();
1532 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Chris Larsen36427492015-10-23 02:19:38 -07001533
1534 DCHECK_NE(base, out);
1535 DCHECK_NE(offset, out);
1536 DCHECK_NE(expected, out);
1537
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001538 if (type == Primitive::kPrimNot) {
Alexey Frunze15958152017-02-09 19:08:30 -08001539 // The only read barrier implementation supporting the
1540 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1541 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1542
1543 // Mark card for object assuming new value is stored. Worst case we will mark an unchanged
1544 // object and scan the receiver at the next GC for nothing.
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001545 bool value_can_be_null = true; // TODO: Worth finding out this information?
1546 codegen->MarkGCCard(base, value, value_can_be_null);
Alexey Frunze15958152017-02-09 19:08:30 -08001547
1548 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1549 Location temp = locations->GetTemp(0);
1550 // Need to make sure the reference stored in the field is a to-space
1551 // one before attempting the CAS or the CAS could fail incorrectly.
1552 codegen->GenerateReferenceLoadWithBakerReadBarrier(
1553 invoke,
1554 out_loc, // Unused, used only as a "temporary" within the read barrier.
1555 base,
1556 /* offset */ 0u,
1557 /* index */ offset_loc,
1558 ScaleFactor::TIMES_1,
1559 temp,
1560 /* needs_null_check */ false,
1561 /* always_update_field */ true);
1562 }
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001563 }
1564
Alexey Frunzec061de12017-02-14 13:27:23 -08001565 Mips64Label loop_head, exit_loop;
1566 __ Daddu(TMP, base, offset);
1567
1568 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1569 __ PoisonHeapReference(expected);
1570 // Do not poison `value`, if it is the same register as
1571 // `expected`, which has just been poisoned.
1572 if (value != expected) {
1573 __ PoisonHeapReference(value);
1574 }
1575 }
1576
Chris Larsen36427492015-10-23 02:19:38 -07001577 // do {
1578 // tmp_value = [tmp_ptr] - expected;
1579 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1580 // result = tmp_value != 0;
1581
Chris Larsen36427492015-10-23 02:19:38 -07001582 __ Sync(0);
1583 __ Bind(&loop_head);
1584 if (type == Primitive::kPrimLong) {
1585 __ Lld(out, TMP);
1586 } else {
Roland Levillain391b8662015-12-18 11:43:38 +00001587 // Note: We will need a read barrier here, when read barrier
1588 // support is added to the MIPS64 back end.
Chris Larsen36427492015-10-23 02:19:38 -07001589 __ Ll(out, TMP);
Alexey Frunzec061de12017-02-14 13:27:23 -08001590 if (type == Primitive::kPrimNot) {
1591 // The LL instruction sign-extends the 32-bit value, but
1592 // 32-bit references must be zero-extended. Zero-extend `out`.
1593 __ Dext(out, out, 0, 32);
1594 }
Chris Larsen36427492015-10-23 02:19:38 -07001595 }
1596 __ Dsubu(out, out, expected); // If we didn't get the 'expected'
1597 __ Sltiu(out, out, 1); // value, set 'out' to false, and
1598 __ Beqzc(out, &exit_loop); // return.
1599 __ Move(out, value); // Use 'out' for the 'store conditional' instruction.
1600 // If we use 'value' directly, we would lose 'value'
1601 // in the case that the store fails. Whether the
1602 // store succeeds, or fails, it will load the
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001603 // correct Boolean value into the 'out' register.
Chris Larsen36427492015-10-23 02:19:38 -07001604 if (type == Primitive::kPrimLong) {
1605 __ Scd(out, TMP);
1606 } else {
1607 __ Sc(out, TMP);
1608 }
1609 __ Beqzc(out, &loop_head); // If we couldn't do the read-modify-write
1610 // cycle atomically then retry.
1611 __ Bind(&exit_loop);
1612 __ Sync(0);
Alexey Frunzec061de12017-02-14 13:27:23 -08001613
1614 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1615 __ UnpoisonHeapReference(expected);
1616 // Do not unpoison `value`, if it is the same register as
1617 // `expected`, which has just been unpoisoned.
1618 if (value != expected) {
1619 __ UnpoisonHeapReference(value);
1620 }
1621 }
Chris Larsen36427492015-10-23 02:19:38 -07001622}
1623
1624// boolean sun.misc.Unsafe.compareAndSwapInt(Object o, long offset, int expected, int x)
1625void IntrinsicLocationsBuilderMIPS64::VisitUnsafeCASInt(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001626 CreateIntIntIntIntIntToIntPlusTemps(arena_, invoke);
Chris Larsen36427492015-10-23 02:19:38 -07001627}
1628
1629void IntrinsicCodeGeneratorMIPS64::VisitUnsafeCASInt(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001630 GenCas(invoke, Primitive::kPrimInt, codegen_);
Chris Larsen36427492015-10-23 02:19:38 -07001631}
1632
1633// boolean sun.misc.Unsafe.compareAndSwapLong(Object o, long offset, long expected, long x)
1634void IntrinsicLocationsBuilderMIPS64::VisitUnsafeCASLong(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001635 CreateIntIntIntIntIntToIntPlusTemps(arena_, invoke);
Chris Larsen36427492015-10-23 02:19:38 -07001636}
1637
1638void IntrinsicCodeGeneratorMIPS64::VisitUnsafeCASLong(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001639 GenCas(invoke, Primitive::kPrimLong, codegen_);
Chris Larsen36427492015-10-23 02:19:38 -07001640}
1641
1642// boolean sun.misc.Unsafe.compareAndSwapObject(Object o, long offset, Object expected, Object x)
1643void IntrinsicLocationsBuilderMIPS64::VisitUnsafeCASObject(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001644 // The only read barrier implementation supporting the
1645 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1646 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
1647 return;
1648 }
1649
1650 CreateIntIntIntIntIntToIntPlusTemps(arena_, invoke);
Chris Larsen36427492015-10-23 02:19:38 -07001651}
1652
1653void IntrinsicCodeGeneratorMIPS64::VisitUnsafeCASObject(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001654 // The only read barrier implementation supporting the
1655 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1656 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1657
1658 GenCas(invoke, Primitive::kPrimNot, codegen_);
Chris Larsen36427492015-10-23 02:19:38 -07001659}
1660
Chris Larsen9701c2e2015-09-04 17:22:47 -07001661// int java.lang.String.compareTo(String anotherString)
1662void IntrinsicLocationsBuilderMIPS64::VisitStringCompareTo(HInvoke* invoke) {
1663 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescufc734082016-07-19 17:18:07 +01001664 LocationSummary::kCallOnMainAndSlowPath,
Chris Larsen9701c2e2015-09-04 17:22:47 -07001665 kIntrinsified);
1666 InvokeRuntimeCallingConvention calling_convention;
1667 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1668 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1669 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1670 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1671}
1672
1673void IntrinsicCodeGeneratorMIPS64::VisitStringCompareTo(HInvoke* invoke) {
1674 Mips64Assembler* assembler = GetAssembler();
1675 LocationSummary* locations = invoke->GetLocations();
1676
1677 // Note that the null check must have been done earlier.
1678 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1679
1680 GpuRegister argument = locations->InAt(1).AsRegister<GpuRegister>();
1681 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1682 codegen_->AddSlowPath(slow_path);
1683 __ Beqzc(argument, slow_path->GetEntryLabel());
1684
Serban Constantinescufc734082016-07-19 17:18:07 +01001685 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Chris Larsen9701c2e2015-09-04 17:22:47 -07001686 __ Bind(slow_path->GetExitLabel());
1687}
1688
Chris Larsen972d6d72015-10-20 11:29:12 -07001689// boolean java.lang.String.equals(Object anObject)
1690void IntrinsicLocationsBuilderMIPS64::VisitStringEquals(HInvoke* invoke) {
1691 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1692 LocationSummary::kNoCall,
1693 kIntrinsified);
1694 locations->SetInAt(0, Location::RequiresRegister());
1695 locations->SetInAt(1, Location::RequiresRegister());
1696 locations->SetOut(Location::RequiresRegister());
1697
1698 // Temporary registers to store lengths of strings and for calculations.
1699 locations->AddTemp(Location::RequiresRegister());
1700 locations->AddTemp(Location::RequiresRegister());
1701 locations->AddTemp(Location::RequiresRegister());
1702}
1703
1704void IntrinsicCodeGeneratorMIPS64::VisitStringEquals(HInvoke* invoke) {
1705 Mips64Assembler* assembler = GetAssembler();
1706 LocationSummary* locations = invoke->GetLocations();
1707
1708 GpuRegister str = locations->InAt(0).AsRegister<GpuRegister>();
1709 GpuRegister arg = locations->InAt(1).AsRegister<GpuRegister>();
1710 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1711
1712 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
1713 GpuRegister temp2 = locations->GetTemp(1).AsRegister<GpuRegister>();
1714 GpuRegister temp3 = locations->GetTemp(2).AsRegister<GpuRegister>();
1715
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001716 Mips64Label loop;
1717 Mips64Label end;
1718 Mips64Label return_true;
1719 Mips64Label return_false;
Chris Larsen972d6d72015-10-20 11:29:12 -07001720
1721 // Get offsets of count, value, and class fields within a string object.
1722 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1723 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1724 const int32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1725
1726 // Note that the null check must have been done earlier.
1727 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1728
1729 // If the register containing the pointer to "this", and the register
1730 // containing the pointer to "anObject" are the same register then
1731 // "this", and "anObject" are the same object and we can
1732 // short-circuit the logic to a true result.
1733 if (str == arg) {
1734 __ LoadConst64(out, 1);
1735 return;
1736 }
1737
Goran Jakovljevic64fa84f2017-02-27 13:14:57 +01001738 StringEqualsOptimizations optimizations(invoke);
1739 if (!optimizations.GetArgumentNotNull()) {
1740 // Check if input is null, return false if it is.
1741 __ Beqzc(arg, &return_false);
1742 }
Chris Larsen972d6d72015-10-20 11:29:12 -07001743
1744 // Reference equality check, return true if same reference.
1745 __ Beqc(str, arg, &return_true);
1746
Goran Jakovljevic64fa84f2017-02-27 13:14:57 +01001747 if (!optimizations.GetArgumentIsString()) {
1748 // Instanceof check for the argument by comparing class fields.
1749 // All string objects must have the same type since String cannot be subclassed.
1750 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1751 // If the argument is a string object, its class field must be equal to receiver's class field.
1752 __ Lw(temp1, str, class_offset);
1753 __ Lw(temp2, arg, class_offset);
1754 __ Bnec(temp1, temp2, &return_false);
1755 }
Chris Larsen972d6d72015-10-20 11:29:12 -07001756
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001757 // Load `count` fields of this and argument strings.
Chris Larsen972d6d72015-10-20 11:29:12 -07001758 __ Lw(temp1, str, count_offset);
1759 __ Lw(temp2, arg, count_offset);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001760 // Check if `count` fields are equal, return false if they're not.
1761 // Also compares the compression style, if differs return false.
Chris Larsen972d6d72015-10-20 11:29:12 -07001762 __ Bnec(temp1, temp2, &return_false);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001763 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1764 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1765 "Expecting 0=compressed, 1=uncompressed");
Chris Larsen972d6d72015-10-20 11:29:12 -07001766 __ Beqzc(temp1, &return_true);
1767
1768 // Don't overwrite input registers
1769 __ Move(TMP, str);
1770 __ Move(temp3, arg);
1771
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001772 // Assertions that must hold in order to compare strings 8 bytes at a time.
Chris Larsen972d6d72015-10-20 11:29:12 -07001773 DCHECK_ALIGNED(value_offset, 8);
1774 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1775
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001776 if (mirror::kUseStringCompression) {
1777 // For string compression, calculate the number of bytes to compare (not chars).
1778 __ Dext(temp2, temp1, 0, 1); // Extract compression flag.
1779 __ Srl(temp1, temp1, 1); // Extract length.
1780 __ Sllv(temp1, temp1, temp2); // Double the byte count if uncompressed.
1781 }
1782
1783 // Loop to compare strings 8 bytes at a time starting at the beginning of the string.
1784 // Ok to do this because strings are zero-padded to kObjectAlignment.
Chris Larsen972d6d72015-10-20 11:29:12 -07001785 __ Bind(&loop);
1786 __ Ld(out, TMP, value_offset);
1787 __ Ld(temp2, temp3, value_offset);
1788 __ Bnec(out, temp2, &return_false);
1789 __ Daddiu(TMP, TMP, 8);
1790 __ Daddiu(temp3, temp3, 8);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001791 // With string compression, we have compared 8 bytes, otherwise 4 chars.
1792 __ Addiu(temp1, temp1, mirror::kUseStringCompression ? -8 : -4);
Chris Larsen972d6d72015-10-20 11:29:12 -07001793 __ Bgtzc(temp1, &loop);
1794
1795 // Return true and exit the function.
1796 // If loop does not result in returning false, we return true.
1797 __ Bind(&return_true);
1798 __ LoadConst64(out, 1);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001799 __ Bc(&end);
Chris Larsen972d6d72015-10-20 11:29:12 -07001800
1801 // Return false and exit the function.
1802 __ Bind(&return_false);
1803 __ LoadConst64(out, 0);
1804 __ Bind(&end);
1805}
1806
Chris Larsen9701c2e2015-09-04 17:22:47 -07001807static void GenerateStringIndexOf(HInvoke* invoke,
1808 Mips64Assembler* assembler,
1809 CodeGeneratorMIPS64* codegen,
1810 ArenaAllocator* allocator,
1811 bool start_at_zero) {
1812 LocationSummary* locations = invoke->GetLocations();
1813 GpuRegister tmp_reg = start_at_zero ? locations->GetTemp(0).AsRegister<GpuRegister>() : TMP;
1814
1815 // Note that the null check must have been done earlier.
1816 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1817
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001818 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1819 // or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
Chris Larsen9701c2e2015-09-04 17:22:47 -07001820 SlowPathCodeMIPS64* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001821 HInstruction* code_point = invoke->InputAt(1);
1822 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001823 if (!IsUint<16>(code_point->AsIntConstant()->GetValue())) {
Chris Larsen9701c2e2015-09-04 17:22:47 -07001824 // Always needs the slow-path. We could directly dispatch to it,
1825 // but this case should be rare, so for simplicity just put the
1826 // full slow-path down and branch unconditionally.
1827 slow_path = new (allocator) IntrinsicSlowPathMIPS64(invoke);
1828 codegen->AddSlowPath(slow_path);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001829 __ Bc(slow_path->GetEntryLabel());
Chris Larsen9701c2e2015-09-04 17:22:47 -07001830 __ Bind(slow_path->GetExitLabel());
1831 return;
1832 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001833 } else if (code_point->GetType() != Primitive::kPrimChar) {
Chris Larsen9701c2e2015-09-04 17:22:47 -07001834 GpuRegister char_reg = locations->InAt(1).AsRegister<GpuRegister>();
1835 __ LoadConst32(tmp_reg, std::numeric_limits<uint16_t>::max());
1836 slow_path = new (allocator) IntrinsicSlowPathMIPS64(invoke);
1837 codegen->AddSlowPath(slow_path);
1838 __ Bltuc(tmp_reg, char_reg, slow_path->GetEntryLabel()); // UTF-16 required
1839 }
1840
1841 if (start_at_zero) {
1842 DCHECK_EQ(tmp_reg, A2);
1843 // Start-index = 0.
1844 __ Clear(tmp_reg);
Chris Larsen9701c2e2015-09-04 17:22:47 -07001845 }
1846
Serban Constantinescufc734082016-07-19 17:18:07 +01001847 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
Roland Levillain42ad2882016-02-29 18:26:54 +00001848 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
Chris Larsen9701c2e2015-09-04 17:22:47 -07001849
1850 if (slow_path != nullptr) {
1851 __ Bind(slow_path->GetExitLabel());
1852 }
1853}
1854
1855// int java.lang.String.indexOf(int ch)
1856void IntrinsicLocationsBuilderMIPS64::VisitStringIndexOf(HInvoke* invoke) {
1857 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001858 LocationSummary::kCallOnMainAndSlowPath,
Chris Larsen9701c2e2015-09-04 17:22:47 -07001859 kIntrinsified);
1860 // We have a hand-crafted assembly stub that follows the runtime
1861 // calling convention. So it's best to align the inputs accordingly.
1862 InvokeRuntimeCallingConvention calling_convention;
1863 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1864 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1865 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1866 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1867
1868 // Need a temp for slow-path codepoint compare, and need to send start-index=0.
1869 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1870}
1871
1872void IntrinsicCodeGeneratorMIPS64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001873 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Chris Larsen9701c2e2015-09-04 17:22:47 -07001874}
1875
1876// int java.lang.String.indexOf(int ch, int fromIndex)
1877void IntrinsicLocationsBuilderMIPS64::VisitStringIndexOfAfter(HInvoke* invoke) {
1878 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001879 LocationSummary::kCallOnMainAndSlowPath,
Chris Larsen9701c2e2015-09-04 17:22:47 -07001880 kIntrinsified);
1881 // We have a hand-crafted assembly stub that follows the runtime
1882 // calling convention. So it's best to align the inputs accordingly.
1883 InvokeRuntimeCallingConvention calling_convention;
1884 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1885 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1886 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1887 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1888 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1889}
1890
1891void IntrinsicCodeGeneratorMIPS64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001892 GenerateStringIndexOf(
1893 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Chris Larsen9701c2e2015-09-04 17:22:47 -07001894}
1895
Roland Levillaincc3839c2016-02-29 16:23:48 +00001896// java.lang.StringFactory.newStringFromBytes(byte[] data, int high, int offset, int byteCount)
Chris Larsen9701c2e2015-09-04 17:22:47 -07001897void IntrinsicLocationsBuilderMIPS64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1898 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001899 LocationSummary::kCallOnMainAndSlowPath,
Chris Larsen9701c2e2015-09-04 17:22:47 -07001900 kIntrinsified);
1901 InvokeRuntimeCallingConvention calling_convention;
1902 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1903 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1904 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1905 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1906 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1907 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1908}
1909
1910void IntrinsicCodeGeneratorMIPS64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1911 Mips64Assembler* assembler = GetAssembler();
1912 LocationSummary* locations = invoke->GetLocations();
1913
1914 GpuRegister byte_array = locations->InAt(0).AsRegister<GpuRegister>();
1915 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1916 codegen_->AddSlowPath(slow_path);
1917 __ Beqzc(byte_array, slow_path->GetEntryLabel());
1918
Serban Constantinescufc734082016-07-19 17:18:07 +01001919 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001920 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Chris Larsen9701c2e2015-09-04 17:22:47 -07001921 __ Bind(slow_path->GetExitLabel());
1922}
1923
Roland Levillaincc3839c2016-02-29 16:23:48 +00001924// java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
Chris Larsen9701c2e2015-09-04 17:22:47 -07001925void IntrinsicLocationsBuilderMIPS64::VisitStringNewStringFromChars(HInvoke* invoke) {
1926 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001927 LocationSummary::kCallOnMainOnly,
Chris Larsen9701c2e2015-09-04 17:22:47 -07001928 kIntrinsified);
1929 InvokeRuntimeCallingConvention calling_convention;
1930 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1931 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1932 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1933 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1934 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1935}
1936
1937void IntrinsicCodeGeneratorMIPS64::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001938 // No need to emit code checking whether `locations->InAt(2)` is a null
1939 // pointer, as callers of the native method
1940 //
1941 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1942 //
1943 // all include a null check on `data` before calling that method.
Serban Constantinescufc734082016-07-19 17:18:07 +01001944 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001945 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Chris Larsen9701c2e2015-09-04 17:22:47 -07001946}
1947
Roland Levillainf969a202016-03-09 16:14:00 +00001948// java.lang.StringFactory.newStringFromString(String toCopy)
Chris Larsen9701c2e2015-09-04 17:22:47 -07001949void IntrinsicLocationsBuilderMIPS64::VisitStringNewStringFromString(HInvoke* invoke) {
1950 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001951 LocationSummary::kCallOnMainAndSlowPath,
Chris Larsen9701c2e2015-09-04 17:22:47 -07001952 kIntrinsified);
1953 InvokeRuntimeCallingConvention calling_convention;
1954 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Chris Larsen9701c2e2015-09-04 17:22:47 -07001955 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1956 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1957}
1958
1959void IntrinsicCodeGeneratorMIPS64::VisitStringNewStringFromString(HInvoke* invoke) {
1960 Mips64Assembler* assembler = GetAssembler();
1961 LocationSummary* locations = invoke->GetLocations();
1962
1963 GpuRegister string_to_copy = locations->InAt(0).AsRegister<GpuRegister>();
1964 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1965 codegen_->AddSlowPath(slow_path);
1966 __ Beqzc(string_to_copy, slow_path->GetEntryLabel());
1967
Serban Constantinescufc734082016-07-19 17:18:07 +01001968 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001969 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Chris Larsen9701c2e2015-09-04 17:22:47 -07001970 __ Bind(slow_path->GetExitLabel());
1971}
1972
Chris Larsenddec7f92016-02-16 12:35:04 -08001973static void GenIsInfinite(LocationSummary* locations,
1974 bool is64bit,
1975 Mips64Assembler* assembler) {
1976 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
1977 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1978
1979 if (is64bit) {
1980 __ ClassD(FTMP, in);
1981 } else {
1982 __ ClassS(FTMP, in);
1983 }
1984 __ Mfc1(out, FTMP);
1985 __ Andi(out, out, kPositiveInfinity | kNegativeInfinity);
1986 __ Sltu(out, ZERO, out);
1987}
1988
1989// boolean java.lang.Float.isInfinite(float)
1990void IntrinsicLocationsBuilderMIPS64::VisitFloatIsInfinite(HInvoke* invoke) {
1991 CreateFPToIntLocations(arena_, invoke);
1992}
1993
1994void IntrinsicCodeGeneratorMIPS64::VisitFloatIsInfinite(HInvoke* invoke) {
1995 GenIsInfinite(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
1996}
1997
1998// boolean java.lang.Double.isInfinite(double)
1999void IntrinsicLocationsBuilderMIPS64::VisitDoubleIsInfinite(HInvoke* invoke) {
2000 CreateFPToIntLocations(arena_, invoke);
2001}
2002
2003void IntrinsicCodeGeneratorMIPS64::VisitDoubleIsInfinite(HInvoke* invoke) {
2004 GenIsInfinite(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
2005}
2006
Chris Larsene3660592016-11-09 11:13:42 -08002007// void java.lang.String.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
2008void IntrinsicLocationsBuilderMIPS64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2009 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Chris Larsen366d4332017-03-23 09:02:56 -07002010 LocationSummary::kNoCall,
Chris Larsene3660592016-11-09 11:13:42 -08002011 kIntrinsified);
2012 locations->SetInAt(0, Location::RequiresRegister());
2013 locations->SetInAt(1, Location::RequiresRegister());
2014 locations->SetInAt(2, Location::RequiresRegister());
2015 locations->SetInAt(3, Location::RequiresRegister());
2016 locations->SetInAt(4, Location::RequiresRegister());
2017
Chris Larsen366d4332017-03-23 09:02:56 -07002018 locations->AddTemp(Location::RequiresRegister());
2019 locations->AddTemp(Location::RequiresRegister());
2020 locations->AddTemp(Location::RequiresRegister());
Chris Larsene3660592016-11-09 11:13:42 -08002021}
2022
2023void IntrinsicCodeGeneratorMIPS64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2024 Mips64Assembler* assembler = GetAssembler();
2025 LocationSummary* locations = invoke->GetLocations();
2026
2027 // Check assumption that sizeof(Char) is 2 (used in scaling below).
2028 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
2029 DCHECK_EQ(char_size, 2u);
2030 const size_t char_shift = Primitive::ComponentSizeShift(Primitive::kPrimChar);
2031
2032 GpuRegister srcObj = locations->InAt(0).AsRegister<GpuRegister>();
2033 GpuRegister srcBegin = locations->InAt(1).AsRegister<GpuRegister>();
2034 GpuRegister srcEnd = locations->InAt(2).AsRegister<GpuRegister>();
2035 GpuRegister dstObj = locations->InAt(3).AsRegister<GpuRegister>();
2036 GpuRegister dstBegin = locations->InAt(4).AsRegister<GpuRegister>();
2037
2038 GpuRegister dstPtr = locations->GetTemp(0).AsRegister<GpuRegister>();
Chris Larsene3660592016-11-09 11:13:42 -08002039 GpuRegister srcPtr = locations->GetTemp(1).AsRegister<GpuRegister>();
Chris Larsene3660592016-11-09 11:13:42 -08002040 GpuRegister numChrs = locations->GetTemp(2).AsRegister<GpuRegister>();
Chris Larsene3660592016-11-09 11:13:42 -08002041
2042 Mips64Label done;
Chris Larsen366d4332017-03-23 09:02:56 -07002043 Mips64Label loop;
Chris Larsene3660592016-11-09 11:13:42 -08002044
2045 // Location of data in char array buffer.
2046 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
2047
2048 // Get offset of value field within a string object.
2049 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
2050
2051 __ Beqc(srcEnd, srcBegin, &done); // No characters to move.
2052
2053 // Calculate number of characters to be copied.
2054 __ Dsubu(numChrs, srcEnd, srcBegin);
2055
2056 // Calculate destination address.
2057 __ Daddiu(dstPtr, dstObj, data_offset);
2058 __ Dlsa(dstPtr, dstBegin, dstPtr, char_shift);
2059
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002060 if (mirror::kUseStringCompression) {
2061 Mips64Label uncompressed_copy, compressed_loop;
2062 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2063 // Load count field and extract compression flag.
2064 __ LoadFromOffset(kLoadWord, TMP, srcObj, count_offset);
2065 __ Dext(TMP, TMP, 0, 1);
2066
Chris Larsen366d4332017-03-23 09:02:56 -07002067 // If string is uncompressed, use uncompressed path.
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002068 __ Bnezc(TMP, &uncompressed_copy);
2069
2070 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
2071 __ Daddu(srcPtr, srcObj, srcBegin);
2072 __ Bind(&compressed_loop);
2073 __ LoadFromOffset(kLoadUnsignedByte, TMP, srcPtr, value_offset);
2074 __ StoreToOffset(kStoreHalfword, TMP, dstPtr, 0);
2075 __ Daddiu(numChrs, numChrs, -1);
2076 __ Daddiu(srcPtr, srcPtr, 1);
2077 __ Daddiu(dstPtr, dstPtr, 2);
2078 __ Bnezc(numChrs, &compressed_loop);
2079
2080 __ Bc(&done);
2081 __ Bind(&uncompressed_copy);
2082 }
2083
Chris Larsene3660592016-11-09 11:13:42 -08002084 // Calculate source address.
2085 __ Daddiu(srcPtr, srcObj, value_offset);
2086 __ Dlsa(srcPtr, srcBegin, srcPtr, char_shift);
2087
Chris Larsen366d4332017-03-23 09:02:56 -07002088 __ Bind(&loop);
2089 __ Lh(AT, srcPtr, 0);
2090 __ Daddiu(numChrs, numChrs, -1);
2091 __ Daddiu(srcPtr, srcPtr, char_size);
2092 __ Sh(AT, dstPtr, 0);
2093 __ Daddiu(dstPtr, dstPtr, char_size);
2094 __ Bnezc(numChrs, &loop);
Chris Larsene3660592016-11-09 11:13:42 -08002095
2096 __ Bind(&done);
2097}
2098
Chris Larsen5863f852017-03-23 15:41:37 -07002099// static void java.lang.System.arraycopy(Object src, int srcPos,
2100// Object dest, int destPos,
2101// int length)
2102void IntrinsicLocationsBuilderMIPS64::VisitSystemArrayCopyChar(HInvoke* invoke) {
2103 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2104 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2105 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2106
2107 // As long as we are checking, we might as well check to see if the src and dest
2108 // positions are >= 0.
2109 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2110 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
2111 // We will have to fail anyways.
2112 return;
2113 }
2114
2115 // And since we are already checking, check the length too.
2116 if (length != nullptr) {
2117 int32_t len = length->GetValue();
2118 if (len < 0) {
2119 // Just call as normal.
2120 return;
2121 }
2122 }
2123
2124 // Okay, it is safe to generate inline code.
2125 LocationSummary* locations =
2126 new (arena_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
2127 // arraycopy(Object src, int srcPos, Object dest, int destPos, int length).
2128 locations->SetInAt(0, Location::RequiresRegister());
2129 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
2130 locations->SetInAt(2, Location::RequiresRegister());
2131 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
2132 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
2133
2134 locations->AddTemp(Location::RequiresRegister());
2135 locations->AddTemp(Location::RequiresRegister());
2136 locations->AddTemp(Location::RequiresRegister());
2137}
2138
2139// Utility routine to verify that "length(input) - pos >= length"
2140static void EnoughItems(Mips64Assembler* assembler,
2141 GpuRegister length_input_minus_pos,
2142 Location length,
2143 SlowPathCodeMIPS64* slow_path) {
2144 if (length.IsConstant()) {
2145 int32_t length_constant = length.GetConstant()->AsIntConstant()->GetValue();
2146
2147 if (IsInt<16>(length_constant)) {
2148 __ Slti(TMP, length_input_minus_pos, length_constant);
2149 __ Bnezc(TMP, slow_path->GetEntryLabel());
2150 } else {
2151 __ LoadConst32(TMP, length_constant);
2152 __ Bltc(length_input_minus_pos, TMP, slow_path->GetEntryLabel());
2153 }
2154 } else {
2155 __ Bltc(length_input_minus_pos, length.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2156 }
2157}
2158
2159static void CheckPosition(Mips64Assembler* assembler,
2160 Location pos,
2161 GpuRegister input,
2162 Location length,
2163 SlowPathCodeMIPS64* slow_path,
2164 bool length_is_input_length = false) {
2165 // Where is the length in the Array?
2166 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
2167
2168 // Calculate length(input) - pos.
2169 if (pos.IsConstant()) {
2170 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
2171 if (pos_const == 0) {
2172 if (!length_is_input_length) {
2173 // Check that length(input) >= length.
2174 __ LoadFromOffset(kLoadWord, AT, input, length_offset);
2175 EnoughItems(assembler, AT, length, slow_path);
2176 }
2177 } else {
2178 // Check that (length(input) - pos) >= zero.
2179 __ LoadFromOffset(kLoadWord, AT, input, length_offset);
2180 DCHECK_GT(pos_const, 0);
2181 __ Addiu32(AT, AT, -pos_const);
2182 __ Bltzc(AT, slow_path->GetEntryLabel());
2183
2184 // Verify that (length(input) - pos) >= length.
2185 EnoughItems(assembler, AT, length, slow_path);
2186 }
2187 } else if (length_is_input_length) {
2188 // The only way the copy can succeed is if pos is zero.
2189 GpuRegister pos_reg = pos.AsRegister<GpuRegister>();
2190 __ Bnezc(pos_reg, slow_path->GetEntryLabel());
2191 } else {
2192 // Verify that pos >= 0.
2193 GpuRegister pos_reg = pos.AsRegister<GpuRegister>();
2194 __ Bltzc(pos_reg, slow_path->GetEntryLabel());
2195
2196 // Check that (length(input) - pos) >= zero.
2197 __ LoadFromOffset(kLoadWord, AT, input, length_offset);
2198 __ Subu(AT, AT, pos_reg);
2199 __ Bltzc(AT, slow_path->GetEntryLabel());
2200
2201 // Verify that (length(input) - pos) >= length.
2202 EnoughItems(assembler, AT, length, slow_path);
2203 }
2204}
2205
2206void IntrinsicCodeGeneratorMIPS64::VisitSystemArrayCopyChar(HInvoke* invoke) {
2207 Mips64Assembler* assembler = GetAssembler();
2208 LocationSummary* locations = invoke->GetLocations();
2209
2210 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
2211 Location src_pos = locations->InAt(1);
2212 GpuRegister dest = locations->InAt(2).AsRegister<GpuRegister>();
2213 Location dest_pos = locations->InAt(3);
2214 Location length = locations->InAt(4);
2215
2216 Mips64Label loop;
2217
2218 GpuRegister dest_base = locations->GetTemp(0).AsRegister<GpuRegister>();
2219 GpuRegister src_base = locations->GetTemp(1).AsRegister<GpuRegister>();
2220 GpuRegister count = locations->GetTemp(2).AsRegister<GpuRegister>();
2221
2222 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
2223 codegen_->AddSlowPath(slow_path);
2224
2225 // Bail out if the source and destination are the same (to handle overlap).
2226 __ Beqc(src, dest, slow_path->GetEntryLabel());
2227
2228 // Bail out if the source is null.
2229 __ Beqzc(src, slow_path->GetEntryLabel());
2230
2231 // Bail out if the destination is null.
2232 __ Beqzc(dest, slow_path->GetEntryLabel());
2233
2234 // Load length into register for count.
2235 if (length.IsConstant()) {
2236 __ LoadConst32(count, length.GetConstant()->AsIntConstant()->GetValue());
2237 } else {
2238 // If the length is negative, bail out.
2239 // We have already checked in the LocationsBuilder for the constant case.
2240 __ Bltzc(length.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2241
2242 __ Move(count, length.AsRegister<GpuRegister>());
2243 }
2244
2245 // Validity checks: source.
2246 CheckPosition(assembler, src_pos, src, Location::RegisterLocation(count), slow_path);
2247
2248 // Validity checks: dest.
2249 CheckPosition(assembler, dest_pos, dest, Location::RegisterLocation(count), slow_path);
2250
2251 // If count is zero, we're done.
2252 __ Beqzc(count, slow_path->GetExitLabel());
2253
2254 // Okay, everything checks out. Finally time to do the copy.
2255 // Check assumption that sizeof(Char) is 2 (used in scaling below).
2256 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
2257 DCHECK_EQ(char_size, 2u);
2258
2259 const size_t char_shift = Primitive::ComponentSizeShift(Primitive::kPrimChar);
2260
2261 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
2262
2263 // Calculate source and destination addresses.
2264 if (src_pos.IsConstant()) {
2265 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
2266
2267 __ Daddiu64(src_base, src, data_offset + char_size * src_pos_const, TMP);
2268 } else {
2269 __ Daddiu64(src_base, src, data_offset, TMP);
2270 __ Dlsa(src_base, src_pos.AsRegister<GpuRegister>(), src_base, char_shift);
2271 }
2272 if (dest_pos.IsConstant()) {
2273 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2274
2275 __ Daddiu64(dest_base, dest, data_offset + char_size * dest_pos_const, TMP);
2276 } else {
2277 __ Daddiu64(dest_base, dest, data_offset, TMP);
2278 __ Dlsa(dest_base, dest_pos.AsRegister<GpuRegister>(), dest_base, char_shift);
2279 }
2280
2281 __ Bind(&loop);
2282 __ Lh(TMP, src_base, 0);
2283 __ Daddiu(src_base, src_base, char_size);
2284 __ Daddiu(count, count, -1);
2285 __ Sh(TMP, dest_base, 0);
2286 __ Daddiu(dest_base, dest_base, char_size);
2287 __ Bnezc(count, &loop);
2288
2289 __ Bind(slow_path->GetExitLabel());
2290}
2291
Chris Larsenab922502016-04-15 10:00:56 -07002292static void GenHighestOneBit(LocationSummary* locations,
2293 Primitive::Type type,
2294 Mips64Assembler* assembler) {
2295 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << PrettyDescriptor(type);
2296
2297 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
2298 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2299
2300 if (type == Primitive::kPrimLong) {
2301 __ Dclz(TMP, in);
2302 __ LoadConst64(AT, INT64_C(0x8000000000000000));
Chris Larsen68db2a92016-09-14 15:41:29 -07002303 __ Dsrlv(AT, AT, TMP);
Chris Larsenab922502016-04-15 10:00:56 -07002304 } else {
2305 __ Clz(TMP, in);
2306 __ LoadConst32(AT, 0x80000000);
Chris Larsen68db2a92016-09-14 15:41:29 -07002307 __ Srlv(AT, AT, TMP);
Chris Larsenab922502016-04-15 10:00:56 -07002308 }
2309 // For either value of "type", when "in" is zero, "out" should also
2310 // be zero. Without this extra "and" operation, when "in" is zero,
2311 // "out" would be either Integer.MIN_VALUE, or Long.MIN_VALUE because
2312 // the MIPS logical shift operations "dsrlv", and "srlv" don't use
2313 // the shift amount (TMP) directly; they use either (TMP % 64) or
2314 // (TMP % 32), respectively.
Chris Larsen68db2a92016-09-14 15:41:29 -07002315 __ And(out, AT, in);
Chris Larsenab922502016-04-15 10:00:56 -07002316}
2317
2318// int java.lang.Integer.highestOneBit(int)
2319void IntrinsicLocationsBuilderMIPS64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2320 CreateIntToIntLocations(arena_, invoke);
2321}
2322
2323void IntrinsicCodeGeneratorMIPS64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2324 GenHighestOneBit(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
2325}
2326
2327// long java.lang.Long.highestOneBit(long)
2328void IntrinsicLocationsBuilderMIPS64::VisitLongHighestOneBit(HInvoke* invoke) {
2329 CreateIntToIntLocations(arena_, invoke);
2330}
2331
2332void IntrinsicCodeGeneratorMIPS64::VisitLongHighestOneBit(HInvoke* invoke) {
2333 GenHighestOneBit(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
2334}
2335
2336static void GenLowestOneBit(LocationSummary* locations,
2337 Primitive::Type type,
2338 Mips64Assembler* assembler) {
2339 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << PrettyDescriptor(type);
2340
2341 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
2342 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2343
2344 if (type == Primitive::kPrimLong) {
2345 __ Dsubu(TMP, ZERO, in);
2346 } else {
2347 __ Subu(TMP, ZERO, in);
2348 }
2349 __ And(out, TMP, in);
2350}
2351
2352// int java.lang.Integer.lowestOneBit(int)
2353void IntrinsicLocationsBuilderMIPS64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2354 CreateIntToIntLocations(arena_, invoke);
2355}
2356
2357void IntrinsicCodeGeneratorMIPS64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2358 GenLowestOneBit(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
2359}
2360
2361// long java.lang.Long.lowestOneBit(long)
2362void IntrinsicLocationsBuilderMIPS64::VisitLongLowestOneBit(HInvoke* invoke) {
2363 CreateIntToIntLocations(arena_, invoke);
2364}
2365
2366void IntrinsicCodeGeneratorMIPS64::VisitLongLowestOneBit(HInvoke* invoke) {
2367 GenLowestOneBit(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
2368}
2369
Chris Larsen74c20582017-03-28 22:17:35 -07002370static void CreateFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
2371 LocationSummary* locations = new (arena) LocationSummary(invoke,
2372 LocationSummary::kCallOnMainOnly,
2373 kIntrinsified);
2374 InvokeRuntimeCallingConvention calling_convention;
2375
2376 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2377 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimDouble));
2378}
2379
2380static void CreateFPFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
2381 LocationSummary* locations = new (arena) LocationSummary(invoke,
2382 LocationSummary::kCallOnMainOnly,
2383 kIntrinsified);
2384 InvokeRuntimeCallingConvention calling_convention;
2385
2386 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2387 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2388 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimDouble));
2389}
2390
2391static void GenFPToFPCall(HInvoke* invoke,
2392 CodeGeneratorMIPS64* codegen,
2393 QuickEntrypointEnum entry) {
2394 LocationSummary* locations = invoke->GetLocations();
2395 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
2396 DCHECK_EQ(in, F12);
2397 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
2398 DCHECK_EQ(out, F0);
2399
2400 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2401}
2402
2403static void GenFPFPToFPCall(HInvoke* invoke,
2404 CodeGeneratorMIPS64* codegen,
2405 QuickEntrypointEnum entry) {
2406 LocationSummary* locations = invoke->GetLocations();
2407 FpuRegister in0 = locations->InAt(0).AsFpuRegister<FpuRegister>();
2408 DCHECK_EQ(in0, F12);
2409 FpuRegister in1 = locations->InAt(1).AsFpuRegister<FpuRegister>();
2410 DCHECK_EQ(in1, F13);
2411 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
2412 DCHECK_EQ(out, F0);
2413
2414 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2415}
2416
2417// static double java.lang.Math.cos(double a)
2418void IntrinsicLocationsBuilderMIPS64::VisitMathCos(HInvoke* invoke) {
2419 CreateFPToFPCallLocations(arena_, invoke);
2420}
2421
2422void IntrinsicCodeGeneratorMIPS64::VisitMathCos(HInvoke* invoke) {
2423 GenFPToFPCall(invoke, codegen_, kQuickCos);
2424}
2425
2426// static double java.lang.Math.sin(double a)
2427void IntrinsicLocationsBuilderMIPS64::VisitMathSin(HInvoke* invoke) {
2428 CreateFPToFPCallLocations(arena_, invoke);
2429}
2430
2431void IntrinsicCodeGeneratorMIPS64::VisitMathSin(HInvoke* invoke) {
2432 GenFPToFPCall(invoke, codegen_, kQuickSin);
2433}
2434
2435// static double java.lang.Math.acos(double a)
2436void IntrinsicLocationsBuilderMIPS64::VisitMathAcos(HInvoke* invoke) {
2437 CreateFPToFPCallLocations(arena_, invoke);
2438}
2439
2440void IntrinsicCodeGeneratorMIPS64::VisitMathAcos(HInvoke* invoke) {
2441 GenFPToFPCall(invoke, codegen_, kQuickAcos);
2442}
2443
2444// static double java.lang.Math.asin(double a)
2445void IntrinsicLocationsBuilderMIPS64::VisitMathAsin(HInvoke* invoke) {
2446 CreateFPToFPCallLocations(arena_, invoke);
2447}
2448
2449void IntrinsicCodeGeneratorMIPS64::VisitMathAsin(HInvoke* invoke) {
2450 GenFPToFPCall(invoke, codegen_, kQuickAsin);
2451}
2452
2453// static double java.lang.Math.atan(double a)
2454void IntrinsicLocationsBuilderMIPS64::VisitMathAtan(HInvoke* invoke) {
2455 CreateFPToFPCallLocations(arena_, invoke);
2456}
2457
2458void IntrinsicCodeGeneratorMIPS64::VisitMathAtan(HInvoke* invoke) {
2459 GenFPToFPCall(invoke, codegen_, kQuickAtan);
2460}
2461
2462// static double java.lang.Math.atan2(double y, double x)
2463void IntrinsicLocationsBuilderMIPS64::VisitMathAtan2(HInvoke* invoke) {
2464 CreateFPFPToFPCallLocations(arena_, invoke);
2465}
2466
2467void IntrinsicCodeGeneratorMIPS64::VisitMathAtan2(HInvoke* invoke) {
2468 GenFPFPToFPCall(invoke, codegen_, kQuickAtan2);
2469}
2470
2471// static double java.lang.Math.cbrt(double a)
2472void IntrinsicLocationsBuilderMIPS64::VisitMathCbrt(HInvoke* invoke) {
2473 CreateFPToFPCallLocations(arena_, invoke);
2474}
2475
2476void IntrinsicCodeGeneratorMIPS64::VisitMathCbrt(HInvoke* invoke) {
2477 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
2478}
2479
2480// static double java.lang.Math.cosh(double x)
2481void IntrinsicLocationsBuilderMIPS64::VisitMathCosh(HInvoke* invoke) {
2482 CreateFPToFPCallLocations(arena_, invoke);
2483}
2484
2485void IntrinsicCodeGeneratorMIPS64::VisitMathCosh(HInvoke* invoke) {
2486 GenFPToFPCall(invoke, codegen_, kQuickCosh);
2487}
2488
2489// static double java.lang.Math.exp(double a)
2490void IntrinsicLocationsBuilderMIPS64::VisitMathExp(HInvoke* invoke) {
2491 CreateFPToFPCallLocations(arena_, invoke);
2492}
2493
2494void IntrinsicCodeGeneratorMIPS64::VisitMathExp(HInvoke* invoke) {
2495 GenFPToFPCall(invoke, codegen_, kQuickExp);
2496}
2497
2498// static double java.lang.Math.expm1(double x)
2499void IntrinsicLocationsBuilderMIPS64::VisitMathExpm1(HInvoke* invoke) {
2500 CreateFPToFPCallLocations(arena_, invoke);
2501}
2502
2503void IntrinsicCodeGeneratorMIPS64::VisitMathExpm1(HInvoke* invoke) {
2504 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
2505}
2506
2507// static double java.lang.Math.hypot(double x, double y)
2508void IntrinsicLocationsBuilderMIPS64::VisitMathHypot(HInvoke* invoke) {
2509 CreateFPFPToFPCallLocations(arena_, invoke);
2510}
2511
2512void IntrinsicCodeGeneratorMIPS64::VisitMathHypot(HInvoke* invoke) {
2513 GenFPFPToFPCall(invoke, codegen_, kQuickHypot);
2514}
2515
2516// static double java.lang.Math.log(double a)
2517void IntrinsicLocationsBuilderMIPS64::VisitMathLog(HInvoke* invoke) {
2518 CreateFPToFPCallLocations(arena_, invoke);
2519}
2520
2521void IntrinsicCodeGeneratorMIPS64::VisitMathLog(HInvoke* invoke) {
2522 GenFPToFPCall(invoke, codegen_, kQuickLog);
2523}
2524
2525// static double java.lang.Math.log10(double x)
2526void IntrinsicLocationsBuilderMIPS64::VisitMathLog10(HInvoke* invoke) {
2527 CreateFPToFPCallLocations(arena_, invoke);
2528}
2529
2530void IntrinsicCodeGeneratorMIPS64::VisitMathLog10(HInvoke* invoke) {
2531 GenFPToFPCall(invoke, codegen_, kQuickLog10);
2532}
2533
2534// static double java.lang.Math.nextAfter(double start, double direction)
2535void IntrinsicLocationsBuilderMIPS64::VisitMathNextAfter(HInvoke* invoke) {
2536 CreateFPFPToFPCallLocations(arena_, invoke);
2537}
2538
2539void IntrinsicCodeGeneratorMIPS64::VisitMathNextAfter(HInvoke* invoke) {
2540 GenFPFPToFPCall(invoke, codegen_, kQuickNextAfter);
2541}
2542
2543// static double java.lang.Math.sinh(double x)
2544void IntrinsicLocationsBuilderMIPS64::VisitMathSinh(HInvoke* invoke) {
2545 CreateFPToFPCallLocations(arena_, invoke);
2546}
2547
2548void IntrinsicCodeGeneratorMIPS64::VisitMathSinh(HInvoke* invoke) {
2549 GenFPToFPCall(invoke, codegen_, kQuickSinh);
2550}
2551
2552// static double java.lang.Math.tan(double a)
2553void IntrinsicLocationsBuilderMIPS64::VisitMathTan(HInvoke* invoke) {
2554 CreateFPToFPCallLocations(arena_, invoke);
2555}
2556
2557void IntrinsicCodeGeneratorMIPS64::VisitMathTan(HInvoke* invoke) {
2558 GenFPToFPCall(invoke, codegen_, kQuickTan);
2559}
2560
2561// static double java.lang.Math.tanh(double x)
2562void IntrinsicLocationsBuilderMIPS64::VisitMathTanh(HInvoke* invoke) {
2563 CreateFPToFPCallLocations(arena_, invoke);
2564}
2565
2566void IntrinsicCodeGeneratorMIPS64::VisitMathTanh(HInvoke* invoke) {
2567 GenFPToFPCall(invoke, codegen_, kQuickTanh);
2568}
2569
Aart Bik2f9fcc92016-03-01 15:16:54 -08002570UNIMPLEMENTED_INTRINSIC(MIPS64, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08002571UNIMPLEMENTED_INTRINSIC(MIPS64, SystemArrayCopy)
Aart Bik3f67e692016-01-15 14:35:12 -08002572
Aart Bikff7d89c2016-11-07 08:49:28 -08002573UNIMPLEMENTED_INTRINSIC(MIPS64, StringStringIndexOf);
2574UNIMPLEMENTED_INTRINSIC(MIPS64, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08002575UNIMPLEMENTED_INTRINSIC(MIPS64, StringBufferAppend);
2576UNIMPLEMENTED_INTRINSIC(MIPS64, StringBufferLength);
2577UNIMPLEMENTED_INTRINSIC(MIPS64, StringBufferToString);
2578UNIMPLEMENTED_INTRINSIC(MIPS64, StringBuilderAppend);
2579UNIMPLEMENTED_INTRINSIC(MIPS64, StringBuilderLength);
2580UNIMPLEMENTED_INTRINSIC(MIPS64, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08002581
Aart Bik0e54c012016-03-04 12:08:31 -08002582// 1.8.
2583UNIMPLEMENTED_INTRINSIC(MIPS64, UnsafeGetAndAddInt)
2584UNIMPLEMENTED_INTRINSIC(MIPS64, UnsafeGetAndAddLong)
2585UNIMPLEMENTED_INTRINSIC(MIPS64, UnsafeGetAndSetInt)
2586UNIMPLEMENTED_INTRINSIC(MIPS64, UnsafeGetAndSetLong)
2587UNIMPLEMENTED_INTRINSIC(MIPS64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002588
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002589UNIMPLEMENTED_INTRINSIC(MIPS64, IntegerValueOf)
2590
Aart Bik2f9fcc92016-03-01 15:16:54 -08002591UNREACHABLE_INTRINSICS(MIPS64)
Chris Larsen3039e382015-08-26 07:54:08 -07002592
2593#undef __
2594
2595} // namespace mips64
2596} // namespace art