blob: a3121a93dc796b639b00aa90e87803fb74efd3db [file] [log] [blame]
Chris Larsen3039e382015-08-26 07:54:08 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "intrinsics_mips64.h"
18
19#include "arch/mips64/instruction_set_features_mips64.h"
20#include "art_method.h"
21#include "code_generator_mips64.h"
22#include "entrypoints/quick/quick_entrypoints.h"
23#include "intrinsics.h"
24#include "mirror/array-inl.h"
25#include "mirror/string.h"
26#include "thread.h"
27#include "utils/mips64/assembler_mips64.h"
28#include "utils/mips64/constants_mips64.h"
29
30namespace art {
31
32namespace mips64 {
33
34IntrinsicLocationsBuilderMIPS64::IntrinsicLocationsBuilderMIPS64(CodeGeneratorMIPS64* codegen)
35 : arena_(codegen->GetGraph()->GetArena()) {
36}
37
38Mips64Assembler* IntrinsicCodeGeneratorMIPS64::GetAssembler() {
39 return reinterpret_cast<Mips64Assembler*>(codegen_->GetAssembler());
40}
41
42ArenaAllocator* IntrinsicCodeGeneratorMIPS64::GetAllocator() {
43 return codegen_->GetGraph()->GetArena();
44}
45
Chris Larsen9701c2e2015-09-04 17:22:47 -070046#define __ codegen->GetAssembler()->
47
48static void MoveFromReturnRegister(Location trg,
49 Primitive::Type type,
50 CodeGeneratorMIPS64* codegen) {
51 if (!trg.IsValid()) {
52 DCHECK_EQ(type, Primitive::kPrimVoid);
53 return;
54 }
55
56 DCHECK_NE(type, Primitive::kPrimVoid);
57
58 if (Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) {
59 GpuRegister trg_reg = trg.AsRegister<GpuRegister>();
60 if (trg_reg != V0) {
61 __ Move(V0, trg_reg);
62 }
63 } else {
64 FpuRegister trg_reg = trg.AsFpuRegister<FpuRegister>();
65 if (trg_reg != F0) {
66 if (type == Primitive::kPrimFloat) {
67 __ MovS(F0, trg_reg);
68 } else {
69 __ MovD(F0, trg_reg);
70 }
71 }
72 }
73}
74
75static void MoveArguments(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
76 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
77 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
78}
79
80// Slow-path for fallback (calling the managed code to handle the
81// intrinsic) in an intrinsified call. This will copy the arguments
82// into the positions for a regular call.
83//
84// Note: The actual parameters are required to be in the locations
85// given by the invoke's location summary. If an intrinsic
86// modifies those locations before a slowpath call, they must be
87// restored!
88class IntrinsicSlowPathMIPS64 : public SlowPathCodeMIPS64 {
89 public:
90 explicit IntrinsicSlowPathMIPS64(HInvoke* invoke) : invoke_(invoke) { }
91
92 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
93 CodeGeneratorMIPS64* codegen = down_cast<CodeGeneratorMIPS64*>(codegen_in);
94
95 __ Bind(GetEntryLabel());
96
97 SaveLiveRegisters(codegen, invoke_->GetLocations());
98
99 MoveArguments(invoke_, codegen);
100
101 if (invoke_->IsInvokeStaticOrDirect()) {
102 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(),
103 Location::RegisterLocation(A0));
104 codegen->RecordPcInfo(invoke_, invoke_->GetDexPc(), this);
105 } else {
106 UNIMPLEMENTED(FATAL) << "Non-direct intrinsic slow-path not yet implemented";
107 UNREACHABLE();
108 }
109
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());
119 __ B(GetExitLabel());
120 }
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) {
166 MoveFPToInt(invoke->GetLocations(), true, GetAssembler());
167}
168
169// int java.lang.Float.floatToRawIntBits(float)
170void IntrinsicLocationsBuilderMIPS64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
171 CreateFPToIntLocations(arena_, invoke);
172}
173
174void IntrinsicCodeGeneratorMIPS64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
175 MoveFPToInt(invoke->GetLocations(), false, GetAssembler());
176}
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) {
203 MoveIntToFP(invoke->GetLocations(), true, GetAssembler());
204}
205
206// float java.lang.Float.intBitsToFloat(int)
207void IntrinsicLocationsBuilderMIPS64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
208 CreateIntToFPLocations(arena_, invoke);
209}
210
211void IntrinsicCodeGeneratorMIPS64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
212 MoveIntToFP(invoke->GetLocations(), false, GetAssembler());
213}
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 Larsen0646da72015-09-22 16:02:40 -0700275static void GenNumberOfLeadingZeroes(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
Chris Larsen3039e382015-08-26 07:54:08 -0700276 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
277 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
278
279 if (is64bit) {
280 __ Dclz(out, in);
281 } else {
282 __ Clz(out, in);
283 }
284}
285
286// int java.lang.Integer.numberOfLeadingZeros(int i)
287void IntrinsicLocationsBuilderMIPS64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
288 CreateIntToIntLocations(arena_, invoke);
289}
290
291void IntrinsicCodeGeneratorMIPS64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Chris Larsen0646da72015-09-22 16:02:40 -0700292 GenNumberOfLeadingZeroes(invoke->GetLocations(), false, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700293}
294
295// int java.lang.Long.numberOfLeadingZeros(long i)
296void IntrinsicLocationsBuilderMIPS64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
297 CreateIntToIntLocations(arena_, invoke);
298}
299
300void IntrinsicCodeGeneratorMIPS64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Chris Larsen0646da72015-09-22 16:02:40 -0700301 GenNumberOfLeadingZeroes(invoke->GetLocations(), true, GetAssembler());
302}
303
304static void GenNumberOfTrailingZeroes(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
305 Location in = locations->InAt(0);
306 Location out = locations->Out();
307
308 if (is64bit) {
309 __ Dsbh(out.AsRegister<GpuRegister>(), in.AsRegister<GpuRegister>());
310 __ Dshd(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
311 __ Dbitswap(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
312 __ Dclz(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
313 } else {
314 __ Rotr(out.AsRegister<GpuRegister>(), in.AsRegister<GpuRegister>(), 16);
315 __ Wsbh(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
316 __ Bitswap(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
317 __ Clz(out.AsRegister<GpuRegister>(), out.AsRegister<GpuRegister>());
318 }
319}
320
321// int java.lang.Integer.numberOfTrailingZeros(int i)
322void IntrinsicLocationsBuilderMIPS64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
323 CreateIntToIntLocations(arena_, invoke);
324}
325
326void IntrinsicCodeGeneratorMIPS64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
327 GenNumberOfTrailingZeroes(invoke->GetLocations(), false, GetAssembler());
328}
329
330// int java.lang.Long.numberOfTrailingZeros(long i)
331void IntrinsicLocationsBuilderMIPS64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
332 CreateIntToIntLocations(arena_, invoke);
333}
334
335void IntrinsicCodeGeneratorMIPS64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
336 GenNumberOfTrailingZeroes(invoke->GetLocations(), true, GetAssembler());
Chris Larsen3039e382015-08-26 07:54:08 -0700337}
338
339static void GenReverse(LocationSummary* locations,
340 Primitive::Type type,
341 Mips64Assembler* assembler) {
342 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
343
344 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
345 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
346
347 if (type == Primitive::kPrimInt) {
348 __ Rotr(out, in, 16);
349 __ Wsbh(out, out);
350 __ Bitswap(out, out);
351 } else {
352 __ Dsbh(out, in);
353 __ Dshd(out, out);
354 __ Dbitswap(out, out);
355 }
356}
357
358// int java.lang.Integer.reverse(int)
359void IntrinsicLocationsBuilderMIPS64::VisitIntegerReverse(HInvoke* invoke) {
360 CreateIntToIntLocations(arena_, invoke);
361}
362
363void IntrinsicCodeGeneratorMIPS64::VisitIntegerReverse(HInvoke* invoke) {
364 GenReverse(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
365}
366
367// long java.lang.Long.reverse(long)
368void IntrinsicLocationsBuilderMIPS64::VisitLongReverse(HInvoke* invoke) {
369 CreateIntToIntLocations(arena_, invoke);
370}
371
372void IntrinsicCodeGeneratorMIPS64::VisitLongReverse(HInvoke* invoke) {
373 GenReverse(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
374}
375
Chris Larsen0b7ac982015-09-04 12:54:28 -0700376static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
377 LocationSummary* locations = new (arena) LocationSummary(invoke,
378 LocationSummary::kNoCall,
379 kIntrinsified);
380 locations->SetInAt(0, Location::RequiresFpuRegister());
381 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
382}
383
384static void MathAbsFP(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
385 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
386 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
387
388 if (is64bit) {
389 __ AbsD(out, in);
390 } else {
391 __ AbsS(out, in);
392 }
393}
394
395// double java.lang.Math.abs(double)
396void IntrinsicLocationsBuilderMIPS64::VisitMathAbsDouble(HInvoke* invoke) {
397 CreateFPToFPLocations(arena_, invoke);
398}
399
400void IntrinsicCodeGeneratorMIPS64::VisitMathAbsDouble(HInvoke* invoke) {
401 MathAbsFP(invoke->GetLocations(), true, GetAssembler());
402}
403
404// float java.lang.Math.abs(float)
405void IntrinsicLocationsBuilderMIPS64::VisitMathAbsFloat(HInvoke* invoke) {
406 CreateFPToFPLocations(arena_, invoke);
407}
408
409void IntrinsicCodeGeneratorMIPS64::VisitMathAbsFloat(HInvoke* invoke) {
410 MathAbsFP(invoke->GetLocations(), false, GetAssembler());
411}
412
413static void CreateIntToInt(ArenaAllocator* arena, HInvoke* invoke) {
414 LocationSummary* locations = new (arena) LocationSummary(invoke,
415 LocationSummary::kNoCall,
416 kIntrinsified);
417 locations->SetInAt(0, Location::RequiresRegister());
418 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
419}
420
421static void GenAbsInteger(LocationSummary* locations, bool is64bit, Mips64Assembler* assembler) {
422 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
423 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
424
425 if (is64bit) {
426 __ Dsra32(AT, in, 31);
427 __ Xor(out, in, AT);
428 __ Dsubu(out, out, AT);
429 } else {
430 __ Sra(AT, in, 31);
431 __ Xor(out, in, AT);
432 __ Subu(out, out, AT);
433 }
434}
435
436// int java.lang.Math.abs(int)
437void IntrinsicLocationsBuilderMIPS64::VisitMathAbsInt(HInvoke* invoke) {
438 CreateIntToInt(arena_, invoke);
439}
440
441void IntrinsicCodeGeneratorMIPS64::VisitMathAbsInt(HInvoke* invoke) {
442 GenAbsInteger(invoke->GetLocations(), false, GetAssembler());
443}
444
445// long java.lang.Math.abs(long)
446void IntrinsicLocationsBuilderMIPS64::VisitMathAbsLong(HInvoke* invoke) {
447 CreateIntToInt(arena_, invoke);
448}
449
450void IntrinsicCodeGeneratorMIPS64::VisitMathAbsLong(HInvoke* invoke) {
451 GenAbsInteger(invoke->GetLocations(), true, GetAssembler());
452}
453
454static void GenMinMaxFP(LocationSummary* locations,
455 bool is_min,
456 bool is_double,
457 Mips64Assembler* assembler) {
458 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
459 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
460 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
461
462 if (is_double) {
463 if (is_min) {
464 __ MinD(out, lhs, rhs);
465 } else {
466 __ MaxD(out, lhs, rhs);
467 }
468 } else {
469 if (is_min) {
470 __ MinS(out, lhs, rhs);
471 } else {
472 __ MaxS(out, lhs, rhs);
473 }
474 }
475}
476
477static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
478 LocationSummary* locations = new (arena) LocationSummary(invoke,
479 LocationSummary::kNoCall,
480 kIntrinsified);
481 locations->SetInAt(0, Location::RequiresFpuRegister());
482 locations->SetInAt(1, Location::RequiresFpuRegister());
483 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
484}
485
486// double java.lang.Math.min(double, double)
487void IntrinsicLocationsBuilderMIPS64::VisitMathMinDoubleDouble(HInvoke* invoke) {
488 CreateFPFPToFPLocations(arena_, invoke);
489}
490
491void IntrinsicCodeGeneratorMIPS64::VisitMathMinDoubleDouble(HInvoke* invoke) {
492 GenMinMaxFP(invoke->GetLocations(), true, true, GetAssembler());
493}
494
495// float java.lang.Math.min(float, float)
496void IntrinsicLocationsBuilderMIPS64::VisitMathMinFloatFloat(HInvoke* invoke) {
497 CreateFPFPToFPLocations(arena_, invoke);
498}
499
500void IntrinsicCodeGeneratorMIPS64::VisitMathMinFloatFloat(HInvoke* invoke) {
501 GenMinMaxFP(invoke->GetLocations(), true, false, GetAssembler());
502}
503
504// double java.lang.Math.max(double, double)
505void IntrinsicLocationsBuilderMIPS64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
506 CreateFPFPToFPLocations(arena_, invoke);
507}
508
509void IntrinsicCodeGeneratorMIPS64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
510 GenMinMaxFP(invoke->GetLocations(), false, true, GetAssembler());
511}
512
513// float java.lang.Math.max(float, float)
514void IntrinsicLocationsBuilderMIPS64::VisitMathMaxFloatFloat(HInvoke* invoke) {
515 CreateFPFPToFPLocations(arena_, invoke);
516}
517
518void IntrinsicCodeGeneratorMIPS64::VisitMathMaxFloatFloat(HInvoke* invoke) {
519 GenMinMaxFP(invoke->GetLocations(), false, false, GetAssembler());
520}
521
522static void GenMinMax(LocationSummary* locations,
523 bool is_min,
524 Mips64Assembler* assembler) {
525 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
526 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
527 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
528
Chris Larsen14500822015-10-01 11:35:18 -0700529 // Some architectures, such as ARM and MIPS (prior to r6), have a
530 // conditional move instruction which only changes the target
531 // (output) register if the condition is true (MIPS prior to r6 had
532 // MOVF, MOVT, and MOVZ). The SELEQZ and SELNEZ instructions always
533 // change the target (output) register. If the condition is true the
534 // output register gets the contents of the "rs" register; otherwise,
535 // the output register is set to zero. One consequence of this is
536 // that to implement something like "rd = c==0 ? rs : rt" MIPS64r6
537 // needs to use a pair of SELEQZ/SELNEZ instructions. After
538 // executing this pair of instructions one of the output registers
539 // from the pair will necessarily contain zero. Then the code ORs the
540 // output registers from the SELEQZ/SELNEZ instructions to get the
541 // final result.
542 //
543 // The initial test to see if the output register is same as the
544 // first input register is needed to make sure that value in the
545 // first input register isn't clobbered before we've finished
546 // computing the output value. The logic in the corresponding else
547 // clause performs the same task but makes sure the second input
548 // register isn't clobbered in the event that it's the same register
549 // as the output register; the else clause also handles the case
550 // where the output register is distinct from both the first, and the
551 // second input registers.
Chris Larsen0b7ac982015-09-04 12:54:28 -0700552 if (out == lhs) {
553 __ Slt(AT, rhs, lhs);
554 if (is_min) {
555 __ Seleqz(out, lhs, AT);
556 __ Selnez(AT, rhs, AT);
557 } else {
558 __ Selnez(out, lhs, AT);
559 __ Seleqz(AT, rhs, AT);
560 }
561 } else {
562 __ Slt(AT, lhs, rhs);
563 if (is_min) {
564 __ Seleqz(out, rhs, AT);
565 __ Selnez(AT, lhs, AT);
566 } else {
567 __ Selnez(out, rhs, AT);
568 __ Seleqz(AT, lhs, AT);
569 }
570 }
571 __ Or(out, out, AT);
572}
573
574static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
575 LocationSummary* locations = new (arena) LocationSummary(invoke,
576 LocationSummary::kNoCall,
577 kIntrinsified);
578 locations->SetInAt(0, Location::RequiresRegister());
579 locations->SetInAt(1, Location::RequiresRegister());
580 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
581}
582
583// int java.lang.Math.min(int, int)
584void IntrinsicLocationsBuilderMIPS64::VisitMathMinIntInt(HInvoke* invoke) {
585 CreateIntIntToIntLocations(arena_, invoke);
586}
587
588void IntrinsicCodeGeneratorMIPS64::VisitMathMinIntInt(HInvoke* invoke) {
589 GenMinMax(invoke->GetLocations(), true, GetAssembler());
590}
591
592// long java.lang.Math.min(long, long)
593void IntrinsicLocationsBuilderMIPS64::VisitMathMinLongLong(HInvoke* invoke) {
594 CreateIntIntToIntLocations(arena_, invoke);
595}
596
597void IntrinsicCodeGeneratorMIPS64::VisitMathMinLongLong(HInvoke* invoke) {
598 GenMinMax(invoke->GetLocations(), true, GetAssembler());
599}
600
601// int java.lang.Math.max(int, int)
602void IntrinsicLocationsBuilderMIPS64::VisitMathMaxIntInt(HInvoke* invoke) {
603 CreateIntIntToIntLocations(arena_, invoke);
604}
605
606void IntrinsicCodeGeneratorMIPS64::VisitMathMaxIntInt(HInvoke* invoke) {
607 GenMinMax(invoke->GetLocations(), false, GetAssembler());
608}
609
610// long java.lang.Math.max(long, long)
611void IntrinsicLocationsBuilderMIPS64::VisitMathMaxLongLong(HInvoke* invoke) {
612 CreateIntIntToIntLocations(arena_, invoke);
613}
614
615void IntrinsicCodeGeneratorMIPS64::VisitMathMaxLongLong(HInvoke* invoke) {
616 GenMinMax(invoke->GetLocations(), false, GetAssembler());
617}
618
619// double java.lang.Math.sqrt(double)
620void IntrinsicLocationsBuilderMIPS64::VisitMathSqrt(HInvoke* invoke) {
621 CreateFPToFPLocations(arena_, invoke);
622}
623
624void IntrinsicCodeGeneratorMIPS64::VisitMathSqrt(HInvoke* invoke) {
625 LocationSummary* locations = invoke->GetLocations();
626 Mips64Assembler* assembler = GetAssembler();
627 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
628 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
629
630 __ SqrtD(out, in);
631}
632
633static void CreateFPToFP(ArenaAllocator* arena, HInvoke* invoke) {
634 LocationSummary* locations = new (arena) LocationSummary(invoke,
635 LocationSummary::kNoCall,
636 kIntrinsified);
637 locations->SetInAt(0, Location::RequiresFpuRegister());
638 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
639}
640
641// double java.lang.Math.rint(double)
642void IntrinsicLocationsBuilderMIPS64::VisitMathRint(HInvoke* invoke) {
643 CreateFPToFP(arena_, invoke);
644}
645
646void IntrinsicCodeGeneratorMIPS64::VisitMathRint(HInvoke* invoke) {
647 LocationSummary* locations = invoke->GetLocations();
648 Mips64Assembler* assembler = GetAssembler();
649 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
650 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
651
652 __ RintD(out, in);
653}
654
655// double java.lang.Math.floor(double)
656void IntrinsicLocationsBuilderMIPS64::VisitMathFloor(HInvoke* invoke) {
657 CreateFPToFP(arena_, invoke);
658}
659
Chris Larsen14500822015-10-01 11:35:18 -0700660const constexpr uint16_t kFPLeaveUnchanged = kPositiveZero |
661 kPositiveInfinity |
662 kNegativeZero |
663 kNegativeInfinity |
664 kQuietNaN |
665 kSignalingNaN;
Chris Larsen0b7ac982015-09-04 12:54:28 -0700666
667void IntrinsicCodeGeneratorMIPS64::VisitMathFloor(HInvoke* invoke) {
668 LocationSummary* locations = invoke->GetLocations();
669 Mips64Assembler* assembler = GetAssembler();
670 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
671 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
672
673 Label done;
674
675 // double floor(double in) {
676 // if in.isNaN || in.isInfinite || in.isZero {
677 // return in;
678 // }
679 __ ClassD(out, in);
680 __ Dmfc1(AT, out);
Chris Larsen14500822015-10-01 11:35:18 -0700681 __ Andi(AT, AT, kFPLeaveUnchanged); // +0.0 | +Inf | -0.0 | -Inf | qNaN | sNaN
Chris Larsen0b7ac982015-09-04 12:54:28 -0700682 __ MovD(out, in);
683 __ Bnezc(AT, &done);
684
685 // Long outLong = floor(in);
686 // if outLong == Long.MAX_VALUE {
687 // // floor() has almost certainly returned a value which
688 // // can't be successfully represented as a signed 64-bit
689 // // number. Java expects that the input value will be
690 // // returned in these cases.
691 // // There is also a small probability that floor(in)
692 // // correctly truncates the input value to Long.MAX_VALUE. In
693 // // that case, this exception handling code still does the
694 // // correct thing.
695 // return in;
696 // }
697 __ FloorLD(out, in);
698 __ Dmfc1(AT, out);
699 __ MovD(out, in);
700 __ LoadConst64(TMP, kPrimLongMax);
701 __ Beqc(AT, TMP, &done);
702
703 // double out = outLong;
704 // return out;
705 __ Dmtc1(AT, out);
706 __ Cvtdl(out, out);
707 __ Bind(&done);
708 // }
709}
710
711// double java.lang.Math.ceil(double)
712void IntrinsicLocationsBuilderMIPS64::VisitMathCeil(HInvoke* invoke) {
713 CreateFPToFP(arena_, invoke);
714}
715
716void IntrinsicCodeGeneratorMIPS64::VisitMathCeil(HInvoke* invoke) {
717 LocationSummary* locations = invoke->GetLocations();
718 Mips64Assembler* assembler = GetAssembler();
719 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
720 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
721
722 Label done;
723
724 // double ceil(double in) {
725 // if in.isNaN || in.isInfinite || in.isZero {
726 // return in;
727 // }
728 __ ClassD(out, in);
729 __ Dmfc1(AT, out);
Chris Larsen14500822015-10-01 11:35:18 -0700730 __ Andi(AT, AT, kFPLeaveUnchanged); // +0.0 | +Inf | -0.0 | -Inf | qNaN | sNaN
Chris Larsen0b7ac982015-09-04 12:54:28 -0700731 __ MovD(out, in);
732 __ Bnezc(AT, &done);
733
734 // Long outLong = ceil(in);
735 // if outLong == Long.MAX_VALUE {
736 // // ceil() has almost certainly returned a value which
737 // // can't be successfully represented as a signed 64-bit
738 // // number. Java expects that the input value will be
739 // // returned in these cases.
740 // // There is also a small probability that ceil(in)
741 // // correctly rounds up the input value to Long.MAX_VALUE. In
742 // // that case, this exception handling code still does the
743 // // correct thing.
744 // return in;
745 // }
746 __ CeilLD(out, in);
747 __ Dmfc1(AT, out);
748 __ MovD(out, in);
749 __ LoadConst64(TMP, kPrimLongMax);
750 __ Beqc(AT, TMP, &done);
751
752 // double out = outLong;
753 // return out;
754 __ Dmtc1(AT, out);
755 __ Cvtdl(out, out);
756 __ Bind(&done);
757 // }
758}
759
Chris Larsen70fb1f42015-09-04 10:15:27 -0700760// byte libcore.io.Memory.peekByte(long address)
761void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekByte(HInvoke* invoke) {
762 CreateIntToIntLocations(arena_, invoke);
763}
764
765void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekByte(HInvoke* invoke) {
766 Mips64Assembler* assembler = GetAssembler();
767 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
768 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
769
770 __ Lb(out, adr, 0);
771}
772
773// short libcore.io.Memory.peekShort(long address)
774void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekShortNative(HInvoke* invoke) {
775 CreateIntToIntLocations(arena_, invoke);
776}
777
778void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekShortNative(HInvoke* invoke) {
779 Mips64Assembler* assembler = GetAssembler();
780 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
781 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
782
783 __ Lh(out, adr, 0);
784}
785
786// int libcore.io.Memory.peekInt(long address)
787void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekIntNative(HInvoke* invoke) {
788 CreateIntToIntLocations(arena_, invoke);
789}
790
791void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekIntNative(HInvoke* invoke) {
792 Mips64Assembler* assembler = GetAssembler();
793 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
794 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
795
796 __ Lw(out, adr, 0);
797}
798
799// long libcore.io.Memory.peekLong(long address)
800void IntrinsicLocationsBuilderMIPS64::VisitMemoryPeekLongNative(HInvoke* invoke) {
801 CreateIntToIntLocations(arena_, invoke);
802}
803
804void IntrinsicCodeGeneratorMIPS64::VisitMemoryPeekLongNative(HInvoke* invoke) {
805 Mips64Assembler* assembler = GetAssembler();
806 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
807 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
808
809 __ Ld(out, adr, 0);
810}
811
812static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
813 LocationSummary* locations = new (arena) LocationSummary(invoke,
814 LocationSummary::kNoCall,
815 kIntrinsified);
816 locations->SetInAt(0, Location::RequiresRegister());
817 locations->SetInAt(1, Location::RequiresRegister());
818}
819
820// void libcore.io.Memory.pokeByte(long address, byte value)
821void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeByte(HInvoke* invoke) {
822 CreateIntIntToVoidLocations(arena_, invoke);
823}
824
825void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeByte(HInvoke* invoke) {
826 Mips64Assembler* assembler = GetAssembler();
827 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
828 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
829
830 __ Sb(val, adr, 0);
831}
832
833// void libcore.io.Memory.pokeShort(long address, short value)
834void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeShortNative(HInvoke* invoke) {
835 CreateIntIntToVoidLocations(arena_, invoke);
836}
837
838void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeShortNative(HInvoke* invoke) {
839 Mips64Assembler* assembler = GetAssembler();
840 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
841 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
842
843 __ Sh(val, adr, 0);
844}
845
846// void libcore.io.Memory.pokeInt(long address, int value)
847void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeIntNative(HInvoke* invoke) {
848 CreateIntIntToVoidLocations(arena_, invoke);
849}
850
851void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeIntNative(HInvoke* invoke) {
852 Mips64Assembler* assembler = GetAssembler();
853 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
854 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
855
856 __ Sw(val, adr, 00);
857}
858
859// void libcore.io.Memory.pokeLong(long address, long value)
860void IntrinsicLocationsBuilderMIPS64::VisitMemoryPokeLongNative(HInvoke* invoke) {
861 CreateIntIntToVoidLocations(arena_, invoke);
862}
863
864void IntrinsicCodeGeneratorMIPS64::VisitMemoryPokeLongNative(HInvoke* invoke) {
865 Mips64Assembler* assembler = GetAssembler();
866 GpuRegister adr = invoke->GetLocations()->InAt(0).AsRegister<GpuRegister>();
867 GpuRegister val = invoke->GetLocations()->InAt(1).AsRegister<GpuRegister>();
868
869 __ Sd(val, adr, 0);
870}
871
Chris Larsen49e55392015-09-04 16:04:03 -0700872// Thread java.lang.Thread.currentThread()
873void IntrinsicLocationsBuilderMIPS64::VisitThreadCurrentThread(HInvoke* invoke) {
874 LocationSummary* locations = new (arena_) LocationSummary(invoke,
875 LocationSummary::kNoCall,
876 kIntrinsified);
877 locations->SetOut(Location::RequiresRegister());
878}
879
880void IntrinsicCodeGeneratorMIPS64::VisitThreadCurrentThread(HInvoke* invoke) {
881 Mips64Assembler* assembler = GetAssembler();
882 GpuRegister out = invoke->GetLocations()->Out().AsRegister<GpuRegister>();
883
884 __ LoadFromOffset(kLoadUnsignedWord,
885 out,
886 TR,
887 Thread::PeerOffset<kMips64PointerSize>().Int32Value());
888}
889
Chris Larsen1360ada2015-09-04 23:38:16 -0700890static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
891 LocationSummary* locations = new (arena) LocationSummary(invoke,
892 LocationSummary::kNoCall,
893 kIntrinsified);
894 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
895 locations->SetInAt(1, Location::RequiresRegister());
896 locations->SetInAt(2, Location::RequiresRegister());
897 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
898}
899
900static void GenUnsafeGet(HInvoke* invoke,
901 Primitive::Type type,
902 bool is_volatile,
903 CodeGeneratorMIPS64* codegen) {
904 LocationSummary* locations = invoke->GetLocations();
905 DCHECK((type == Primitive::kPrimInt) ||
906 (type == Primitive::kPrimLong) ||
907 (type == Primitive::kPrimNot));
908 Mips64Assembler* assembler = codegen->GetAssembler();
909 // Object pointer.
910 GpuRegister base = locations->InAt(1).AsRegister<GpuRegister>();
911 // Long offset.
912 GpuRegister offset = locations->InAt(2).AsRegister<GpuRegister>();
913 GpuRegister trg = locations->Out().AsRegister<GpuRegister>();
914
915 __ Daddu(TMP, base, offset);
916 if (is_volatile) {
917 __ Sync(0);
918 }
919 switch (type) {
920 case Primitive::kPrimInt:
921 __ Lw(trg, TMP, 0);
922 break;
923
924 case Primitive::kPrimNot:
925 __ Lwu(trg, TMP, 0);
926 break;
927
928 case Primitive::kPrimLong:
929 __ Ld(trg, TMP, 0);
930 break;
931
932 default:
933 LOG(FATAL) << "Unsupported op size " << type;
934 UNREACHABLE();
935 }
936}
937
938// int sun.misc.Unsafe.getInt(Object o, long offset)
939void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGet(HInvoke* invoke) {
940 CreateIntIntIntToIntLocations(arena_, invoke);
941}
942
943void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGet(HInvoke* invoke) {
944 GenUnsafeGet(invoke, Primitive::kPrimInt, false, codegen_);
945}
946
947// int sun.misc.Unsafe.getIntVolatile(Object o, long offset)
948void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetVolatile(HInvoke* invoke) {
949 CreateIntIntIntToIntLocations(arena_, invoke);
950}
951
952void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetVolatile(HInvoke* invoke) {
953 GenUnsafeGet(invoke, Primitive::kPrimInt, true, codegen_);
954}
955
956// long sun.misc.Unsafe.getLong(Object o, long offset)
957void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetLong(HInvoke* invoke) {
958 CreateIntIntIntToIntLocations(arena_, invoke);
959}
960
961void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetLong(HInvoke* invoke) {
962 GenUnsafeGet(invoke, Primitive::kPrimLong, false, codegen_);
963}
964
965// long sun.misc.Unsafe.getLongVolatile(Object o, long offset)
966void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
967 CreateIntIntIntToIntLocations(arena_, invoke);
968}
969
970void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
971 GenUnsafeGet(invoke, Primitive::kPrimLong, true, codegen_);
972}
973
974// Object sun.misc.Unsafe.getObject(Object o, long offset)
975void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetObject(HInvoke* invoke) {
976 CreateIntIntIntToIntLocations(arena_, invoke);
977}
978
979void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetObject(HInvoke* invoke) {
980 GenUnsafeGet(invoke, Primitive::kPrimNot, false, codegen_);
981}
982
983// Object sun.misc.Unsafe.getObjectVolatile(Object o, long offset)
984void IntrinsicLocationsBuilderMIPS64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
985 CreateIntIntIntToIntLocations(arena_, invoke);
986}
987
988void IntrinsicCodeGeneratorMIPS64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
989 GenUnsafeGet(invoke, Primitive::kPrimNot, true, codegen_);
990}
991
992static void CreateIntIntIntIntToVoid(ArenaAllocator* arena, HInvoke* invoke) {
993 LocationSummary* locations = new (arena) LocationSummary(invoke,
994 LocationSummary::kNoCall,
995 kIntrinsified);
996 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
997 locations->SetInAt(1, Location::RequiresRegister());
998 locations->SetInAt(2, Location::RequiresRegister());
999 locations->SetInAt(3, Location::RequiresRegister());
1000}
1001
1002static void GenUnsafePut(LocationSummary* locations,
1003 Primitive::Type type,
1004 bool is_volatile,
1005 bool is_ordered,
1006 CodeGeneratorMIPS64* codegen) {
1007 DCHECK((type == Primitive::kPrimInt) ||
1008 (type == Primitive::kPrimLong) ||
1009 (type == Primitive::kPrimNot));
1010 Mips64Assembler* assembler = codegen->GetAssembler();
1011 // Object pointer.
1012 GpuRegister base = locations->InAt(1).AsRegister<GpuRegister>();
1013 // Long offset.
1014 GpuRegister offset = locations->InAt(2).AsRegister<GpuRegister>();
1015 GpuRegister value = locations->InAt(3).AsRegister<GpuRegister>();
1016
1017 __ Daddu(TMP, base, offset);
1018 if (is_volatile || is_ordered) {
1019 __ Sync(0);
1020 }
1021 switch (type) {
1022 case Primitive::kPrimInt:
1023 case Primitive::kPrimNot:
1024 __ Sw(value, TMP, 0);
1025 break;
1026
1027 case Primitive::kPrimLong:
1028 __ Sd(value, TMP, 0);
1029 break;
1030
1031 default:
1032 LOG(FATAL) << "Unsupported op size " << type;
1033 UNREACHABLE();
1034 }
1035 if (is_volatile) {
1036 __ Sync(0);
1037 }
1038
1039 if (type == Primitive::kPrimNot) {
1040 codegen->MarkGCCard(base, value);
1041 }
1042}
1043
1044// void sun.misc.Unsafe.putInt(Object o, long offset, int x)
1045void IntrinsicLocationsBuilderMIPS64::VisitUnsafePut(HInvoke* invoke) {
1046 CreateIntIntIntIntToVoid(arena_, invoke);
1047}
1048
1049void IntrinsicCodeGeneratorMIPS64::VisitUnsafePut(HInvoke* invoke) {
1050 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, false, codegen_);
1051}
1052
1053// void sun.misc.Unsafe.putOrderedInt(Object o, long offset, int x)
1054void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutOrdered(HInvoke* invoke) {
1055 CreateIntIntIntIntToVoid(arena_, invoke);
1056}
1057
1058void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutOrdered(HInvoke* invoke) {
1059 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, true, codegen_);
1060}
1061
1062// void sun.misc.Unsafe.putIntVolatile(Object o, long offset, int x)
1063void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutVolatile(HInvoke* invoke) {
1064 CreateIntIntIntIntToVoid(arena_, invoke);
1065}
1066
1067void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutVolatile(HInvoke* invoke) {
1068 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, false, codegen_);
1069}
1070
1071// void sun.misc.Unsafe.putObject(Object o, long offset, Object x)
1072void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutObject(HInvoke* invoke) {
1073 CreateIntIntIntIntToVoid(arena_, invoke);
1074}
1075
1076void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutObject(HInvoke* invoke) {
1077 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, false, codegen_);
1078}
1079
1080// void sun.misc.Unsafe.putOrderedObject(Object o, long offset, Object x)
1081void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1082 CreateIntIntIntIntToVoid(arena_, invoke);
1083}
1084
1085void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1086 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, true, codegen_);
1087}
1088
1089// void sun.misc.Unsafe.putObjectVolatile(Object o, long offset, Object x)
1090void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1091 CreateIntIntIntIntToVoid(arena_, invoke);
1092}
1093
1094void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1095 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, false, codegen_);
1096}
1097
1098// void sun.misc.Unsafe.putLong(Object o, long offset, long x)
1099void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutLong(HInvoke* invoke) {
1100 CreateIntIntIntIntToVoid(arena_, invoke);
1101}
1102
1103void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutLong(HInvoke* invoke) {
1104 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, false, codegen_);
1105}
1106
1107// void sun.misc.Unsafe.putOrderedLong(Object o, long offset, long x)
1108void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1109 CreateIntIntIntIntToVoid(arena_, invoke);
1110}
1111
1112void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1113 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, true, codegen_);
1114}
1115
1116// void sun.misc.Unsafe.putLongVolatile(Object o, long offset, long x)
1117void IntrinsicLocationsBuilderMIPS64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1118 CreateIntIntIntIntToVoid(arena_, invoke);
1119}
1120
1121void IntrinsicCodeGeneratorMIPS64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1122 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, false, codegen_);
1123}
1124
Chris Larsen9701c2e2015-09-04 17:22:47 -07001125// char java.lang.String.charAt(int index)
1126void IntrinsicLocationsBuilderMIPS64::VisitStringCharAt(HInvoke* invoke) {
1127 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1128 LocationSummary::kCallOnSlowPath,
1129 kIntrinsified);
1130 locations->SetInAt(0, Location::RequiresRegister());
1131 locations->SetInAt(1, Location::RequiresRegister());
1132 locations->SetOut(Location::SameAsFirstInput());
1133}
1134
1135void IntrinsicCodeGeneratorMIPS64::VisitStringCharAt(HInvoke* invoke) {
1136 LocationSummary* locations = invoke->GetLocations();
1137 Mips64Assembler* assembler = GetAssembler();
1138
1139 // Location of reference to data array
1140 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1141 // Location of count
1142 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1143
1144 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1145 GpuRegister idx = locations->InAt(1).AsRegister<GpuRegister>();
1146 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1147
1148 // TODO: Maybe we can support range check elimination. Overall,
1149 // though, I think it's not worth the cost.
1150 // TODO: For simplicity, the index parameter is requested in a
1151 // register, so different from Quick we will not optimize the
1152 // code for constants (which would save a register).
1153
1154 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1155 codegen_->AddSlowPath(slow_path);
1156
1157 // Load the string size
1158 __ Lw(TMP, obj, count_offset);
1159 codegen_->MaybeRecordImplicitNullCheck(invoke);
1160 // Revert to slow path if idx is too large, or negative
1161 __ Bgeuc(idx, TMP, slow_path->GetEntryLabel());
1162
1163 // out = obj[2*idx].
1164 __ Sll(TMP, idx, 1); // idx * 2
1165 __ Daddu(TMP, TMP, obj); // Address of char at location idx
1166 __ Lhu(out, TMP, value_offset); // Load char at location idx
1167
1168 __ Bind(slow_path->GetExitLabel());
1169}
1170
1171// int java.lang.String.compareTo(String anotherString)
1172void IntrinsicLocationsBuilderMIPS64::VisitStringCompareTo(HInvoke* invoke) {
1173 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1174 LocationSummary::kCall,
1175 kIntrinsified);
1176 InvokeRuntimeCallingConvention calling_convention;
1177 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1178 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1179 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1180 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1181}
1182
1183void IntrinsicCodeGeneratorMIPS64::VisitStringCompareTo(HInvoke* invoke) {
1184 Mips64Assembler* assembler = GetAssembler();
1185 LocationSummary* locations = invoke->GetLocations();
1186
1187 // Note that the null check must have been done earlier.
1188 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1189
1190 GpuRegister argument = locations->InAt(1).AsRegister<GpuRegister>();
1191 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1192 codegen_->AddSlowPath(slow_path);
1193 __ Beqzc(argument, slow_path->GetEntryLabel());
1194
1195 __ LoadFromOffset(kLoadDoubleword,
1196 TMP,
1197 TR,
1198 QUICK_ENTRYPOINT_OFFSET(kMips64WordSize,
1199 pStringCompareTo).Int32Value());
1200 __ Jalr(TMP);
1201 __ Nop();
1202 __ Bind(slow_path->GetExitLabel());
1203}
1204
1205static void GenerateStringIndexOf(HInvoke* invoke,
1206 Mips64Assembler* assembler,
1207 CodeGeneratorMIPS64* codegen,
1208 ArenaAllocator* allocator,
1209 bool start_at_zero) {
1210 LocationSummary* locations = invoke->GetLocations();
1211 GpuRegister tmp_reg = start_at_zero ? locations->GetTemp(0).AsRegister<GpuRegister>() : TMP;
1212
1213 // Note that the null check must have been done earlier.
1214 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1215
1216 // Check for code points > 0xFFFF. Either a slow-path check when we
1217 // don't know statically, or directly dispatch if we have a constant.
1218 SlowPathCodeMIPS64* slow_path = nullptr;
1219 if (invoke->InputAt(1)->IsIntConstant()) {
1220 if (!IsUint<16>(invoke->InputAt(1)->AsIntConstant()->GetValue())) {
1221 // Always needs the slow-path. We could directly dispatch to it,
1222 // but this case should be rare, so for simplicity just put the
1223 // full slow-path down and branch unconditionally.
1224 slow_path = new (allocator) IntrinsicSlowPathMIPS64(invoke);
1225 codegen->AddSlowPath(slow_path);
1226 __ B(slow_path->GetEntryLabel());
1227 __ Bind(slow_path->GetExitLabel());
1228 return;
1229 }
1230 } else {
1231 GpuRegister char_reg = locations->InAt(1).AsRegister<GpuRegister>();
1232 __ LoadConst32(tmp_reg, std::numeric_limits<uint16_t>::max());
1233 slow_path = new (allocator) IntrinsicSlowPathMIPS64(invoke);
1234 codegen->AddSlowPath(slow_path);
1235 __ Bltuc(tmp_reg, char_reg, slow_path->GetEntryLabel()); // UTF-16 required
1236 }
1237
1238 if (start_at_zero) {
1239 DCHECK_EQ(tmp_reg, A2);
1240 // Start-index = 0.
1241 __ Clear(tmp_reg);
1242 } else {
1243 __ Slt(TMP, A2, ZERO); // if fromIndex < 0
1244 __ Seleqz(A2, A2, TMP); // fromIndex = 0
1245 }
1246
1247 __ LoadFromOffset(kLoadDoubleword,
1248 TMP,
1249 TR,
1250 QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, pIndexOf).Int32Value());
1251 __ Jalr(TMP);
1252 __ Nop();
1253
1254 if (slow_path != nullptr) {
1255 __ Bind(slow_path->GetExitLabel());
1256 }
1257}
1258
1259// int java.lang.String.indexOf(int ch)
1260void IntrinsicLocationsBuilderMIPS64::VisitStringIndexOf(HInvoke* invoke) {
1261 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1262 LocationSummary::kCall,
1263 kIntrinsified);
1264 // We have a hand-crafted assembly stub that follows the runtime
1265 // calling convention. So it's best to align the inputs accordingly.
1266 InvokeRuntimeCallingConvention calling_convention;
1267 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1268 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1269 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1270 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1271
1272 // Need a temp for slow-path codepoint compare, and need to send start-index=0.
1273 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1274}
1275
1276void IntrinsicCodeGeneratorMIPS64::VisitStringIndexOf(HInvoke* invoke) {
1277 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), true);
1278}
1279
1280// int java.lang.String.indexOf(int ch, int fromIndex)
1281void IntrinsicLocationsBuilderMIPS64::VisitStringIndexOfAfter(HInvoke* invoke) {
1282 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1283 LocationSummary::kCall,
1284 kIntrinsified);
1285 // We have a hand-crafted assembly stub that follows the runtime
1286 // calling convention. So it's best to align the inputs accordingly.
1287 InvokeRuntimeCallingConvention calling_convention;
1288 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1289 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1290 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1291 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1292 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1293}
1294
1295void IntrinsicCodeGeneratorMIPS64::VisitStringIndexOfAfter(HInvoke* invoke) {
1296 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), false);
1297}
1298
1299// java.lang.String.String(byte[] bytes)
1300void IntrinsicLocationsBuilderMIPS64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1301 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1302 LocationSummary::kCall,
1303 kIntrinsified);
1304 InvokeRuntimeCallingConvention calling_convention;
1305 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1306 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1307 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1308 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1309 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1310 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1311}
1312
1313void IntrinsicCodeGeneratorMIPS64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1314 Mips64Assembler* assembler = GetAssembler();
1315 LocationSummary* locations = invoke->GetLocations();
1316
1317 GpuRegister byte_array = locations->InAt(0).AsRegister<GpuRegister>();
1318 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1319 codegen_->AddSlowPath(slow_path);
1320 __ Beqzc(byte_array, slow_path->GetEntryLabel());
1321
1322 __ LoadFromOffset(kLoadDoubleword,
1323 TMP,
1324 TR,
1325 QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, pAllocStringFromBytes).Int32Value());
1326 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1327 __ Jalr(TMP);
1328 __ Nop();
1329 __ Bind(slow_path->GetExitLabel());
1330}
1331
1332// java.lang.String.String(char[] value)
1333void IntrinsicLocationsBuilderMIPS64::VisitStringNewStringFromChars(HInvoke* invoke) {
1334 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1335 LocationSummary::kCall,
1336 kIntrinsified);
1337 InvokeRuntimeCallingConvention calling_convention;
1338 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1339 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1340 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1341 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1342 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1343}
1344
1345void IntrinsicCodeGeneratorMIPS64::VisitStringNewStringFromChars(HInvoke* invoke) {
1346 Mips64Assembler* assembler = GetAssembler();
1347
1348 __ LoadFromOffset(kLoadDoubleword,
1349 TMP,
1350 TR,
1351 QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, pAllocStringFromChars).Int32Value());
1352 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1353 __ Jalr(TMP);
1354 __ Nop();
1355}
1356
1357// java.lang.String.String(String original)
1358void IntrinsicLocationsBuilderMIPS64::VisitStringNewStringFromString(HInvoke* invoke) {
1359 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1360 LocationSummary::kCall,
1361 kIntrinsified);
1362 InvokeRuntimeCallingConvention calling_convention;
1363 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1364 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1365 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1366 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
1367 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<GpuRegister>()));
1368}
1369
1370void IntrinsicCodeGeneratorMIPS64::VisitStringNewStringFromString(HInvoke* invoke) {
1371 Mips64Assembler* assembler = GetAssembler();
1372 LocationSummary* locations = invoke->GetLocations();
1373
1374 GpuRegister string_to_copy = locations->InAt(0).AsRegister<GpuRegister>();
1375 SlowPathCodeMIPS64* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS64(invoke);
1376 codegen_->AddSlowPath(slow_path);
1377 __ Beqzc(string_to_copy, slow_path->GetEntryLabel());
1378
1379 __ LoadFromOffset(kLoadDoubleword,
1380 TMP,
1381 TR,
1382 QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, pAllocStringFromString).Int32Value());
1383 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1384 __ Jalr(TMP);
1385 __ Nop();
1386 __ Bind(slow_path->GetExitLabel());
1387}
1388
Chris Larsen3039e382015-08-26 07:54:08 -07001389// Unimplemented intrinsics.
1390
1391#define UNIMPLEMENTED_INTRINSIC(Name) \
1392void IntrinsicLocationsBuilderMIPS64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1393} \
1394void IntrinsicCodeGeneratorMIPS64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1395}
1396
Chris Larsen3039e382015-08-26 07:54:08 -07001397UNIMPLEMENTED_INTRINSIC(MathRoundDouble)
1398UNIMPLEMENTED_INTRINSIC(MathRoundFloat)
Chris Larsen0b7ac982015-09-04 12:54:28 -07001399
Chris Larsen3039e382015-08-26 07:54:08 -07001400UNIMPLEMENTED_INTRINSIC(UnsafeCASInt)
1401UNIMPLEMENTED_INTRINSIC(UnsafeCASLong)
1402UNIMPLEMENTED_INTRINSIC(UnsafeCASObject)
Chris Larsen3039e382015-08-26 07:54:08 -07001403UNIMPLEMENTED_INTRINSIC(StringEquals)
Chris Larsen3039e382015-08-26 07:54:08 -07001404UNIMPLEMENTED_INTRINSIC(LongRotateLeft)
1405UNIMPLEMENTED_INTRINSIC(LongRotateRight)
Chris Larsen3039e382015-08-26 07:54:08 -07001406UNIMPLEMENTED_INTRINSIC(IntegerRotateLeft)
1407UNIMPLEMENTED_INTRINSIC(IntegerRotateRight)
Chris Larsen3039e382015-08-26 07:54:08 -07001408
1409UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
1410UNIMPLEMENTED_INTRINSIC(StringGetCharsNoCheck)
1411UNIMPLEMENTED_INTRINSIC(SystemArrayCopyChar)
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001412UNIMPLEMENTED_INTRINSIC(SystemArrayCopy)
Chris Larsen3039e382015-08-26 07:54:08 -07001413
1414#undef UNIMPLEMENTED_INTRINSIC
1415
1416#undef __
1417
1418} // namespace mips64
1419} // namespace art