blob: 98ccce79e4ae54b3a733f1450dbd1090c521ffc5 [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"
Andreas Gampe09659c22017-09-18 18:23:32 -070023#include "heap_poisoning.h"
Chris Larsen701566a2015-10-27 15:29:13 -070024#include "intrinsics.h"
25#include "mirror/array-inl.h"
Andreas Gampe895f9222017-07-05 09:53:32 -070026#include "mirror/object_array-inl.h"
Chris Larsen701566a2015-10-27 15:29:13 -070027#include "mirror/string.h"
Andreas Gampe508fdf32017-06-05 16:42:13 -070028#include "scoped_thread_state_change-inl.h"
Chris Larsen701566a2015-10-27 15:29:13 -070029#include "thread.h"
30#include "utils/mips/assembler_mips.h"
31#include "utils/mips/constants_mips.h"
32
33namespace art {
34
35namespace mips {
36
37IntrinsicLocationsBuilderMIPS::IntrinsicLocationsBuilderMIPS(CodeGeneratorMIPS* codegen)
Vladimir Markoca6fff82017-10-03 14:49:14 +010038 : codegen_(codegen), allocator_(codegen->GetGraph()->GetAllocator()) {
Chris Larsen701566a2015-10-27 15:29:13 -070039}
40
41MipsAssembler* IntrinsicCodeGeneratorMIPS::GetAssembler() {
42 return reinterpret_cast<MipsAssembler*>(codegen_->GetAssembler());
43}
44
45ArenaAllocator* IntrinsicCodeGeneratorMIPS::GetAllocator() {
Vladimir Markoca6fff82017-10-03 14:49:14 +010046 return codegen_->GetGraph()->GetAllocator();
Chris Larsen701566a2015-10-27 15:29:13 -070047}
48
Alexey Frunzebb9863a2016-01-11 15:51:16 -080049inline bool IntrinsicCodeGeneratorMIPS::IsR2OrNewer() const {
Chris Larsene16ce5a2015-11-18 12:30:20 -080050 return codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
51}
52
Alexey Frunzebb9863a2016-01-11 15:51:16 -080053inline bool IntrinsicCodeGeneratorMIPS::IsR6() const {
Chris Larsene16ce5a2015-11-18 12:30:20 -080054 return codegen_->GetInstructionSetFeatures().IsR6();
55}
56
Alexey Frunzebb9863a2016-01-11 15:51:16 -080057inline bool IntrinsicCodeGeneratorMIPS::Is32BitFPU() const {
58 return codegen_->GetInstructionSetFeatures().Is32BitFloatingPoint();
59}
60
Chris Larsen701566a2015-10-27 15:29:13 -070061#define __ codegen->GetAssembler()->
62
63static void MoveFromReturnRegister(Location trg,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010064 DataType::Type type,
Chris Larsen701566a2015-10-27 15:29:13 -070065 CodeGeneratorMIPS* codegen) {
66 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 DCHECK_EQ(type, DataType::Type::kVoid);
Chris Larsen701566a2015-10-27 15:29:13 -070068 return;
69 }
70
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010071 DCHECK_NE(type, DataType::Type::kVoid);
Chris Larsen701566a2015-10-27 15:29:13 -070072
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010073 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
Chris Larsen701566a2015-10-27 15:29:13 -070074 Register trg_reg = trg.AsRegister<Register>();
75 if (trg_reg != V0) {
76 __ Move(V0, trg_reg);
77 }
78 } else {
79 FRegister trg_reg = trg.AsFpuRegister<FRegister>();
80 if (trg_reg != F0) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010081 if (type == DataType::Type::kFloat32) {
Chris Larsen701566a2015-10-27 15:29:13 -070082 __ MovS(F0, trg_reg);
83 } else {
84 __ MovD(F0, trg_reg);
85 }
86 }
87 }
88}
89
90static void MoveArguments(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
91 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
92 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
93}
94
95// Slow-path for fallback (calling the managed code to handle the
96// intrinsic) in an intrinsified call. This will copy the arguments
97// into the positions for a regular call.
98//
99// Note: The actual parameters are required to be in the locations
100// given by the invoke's location summary. If an intrinsic
101// modifies those locations before a slowpath call, they must be
102// restored!
103class IntrinsicSlowPathMIPS : public SlowPathCodeMIPS {
104 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000105 explicit IntrinsicSlowPathMIPS(HInvoke* invoke) : SlowPathCodeMIPS(invoke), invoke_(invoke) { }
Chris Larsen701566a2015-10-27 15:29:13 -0700106
107 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
108 CodeGeneratorMIPS* codegen = down_cast<CodeGeneratorMIPS*>(codegen_in);
109
110 __ Bind(GetEntryLabel());
111
112 SaveLiveRegisters(codegen, invoke_->GetLocations());
113
114 MoveArguments(invoke_, codegen);
115
116 if (invoke_->IsInvokeStaticOrDirect()) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100117 codegen->GenerateStaticOrDirectCall(
118 invoke_->AsInvokeStaticOrDirect(), Location::RegisterLocation(A0), this);
Chris Larsen701566a2015-10-27 15:29:13 -0700119 } else {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100120 codegen->GenerateVirtualCall(
121 invoke_->AsInvokeVirtual(), Location::RegisterLocation(A0), this);
Chris Larsen701566a2015-10-27 15:29:13 -0700122 }
123
124 // Copy the result back to the expected output.
125 Location out = invoke_->GetLocations()->Out();
126 if (out.IsValid()) {
127 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
128 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
129 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
130 }
131
132 RestoreLiveRegisters(codegen, invoke_->GetLocations());
133 __ B(GetExitLabel());
134 }
135
136 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathMIPS"; }
137
138 private:
139 // The instruction where this slow path is happening.
140 HInvoke* const invoke_;
141
142 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathMIPS);
143};
144
145#undef __
146
147bool IntrinsicLocationsBuilderMIPS::TryDispatch(HInvoke* invoke) {
148 Dispatch(invoke);
149 LocationSummary* res = invoke->GetLocations();
150 return res != nullptr && res->Intrinsified();
151}
152
153#define __ assembler->
154
Vladimir Markoca6fff82017-10-03 14:49:14 +0100155static void CreateFPToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
156 LocationSummary* locations =
157 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700158 locations->SetInAt(0, Location::RequiresFpuRegister());
159 locations->SetOut(Location::RequiresRegister());
160}
161
162static void MoveFPToInt(LocationSummary* locations, bool is64bit, MipsAssembler* assembler) {
163 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
164
165 if (is64bit) {
166 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
167 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
168
169 __ Mfc1(out_lo, in);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800170 __ MoveFromFpuHigh(out_hi, in);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700171 } else {
172 Register out = locations->Out().AsRegister<Register>();
173
174 __ Mfc1(out, in);
175 }
176}
177
178// long java.lang.Double.doubleToRawLongBits(double)
179void IntrinsicLocationsBuilderMIPS::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100180 CreateFPToIntLocations(allocator_, invoke);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700181}
182
183void IntrinsicCodeGeneratorMIPS::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000184 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700185}
186
187// int java.lang.Float.floatToRawIntBits(float)
188void IntrinsicLocationsBuilderMIPS::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100189 CreateFPToIntLocations(allocator_, invoke);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700190}
191
192void IntrinsicCodeGeneratorMIPS::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000193 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700194}
195
Vladimir Markoca6fff82017-10-03 14:49:14 +0100196static void CreateIntToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
197 LocationSummary* locations =
198 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700199 locations->SetInAt(0, Location::RequiresRegister());
200 locations->SetOut(Location::RequiresFpuRegister());
201}
202
203static void MoveIntToFP(LocationSummary* locations, bool is64bit, MipsAssembler* assembler) {
204 FRegister out = locations->Out().AsFpuRegister<FRegister>();
205
206 if (is64bit) {
207 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
208 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
209
210 __ Mtc1(in_lo, out);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800211 __ MoveToFpuHigh(in_hi, out);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700212 } else {
213 Register in = locations->InAt(0).AsRegister<Register>();
214
215 __ Mtc1(in, out);
216 }
217}
218
219// double java.lang.Double.longBitsToDouble(long)
220void IntrinsicLocationsBuilderMIPS::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100221 CreateIntToFPLocations(allocator_, invoke);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700222}
223
224void IntrinsicCodeGeneratorMIPS::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000225 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700226}
227
228// float java.lang.Float.intBitsToFloat(int)
229void IntrinsicLocationsBuilderMIPS::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100230 CreateIntToFPLocations(allocator_, invoke);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700231}
232
233void IntrinsicCodeGeneratorMIPS::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000234 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700235}
236
Vladimir Markoca6fff82017-10-03 14:49:14 +0100237static void CreateIntToIntLocations(ArenaAllocator* allocator,
Chris Larsen86829602015-11-18 12:27:52 -0800238 HInvoke* invoke,
239 Location::OutputOverlap overlaps = Location::kNoOutputOverlap) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100240 LocationSummary* locations =
241 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700242 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,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100247 DataType::Type type,
Chris Larsen70014c82015-11-18 12:26:08 -0800248 bool isR2OrNewer,
249 bool isR6,
250 bool reverseBits,
251 MipsAssembler* assembler) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100252 DCHECK(type == DataType::Type::kInt16 ||
253 type == DataType::Type::kInt32 ||
254 type == DataType::Type::kInt64);
255 DCHECK(type != DataType::Type::kInt16 || !reverseBits);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100257 if (type == DataType::Type::kInt16) {
Chris Larsen3f8bf652015-10-28 10:08:56 -0700258 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 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100271 } else if (type == DataType::Type::kInt32) {
Chris Larsen3f8bf652015-10-28 10:08:56 -0700272 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 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100316 } else if (type == DataType::Type::kInt64) {
Chris Larsen3f8bf652015-10-28 10:08:56 -0700317 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) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100402 CreateIntToIntLocations(allocator_, invoke);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700403}
404
405void IntrinsicCodeGeneratorMIPS::VisitIntegerReverseBytes(HInvoke* invoke) {
Chris Larsen70014c82015-11-18 12:26:08 -0800406 GenReverse(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100407 DataType::Type::kInt32,
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) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100416 CreateIntToIntLocations(allocator_, invoke);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700417}
418
419void IntrinsicCodeGeneratorMIPS::VisitLongReverseBytes(HInvoke* invoke) {
Chris Larsen70014c82015-11-18 12:26:08 -0800420 GenReverse(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100421 DataType::Type::kInt64,
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) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100430 CreateIntToIntLocations(allocator_, invoke);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700431}
432
433void IntrinsicCodeGeneratorMIPS::VisitShortReverseBytes(HInvoke* invoke) {
Chris Larsen70014c82015-11-18 12:26:08 -0800434 GenReverse(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100435 DataType::Type::kInt16,
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) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100474 CreateIntToIntLocations(allocator_, invoke);
Chris Larsene3845472015-11-18 12:27:15 -0800475}
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) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100483 CreateIntToIntLocations(allocator_, invoke);
Chris Larsene3845472015-11-18 12:27:15 -0800484}
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) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100561 CreateIntToIntLocations(allocator_, invoke, Location::kOutputOverlap);
Chris Larsen86829602015-11-18 12:27:52 -0800562}
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) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100570 CreateIntToIntLocations(allocator_, invoke, Location::kOutputOverlap);
Chris Larsen86829602015-11-18 12:27:52 -0800571}
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) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100579 CreateIntToIntLocations(allocator_, invoke);
Chris Larsen70014c82015-11-18 12:26:08 -0800580}
581
582void IntrinsicCodeGeneratorMIPS::VisitIntegerReverse(HInvoke* invoke) {
583 GenReverse(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100584 DataType::Type::kInt32,
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) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100593 CreateIntToIntLocations(allocator_, invoke);
Chris Larsen70014c82015-11-18 12:26:08 -0800594}
595
596void IntrinsicCodeGeneratorMIPS::VisitLongReverse(HInvoke* invoke) {
597 GenReverse(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100598 DataType::Type::kInt64,
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
Vladimir Markoca6fff82017-10-03 14:49:14 +0100605static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
606 LocationSummary* locations =
607 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsenb74353a2015-11-20 09:07:09 -0800608 locations->SetInAt(0, Location::RequiresFpuRegister());
609 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
610}
611
Chris Larsenedc16452016-02-12 17:59:00 -0800612static void GenBitCount(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100613 DataType::Type type,
Chris Larsenedc16452016-02-12 17:59:00 -0800614 bool isR6,
615 MipsAssembler* assembler) {
Chris Larsenedc16452016-02-12 17:59:00 -0800616 Register out = locations->Out().AsRegister<Register>();
617
618 // https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
619 //
620 // A generalization of the best bit counting method to integers of
621 // bit-widths up to 128 (parameterized by type T) is this:
622 //
623 // v = v - ((v >> 1) & (T)~(T)0/3); // temp
624 // v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); // temp
625 // v = (v + (v >> 4)) & (T)~(T)0/255*15; // temp
626 // c = (T)(v * ((T)~(T)0/255)) >> (sizeof(T) - 1) * BITS_PER_BYTE; // count
627 //
628 // For comparison, for 32-bit quantities, this algorithm can be executed
629 // using 20 MIPS instructions (the calls to LoadConst32() generate two
630 // machine instructions each for the values being used in this algorithm).
631 // A(n unrolled) loop-based algorithm required 25 instructions.
632 //
633 // For 64-bit quantities, this algorithm gets executed twice, (once
634 // for in_lo, and again for in_hi), but saves a few instructions
635 // because the mask values only have to be loaded once. Using this
Chris Larsen8ca4f972016-04-14 16:16:29 -0700636 // algorithm the count for a 64-bit operand can be performed in 29
Chris Larsenedc16452016-02-12 17:59:00 -0800637 // instructions compared to a loop-based algorithm which required 47
638 // instructions.
639
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100640 if (type == DataType::Type::kInt32) {
Chris Larsenedc16452016-02-12 17:59:00 -0800641 Register in = locations->InAt(0).AsRegister<Register>();
642
643 __ Srl(TMP, in, 1);
644 __ LoadConst32(AT, 0x55555555);
645 __ And(TMP, TMP, AT);
646 __ Subu(TMP, in, TMP);
647 __ LoadConst32(AT, 0x33333333);
648 __ And(out, TMP, AT);
649 __ Srl(TMP, TMP, 2);
650 __ And(TMP, TMP, AT);
651 __ Addu(TMP, out, TMP);
652 __ Srl(out, TMP, 4);
653 __ Addu(out, out, TMP);
654 __ LoadConst32(AT, 0x0F0F0F0F);
655 __ And(out, out, AT);
656 __ LoadConst32(TMP, 0x01010101);
657 if (isR6) {
658 __ MulR6(out, out, TMP);
659 } else {
660 __ MulR2(out, out, TMP);
661 }
662 __ Srl(out, out, 24);
Roland Levillainfa3912e2016-04-01 18:21:55 +0100663 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100664 DCHECK_EQ(type, DataType::Type::kInt64);
Chris Larsenedc16452016-02-12 17:59:00 -0800665 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
666 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
667 Register tmp_hi = locations->GetTemp(0).AsRegister<Register>();
668 Register out_hi = locations->GetTemp(1).AsRegister<Register>();
669 Register tmp_lo = TMP;
670 Register out_lo = out;
671
672 __ Srl(tmp_lo, in_lo, 1);
673 __ Srl(tmp_hi, in_hi, 1);
674
675 __ LoadConst32(AT, 0x55555555);
676
677 __ And(tmp_lo, tmp_lo, AT);
678 __ Subu(tmp_lo, in_lo, tmp_lo);
679
680 __ And(tmp_hi, tmp_hi, AT);
681 __ Subu(tmp_hi, in_hi, tmp_hi);
682
683 __ LoadConst32(AT, 0x33333333);
684
685 __ And(out_lo, tmp_lo, AT);
686 __ Srl(tmp_lo, tmp_lo, 2);
687 __ And(tmp_lo, tmp_lo, AT);
688 __ Addu(tmp_lo, out_lo, tmp_lo);
Chris Larsenedc16452016-02-12 17:59:00 -0800689
690 __ And(out_hi, tmp_hi, AT);
691 __ Srl(tmp_hi, tmp_hi, 2);
692 __ And(tmp_hi, tmp_hi, AT);
693 __ Addu(tmp_hi, out_hi, tmp_hi);
Chris Larsenedc16452016-02-12 17:59:00 -0800694
Chris Larsen8ca4f972016-04-14 16:16:29 -0700695 // Here we deviate from the original algorithm a bit. We've reached
696 // the stage where the bitfields holding the subtotals are large
697 // enough to hold the combined subtotals for both the low word, and
698 // the high word. This means that we can add the subtotals for the
699 // the high, and low words into a single word, and compute the final
700 // result for both the high, and low words using fewer instructions.
Chris Larsenedc16452016-02-12 17:59:00 -0800701 __ LoadConst32(AT, 0x0F0F0F0F);
702
Chris Larsen8ca4f972016-04-14 16:16:29 -0700703 __ Addu(TMP, tmp_hi, tmp_lo);
704
705 __ Srl(out, TMP, 4);
706 __ And(out, out, AT);
707 __ And(TMP, TMP, AT);
708 __ Addu(out, out, TMP);
Chris Larsenedc16452016-02-12 17:59:00 -0800709
710 __ LoadConst32(AT, 0x01010101);
711
712 if (isR6) {
Chris Larsen8ca4f972016-04-14 16:16:29 -0700713 __ MulR6(out, out, AT);
Chris Larsenedc16452016-02-12 17:59:00 -0800714 } else {
Chris Larsen8ca4f972016-04-14 16:16:29 -0700715 __ MulR2(out, out, AT);
Chris Larsenedc16452016-02-12 17:59:00 -0800716 }
717
Chris Larsen8ca4f972016-04-14 16:16:29 -0700718 __ Srl(out, out, 24);
Chris Larsenedc16452016-02-12 17:59:00 -0800719 }
720}
721
722// int java.lang.Integer.bitCount(int)
723void IntrinsicLocationsBuilderMIPS::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100724 CreateIntToIntLocations(allocator_, invoke);
Chris Larsenedc16452016-02-12 17:59:00 -0800725}
726
727void IntrinsicCodeGeneratorMIPS::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100728 GenBitCount(invoke->GetLocations(), DataType::Type::kInt32, IsR6(), GetAssembler());
Chris Larsenedc16452016-02-12 17:59:00 -0800729}
730
731// int java.lang.Long.bitCount(int)
732void IntrinsicLocationsBuilderMIPS::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100733 LocationSummary* locations =
734 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsenedc16452016-02-12 17:59:00 -0800735 locations->SetInAt(0, Location::RequiresRegister());
736 locations->SetOut(Location::RequiresRegister());
737 locations->AddTemp(Location::RequiresRegister());
738 locations->AddTemp(Location::RequiresRegister());
739}
740
741void IntrinsicCodeGeneratorMIPS::VisitLongBitCount(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100742 GenBitCount(invoke->GetLocations(), DataType::Type::kInt64, IsR6(), GetAssembler());
Chris Larsenedc16452016-02-12 17:59:00 -0800743}
744
Goran Jakovljevicb6684652017-01-11 13:42:38 +0100745static void MathAbsFP(LocationSummary* locations,
746 bool is64bit,
747 bool isR2OrNewer,
748 bool isR6,
749 MipsAssembler* assembler) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800750 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
751 FRegister out = locations->Out().AsFpuRegister<FRegister>();
752
Goran Jakovljevic5a6cbfc2017-01-13 12:13:39 +0100753 // Note, as a "quality of implementation", rather than pure "spec compliance", we require that
754 // Math.abs() clears the sign bit (but changes nothing else) for all numbers, including NaN
755 // (signaling NaN may become quiet though).
Goran Jakovljevicb6684652017-01-11 13:42:38 +0100756 //
757 // The ABS.fmt instructions (abs.s and abs.d) do exactly that when NAN2008=1 (R6). For this case,
758 // both regular floating point numbers and NAN values are treated alike, only the sign bit is
759 // affected by this instruction.
760 // But when NAN2008=0 (R2 and before), the ABS.fmt instructions can't be used. For this case, any
761 // NaN operand signals invalid operation. This means that other bits (not just sign bit) might be
762 // changed when doing abs(NaN). Because of that, we clear sign bit in a different way.
763 if (isR6) {
764 if (is64bit) {
765 __ AbsD(out, in);
766 } else {
767 __ AbsS(out, in);
768 }
Chris Larsenb74353a2015-11-20 09:07:09 -0800769 } else {
Goran Jakovljevicb6684652017-01-11 13:42:38 +0100770 if (is64bit) {
771 if (in != out) {
772 __ MovD(out, in);
773 }
774 __ MoveFromFpuHigh(TMP, in);
775 // ins instruction is not available for R1.
776 if (isR2OrNewer) {
777 __ Ins(TMP, ZERO, 31, 1);
778 } else {
779 __ Sll(TMP, TMP, 1);
780 __ Srl(TMP, TMP, 1);
781 }
782 __ MoveToFpuHigh(TMP, out);
783 } else {
784 __ Mfc1(TMP, in);
785 // ins instruction is not available for R1.
786 if (isR2OrNewer) {
787 __ Ins(TMP, ZERO, 31, 1);
788 } else {
789 __ Sll(TMP, TMP, 1);
790 __ Srl(TMP, TMP, 1);
791 }
792 __ Mtc1(TMP, out);
793 }
Chris Larsenb74353a2015-11-20 09:07:09 -0800794 }
795}
796
797// double java.lang.Math.abs(double)
798void IntrinsicLocationsBuilderMIPS::VisitMathAbsDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100799 CreateFPToFPLocations(allocator_, invoke);
Chris Larsenb74353a2015-11-20 09:07:09 -0800800}
801
802void IntrinsicCodeGeneratorMIPS::VisitMathAbsDouble(HInvoke* invoke) {
Goran Jakovljevicb6684652017-01-11 13:42:38 +0100803 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, IsR2OrNewer(), IsR6(), GetAssembler());
Chris Larsenb74353a2015-11-20 09:07:09 -0800804}
805
806// float java.lang.Math.abs(float)
807void IntrinsicLocationsBuilderMIPS::VisitMathAbsFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100808 CreateFPToFPLocations(allocator_, invoke);
Chris Larsenb74353a2015-11-20 09:07:09 -0800809}
810
811void IntrinsicCodeGeneratorMIPS::VisitMathAbsFloat(HInvoke* invoke) {
Goran Jakovljevicb6684652017-01-11 13:42:38 +0100812 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, IsR2OrNewer(), IsR6(), GetAssembler());
Chris Larsenb74353a2015-11-20 09:07:09 -0800813}
814
815static void GenAbsInteger(LocationSummary* locations, bool is64bit, MipsAssembler* assembler) {
816 if (is64bit) {
817 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
818 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
819 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
820 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
821
822 // The comments in this section show the analogous operations which would
823 // be performed if we had 64-bit registers "in", and "out".
824 // __ Dsra32(AT, in, 31);
825 __ Sra(AT, in_hi, 31);
826 // __ Xor(out, in, AT);
827 __ Xor(TMP, in_lo, AT);
828 __ Xor(out_hi, in_hi, AT);
829 // __ Dsubu(out, out, AT);
830 __ Subu(out_lo, TMP, AT);
831 __ Sltu(TMP, out_lo, TMP);
832 __ Addu(out_hi, out_hi, TMP);
833 } else {
834 Register in = locations->InAt(0).AsRegister<Register>();
835 Register out = locations->Out().AsRegister<Register>();
836
837 __ Sra(AT, in, 31);
838 __ Xor(out, in, AT);
839 __ Subu(out, out, AT);
840 }
841}
842
843// int java.lang.Math.abs(int)
844void IntrinsicLocationsBuilderMIPS::VisitMathAbsInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100845 CreateIntToIntLocations(allocator_, invoke);
Chris Larsenb74353a2015-11-20 09:07:09 -0800846}
847
848void IntrinsicCodeGeneratorMIPS::VisitMathAbsInt(HInvoke* invoke) {
849 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
850}
851
852// long java.lang.Math.abs(long)
853void IntrinsicLocationsBuilderMIPS::VisitMathAbsLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100854 CreateIntToIntLocations(allocator_, invoke);
Chris Larsenb74353a2015-11-20 09:07:09 -0800855}
856
857void IntrinsicCodeGeneratorMIPS::VisitMathAbsLong(HInvoke* invoke) {
858 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
859}
860
861static void GenMinMaxFP(LocationSummary* locations,
862 bool is_min,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100863 DataType::Type type,
Chris Larsenb74353a2015-11-20 09:07:09 -0800864 bool is_R6,
865 MipsAssembler* assembler) {
866 FRegister out = locations->Out().AsFpuRegister<FRegister>();
867 FRegister a = locations->InAt(0).AsFpuRegister<FRegister>();
868 FRegister b = locations->InAt(1).AsFpuRegister<FRegister>();
869
870 if (is_R6) {
871 MipsLabel noNaNs;
872 MipsLabel done;
873 FRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
874
875 // When Java computes min/max it prefers a NaN to a number; the
876 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
877 // the inputs is a NaN and the other is a valid number, the MIPS
878 // instruction will return the number; Java wants the NaN value
879 // returned. This is why there is extra logic preceding the use of
880 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
881 // NaN, return the NaN, otherwise return the min/max.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100882 if (type == DataType::Type::kFloat64) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800883 __ CmpUnD(FTMP, a, b);
884 __ Bc1eqz(FTMP, &noNaNs);
885
886 // One of the inputs is a NaN
887 __ CmpEqD(ftmp, a, a);
888 // If a == a then b is the NaN, otherwise a is the NaN.
889 __ SelD(ftmp, a, b);
890
891 if (ftmp != out) {
892 __ MovD(out, ftmp);
893 }
894
895 __ B(&done);
896
897 __ Bind(&noNaNs);
898
899 if (is_min) {
900 __ MinD(out, a, b);
901 } else {
902 __ MaxD(out, a, b);
903 }
904 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100905 DCHECK_EQ(type, DataType::Type::kFloat32);
Chris Larsenb74353a2015-11-20 09:07:09 -0800906 __ CmpUnS(FTMP, a, b);
907 __ Bc1eqz(FTMP, &noNaNs);
908
909 // One of the inputs is a NaN
910 __ CmpEqS(ftmp, a, a);
911 // If a == a then b is the NaN, otherwise a is the NaN.
912 __ SelS(ftmp, a, b);
913
914 if (ftmp != out) {
915 __ MovS(out, ftmp);
916 }
917
918 __ B(&done);
919
920 __ Bind(&noNaNs);
921
922 if (is_min) {
923 __ MinS(out, a, b);
924 } else {
925 __ MaxS(out, a, b);
926 }
927 }
928
929 __ Bind(&done);
930 } else {
931 MipsLabel ordered;
932 MipsLabel compare;
933 MipsLabel select;
934 MipsLabel done;
935
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100936 if (type == DataType::Type::kFloat64) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800937 __ CunD(a, b);
938 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100939 DCHECK_EQ(type, DataType::Type::kFloat32);
Chris Larsenb74353a2015-11-20 09:07:09 -0800940 __ CunS(a, b);
941 }
942 __ Bc1f(&ordered);
943
944 // a or b (or both) is a NaN. Return one, which is a NaN.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100945 if (type == DataType::Type::kFloat64) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800946 __ CeqD(b, b);
947 } else {
948 __ CeqS(b, b);
949 }
950 __ B(&select);
951
952 __ Bind(&ordered);
953
954 // Neither is a NaN.
955 // a == b? (-0.0 compares equal with +0.0)
956 // If equal, handle zeroes, else compare further.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100957 if (type == DataType::Type::kFloat64) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800958 __ CeqD(a, b);
959 } else {
960 __ CeqS(a, b);
961 }
962 __ Bc1f(&compare);
963
964 // a == b either bit for bit or one is -0.0 and the other is +0.0.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100965 if (type == DataType::Type::kFloat64) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800966 __ MoveFromFpuHigh(TMP, a);
967 __ MoveFromFpuHigh(AT, b);
968 } else {
969 __ Mfc1(TMP, a);
970 __ Mfc1(AT, b);
971 }
972
973 if (is_min) {
974 // -0.0 prevails over +0.0.
975 __ Or(TMP, TMP, AT);
976 } else {
977 // +0.0 prevails over -0.0.
978 __ And(TMP, TMP, AT);
979 }
980
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100981 if (type == DataType::Type::kFloat64) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800982 __ Mfc1(AT, a);
983 __ Mtc1(AT, out);
984 __ MoveToFpuHigh(TMP, out);
985 } else {
986 __ Mtc1(TMP, out);
987 }
988 __ B(&done);
989
990 __ Bind(&compare);
991
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100992 if (type == DataType::Type::kFloat64) {
Chris Larsenb74353a2015-11-20 09:07:09 -0800993 if (is_min) {
994 // return (a <= b) ? a : b;
995 __ ColeD(a, b);
996 } else {
997 // return (a >= b) ? a : b;
998 __ ColeD(b, a); // b <= a
999 }
1000 } else {
1001 if (is_min) {
1002 // return (a <= b) ? a : b;
1003 __ ColeS(a, b);
1004 } else {
1005 // return (a >= b) ? a : b;
1006 __ ColeS(b, a); // b <= a
1007 }
1008 }
1009
1010 __ Bind(&select);
1011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001012 if (type == DataType::Type::kFloat64) {
Chris Larsenb74353a2015-11-20 09:07:09 -08001013 __ MovtD(out, a);
1014 __ MovfD(out, b);
1015 } else {
1016 __ MovtS(out, a);
1017 __ MovfS(out, b);
1018 }
1019
1020 __ Bind(&done);
1021 }
1022}
1023
Vladimir Markoca6fff82017-10-03 14:49:14 +01001024static void CreateFPFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
1025 LocationSummary* locations =
1026 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsenb74353a2015-11-20 09:07:09 -08001027 locations->SetInAt(0, Location::RequiresFpuRegister());
1028 locations->SetInAt(1, Location::RequiresFpuRegister());
1029 locations->SetOut(Location::RequiresFpuRegister(), Location::kOutputOverlap);
1030}
1031
1032// double java.lang.Math.min(double, double)
1033void IntrinsicLocationsBuilderMIPS::VisitMathMinDoubleDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001034 CreateFPFPToFPLocations(allocator_, invoke);
Chris Larsenb74353a2015-11-20 09:07:09 -08001035}
1036
1037void IntrinsicCodeGeneratorMIPS::VisitMathMinDoubleDouble(HInvoke* invoke) {
1038 GenMinMaxFP(invoke->GetLocations(),
1039 /* is_min */ true,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001040 DataType::Type::kFloat64,
Chris Larsenb74353a2015-11-20 09:07:09 -08001041 IsR6(),
1042 GetAssembler());
1043}
1044
1045// float java.lang.Math.min(float, float)
1046void IntrinsicLocationsBuilderMIPS::VisitMathMinFloatFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001047 CreateFPFPToFPLocations(allocator_, invoke);
Chris Larsenb74353a2015-11-20 09:07:09 -08001048}
1049
1050void IntrinsicCodeGeneratorMIPS::VisitMathMinFloatFloat(HInvoke* invoke) {
1051 GenMinMaxFP(invoke->GetLocations(),
1052 /* is_min */ true,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001053 DataType::Type::kFloat32,
Chris Larsenb74353a2015-11-20 09:07:09 -08001054 IsR6(),
1055 GetAssembler());
1056}
1057
1058// double java.lang.Math.max(double, double)
1059void IntrinsicLocationsBuilderMIPS::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001060 CreateFPFPToFPLocations(allocator_, invoke);
Chris Larsenb74353a2015-11-20 09:07:09 -08001061}
1062
1063void IntrinsicCodeGeneratorMIPS::VisitMathMaxDoubleDouble(HInvoke* invoke) {
1064 GenMinMaxFP(invoke->GetLocations(),
1065 /* is_min */ false,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001066 DataType::Type::kFloat64,
Chris Larsenb74353a2015-11-20 09:07:09 -08001067 IsR6(),
1068 GetAssembler());
1069}
1070
1071// float java.lang.Math.max(float, float)
1072void IntrinsicLocationsBuilderMIPS::VisitMathMaxFloatFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001073 CreateFPFPToFPLocations(allocator_, invoke);
Chris Larsenb74353a2015-11-20 09:07:09 -08001074}
1075
1076void IntrinsicCodeGeneratorMIPS::VisitMathMaxFloatFloat(HInvoke* invoke) {
1077 GenMinMaxFP(invoke->GetLocations(),
1078 /* is_min */ false,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001079 DataType::Type::kFloat32,
Chris Larsenb74353a2015-11-20 09:07:09 -08001080 IsR6(),
1081 GetAssembler());
1082}
1083
Vladimir Markoca6fff82017-10-03 14:49:14 +01001084static void CreateIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
1085 LocationSummary* locations =
1086 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsenb74353a2015-11-20 09:07:09 -08001087 locations->SetInAt(0, Location::RequiresRegister());
1088 locations->SetInAt(1, Location::RequiresRegister());
1089 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1090}
1091
1092static void GenMinMax(LocationSummary* locations,
1093 bool is_min,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001094 DataType::Type type,
Chris Larsenb74353a2015-11-20 09:07:09 -08001095 bool is_R6,
1096 MipsAssembler* assembler) {
1097 if (is_R6) {
1098 // Some architectures, such as ARM and MIPS (prior to r6), have a
1099 // conditional move instruction which only changes the target
1100 // (output) register if the condition is true (MIPS prior to r6 had
1101 // MOVF, MOVT, MOVN, and MOVZ). The SELEQZ and SELNEZ instructions
1102 // always change the target (output) register. If the condition is
1103 // true the output register gets the contents of the "rs" register;
1104 // otherwise, the output register is set to zero. One consequence
1105 // of this is that to implement something like "rd = c==0 ? rs : rt"
1106 // MIPS64r6 needs to use a pair of SELEQZ/SELNEZ instructions.
1107 // After executing this pair of instructions one of the output
1108 // registers from the pair will necessarily contain zero. Then the
1109 // code ORs the output registers from the SELEQZ/SELNEZ instructions
1110 // to get the final result.
1111 //
1112 // The initial test to see if the output register is same as the
1113 // first input register is needed to make sure that value in the
1114 // first input register isn't clobbered before we've finished
1115 // computing the output value. The logic in the corresponding else
1116 // clause performs the same task but makes sure the second input
1117 // register isn't clobbered in the event that it's the same register
1118 // as the output register; the else clause also handles the case
1119 // where the output register is distinct from both the first, and the
1120 // second input registers.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001121 if (type == DataType::Type::kInt64) {
Chris Larsenb74353a2015-11-20 09:07:09 -08001122 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
1123 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
1124 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
1125 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
1126 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
1127 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
1128
1129 MipsLabel compare_done;
1130
1131 if (a_lo == b_lo) {
1132 if (out_lo != a_lo) {
1133 __ Move(out_lo, a_lo);
1134 __ Move(out_hi, a_hi);
1135 }
1136 } else {
1137 __ Slt(TMP, b_hi, a_hi);
1138 __ Bne(b_hi, a_hi, &compare_done);
1139
1140 __ Sltu(TMP, b_lo, a_lo);
1141
1142 __ Bind(&compare_done);
1143
1144 if (is_min) {
1145 __ Seleqz(AT, a_lo, TMP);
1146 __ Selnez(out_lo, b_lo, TMP); // Safe even if out_lo == a_lo/b_lo
1147 // because at this point we're
1148 // done using a_lo/b_lo.
1149 } else {
1150 __ Selnez(AT, a_lo, TMP);
1151 __ Seleqz(out_lo, b_lo, TMP); // ditto
1152 }
1153 __ Or(out_lo, out_lo, AT);
1154 if (is_min) {
1155 __ Seleqz(AT, a_hi, TMP);
1156 __ Selnez(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
1157 } else {
1158 __ Selnez(AT, a_hi, TMP);
1159 __ Seleqz(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
1160 }
1161 __ Or(out_hi, out_hi, AT);
1162 }
1163 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001164 DCHECK_EQ(type, DataType::Type::kInt32);
Chris Larsenb74353a2015-11-20 09:07:09 -08001165 Register a = locations->InAt(0).AsRegister<Register>();
1166 Register b = locations->InAt(1).AsRegister<Register>();
1167 Register out = locations->Out().AsRegister<Register>();
1168
1169 if (a == b) {
1170 if (out != a) {
1171 __ Move(out, a);
1172 }
1173 } else {
1174 __ Slt(AT, b, a);
1175 if (is_min) {
1176 __ Seleqz(TMP, a, AT);
1177 __ Selnez(AT, b, AT);
1178 } else {
1179 __ Selnez(TMP, a, AT);
1180 __ Seleqz(AT, b, AT);
1181 }
1182 __ Or(out, TMP, AT);
1183 }
1184 }
1185 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001186 if (type == DataType::Type::kInt64) {
Chris Larsenb74353a2015-11-20 09:07:09 -08001187 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
1188 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
1189 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
1190 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
1191 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
1192 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
1193
1194 MipsLabel compare_done;
1195
1196 if (a_lo == b_lo) {
1197 if (out_lo != a_lo) {
1198 __ Move(out_lo, a_lo);
1199 __ Move(out_hi, a_hi);
1200 }
1201 } else {
1202 __ Slt(TMP, a_hi, b_hi);
1203 __ Bne(a_hi, b_hi, &compare_done);
1204
1205 __ Sltu(TMP, a_lo, b_lo);
1206
1207 __ Bind(&compare_done);
1208
1209 if (is_min) {
1210 if (out_lo != a_lo) {
1211 __ Movn(out_hi, a_hi, TMP);
1212 __ Movn(out_lo, a_lo, TMP);
1213 }
1214 if (out_lo != b_lo) {
1215 __ Movz(out_hi, b_hi, TMP);
1216 __ Movz(out_lo, b_lo, TMP);
1217 }
1218 } else {
1219 if (out_lo != a_lo) {
1220 __ Movz(out_hi, a_hi, TMP);
1221 __ Movz(out_lo, a_lo, TMP);
1222 }
1223 if (out_lo != b_lo) {
1224 __ Movn(out_hi, b_hi, TMP);
1225 __ Movn(out_lo, b_lo, TMP);
1226 }
1227 }
1228 }
1229 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001230 DCHECK_EQ(type, DataType::Type::kInt32);
Chris Larsenb74353a2015-11-20 09:07:09 -08001231 Register a = locations->InAt(0).AsRegister<Register>();
1232 Register b = locations->InAt(1).AsRegister<Register>();
1233 Register out = locations->Out().AsRegister<Register>();
1234
1235 if (a == b) {
1236 if (out != a) {
1237 __ Move(out, a);
1238 }
1239 } else {
1240 __ Slt(AT, a, b);
1241 if (is_min) {
1242 if (out != a) {
1243 __ Movn(out, a, AT);
1244 }
1245 if (out != b) {
1246 __ Movz(out, b, AT);
1247 }
1248 } else {
1249 if (out != a) {
1250 __ Movz(out, a, AT);
1251 }
1252 if (out != b) {
1253 __ Movn(out, b, AT);
1254 }
1255 }
1256 }
1257 }
1258 }
1259}
1260
1261// int java.lang.Math.min(int, int)
1262void IntrinsicLocationsBuilderMIPS::VisitMathMinIntInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001263 CreateIntIntToIntLocations(allocator_, invoke);
Chris Larsenb74353a2015-11-20 09:07:09 -08001264}
1265
1266void IntrinsicCodeGeneratorMIPS::VisitMathMinIntInt(HInvoke* invoke) {
1267 GenMinMax(invoke->GetLocations(),
1268 /* is_min */ true,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001269 DataType::Type::kInt32,
Chris Larsenb74353a2015-11-20 09:07:09 -08001270 IsR6(),
1271 GetAssembler());
1272}
1273
1274// long java.lang.Math.min(long, long)
1275void IntrinsicLocationsBuilderMIPS::VisitMathMinLongLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001276 CreateIntIntToIntLocations(allocator_, invoke);
Chris Larsenb74353a2015-11-20 09:07:09 -08001277}
1278
1279void IntrinsicCodeGeneratorMIPS::VisitMathMinLongLong(HInvoke* invoke) {
1280 GenMinMax(invoke->GetLocations(),
1281 /* is_min */ true,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001282 DataType::Type::kInt64,
Chris Larsenb74353a2015-11-20 09:07:09 -08001283 IsR6(),
1284 GetAssembler());
1285}
1286
1287// int java.lang.Math.max(int, int)
1288void IntrinsicLocationsBuilderMIPS::VisitMathMaxIntInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001289 CreateIntIntToIntLocations(allocator_, invoke);
Chris Larsenb74353a2015-11-20 09:07:09 -08001290}
1291
1292void IntrinsicCodeGeneratorMIPS::VisitMathMaxIntInt(HInvoke* invoke) {
1293 GenMinMax(invoke->GetLocations(),
1294 /* is_min */ false,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001295 DataType::Type::kInt32,
Chris Larsenb74353a2015-11-20 09:07:09 -08001296 IsR6(),
1297 GetAssembler());
1298}
1299
1300// long java.lang.Math.max(long, long)
1301void IntrinsicLocationsBuilderMIPS::VisitMathMaxLongLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001302 CreateIntIntToIntLocations(allocator_, invoke);
Chris Larsenb74353a2015-11-20 09:07:09 -08001303}
1304
1305void IntrinsicCodeGeneratorMIPS::VisitMathMaxLongLong(HInvoke* invoke) {
1306 GenMinMax(invoke->GetLocations(),
1307 /* is_min */ false,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001308 DataType::Type::kInt64,
Chris Larsenb74353a2015-11-20 09:07:09 -08001309 IsR6(),
1310 GetAssembler());
1311}
1312
1313// double java.lang.Math.sqrt(double)
1314void IntrinsicLocationsBuilderMIPS::VisitMathSqrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001315 CreateFPToFPLocations(allocator_, invoke);
Chris Larsenb74353a2015-11-20 09:07:09 -08001316}
1317
1318void IntrinsicCodeGeneratorMIPS::VisitMathSqrt(HInvoke* invoke) {
1319 LocationSummary* locations = invoke->GetLocations();
1320 MipsAssembler* assembler = GetAssembler();
1321 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
1322 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1323
1324 __ SqrtD(out, in);
1325}
1326
Chris Larsen3acee732015-11-18 13:31:08 -08001327// byte libcore.io.Memory.peekByte(long address)
1328void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001329 CreateIntToIntLocations(allocator_, invoke);
Chris Larsen3acee732015-11-18 13:31:08 -08001330}
1331
1332void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekByte(HInvoke* invoke) {
1333 MipsAssembler* assembler = GetAssembler();
1334 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1335 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
1336
1337 __ Lb(out, adr, 0);
1338}
1339
1340// short libcore.io.Memory.peekShort(long address)
1341void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001342 CreateIntToIntLocations(allocator_, invoke);
Chris Larsen3acee732015-11-18 13:31:08 -08001343}
1344
1345void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekShortNative(HInvoke* invoke) {
1346 MipsAssembler* assembler = GetAssembler();
1347 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1348 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
1349
1350 if (IsR6()) {
1351 __ Lh(out, adr, 0);
1352 } else if (IsR2OrNewer()) {
1353 // Unlike for words, there are no lhl/lhr instructions to load
1354 // unaligned halfwords so the code loads individual bytes, in case
1355 // the address isn't halfword-aligned, and assembles them into a
1356 // signed halfword.
1357 __ Lb(AT, adr, 1); // This byte must be sign-extended.
1358 __ Lb(out, adr, 0); // This byte can be either sign-extended, or
1359 // zero-extended because the following
1360 // instruction overwrites the sign bits.
1361 __ Ins(out, AT, 8, 24);
1362 } else {
1363 __ Lbu(AT, adr, 0); // This byte must be zero-extended. If it's not
1364 // the "or" instruction below will destroy the upper
1365 // 24 bits of the final result.
1366 __ Lb(out, adr, 1); // This byte must be sign-extended.
1367 __ Sll(out, out, 8);
1368 __ Or(out, out, AT);
1369 }
1370}
1371
1372// int libcore.io.Memory.peekInt(long address)
1373void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001374 CreateIntToIntLocations(allocator_, invoke, Location::kOutputOverlap);
Chris Larsen3acee732015-11-18 13:31:08 -08001375}
1376
1377void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekIntNative(HInvoke* invoke) {
1378 MipsAssembler* assembler = GetAssembler();
1379 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1380 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
1381
1382 if (IsR6()) {
1383 __ Lw(out, adr, 0);
1384 } else {
1385 __ Lwr(out, adr, 0);
1386 __ Lwl(out, adr, 3);
1387 }
1388}
1389
1390// long libcore.io.Memory.peekLong(long address)
1391void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001392 CreateIntToIntLocations(allocator_, invoke, Location::kOutputOverlap);
Chris Larsen3acee732015-11-18 13:31:08 -08001393}
1394
1395void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekLongNative(HInvoke* invoke) {
1396 MipsAssembler* assembler = GetAssembler();
1397 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1398 Register out_lo = invoke->GetLocations()->Out().AsRegisterPairLow<Register>();
1399 Register out_hi = invoke->GetLocations()->Out().AsRegisterPairHigh<Register>();
1400
1401 if (IsR6()) {
1402 __ Lw(out_lo, adr, 0);
1403 __ Lw(out_hi, adr, 4);
1404 } else {
1405 __ Lwr(out_lo, adr, 0);
1406 __ Lwl(out_lo, adr, 3);
1407 __ Lwr(out_hi, adr, 4);
1408 __ Lwl(out_hi, adr, 7);
1409 }
1410}
1411
Vladimir Markoca6fff82017-10-03 14:49:14 +01001412static void CreateIntIntToVoidLocations(ArenaAllocator* allocator, HInvoke* invoke) {
1413 LocationSummary* locations =
1414 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsen3acee732015-11-18 13:31:08 -08001415 locations->SetInAt(0, Location::RequiresRegister());
1416 locations->SetInAt(1, Location::RequiresRegister());
1417}
1418
1419// void libcore.io.Memory.pokeByte(long address, byte value)
1420void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001421 CreateIntIntToVoidLocations(allocator_, invoke);
Chris Larsen3acee732015-11-18 13:31:08 -08001422}
1423
1424void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeByte(HInvoke* invoke) {
1425 MipsAssembler* assembler = GetAssembler();
1426 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1427 Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>();
1428
1429 __ Sb(val, adr, 0);
1430}
1431
1432// void libcore.io.Memory.pokeShort(long address, short value)
1433void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001434 CreateIntIntToVoidLocations(allocator_, invoke);
Chris Larsen3acee732015-11-18 13:31:08 -08001435}
1436
1437void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeShortNative(HInvoke* invoke) {
1438 MipsAssembler* assembler = GetAssembler();
1439 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1440 Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>();
1441
1442 if (IsR6()) {
1443 __ Sh(val, adr, 0);
1444 } else {
1445 // Unlike for words, there are no shl/shr instructions to store
1446 // unaligned halfwords so the code stores individual bytes, in case
1447 // the address isn't halfword-aligned.
1448 __ Sb(val, adr, 0);
1449 __ Srl(AT, val, 8);
1450 __ Sb(AT, adr, 1);
1451 }
1452}
1453
1454// void libcore.io.Memory.pokeInt(long address, int value)
1455void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001456 CreateIntIntToVoidLocations(allocator_, invoke);
Chris Larsen3acee732015-11-18 13:31:08 -08001457}
1458
1459void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeIntNative(HInvoke* invoke) {
1460 MipsAssembler* assembler = GetAssembler();
1461 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1462 Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>();
1463
1464 if (IsR6()) {
1465 __ Sw(val, adr, 0);
1466 } else {
1467 __ Swr(val, adr, 0);
1468 __ Swl(val, adr, 3);
1469 }
1470}
1471
1472// void libcore.io.Memory.pokeLong(long address, long value)
1473void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001474 CreateIntIntToVoidLocations(allocator_, invoke);
Chris Larsen3acee732015-11-18 13:31:08 -08001475}
1476
1477void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeLongNative(HInvoke* invoke) {
1478 MipsAssembler* assembler = GetAssembler();
1479 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
1480 Register val_lo = invoke->GetLocations()->InAt(1).AsRegisterPairLow<Register>();
1481 Register val_hi = invoke->GetLocations()->InAt(1).AsRegisterPairHigh<Register>();
1482
1483 if (IsR6()) {
1484 __ Sw(val_lo, adr, 0);
1485 __ Sw(val_hi, adr, 4);
1486 } else {
1487 __ Swr(val_lo, adr, 0);
1488 __ Swl(val_lo, adr, 3);
1489 __ Swr(val_hi, adr, 4);
1490 __ Swl(val_hi, adr, 7);
1491 }
1492}
1493
Chris Larsencf283da2016-01-19 16:45:35 -08001494// Thread java.lang.Thread.currentThread()
1495void IntrinsicLocationsBuilderMIPS::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001496 LocationSummary* locations =
1497 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsencf283da2016-01-19 16:45:35 -08001498 locations->SetOut(Location::RequiresRegister());
1499}
1500
1501void IntrinsicCodeGeneratorMIPS::VisitThreadCurrentThread(HInvoke* invoke) {
1502 MipsAssembler* assembler = GetAssembler();
1503 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
1504
1505 __ LoadFromOffset(kLoadWord,
1506 out,
1507 TR,
1508 Thread::PeerOffset<kMipsPointerSize>().Int32Value());
1509}
1510
Vladimir Markoca6fff82017-10-03 14:49:14 +01001511static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator,
Alexey Frunze15958152017-02-09 19:08:30 -08001512 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001513 DataType::Type type) {
Alexey Frunze15958152017-02-09 19:08:30 -08001514 bool can_call = kEmitCompilerReadBarrier &&
1515 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1516 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001517 LocationSummary* locations =
1518 new (allocator) LocationSummary(invoke,
1519 can_call
1520 ? LocationSummary::kCallOnSlowPath
1521 : LocationSummary::kNoCall,
1522 kIntrinsified);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001523 if (can_call && kUseBakerReadBarrier) {
1524 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1525 }
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001526 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1527 locations->SetInAt(1, Location::RequiresRegister());
1528 locations->SetInAt(2, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08001529 locations->SetOut(Location::RequiresRegister(),
1530 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001531 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze15958152017-02-09 19:08:30 -08001532 // We need a temporary register for the read barrier marking slow
1533 // path in InstructionCodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier.
1534 locations->AddTemp(Location::RequiresRegister());
1535 }
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001536}
1537
Alexey Frunze15958152017-02-09 19:08:30 -08001538// Note that the caller must supply a properly aligned memory address.
1539// If they do not, the behavior is undefined (atomicity not guaranteed, exception may occur).
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001540static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001541 DataType::Type type,
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001542 bool is_volatile,
1543 bool is_R6,
1544 CodeGeneratorMIPS* codegen) {
1545 LocationSummary* locations = invoke->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001546 DCHECK((type == DataType::Type::kInt32) ||
1547 (type == DataType::Type::kInt64) ||
1548 (type == DataType::Type::kReference)) << type;
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001549 MipsAssembler* assembler = codegen->GetAssembler();
Alexey Frunze15958152017-02-09 19:08:30 -08001550 // Target register.
1551 Location trg_loc = locations->Out();
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001552 // Object pointer.
Alexey Frunze15958152017-02-09 19:08:30 -08001553 Location base_loc = locations->InAt(1);
1554 Register base = base_loc.AsRegister<Register>();
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001555 // The "offset" argument is passed as a "long". Since this code is for
1556 // a 32-bit processor, we can only use 32-bit addresses, so we only
1557 // need the low 32-bits of offset.
Alexey Frunze15958152017-02-09 19:08:30 -08001558 Location offset_loc = locations->InAt(2);
1559 Register offset_lo = offset_loc.AsRegisterPairLow<Register>();
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001560
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001561 if (!(kEmitCompilerReadBarrier && kUseBakerReadBarrier && (type == DataType::Type::kReference))) {
Alexey Frunze15958152017-02-09 19:08:30 -08001562 __ Addu(TMP, base, offset_lo);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001563 }
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001564
Alexey Frunze15958152017-02-09 19:08:30 -08001565 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001566 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08001567 Register trg_lo = trg_loc.AsRegisterPairLow<Register>();
1568 Register trg_hi = trg_loc.AsRegisterPairHigh<Register>();
1569 CHECK(!is_volatile); // TODO: support atomic 8-byte volatile loads.
1570 if (is_R6) {
1571 __ Lw(trg_lo, TMP, 0);
1572 __ Lw(trg_hi, TMP, 4);
1573 } else {
1574 __ Lwr(trg_lo, TMP, 0);
1575 __ Lwl(trg_lo, TMP, 3);
1576 __ Lwr(trg_hi, TMP, 4);
1577 __ Lwl(trg_hi, TMP, 7);
1578 }
1579 break;
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001580 }
Alexey Frunzec061de12017-02-14 13:27:23 -08001581
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001582 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08001583 Register trg = trg_loc.AsRegister<Register>();
1584 if (is_R6) {
1585 __ Lw(trg, TMP, 0);
1586 } else {
1587 __ Lwr(trg, TMP, 0);
1588 __ Lwl(trg, TMP, 3);
1589 }
1590 if (is_volatile) {
1591 __ Sync(0);
1592 }
1593 break;
Alexey Frunzec061de12017-02-14 13:27:23 -08001594 }
Alexey Frunze15958152017-02-09 19:08:30 -08001595
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001596 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08001597 Register trg = trg_loc.AsRegister<Register>();
1598 if (kEmitCompilerReadBarrier) {
1599 if (kUseBakerReadBarrier) {
1600 Location temp = locations->GetTemp(0);
1601 codegen->GenerateReferenceLoadWithBakerReadBarrier(invoke,
1602 trg_loc,
1603 base,
1604 /* offset */ 0U,
1605 /* index */ offset_loc,
1606 TIMES_1,
1607 temp,
1608 /* needs_null_check */ false);
1609 if (is_volatile) {
1610 __ Sync(0);
1611 }
1612 } else {
1613 if (is_R6) {
1614 __ Lw(trg, TMP, 0);
1615 } else {
1616 __ Lwr(trg, TMP, 0);
1617 __ Lwl(trg, TMP, 3);
1618 }
1619 if (is_volatile) {
1620 __ Sync(0);
1621 }
1622 codegen->GenerateReadBarrierSlow(invoke,
1623 trg_loc,
1624 trg_loc,
1625 base_loc,
1626 /* offset */ 0U,
1627 /* index */ offset_loc);
1628 }
1629 } else {
1630 if (is_R6) {
1631 __ Lw(trg, TMP, 0);
1632 } else {
1633 __ Lwr(trg, TMP, 0);
1634 __ Lwl(trg, TMP, 3);
1635 }
1636 if (is_volatile) {
1637 __ Sync(0);
1638 }
1639 __ MaybeUnpoisonHeapReference(trg);
1640 }
1641 break;
1642 }
1643
1644 default:
1645 LOG(FATAL) << "Unexpected type " << type;
1646 UNREACHABLE();
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001647 }
1648}
1649
1650// int sun.misc.Unsafe.getInt(Object o, long offset)
1651void IntrinsicLocationsBuilderMIPS::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001652 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001653}
1654
1655void IntrinsicCodeGeneratorMIPS::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001656 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, IsR6(), codegen_);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001657}
1658
1659// int sun.misc.Unsafe.getIntVolatile(Object o, long offset)
1660void IntrinsicLocationsBuilderMIPS::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001661 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001662}
1663
1664void IntrinsicCodeGeneratorMIPS::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001665 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, IsR6(), codegen_);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001666}
1667
1668// long sun.misc.Unsafe.getLong(Object o, long offset)
1669void IntrinsicLocationsBuilderMIPS::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001670 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001671}
1672
1673void IntrinsicCodeGeneratorMIPS::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001674 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, IsR6(), codegen_);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001675}
1676
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001677// Object sun.misc.Unsafe.getObject(Object o, long offset)
1678void IntrinsicLocationsBuilderMIPS::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001679 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kReference);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001680}
1681
1682void IntrinsicCodeGeneratorMIPS::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, IsR6(), codegen_);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001684}
1685
1686// Object sun.misc.Unsafe.getObjectVolatile(Object o, long offset)
1687void IntrinsicLocationsBuilderMIPS::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001688 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kReference);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001689}
1690
1691void IntrinsicCodeGeneratorMIPS::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001692 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, IsR6(), codegen_);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001693}
1694
Vladimir Markoca6fff82017-10-03 14:49:14 +01001695static void CreateIntIntIntIntToVoidLocations(ArenaAllocator* allocator, HInvoke* invoke) {
1696 LocationSummary* locations =
1697 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001698 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1699 locations->SetInAt(1, Location::RequiresRegister());
1700 locations->SetInAt(2, Location::RequiresRegister());
1701 locations->SetInAt(3, Location::RequiresRegister());
1702}
1703
Alexey Frunze15958152017-02-09 19:08:30 -08001704// Note that the caller must supply a properly aligned memory address.
1705// If they do not, the behavior is undefined (atomicity not guaranteed, exception may occur).
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001706static void GenUnsafePut(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001707 DataType::Type type,
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001708 bool is_volatile,
1709 bool is_ordered,
1710 bool is_R6,
1711 CodeGeneratorMIPS* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001712 DCHECK((type == DataType::Type::kInt32) ||
1713 (type == DataType::Type::kInt64) ||
1714 (type == DataType::Type::kReference)) << type;
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001715 MipsAssembler* assembler = codegen->GetAssembler();
1716 // Object pointer.
1717 Register base = locations->InAt(1).AsRegister<Register>();
1718 // The "offset" argument is passed as a "long", i.e., it's 64-bits in
1719 // size. Since this code is for a 32-bit processor, we can only use
1720 // 32-bit addresses, so we only need the low 32-bits of offset.
1721 Register offset_lo = locations->InAt(2).AsRegisterPairLow<Register>();
1722
1723 __ Addu(TMP, base, offset_lo);
1724 if (is_volatile || is_ordered) {
1725 __ Sync(0);
1726 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001727 if ((type == DataType::Type::kInt32) || (type == DataType::Type::kReference)) {
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001728 Register value = locations->InAt(3).AsRegister<Register>();
1729
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001730 if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Alexey Frunzec061de12017-02-14 13:27:23 -08001731 __ PoisonHeapReference(AT, value);
1732 value = AT;
1733 }
1734
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001735 if (is_R6) {
1736 __ Sw(value, TMP, 0);
1737 } else {
1738 __ Swr(value, TMP, 0);
1739 __ Swl(value, TMP, 3);
1740 }
1741 } else {
1742 Register value_lo = locations->InAt(3).AsRegisterPairLow<Register>();
1743 Register value_hi = locations->InAt(3).AsRegisterPairHigh<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08001744 CHECK(!is_volatile); // TODO: support atomic 8-byte volatile stores.
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001745 if (is_R6) {
1746 __ Sw(value_lo, TMP, 0);
1747 __ Sw(value_hi, TMP, 4);
1748 } else {
1749 __ Swr(value_lo, TMP, 0);
1750 __ Swl(value_lo, TMP, 3);
1751 __ Swr(value_hi, TMP, 4);
1752 __ Swl(value_hi, TMP, 7);
1753 }
1754 }
1755
1756 if (is_volatile) {
1757 __ Sync(0);
1758 }
1759
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001760 if (type == DataType::Type::kReference) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01001761 bool value_can_be_null = true; // TODO: Worth finding out this information?
1762 codegen->MarkGCCard(base, locations->InAt(3).AsRegister<Register>(), value_can_be_null);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001763 }
1764}
1765
1766// void sun.misc.Unsafe.putInt(Object o, long offset, int x)
1767void IntrinsicLocationsBuilderMIPS::VisitUnsafePut(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001768 CreateIntIntIntIntToVoidLocations(allocator_, invoke);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001769}
1770
1771void IntrinsicCodeGeneratorMIPS::VisitUnsafePut(HInvoke* invoke) {
1772 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001773 DataType::Type::kInt32,
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001774 /* is_volatile */ false,
1775 /* is_ordered */ false,
1776 IsR6(),
1777 codegen_);
1778}
1779
1780// void sun.misc.Unsafe.putOrderedInt(Object o, long offset, int x)
1781void IntrinsicLocationsBuilderMIPS::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001782 CreateIntIntIntIntToVoidLocations(allocator_, invoke);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001783}
1784
1785void IntrinsicCodeGeneratorMIPS::VisitUnsafePutOrdered(HInvoke* invoke) {
1786 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001787 DataType::Type::kInt32,
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001788 /* is_volatile */ false,
1789 /* is_ordered */ true,
1790 IsR6(),
1791 codegen_);
1792}
1793
1794// void sun.misc.Unsafe.putIntVolatile(Object o, long offset, int x)
1795void IntrinsicLocationsBuilderMIPS::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001796 CreateIntIntIntIntToVoidLocations(allocator_, invoke);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001797}
1798
1799void IntrinsicCodeGeneratorMIPS::VisitUnsafePutVolatile(HInvoke* invoke) {
1800 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001801 DataType::Type::kInt32,
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001802 /* is_volatile */ true,
1803 /* is_ordered */ false,
1804 IsR6(),
1805 codegen_);
1806}
1807
1808// void sun.misc.Unsafe.putObject(Object o, long offset, Object x)
1809void IntrinsicLocationsBuilderMIPS::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001810 CreateIntIntIntIntToVoidLocations(allocator_, invoke);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001811}
1812
1813void IntrinsicCodeGeneratorMIPS::VisitUnsafePutObject(HInvoke* invoke) {
1814 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001815 DataType::Type::kReference,
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001816 /* is_volatile */ false,
1817 /* is_ordered */ false,
1818 IsR6(),
1819 codegen_);
1820}
1821
1822// void sun.misc.Unsafe.putOrderedObject(Object o, long offset, Object x)
1823void IntrinsicLocationsBuilderMIPS::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001824 CreateIntIntIntIntToVoidLocations(allocator_, invoke);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001825}
1826
1827void IntrinsicCodeGeneratorMIPS::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1828 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001829 DataType::Type::kReference,
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001830 /* is_volatile */ false,
1831 /* is_ordered */ true,
1832 IsR6(),
1833 codegen_);
1834}
1835
1836// void sun.misc.Unsafe.putObjectVolatile(Object o, long offset, Object x)
1837void IntrinsicLocationsBuilderMIPS::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001838 CreateIntIntIntIntToVoidLocations(allocator_, invoke);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001839}
1840
1841void IntrinsicCodeGeneratorMIPS::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1842 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001843 DataType::Type::kReference,
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001844 /* is_volatile */ true,
1845 /* is_ordered */ false,
1846 IsR6(),
1847 codegen_);
1848}
1849
1850// void sun.misc.Unsafe.putLong(Object o, long offset, long x)
1851void IntrinsicLocationsBuilderMIPS::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001852 CreateIntIntIntIntToVoidLocations(allocator_, invoke);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001853}
1854
1855void IntrinsicCodeGeneratorMIPS::VisitUnsafePutLong(HInvoke* invoke) {
1856 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001857 DataType::Type::kInt64,
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001858 /* is_volatile */ false,
1859 /* is_ordered */ false,
1860 IsR6(),
1861 codegen_);
1862}
1863
1864// void sun.misc.Unsafe.putOrderedLong(Object o, long offset, long x)
1865void IntrinsicLocationsBuilderMIPS::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001866 CreateIntIntIntIntToVoidLocations(allocator_, invoke);
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001867}
1868
1869void IntrinsicCodeGeneratorMIPS::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1870 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001871 DataType::Type::kInt64,
Chris Larsen4fdc6d92015-12-14 13:26:14 -08001872 /* is_volatile */ false,
1873 /* is_ordered */ true,
1874 IsR6(),
1875 codegen_);
1876}
1877
Vladimir Markoca6fff82017-10-03 14:49:14 +01001878static void CreateIntIntIntIntIntToIntPlusTemps(ArenaAllocator* allocator, HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08001879 bool can_call = kEmitCompilerReadBarrier &&
1880 kUseBakerReadBarrier &&
1881 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001882 LocationSummary* locations =
1883 new (allocator) LocationSummary(invoke,
1884 can_call
1885 ? LocationSummary::kCallOnSlowPath
1886 : LocationSummary::kNoCall,
1887 kIntrinsified);
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001888 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1889 locations->SetInAt(1, Location::RequiresRegister());
1890 locations->SetInAt(2, Location::RequiresRegister());
1891 locations->SetInAt(3, Location::RequiresRegister());
1892 locations->SetInAt(4, Location::RequiresRegister());
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001893 locations->SetOut(Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08001894
1895 // Temporary register used in CAS by (Baker) read barrier.
1896 if (can_call) {
1897 locations->AddTemp(Location::RequiresRegister());
1898 }
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001899}
1900
Alexey Frunze15958152017-02-09 19:08:30 -08001901// Note that the caller must supply a properly aligned memory address.
1902// If they do not, the behavior is undefined (atomicity not guaranteed, exception may occur).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001903static void GenCas(HInvoke* invoke, DataType::Type type, CodeGeneratorMIPS* codegen) {
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001904 MipsAssembler* assembler = codegen->GetAssembler();
Alexey Frunze15958152017-02-09 19:08:30 -08001905 LocationSummary* locations = invoke->GetLocations();
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001906 bool isR6 = codegen->GetInstructionSetFeatures().IsR6();
1907 Register base = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08001908 Location offset_loc = locations->InAt(2);
1909 Register offset_lo = offset_loc.AsRegisterPairLow<Register>();
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001910 Register expected = locations->InAt(3).AsRegister<Register>();
1911 Register value = locations->InAt(4).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08001912 Location out_loc = locations->Out();
1913 Register out = out_loc.AsRegister<Register>();
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001914
1915 DCHECK_NE(base, out);
1916 DCHECK_NE(offset_lo, out);
1917 DCHECK_NE(expected, out);
1918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001919 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08001920 // The only read barrier implementation supporting the
1921 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1922 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1923
1924 // Mark card for object assuming new value is stored. Worst case we will mark an unchanged
1925 // object and scan the receiver at the next GC for nothing.
Goran Jakovljevice114da22016-12-26 14:21:43 +01001926 bool value_can_be_null = true; // TODO: Worth finding out this information?
1927 codegen->MarkGCCard(base, value, value_can_be_null);
Alexey Frunze15958152017-02-09 19:08:30 -08001928
1929 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1930 Location temp = locations->GetTemp(0);
1931 // Need to make sure the reference stored in the field is a to-space
1932 // one before attempting the CAS or the CAS could fail incorrectly.
1933 codegen->GenerateReferenceLoadWithBakerReadBarrier(
1934 invoke,
1935 out_loc, // Unused, used only as a "temporary" within the read barrier.
1936 base,
1937 /* offset */ 0u,
1938 /* index */ offset_loc,
1939 ScaleFactor::TIMES_1,
1940 temp,
1941 /* needs_null_check */ false,
1942 /* always_update_field */ true);
1943 }
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001944 }
1945
Alexey Frunzec061de12017-02-14 13:27:23 -08001946 MipsLabel loop_head, exit_loop;
1947 __ Addu(TMP, base, offset_lo);
1948
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001949 if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Alexey Frunzec061de12017-02-14 13:27:23 -08001950 __ PoisonHeapReference(expected);
1951 // Do not poison `value`, if it is the same register as
1952 // `expected`, which has just been poisoned.
1953 if (value != expected) {
1954 __ PoisonHeapReference(value);
1955 }
1956 }
1957
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001958 // do {
1959 // tmp_value = [tmp_ptr] - expected;
1960 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1961 // result = tmp_value != 0;
1962
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001963 __ Sync(0);
1964 __ Bind(&loop_head);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001965 if ((type == DataType::Type::kInt32) || (type == DataType::Type::kReference)) {
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001966 if (isR6) {
1967 __ LlR6(out, TMP);
1968 } else {
1969 __ LlR2(out, TMP);
1970 }
1971 } else {
Alexey Frunzec061de12017-02-14 13:27:23 -08001972 LOG(FATAL) << "Unsupported op size " << type;
1973 UNREACHABLE();
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001974 }
1975 __ Subu(out, out, expected); // If we didn't get the 'expected'
1976 __ Sltiu(out, out, 1); // value, set 'out' to false, and
1977 __ Beqz(out, &exit_loop); // return.
1978 __ Move(out, value); // Use 'out' for the 'store conditional' instruction.
1979 // If we use 'value' directly, we would lose 'value'
1980 // in the case that the store fails. Whether the
1981 // store succeeds, or fails, it will load the
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001982 // correct Boolean value into the 'out' register.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001983 // This test isn't really necessary. We only support DataType::Type::kInt,
1984 // DataType::Type::kReference, and we already verified that we're working on one
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001985 // of those two types. It's left here in case the code needs to support
1986 // other types in the future.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001987 if ((type == DataType::Type::kInt32) || (type == DataType::Type::kReference)) {
Alexey Frunze51aff3a2016-03-17 17:21:45 -07001988 if (isR6) {
1989 __ ScR6(out, TMP);
1990 } else {
1991 __ ScR2(out, TMP);
1992 }
1993 }
1994 __ Beqz(out, &loop_head); // If we couldn't do the read-modify-write
1995 // cycle atomically then retry.
1996 __ Bind(&exit_loop);
1997 __ Sync(0);
Alexey Frunzec061de12017-02-14 13:27:23 -08001998
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001999 if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002000 __ UnpoisonHeapReference(expected);
2001 // Do not unpoison `value`, if it is the same register as
2002 // `expected`, which has just been unpoisoned.
2003 if (value != expected) {
2004 __ UnpoisonHeapReference(value);
2005 }
2006 }
Alexey Frunze51aff3a2016-03-17 17:21:45 -07002007}
2008
2009// boolean sun.misc.Unsafe.compareAndSwapInt(Object o, long offset, int expected, int x)
2010void IntrinsicLocationsBuilderMIPS::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002011 CreateIntIntIntIntIntToIntPlusTemps(allocator_, invoke);
Alexey Frunze51aff3a2016-03-17 17:21:45 -07002012}
2013
2014void IntrinsicCodeGeneratorMIPS::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002015 GenCas(invoke, DataType::Type::kInt32, codegen_);
Alexey Frunze51aff3a2016-03-17 17:21:45 -07002016}
2017
2018// boolean sun.misc.Unsafe.compareAndSwapObject(Object o, long offset, Object expected, Object x)
2019void IntrinsicLocationsBuilderMIPS::VisitUnsafeCASObject(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08002020 // The only read barrier implementation supporting the
2021 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2022 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
2023 return;
2024 }
2025
Vladimir Markoca6fff82017-10-03 14:49:14 +01002026 CreateIntIntIntIntIntToIntPlusTemps(allocator_, invoke);
Alexey Frunze51aff3a2016-03-17 17:21:45 -07002027}
2028
2029void IntrinsicCodeGeneratorMIPS::VisitUnsafeCASObject(HInvoke* invoke) {
Alexey Frunze15958152017-02-09 19:08:30 -08002030 // The only read barrier implementation supporting the
2031 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2032 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002034 GenCas(invoke, DataType::Type::kReference, codegen_);
Alexey Frunze51aff3a2016-03-17 17:21:45 -07002035}
2036
Chris Larsencf283da2016-01-19 16:45:35 -08002037// int java.lang.String.compareTo(String anotherString)
2038void IntrinsicLocationsBuilderMIPS::VisitStringCompareTo(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002039 LocationSummary* locations = new (allocator_) LocationSummary(
2040 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Chris Larsencf283da2016-01-19 16:45:35 -08002041 InvokeRuntimeCallingConvention calling_convention;
2042 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2043 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 Location outLocation = calling_convention.GetReturnLocation(DataType::Type::kInt32);
Chris Larsencf283da2016-01-19 16:45:35 -08002045 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2046}
2047
2048void IntrinsicCodeGeneratorMIPS::VisitStringCompareTo(HInvoke* invoke) {
2049 MipsAssembler* assembler = GetAssembler();
2050 LocationSummary* locations = invoke->GetLocations();
2051
2052 // Note that the null check must have been done earlier.
2053 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
2054
2055 Register argument = locations->InAt(1).AsRegister<Register>();
Vladimir Marko174b2e22017-10-12 13:34:49 +01002056 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathMIPS(invoke);
Chris Larsencf283da2016-01-19 16:45:35 -08002057 codegen_->AddSlowPath(slow_path);
2058 __ Beqz(argument, slow_path->GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +01002059 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Chris Larsencf283da2016-01-19 16:45:35 -08002060 __ Bind(slow_path->GetExitLabel());
2061}
2062
Chris Larsen16ba2b42015-11-02 10:58:31 -08002063// boolean java.lang.String.equals(Object anObject)
2064void IntrinsicLocationsBuilderMIPS::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002065 LocationSummary* locations =
2066 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsen16ba2b42015-11-02 10:58:31 -08002067 locations->SetInAt(0, Location::RequiresRegister());
2068 locations->SetInAt(1, Location::RequiresRegister());
2069 locations->SetOut(Location::RequiresRegister());
2070
2071 // Temporary registers to store lengths of strings and for calculations.
2072 locations->AddTemp(Location::RequiresRegister());
2073 locations->AddTemp(Location::RequiresRegister());
2074 locations->AddTemp(Location::RequiresRegister());
2075}
2076
2077void IntrinsicCodeGeneratorMIPS::VisitStringEquals(HInvoke* invoke) {
2078 MipsAssembler* assembler = GetAssembler();
2079 LocationSummary* locations = invoke->GetLocations();
2080
2081 Register str = locations->InAt(0).AsRegister<Register>();
2082 Register arg = locations->InAt(1).AsRegister<Register>();
2083 Register out = locations->Out().AsRegister<Register>();
2084
2085 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2086 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2087 Register temp3 = locations->GetTemp(2).AsRegister<Register>();
2088
2089 MipsLabel loop;
2090 MipsLabel end;
2091 MipsLabel return_true;
2092 MipsLabel return_false;
2093
2094 // Get offsets of count, value, and class fields within a string object.
2095 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2096 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
2097 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
2098
2099 // Note that the null check must have been done earlier.
2100 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
2101
2102 // If the register containing the pointer to "this", and the register
2103 // containing the pointer to "anObject" are the same register then
2104 // "this", and "anObject" are the same object and we can
2105 // short-circuit the logic to a true result.
2106 if (str == arg) {
2107 __ LoadConst32(out, 1);
2108 return;
2109 }
Goran Jakovljevic64fa84f2017-02-27 13:14:57 +01002110 StringEqualsOptimizations optimizations(invoke);
2111 if (!optimizations.GetArgumentNotNull()) {
2112 // Check if input is null, return false if it is.
2113 __ Beqz(arg, &return_false);
2114 }
Chris Larsen16ba2b42015-11-02 10:58:31 -08002115
2116 // Reference equality check, return true if same reference.
2117 __ Beq(str, arg, &return_true);
2118
Goran Jakovljevic64fa84f2017-02-27 13:14:57 +01002119 if (!optimizations.GetArgumentIsString()) {
2120 // Instanceof check for the argument by comparing class fields.
2121 // All string objects must have the same type since String cannot be subclassed.
2122 // Receiver must be a string object, so its class field is equal to all strings' class fields.
2123 // If the argument is a string object, its class field must be equal to receiver's class field.
2124 __ Lw(temp1, str, class_offset);
2125 __ Lw(temp2, arg, class_offset);
2126 __ Bne(temp1, temp2, &return_false);
2127 }
Chris Larsen16ba2b42015-11-02 10:58:31 -08002128
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002129 // Load `count` fields of this and argument strings.
Chris Larsen16ba2b42015-11-02 10:58:31 -08002130 __ Lw(temp1, str, count_offset);
2131 __ Lw(temp2, arg, count_offset);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002132 // Check if `count` fields are equal, return false if they're not.
2133 // Also compares the compression style, if differs return false.
Chris Larsen16ba2b42015-11-02 10:58:31 -08002134 __ Bne(temp1, temp2, &return_false);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002135 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
2136 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2137 "Expecting 0=compressed, 1=uncompressed");
Chris Larsen16ba2b42015-11-02 10:58:31 -08002138 __ Beqz(temp1, &return_true);
2139
2140 // Don't overwrite input registers
2141 __ Move(TMP, str);
2142 __ Move(temp3, arg);
2143
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002144 // Assertions that must hold in order to compare strings 4 bytes at a time.
Chris Larsen16ba2b42015-11-02 10:58:31 -08002145 DCHECK_ALIGNED(value_offset, 4);
2146 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
2147
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002148 // For string compression, calculate the number of bytes to compare (not chars).
2149 if (mirror::kUseStringCompression) {
2150 // Extract compression flag.
2151 if (IsR2OrNewer()) {
2152 __ Ext(temp2, temp1, 0, 1);
2153 } else {
2154 __ Sll(temp2, temp1, 31);
2155 __ Srl(temp2, temp2, 31);
2156 }
2157 __ Srl(temp1, temp1, 1); // Extract length.
2158 __ Sllv(temp1, temp1, temp2); // Double the byte count if uncompressed.
2159 }
2160
2161 // Loop to compare strings 4 bytes at a time starting at the beginning of the string.
2162 // Ok to do this because strings are zero-padded to kObjectAlignment.
Chris Larsen16ba2b42015-11-02 10:58:31 -08002163 __ Bind(&loop);
2164 __ Lw(out, TMP, value_offset);
2165 __ Lw(temp2, temp3, value_offset);
2166 __ Bne(out, temp2, &return_false);
2167 __ Addiu(TMP, TMP, 4);
2168 __ Addiu(temp3, temp3, 4);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002169 // With string compression, we have compared 4 bytes, otherwise 2 chars.
2170 __ Addiu(temp1, temp1, mirror::kUseStringCompression ? -4 : -2);
Chris Larsen16ba2b42015-11-02 10:58:31 -08002171 __ Bgtz(temp1, &loop);
2172
2173 // Return true and exit the function.
2174 // If loop does not result in returning false, we return true.
2175 __ Bind(&return_true);
2176 __ LoadConst32(out, 1);
2177 __ B(&end);
2178
2179 // Return false and exit the function.
2180 __ Bind(&return_false);
2181 __ LoadConst32(out, 0);
2182 __ Bind(&end);
2183}
2184
Chris Larsencf283da2016-01-19 16:45:35 -08002185static void GenerateStringIndexOf(HInvoke* invoke,
2186 bool start_at_zero,
2187 MipsAssembler* assembler,
Vladimir Marko174b2e22017-10-12 13:34:49 +01002188 CodeGeneratorMIPS* codegen) {
Chris Larsencf283da2016-01-19 16:45:35 -08002189 LocationSummary* locations = invoke->GetLocations();
2190 Register tmp_reg = start_at_zero ? locations->GetTemp(0).AsRegister<Register>() : TMP;
2191
2192 // Note that the null check must have been done earlier.
2193 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
2194
Vladimir Markofb6c90a2016-05-06 15:52:12 +01002195 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
2196 // 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 -08002197 SlowPathCodeMIPS* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01002198 HInstruction* code_point = invoke->InputAt(1);
2199 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01002200 if (!IsUint<16>(code_point->AsIntConstant()->GetValue())) {
Chris Larsencf283da2016-01-19 16:45:35 -08002201 // Always needs the slow-path. We could directly dispatch to it,
2202 // but this case should be rare, so for simplicity just put the
2203 // full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01002204 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathMIPS(invoke);
Chris Larsencf283da2016-01-19 16:45:35 -08002205 codegen->AddSlowPath(slow_path);
2206 __ B(slow_path->GetEntryLabel());
2207 __ Bind(slow_path->GetExitLabel());
2208 return;
2209 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002210 } else if (code_point->GetType() != DataType::Type::kUint16) {
Chris Larsencf283da2016-01-19 16:45:35 -08002211 Register char_reg = locations->InAt(1).AsRegister<Register>();
2212 // The "bltu" conditional branch tests to see if the character value
2213 // fits in a valid 16-bit (MIPS halfword) value. If it doesn't then
2214 // the character being searched for, if it exists in the string, is
2215 // encoded using UTF-16 and stored in the string as two (16-bit)
2216 // halfwords. Currently the assembly code used to implement this
2217 // intrinsic doesn't support searching for a character stored as
2218 // two halfwords so we fallback to using the generic implementation
2219 // of indexOf().
2220 __ LoadConst32(tmp_reg, std::numeric_limits<uint16_t>::max());
Vladimir Marko174b2e22017-10-12 13:34:49 +01002221 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathMIPS(invoke);
Chris Larsencf283da2016-01-19 16:45:35 -08002222 codegen->AddSlowPath(slow_path);
2223 __ Bltu(tmp_reg, char_reg, slow_path->GetEntryLabel());
2224 }
2225
2226 if (start_at_zero) {
2227 DCHECK_EQ(tmp_reg, A2);
2228 // Start-index = 0.
2229 __ Clear(tmp_reg);
2230 }
2231
Serban Constantinescufca16662016-07-14 09:21:59 +01002232 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
Chris Larsencf283da2016-01-19 16:45:35 -08002233 if (slow_path != nullptr) {
2234 __ Bind(slow_path->GetExitLabel());
2235 }
2236}
2237
2238// int java.lang.String.indexOf(int ch)
2239void IntrinsicLocationsBuilderMIPS::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002240 LocationSummary* locations = new (allocator_) LocationSummary(
2241 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Chris Larsencf283da2016-01-19 16:45:35 -08002242 // We have a hand-crafted assembly stub that follows the runtime
2243 // calling convention. So it's best to align the inputs accordingly.
2244 InvokeRuntimeCallingConvention calling_convention;
2245 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2246 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002247 Location outLocation = calling_convention.GetReturnLocation(DataType::Type::kInt32);
Chris Larsencf283da2016-01-19 16:45:35 -08002248 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2249
2250 // Need a temp for slow-path codepoint compare, and need to send start-index=0.
2251 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2252}
2253
2254void IntrinsicCodeGeneratorMIPS::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002255 GenerateStringIndexOf(invoke, /* start_at_zero */ true, GetAssembler(), codegen_);
Chris Larsencf283da2016-01-19 16:45:35 -08002256}
2257
2258// int java.lang.String.indexOf(int ch, int fromIndex)
2259void IntrinsicLocationsBuilderMIPS::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002260 LocationSummary* locations = new (allocator_) LocationSummary(
2261 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Chris Larsencf283da2016-01-19 16:45:35 -08002262 // We have a hand-crafted assembly stub that follows the runtime
2263 // calling convention. So it's best to align the inputs accordingly.
2264 InvokeRuntimeCallingConvention calling_convention;
2265 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2266 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2267 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002268 Location outLocation = calling_convention.GetReturnLocation(DataType::Type::kInt32);
Chris Larsencf283da2016-01-19 16:45:35 -08002269 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2270
2271 // Need a temp for slow-path codepoint compare.
2272 locations->AddTemp(Location::RequiresRegister());
2273}
2274
2275void IntrinsicCodeGeneratorMIPS::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002276 GenerateStringIndexOf(invoke, /* start_at_zero */ false, GetAssembler(), codegen_);
Chris Larsencf283da2016-01-19 16:45:35 -08002277}
2278
2279// java.lang.StringFactory.newStringFromBytes(byte[] data, int high, int offset, int byteCount)
2280void IntrinsicLocationsBuilderMIPS::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002281 LocationSummary* locations = new (allocator_) LocationSummary(
2282 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Chris Larsencf283da2016-01-19 16:45:35 -08002283 InvokeRuntimeCallingConvention calling_convention;
2284 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2285 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2286 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2287 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002288 Location outLocation = calling_convention.GetReturnLocation(DataType::Type::kInt32);
Chris Larsencf283da2016-01-19 16:45:35 -08002289 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2290}
2291
2292void IntrinsicCodeGeneratorMIPS::VisitStringNewStringFromBytes(HInvoke* invoke) {
2293 MipsAssembler* assembler = GetAssembler();
2294 LocationSummary* locations = invoke->GetLocations();
2295
2296 Register byte_array = locations->InAt(0).AsRegister<Register>();
Vladimir Marko174b2e22017-10-12 13:34:49 +01002297 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathMIPS(invoke);
Chris Larsencf283da2016-01-19 16:45:35 -08002298 codegen_->AddSlowPath(slow_path);
2299 __ Beqz(byte_array, slow_path->GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +01002300 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
Chris Larsencf283da2016-01-19 16:45:35 -08002301 __ Bind(slow_path->GetExitLabel());
2302}
2303
2304// java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
2305void IntrinsicLocationsBuilderMIPS::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002306 LocationSummary* locations =
2307 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Chris Larsencf283da2016-01-19 16:45:35 -08002308 InvokeRuntimeCallingConvention calling_convention;
2309 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2310 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2311 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002312 Location outLocation = calling_convention.GetReturnLocation(DataType::Type::kInt32);
Chris Larsencf283da2016-01-19 16:45:35 -08002313 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2314}
2315
2316void IntrinsicCodeGeneratorMIPS::VisitStringNewStringFromChars(HInvoke* invoke) {
Chris Larsencf283da2016-01-19 16:45:35 -08002317 // No need to emit code checking whether `locations->InAt(2)` is a null
2318 // pointer, as callers of the native method
2319 //
2320 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
2321 //
2322 // all include a null check on `data` before calling that method.
Serban Constantinescufca16662016-07-14 09:21:59 +01002323 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Chris Larsencf283da2016-01-19 16:45:35 -08002324}
2325
2326// java.lang.StringFactory.newStringFromString(String toCopy)
2327void IntrinsicLocationsBuilderMIPS::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002328 LocationSummary* locations = new (allocator_) LocationSummary(
2329 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Chris Larsencf283da2016-01-19 16:45:35 -08002330 InvokeRuntimeCallingConvention calling_convention;
2331 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002332 Location outLocation = calling_convention.GetReturnLocation(DataType::Type::kInt32);
Chris Larsencf283da2016-01-19 16:45:35 -08002333 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2334}
2335
2336void IntrinsicCodeGeneratorMIPS::VisitStringNewStringFromString(HInvoke* invoke) {
2337 MipsAssembler* assembler = GetAssembler();
2338 LocationSummary* locations = invoke->GetLocations();
2339
2340 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
Vladimir Marko174b2e22017-10-12 13:34:49 +01002341 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathMIPS(invoke);
Chris Larsencf283da2016-01-19 16:45:35 -08002342 codegen_->AddSlowPath(slow_path);
2343 __ Beqz(string_to_copy, slow_path->GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +01002344 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Chris Larsencf283da2016-01-19 16:45:35 -08002345 __ Bind(slow_path->GetExitLabel());
2346}
2347
Chris Larsen2714fe62016-02-11 14:23:53 -08002348static void GenIsInfinite(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002349 const DataType::Type type,
Chris Larsen2714fe62016-02-11 14:23:53 -08002350 const bool isR6,
2351 MipsAssembler* assembler) {
2352 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
2353 Register out = locations->Out().AsRegister<Register>();
2354
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002355 DCHECK(type == DataType::Type::kFloat32 || type == DataType::Type::kFloat64);
Chris Larsen2714fe62016-02-11 14:23:53 -08002356
2357 if (isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002358 if (type == DataType::Type::kFloat64) {
Chris Larsen2714fe62016-02-11 14:23:53 -08002359 __ ClassD(FTMP, in);
2360 } else {
2361 __ ClassS(FTMP, in);
2362 }
2363 __ Mfc1(out, FTMP);
2364 __ Andi(out, out, kPositiveInfinity | kNegativeInfinity);
2365 __ Sltu(out, ZERO, out);
2366 } else {
2367 // If one, or more, of the exponent bits is zero, then the number can't be infinite.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 if (type == DataType::Type::kFloat64) {
Chris Larsen2714fe62016-02-11 14:23:53 -08002369 __ MoveFromFpuHigh(TMP, in);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002370 __ LoadConst32(AT, High32Bits(kPositiveInfinityDouble));
Chris Larsen2714fe62016-02-11 14:23:53 -08002371 } else {
2372 __ Mfc1(TMP, in);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002373 __ LoadConst32(AT, kPositiveInfinityFloat);
Chris Larsen2714fe62016-02-11 14:23:53 -08002374 }
2375 __ Xor(TMP, TMP, AT);
2376
2377 __ Sll(TMP, TMP, 1);
2378
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002379 if (type == DataType::Type::kFloat64) {
Chris Larsen2714fe62016-02-11 14:23:53 -08002380 __ Mfc1(AT, in);
2381 __ Or(TMP, TMP, AT);
2382 }
2383 // If any of the significand bits are one, then the number is not infinite.
2384 __ Sltiu(out, TMP, 1);
2385 }
2386}
2387
2388// boolean java.lang.Float.isInfinite(float)
2389void IntrinsicLocationsBuilderMIPS::VisitFloatIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002390 CreateFPToIntLocations(allocator_, invoke);
Chris Larsen2714fe62016-02-11 14:23:53 -08002391}
2392
2393void IntrinsicCodeGeneratorMIPS::VisitFloatIsInfinite(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002394 GenIsInfinite(invoke->GetLocations(), DataType::Type::kFloat32, IsR6(), GetAssembler());
Chris Larsen2714fe62016-02-11 14:23:53 -08002395}
2396
2397// boolean java.lang.Double.isInfinite(double)
2398void IntrinsicLocationsBuilderMIPS::VisitDoubleIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002399 CreateFPToIntLocations(allocator_, invoke);
Chris Larsen2714fe62016-02-11 14:23:53 -08002400}
2401
2402void IntrinsicCodeGeneratorMIPS::VisitDoubleIsInfinite(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002403 GenIsInfinite(invoke->GetLocations(), DataType::Type::kFloat64, IsR6(), GetAssembler());
Chris Larsen2714fe62016-02-11 14:23:53 -08002404}
2405
Chris Larsen97759342016-02-16 17:10:40 -08002406static void GenHighestOneBit(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002407 const DataType::Type type,
Chris Larsen97759342016-02-16 17:10:40 -08002408 bool isR6,
2409 MipsAssembler* assembler) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002410 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Chris Larsen97759342016-02-16 17:10:40 -08002411
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 if (type == DataType::Type::kInt64) {
Chris Larsen97759342016-02-16 17:10:40 -08002413 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
2414 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
2415 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
2416 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
2417
2418 if (isR6) {
2419 __ ClzR6(TMP, in_hi);
2420 } else {
2421 __ ClzR2(TMP, in_hi);
2422 }
2423 __ LoadConst32(AT, 0x80000000);
2424 __ Srlv(out_hi, AT, TMP);
2425 __ And(out_hi, out_hi, in_hi);
2426 if (isR6) {
2427 __ ClzR6(TMP, in_lo);
2428 } else {
2429 __ ClzR2(TMP, in_lo);
2430 }
2431 __ Srlv(out_lo, AT, TMP);
2432 __ And(out_lo, out_lo, in_lo);
2433 if (isR6) {
2434 __ Seleqz(out_lo, out_lo, out_hi);
2435 } else {
2436 __ Movn(out_lo, ZERO, out_hi);
2437 }
2438 } else {
2439 Register in = locations->InAt(0).AsRegister<Register>();
2440 Register out = locations->Out().AsRegister<Register>();
2441
2442 if (isR6) {
2443 __ ClzR6(TMP, in);
2444 } else {
2445 __ ClzR2(TMP, in);
2446 }
2447 __ LoadConst32(AT, 0x80000000);
2448 __ Srlv(AT, AT, TMP); // Srlv shifts in the range of [0;31] bits (lower 5 bits of arg).
2449 __ And(out, AT, in); // So this is required for 0 (=shift by 32).
2450 }
2451}
2452
2453// int java.lang.Integer.highestOneBit(int)
2454void IntrinsicLocationsBuilderMIPS::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002455 CreateIntToIntLocations(allocator_, invoke);
Chris Larsen97759342016-02-16 17:10:40 -08002456}
2457
2458void IntrinsicCodeGeneratorMIPS::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002459 GenHighestOneBit(invoke->GetLocations(), DataType::Type::kInt32, IsR6(), GetAssembler());
Chris Larsen97759342016-02-16 17:10:40 -08002460}
2461
2462// long java.lang.Long.highestOneBit(long)
2463void IntrinsicLocationsBuilderMIPS::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002464 CreateIntToIntLocations(allocator_, invoke, Location::kOutputOverlap);
Chris Larsen97759342016-02-16 17:10:40 -08002465}
2466
2467void IntrinsicCodeGeneratorMIPS::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002468 GenHighestOneBit(invoke->GetLocations(), DataType::Type::kInt64, IsR6(), GetAssembler());
Chris Larsen97759342016-02-16 17:10:40 -08002469}
2470
2471static void GenLowestOneBit(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002472 const DataType::Type type,
Chris Larsen97759342016-02-16 17:10:40 -08002473 bool isR6,
2474 MipsAssembler* assembler) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002475 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Chris Larsen97759342016-02-16 17:10:40 -08002476
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002477 if (type == DataType::Type::kInt64) {
Chris Larsen97759342016-02-16 17:10:40 -08002478 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
2479 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
2480 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
2481 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
2482
2483 __ Subu(TMP, ZERO, in_lo);
2484 __ And(out_lo, TMP, in_lo);
2485 __ Subu(TMP, ZERO, in_hi);
2486 __ And(out_hi, TMP, in_hi);
2487 if (isR6) {
2488 __ Seleqz(out_hi, out_hi, out_lo);
2489 } else {
2490 __ Movn(out_hi, ZERO, out_lo);
2491 }
2492 } else {
2493 Register in = locations->InAt(0).AsRegister<Register>();
2494 Register out = locations->Out().AsRegister<Register>();
2495
2496 __ Subu(TMP, ZERO, in);
2497 __ And(out, TMP, in);
2498 }
2499}
2500
2501// int java.lang.Integer.lowestOneBit(int)
2502void IntrinsicLocationsBuilderMIPS::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002503 CreateIntToIntLocations(allocator_, invoke);
Chris Larsen97759342016-02-16 17:10:40 -08002504}
2505
2506void IntrinsicCodeGeneratorMIPS::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002507 GenLowestOneBit(invoke->GetLocations(), DataType::Type::kInt32, IsR6(), GetAssembler());
Chris Larsen97759342016-02-16 17:10:40 -08002508}
2509
2510// long java.lang.Long.lowestOneBit(long)
2511void IntrinsicLocationsBuilderMIPS::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002512 CreateIntToIntLocations(allocator_, invoke);
Chris Larsen97759342016-02-16 17:10:40 -08002513}
2514
2515void IntrinsicCodeGeneratorMIPS::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 GenLowestOneBit(invoke->GetLocations(), DataType::Type::kInt64, IsR6(), GetAssembler());
Chris Larsen97759342016-02-16 17:10:40 -08002517}
2518
Chris Larsenf09d5322016-04-22 12:06:34 -07002519// int java.lang.Math.round(float)
2520void IntrinsicLocationsBuilderMIPS::VisitMathRoundFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002521 LocationSummary* locations =
2522 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsenf09d5322016-04-22 12:06:34 -07002523 locations->SetInAt(0, Location::RequiresFpuRegister());
2524 locations->AddTemp(Location::RequiresFpuRegister());
2525 locations->SetOut(Location::RequiresRegister());
2526}
2527
2528void IntrinsicCodeGeneratorMIPS::VisitMathRoundFloat(HInvoke* invoke) {
2529 LocationSummary* locations = invoke->GetLocations();
2530 MipsAssembler* assembler = GetAssembler();
2531 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
2532 FRegister half = locations->GetTemp(0).AsFpuRegister<FRegister>();
2533 Register out = locations->Out().AsRegister<Register>();
2534
2535 MipsLabel done;
Chris Larsenf09d5322016-04-22 12:06:34 -07002536
Chris Larsenf09d5322016-04-22 12:06:34 -07002537 if (IsR6()) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02002538 // out = floor(in);
2539 //
2540 // if (out != MAX_VALUE && out != MIN_VALUE) {
2541 // TMP = ((in - out) >= 0.5) ? 1 : 0;
2542 // return out += TMP;
2543 // }
2544 // return out;
Chris Larsenf09d5322016-04-22 12:06:34 -07002545
Lena Djokicf4e23a82017-05-09 15:43:45 +02002546 // out = floor(in);
2547 __ FloorWS(FTMP, in);
2548 __ Mfc1(out, FTMP);
Chris Larsenf09d5322016-04-22 12:06:34 -07002549
Lena Djokicf4e23a82017-05-09 15:43:45 +02002550 // if (out != MAX_VALUE && out != MIN_VALUE)
2551 __ Addiu(TMP, out, 1);
2552 __ Aui(TMP, TMP, 0x8000); // TMP = out + 0x8000 0001
2553 // or out - 0x7FFF FFFF.
2554 // IOW, TMP = 1 if out = Int.MIN_VALUE
2555 // or TMP = 0 if out = Int.MAX_VALUE.
2556 __ Srl(TMP, TMP, 1); // TMP = 0 if out = Int.MIN_VALUE
2557 // or out = Int.MAX_VALUE.
2558 __ Beqz(TMP, &done);
Chris Larsenf09d5322016-04-22 12:06:34 -07002559
Lena Djokicf4e23a82017-05-09 15:43:45 +02002560 // TMP = (0.5f <= (in - out)) ? -1 : 0;
2561 __ Cvtsw(FTMP, FTMP); // Convert output of floor.w.s back to "float".
2562 __ LoadConst32(AT, bit_cast<int32_t, float>(0.5f));
2563 __ SubS(FTMP, in, FTMP);
2564 __ Mtc1(AT, half);
Chris Larsenf09d5322016-04-22 12:06:34 -07002565
Chris Larsenf09d5322016-04-22 12:06:34 -07002566 __ CmpLeS(FTMP, half, FTMP);
Chris Larsen07f712f2016-06-10 16:06:02 -07002567 __ Mfc1(TMP, FTMP);
Lena Djokicf4e23a82017-05-09 15:43:45 +02002568
2569 // Return out -= TMP.
2570 __ Subu(out, out, TMP);
Chris Larsenf09d5322016-04-22 12:06:34 -07002571 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02002572 // if (in.isNaN) {
2573 // return 0;
2574 // }
2575 //
2576 // out = floor.w.s(in);
2577 //
2578 // /*
2579 // * This "if" statement is only needed for the pre-R6 version of floor.w.s
2580 // * which outputs Integer.MAX_VALUE for negative numbers with magnitudes
2581 // * too large to fit in a 32-bit integer.
2582 // */
2583 // if (out == Integer.MAX_VALUE) {
2584 // TMP = (in < 0.0f) ? 1 : 0;
2585 // /*
2586 // * If TMP is 1, then adding it to out will wrap its value from
2587 // * Integer.MAX_VALUE to Integer.MIN_VALUE.
2588 // */
2589 // return out += TMP;
2590 // }
2591 //
2592 // /*
2593 // * For negative values not handled by the previous "if" statement the
2594 // * test here will correctly set the value of TMP.
2595 // */
2596 // TMP = ((in - out) >= 0.5f) ? 1 : 0;
2597 // return out += TMP;
2598
2599 MipsLabel finite;
2600 MipsLabel add;
2601
2602 // Test for NaN.
2603 __ CunS(in, in);
2604
2605 // Return zero for NaN.
2606 __ Move(out, ZERO);
2607 __ Bc1t(&done);
2608
2609 // out = floor(in);
2610 __ FloorWS(FTMP, in);
2611 __ Mfc1(out, FTMP);
2612
2613 __ LoadConst32(TMP, -1);
2614
2615 // TMP = (out = java.lang.Integer.MAX_VALUE) ? -1 : 0;
2616 __ LoadConst32(AT, std::numeric_limits<int32_t>::max());
2617 __ Bne(AT, out, &finite);
2618
2619 __ Mtc1(ZERO, FTMP);
2620 __ ColtS(in, FTMP);
2621
2622 __ B(&add);
2623
2624 __ Bind(&finite);
2625
2626 // TMP = (0.5f <= (in - out)) ? -1 : 0;
2627 __ Cvtsw(FTMP, FTMP); // Convert output of floor.w.s back to "float".
2628 __ LoadConst32(AT, bit_cast<int32_t, float>(0.5f));
2629 __ SubS(FTMP, in, FTMP);
2630 __ Mtc1(AT, half);
Chris Larsenf09d5322016-04-22 12:06:34 -07002631 __ ColeS(half, FTMP);
Chris Larsenf09d5322016-04-22 12:06:34 -07002632
Lena Djokicf4e23a82017-05-09 15:43:45 +02002633 __ Bind(&add);
Chris Larsenf09d5322016-04-22 12:06:34 -07002634
Chris Larsenf09d5322016-04-22 12:06:34 -07002635 __ Movf(TMP, ZERO);
Lena Djokicf4e23a82017-05-09 15:43:45 +02002636
2637 // Return out -= TMP.
2638 __ Subu(out, out, TMP);
Chris Larsenf09d5322016-04-22 12:06:34 -07002639 }
Chris Larsenf09d5322016-04-22 12:06:34 -07002640 __ Bind(&done);
2641}
2642
Chris Larsen692235e2016-11-21 16:04:53 -08002643// void java.lang.String.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
2644void IntrinsicLocationsBuilderMIPS::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002645 LocationSummary* locations =
2646 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsen692235e2016-11-21 16:04:53 -08002647 locations->SetInAt(0, Location::RequiresRegister());
2648 locations->SetInAt(1, Location::RequiresRegister());
2649 locations->SetInAt(2, Location::RequiresRegister());
2650 locations->SetInAt(3, Location::RequiresRegister());
2651 locations->SetInAt(4, Location::RequiresRegister());
2652
Chris Larsenfe4ff442017-03-23 11:25:12 -07002653 locations->AddTemp(Location::RequiresRegister());
2654 locations->AddTemp(Location::RequiresRegister());
2655 locations->AddTemp(Location::RequiresRegister());
Chris Larsen692235e2016-11-21 16:04:53 -08002656}
2657
2658void IntrinsicCodeGeneratorMIPS::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2659 MipsAssembler* assembler = GetAssembler();
2660 LocationSummary* locations = invoke->GetLocations();
2661
2662 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002663 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Chris Larsen692235e2016-11-21 16:04:53 -08002664 DCHECK_EQ(char_size, 2u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002665 const size_t char_shift = DataType::SizeShift(DataType::Type::kUint16);
Chris Larsen692235e2016-11-21 16:04:53 -08002666
2667 Register srcObj = locations->InAt(0).AsRegister<Register>();
2668 Register srcBegin = locations->InAt(1).AsRegister<Register>();
2669 Register srcEnd = locations->InAt(2).AsRegister<Register>();
2670 Register dstObj = locations->InAt(3).AsRegister<Register>();
2671 Register dstBegin = locations->InAt(4).AsRegister<Register>();
2672
2673 Register dstPtr = locations->GetTemp(0).AsRegister<Register>();
Chris Larsen692235e2016-11-21 16:04:53 -08002674 Register srcPtr = locations->GetTemp(1).AsRegister<Register>();
Chris Larsen692235e2016-11-21 16:04:53 -08002675 Register numChrs = locations->GetTemp(2).AsRegister<Register>();
Chris Larsen692235e2016-11-21 16:04:53 -08002676
2677 MipsLabel done;
Chris Larsenfe4ff442017-03-23 11:25:12 -07002678 MipsLabel loop;
Chris Larsen692235e2016-11-21 16:04:53 -08002679
2680 // Location of data in char array buffer.
2681 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
2682
2683 // Get offset of value field within a string object.
2684 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
2685
2686 __ Beq(srcEnd, srcBegin, &done); // No characters to move.
2687
2688 // Calculate number of characters to be copied.
2689 __ Subu(numChrs, srcEnd, srcBegin);
2690
2691 // Calculate destination address.
2692 __ Addiu(dstPtr, dstObj, data_offset);
Chris Larsencd0295d2017-03-31 15:26:54 -07002693 __ ShiftAndAdd(dstPtr, dstBegin, dstPtr, char_shift);
Chris Larsen692235e2016-11-21 16:04:53 -08002694
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002695 if (mirror::kUseStringCompression) {
2696 MipsLabel uncompressed_copy, compressed_loop;
2697 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2698 // Load count field and extract compression flag.
2699 __ LoadFromOffset(kLoadWord, TMP, srcObj, count_offset);
2700 __ Sll(TMP, TMP, 31);
2701
Chris Larsenfe4ff442017-03-23 11:25:12 -07002702 // If string is uncompressed, use uncompressed path.
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002703 __ Bnez(TMP, &uncompressed_copy);
2704
2705 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
2706 __ Addu(srcPtr, srcObj, srcBegin);
2707 __ Bind(&compressed_loop);
2708 __ LoadFromOffset(kLoadUnsignedByte, TMP, srcPtr, value_offset);
2709 __ StoreToOffset(kStoreHalfword, TMP, dstPtr, 0);
2710 __ Addiu(numChrs, numChrs, -1);
2711 __ Addiu(srcPtr, srcPtr, 1);
2712 __ Addiu(dstPtr, dstPtr, 2);
2713 __ Bnez(numChrs, &compressed_loop);
2714
2715 __ B(&done);
2716 __ Bind(&uncompressed_copy);
2717 }
2718
Chris Larsen692235e2016-11-21 16:04:53 -08002719 // Calculate source address.
2720 __ Addiu(srcPtr, srcObj, value_offset);
Chris Larsencd0295d2017-03-31 15:26:54 -07002721 __ ShiftAndAdd(srcPtr, srcBegin, srcPtr, char_shift);
Chris Larsen692235e2016-11-21 16:04:53 -08002722
Chris Larsenfe4ff442017-03-23 11:25:12 -07002723 __ Bind(&loop);
2724 __ Lh(AT, srcPtr, 0);
2725 __ Addiu(numChrs, numChrs, -1);
2726 __ Addiu(srcPtr, srcPtr, char_size);
2727 __ Sh(AT, dstPtr, 0);
2728 __ Addiu(dstPtr, dstPtr, char_size);
2729 __ Bnez(numChrs, &loop);
Chris Larsen692235e2016-11-21 16:04:53 -08002730
2731 __ Bind(&done);
2732}
2733
Vladimir Markoca6fff82017-10-03 14:49:14 +01002734static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
2735 LocationSummary* locations =
2736 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002737 InvokeRuntimeCallingConvention calling_convention;
2738
2739 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002740 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kFloat64));
Chris Larsenb9005fa2017-03-24 12:11:54 -07002741}
2742
Vladimir Markoca6fff82017-10-03 14:49:14 +01002743static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
2744 LocationSummary* locations =
2745 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002746 InvokeRuntimeCallingConvention calling_convention;
2747
2748 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2749 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002750 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kFloat64));
Chris Larsenb9005fa2017-03-24 12:11:54 -07002751}
2752
2753static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorMIPS* codegen, QuickEntrypointEnum entry) {
2754 LocationSummary* locations = invoke->GetLocations();
2755 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
2756 DCHECK_EQ(in, F12);
2757 FRegister out = locations->Out().AsFpuRegister<FRegister>();
2758 DCHECK_EQ(out, F0);
2759
2760 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2761}
2762
2763static void GenFPFPToFPCall(HInvoke* invoke,
2764 CodeGeneratorMIPS* codegen,
2765 QuickEntrypointEnum entry) {
2766 LocationSummary* locations = invoke->GetLocations();
2767 FRegister in0 = locations->InAt(0).AsFpuRegister<FRegister>();
2768 DCHECK_EQ(in0, F12);
2769 FRegister in1 = locations->InAt(1).AsFpuRegister<FRegister>();
2770 DCHECK_EQ(in1, F14);
2771 FRegister out = locations->Out().AsFpuRegister<FRegister>();
2772 DCHECK_EQ(out, F0);
2773
2774 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2775}
2776
2777// static double java.lang.Math.cos(double a)
2778void IntrinsicLocationsBuilderMIPS::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002779 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002780}
2781
2782void IntrinsicCodeGeneratorMIPS::VisitMathCos(HInvoke* invoke) {
2783 GenFPToFPCall(invoke, codegen_, kQuickCos);
2784}
2785
2786// static double java.lang.Math.sin(double a)
2787void IntrinsicLocationsBuilderMIPS::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002788 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002789}
2790
2791void IntrinsicCodeGeneratorMIPS::VisitMathSin(HInvoke* invoke) {
2792 GenFPToFPCall(invoke, codegen_, kQuickSin);
2793}
2794
2795// static double java.lang.Math.acos(double a)
2796void IntrinsicLocationsBuilderMIPS::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002797 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002798}
2799
2800void IntrinsicCodeGeneratorMIPS::VisitMathAcos(HInvoke* invoke) {
2801 GenFPToFPCall(invoke, codegen_, kQuickAcos);
2802}
2803
2804// static double java.lang.Math.asin(double a)
2805void IntrinsicLocationsBuilderMIPS::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002806 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002807}
2808
2809void IntrinsicCodeGeneratorMIPS::VisitMathAsin(HInvoke* invoke) {
2810 GenFPToFPCall(invoke, codegen_, kQuickAsin);
2811}
2812
2813// static double java.lang.Math.atan(double a)
2814void IntrinsicLocationsBuilderMIPS::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002815 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002816}
2817
2818void IntrinsicCodeGeneratorMIPS::VisitMathAtan(HInvoke* invoke) {
2819 GenFPToFPCall(invoke, codegen_, kQuickAtan);
2820}
2821
2822// static double java.lang.Math.atan2(double y, double x)
2823void IntrinsicLocationsBuilderMIPS::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002824 CreateFPFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002825}
2826
2827void IntrinsicCodeGeneratorMIPS::VisitMathAtan2(HInvoke* invoke) {
2828 GenFPFPToFPCall(invoke, codegen_, kQuickAtan2);
2829}
2830
2831// static double java.lang.Math.cbrt(double a)
2832void IntrinsicLocationsBuilderMIPS::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002833 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002834}
2835
2836void IntrinsicCodeGeneratorMIPS::VisitMathCbrt(HInvoke* invoke) {
2837 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
2838}
2839
2840// static double java.lang.Math.cosh(double x)
2841void IntrinsicLocationsBuilderMIPS::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002842 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002843}
2844
2845void IntrinsicCodeGeneratorMIPS::VisitMathCosh(HInvoke* invoke) {
2846 GenFPToFPCall(invoke, codegen_, kQuickCosh);
2847}
2848
2849// static double java.lang.Math.exp(double a)
2850void IntrinsicLocationsBuilderMIPS::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002851 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002852}
2853
2854void IntrinsicCodeGeneratorMIPS::VisitMathExp(HInvoke* invoke) {
2855 GenFPToFPCall(invoke, codegen_, kQuickExp);
2856}
2857
2858// static double java.lang.Math.expm1(double x)
2859void IntrinsicLocationsBuilderMIPS::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002860 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002861}
2862
2863void IntrinsicCodeGeneratorMIPS::VisitMathExpm1(HInvoke* invoke) {
2864 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
2865}
2866
2867// static double java.lang.Math.hypot(double x, double y)
2868void IntrinsicLocationsBuilderMIPS::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002869 CreateFPFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002870}
2871
2872void IntrinsicCodeGeneratorMIPS::VisitMathHypot(HInvoke* invoke) {
2873 GenFPFPToFPCall(invoke, codegen_, kQuickHypot);
2874}
2875
2876// static double java.lang.Math.log(double a)
2877void IntrinsicLocationsBuilderMIPS::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002878 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002879}
2880
2881void IntrinsicCodeGeneratorMIPS::VisitMathLog(HInvoke* invoke) {
2882 GenFPToFPCall(invoke, codegen_, kQuickLog);
2883}
2884
2885// static double java.lang.Math.log10(double x)
2886void IntrinsicLocationsBuilderMIPS::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002887 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002888}
2889
2890void IntrinsicCodeGeneratorMIPS::VisitMathLog10(HInvoke* invoke) {
2891 GenFPToFPCall(invoke, codegen_, kQuickLog10);
2892}
2893
2894// static double java.lang.Math.nextAfter(double start, double direction)
2895void IntrinsicLocationsBuilderMIPS::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002896 CreateFPFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002897}
2898
2899void IntrinsicCodeGeneratorMIPS::VisitMathNextAfter(HInvoke* invoke) {
2900 GenFPFPToFPCall(invoke, codegen_, kQuickNextAfter);
2901}
2902
2903// static double java.lang.Math.sinh(double x)
2904void IntrinsicLocationsBuilderMIPS::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002905 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002906}
2907
2908void IntrinsicCodeGeneratorMIPS::VisitMathSinh(HInvoke* invoke) {
2909 GenFPToFPCall(invoke, codegen_, kQuickSinh);
2910}
2911
2912// static double java.lang.Math.tan(double a)
2913void IntrinsicLocationsBuilderMIPS::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002914 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002915}
2916
2917void IntrinsicCodeGeneratorMIPS::VisitMathTan(HInvoke* invoke) {
2918 GenFPToFPCall(invoke, codegen_, kQuickTan);
2919}
2920
2921// static double java.lang.Math.tanh(double x)
2922void IntrinsicLocationsBuilderMIPS::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002923 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002924}
2925
2926void IntrinsicCodeGeneratorMIPS::VisitMathTanh(HInvoke* invoke) {
2927 GenFPToFPCall(invoke, codegen_, kQuickTanh);
2928}
2929
Chris Larsen2f6ad9d2017-03-23 15:37:03 -07002930// static void java.lang.System.arraycopy(Object src, int srcPos,
2931// Object dest, int destPos,
2932// int length)
2933void IntrinsicLocationsBuilderMIPS::VisitSystemArrayCopyChar(HInvoke* invoke) {
2934 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2935 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2936 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2937
2938 // As long as we are checking, we might as well check to see if the src and dest
2939 // positions are >= 0.
2940 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2941 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
2942 // We will have to fail anyways.
2943 return;
2944 }
2945
2946 // And since we are already checking, check the length too.
2947 if (length != nullptr) {
2948 int32_t len = length->GetValue();
2949 if (len < 0) {
2950 // Just call as normal.
2951 return;
2952 }
2953 }
2954
2955 // Okay, it is safe to generate inline code.
2956 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002957 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
Chris Larsen2f6ad9d2017-03-23 15:37:03 -07002958 // arraycopy(Object src, int srcPos, Object dest, int destPos, int length).
2959 locations->SetInAt(0, Location::RequiresRegister());
2960 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
2961 locations->SetInAt(2, Location::RequiresRegister());
2962 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
2963 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
2964
2965 locations->AddTemp(Location::RequiresRegister());
2966 locations->AddTemp(Location::RequiresRegister());
2967 locations->AddTemp(Location::RequiresRegister());
2968}
2969
2970// Utility routine to verify that "length(input) - pos >= length"
2971static void EnoughItems(MipsAssembler* assembler,
2972 Register length_input_minus_pos,
2973 Location length,
2974 SlowPathCodeMIPS* slow_path) {
2975 if (length.IsConstant()) {
2976 int32_t length_constant = length.GetConstant()->AsIntConstant()->GetValue();
2977
2978 if (IsInt<16>(length_constant)) {
2979 __ Slti(TMP, length_input_minus_pos, length_constant);
2980 __ Bnez(TMP, slow_path->GetEntryLabel());
2981 } else {
2982 __ LoadConst32(TMP, length_constant);
2983 __ Blt(length_input_minus_pos, TMP, slow_path->GetEntryLabel());
2984 }
2985 } else {
2986 __ Blt(length_input_minus_pos, length.AsRegister<Register>(), slow_path->GetEntryLabel());
2987 }
2988}
2989
2990static void CheckPosition(MipsAssembler* assembler,
2991 Location pos,
2992 Register input,
2993 Location length,
2994 SlowPathCodeMIPS* slow_path,
2995 bool length_is_input_length = false) {
2996 // Where is the length in the Array?
2997 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
2998
2999 // Calculate length(input) - pos.
3000 if (pos.IsConstant()) {
3001 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
3002 if (pos_const == 0) {
3003 if (!length_is_input_length) {
3004 // Check that length(input) >= length.
3005 __ LoadFromOffset(kLoadWord, AT, input, length_offset);
3006 EnoughItems(assembler, AT, length, slow_path);
3007 }
3008 } else {
3009 // Check that (length(input) - pos) >= zero.
3010 __ LoadFromOffset(kLoadWord, AT, input, length_offset);
3011 DCHECK_GT(pos_const, 0);
3012 __ Addiu32(AT, AT, -pos_const, TMP);
3013 __ Bltz(AT, slow_path->GetEntryLabel());
3014
3015 // Verify that (length(input) - pos) >= length.
3016 EnoughItems(assembler, AT, length, slow_path);
3017 }
3018 } else if (length_is_input_length) {
3019 // The only way the copy can succeed is if pos is zero.
3020 Register pos_reg = pos.AsRegister<Register>();
3021 __ Bnez(pos_reg, slow_path->GetEntryLabel());
3022 } else {
3023 // Verify that pos >= 0.
3024 Register pos_reg = pos.AsRegister<Register>();
3025 __ Bltz(pos_reg, slow_path->GetEntryLabel());
3026
3027 // Check that (length(input) - pos) >= zero.
3028 __ LoadFromOffset(kLoadWord, AT, input, length_offset);
3029 __ Subu(AT, AT, pos_reg);
3030 __ Bltz(AT, slow_path->GetEntryLabel());
3031
3032 // Verify that (length(input) - pos) >= length.
3033 EnoughItems(assembler, AT, length, slow_path);
3034 }
3035}
3036
3037void IntrinsicCodeGeneratorMIPS::VisitSystemArrayCopyChar(HInvoke* invoke) {
3038 MipsAssembler* assembler = GetAssembler();
3039 LocationSummary* locations = invoke->GetLocations();
3040
3041 Register src = locations->InAt(0).AsRegister<Register>();
3042 Location src_pos = locations->InAt(1);
3043 Register dest = locations->InAt(2).AsRegister<Register>();
3044 Location dest_pos = locations->InAt(3);
3045 Location length = locations->InAt(4);
3046
3047 MipsLabel loop;
3048
3049 Register dest_base = locations->GetTemp(0).AsRegister<Register>();
3050 Register src_base = locations->GetTemp(1).AsRegister<Register>();
3051 Register count = locations->GetTemp(2).AsRegister<Register>();
3052
Vladimir Marko174b2e22017-10-12 13:34:49 +01003053 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathMIPS(invoke);
Chris Larsen2f6ad9d2017-03-23 15:37:03 -07003054 codegen_->AddSlowPath(slow_path);
3055
3056 // Bail out if the source and destination are the same (to handle overlap).
3057 __ Beq(src, dest, slow_path->GetEntryLabel());
3058
3059 // Bail out if the source is null.
3060 __ Beqz(src, slow_path->GetEntryLabel());
3061
3062 // Bail out if the destination is null.
3063 __ Beqz(dest, slow_path->GetEntryLabel());
3064
3065 // Load length into register for count.
3066 if (length.IsConstant()) {
3067 __ LoadConst32(count, length.GetConstant()->AsIntConstant()->GetValue());
3068 } else {
3069 // If the length is negative, bail out.
3070 // We have already checked in the LocationsBuilder for the constant case.
3071 __ Bltz(length.AsRegister<Register>(), slow_path->GetEntryLabel());
3072
3073 __ Move(count, length.AsRegister<Register>());
3074 }
3075
3076 // Validity checks: source.
3077 CheckPosition(assembler, src_pos, src, Location::RegisterLocation(count), slow_path);
3078
3079 // Validity checks: dest.
3080 CheckPosition(assembler, dest_pos, dest, Location::RegisterLocation(count), slow_path);
3081
3082 // If count is zero, we're done.
3083 __ Beqz(count, slow_path->GetExitLabel());
3084
3085 // Okay, everything checks out. Finally time to do the copy.
3086 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003087 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Chris Larsen2f6ad9d2017-03-23 15:37:03 -07003088 DCHECK_EQ(char_size, 2u);
3089
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003090 const size_t char_shift = DataType::SizeShift(DataType::Type::kUint16);
Chris Larsen2f6ad9d2017-03-23 15:37:03 -07003091
3092 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
3093
3094 // Calculate source and destination addresses.
3095 if (src_pos.IsConstant()) {
3096 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
3097
3098 __ Addiu32(src_base, src, data_offset + char_size * src_pos_const, TMP);
3099 } else {
3100 __ Addiu32(src_base, src, data_offset, TMP);
3101 __ ShiftAndAdd(src_base, src_pos.AsRegister<Register>(), src_base, char_shift);
3102 }
3103 if (dest_pos.IsConstant()) {
3104 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
3105
3106 __ Addiu32(dest_base, dest, data_offset + char_size * dest_pos_const, TMP);
3107 } else {
3108 __ Addiu32(dest_base, dest, data_offset, TMP);
3109 __ ShiftAndAdd(dest_base, dest_pos.AsRegister<Register>(), dest_base, char_shift);
3110 }
3111
3112 __ Bind(&loop);
3113 __ Lh(TMP, src_base, 0);
3114 __ Addiu(src_base, src_base, char_size);
3115 __ Addiu(count, count, -1);
3116 __ Sh(TMP, dest_base, 0);
3117 __ Addiu(dest_base, dest_base, char_size);
3118 __ Bnez(count, &loop);
3119
3120 __ Bind(slow_path->GetExitLabel());
3121}
3122
Chris Larsen5633ce72017-04-10 15:47:40 -07003123// long java.lang.Integer.valueOf(long)
3124void IntrinsicLocationsBuilderMIPS::VisitIntegerValueOf(HInvoke* invoke) {
3125 InvokeRuntimeCallingConvention calling_convention;
3126 IntrinsicVisitor::ComputeIntegerValueOfLocations(
3127 invoke,
3128 codegen_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003129 calling_convention.GetReturnLocation(DataType::Type::kReference),
Chris Larsen5633ce72017-04-10 15:47:40 -07003130 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3131}
3132
3133void IntrinsicCodeGeneratorMIPS::VisitIntegerValueOf(HInvoke* invoke) {
3134 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
3135 LocationSummary* locations = invoke->GetLocations();
3136 MipsAssembler* assembler = GetAssembler();
3137 InstructionCodeGeneratorMIPS* icodegen =
3138 down_cast<InstructionCodeGeneratorMIPS*>(codegen_->GetInstructionVisitor());
3139
3140 Register out = locations->Out().AsRegister<Register>();
3141 InvokeRuntimeCallingConvention calling_convention;
3142 if (invoke->InputAt(0)->IsConstant()) {
3143 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
3144 if (value >= info.low && value <= info.high) {
3145 // Just embed the j.l.Integer in the code.
3146 ScopedObjectAccess soa(Thread::Current());
3147 mirror::Object* boxed = info.cache->Get(value + (-info.low));
3148 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
3149 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
3150 __ LoadConst32(out, address);
3151 } else {
3152 // Allocate and initialize a new j.l.Integer.
3153 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
3154 // JIT object table.
3155 uint32_t address =
3156 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3157 __ LoadConst32(calling_convention.GetRegisterAt(0), address);
3158 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3159 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3160 __ StoreConstToOffset(kStoreWord, value, out, info.value_offset, TMP);
3161 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3162 // one.
3163 icodegen->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3164 }
3165 } else {
3166 Register in = locations->InAt(0).AsRegister<Register>();
3167 MipsLabel allocate, done;
3168 int32_t count = static_cast<uint32_t>(info.high) - info.low + 1;
3169
3170 // Is (info.low <= in) && (in <= info.high)?
3171 __ Addiu32(out, in, -info.low);
3172 // As unsigned quantities is out < (info.high - info.low + 1)?
3173 if (IsInt<16>(count)) {
3174 __ Sltiu(AT, out, count);
3175 } else {
3176 __ LoadConst32(AT, count);
3177 __ Sltu(AT, out, AT);
3178 }
3179 // Branch if out >= (info.high - info.low + 1).
3180 // This means that "in" is outside of the range [info.low, info.high].
3181 __ Beqz(AT, &allocate);
3182
3183 // If the value is within the bounds, load the j.l.Integer directly from the array.
3184 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3185 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
3186 __ LoadConst32(TMP, data_offset + address);
3187 __ ShiftAndAdd(out, out, TMP, TIMES_4);
3188 __ Lw(out, out, 0);
3189 __ MaybeUnpoisonHeapReference(out);
3190 __ B(&done);
3191
3192 __ Bind(&allocate);
3193 // Otherwise allocate and initialize a new j.l.Integer.
3194 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3195 __ LoadConst32(calling_convention.GetRegisterAt(0), address);
3196 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3197 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3198 __ StoreToOffset(kStoreWord, in, out, info.value_offset);
3199 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3200 // one.
3201 icodegen->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3202 __ Bind(&done);
3203 }
3204}
3205
Chris Larsenb065b032017-11-02 12:13:20 -07003206// static boolean java.lang.Thread.interrupted()
3207void IntrinsicLocationsBuilderMIPS::VisitThreadInterrupted(HInvoke* invoke) {
3208 LocationSummary* locations =
3209 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
3210 locations->SetOut(Location::RequiresRegister());
3211}
3212
3213void IntrinsicCodeGeneratorMIPS::VisitThreadInterrupted(HInvoke* invoke) {
3214 MipsAssembler* assembler = GetAssembler();
3215 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
3216 int32_t offset = Thread::InterruptedOffset<kMipsPointerSize>().Int32Value();
3217 __ LoadFromOffset(kLoadWord, out, TR, offset);
3218 MipsLabel done;
3219 __ Beqz(out, &done);
3220 __ Sync(0);
3221 __ StoreToOffset(kStoreWord, ZERO, TR, offset);
3222 __ Sync(0);
3223 __ Bind(&done);
3224}
3225
Chris Larsen2714fe62016-02-11 14:23:53 -08003226// Unimplemented intrinsics.
3227
Aart Bik2f9fcc92016-03-01 15:16:54 -08003228UNIMPLEMENTED_INTRINSIC(MIPS, MathCeil)
3229UNIMPLEMENTED_INTRINSIC(MIPS, MathFloor)
3230UNIMPLEMENTED_INTRINSIC(MIPS, MathRint)
3231UNIMPLEMENTED_INTRINSIC(MIPS, MathRoundDouble)
Alexey Frunze15958152017-02-09 19:08:30 -08003232UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetLongVolatile);
3233UNIMPLEMENTED_INTRINSIC(MIPS, UnsafePutLongVolatile);
Aart Bik2f9fcc92016-03-01 15:16:54 -08003234UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeCASLong)
Chris Larsen701566a2015-10-27 15:29:13 -07003235
Aart Bik2f9fcc92016-03-01 15:16:54 -08003236UNIMPLEMENTED_INTRINSIC(MIPS, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08003237UNIMPLEMENTED_INTRINSIC(MIPS, SystemArrayCopy)
Aart Bik3f67e692016-01-15 14:35:12 -08003238
Aart Bikff7d89c2016-11-07 08:49:28 -08003239UNIMPLEMENTED_INTRINSIC(MIPS, StringStringIndexOf);
3240UNIMPLEMENTED_INTRINSIC(MIPS, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003241UNIMPLEMENTED_INTRINSIC(MIPS, StringBufferAppend);
3242UNIMPLEMENTED_INTRINSIC(MIPS, StringBufferLength);
3243UNIMPLEMENTED_INTRINSIC(MIPS, StringBufferToString);
3244UNIMPLEMENTED_INTRINSIC(MIPS, StringBuilderAppend);
3245UNIMPLEMENTED_INTRINSIC(MIPS, StringBuilderLength);
3246UNIMPLEMENTED_INTRINSIC(MIPS, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003247
Aart Bik0e54c012016-03-04 12:08:31 -08003248// 1.8.
3249UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndAddInt)
3250UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndAddLong)
3251UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndSetInt)
3252UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndSetLong)
3253UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndSetObject)
Chris Larsen701566a2015-10-27 15:29:13 -07003254
Aart Bik0e54c012016-03-04 12:08:31 -08003255UNREACHABLE_INTRINSICS(MIPS)
Chris Larsen2714fe62016-02-11 14:23:53 -08003256
Chris Larsen701566a2015-10-27 15:29:13 -07003257#undef __
3258
3259} // namespace mips
3260} // namespace art