blob: abf5b122c82b721f45039395ae7ff2f7bd043910 [file] [log] [blame]
Chris Larsen701566a2015-10-27 15:29:13 -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_mips.h"
18
19#include "arch/mips/instruction_set_features_mips.h"
20#include "art_method.h"
21#include "code_generator_mips.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/mips/assembler_mips.h"
28#include "utils/mips/constants_mips.h"
29
30namespace art {
31
32namespace mips {
33
34IntrinsicLocationsBuilderMIPS::IntrinsicLocationsBuilderMIPS(CodeGeneratorMIPS* codegen)
Chris Larsen5633ce72017-04-10 15:47:40 -070035 : codegen_(codegen), arena_(codegen->GetGraph()->GetArena()) {
Chris Larsen701566a2015-10-27 15:29:13 -070036}
37
38MipsAssembler* IntrinsicCodeGeneratorMIPS::GetAssembler() {
39 return reinterpret_cast<MipsAssembler*>(codegen_->GetAssembler());
40}
41
42ArenaAllocator* IntrinsicCodeGeneratorMIPS::GetAllocator() {
43 return codegen_->GetGraph()->GetArena();
44}
45
Alexey Frunzebb9863a2016-01-11 15:51:16 -080046inline bool IntrinsicCodeGeneratorMIPS::IsR2OrNewer() const {
Chris Larsene16ce5a2015-11-18 12:30:20 -080047 return codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
48}
49
Alexey Frunzebb9863a2016-01-11 15:51:16 -080050inline bool IntrinsicCodeGeneratorMIPS::IsR6() const {
Chris Larsene16ce5a2015-11-18 12:30:20 -080051 return codegen_->GetInstructionSetFeatures().IsR6();
52}
53
Alexey Frunzebb9863a2016-01-11 15:51:16 -080054inline bool IntrinsicCodeGeneratorMIPS::Is32BitFPU() const {
55 return codegen_->GetInstructionSetFeatures().Is32BitFloatingPoint();
56}
57
Chris Larsen701566a2015-10-27 15:29:13 -070058#define __ codegen->GetAssembler()->
59
60static void MoveFromReturnRegister(Location trg,
61 Primitive::Type type,
62 CodeGeneratorMIPS* codegen) {
63 if (!trg.IsValid()) {
64 DCHECK_EQ(type, Primitive::kPrimVoid);
65 return;
66 }
67
68 DCHECK_NE(type, Primitive::kPrimVoid);
69
70 if (Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) {
71 Register trg_reg = trg.AsRegister<Register>();
72 if (trg_reg != V0) {
73 __ Move(V0, trg_reg);
74 }
75 } else {
76 FRegister trg_reg = trg.AsFpuRegister<FRegister>();
77 if (trg_reg != F0) {
78 if (type == Primitive::kPrimFloat) {
79 __ MovS(F0, trg_reg);
80 } else {
81 __ MovD(F0, trg_reg);
82 }
83 }
84 }
85}
86
87static void MoveArguments(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
88 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
89 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
90}
91
92// Slow-path for fallback (calling the managed code to handle the
93// intrinsic) in an intrinsified call. This will copy the arguments
94// into the positions for a regular call.
95//
96// Note: The actual parameters are required to be in the locations
97// given by the invoke's location summary. If an intrinsic
98// modifies those locations before a slowpath call, they must be
99// restored!
100class IntrinsicSlowPathMIPS : public SlowPathCodeMIPS {
101 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000102 explicit IntrinsicSlowPathMIPS(HInvoke* invoke) : SlowPathCodeMIPS(invoke), invoke_(invoke) { }
Chris Larsen701566a2015-10-27 15:29:13 -0700103
104 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
105 CodeGeneratorMIPS* codegen = down_cast<CodeGeneratorMIPS*>(codegen_in);
106
107 __ Bind(GetEntryLabel());
108
109 SaveLiveRegisters(codegen, invoke_->GetLocations());
110
111 MoveArguments(invoke_, codegen);
112
113 if (invoke_->IsInvokeStaticOrDirect()) {
114 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(),
115 Location::RegisterLocation(A0));
Chris Larsen701566a2015-10-27 15:29:13 -0700116 } else {
Chris Larsen3acee732015-11-18 13:31:08 -0800117 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), Location::RegisterLocation(A0));
Chris Larsen701566a2015-10-27 15:29:13 -0700118 }
Chris Larsen3acee732015-11-18 13:31:08 -0800119 codegen->RecordPcInfo(invoke_, invoke_->GetDexPc(), this);
Chris Larsen701566a2015-10-27 15:29:13 -0700120
121 // Copy the result back to the expected output.
122 Location out = invoke_->GetLocations()->Out();
123 if (out.IsValid()) {
124 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
125 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
126 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
127 }
128
129 RestoreLiveRegisters(codegen, invoke_->GetLocations());
130 __ B(GetExitLabel());
131 }
132
133 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathMIPS"; }
134
135 private:
136 // The instruction where this slow path is happening.
137 HInvoke* const invoke_;
138
139 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathMIPS);
140};
141
142#undef __
143
144bool IntrinsicLocationsBuilderMIPS::TryDispatch(HInvoke* invoke) {
145 Dispatch(invoke);
146 LocationSummary* res = invoke->GetLocations();
147 return res != nullptr && res->Intrinsified();
148}
149
150#define __ assembler->
151
Chris Larsen3f8bf652015-10-28 10:08:56 -0700152static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
153 LocationSummary* locations = new (arena) LocationSummary(invoke,
154 LocationSummary::kNoCall,
155 kIntrinsified);
156 locations->SetInAt(0, Location::RequiresFpuRegister());
157 locations->SetOut(Location::RequiresRegister());
158}
159
160static void MoveFPToInt(LocationSummary* locations, bool is64bit, MipsAssembler* assembler) {
161 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
162
163 if (is64bit) {
164 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
165 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
166
167 __ Mfc1(out_lo, in);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800168 __ MoveFromFpuHigh(out_hi, in);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700169 } else {
170 Register out = locations->Out().AsRegister<Register>();
171
172 __ Mfc1(out, in);
173 }
174}
175
176// long java.lang.Double.doubleToRawLongBits(double)
177void IntrinsicLocationsBuilderMIPS::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
178 CreateFPToIntLocations(arena_, invoke);
179}
180
181void IntrinsicCodeGeneratorMIPS::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000182 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700183}
184
185// int java.lang.Float.floatToRawIntBits(float)
186void IntrinsicLocationsBuilderMIPS::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
187 CreateFPToIntLocations(arena_, invoke);
188}
189
190void IntrinsicCodeGeneratorMIPS::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000191 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700192}
193
194static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
195 LocationSummary* locations = new (arena) LocationSummary(invoke,
196 LocationSummary::kNoCall,
197 kIntrinsified);
198 locations->SetInAt(0, Location::RequiresRegister());
199 locations->SetOut(Location::RequiresFpuRegister());
200}
201
202static void MoveIntToFP(LocationSummary* locations, bool is64bit, MipsAssembler* assembler) {
203 FRegister out = locations->Out().AsFpuRegister<FRegister>();
204
205 if (is64bit) {
206 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
207 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
208
209 __ Mtc1(in_lo, out);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800210 __ MoveToFpuHigh(in_hi, out);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700211 } else {
212 Register in = locations->InAt(0).AsRegister<Register>();
213
214 __ Mtc1(in, out);
215 }
216}
217
218// double java.lang.Double.longBitsToDouble(long)
219void IntrinsicLocationsBuilderMIPS::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
220 CreateIntToFPLocations(arena_, invoke);
221}
222
223void IntrinsicCodeGeneratorMIPS::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000224 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700225}
226
227// float java.lang.Float.intBitsToFloat(int)
228void IntrinsicLocationsBuilderMIPS::VisitFloatIntBitsToFloat(HInvoke* invoke) {
229 CreateIntToFPLocations(arena_, invoke);
230}
231
232void IntrinsicCodeGeneratorMIPS::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000233 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700234}
235
Chris Larsen86829602015-11-18 12:27:52 -0800236static void CreateIntToIntLocations(ArenaAllocator* arena,
237 HInvoke* invoke,
238 Location::OutputOverlap overlaps = Location::kNoOutputOverlap) {
Chris Larsen3f8bf652015-10-28 10:08:56 -0700239 LocationSummary* locations = new (arena) LocationSummary(invoke,
240 LocationSummary::kNoCall,
241 kIntrinsified);
242 locations->SetInAt(0, Location::RequiresRegister());
Chris Larsen86829602015-11-18 12:27:52 -0800243 locations->SetOut(Location::RequiresRegister(), overlaps);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700244}
245
Chris Larsen70014c82015-11-18 12:26:08 -0800246static void GenReverse(LocationSummary* locations,
247 Primitive::Type type,
248 bool isR2OrNewer,
249 bool isR6,
250 bool reverseBits,
251 MipsAssembler* assembler) {
Chris Larsen3f8bf652015-10-28 10:08:56 -0700252 DCHECK(type == Primitive::kPrimShort ||
253 type == Primitive::kPrimInt ||
254 type == Primitive::kPrimLong);
Chris Larsen70014c82015-11-18 12:26:08 -0800255 DCHECK(type != Primitive::kPrimShort || !reverseBits);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700256
257 if (type == Primitive::kPrimShort) {
258 Register in = locations->InAt(0).AsRegister<Register>();
259 Register out = locations->Out().AsRegister<Register>();
260
261 if (isR2OrNewer) {
262 __ Wsbh(out, in);
263 __ Seh(out, out);
264 } else {
265 __ Sll(TMP, in, 24);
266 __ Sra(TMP, TMP, 16);
267 __ Sll(out, in, 16);
268 __ Srl(out, out, 24);
269 __ Or(out, out, TMP);
270 }
271 } else if (type == Primitive::kPrimInt) {
272 Register in = locations->InAt(0).AsRegister<Register>();
273 Register out = locations->Out().AsRegister<Register>();
274
275 if (isR2OrNewer) {
276 __ Rotr(out, in, 16);
277 __ Wsbh(out, out);
278 } else {
279 // MIPS32r1
280 // __ Rotr(out, in, 16);
281 __ Sll(TMP, in, 16);
282 __ Srl(out, in, 16);
283 __ Or(out, out, TMP);
284 // __ Wsbh(out, out);
285 __ LoadConst32(AT, 0x00FF00FF);
286 __ And(TMP, out, AT);
287 __ Sll(TMP, TMP, 8);
288 __ Srl(out, out, 8);
289 __ And(out, out, AT);
290 __ Or(out, out, TMP);
291 }
Chris Larsen70014c82015-11-18 12:26:08 -0800292 if (reverseBits) {
293 if (isR6) {
294 __ Bitswap(out, out);
295 } else {
296 __ LoadConst32(AT, 0x0F0F0F0F);
297 __ And(TMP, out, AT);
298 __ Sll(TMP, TMP, 4);
299 __ Srl(out, out, 4);
300 __ And(out, out, AT);
301 __ Or(out, TMP, out);
302 __ LoadConst32(AT, 0x33333333);
303 __ And(TMP, out, AT);
304 __ Sll(TMP, TMP, 2);
305 __ Srl(out, out, 2);
306 __ And(out, out, AT);
307 __ Or(out, TMP, out);
308 __ LoadConst32(AT, 0x55555555);
309 __ And(TMP, out, AT);
310 __ Sll(TMP, TMP, 1);
311 __ Srl(out, out, 1);
312 __ And(out, out, AT);
313 __ Or(out, TMP, out);
314 }
315 }
Chris Larsen3f8bf652015-10-28 10:08:56 -0700316 } else if (type == Primitive::kPrimLong) {
317 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
318 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
319 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
320 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
321
322 if (isR2OrNewer) {
323 __ Rotr(AT, in_hi, 16);
324 __ Rotr(TMP, in_lo, 16);
325 __ Wsbh(out_lo, AT);
326 __ Wsbh(out_hi, TMP);
327 } else {
328 // When calling CreateIntToIntLocations() we promised that the
329 // use of the out_lo/out_hi wouldn't overlap with the use of
330 // in_lo/in_hi. Be very careful not to write to out_lo/out_hi
331 // until we're completely done reading from in_lo/in_hi.
332 // __ Rotr(TMP, in_lo, 16);
333 __ Sll(TMP, in_lo, 16);
334 __ Srl(AT, in_lo, 16);
335 __ Or(TMP, TMP, AT); // Hold in TMP until it's safe
336 // to write to out_hi.
337 // __ Rotr(out_lo, in_hi, 16);
338 __ Sll(AT, in_hi, 16);
339 __ Srl(out_lo, in_hi, 16); // Here we are finally done reading
340 // from in_lo/in_hi so it's okay to
341 // write to out_lo/out_hi.
342 __ Or(out_lo, out_lo, AT);
343 // __ Wsbh(out_hi, out_hi);
344 __ LoadConst32(AT, 0x00FF00FF);
345 __ And(out_hi, TMP, AT);
346 __ Sll(out_hi, out_hi, 8);
347 __ Srl(TMP, TMP, 8);
348 __ And(TMP, TMP, AT);
349 __ Or(out_hi, out_hi, TMP);
350 // __ Wsbh(out_lo, out_lo);
351 __ And(TMP, out_lo, AT); // AT already holds the correct mask value
352 __ Sll(TMP, TMP, 8);
353 __ Srl(out_lo, out_lo, 8);
354 __ And(out_lo, out_lo, AT);
355 __ Or(out_lo, out_lo, TMP);
356 }
Chris Larsen70014c82015-11-18 12:26:08 -0800357 if (reverseBits) {
358 if (isR6) {
359 __ Bitswap(out_hi, out_hi);
360 __ Bitswap(out_lo, out_lo);
361 } else {
362 __ LoadConst32(AT, 0x0F0F0F0F);
363 __ And(TMP, out_hi, AT);
364 __ Sll(TMP, TMP, 4);
365 __ Srl(out_hi, out_hi, 4);
366 __ And(out_hi, out_hi, AT);
367 __ Or(out_hi, TMP, out_hi);
368 __ And(TMP, out_lo, AT);
369 __ Sll(TMP, TMP, 4);
370 __ Srl(out_lo, out_lo, 4);
371 __ And(out_lo, out_lo, AT);
372 __ Or(out_lo, TMP, out_lo);
373 __ LoadConst32(AT, 0x33333333);
374 __ And(TMP, out_hi, AT);
375 __ Sll(TMP, TMP, 2);
376 __ Srl(out_hi, out_hi, 2);
377 __ And(out_hi, out_hi, AT);
378 __ Or(out_hi, TMP, out_hi);
379 __ And(TMP, out_lo, AT);
380 __ Sll(TMP, TMP, 2);
381 __ Srl(out_lo, out_lo, 2);
382 __ And(out_lo, out_lo, AT);
383 __ Or(out_lo, TMP, out_lo);
384 __ LoadConst32(AT, 0x55555555);
385 __ And(TMP, out_hi, AT);
386 __ Sll(TMP, TMP, 1);
387 __ Srl(out_hi, out_hi, 1);
388 __ And(out_hi, out_hi, AT);
389 __ Or(out_hi, TMP, out_hi);
390 __ And(TMP, out_lo, AT);
391 __ Sll(TMP, TMP, 1);
392 __ Srl(out_lo, out_lo, 1);
393 __ And(out_lo, out_lo, AT);
394 __ Or(out_lo, TMP, out_lo);
395 }
396 }
Chris Larsen3f8bf652015-10-28 10:08:56 -0700397 }
398}
399
400// int java.lang.Integer.reverseBytes(int)
401void IntrinsicLocationsBuilderMIPS::VisitIntegerReverseBytes(HInvoke* invoke) {
402 CreateIntToIntLocations(arena_, invoke);
403}
404
405void IntrinsicCodeGeneratorMIPS::VisitIntegerReverseBytes(HInvoke* invoke) {
Chris Larsen70014c82015-11-18 12:26:08 -0800406 GenReverse(invoke->GetLocations(),
407 Primitive::kPrimInt,
Chris Larsene16ce5a2015-11-18 12:30:20 -0800408 IsR2OrNewer(),
409 IsR6(),
Chris Larsenb74353a2015-11-20 09:07:09 -0800410 /* reverseBits */ false,
Chris Larsen70014c82015-11-18 12:26:08 -0800411 GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700412}
413
414// long java.lang.Long.reverseBytes(long)
415void IntrinsicLocationsBuilderMIPS::VisitLongReverseBytes(HInvoke* invoke) {
416 CreateIntToIntLocations(arena_, invoke);
417}
418
419void IntrinsicCodeGeneratorMIPS::VisitLongReverseBytes(HInvoke* invoke) {
Chris Larsen70014c82015-11-18 12:26:08 -0800420 GenReverse(invoke->GetLocations(),
421 Primitive::kPrimLong,
Chris Larsene16ce5a2015-11-18 12:30:20 -0800422 IsR2OrNewer(),
423 IsR6(),
Chris Larsenb74353a2015-11-20 09:07:09 -0800424 /* reverseBits */ false,
Chris Larsen70014c82015-11-18 12:26:08 -0800425 GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700426}
427
428// short java.lang.Short.reverseBytes(short)
429void IntrinsicLocationsBuilderMIPS::VisitShortReverseBytes(HInvoke* invoke) {
430 CreateIntToIntLocations(arena_, invoke);
431}
432
433void IntrinsicCodeGeneratorMIPS::VisitShortReverseBytes(HInvoke* invoke) {
Chris Larsen70014c82015-11-18 12:26:08 -0800434 GenReverse(invoke->GetLocations(),
435 Primitive::kPrimShort,
Chris Larsene16ce5a2015-11-18 12:30:20 -0800436 IsR2OrNewer(),
437 IsR6(),
Chris Larsenb74353a2015-11-20 09:07:09 -0800438 /* reverseBits */ false,
Chris Larsen70014c82015-11-18 12:26:08 -0800439 GetAssembler());
440}
441
Chris Larsene3845472015-11-18 12:27:15 -0800442static void GenNumberOfLeadingZeroes(LocationSummary* locations,
443 bool is64bit,
444 bool isR6,
445 MipsAssembler* assembler) {
446 Register out = locations->Out().AsRegister<Register>();
447 if (is64bit) {
448 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
449 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
450
451 if (isR6) {
452 __ ClzR6(AT, in_hi);
453 __ ClzR6(TMP, in_lo);
454 __ Seleqz(TMP, TMP, in_hi);
455 } else {
456 __ ClzR2(AT, in_hi);
457 __ ClzR2(TMP, in_lo);
458 __ Movn(TMP, ZERO, in_hi);
459 }
460 __ Addu(out, AT, TMP);
461 } else {
462 Register in = locations->InAt(0).AsRegister<Register>();
463
464 if (isR6) {
465 __ ClzR6(out, in);
466 } else {
467 __ ClzR2(out, in);
468 }
469 }
470}
471
472// int java.lang.Integer.numberOfLeadingZeros(int i)
473void IntrinsicLocationsBuilderMIPS::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
474 CreateIntToIntLocations(arena_, invoke);
475}
476
477void IntrinsicCodeGeneratorMIPS::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800478 GenNumberOfLeadingZeroes(invoke->GetLocations(), /* is64bit */ false, IsR6(), GetAssembler());
Chris Larsene3845472015-11-18 12:27:15 -0800479}
480
481// int java.lang.Long.numberOfLeadingZeros(long i)
482void IntrinsicLocationsBuilderMIPS::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
483 CreateIntToIntLocations(arena_, invoke);
484}
485
486void IntrinsicCodeGeneratorMIPS::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800487 GenNumberOfLeadingZeroes(invoke->GetLocations(), /* is64bit */ true, IsR6(), GetAssembler());
Chris Larsene3845472015-11-18 12:27:15 -0800488}
489
Chris Larsen86829602015-11-18 12:27:52 -0800490static void GenNumberOfTrailingZeroes(LocationSummary* locations,
491 bool is64bit,
492 bool isR6,
Chris Larsen86829602015-11-18 12:27:52 -0800493 MipsAssembler* assembler) {
494 Register out = locations->Out().AsRegister<Register>();
495 Register in_lo;
496 Register in;
497
498 if (is64bit) {
Chris Larsen86829602015-11-18 12:27:52 -0800499 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
500
501 in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
502
503 // If in_lo is zero then count the number of trailing zeroes in in_hi;
504 // otherwise count the number of trailing zeroes in in_lo.
Chris Larsenbbb2ebe2016-02-17 17:44:58 -0800505 // out = in_lo ? in_lo : in_hi;
Chris Larsen86829602015-11-18 12:27:52 -0800506 if (isR6) {
507 __ Seleqz(out, in_hi, in_lo);
508 __ Selnez(TMP, in_lo, in_lo);
509 __ Or(out, out, TMP);
510 } else {
511 __ Movz(out, in_hi, in_lo);
512 __ Movn(out, in_lo, in_lo);
513 }
514
515 in = out;
516 } else {
517 in = locations->InAt(0).AsRegister<Register>();
518 // Give in_lo a dummy value to keep the compiler from complaining.
519 // Since we only get here in the 32-bit case, this value will never
520 // be used.
521 in_lo = in;
522 }
523
Chris Larsenbbb2ebe2016-02-17 17:44:58 -0800524 if (isR6) {
525 // We don't have an instruction to count the number of trailing zeroes.
526 // Start by flipping the bits end-for-end so we can count the number of
527 // leading zeroes instead.
Chris Larsen86829602015-11-18 12:27:52 -0800528 __ Rotr(out, in, 16);
529 __ Wsbh(out, out);
Chris Larsen86829602015-11-18 12:27:52 -0800530 __ Bitswap(out, out);
531 __ ClzR6(out, out);
532 } else {
Chris Larsenbbb2ebe2016-02-17 17:44:58 -0800533 // Convert trailing zeroes to trailing ones, and bits to their left
534 // to zeroes.
535 __ Addiu(TMP, in, -1);
536 __ Xor(out, TMP, in);
537 __ And(out, out, TMP);
538 // Count number of leading zeroes.
Chris Larsen86829602015-11-18 12:27:52 -0800539 __ ClzR2(out, out);
Chris Larsenbbb2ebe2016-02-17 17:44:58 -0800540 // Subtract number of leading zeroes from 32 to get number of trailing ones.
541 // Remember that the trailing ones were formerly trailing zeroes.
542 __ LoadConst32(TMP, 32);
543 __ Subu(out, TMP, out);
Chris Larsen86829602015-11-18 12:27:52 -0800544 }
545
546 if (is64bit) {
547 // If in_lo is zero, then we counted the number of trailing zeroes in in_hi so we must add the
548 // number of trailing zeroes in in_lo (32) to get the correct final count
549 __ LoadConst32(TMP, 32);
550 if (isR6) {
551 __ Seleqz(TMP, TMP, in_lo);
552 } else {
553 __ Movn(TMP, ZERO, in_lo);
554 }
555 __ Addu(out, out, TMP);
556 }
557}
558
559// int java.lang.Integer.numberOfTrailingZeros(int i)
560void IntrinsicLocationsBuilderMIPS::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
561 CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap);
562}
563
564void IntrinsicCodeGeneratorMIPS::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Chris Larsenbbb2ebe2016-02-17 17:44:58 -0800565 GenNumberOfTrailingZeroes(invoke->GetLocations(), /* is64bit */ false, IsR6(), GetAssembler());
Chris Larsen86829602015-11-18 12:27:52 -0800566}
567
568// int java.lang.Long.numberOfTrailingZeros(long i)
569void IntrinsicLocationsBuilderMIPS::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
570 CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap);
571}
572
573void IntrinsicCodeGeneratorMIPS::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Chris Larsenbbb2ebe2016-02-17 17:44:58 -0800574 GenNumberOfTrailingZeroes(invoke->GetLocations(), /* is64bit */ true, IsR6(), GetAssembler());
Chris Larsene16ce5a2015-11-18 12:30:20 -0800575}
576
Chris Larsen70014c82015-11-18 12:26:08 -0800577// int java.lang.Integer.reverse(int)
578void IntrinsicLocationsBuilderMIPS::VisitIntegerReverse(HInvoke* invoke) {
579 CreateIntToIntLocations(arena_, invoke);
580}
581
582void IntrinsicCodeGeneratorMIPS::VisitIntegerReverse(HInvoke* invoke) {
583 GenReverse(invoke->GetLocations(),
584 Primitive::kPrimInt,
Chris Larsene16ce5a2015-11-18 12:30:20 -0800585 IsR2OrNewer(),
586 IsR6(),
Chris Larsenb74353a2015-11-20 09:07:09 -0800587 /* reverseBits */ true,
Chris Larsen70014c82015-11-18 12:26:08 -0800588 GetAssembler());
589}
590
591// long java.lang.Long.reverse(long)
592void IntrinsicLocationsBuilderMIPS::VisitLongReverse(HInvoke* invoke) {
593 CreateIntToIntLocations(arena_, invoke);
594}
595
596void IntrinsicCodeGeneratorMIPS::VisitLongReverse(HInvoke* invoke) {
597 GenReverse(invoke->GetLocations(),
598 Primitive::kPrimLong,
Chris Larsene16ce5a2015-11-18 12:30:20 -0800599 IsR2OrNewer(),
600 IsR6(),
Chris Larsenb74353a2015-11-20 09:07:09 -0800601 /* reverseBits */ true,
Chris Larsen70014c82015-11-18 12:26:08 -0800602 GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700603}
604
Chris Larsenb74353a2015-11-20 09:07:09 -0800605static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
606 LocationSummary* locations = new (arena) LocationSummary(invoke,
607 LocationSummary::kNoCall,
608 kIntrinsified);
609 locations->SetInAt(0, Location::RequiresFpuRegister());
610 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
611}
612
Chris Larsenedc16452016-02-12 17:59:00 -0800613static void GenBitCount(LocationSummary* locations,
614 Primitive::Type type,
615 bool isR6,
616 MipsAssembler* assembler) {
Chris Larsenedc16452016-02-12 17:59:00 -0800617 Register out = locations->Out().AsRegister<Register>();
618
619 // https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
620 //
621 // A generalization of the best bit counting method to integers of
622 // bit-widths up to 128 (parameterized by type T) is this:
623 //
624 // v = v - ((v >> 1) & (T)~(T)0/3); // temp
625 // v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); // temp
626 // v = (v + (v >> 4)) & (T)~(T)0/255*15; // temp
627 // c = (T)(v * ((T)~(T)0/255)) >> (sizeof(T) - 1) * BITS_PER_BYTE; // count
628 //
629 // For comparison, for 32-bit quantities, this algorithm can be executed
630 // using 20 MIPS instructions (the calls to LoadConst32() generate two
631 // machine instructions each for the values being used in this algorithm).
632 // A(n unrolled) loop-based algorithm required 25 instructions.
633 //
634 // For 64-bit quantities, this algorithm gets executed twice, (once
635 // for in_lo, and again for in_hi), but saves a few instructions
636 // because the mask values only have to be loaded once. Using this
Chris Larsen8ca4f972016-04-14 16:16:29 -0700637 // algorithm the count for a 64-bit operand can be performed in 29
Chris Larsenedc16452016-02-12 17:59:00 -0800638 // instructions compared to a loop-based algorithm which required 47
639 // instructions.
640
641 if (type == Primitive::kPrimInt) {
642 Register in = locations->InAt(0).AsRegister<Register>();
643
644 __ Srl(TMP, in, 1);
645 __ LoadConst32(AT, 0x55555555);
646 __ And(TMP, TMP, AT);
647 __ Subu(TMP, in, TMP);
648 __ LoadConst32(AT, 0x33333333);
649 __ And(out, TMP, AT);
650 __ Srl(TMP, TMP, 2);
651 __ And(TMP, TMP, AT);
652 __ Addu(TMP, out, TMP);
653 __ Srl(out, TMP, 4);
654 __ Addu(out, out, TMP);
655 __ LoadConst32(AT, 0x0F0F0F0F);
656 __ And(out, out, AT);
657 __ LoadConst32(TMP, 0x01010101);
658 if (isR6) {
659 __ MulR6(out, out, TMP);
660 } else {
661 __ MulR2(out, out, TMP);
662 }
663 __ Srl(out, out, 24);
Roland Levillainfa3912e2016-04-01 18:21:55 +0100664 } else {
665 DCHECK_EQ(type, Primitive::kPrimLong);
Chris Larsenedc16452016-02-12 17:59:00 -0800666 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
667 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
668 Register tmp_hi = locations->GetTemp(0).AsRegister<Register>();
669 Register out_hi = locations->GetTemp(1).AsRegister<Register>();
670 Register tmp_lo = TMP;
671 Register out_lo = out;
672
673 __ Srl(tmp_lo, in_lo, 1);
674 __ Srl(tmp_hi, in_hi, 1);
675
676 __ LoadConst32(AT, 0x55555555);
677
678 __ And(tmp_lo, tmp_lo, AT);
679 __ Subu(tmp_lo, in_lo, tmp_lo);
680
681 __ And(tmp_hi, tmp_hi, AT);
682 __ Subu(tmp_hi, in_hi, tmp_hi);
683
684 __ LoadConst32(AT, 0x33333333);
685
686 __ And(out_lo, tmp_lo, AT);
687 __ Srl(tmp_lo, tmp_lo, 2);
688 __ And(tmp_lo, tmp_lo, AT);
689 __ Addu(tmp_lo, out_lo, tmp_lo);
Chris Larsenedc16452016-02-12 17:59:00 -0800690
691 __ And(out_hi, tmp_hi, AT);
692 __ Srl(tmp_hi, tmp_hi, 2);
693 __ And(tmp_hi, tmp_hi, AT);
694 __ Addu(tmp_hi, out_hi, tmp_hi);
Chris Larsenedc16452016-02-12 17:59:00 -0800695
Chris Larsen8ca4f972016-04-14 16:16:29 -0700696 // Here we deviate from the original algorithm a bit. We've reached
697 // the stage where the bitfields holding the subtotals are large
698 // enough to hold the combined subtotals for both the low word, and
699 // the high word. This means that we can add the subtotals for the
700 // the high, and low words into a single word, and compute the final
701 // result for both the high, and low words using fewer instructions.
Chris Larsenedc16452016-02-12 17:59:00 -0800702 __ LoadConst32(AT, 0x0F0F0F0F);
703
Chris Larsen8ca4f972016-04-14 16:16:29 -0700704 __ Addu(TMP, tmp_hi, tmp_lo);
705
706 __ Srl(out, TMP, 4);
707 __ And(out, out, AT);
708 __ And(TMP, TMP, AT);
709 __ Addu(out, out, TMP);
Chris Larsenedc16452016-02-12 17:59:00 -0800710
711 __ LoadConst32(AT, 0x01010101);
712
713 if (isR6) {
Chris Larsen8ca4f972016-04-14 16:16:29 -0700714 __ MulR6(out, out, AT);
Chris Larsenedc16452016-02-12 17:59:00 -0800715 } else {
Chris Larsen8ca4f972016-04-14 16:16:29 -0700716 __ MulR2(out, out, AT);
Chris Larsenedc16452016-02-12 17:59:00 -0800717 }
718
Chris Larsen8ca4f972016-04-14 16:16:29 -0700719 __ Srl(out, out, 24);
Chris Larsenedc16452016-02-12 17:59:00 -0800720 }
721}
722
723// int java.lang.Integer.bitCount(int)
724void IntrinsicLocationsBuilderMIPS::VisitIntegerBitCount(HInvoke* invoke) {
725 CreateIntToIntLocations(arena_, invoke);
726}
727
728void IntrinsicCodeGeneratorMIPS::VisitIntegerBitCount(HInvoke* invoke) {
729 GenBitCount(invoke->GetLocations(), Primitive::kPrimInt, IsR6(), GetAssembler());
730}
731
732// int java.lang.Long.bitCount(int)
733void IntrinsicLocationsBuilderMIPS::VisitLongBitCount(HInvoke* invoke) {
734 LocationSummary* locations = new (arena_) LocationSummary(invoke,
735 LocationSummary::kNoCall,
736 kIntrinsified);
737 locations->SetInAt(0, Location::RequiresRegister());
738 locations->SetOut(Location::RequiresRegister());
739 locations->AddTemp(Location::RequiresRegister());
740 locations->AddTemp(Location::RequiresRegister());
741}
742
743void IntrinsicCodeGeneratorMIPS::VisitLongBitCount(HInvoke* invoke) {
744 GenBitCount(invoke->GetLocations(), Primitive::kPrimLong, IsR6(), GetAssembler());
745}
746
Goran Jakovljevicb6684652017-01-11 13:42:38 +0100747static void MathAbsFP(LocationSummary* locations,
748 bool is64bit,
749 bool isR2OrNewer,
750 bool isR6,
751 MipsAssembler* assembler) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800752 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
753 FRegister out = locations->Out().AsFpuRegister<FRegister>();
754
Goran Jakovljevic5a6cbfc2017-01-13 12:13:39 +0100755 // Note, as a "quality of implementation", rather than pure "spec compliance", we require that
756 // Math.abs() clears the sign bit (but changes nothing else) for all numbers, including NaN
757 // (signaling NaN may become quiet though).
Goran Jakovljevicb6684652017-01-11 13:42:38 +0100758 //
759 // The ABS.fmt instructions (abs.s and abs.d) do exactly that when NAN2008=1 (R6). For this case,
760 // both regular floating point numbers and NAN values are treated alike, only the sign bit is
761 // affected by this instruction.
762 // But when NAN2008=0 (R2 and before), the ABS.fmt instructions can't be used. For this case, any
763 // NaN operand signals invalid operation. This means that other bits (not just sign bit) might be
764 // changed when doing abs(NaN). Because of that, we clear sign bit in a different way.
765 if (isR6) {
766 if (is64bit) {
767 __ AbsD(out, in);
768 } else {
769 __ AbsS(out, in);
770 }
Chris Larsenb74353a2015-11-20 09:07:09 -0800771 } else {
Goran Jakovljevicb6684652017-01-11 13:42:38 +0100772 if (is64bit) {
773 if (in != out) {
774 __ MovD(out, in);
775 }
776 __ MoveFromFpuHigh(TMP, in);
777 // ins instruction is not available for R1.
778 if (isR2OrNewer) {
779 __ Ins(TMP, ZERO, 31, 1);
780 } else {
781 __ Sll(TMP, TMP, 1);
782 __ Srl(TMP, TMP, 1);
783 }
784 __ MoveToFpuHigh(TMP, out);
785 } else {
786 __ Mfc1(TMP, in);
787 // ins instruction is not available for R1.
788 if (isR2OrNewer) {
789 __ Ins(TMP, ZERO, 31, 1);
790 } else {
791 __ Sll(TMP, TMP, 1);
792 __ Srl(TMP, TMP, 1);
793 }
794 __ Mtc1(TMP, out);
795 }
Chris Larsenb74353a2015-11-20 09:07:09 -0800796 }
797}
798
799// double java.lang.Math.abs(double)
800void IntrinsicLocationsBuilderMIPS::VisitMathAbsDouble(HInvoke* invoke) {
801 CreateFPToFPLocations(arena_, invoke);
802}
803
804void IntrinsicCodeGeneratorMIPS::VisitMathAbsDouble(HInvoke* invoke) {
Goran Jakovljevicb6684652017-01-11 13:42:38 +0100805 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, IsR2OrNewer(), IsR6(), GetAssembler());
Chris Larsenb74353a2015-11-20 09:07:09 -0800806}
807
808// float java.lang.Math.abs(float)
809void IntrinsicLocationsBuilderMIPS::VisitMathAbsFloat(HInvoke* invoke) {
810 CreateFPToFPLocations(arena_, invoke);
811}
812
813void IntrinsicCodeGeneratorMIPS::VisitMathAbsFloat(HInvoke* invoke) {
Goran Jakovljevicb6684652017-01-11 13:42:38 +0100814 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, IsR2OrNewer(), IsR6(), GetAssembler());
Chris Larsenb74353a2015-11-20 09:07:09 -0800815}
816
817static void GenAbsInteger(LocationSummary* locations, bool is64bit, MipsAssembler* assembler) {
818 if (is64bit) {
819 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
820 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
821 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
822 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
823
824 // The comments in this section show the analogous operations which would
825 // be performed if we had 64-bit registers "in", and "out".
826 // __ Dsra32(AT, in, 31);
827 __ Sra(AT, in_hi, 31);
828 // __ Xor(out, in, AT);
829 __ Xor(TMP, in_lo, AT);
830 __ Xor(out_hi, in_hi, AT);
831 // __ Dsubu(out, out, AT);
832 __ Subu(out_lo, TMP, AT);
833 __ Sltu(TMP, out_lo, TMP);
834 __ Addu(out_hi, out_hi, TMP);
835 } else {
836 Register in = locations->InAt(0).AsRegister<Register>();
837 Register out = locations->Out().AsRegister<Register>();
838
839 __ Sra(AT, in, 31);
840 __ Xor(out, in, AT);
841 __ Subu(out, out, AT);
842 }
843}
844
845// int java.lang.Math.abs(int)
846void IntrinsicLocationsBuilderMIPS::VisitMathAbsInt(HInvoke* invoke) {
847 CreateIntToIntLocations(arena_, invoke);
848}
849
850void IntrinsicCodeGeneratorMIPS::VisitMathAbsInt(HInvoke* invoke) {
851 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
852}
853
854// long java.lang.Math.abs(long)
855void IntrinsicLocationsBuilderMIPS::VisitMathAbsLong(HInvoke* invoke) {
856 CreateIntToIntLocations(arena_, invoke);
857}
858
859void IntrinsicCodeGeneratorMIPS::VisitMathAbsLong(HInvoke* invoke) {
860 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
861}
862
863static void GenMinMaxFP(LocationSummary* locations,
864 bool is_min,
865 Primitive::Type type,
866 bool is_R6,
867 MipsAssembler* assembler) {
868 FRegister out = locations->Out().AsFpuRegister<FRegister>();
869 FRegister a = locations->InAt(0).AsFpuRegister<FRegister>();
870 FRegister b = locations->InAt(1).AsFpuRegister<FRegister>();
871
872 if (is_R6) {
873 MipsLabel noNaNs;
874 MipsLabel done;
875 FRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
876
877 // When Java computes min/max it prefers a NaN to a number; the
878 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
879 // the inputs is a NaN and the other is a valid number, the MIPS
880 // instruction will return the number; Java wants the NaN value
881 // returned. This is why there is extra logic preceding the use of
882 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
883 // NaN, return the NaN, otherwise return the min/max.
884 if (type == Primitive::kPrimDouble) {
885 __ CmpUnD(FTMP, a, b);
886 __ Bc1eqz(FTMP, &noNaNs);
887
888 // One of the inputs is a NaN
889 __ CmpEqD(ftmp, a, a);
890 // If a == a then b is the NaN, otherwise a is the NaN.
891 __ SelD(ftmp, a, b);
892
893 if (ftmp != out) {
894 __ MovD(out, ftmp);
895 }
896
897 __ B(&done);
898
899 __ Bind(&noNaNs);
900
901 if (is_min) {
902 __ MinD(out, a, b);
903 } else {
904 __ MaxD(out, a, b);
905 }
906 } else {
907 DCHECK_EQ(type, Primitive::kPrimFloat);
908 __ CmpUnS(FTMP, a, b);
909 __ Bc1eqz(FTMP, &noNaNs);
910
911 // One of the inputs is a NaN
912 __ CmpEqS(ftmp, a, a);
913 // If a == a then b is the NaN, otherwise a is the NaN.
914 __ SelS(ftmp, a, b);
915
916 if (ftmp != out) {
917 __ MovS(out, ftmp);
918 }
919
920 __ B(&done);
921
922 __ Bind(&noNaNs);
923
924 if (is_min) {
925 __ MinS(out, a, b);
926 } else {
927 __ MaxS(out, a, b);
928 }
929 }
930
931 __ Bind(&done);
932 } else {
933 MipsLabel ordered;
934 MipsLabel compare;
935 MipsLabel select;
936 MipsLabel done;
937
938 if (type == Primitive::kPrimDouble) {
939 __ CunD(a, b);
940 } else {
941 DCHECK_EQ(type, Primitive::kPrimFloat);
942 __ CunS(a, b);
943 }
944 __ Bc1f(&ordered);
945
946 // a or b (or both) is a NaN. Return one, which is a NaN.
947 if (type == Primitive::kPrimDouble) {
948 __ CeqD(b, b);
949 } else {
950 __ CeqS(b, b);
951 }
952 __ B(&select);
953
954 __ Bind(&ordered);
955
956 // Neither is a NaN.
957 // a == b? (-0.0 compares equal with +0.0)
958 // If equal, handle zeroes, else compare further.
959 if (type == Primitive::kPrimDouble) {
960 __ CeqD(a, b);
961 } else {
962 __ CeqS(a, b);
963 }
964 __ Bc1f(&compare);
965
966 // a == b either bit for bit or one is -0.0 and the other is +0.0.
967 if (type == Primitive::kPrimDouble) {
968 __ MoveFromFpuHigh(TMP, a);
969 __ MoveFromFpuHigh(AT, b);
970 } else {
971 __ Mfc1(TMP, a);
972 __ Mfc1(AT, b);
973 }
974
975 if (is_min) {
976 // -0.0 prevails over +0.0.
977 __ Or(TMP, TMP, AT);
978 } else {
979 // +0.0 prevails over -0.0.
980 __ And(TMP, TMP, AT);
981 }
982
983 if (type == Primitive::kPrimDouble) {
984 __ Mfc1(AT, a);
985 __ Mtc1(AT, out);
986 __ MoveToFpuHigh(TMP, out);
987 } else {
988 __ Mtc1(TMP, out);
989 }
990 __ B(&done);
991
992 __ Bind(&compare);
993
994 if (type == Primitive::kPrimDouble) {
995 if (is_min) {
996 // return (a <= b) ? a : b;
997 __ ColeD(a, b);
998 } else {
999 // return (a >= b) ? a : b;
1000 __ ColeD(b, a); // b <= a
1001 }
1002 } else {
1003 if (is_min) {
1004 // return (a <= b) ? a : b;
1005 __ ColeS(a, b);
1006 } else {
1007 // return (a >= b) ? a : b;
1008 __ ColeS(b, a); // b <= a
1009 }
1010 }
1011
1012 __ Bind(&select);
1013
1014 if (type == Primitive::kPrimDouble) {
1015 __ MovtD(out, a);
1016 __ MovfD(out, b);
1017 } else {
1018 __ MovtS(out, a);
1019 __ MovfS(out, b);
1020 }
1021
1022 __ Bind(&done);
1023 }
1024}
1025
1026static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
1027 LocationSummary* locations = new (arena) LocationSummary(invoke,
1028 LocationSummary::kNoCall,
1029 kIntrinsified);
1030 locations->SetInAt(0, Location::RequiresFpuRegister());
1031 locations->SetInAt(1, Location::RequiresFpuRegister());
1032 locations->SetOut(Location::RequiresFpuRegister(), Location::kOutputOverlap);
1033}
1034
1035// double java.lang.Math.min(double, double)
1036void IntrinsicLocationsBuilderMIPS::VisitMathMinDoubleDouble(HInvoke* invoke) {
1037 CreateFPFPToFPLocations(arena_, invoke);
1038}
1039
1040void IntrinsicCodeGeneratorMIPS::VisitMathMinDoubleDouble(HInvoke* invoke) {
1041 GenMinMaxFP(invoke->GetLocations(),
1042 /* is_min */ true,
1043 Primitive::kPrimDouble,
1044 IsR6(),
1045 GetAssembler());
1046}
1047
1048// float java.lang.Math.min(float, float)
1049void IntrinsicLocationsBuilderMIPS::VisitMathMinFloatFloat(HInvoke* invoke) {
1050 CreateFPFPToFPLocations(arena_, invoke);
1051}
1052
1053void IntrinsicCodeGeneratorMIPS::VisitMathMinFloatFloat(HInvoke* invoke) {
1054 GenMinMaxFP(invoke->GetLocations(),
1055 /* is_min */ true,
1056 Primitive::kPrimFloat,
1057 IsR6(),
1058 GetAssembler());
1059}
1060
1061// double java.lang.Math.max(double, double)
1062void IntrinsicLocationsBuilderMIPS::VisitMathMaxDoubleDouble(HInvoke* invoke) {
1063 CreateFPFPToFPLocations(arena_, invoke);
1064}
1065
1066void IntrinsicCodeGeneratorMIPS::VisitMathMaxDoubleDouble(HInvoke* invoke) {
1067 GenMinMaxFP(invoke->GetLocations(),
1068 /* is_min */ false,
1069 Primitive::kPrimDouble,
1070 IsR6(),
1071 GetAssembler());
1072}
1073
1074// float java.lang.Math.max(float, float)
1075void IntrinsicLocationsBuilderMIPS::VisitMathMaxFloatFloat(HInvoke* invoke) {
1076 CreateFPFPToFPLocations(arena_, invoke);
1077}
1078
1079void IntrinsicCodeGeneratorMIPS::VisitMathMaxFloatFloat(HInvoke* invoke) {
1080 GenMinMaxFP(invoke->GetLocations(),
1081 /* is_min */ false,
1082 Primitive::kPrimFloat,
1083 IsR6(),
1084 GetAssembler());
1085}
1086
1087static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
1088 LocationSummary* locations = new (arena) LocationSummary(invoke,
1089 LocationSummary::kNoCall,
1090 kIntrinsified);
1091 locations->SetInAt(0, Location::RequiresRegister());
1092 locations->SetInAt(1, Location::RequiresRegister());
1093 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1094}
1095
1096static void GenMinMax(LocationSummary* locations,
1097 bool is_min,
1098 Primitive::Type type,
1099 bool is_R6,
1100 MipsAssembler* assembler) {
1101 if (is_R6) {
1102 // Some architectures, such as ARM and MIPS (prior to r6), have a
1103 // conditional move instruction which only changes the target
1104 // (output) register if the condition is true (MIPS prior to r6 had
1105 // MOVF, MOVT, MOVN, and MOVZ). The SELEQZ and SELNEZ instructions
1106 // always change the target (output) register. If the condition is
1107 // true the output register gets the contents of the "rs" register;
1108 // otherwise, the output register is set to zero. One consequence
1109 // of this is that to implement something like "rd = c==0 ? rs : rt"
1110 // MIPS64r6 needs to use a pair of SELEQZ/SELNEZ instructions.
1111 // After executing this pair of instructions one of the output
1112 // registers from the pair will necessarily contain zero. Then the
1113 // code ORs the output registers from the SELEQZ/SELNEZ instructions
1114 // to get the final result.
1115 //
1116 // The initial test to see if the output register is same as the
1117 // first input register is needed to make sure that value in the
1118 // first input register isn't clobbered before we've finished
1119 // computing the output value. The logic in the corresponding else
1120 // clause performs the same task but makes sure the second input
1121 // register isn't clobbered in the event that it's the same register
1122 // as the output register; the else clause also handles the case
1123 // where the output register is distinct from both the first, and the
1124 // second input registers.
1125 if (type == Primitive::kPrimLong) {
1126 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
1127 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
1128 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
1129 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
1130 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
1131 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
1132
1133 MipsLabel compare_done;
1134
1135 if (a_lo == b_lo) {
1136 if (out_lo != a_lo) {
1137 __ Move(out_lo, a_lo);
1138 __ Move(out_hi, a_hi);
1139 }
1140 } else {
1141 __ Slt(TMP, b_hi, a_hi);
1142 __ Bne(b_hi, a_hi, &compare_done);
1143
1144 __ Sltu(TMP, b_lo, a_lo);
1145
1146 __ Bind(&compare_done);
1147
1148 if (is_min) {
1149 __ Seleqz(AT, a_lo, TMP);
1150 __ Selnez(out_lo, b_lo, TMP); // Safe even if out_lo == a_lo/b_lo
1151 // because at this point we're
1152 // done using a_lo/b_lo.
1153 } else {
1154 __ Selnez(AT, a_lo, TMP);
1155 __ Seleqz(out_lo, b_lo, TMP); // ditto
1156 }
1157 __ Or(out_lo, out_lo, AT);
1158 if (is_min) {
1159 __ Seleqz(AT, a_hi, TMP);
1160 __ Selnez(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
1161 } else {
1162 __ Selnez(AT, a_hi, TMP);
1163 __ Seleqz(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
1164 }
1165 __ Or(out_hi, out_hi, AT);
1166 }
1167 } else {
1168 DCHECK_EQ(type, Primitive::kPrimInt);
1169 Register a = locations->InAt(0).AsRegister<Register>();
1170 Register b = locations->InAt(1).AsRegister<Register>();
1171 Register out = locations->Out().AsRegister<Register>();
1172
1173 if (a == b) {
1174 if (out != a) {
1175 __ Move(out, a);
1176 }
1177 } else {
1178 __ Slt(AT, b, a);
1179 if (is_min) {
1180 __ Seleqz(TMP, a, AT);
1181 __ Selnez(AT, b, AT);
1182 } else {
1183 __ Selnez(TMP, a, AT);
1184 __ Seleqz(AT, b, AT);
1185 }
1186 __ Or(out, TMP, AT);
1187 }
1188 }
1189 } else {
1190 if (type == Primitive::kPrimLong) {
1191 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
1192 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
1193 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
1194 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
1195 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
1196 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
1197
1198 MipsLabel compare_done;
1199
1200 if (a_lo == b_lo) {
1201 if (out_lo != a_lo) {
1202 __ Move(out_lo, a_lo);
1203 __ Move(out_hi, a_hi);
1204 }
1205 } else {
1206 __ Slt(TMP, a_hi, b_hi);
1207 __ Bne(a_hi, b_hi, &compare_done);
1208
1209 __ Sltu(TMP, a_lo, b_lo);
1210
1211 __ Bind(&compare_done);
1212
1213 if (is_min) {
1214 if (out_lo != a_lo) {
1215 __ Movn(out_hi, a_hi, TMP);
1216 __ Movn(out_lo, a_lo, TMP);
1217 }
1218 if (out_lo != b_lo) {
1219 __ Movz(out_hi, b_hi, TMP);
1220 __ Movz(out_lo, b_lo, TMP);
1221 }
1222 } else {
1223 if (out_lo != a_lo) {
1224 __ Movz(out_hi, a_hi, TMP);
1225 __ Movz(out_lo, a_lo, TMP);
1226 }
1227 if (out_lo != b_lo) {
1228 __ Movn(out_hi, b_hi, TMP);
1229 __ Movn(out_lo, b_lo, TMP);
1230 }
1231 }
1232 }
1233 } else {
1234 DCHECK_EQ(type, Primitive::kPrimInt);
1235 Register a = locations->InAt(0).AsRegister<Register>();
1236 Register b = locations->InAt(1).AsRegister<Register>();
1237 Register out = locations->Out().AsRegister<Register>();
1238
1239 if (a == b) {
1240 if (out != a) {
1241 __ Move(out, a);
1242 }
1243 } else {
1244 __ Slt(AT, a, b);
1245 if (is_min) {
1246 if (out != a) {
1247 __ Movn(out, a, AT);
1248 }
1249 if (out != b) {
1250 __ Movz(out, b, AT);
1251 }
1252 } else {
1253 if (out != a) {
1254 __ Movz(out, a, AT);
1255 }
1256 if (out != b) {
1257 __ Movn(out, b, AT);
1258 }
1259 }
1260 }
1261 }
1262 }
1263}
1264
1265// int java.lang.Math.min(int, int)
1266void IntrinsicLocationsBuilderMIPS::VisitMathMinIntInt(HInvoke* invoke) {
1267 CreateIntIntToIntLocations(arena_, invoke);
1268}
1269
1270void IntrinsicCodeGeneratorMIPS::VisitMathMinIntInt(HInvoke* invoke) {
1271 GenMinMax(invoke->GetLocations(),
1272 /* is_min */ true,
1273 Primitive::kPrimInt,
1274 IsR6(),
1275 GetAssembler());
1276}
1277
1278// long java.lang.Math.min(long, long)
1279void IntrinsicLocationsBuilderMIPS::VisitMathMinLongLong(HInvoke* invoke) {
1280 CreateIntIntToIntLocations(arena_, invoke);
1281}
1282
1283void IntrinsicCodeGeneratorMIPS::VisitMathMinLongLong(HInvoke* invoke) {
1284 GenMinMax(invoke->GetLocations(),
1285 /* is_min */ true,
1286 Primitive::kPrimLong,
1287 IsR6(),
1288 GetAssembler());
1289}
1290
1291// int java.lang.Math.max(int, int)
1292void IntrinsicLocationsBuilderMIPS::VisitMathMaxIntInt(HInvoke* invoke) {
1293 CreateIntIntToIntLocations(arena_, invoke);
1294}
1295
1296void IntrinsicCodeGeneratorMIPS::VisitMathMaxIntInt(HInvoke* invoke) {
1297 GenMinMax(invoke->GetLocations(),
1298 /* is_min */ false,
1299 Primitive::kPrimInt,
1300 IsR6(),
1301 GetAssembler());
1302}
1303
1304// long java.lang.Math.max(long, long)
1305void IntrinsicLocationsBuilderMIPS::VisitMathMaxLongLong(HInvoke* invoke) {
1306 CreateIntIntToIntLocations(arena_, invoke);
1307}
1308
1309void IntrinsicCodeGeneratorMIPS::VisitMathMaxLongLong(HInvoke* invoke) {
1310 GenMinMax(invoke->GetLocations(),
1311 /* is_min */ false,
1312 Primitive::kPrimLong,
1313 IsR6(),
1314 GetAssembler());
1315}
1316
1317// double java.lang.Math.sqrt(double)
1318void IntrinsicLocationsBuilderMIPS::VisitMathSqrt(HInvoke* invoke) {
1319 CreateFPToFPLocations(arena_, invoke);
1320}
1321
1322void IntrinsicCodeGeneratorMIPS::VisitMathSqrt(HInvoke* invoke) {
1323 LocationSummary* locations = invoke->GetLocations();
1324 MipsAssembler* assembler = GetAssembler();
1325 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
1326 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1327
1328 __ SqrtD(out, in);
1329}
1330
Chris Larsen3acee732015-11-18 13:31:08 -08001331// byte libcore.io.Memory.peekByte(long address)
1332void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekByte(HInvoke* invoke) {
1333 CreateIntToIntLocations(arena_, invoke);
1334}
1335
1336void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekByte(HInvoke* invoke) {
1337 MipsAssembler* assembler = GetAssembler();
1338 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1339 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
1340
1341 __ Lb(out, adr, 0);
1342}
1343
1344// short libcore.io.Memory.peekShort(long address)
1345void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekShortNative(HInvoke* invoke) {
1346 CreateIntToIntLocations(arena_, invoke);
1347}
1348
1349void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekShortNative(HInvoke* invoke) {
1350 MipsAssembler* assembler = GetAssembler();
1351 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1352 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
1353
1354 if (IsR6()) {
1355 __ Lh(out, adr, 0);
1356 } else if (IsR2OrNewer()) {
1357 // Unlike for words, there are no lhl/lhr instructions to load
1358 // unaligned halfwords so the code loads individual bytes, in case
1359 // the address isn't halfword-aligned, and assembles them into a
1360 // signed halfword.
1361 __ Lb(AT, adr, 1); // This byte must be sign-extended.
1362 __ Lb(out, adr, 0); // This byte can be either sign-extended, or
1363 // zero-extended because the following
1364 // instruction overwrites the sign bits.
1365 __ Ins(out, AT, 8, 24);
1366 } else {
1367 __ Lbu(AT, adr, 0); // This byte must be zero-extended. If it's not
1368 // the "or" instruction below will destroy the upper
1369 // 24 bits of the final result.
1370 __ Lb(out, adr, 1); // This byte must be sign-extended.
1371 __ Sll(out, out, 8);
1372 __ Or(out, out, AT);
1373 }
1374}
1375
1376// int libcore.io.Memory.peekInt(long address)
1377void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekIntNative(HInvoke* invoke) {
1378 CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap);
1379}
1380
1381void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekIntNative(HInvoke* invoke) {
1382 MipsAssembler* assembler = GetAssembler();
1383 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1384 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
1385
1386 if (IsR6()) {
1387 __ Lw(out, adr, 0);
1388 } else {
1389 __ Lwr(out, adr, 0);
1390 __ Lwl(out, adr, 3);
1391 }
1392}
1393
1394// long libcore.io.Memory.peekLong(long address)
1395void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekLongNative(HInvoke* invoke) {
1396 CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap);
1397}
1398
1399void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekLongNative(HInvoke* invoke) {
1400 MipsAssembler* assembler = GetAssembler();
1401 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1402 Register out_lo = invoke->GetLocations()->Out().AsRegisterPairLow<Register>();
1403 Register out_hi = invoke->GetLocations()->Out().AsRegisterPairHigh<Register>();
1404
1405 if (IsR6()) {
1406 __ Lw(out_lo, adr, 0);
1407 __ Lw(out_hi, adr, 4);
1408 } else {
1409 __ Lwr(out_lo, adr, 0);
1410 __ Lwl(out_lo, adr, 3);
1411 __ Lwr(out_hi, adr, 4);
1412 __ Lwl(out_hi, adr, 7);
1413 }
1414}
1415
1416static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1417 LocationSummary* locations = new (arena) LocationSummary(invoke,
1418 LocationSummary::kNoCall,
1419 kIntrinsified);
1420 locations->SetInAt(0, Location::RequiresRegister());
1421 locations->SetInAt(1, Location::RequiresRegister());
1422}
1423
1424// void libcore.io.Memory.pokeByte(long address, byte value)
1425void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeByte(HInvoke* invoke) {
1426 CreateIntIntToVoidLocations(arena_, invoke);
1427}
1428
1429void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeByte(HInvoke* invoke) {
1430 MipsAssembler* assembler = GetAssembler();
1431 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1432 Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>();
1433
1434 __ Sb(val, adr, 0);
1435}
1436
1437// void libcore.io.Memory.pokeShort(long address, short value)
1438void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeShortNative(HInvoke* invoke) {
1439 CreateIntIntToVoidLocations(arena_, invoke);
1440}
1441
1442void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeShortNative(HInvoke* invoke) {
1443 MipsAssembler* assembler = GetAssembler();
1444 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1445 Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>();
1446
1447 if (IsR6()) {
1448 __ Sh(val, adr, 0);
1449 } else {
1450 // Unlike for words, there are no shl/shr instructions to store
1451 // unaligned halfwords so the code stores individual bytes, in case
1452 // the address isn't halfword-aligned.
1453 __ Sb(val, adr, 0);
1454 __ Srl(AT, val, 8);
1455 __ Sb(AT, adr, 1);
1456 }
1457}
1458
1459// void libcore.io.Memory.pokeInt(long address, int value)
1460void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeIntNative(HInvoke* invoke) {
1461 CreateIntIntToVoidLocations(arena_, invoke);
1462}
1463
1464void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeIntNative(HInvoke* invoke) {
1465 MipsAssembler* assembler = GetAssembler();
1466 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1467 Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>();
1468
1469 if (IsR6()) {
1470 __ Sw(val, adr, 0);
1471 } else {
1472 __ Swr(val, adr, 0);
1473 __ Swl(val, adr, 3);
1474 }
1475}
1476
1477// void libcore.io.Memory.pokeLong(long address, long value)
1478void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeLongNative(HInvoke* invoke) {
1479 CreateIntIntToVoidLocations(arena_, invoke);
1480}
1481
1482void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeLongNative(HInvoke* invoke) {
1483 MipsAssembler* assembler = GetAssembler();
1484 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1485 Register val_lo = invoke->GetLocations()->InAt(1).AsRegisterPairLow<Register>();
1486 Register val_hi = invoke->GetLocations()->InAt(1).AsRegisterPairHigh<Register>();
1487
1488 if (IsR6()) {
1489 __ Sw(val_lo, adr, 0);
1490 __ Sw(val_hi, adr, 4);
1491 } else {
1492 __ Swr(val_lo, adr, 0);
1493 __ Swl(val_lo, adr, 3);
1494 __ Swr(val_hi, adr, 4);
1495 __ Swl(val_hi, adr, 7);
1496 }
1497}
1498
Chris Larsencf283da2016-01-19 16:45:35 -08001499// Thread java.lang.Thread.currentThread()
1500void IntrinsicLocationsBuilderMIPS::VisitThreadCurrentThread(HInvoke* invoke) {
1501 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1502 LocationSummary::kNoCall,
1503 kIntrinsified);
1504 locations->SetOut(Location::RequiresRegister());
1505}
1506
1507void IntrinsicCodeGeneratorMIPS::VisitThreadCurrentThread(HInvoke* invoke) {
1508 MipsAssembler* assembler = GetAssembler();
1509 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
1510
1511 __ LoadFromOffset(kLoadWord,
1512 out,
1513 TR,
1514 Thread::PeerOffset<kMipsPointerSize>().Int32Value());
1515}
1516
Alexey Frunze15958152017-02-09 19:08:30 -08001517static void CreateIntIntIntToIntLocations(ArenaAllocator* arena,
1518 HInvoke* invoke,
1519 Primitive::Type type) {
1520 bool can_call = kEmitCompilerReadBarrier &&
1521 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1522 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001523 LocationSummary* locations = new (arena) LocationSummary(invoke,
Alexey Frunze15958152017-02-09 19:08:30 -08001524 (can_call
1525 ? LocationSummary::kCallOnSlowPath
1526 : LocationSummary::kNoCall),
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001527 kIntrinsified);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001528 if (can_call && kUseBakerReadBarrier) {
1529 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1530 }
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001531 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1532 locations->SetInAt(1, Location::RequiresRegister());
1533 locations->SetInAt(2, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08001534 locations->SetOut(Location::RequiresRegister(),
1535 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
1536 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1537 // We need a temporary register for the read barrier marking slow
1538 // path in InstructionCodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier.
1539 locations->AddTemp(Location::RequiresRegister());
1540 }
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001541}
1542
Alexey Frunze15958152017-02-09 19:08:30 -08001543// Note that the caller must supply a properly aligned memory address.
1544// If they do not, the behavior is undefined (atomicity not guaranteed, exception may occur).
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001545static void GenUnsafeGet(HInvoke* invoke,
1546 Primitive::Type type,
1547 bool is_volatile,
1548 bool is_R6,
1549 CodeGeneratorMIPS* codegen) {
1550 LocationSummary* locations = invoke->GetLocations();
1551 DCHECK((type == Primitive::kPrimInt) ||
1552 (type == Primitive::kPrimLong) ||
1553 (type == Primitive::kPrimNot)) << type;
1554 MipsAssembler* assembler = codegen->GetAssembler();
Alexey Frunze15958152017-02-09 19:08:30 -08001555 // Target register.
1556 Location trg_loc = locations->Out();
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001557 // Object pointer.
Alexey Frunze15958152017-02-09 19:08:30 -08001558 Location base_loc = locations->InAt(1);
1559 Register base = base_loc.AsRegister<Register>();
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001560 // The "offset" argument is passed as a "long". Since this code is for
1561 // a 32-bit processor, we can only use 32-bit addresses, so we only
1562 // need the low 32-bits of offset.
Alexey Frunze15958152017-02-09 19:08:30 -08001563 Location offset_loc = locations->InAt(2);
1564 Register offset_lo = offset_loc.AsRegisterPairLow<Register>();
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001565
Alexey Frunze15958152017-02-09 19:08:30 -08001566 if (!(kEmitCompilerReadBarrier && kUseBakerReadBarrier && (type == Primitive::kPrimNot))) {
1567 __ Addu(TMP, base, offset_lo);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001568 }
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001569
Alexey Frunze15958152017-02-09 19:08:30 -08001570 switch (type) {
1571 case Primitive::kPrimLong: {
1572 Register trg_lo = trg_loc.AsRegisterPairLow<Register>();
1573 Register trg_hi = trg_loc.AsRegisterPairHigh<Register>();
1574 CHECK(!is_volatile); // TODO: support atomic 8-byte volatile loads.
1575 if (is_R6) {
1576 __ Lw(trg_lo, TMP, 0);
1577 __ Lw(trg_hi, TMP, 4);
1578 } else {
1579 __ Lwr(trg_lo, TMP, 0);
1580 __ Lwl(trg_lo, TMP, 3);
1581 __ Lwr(trg_hi, TMP, 4);
1582 __ Lwl(trg_hi, TMP, 7);
1583 }
1584 break;
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001585 }
Alexey Frunzec061de12017-02-14 13:27:23 -08001586
Alexey Frunze15958152017-02-09 19:08:30 -08001587 case Primitive::kPrimInt: {
1588 Register trg = trg_loc.AsRegister<Register>();
1589 if (is_R6) {
1590 __ Lw(trg, TMP, 0);
1591 } else {
1592 __ Lwr(trg, TMP, 0);
1593 __ Lwl(trg, TMP, 3);
1594 }
1595 if (is_volatile) {
1596 __ Sync(0);
1597 }
1598 break;
Alexey Frunzec061de12017-02-14 13:27:23 -08001599 }
Alexey Frunze15958152017-02-09 19:08:30 -08001600
1601 case Primitive::kPrimNot: {
1602 Register trg = trg_loc.AsRegister<Register>();
1603 if (kEmitCompilerReadBarrier) {
1604 if (kUseBakerReadBarrier) {
1605 Location temp = locations->GetTemp(0);
1606 codegen->GenerateReferenceLoadWithBakerReadBarrier(invoke,
1607 trg_loc,
1608 base,
1609 /* offset */ 0U,
1610 /* index */ offset_loc,
1611 TIMES_1,
1612 temp,
1613 /* needs_null_check */ false);
1614 if (is_volatile) {
1615 __ Sync(0);
1616 }
1617 } else {
1618 if (is_R6) {
1619 __ Lw(trg, TMP, 0);
1620 } else {
1621 __ Lwr(trg, TMP, 0);
1622 __ Lwl(trg, TMP, 3);
1623 }
1624 if (is_volatile) {
1625 __ Sync(0);
1626 }
1627 codegen->GenerateReadBarrierSlow(invoke,
1628 trg_loc,
1629 trg_loc,
1630 base_loc,
1631 /* offset */ 0U,
1632 /* index */ offset_loc);
1633 }
1634 } else {
1635 if (is_R6) {
1636 __ Lw(trg, TMP, 0);
1637 } else {
1638 __ Lwr(trg, TMP, 0);
1639 __ Lwl(trg, TMP, 3);
1640 }
1641 if (is_volatile) {
1642 __ Sync(0);
1643 }
1644 __ MaybeUnpoisonHeapReference(trg);
1645 }
1646 break;
1647 }
1648
1649 default:
1650 LOG(FATAL) << "Unexpected type " << type;
1651 UNREACHABLE();
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001652 }
1653}
1654
1655// int sun.misc.Unsafe.getInt(Object o, long offset)
1656void IntrinsicLocationsBuilderMIPS::VisitUnsafeGet(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001657 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001658}
1659
1660void IntrinsicCodeGeneratorMIPS::VisitUnsafeGet(HInvoke* invoke) {
1661 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, IsR6(), codegen_);
1662}
1663
1664// int sun.misc.Unsafe.getIntVolatile(Object o, long offset)
1665void IntrinsicLocationsBuilderMIPS::VisitUnsafeGetVolatile(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001666 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001667}
1668
1669void IntrinsicCodeGeneratorMIPS::VisitUnsafeGetVolatile(HInvoke* invoke) {
1670 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, IsR6(), codegen_);
1671}
1672
1673// long sun.misc.Unsafe.getLong(Object o, long offset)
1674void IntrinsicLocationsBuilderMIPS::VisitUnsafeGetLong(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001675 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001676}
1677
1678void IntrinsicCodeGeneratorMIPS::VisitUnsafeGetLong(HInvoke* invoke) {
1679 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, IsR6(), codegen_);
1680}
1681
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001682// Object sun.misc.Unsafe.getObject(Object o, long offset)
1683void IntrinsicLocationsBuilderMIPS::VisitUnsafeGetObject(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001684 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001685}
1686
1687void IntrinsicCodeGeneratorMIPS::VisitUnsafeGetObject(HInvoke* invoke) {
1688 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, IsR6(), codegen_);
1689}
1690
1691// Object sun.misc.Unsafe.getObjectVolatile(Object o, long offset)
1692void IntrinsicLocationsBuilderMIPS::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001693 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001694}
1695
1696void IntrinsicCodeGeneratorMIPS::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1697 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, IsR6(), codegen_);
1698}
1699
1700static void CreateIntIntIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1701 LocationSummary* locations = new (arena) LocationSummary(invoke,
1702 LocationSummary::kNoCall,
1703 kIntrinsified);
1704 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1705 locations->SetInAt(1, Location::RequiresRegister());
1706 locations->SetInAt(2, Location::RequiresRegister());
1707 locations->SetInAt(3, Location::RequiresRegister());
1708}
1709
Alexey Frunze15958152017-02-09 19:08:30 -08001710// Note that the caller must supply a properly aligned memory address.
1711// If they do not, the behavior is undefined (atomicity not guaranteed, exception may occur).
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001712static void GenUnsafePut(LocationSummary* locations,
1713 Primitive::Type type,
1714 bool is_volatile,
1715 bool is_ordered,
1716 bool is_R6,
1717 CodeGeneratorMIPS* codegen) {
1718 DCHECK((type == Primitive::kPrimInt) ||
1719 (type == Primitive::kPrimLong) ||
1720 (type == Primitive::kPrimNot)) << type;
1721 MipsAssembler* assembler = codegen->GetAssembler();
1722 // Object pointer.
1723 Register base = locations->InAt(1).AsRegister<Register>();
1724 // The "offset" argument is passed as a "long", i.e., it's 64-bits in
1725 // size. Since this code is for a 32-bit processor, we can only use
1726 // 32-bit addresses, so we only need the low 32-bits of offset.
1727 Register offset_lo = locations->InAt(2).AsRegisterPairLow<Register>();
1728
1729 __ Addu(TMP, base, offset_lo);
1730 if (is_volatile || is_ordered) {
1731 __ Sync(0);
1732 }
1733 if ((type == Primitive::kPrimInt) || (type == Primitive::kPrimNot)) {
1734 Register value = locations->InAt(3).AsRegister<Register>();
1735
Alexey Frunzec061de12017-02-14 13:27:23 -08001736 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1737 __ PoisonHeapReference(AT, value);
1738 value = AT;
1739 }
1740
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001741 if (is_R6) {
1742 __ Sw(value, TMP, 0);
1743 } else {
1744 __ Swr(value, TMP, 0);
1745 __ Swl(value, TMP, 3);
1746 }
1747 } else {
1748 Register value_lo = locations->InAt(3).AsRegisterPairLow<Register>();
1749 Register value_hi = locations->InAt(3).AsRegisterPairHigh<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08001750 CHECK(!is_volatile); // TODO: support atomic 8-byte volatile stores.
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001751 if (is_R6) {
1752 __ Sw(value_lo, TMP, 0);
1753 __ Sw(value_hi, TMP, 4);
1754 } else {
1755 __ Swr(value_lo, TMP, 0);
1756 __ Swl(value_lo, TMP, 3);
1757 __ Swr(value_hi, TMP, 4);
1758 __ Swl(value_hi, TMP, 7);
1759 }
1760 }
1761
1762 if (is_volatile) {
1763 __ Sync(0);
1764 }
1765
1766 if (type == Primitive::kPrimNot) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01001767 bool value_can_be_null = true; // TODO: Worth finding out this information?
1768 codegen->MarkGCCard(base, locations->InAt(3).AsRegister<Register>(), value_can_be_null);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001769 }
1770}
1771
1772// void sun.misc.Unsafe.putInt(Object o, long offset, int x)
1773void IntrinsicLocationsBuilderMIPS::VisitUnsafePut(HInvoke* invoke) {
1774 CreateIntIntIntIntToVoidLocations(arena_, invoke);
1775}
1776
1777void IntrinsicCodeGeneratorMIPS::VisitUnsafePut(HInvoke* invoke) {
1778 GenUnsafePut(invoke->GetLocations(),
1779 Primitive::kPrimInt,
1780 /* is_volatile */ false,
1781 /* is_ordered */ false,
1782 IsR6(),
1783 codegen_);
1784}
1785
1786// void sun.misc.Unsafe.putOrderedInt(Object o, long offset, int x)
1787void IntrinsicLocationsBuilderMIPS::VisitUnsafePutOrdered(HInvoke* invoke) {
1788 CreateIntIntIntIntToVoidLocations(arena_, invoke);
1789}
1790
1791void IntrinsicCodeGeneratorMIPS::VisitUnsafePutOrdered(HInvoke* invoke) {
1792 GenUnsafePut(invoke->GetLocations(),
1793 Primitive::kPrimInt,
1794 /* is_volatile */ false,
1795 /* is_ordered */ true,
1796 IsR6(),
1797 codegen_);
1798}
1799
1800// void sun.misc.Unsafe.putIntVolatile(Object o, long offset, int x)
1801void IntrinsicLocationsBuilderMIPS::VisitUnsafePutVolatile(HInvoke* invoke) {
1802 CreateIntIntIntIntToVoidLocations(arena_, invoke);
1803}
1804
1805void IntrinsicCodeGeneratorMIPS::VisitUnsafePutVolatile(HInvoke* invoke) {
1806 GenUnsafePut(invoke->GetLocations(),
1807 Primitive::kPrimInt,
1808 /* is_volatile */ true,
1809 /* is_ordered */ false,
1810 IsR6(),
1811 codegen_);
1812}
1813
1814// void sun.misc.Unsafe.putObject(Object o, long offset, Object x)
1815void IntrinsicLocationsBuilderMIPS::VisitUnsafePutObject(HInvoke* invoke) {
1816 CreateIntIntIntIntToVoidLocations(arena_, invoke);
1817}
1818
1819void IntrinsicCodeGeneratorMIPS::VisitUnsafePutObject(HInvoke* invoke) {
1820 GenUnsafePut(invoke->GetLocations(),
1821 Primitive::kPrimNot,
1822 /* is_volatile */ false,
1823 /* is_ordered */ false,
1824 IsR6(),
1825 codegen_);
1826}
1827
1828// void sun.misc.Unsafe.putOrderedObject(Object o, long offset, Object x)
1829void IntrinsicLocationsBuilderMIPS::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1830 CreateIntIntIntIntToVoidLocations(arena_, invoke);
1831}
1832
1833void IntrinsicCodeGeneratorMIPS::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1834 GenUnsafePut(invoke->GetLocations(),
1835 Primitive::kPrimNot,
1836 /* is_volatile */ false,
1837 /* is_ordered */ true,
1838 IsR6(),
1839 codegen_);
1840}
1841
1842// void sun.misc.Unsafe.putObjectVolatile(Object o, long offset, Object x)
1843void IntrinsicLocationsBuilderMIPS::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1844 CreateIntIntIntIntToVoidLocations(arena_, invoke);
1845}
1846
1847void IntrinsicCodeGeneratorMIPS::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1848 GenUnsafePut(invoke->GetLocations(),
1849 Primitive::kPrimNot,
1850 /* is_volatile */ true,
1851 /* is_ordered */ false,
1852 IsR6(),
1853 codegen_);
1854}
1855
1856// void sun.misc.Unsafe.putLong(Object o, long offset, long x)
1857void IntrinsicLocationsBuilderMIPS::VisitUnsafePutLong(HInvoke* invoke) {
1858 CreateIntIntIntIntToVoidLocations(arena_, invoke);
1859}
1860
1861void IntrinsicCodeGeneratorMIPS::VisitUnsafePutLong(HInvoke* invoke) {
1862 GenUnsafePut(invoke->GetLocations(),
1863 Primitive::kPrimLong,
1864 /* is_volatile */ false,
1865 /* is_ordered */ false,
1866 IsR6(),
1867 codegen_);
1868}
1869
1870// void sun.misc.Unsafe.putOrderedLong(Object o, long offset, long x)
1871void IntrinsicLocationsBuilderMIPS::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1872 CreateIntIntIntIntToVoidLocations(arena_, invoke);
1873}
1874
1875void IntrinsicCodeGeneratorMIPS::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1876 GenUnsafePut(invoke->GetLocations(),
1877 Primitive::kPrimLong,
1878 /* is_volatile */ false,
1879 /* is_ordered */ true,
1880 IsR6(),
1881 codegen_);
1882}
1883
Alexey Frunze15958152017-02-09 19:08:30 -08001884static void CreateIntIntIntIntIntToIntPlusTemps(ArenaAllocator* arena, HInvoke* invoke) {
1885 bool can_call = kEmitCompilerReadBarrier &&
1886 kUseBakerReadBarrier &&
1887 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001888 LocationSummary* locations = new (arena) LocationSummary(invoke,
Alexey Frunze15958152017-02-09 19:08:30 -08001889 (can_call
1890 ? LocationSummary::kCallOnSlowPath
1891 : LocationSummary::kNoCall),
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001892 kIntrinsified);
1893 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1894 locations->SetInAt(1, Location::RequiresRegister());
1895 locations->SetInAt(2, Location::RequiresRegister());
1896 locations->SetInAt(3, Location::RequiresRegister());
1897 locations->SetInAt(4, Location::RequiresRegister());
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001898 locations->SetOut(Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08001899
1900 // Temporary register used in CAS by (Baker) read barrier.
1901 if (can_call) {
1902 locations->AddTemp(Location::RequiresRegister());
1903 }
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001904}
1905
Alexey Frunze15958152017-02-09 19:08:30 -08001906// Note that the caller must supply a properly aligned memory address.
1907// If they do not, the behavior is undefined (atomicity not guaranteed, exception may occur).
1908static void GenCas(HInvoke* invoke, Primitive::Type type, CodeGeneratorMIPS* codegen) {
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001909 MipsAssembler* assembler = codegen->GetAssembler();
Alexey Frunze15958152017-02-09 19:08:30 -08001910 LocationSummary* locations = invoke->GetLocations();
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001911 bool isR6 = codegen->GetInstructionSetFeatures().IsR6();
1912 Register base = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08001913 Location offset_loc = locations->InAt(2);
1914 Register offset_lo = offset_loc.AsRegisterPairLow<Register>();
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001915 Register expected = locations->InAt(3).AsRegister<Register>();
1916 Register value = locations->InAt(4).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08001917 Location out_loc = locations->Out();
1918 Register out = out_loc.AsRegister<Register>();
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001919
1920 DCHECK_NE(base, out);
1921 DCHECK_NE(offset_lo, out);
1922 DCHECK_NE(expected, out);
1923
1924 if (type == Primitive::kPrimNot) {
Alexey Frunze15958152017-02-09 19:08:30 -08001925 // The only read barrier implementation supporting the
1926 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1927 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1928
1929 // Mark card for object assuming new value is stored. Worst case we will mark an unchanged
1930 // object and scan the receiver at the next GC for nothing.
Goran Jakovljevice114da22016-12-26 14:21:43 +01001931 bool value_can_be_null = true; // TODO: Worth finding out this information?
1932 codegen->MarkGCCard(base, value, value_can_be_null);
Alexey Frunze15958152017-02-09 19:08:30 -08001933
1934 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1935 Location temp = locations->GetTemp(0);
1936 // Need to make sure the reference stored in the field is a to-space
1937 // one before attempting the CAS or the CAS could fail incorrectly.
1938 codegen->GenerateReferenceLoadWithBakerReadBarrier(
1939 invoke,
1940 out_loc, // Unused, used only as a "temporary" within the read barrier.
1941 base,
1942 /* offset */ 0u,
1943 /* index */ offset_loc,
1944 ScaleFactor::TIMES_1,
1945 temp,
1946 /* needs_null_check */ false,
1947 /* always_update_field */ true);
1948 }
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001949 }
1950
Alexey Frunzec061de12017-02-14 13:27:23 -08001951 MipsLabel loop_head, exit_loop;
1952 __ Addu(TMP, base, offset_lo);
1953
1954 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1955 __ PoisonHeapReference(expected);
1956 // Do not poison `value`, if it is the same register as
1957 // `expected`, which has just been poisoned.
1958 if (value != expected) {
1959 __ PoisonHeapReference(value);
1960 }
1961 }
1962
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001963 // do {
1964 // tmp_value = [tmp_ptr] - expected;
1965 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1966 // result = tmp_value != 0;
1967
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001968 __ Sync(0);
1969 __ Bind(&loop_head);
1970 if ((type == Primitive::kPrimInt) || (type == Primitive::kPrimNot)) {
1971 if (isR6) {
1972 __ LlR6(out, TMP);
1973 } else {
1974 __ LlR2(out, TMP);
1975 }
1976 } else {
Alexey Frunzec061de12017-02-14 13:27:23 -08001977 LOG(FATAL) << "Unsupported op size " << type;
1978 UNREACHABLE();
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001979 }
1980 __ Subu(out, out, expected); // If we didn't get the 'expected'
1981 __ Sltiu(out, out, 1); // value, set 'out' to false, and
1982 __ Beqz(out, &exit_loop); // return.
1983 __ Move(out, value); // Use 'out' for the 'store conditional' instruction.
1984 // If we use 'value' directly, we would lose 'value'
1985 // in the case that the store fails. Whether the
1986 // store succeeds, or fails, it will load the
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001987 // correct Boolean value into the 'out' register.
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001988 // This test isn't really necessary. We only support Primitive::kPrimInt,
1989 // Primitive::kPrimNot, and we already verified that we're working on one
1990 // of those two types. It's left here in case the code needs to support
1991 // other types in the future.
1992 if ((type == Primitive::kPrimInt) || (type == Primitive::kPrimNot)) {
1993 if (isR6) {
1994 __ ScR6(out, TMP);
1995 } else {
1996 __ ScR2(out, TMP);
1997 }
1998 }
1999 __ Beqz(out, &loop_head); // If we couldn't do the read-modify-write
2000 // cycle atomically then retry.
2001 __ Bind(&exit_loop);
2002 __ Sync(0);
Alexey Frunzec061de12017-02-14 13:27:23 -08002003
2004 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
2005 __ UnpoisonHeapReference(expected);
2006 // Do not unpoison `value`, if it is the same register as
2007 // `expected`, which has just been unpoisoned.
2008 if (value != expected) {
2009 __ UnpoisonHeapReference(value);
2010 }
2011 }
Alexey Frunze51aff3a2016-03-17 17:21:45 -07002012}
2013
2014// boolean sun.misc.Unsafe.compareAndSwapInt(Object o, long offset, int expected, int x)
2015void IntrinsicLocationsBuilderMIPS::VisitUnsafeCASInt(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08002016 CreateIntIntIntIntIntToIntPlusTemps(arena_, invoke);
Alexey Frunze51aff3a2016-03-17 17:21:45 -07002017}
2018
2019void IntrinsicCodeGeneratorMIPS::VisitUnsafeCASInt(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08002020 GenCas(invoke, Primitive::kPrimInt, codegen_);
Alexey Frunze51aff3a2016-03-17 17:21:45 -07002021}
2022
2023// boolean sun.misc.Unsafe.compareAndSwapObject(Object o, long offset, Object expected, Object x)
2024void IntrinsicLocationsBuilderMIPS::VisitUnsafeCASObject(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08002025 // The only read barrier implementation supporting the
2026 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2027 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
2028 return;
2029 }
2030
2031 CreateIntIntIntIntIntToIntPlusTemps(arena_, invoke);
Alexey Frunze51aff3a2016-03-17 17:21:45 -07002032}
2033
2034void IntrinsicCodeGeneratorMIPS::VisitUnsafeCASObject(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08002035 // The only read barrier implementation supporting the
2036 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2037 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2038
2039 GenCas(invoke, Primitive::kPrimNot, codegen_);
Alexey Frunze51aff3a2016-03-17 17:21:45 -07002040}
2041
Chris Larsencf283da2016-01-19 16:45:35 -08002042// int java.lang.String.compareTo(String anotherString)
2043void IntrinsicLocationsBuilderMIPS::VisitStringCompareTo(HInvoke* invoke) {
2044 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescufca16662016-07-14 09:21:59 +01002045 LocationSummary::kCallOnMainAndSlowPath,
Chris Larsencf283da2016-01-19 16:45:35 -08002046 kIntrinsified);
2047 InvokeRuntimeCallingConvention calling_convention;
2048 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2049 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2050 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
2051 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2052}
2053
2054void IntrinsicCodeGeneratorMIPS::VisitStringCompareTo(HInvoke* invoke) {
2055 MipsAssembler* assembler = GetAssembler();
2056 LocationSummary* locations = invoke->GetLocations();
2057
2058 // Note that the null check must have been done earlier.
2059 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
2060
2061 Register argument = locations->InAt(1).AsRegister<Register>();
2062 SlowPathCodeMIPS* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS(invoke);
2063 codegen_->AddSlowPath(slow_path);
2064 __ Beqz(argument, slow_path->GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +01002065 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Chris Larsencf283da2016-01-19 16:45:35 -08002066 __ Bind(slow_path->GetExitLabel());
2067}
2068
Chris Larsen16ba2b42015-11-02 10:58:31 -08002069// boolean java.lang.String.equals(Object anObject)
2070void IntrinsicLocationsBuilderMIPS::VisitStringEquals(HInvoke* invoke) {
2071 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2072 LocationSummary::kNoCall,
2073 kIntrinsified);
2074 locations->SetInAt(0, Location::RequiresRegister());
2075 locations->SetInAt(1, Location::RequiresRegister());
2076 locations->SetOut(Location::RequiresRegister());
2077
2078 // Temporary registers to store lengths of strings and for calculations.
2079 locations->AddTemp(Location::RequiresRegister());
2080 locations->AddTemp(Location::RequiresRegister());
2081 locations->AddTemp(Location::RequiresRegister());
2082}
2083
2084void IntrinsicCodeGeneratorMIPS::VisitStringEquals(HInvoke* invoke) {
2085 MipsAssembler* assembler = GetAssembler();
2086 LocationSummary* locations = invoke->GetLocations();
2087
2088 Register str = locations->InAt(0).AsRegister<Register>();
2089 Register arg = locations->InAt(1).AsRegister<Register>();
2090 Register out = locations->Out().AsRegister<Register>();
2091
2092 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2093 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2094 Register temp3 = locations->GetTemp(2).AsRegister<Register>();
2095
2096 MipsLabel loop;
2097 MipsLabel end;
2098 MipsLabel return_true;
2099 MipsLabel return_false;
2100
2101 // Get offsets of count, value, and class fields within a string object.
2102 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2103 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
2104 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
2105
2106 // Note that the null check must have been done earlier.
2107 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
2108
2109 // If the register containing the pointer to "this", and the register
2110 // containing the pointer to "anObject" are the same register then
2111 // "this", and "anObject" are the same object and we can
2112 // short-circuit the logic to a true result.
2113 if (str == arg) {
2114 __ LoadConst32(out, 1);
2115 return;
2116 }
Goran Jakovljevic64fa84f2017-02-27 13:14:57 +01002117 StringEqualsOptimizations optimizations(invoke);
2118 if (!optimizations.GetArgumentNotNull()) {
2119 // Check if input is null, return false if it is.
2120 __ Beqz(arg, &return_false);
2121 }
Chris Larsen16ba2b42015-11-02 10:58:31 -08002122
2123 // Reference equality check, return true if same reference.
2124 __ Beq(str, arg, &return_true);
2125
Goran Jakovljevic64fa84f2017-02-27 13:14:57 +01002126 if (!optimizations.GetArgumentIsString()) {
2127 // Instanceof check for the argument by comparing class fields.
2128 // All string objects must have the same type since String cannot be subclassed.
2129 // Receiver must be a string object, so its class field is equal to all strings' class fields.
2130 // If the argument is a string object, its class field must be equal to receiver's class field.
2131 __ Lw(temp1, str, class_offset);
2132 __ Lw(temp2, arg, class_offset);
2133 __ Bne(temp1, temp2, &return_false);
2134 }
Chris Larsen16ba2b42015-11-02 10:58:31 -08002135
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002136 // Load `count` fields of this and argument strings.
Chris Larsen16ba2b42015-11-02 10:58:31 -08002137 __ Lw(temp1, str, count_offset);
2138 __ Lw(temp2, arg, count_offset);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002139 // Check if `count` fields are equal, return false if they're not.
2140 // Also compares the compression style, if differs return false.
Chris Larsen16ba2b42015-11-02 10:58:31 -08002141 __ Bne(temp1, temp2, &return_false);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002142 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
2143 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2144 "Expecting 0=compressed, 1=uncompressed");
Chris Larsen16ba2b42015-11-02 10:58:31 -08002145 __ Beqz(temp1, &return_true);
2146
2147 // Don't overwrite input registers
2148 __ Move(TMP, str);
2149 __ Move(temp3, arg);
2150
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002151 // Assertions that must hold in order to compare strings 4 bytes at a time.
Chris Larsen16ba2b42015-11-02 10:58:31 -08002152 DCHECK_ALIGNED(value_offset, 4);
2153 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
2154
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002155 // For string compression, calculate the number of bytes to compare (not chars).
2156 if (mirror::kUseStringCompression) {
2157 // Extract compression flag.
2158 if (IsR2OrNewer()) {
2159 __ Ext(temp2, temp1, 0, 1);
2160 } else {
2161 __ Sll(temp2, temp1, 31);
2162 __ Srl(temp2, temp2, 31);
2163 }
2164 __ Srl(temp1, temp1, 1); // Extract length.
2165 __ Sllv(temp1, temp1, temp2); // Double the byte count if uncompressed.
2166 }
2167
2168 // Loop to compare strings 4 bytes at a time starting at the beginning of the string.
2169 // Ok to do this because strings are zero-padded to kObjectAlignment.
Chris Larsen16ba2b42015-11-02 10:58:31 -08002170 __ Bind(&loop);
2171 __ Lw(out, TMP, value_offset);
2172 __ Lw(temp2, temp3, value_offset);
2173 __ Bne(out, temp2, &return_false);
2174 __ Addiu(TMP, TMP, 4);
2175 __ Addiu(temp3, temp3, 4);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002176 // With string compression, we have compared 4 bytes, otherwise 2 chars.
2177 __ Addiu(temp1, temp1, mirror::kUseStringCompression ? -4 : -2);
Chris Larsen16ba2b42015-11-02 10:58:31 -08002178 __ Bgtz(temp1, &loop);
2179
2180 // Return true and exit the function.
2181 // If loop does not result in returning false, we return true.
2182 __ Bind(&return_true);
2183 __ LoadConst32(out, 1);
2184 __ B(&end);
2185
2186 // Return false and exit the function.
2187 __ Bind(&return_false);
2188 __ LoadConst32(out, 0);
2189 __ Bind(&end);
2190}
2191
Chris Larsencf283da2016-01-19 16:45:35 -08002192static void GenerateStringIndexOf(HInvoke* invoke,
2193 bool start_at_zero,
2194 MipsAssembler* assembler,
2195 CodeGeneratorMIPS* codegen,
2196 ArenaAllocator* allocator) {
2197 LocationSummary* locations = invoke->GetLocations();
2198 Register tmp_reg = start_at_zero ? locations->GetTemp(0).AsRegister<Register>() : TMP;
2199
2200 // Note that the null check must have been done earlier.
2201 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
2202
Vladimir Markofb6c90a2016-05-06 15:52:12 +01002203 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
2204 // or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
Chris Larsencf283da2016-01-19 16:45:35 -08002205 SlowPathCodeMIPS* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01002206 HInstruction* code_point = invoke->InputAt(1);
2207 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01002208 if (!IsUint<16>(code_point->AsIntConstant()->GetValue())) {
Chris Larsencf283da2016-01-19 16:45:35 -08002209 // Always needs the slow-path. We could directly dispatch to it,
2210 // but this case should be rare, so for simplicity just put the
2211 // full slow-path down and branch unconditionally.
2212 slow_path = new (allocator) IntrinsicSlowPathMIPS(invoke);
2213 codegen->AddSlowPath(slow_path);
2214 __ B(slow_path->GetEntryLabel());
2215 __ Bind(slow_path->GetExitLabel());
2216 return;
2217 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01002218 } else if (code_point->GetType() != Primitive::kPrimChar) {
Chris Larsencf283da2016-01-19 16:45:35 -08002219 Register char_reg = locations->InAt(1).AsRegister<Register>();
2220 // The "bltu" conditional branch tests to see if the character value
2221 // fits in a valid 16-bit (MIPS halfword) value. If it doesn't then
2222 // the character being searched for, if it exists in the string, is
2223 // encoded using UTF-16 and stored in the string as two (16-bit)
2224 // halfwords. Currently the assembly code used to implement this
2225 // intrinsic doesn't support searching for a character stored as
2226 // two halfwords so we fallback to using the generic implementation
2227 // of indexOf().
2228 __ LoadConst32(tmp_reg, std::numeric_limits<uint16_t>::max());
2229 slow_path = new (allocator) IntrinsicSlowPathMIPS(invoke);
2230 codegen->AddSlowPath(slow_path);
2231 __ Bltu(tmp_reg, char_reg, slow_path->GetEntryLabel());
2232 }
2233
2234 if (start_at_zero) {
2235 DCHECK_EQ(tmp_reg, A2);
2236 // Start-index = 0.
2237 __ Clear(tmp_reg);
2238 }
2239
Serban Constantinescufca16662016-07-14 09:21:59 +01002240 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
Chris Larsencf283da2016-01-19 16:45:35 -08002241 if (slow_path != nullptr) {
2242 __ Bind(slow_path->GetExitLabel());
2243 }
2244}
2245
2246// int java.lang.String.indexOf(int ch)
2247void IntrinsicLocationsBuilderMIPS::VisitStringIndexOf(HInvoke* invoke) {
2248 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00002249 LocationSummary::kCallOnMainAndSlowPath,
Chris Larsencf283da2016-01-19 16:45:35 -08002250 kIntrinsified);
2251 // We have a hand-crafted assembly stub that follows the runtime
2252 // calling convention. So it's best to align the inputs accordingly.
2253 InvokeRuntimeCallingConvention calling_convention;
2254 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2255 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2256 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
2257 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2258
2259 // Need a temp for slow-path codepoint compare, and need to send start-index=0.
2260 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2261}
2262
2263void IntrinsicCodeGeneratorMIPS::VisitStringIndexOf(HInvoke* invoke) {
2264 GenerateStringIndexOf(invoke,
2265 /* start_at_zero */ true,
2266 GetAssembler(),
2267 codegen_,
2268 GetAllocator());
2269}
2270
2271// int java.lang.String.indexOf(int ch, int fromIndex)
2272void IntrinsicLocationsBuilderMIPS::VisitStringIndexOfAfter(HInvoke* invoke) {
2273 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00002274 LocationSummary::kCallOnMainAndSlowPath,
Chris Larsencf283da2016-01-19 16:45:35 -08002275 kIntrinsified);
2276 // We have a hand-crafted assembly stub that follows the runtime
2277 // calling convention. So it's best to align the inputs accordingly.
2278 InvokeRuntimeCallingConvention calling_convention;
2279 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2280 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2281 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2282 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
2283 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2284
2285 // Need a temp for slow-path codepoint compare.
2286 locations->AddTemp(Location::RequiresRegister());
2287}
2288
2289void IntrinsicCodeGeneratorMIPS::VisitStringIndexOfAfter(HInvoke* invoke) {
2290 GenerateStringIndexOf(invoke,
2291 /* start_at_zero */ false,
2292 GetAssembler(),
2293 codegen_,
2294 GetAllocator());
2295}
2296
2297// java.lang.StringFactory.newStringFromBytes(byte[] data, int high, int offset, int byteCount)
2298void IntrinsicLocationsBuilderMIPS::VisitStringNewStringFromBytes(HInvoke* invoke) {
2299 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00002300 LocationSummary::kCallOnMainAndSlowPath,
Chris Larsencf283da2016-01-19 16:45:35 -08002301 kIntrinsified);
2302 InvokeRuntimeCallingConvention calling_convention;
2303 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2304 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2305 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2306 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
2307 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
2308 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2309}
2310
2311void IntrinsicCodeGeneratorMIPS::VisitStringNewStringFromBytes(HInvoke* invoke) {
2312 MipsAssembler* assembler = GetAssembler();
2313 LocationSummary* locations = invoke->GetLocations();
2314
2315 Register byte_array = locations->InAt(0).AsRegister<Register>();
2316 SlowPathCodeMIPS* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS(invoke);
2317 codegen_->AddSlowPath(slow_path);
2318 __ Beqz(byte_array, slow_path->GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +01002319 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
Chris Larsencf283da2016-01-19 16:45:35 -08002320 __ Bind(slow_path->GetExitLabel());
2321}
2322
2323// java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
2324void IntrinsicLocationsBuilderMIPS::VisitStringNewStringFromChars(HInvoke* invoke) {
2325 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002326 LocationSummary::kCallOnMainOnly,
Chris Larsencf283da2016-01-19 16:45:35 -08002327 kIntrinsified);
2328 InvokeRuntimeCallingConvention calling_convention;
2329 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2330 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2331 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2332 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
2333 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2334}
2335
2336void IntrinsicCodeGeneratorMIPS::VisitStringNewStringFromChars(HInvoke* invoke) {
Chris Larsencf283da2016-01-19 16:45:35 -08002337 // No need to emit code checking whether `locations->InAt(2)` is a null
2338 // pointer, as callers of the native method
2339 //
2340 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
2341 //
2342 // all include a null check on `data` before calling that method.
Serban Constantinescufca16662016-07-14 09:21:59 +01002343 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Chris Larsencf283da2016-01-19 16:45:35 -08002344}
2345
2346// java.lang.StringFactory.newStringFromString(String toCopy)
2347void IntrinsicLocationsBuilderMIPS::VisitStringNewStringFromString(HInvoke* invoke) {
2348 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00002349 LocationSummary::kCallOnMainAndSlowPath,
Chris Larsencf283da2016-01-19 16:45:35 -08002350 kIntrinsified);
2351 InvokeRuntimeCallingConvention calling_convention;
2352 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2353 Location outLocation = calling_convention.GetReturnLocation(Primitive::kPrimInt);
2354 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2355}
2356
2357void IntrinsicCodeGeneratorMIPS::VisitStringNewStringFromString(HInvoke* invoke) {
2358 MipsAssembler* assembler = GetAssembler();
2359 LocationSummary* locations = invoke->GetLocations();
2360
2361 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
2362 SlowPathCodeMIPS* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS(invoke);
2363 codegen_->AddSlowPath(slow_path);
2364 __ Beqz(string_to_copy, slow_path->GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +01002365 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Chris Larsencf283da2016-01-19 16:45:35 -08002366 __ Bind(slow_path->GetExitLabel());
2367}
2368
Chris Larsen2714fe62016-02-11 14:23:53 -08002369static void GenIsInfinite(LocationSummary* locations,
2370 const Primitive::Type type,
2371 const bool isR6,
2372 MipsAssembler* assembler) {
2373 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
2374 Register out = locations->Out().AsRegister<Register>();
2375
2376 DCHECK(type == Primitive::kPrimFloat || type == Primitive::kPrimDouble);
2377
2378 if (isR6) {
2379 if (type == Primitive::kPrimDouble) {
2380 __ ClassD(FTMP, in);
2381 } else {
2382 __ ClassS(FTMP, in);
2383 }
2384 __ Mfc1(out, FTMP);
2385 __ Andi(out, out, kPositiveInfinity | kNegativeInfinity);
2386 __ Sltu(out, ZERO, out);
2387 } else {
2388 // If one, or more, of the exponent bits is zero, then the number can't be infinite.
2389 if (type == Primitive::kPrimDouble) {
2390 __ MoveFromFpuHigh(TMP, in);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002391 __ LoadConst32(AT, High32Bits(kPositiveInfinityDouble));
Chris Larsen2714fe62016-02-11 14:23:53 -08002392 } else {
2393 __ Mfc1(TMP, in);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002394 __ LoadConst32(AT, kPositiveInfinityFloat);
Chris Larsen2714fe62016-02-11 14:23:53 -08002395 }
2396 __ Xor(TMP, TMP, AT);
2397
2398 __ Sll(TMP, TMP, 1);
2399
2400 if (type == Primitive::kPrimDouble) {
2401 __ Mfc1(AT, in);
2402 __ Or(TMP, TMP, AT);
2403 }
2404 // If any of the significand bits are one, then the number is not infinite.
2405 __ Sltiu(out, TMP, 1);
2406 }
2407}
2408
2409// boolean java.lang.Float.isInfinite(float)
2410void IntrinsicLocationsBuilderMIPS::VisitFloatIsInfinite(HInvoke* invoke) {
2411 CreateFPToIntLocations(arena_, invoke);
2412}
2413
2414void IntrinsicCodeGeneratorMIPS::VisitFloatIsInfinite(HInvoke* invoke) {
2415 GenIsInfinite(invoke->GetLocations(), Primitive::kPrimFloat, IsR6(), GetAssembler());
2416}
2417
2418// boolean java.lang.Double.isInfinite(double)
2419void IntrinsicLocationsBuilderMIPS::VisitDoubleIsInfinite(HInvoke* invoke) {
2420 CreateFPToIntLocations(arena_, invoke);
2421}
2422
2423void IntrinsicCodeGeneratorMIPS::VisitDoubleIsInfinite(HInvoke* invoke) {
2424 GenIsInfinite(invoke->GetLocations(), Primitive::kPrimDouble, IsR6(), GetAssembler());
2425}
2426
Chris Larsen97759342016-02-16 17:10:40 -08002427static void GenHighestOneBit(LocationSummary* locations,
2428 const Primitive::Type type,
2429 bool isR6,
2430 MipsAssembler* assembler) {
2431 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2432
2433 if (type == Primitive::kPrimLong) {
2434 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
2435 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
2436 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
2437 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
2438
2439 if (isR6) {
2440 __ ClzR6(TMP, in_hi);
2441 } else {
2442 __ ClzR2(TMP, in_hi);
2443 }
2444 __ LoadConst32(AT, 0x80000000);
2445 __ Srlv(out_hi, AT, TMP);
2446 __ And(out_hi, out_hi, in_hi);
2447 if (isR6) {
2448 __ ClzR6(TMP, in_lo);
2449 } else {
2450 __ ClzR2(TMP, in_lo);
2451 }
2452 __ Srlv(out_lo, AT, TMP);
2453 __ And(out_lo, out_lo, in_lo);
2454 if (isR6) {
2455 __ Seleqz(out_lo, out_lo, out_hi);
2456 } else {
2457 __ Movn(out_lo, ZERO, out_hi);
2458 }
2459 } else {
2460 Register in = locations->InAt(0).AsRegister<Register>();
2461 Register out = locations->Out().AsRegister<Register>();
2462
2463 if (isR6) {
2464 __ ClzR6(TMP, in);
2465 } else {
2466 __ ClzR2(TMP, in);
2467 }
2468 __ LoadConst32(AT, 0x80000000);
2469 __ Srlv(AT, AT, TMP); // Srlv shifts in the range of [0;31] bits (lower 5 bits of arg).
2470 __ And(out, AT, in); // So this is required for 0 (=shift by 32).
2471 }
2472}
2473
2474// int java.lang.Integer.highestOneBit(int)
2475void IntrinsicLocationsBuilderMIPS::VisitIntegerHighestOneBit(HInvoke* invoke) {
2476 CreateIntToIntLocations(arena_, invoke);
2477}
2478
2479void IntrinsicCodeGeneratorMIPS::VisitIntegerHighestOneBit(HInvoke* invoke) {
2480 GenHighestOneBit(invoke->GetLocations(), Primitive::kPrimInt, IsR6(), GetAssembler());
2481}
2482
2483// long java.lang.Long.highestOneBit(long)
2484void IntrinsicLocationsBuilderMIPS::VisitLongHighestOneBit(HInvoke* invoke) {
2485 CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap);
2486}
2487
2488void IntrinsicCodeGeneratorMIPS::VisitLongHighestOneBit(HInvoke* invoke) {
2489 GenHighestOneBit(invoke->GetLocations(), Primitive::kPrimLong, IsR6(), GetAssembler());
2490}
2491
2492static void GenLowestOneBit(LocationSummary* locations,
2493 const Primitive::Type type,
2494 bool isR6,
2495 MipsAssembler* assembler) {
2496 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2497
2498 if (type == Primitive::kPrimLong) {
2499 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
2500 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
2501 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
2502 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
2503
2504 __ Subu(TMP, ZERO, in_lo);
2505 __ And(out_lo, TMP, in_lo);
2506 __ Subu(TMP, ZERO, in_hi);
2507 __ And(out_hi, TMP, in_hi);
2508 if (isR6) {
2509 __ Seleqz(out_hi, out_hi, out_lo);
2510 } else {
2511 __ Movn(out_hi, ZERO, out_lo);
2512 }
2513 } else {
2514 Register in = locations->InAt(0).AsRegister<Register>();
2515 Register out = locations->Out().AsRegister<Register>();
2516
2517 __ Subu(TMP, ZERO, in);
2518 __ And(out, TMP, in);
2519 }
2520}
2521
2522// int java.lang.Integer.lowestOneBit(int)
2523void IntrinsicLocationsBuilderMIPS::VisitIntegerLowestOneBit(HInvoke* invoke) {
2524 CreateIntToIntLocations(arena_, invoke);
2525}
2526
2527void IntrinsicCodeGeneratorMIPS::VisitIntegerLowestOneBit(HInvoke* invoke) {
2528 GenLowestOneBit(invoke->GetLocations(), Primitive::kPrimInt, IsR6(), GetAssembler());
2529}
2530
2531// long java.lang.Long.lowestOneBit(long)
2532void IntrinsicLocationsBuilderMIPS::VisitLongLowestOneBit(HInvoke* invoke) {
2533 CreateIntToIntLocations(arena_, invoke);
2534}
2535
2536void IntrinsicCodeGeneratorMIPS::VisitLongLowestOneBit(HInvoke* invoke) {
2537 GenLowestOneBit(invoke->GetLocations(), Primitive::kPrimLong, IsR6(), GetAssembler());
2538}
2539
Chris Larsenf09d5322016-04-22 12:06:34 -07002540// int java.lang.Math.round(float)
2541void IntrinsicLocationsBuilderMIPS::VisitMathRoundFloat(HInvoke* invoke) {
2542 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2543 LocationSummary::kNoCall,
2544 kIntrinsified);
2545 locations->SetInAt(0, Location::RequiresFpuRegister());
2546 locations->AddTemp(Location::RequiresFpuRegister());
2547 locations->SetOut(Location::RequiresRegister());
2548}
2549
2550void IntrinsicCodeGeneratorMIPS::VisitMathRoundFloat(HInvoke* invoke) {
2551 LocationSummary* locations = invoke->GetLocations();
2552 MipsAssembler* assembler = GetAssembler();
2553 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
2554 FRegister half = locations->GetTemp(0).AsFpuRegister<FRegister>();
2555 Register out = locations->Out().AsRegister<Register>();
2556
2557 MipsLabel done;
2558 MipsLabel finite;
2559 MipsLabel add;
2560
2561 // if (in.isNaN) {
2562 // return 0;
2563 // }
2564 //
2565 // out = floor.w.s(in);
2566 //
2567 // /*
2568 // * This "if" statement is only needed for the pre-R6 version of floor.w.s
2569 // * which outputs Integer.MAX_VALUE for negative numbers with magnitudes
2570 // * too large to fit in a 32-bit integer.
2571 // *
2572 // * Starting with MIPSR6, which always sets FCSR.NAN2008=1, negative
2573 // * numbers which are too large to be represented in a 32-bit signed
2574 // * integer will be processed by floor.w.s to output Integer.MIN_VALUE,
2575 // * and will no longer be processed by this "if" statement.
2576 // */
2577 // if (out == Integer.MAX_VALUE) {
2578 // TMP = (in < 0.0f) ? 1 : 0;
2579 // /*
2580 // * If TMP is 1, then adding it to out will wrap its value from
2581 // * Integer.MAX_VALUE to Integer.MIN_VALUE.
2582 // */
2583 // return out += TMP;
2584 // }
2585 //
2586 // /*
2587 // * For negative values not handled by the previous "if" statement the
2588 // * test here will correctly set the value of TMP.
2589 // */
2590 // TMP = ((in - out) >= 0.5f) ? 1 : 0;
2591 // return out += TMP;
2592
2593 // Test for NaN.
2594 if (IsR6()) {
2595 __ CmpUnS(FTMP, in, in);
2596 } else {
2597 __ CunS(in, in);
2598 }
2599
2600 // Return zero for NaN.
2601 __ Move(out, ZERO);
2602 if (IsR6()) {
2603 __ Bc1nez(FTMP, &done);
2604 } else {
2605 __ Bc1t(&done);
2606 }
2607
2608 // out = floor(in);
2609 __ FloorWS(FTMP, in);
2610 __ Mfc1(out, FTMP);
2611
Chris Larsen07f712f2016-06-10 16:06:02 -07002612 if (!IsR6()) {
2613 __ LoadConst32(TMP, -1);
2614 }
Chris Larsenf09d5322016-04-22 12:06:34 -07002615
Chris Larsen07f712f2016-06-10 16:06:02 -07002616 // TMP = (out = java.lang.Integer.MAX_VALUE) ? -1 : 0;
Chris Larsenf09d5322016-04-22 12:06:34 -07002617 __ LoadConst32(AT, std::numeric_limits<int32_t>::max());
2618 __ Bne(AT, out, &finite);
2619
2620 __ Mtc1(ZERO, FTMP);
2621 if (IsR6()) {
2622 __ CmpLtS(FTMP, in, FTMP);
Chris Larsen07f712f2016-06-10 16:06:02 -07002623 __ Mfc1(TMP, FTMP);
Chris Larsenf09d5322016-04-22 12:06:34 -07002624 } else {
2625 __ ColtS(in, FTMP);
2626 }
2627
2628 __ B(&add);
2629
2630 __ Bind(&finite);
2631
Chris Larsen07f712f2016-06-10 16:06:02 -07002632 // TMP = (0.5f <= (in - out)) ? -1 : 0;
Chris Larsenf09d5322016-04-22 12:06:34 -07002633 __ Cvtsw(FTMP, FTMP); // Convert output of floor.w.s back to "float".
2634 __ LoadConst32(AT, bit_cast<int32_t, float>(0.5f));
2635 __ SubS(FTMP, in, FTMP);
2636 __ Mtc1(AT, half);
2637 if (IsR6()) {
2638 __ CmpLeS(FTMP, half, FTMP);
Chris Larsen07f712f2016-06-10 16:06:02 -07002639 __ Mfc1(TMP, FTMP);
Chris Larsenf09d5322016-04-22 12:06:34 -07002640 } else {
2641 __ ColeS(half, FTMP);
2642 }
2643
2644 __ Bind(&add);
2645
Chris Larsen07f712f2016-06-10 16:06:02 -07002646 if (!IsR6()) {
Chris Larsenf09d5322016-04-22 12:06:34 -07002647 __ Movf(TMP, ZERO);
2648 }
2649
Chris Larsen07f712f2016-06-10 16:06:02 -07002650 // Return out -= TMP.
2651 __ Subu(out, out, TMP);
Chris Larsenf09d5322016-04-22 12:06:34 -07002652
2653 __ Bind(&done);
2654}
2655
Chris Larsen692235e2016-11-21 16:04:53 -08002656// void java.lang.String.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
2657void IntrinsicLocationsBuilderMIPS::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2658 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Chris Larsenfe4ff442017-03-23 11:25:12 -07002659 LocationSummary::kNoCall,
Chris Larsen692235e2016-11-21 16:04:53 -08002660 kIntrinsified);
2661 locations->SetInAt(0, Location::RequiresRegister());
2662 locations->SetInAt(1, Location::RequiresRegister());
2663 locations->SetInAt(2, Location::RequiresRegister());
2664 locations->SetInAt(3, Location::RequiresRegister());
2665 locations->SetInAt(4, Location::RequiresRegister());
2666
Chris Larsenfe4ff442017-03-23 11:25:12 -07002667 locations->AddTemp(Location::RequiresRegister());
2668 locations->AddTemp(Location::RequiresRegister());
2669 locations->AddTemp(Location::RequiresRegister());
Chris Larsen692235e2016-11-21 16:04:53 -08002670}
2671
2672void IntrinsicCodeGeneratorMIPS::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2673 MipsAssembler* assembler = GetAssembler();
2674 LocationSummary* locations = invoke->GetLocations();
2675
2676 // Check assumption that sizeof(Char) is 2 (used in scaling below).
2677 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
2678 DCHECK_EQ(char_size, 2u);
2679 const size_t char_shift = Primitive::ComponentSizeShift(Primitive::kPrimChar);
2680
2681 Register srcObj = locations->InAt(0).AsRegister<Register>();
2682 Register srcBegin = locations->InAt(1).AsRegister<Register>();
2683 Register srcEnd = locations->InAt(2).AsRegister<Register>();
2684 Register dstObj = locations->InAt(3).AsRegister<Register>();
2685 Register dstBegin = locations->InAt(4).AsRegister<Register>();
2686
2687 Register dstPtr = locations->GetTemp(0).AsRegister<Register>();
Chris Larsen692235e2016-11-21 16:04:53 -08002688 Register srcPtr = locations->GetTemp(1).AsRegister<Register>();
Chris Larsen692235e2016-11-21 16:04:53 -08002689 Register numChrs = locations->GetTemp(2).AsRegister<Register>();
Chris Larsen692235e2016-11-21 16:04:53 -08002690
2691 MipsLabel done;
Chris Larsenfe4ff442017-03-23 11:25:12 -07002692 MipsLabel loop;
Chris Larsen692235e2016-11-21 16:04:53 -08002693
2694 // Location of data in char array buffer.
2695 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
2696
2697 // Get offset of value field within a string object.
2698 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
2699
2700 __ Beq(srcEnd, srcBegin, &done); // No characters to move.
2701
2702 // Calculate number of characters to be copied.
2703 __ Subu(numChrs, srcEnd, srcBegin);
2704
2705 // Calculate destination address.
2706 __ Addiu(dstPtr, dstObj, data_offset);
Chris Larsencd0295d2017-03-31 15:26:54 -07002707 __ ShiftAndAdd(dstPtr, dstBegin, dstPtr, char_shift);
Chris Larsen692235e2016-11-21 16:04:53 -08002708
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002709 if (mirror::kUseStringCompression) {
2710 MipsLabel uncompressed_copy, compressed_loop;
2711 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2712 // Load count field and extract compression flag.
2713 __ LoadFromOffset(kLoadWord, TMP, srcObj, count_offset);
2714 __ Sll(TMP, TMP, 31);
2715
Chris Larsenfe4ff442017-03-23 11:25:12 -07002716 // If string is uncompressed, use uncompressed path.
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002717 __ Bnez(TMP, &uncompressed_copy);
2718
2719 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
2720 __ Addu(srcPtr, srcObj, srcBegin);
2721 __ Bind(&compressed_loop);
2722 __ LoadFromOffset(kLoadUnsignedByte, TMP, srcPtr, value_offset);
2723 __ StoreToOffset(kStoreHalfword, TMP, dstPtr, 0);
2724 __ Addiu(numChrs, numChrs, -1);
2725 __ Addiu(srcPtr, srcPtr, 1);
2726 __ Addiu(dstPtr, dstPtr, 2);
2727 __ Bnez(numChrs, &compressed_loop);
2728
2729 __ B(&done);
2730 __ Bind(&uncompressed_copy);
2731 }
2732
Chris Larsen692235e2016-11-21 16:04:53 -08002733 // Calculate source address.
2734 __ Addiu(srcPtr, srcObj, value_offset);
Chris Larsencd0295d2017-03-31 15:26:54 -07002735 __ ShiftAndAdd(srcPtr, srcBegin, srcPtr, char_shift);
Chris Larsen692235e2016-11-21 16:04:53 -08002736
Chris Larsenfe4ff442017-03-23 11:25:12 -07002737 __ Bind(&loop);
2738 __ Lh(AT, srcPtr, 0);
2739 __ Addiu(numChrs, numChrs, -1);
2740 __ Addiu(srcPtr, srcPtr, char_size);
2741 __ Sh(AT, dstPtr, 0);
2742 __ Addiu(dstPtr, dstPtr, char_size);
2743 __ Bnez(numChrs, &loop);
Chris Larsen692235e2016-11-21 16:04:53 -08002744
2745 __ Bind(&done);
2746}
2747
Chris Larsenb9005fa2017-03-24 12:11:54 -07002748static void CreateFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
2749 LocationSummary* locations = new (arena) LocationSummary(invoke,
2750 LocationSummary::kCallOnMainOnly,
2751 kIntrinsified);
2752 InvokeRuntimeCallingConvention calling_convention;
2753
2754 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2755 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimDouble));
2756}
2757
2758static void CreateFPFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
2759 LocationSummary* locations = new (arena) LocationSummary(invoke,
2760 LocationSummary::kCallOnMainOnly,
2761 kIntrinsified);
2762 InvokeRuntimeCallingConvention calling_convention;
2763
2764 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2765 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2766 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimDouble));
2767}
2768
2769static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorMIPS* codegen, QuickEntrypointEnum entry) {
2770 LocationSummary* locations = invoke->GetLocations();
2771 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
2772 DCHECK_EQ(in, F12);
2773 FRegister out = locations->Out().AsFpuRegister<FRegister>();
2774 DCHECK_EQ(out, F0);
2775
2776 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2777}
2778
2779static void GenFPFPToFPCall(HInvoke* invoke,
2780 CodeGeneratorMIPS* codegen,
2781 QuickEntrypointEnum entry) {
2782 LocationSummary* locations = invoke->GetLocations();
2783 FRegister in0 = locations->InAt(0).AsFpuRegister<FRegister>();
2784 DCHECK_EQ(in0, F12);
2785 FRegister in1 = locations->InAt(1).AsFpuRegister<FRegister>();
2786 DCHECK_EQ(in1, F14);
2787 FRegister out = locations->Out().AsFpuRegister<FRegister>();
2788 DCHECK_EQ(out, F0);
2789
2790 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2791}
2792
2793// static double java.lang.Math.cos(double a)
2794void IntrinsicLocationsBuilderMIPS::VisitMathCos(HInvoke* invoke) {
2795 CreateFPToFPCallLocations(arena_, invoke);
2796}
2797
2798void IntrinsicCodeGeneratorMIPS::VisitMathCos(HInvoke* invoke) {
2799 GenFPToFPCall(invoke, codegen_, kQuickCos);
2800}
2801
2802// static double java.lang.Math.sin(double a)
2803void IntrinsicLocationsBuilderMIPS::VisitMathSin(HInvoke* invoke) {
2804 CreateFPToFPCallLocations(arena_, invoke);
2805}
2806
2807void IntrinsicCodeGeneratorMIPS::VisitMathSin(HInvoke* invoke) {
2808 GenFPToFPCall(invoke, codegen_, kQuickSin);
2809}
2810
2811// static double java.lang.Math.acos(double a)
2812void IntrinsicLocationsBuilderMIPS::VisitMathAcos(HInvoke* invoke) {
2813 CreateFPToFPCallLocations(arena_, invoke);
2814}
2815
2816void IntrinsicCodeGeneratorMIPS::VisitMathAcos(HInvoke* invoke) {
2817 GenFPToFPCall(invoke, codegen_, kQuickAcos);
2818}
2819
2820// static double java.lang.Math.asin(double a)
2821void IntrinsicLocationsBuilderMIPS::VisitMathAsin(HInvoke* invoke) {
2822 CreateFPToFPCallLocations(arena_, invoke);
2823}
2824
2825void IntrinsicCodeGeneratorMIPS::VisitMathAsin(HInvoke* invoke) {
2826 GenFPToFPCall(invoke, codegen_, kQuickAsin);
2827}
2828
2829// static double java.lang.Math.atan(double a)
2830void IntrinsicLocationsBuilderMIPS::VisitMathAtan(HInvoke* invoke) {
2831 CreateFPToFPCallLocations(arena_, invoke);
2832}
2833
2834void IntrinsicCodeGeneratorMIPS::VisitMathAtan(HInvoke* invoke) {
2835 GenFPToFPCall(invoke, codegen_, kQuickAtan);
2836}
2837
2838// static double java.lang.Math.atan2(double y, double x)
2839void IntrinsicLocationsBuilderMIPS::VisitMathAtan2(HInvoke* invoke) {
2840 CreateFPFPToFPCallLocations(arena_, invoke);
2841}
2842
2843void IntrinsicCodeGeneratorMIPS::VisitMathAtan2(HInvoke* invoke) {
2844 GenFPFPToFPCall(invoke, codegen_, kQuickAtan2);
2845}
2846
2847// static double java.lang.Math.cbrt(double a)
2848void IntrinsicLocationsBuilderMIPS::VisitMathCbrt(HInvoke* invoke) {
2849 CreateFPToFPCallLocations(arena_, invoke);
2850}
2851
2852void IntrinsicCodeGeneratorMIPS::VisitMathCbrt(HInvoke* invoke) {
2853 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
2854}
2855
2856// static double java.lang.Math.cosh(double x)
2857void IntrinsicLocationsBuilderMIPS::VisitMathCosh(HInvoke* invoke) {
2858 CreateFPToFPCallLocations(arena_, invoke);
2859}
2860
2861void IntrinsicCodeGeneratorMIPS::VisitMathCosh(HInvoke* invoke) {
2862 GenFPToFPCall(invoke, codegen_, kQuickCosh);
2863}
2864
2865// static double java.lang.Math.exp(double a)
2866void IntrinsicLocationsBuilderMIPS::VisitMathExp(HInvoke* invoke) {
2867 CreateFPToFPCallLocations(arena_, invoke);
2868}
2869
2870void IntrinsicCodeGeneratorMIPS::VisitMathExp(HInvoke* invoke) {
2871 GenFPToFPCall(invoke, codegen_, kQuickExp);
2872}
2873
2874// static double java.lang.Math.expm1(double x)
2875void IntrinsicLocationsBuilderMIPS::VisitMathExpm1(HInvoke* invoke) {
2876 CreateFPToFPCallLocations(arena_, invoke);
2877}
2878
2879void IntrinsicCodeGeneratorMIPS::VisitMathExpm1(HInvoke* invoke) {
2880 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
2881}
2882
2883// static double java.lang.Math.hypot(double x, double y)
2884void IntrinsicLocationsBuilderMIPS::VisitMathHypot(HInvoke* invoke) {
2885 CreateFPFPToFPCallLocations(arena_, invoke);
2886}
2887
2888void IntrinsicCodeGeneratorMIPS::VisitMathHypot(HInvoke* invoke) {
2889 GenFPFPToFPCall(invoke, codegen_, kQuickHypot);
2890}
2891
2892// static double java.lang.Math.log(double a)
2893void IntrinsicLocationsBuilderMIPS::VisitMathLog(HInvoke* invoke) {
2894 CreateFPToFPCallLocations(arena_, invoke);
2895}
2896
2897void IntrinsicCodeGeneratorMIPS::VisitMathLog(HInvoke* invoke) {
2898 GenFPToFPCall(invoke, codegen_, kQuickLog);
2899}
2900
2901// static double java.lang.Math.log10(double x)
2902void IntrinsicLocationsBuilderMIPS::VisitMathLog10(HInvoke* invoke) {
2903 CreateFPToFPCallLocations(arena_, invoke);
2904}
2905
2906void IntrinsicCodeGeneratorMIPS::VisitMathLog10(HInvoke* invoke) {
2907 GenFPToFPCall(invoke, codegen_, kQuickLog10);
2908}
2909
2910// static double java.lang.Math.nextAfter(double start, double direction)
2911void IntrinsicLocationsBuilderMIPS::VisitMathNextAfter(HInvoke* invoke) {
2912 CreateFPFPToFPCallLocations(arena_, invoke);
2913}
2914
2915void IntrinsicCodeGeneratorMIPS::VisitMathNextAfter(HInvoke* invoke) {
2916 GenFPFPToFPCall(invoke, codegen_, kQuickNextAfter);
2917}
2918
2919// static double java.lang.Math.sinh(double x)
2920void IntrinsicLocationsBuilderMIPS::VisitMathSinh(HInvoke* invoke) {
2921 CreateFPToFPCallLocations(arena_, invoke);
2922}
2923
2924void IntrinsicCodeGeneratorMIPS::VisitMathSinh(HInvoke* invoke) {
2925 GenFPToFPCall(invoke, codegen_, kQuickSinh);
2926}
2927
2928// static double java.lang.Math.tan(double a)
2929void IntrinsicLocationsBuilderMIPS::VisitMathTan(HInvoke* invoke) {
2930 CreateFPToFPCallLocations(arena_, invoke);
2931}
2932
2933void IntrinsicCodeGeneratorMIPS::VisitMathTan(HInvoke* invoke) {
2934 GenFPToFPCall(invoke, codegen_, kQuickTan);
2935}
2936
2937// static double java.lang.Math.tanh(double x)
2938void IntrinsicLocationsBuilderMIPS::VisitMathTanh(HInvoke* invoke) {
2939 CreateFPToFPCallLocations(arena_, invoke);
2940}
2941
2942void IntrinsicCodeGeneratorMIPS::VisitMathTanh(HInvoke* invoke) {
2943 GenFPToFPCall(invoke, codegen_, kQuickTanh);
2944}
2945
Chris Larsen2f6ad9d2017-03-23 15:37:03 -07002946// static void java.lang.System.arraycopy(Object src, int srcPos,
2947// Object dest, int destPos,
2948// int length)
2949void IntrinsicLocationsBuilderMIPS::VisitSystemArrayCopyChar(HInvoke* invoke) {
2950 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2951 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2952 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2953
2954 // As long as we are checking, we might as well check to see if the src and dest
2955 // positions are >= 0.
2956 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2957 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
2958 // We will have to fail anyways.
2959 return;
2960 }
2961
2962 // And since we are already checking, check the length too.
2963 if (length != nullptr) {
2964 int32_t len = length->GetValue();
2965 if (len < 0) {
2966 // Just call as normal.
2967 return;
2968 }
2969 }
2970
2971 // Okay, it is safe to generate inline code.
2972 LocationSummary* locations =
2973 new (arena_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
2974 // arraycopy(Object src, int srcPos, Object dest, int destPos, int length).
2975 locations->SetInAt(0, Location::RequiresRegister());
2976 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
2977 locations->SetInAt(2, Location::RequiresRegister());
2978 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
2979 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
2980
2981 locations->AddTemp(Location::RequiresRegister());
2982 locations->AddTemp(Location::RequiresRegister());
2983 locations->AddTemp(Location::RequiresRegister());
2984}
2985
2986// Utility routine to verify that "length(input) - pos >= length"
2987static void EnoughItems(MipsAssembler* assembler,
2988 Register length_input_minus_pos,
2989 Location length,
2990 SlowPathCodeMIPS* slow_path) {
2991 if (length.IsConstant()) {
2992 int32_t length_constant = length.GetConstant()->AsIntConstant()->GetValue();
2993
2994 if (IsInt<16>(length_constant)) {
2995 __ Slti(TMP, length_input_minus_pos, length_constant);
2996 __ Bnez(TMP, slow_path->GetEntryLabel());
2997 } else {
2998 __ LoadConst32(TMP, length_constant);
2999 __ Blt(length_input_minus_pos, TMP, slow_path->GetEntryLabel());
3000 }
3001 } else {
3002 __ Blt(length_input_minus_pos, length.AsRegister<Register>(), slow_path->GetEntryLabel());
3003 }
3004}
3005
3006static void CheckPosition(MipsAssembler* assembler,
3007 Location pos,
3008 Register input,
3009 Location length,
3010 SlowPathCodeMIPS* slow_path,
3011 bool length_is_input_length = false) {
3012 // Where is the length in the Array?
3013 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
3014
3015 // Calculate length(input) - pos.
3016 if (pos.IsConstant()) {
3017 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
3018 if (pos_const == 0) {
3019 if (!length_is_input_length) {
3020 // Check that length(input) >= length.
3021 __ LoadFromOffset(kLoadWord, AT, input, length_offset);
3022 EnoughItems(assembler, AT, length, slow_path);
3023 }
3024 } else {
3025 // Check that (length(input) - pos) >= zero.
3026 __ LoadFromOffset(kLoadWord, AT, input, length_offset);
3027 DCHECK_GT(pos_const, 0);
3028 __ Addiu32(AT, AT, -pos_const, TMP);
3029 __ Bltz(AT, slow_path->GetEntryLabel());
3030
3031 // Verify that (length(input) - pos) >= length.
3032 EnoughItems(assembler, AT, length, slow_path);
3033 }
3034 } else if (length_is_input_length) {
3035 // The only way the copy can succeed is if pos is zero.
3036 Register pos_reg = pos.AsRegister<Register>();
3037 __ Bnez(pos_reg, slow_path->GetEntryLabel());
3038 } else {
3039 // Verify that pos >= 0.
3040 Register pos_reg = pos.AsRegister<Register>();
3041 __ Bltz(pos_reg, slow_path->GetEntryLabel());
3042
3043 // Check that (length(input) - pos) >= zero.
3044 __ LoadFromOffset(kLoadWord, AT, input, length_offset);
3045 __ Subu(AT, AT, pos_reg);
3046 __ Bltz(AT, slow_path->GetEntryLabel());
3047
3048 // Verify that (length(input) - pos) >= length.
3049 EnoughItems(assembler, AT, length, slow_path);
3050 }
3051}
3052
3053void IntrinsicCodeGeneratorMIPS::VisitSystemArrayCopyChar(HInvoke* invoke) {
3054 MipsAssembler* assembler = GetAssembler();
3055 LocationSummary* locations = invoke->GetLocations();
3056
3057 Register src = locations->InAt(0).AsRegister<Register>();
3058 Location src_pos = locations->InAt(1);
3059 Register dest = locations->InAt(2).AsRegister<Register>();
3060 Location dest_pos = locations->InAt(3);
3061 Location length = locations->InAt(4);
3062
3063 MipsLabel loop;
3064
3065 Register dest_base = locations->GetTemp(0).AsRegister<Register>();
3066 Register src_base = locations->GetTemp(1).AsRegister<Register>();
3067 Register count = locations->GetTemp(2).AsRegister<Register>();
3068
3069 SlowPathCodeMIPS* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS(invoke);
3070 codegen_->AddSlowPath(slow_path);
3071
3072 // Bail out if the source and destination are the same (to handle overlap).
3073 __ Beq(src, dest, slow_path->GetEntryLabel());
3074
3075 // Bail out if the source is null.
3076 __ Beqz(src, slow_path->GetEntryLabel());
3077
3078 // Bail out if the destination is null.
3079 __ Beqz(dest, slow_path->GetEntryLabel());
3080
3081 // Load length into register for count.
3082 if (length.IsConstant()) {
3083 __ LoadConst32(count, length.GetConstant()->AsIntConstant()->GetValue());
3084 } else {
3085 // If the length is negative, bail out.
3086 // We have already checked in the LocationsBuilder for the constant case.
3087 __ Bltz(length.AsRegister<Register>(), slow_path->GetEntryLabel());
3088
3089 __ Move(count, length.AsRegister<Register>());
3090 }
3091
3092 // Validity checks: source.
3093 CheckPosition(assembler, src_pos, src, Location::RegisterLocation(count), slow_path);
3094
3095 // Validity checks: dest.
3096 CheckPosition(assembler, dest_pos, dest, Location::RegisterLocation(count), slow_path);
3097
3098 // If count is zero, we're done.
3099 __ Beqz(count, slow_path->GetExitLabel());
3100
3101 // Okay, everything checks out. Finally time to do the copy.
3102 // Check assumption that sizeof(Char) is 2 (used in scaling below).
3103 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
3104 DCHECK_EQ(char_size, 2u);
3105
3106 const size_t char_shift = Primitive::ComponentSizeShift(Primitive::kPrimChar);
3107
3108 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
3109
3110 // Calculate source and destination addresses.
3111 if (src_pos.IsConstant()) {
3112 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
3113
3114 __ Addiu32(src_base, src, data_offset + char_size * src_pos_const, TMP);
3115 } else {
3116 __ Addiu32(src_base, src, data_offset, TMP);
3117 __ ShiftAndAdd(src_base, src_pos.AsRegister<Register>(), src_base, char_shift);
3118 }
3119 if (dest_pos.IsConstant()) {
3120 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
3121
3122 __ Addiu32(dest_base, dest, data_offset + char_size * dest_pos_const, TMP);
3123 } else {
3124 __ Addiu32(dest_base, dest, data_offset, TMP);
3125 __ ShiftAndAdd(dest_base, dest_pos.AsRegister<Register>(), dest_base, char_shift);
3126 }
3127
3128 __ Bind(&loop);
3129 __ Lh(TMP, src_base, 0);
3130 __ Addiu(src_base, src_base, char_size);
3131 __ Addiu(count, count, -1);
3132 __ Sh(TMP, dest_base, 0);
3133 __ Addiu(dest_base, dest_base, char_size);
3134 __ Bnez(count, &loop);
3135
3136 __ Bind(slow_path->GetExitLabel());
3137}
3138
Chris Larsen5633ce72017-04-10 15:47:40 -07003139// long java.lang.Integer.valueOf(long)
3140void IntrinsicLocationsBuilderMIPS::VisitIntegerValueOf(HInvoke* invoke) {
3141 InvokeRuntimeCallingConvention calling_convention;
3142 IntrinsicVisitor::ComputeIntegerValueOfLocations(
3143 invoke,
3144 codegen_,
3145 calling_convention.GetReturnLocation(Primitive::kPrimNot),
3146 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3147}
3148
3149void IntrinsicCodeGeneratorMIPS::VisitIntegerValueOf(HInvoke* invoke) {
3150 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
3151 LocationSummary* locations = invoke->GetLocations();
3152 MipsAssembler* assembler = GetAssembler();
3153 InstructionCodeGeneratorMIPS* icodegen =
3154 down_cast<InstructionCodeGeneratorMIPS*>(codegen_->GetInstructionVisitor());
3155
3156 Register out = locations->Out().AsRegister<Register>();
3157 InvokeRuntimeCallingConvention calling_convention;
3158 if (invoke->InputAt(0)->IsConstant()) {
3159 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
3160 if (value >= info.low && value <= info.high) {
3161 // Just embed the j.l.Integer in the code.
3162 ScopedObjectAccess soa(Thread::Current());
3163 mirror::Object* boxed = info.cache->Get(value + (-info.low));
3164 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
3165 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
3166 __ LoadConst32(out, address);
3167 } else {
3168 // Allocate and initialize a new j.l.Integer.
3169 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
3170 // JIT object table.
3171 uint32_t address =
3172 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3173 __ LoadConst32(calling_convention.GetRegisterAt(0), address);
3174 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3175 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3176 __ StoreConstToOffset(kStoreWord, value, out, info.value_offset, TMP);
3177 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3178 // one.
3179 icodegen->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3180 }
3181 } else {
3182 Register in = locations->InAt(0).AsRegister<Register>();
3183 MipsLabel allocate, done;
3184 int32_t count = static_cast<uint32_t>(info.high) - info.low + 1;
3185
3186 // Is (info.low <= in) && (in <= info.high)?
3187 __ Addiu32(out, in, -info.low);
3188 // As unsigned quantities is out < (info.high - info.low + 1)?
3189 if (IsInt<16>(count)) {
3190 __ Sltiu(AT, out, count);
3191 } else {
3192 __ LoadConst32(AT, count);
3193 __ Sltu(AT, out, AT);
3194 }
3195 // Branch if out >= (info.high - info.low + 1).
3196 // This means that "in" is outside of the range [info.low, info.high].
3197 __ Beqz(AT, &allocate);
3198
3199 // If the value is within the bounds, load the j.l.Integer directly from the array.
3200 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3201 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
3202 __ LoadConst32(TMP, data_offset + address);
3203 __ ShiftAndAdd(out, out, TMP, TIMES_4);
3204 __ Lw(out, out, 0);
3205 __ MaybeUnpoisonHeapReference(out);
3206 __ B(&done);
3207
3208 __ Bind(&allocate);
3209 // Otherwise allocate and initialize a new j.l.Integer.
3210 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3211 __ LoadConst32(calling_convention.GetRegisterAt(0), address);
3212 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3213 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3214 __ StoreToOffset(kStoreWord, in, out, info.value_offset);
3215 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3216 // one.
3217 icodegen->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3218 __ Bind(&done);
3219 }
3220}
3221
Chris Larsen2714fe62016-02-11 14:23:53 -08003222// Unimplemented intrinsics.
3223
Aart Bik2f9fcc92016-03-01 15:16:54 -08003224UNIMPLEMENTED_INTRINSIC(MIPS, MathCeil)
3225UNIMPLEMENTED_INTRINSIC(MIPS, MathFloor)
3226UNIMPLEMENTED_INTRINSIC(MIPS, MathRint)
3227UNIMPLEMENTED_INTRINSIC(MIPS, MathRoundDouble)
Alexey Frunze15958152017-02-09 19:08:30 -08003228UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetLongVolatile);
3229UNIMPLEMENTED_INTRINSIC(MIPS, UnsafePutLongVolatile);
Aart Bik2f9fcc92016-03-01 15:16:54 -08003230UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeCASLong)
Chris Larsen701566a2015-10-27 15:29:13 -07003231
Aart Bik2f9fcc92016-03-01 15:16:54 -08003232UNIMPLEMENTED_INTRINSIC(MIPS, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08003233UNIMPLEMENTED_INTRINSIC(MIPS, SystemArrayCopy)
Aart Bik3f67e692016-01-15 14:35:12 -08003234
Aart Bikff7d89c2016-11-07 08:49:28 -08003235UNIMPLEMENTED_INTRINSIC(MIPS, StringStringIndexOf);
3236UNIMPLEMENTED_INTRINSIC(MIPS, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003237UNIMPLEMENTED_INTRINSIC(MIPS, StringBufferAppend);
3238UNIMPLEMENTED_INTRINSIC(MIPS, StringBufferLength);
3239UNIMPLEMENTED_INTRINSIC(MIPS, StringBufferToString);
3240UNIMPLEMENTED_INTRINSIC(MIPS, StringBuilderAppend);
3241UNIMPLEMENTED_INTRINSIC(MIPS, StringBuilderLength);
3242UNIMPLEMENTED_INTRINSIC(MIPS, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003243
Aart Bik0e54c012016-03-04 12:08:31 -08003244// 1.8.
3245UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndAddInt)
3246UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndAddLong)
3247UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndSetInt)
3248UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndSetLong)
3249UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndSetObject)
Chris Larsen701566a2015-10-27 15:29:13 -07003250
Aart Bik0e54c012016-03-04 12:08:31 -08003251UNREACHABLE_INTRINSICS(MIPS)
Chris Larsen2714fe62016-02-11 14:23:53 -08003252
Chris Larsen701566a2015-10-27 15:29:13 -07003253#undef __
3254
3255} // namespace mips
3256} // namespace art