blob: 5f2f71bd4d3330ce22e451dad3d140438c23ecd8 [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>();
2056 SlowPathCodeMIPS* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS(invoke);
2057 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,
2188 CodeGeneratorMIPS* codegen,
2189 ArenaAllocator* allocator) {
2190 LocationSummary* locations = invoke->GetLocations();
2191 Register tmp_reg = start_at_zero ? locations->GetTemp(0).AsRegister<Register>() : TMP;
2192
2193 // Note that the null check must have been done earlier.
2194 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
2195
Vladimir Markofb6c90a2016-05-06 15:52:12 +01002196 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
2197 // 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 -08002198 SlowPathCodeMIPS* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01002199 HInstruction* code_point = invoke->InputAt(1);
2200 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01002201 if (!IsUint<16>(code_point->AsIntConstant()->GetValue())) {
Chris Larsencf283da2016-01-19 16:45:35 -08002202 // Always needs the slow-path. We could directly dispatch to it,
2203 // but this case should be rare, so for simplicity just put the
2204 // full slow-path down and branch unconditionally.
2205 slow_path = new (allocator) IntrinsicSlowPathMIPS(invoke);
2206 codegen->AddSlowPath(slow_path);
2207 __ B(slow_path->GetEntryLabel());
2208 __ Bind(slow_path->GetExitLabel());
2209 return;
2210 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002211 } else if (code_point->GetType() != DataType::Type::kUint16) {
Chris Larsencf283da2016-01-19 16:45:35 -08002212 Register char_reg = locations->InAt(1).AsRegister<Register>();
2213 // The "bltu" conditional branch tests to see if the character value
2214 // fits in a valid 16-bit (MIPS halfword) value. If it doesn't then
2215 // the character being searched for, if it exists in the string, is
2216 // encoded using UTF-16 and stored in the string as two (16-bit)
2217 // halfwords. Currently the assembly code used to implement this
2218 // intrinsic doesn't support searching for a character stored as
2219 // two halfwords so we fallback to using the generic implementation
2220 // of indexOf().
2221 __ LoadConst32(tmp_reg, std::numeric_limits<uint16_t>::max());
2222 slow_path = new (allocator) IntrinsicSlowPathMIPS(invoke);
2223 codegen->AddSlowPath(slow_path);
2224 __ Bltu(tmp_reg, char_reg, slow_path->GetEntryLabel());
2225 }
2226
2227 if (start_at_zero) {
2228 DCHECK_EQ(tmp_reg, A2);
2229 // Start-index = 0.
2230 __ Clear(tmp_reg);
2231 }
2232
Serban Constantinescufca16662016-07-14 09:21:59 +01002233 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
Chris Larsencf283da2016-01-19 16:45:35 -08002234 if (slow_path != nullptr) {
2235 __ Bind(slow_path->GetExitLabel());
2236 }
2237}
2238
2239// int java.lang.String.indexOf(int ch)
2240void IntrinsicLocationsBuilderMIPS::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002241 LocationSummary* locations = new (allocator_) LocationSummary(
2242 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Chris Larsencf283da2016-01-19 16:45:35 -08002243 // We have a hand-crafted assembly stub that follows the runtime
2244 // calling convention. So it's best to align the inputs accordingly.
2245 InvokeRuntimeCallingConvention calling_convention;
2246 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2247 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002248 Location outLocation = calling_convention.GetReturnLocation(DataType::Type::kInt32);
Chris Larsencf283da2016-01-19 16:45:35 -08002249 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2250
2251 // Need a temp for slow-path codepoint compare, and need to send start-index=0.
2252 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2253}
2254
2255void IntrinsicCodeGeneratorMIPS::VisitStringIndexOf(HInvoke* invoke) {
2256 GenerateStringIndexOf(invoke,
2257 /* start_at_zero */ true,
2258 GetAssembler(),
2259 codegen_,
2260 GetAllocator());
2261}
2262
2263// int java.lang.String.indexOf(int ch, int fromIndex)
2264void IntrinsicLocationsBuilderMIPS::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002265 LocationSummary* locations = new (allocator_) LocationSummary(
2266 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Chris Larsencf283da2016-01-19 16:45:35 -08002267 // We have a hand-crafted assembly stub that follows the runtime
2268 // calling convention. So it's best to align the inputs accordingly.
2269 InvokeRuntimeCallingConvention calling_convention;
2270 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2271 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2272 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 Location outLocation = calling_convention.GetReturnLocation(DataType::Type::kInt32);
Chris Larsencf283da2016-01-19 16:45:35 -08002274 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2275
2276 // Need a temp for slow-path codepoint compare.
2277 locations->AddTemp(Location::RequiresRegister());
2278}
2279
2280void IntrinsicCodeGeneratorMIPS::VisitStringIndexOfAfter(HInvoke* invoke) {
2281 GenerateStringIndexOf(invoke,
2282 /* start_at_zero */ false,
2283 GetAssembler(),
2284 codegen_,
2285 GetAllocator());
2286}
2287
2288// java.lang.StringFactory.newStringFromBytes(byte[] data, int high, int offset, int byteCount)
2289void IntrinsicLocationsBuilderMIPS::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002290 LocationSummary* locations = new (allocator_) LocationSummary(
2291 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Chris Larsencf283da2016-01-19 16:45:35 -08002292 InvokeRuntimeCallingConvention calling_convention;
2293 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2294 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2295 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2296 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002297 Location outLocation = calling_convention.GetReturnLocation(DataType::Type::kInt32);
Chris Larsencf283da2016-01-19 16:45:35 -08002298 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2299}
2300
2301void IntrinsicCodeGeneratorMIPS::VisitStringNewStringFromBytes(HInvoke* invoke) {
2302 MipsAssembler* assembler = GetAssembler();
2303 LocationSummary* locations = invoke->GetLocations();
2304
2305 Register byte_array = locations->InAt(0).AsRegister<Register>();
2306 SlowPathCodeMIPS* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS(invoke);
2307 codegen_->AddSlowPath(slow_path);
2308 __ Beqz(byte_array, slow_path->GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +01002309 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
Chris Larsencf283da2016-01-19 16:45:35 -08002310 __ Bind(slow_path->GetExitLabel());
2311}
2312
2313// java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
2314void IntrinsicLocationsBuilderMIPS::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002315 LocationSummary* locations =
2316 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Chris Larsencf283da2016-01-19 16:45:35 -08002317 InvokeRuntimeCallingConvention calling_convention;
2318 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2319 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2320 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002321 Location outLocation = calling_convention.GetReturnLocation(DataType::Type::kInt32);
Chris Larsencf283da2016-01-19 16:45:35 -08002322 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2323}
2324
2325void IntrinsicCodeGeneratorMIPS::VisitStringNewStringFromChars(HInvoke* invoke) {
Chris Larsencf283da2016-01-19 16:45:35 -08002326 // No need to emit code checking whether `locations->InAt(2)` is a null
2327 // pointer, as callers of the native method
2328 //
2329 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
2330 //
2331 // all include a null check on `data` before calling that method.
Serban Constantinescufca16662016-07-14 09:21:59 +01002332 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Chris Larsencf283da2016-01-19 16:45:35 -08002333}
2334
2335// java.lang.StringFactory.newStringFromString(String toCopy)
2336void IntrinsicLocationsBuilderMIPS::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002337 LocationSummary* locations = new (allocator_) LocationSummary(
2338 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Chris Larsencf283da2016-01-19 16:45:35 -08002339 InvokeRuntimeCallingConvention calling_convention;
2340 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 Location outLocation = calling_convention.GetReturnLocation(DataType::Type::kInt32);
Chris Larsencf283da2016-01-19 16:45:35 -08002342 locations->SetOut(Location::RegisterLocation(outLocation.AsRegister<Register>()));
2343}
2344
2345void IntrinsicCodeGeneratorMIPS::VisitStringNewStringFromString(HInvoke* invoke) {
2346 MipsAssembler* assembler = GetAssembler();
2347 LocationSummary* locations = invoke->GetLocations();
2348
2349 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
2350 SlowPathCodeMIPS* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS(invoke);
2351 codegen_->AddSlowPath(slow_path);
2352 __ Beqz(string_to_copy, slow_path->GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +01002353 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Chris Larsencf283da2016-01-19 16:45:35 -08002354 __ Bind(slow_path->GetExitLabel());
2355}
2356
Chris Larsen2714fe62016-02-11 14:23:53 -08002357static void GenIsInfinite(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002358 const DataType::Type type,
Chris Larsen2714fe62016-02-11 14:23:53 -08002359 const bool isR6,
2360 MipsAssembler* assembler) {
2361 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
2362 Register out = locations->Out().AsRegister<Register>();
2363
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002364 DCHECK(type == DataType::Type::kFloat32 || type == DataType::Type::kFloat64);
Chris Larsen2714fe62016-02-11 14:23:53 -08002365
2366 if (isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002367 if (type == DataType::Type::kFloat64) {
Chris Larsen2714fe62016-02-11 14:23:53 -08002368 __ ClassD(FTMP, in);
2369 } else {
2370 __ ClassS(FTMP, in);
2371 }
2372 __ Mfc1(out, FTMP);
2373 __ Andi(out, out, kPositiveInfinity | kNegativeInfinity);
2374 __ Sltu(out, ZERO, out);
2375 } else {
2376 // If one, or more, of the exponent bits is zero, then the number can't be infinite.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 if (type == DataType::Type::kFloat64) {
Chris Larsen2714fe62016-02-11 14:23:53 -08002378 __ MoveFromFpuHigh(TMP, in);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002379 __ LoadConst32(AT, High32Bits(kPositiveInfinityDouble));
Chris Larsen2714fe62016-02-11 14:23:53 -08002380 } else {
2381 __ Mfc1(TMP, in);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002382 __ LoadConst32(AT, kPositiveInfinityFloat);
Chris Larsen2714fe62016-02-11 14:23:53 -08002383 }
2384 __ Xor(TMP, TMP, AT);
2385
2386 __ Sll(TMP, TMP, 1);
2387
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002388 if (type == DataType::Type::kFloat64) {
Chris Larsen2714fe62016-02-11 14:23:53 -08002389 __ Mfc1(AT, in);
2390 __ Or(TMP, TMP, AT);
2391 }
2392 // If any of the significand bits are one, then the number is not infinite.
2393 __ Sltiu(out, TMP, 1);
2394 }
2395}
2396
2397// boolean java.lang.Float.isInfinite(float)
2398void IntrinsicLocationsBuilderMIPS::VisitFloatIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002399 CreateFPToIntLocations(allocator_, invoke);
Chris Larsen2714fe62016-02-11 14:23:53 -08002400}
2401
2402void IntrinsicCodeGeneratorMIPS::VisitFloatIsInfinite(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002403 GenIsInfinite(invoke->GetLocations(), DataType::Type::kFloat32, IsR6(), GetAssembler());
Chris Larsen2714fe62016-02-11 14:23:53 -08002404}
2405
2406// boolean java.lang.Double.isInfinite(double)
2407void IntrinsicLocationsBuilderMIPS::VisitDoubleIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002408 CreateFPToIntLocations(allocator_, invoke);
Chris Larsen2714fe62016-02-11 14:23:53 -08002409}
2410
2411void IntrinsicCodeGeneratorMIPS::VisitDoubleIsInfinite(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 GenIsInfinite(invoke->GetLocations(), DataType::Type::kFloat64, IsR6(), GetAssembler());
Chris Larsen2714fe62016-02-11 14:23:53 -08002413}
2414
Chris Larsen97759342016-02-16 17:10:40 -08002415static void GenHighestOneBit(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002416 const DataType::Type type,
Chris Larsen97759342016-02-16 17:10:40 -08002417 bool isR6,
2418 MipsAssembler* assembler) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002419 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Chris Larsen97759342016-02-16 17:10:40 -08002420
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002421 if (type == DataType::Type::kInt64) {
Chris Larsen97759342016-02-16 17:10:40 -08002422 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
2423 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
2424 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
2425 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
2426
2427 if (isR6) {
2428 __ ClzR6(TMP, in_hi);
2429 } else {
2430 __ ClzR2(TMP, in_hi);
2431 }
2432 __ LoadConst32(AT, 0x80000000);
2433 __ Srlv(out_hi, AT, TMP);
2434 __ And(out_hi, out_hi, in_hi);
2435 if (isR6) {
2436 __ ClzR6(TMP, in_lo);
2437 } else {
2438 __ ClzR2(TMP, in_lo);
2439 }
2440 __ Srlv(out_lo, AT, TMP);
2441 __ And(out_lo, out_lo, in_lo);
2442 if (isR6) {
2443 __ Seleqz(out_lo, out_lo, out_hi);
2444 } else {
2445 __ Movn(out_lo, ZERO, out_hi);
2446 }
2447 } else {
2448 Register in = locations->InAt(0).AsRegister<Register>();
2449 Register out = locations->Out().AsRegister<Register>();
2450
2451 if (isR6) {
2452 __ ClzR6(TMP, in);
2453 } else {
2454 __ ClzR2(TMP, in);
2455 }
2456 __ LoadConst32(AT, 0x80000000);
2457 __ Srlv(AT, AT, TMP); // Srlv shifts in the range of [0;31] bits (lower 5 bits of arg).
2458 __ And(out, AT, in); // So this is required for 0 (=shift by 32).
2459 }
2460}
2461
2462// int java.lang.Integer.highestOneBit(int)
2463void IntrinsicLocationsBuilderMIPS::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002464 CreateIntToIntLocations(allocator_, invoke);
Chris Larsen97759342016-02-16 17:10:40 -08002465}
2466
2467void IntrinsicCodeGeneratorMIPS::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002468 GenHighestOneBit(invoke->GetLocations(), DataType::Type::kInt32, IsR6(), GetAssembler());
Chris Larsen97759342016-02-16 17:10:40 -08002469}
2470
2471// long java.lang.Long.highestOneBit(long)
2472void IntrinsicLocationsBuilderMIPS::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002473 CreateIntToIntLocations(allocator_, invoke, Location::kOutputOverlap);
Chris Larsen97759342016-02-16 17:10:40 -08002474}
2475
2476void IntrinsicCodeGeneratorMIPS::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002477 GenHighestOneBit(invoke->GetLocations(), DataType::Type::kInt64, IsR6(), GetAssembler());
Chris Larsen97759342016-02-16 17:10:40 -08002478}
2479
2480static void GenLowestOneBit(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002481 const DataType::Type type,
Chris Larsen97759342016-02-16 17:10:40 -08002482 bool isR6,
2483 MipsAssembler* assembler) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002484 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Chris Larsen97759342016-02-16 17:10:40 -08002485
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002486 if (type == DataType::Type::kInt64) {
Chris Larsen97759342016-02-16 17:10:40 -08002487 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
2488 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
2489 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
2490 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
2491
2492 __ Subu(TMP, ZERO, in_lo);
2493 __ And(out_lo, TMP, in_lo);
2494 __ Subu(TMP, ZERO, in_hi);
2495 __ And(out_hi, TMP, in_hi);
2496 if (isR6) {
2497 __ Seleqz(out_hi, out_hi, out_lo);
2498 } else {
2499 __ Movn(out_hi, ZERO, out_lo);
2500 }
2501 } else {
2502 Register in = locations->InAt(0).AsRegister<Register>();
2503 Register out = locations->Out().AsRegister<Register>();
2504
2505 __ Subu(TMP, ZERO, in);
2506 __ And(out, TMP, in);
2507 }
2508}
2509
2510// int java.lang.Integer.lowestOneBit(int)
2511void IntrinsicLocationsBuilderMIPS::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002512 CreateIntToIntLocations(allocator_, invoke);
Chris Larsen97759342016-02-16 17:10:40 -08002513}
2514
2515void IntrinsicCodeGeneratorMIPS::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 GenLowestOneBit(invoke->GetLocations(), DataType::Type::kInt32, IsR6(), GetAssembler());
Chris Larsen97759342016-02-16 17:10:40 -08002517}
2518
2519// long java.lang.Long.lowestOneBit(long)
2520void IntrinsicLocationsBuilderMIPS::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002521 CreateIntToIntLocations(allocator_, invoke);
Chris Larsen97759342016-02-16 17:10:40 -08002522}
2523
2524void IntrinsicCodeGeneratorMIPS::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002525 GenLowestOneBit(invoke->GetLocations(), DataType::Type::kInt64, IsR6(), GetAssembler());
Chris Larsen97759342016-02-16 17:10:40 -08002526}
2527
Chris Larsenf09d5322016-04-22 12:06:34 -07002528// int java.lang.Math.round(float)
2529void IntrinsicLocationsBuilderMIPS::VisitMathRoundFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002530 LocationSummary* locations =
2531 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsenf09d5322016-04-22 12:06:34 -07002532 locations->SetInAt(0, Location::RequiresFpuRegister());
2533 locations->AddTemp(Location::RequiresFpuRegister());
2534 locations->SetOut(Location::RequiresRegister());
2535}
2536
2537void IntrinsicCodeGeneratorMIPS::VisitMathRoundFloat(HInvoke* invoke) {
2538 LocationSummary* locations = invoke->GetLocations();
2539 MipsAssembler* assembler = GetAssembler();
2540 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
2541 FRegister half = locations->GetTemp(0).AsFpuRegister<FRegister>();
2542 Register out = locations->Out().AsRegister<Register>();
2543
2544 MipsLabel done;
Chris Larsenf09d5322016-04-22 12:06:34 -07002545
Chris Larsenf09d5322016-04-22 12:06:34 -07002546 if (IsR6()) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02002547 // out = floor(in);
2548 //
2549 // if (out != MAX_VALUE && out != MIN_VALUE) {
2550 // TMP = ((in - out) >= 0.5) ? 1 : 0;
2551 // return out += TMP;
2552 // }
2553 // return out;
Chris Larsenf09d5322016-04-22 12:06:34 -07002554
Lena Djokicf4e23a82017-05-09 15:43:45 +02002555 // out = floor(in);
2556 __ FloorWS(FTMP, in);
2557 __ Mfc1(out, FTMP);
Chris Larsenf09d5322016-04-22 12:06:34 -07002558
Lena Djokicf4e23a82017-05-09 15:43:45 +02002559 // if (out != MAX_VALUE && out != MIN_VALUE)
2560 __ Addiu(TMP, out, 1);
2561 __ Aui(TMP, TMP, 0x8000); // TMP = out + 0x8000 0001
2562 // or out - 0x7FFF FFFF.
2563 // IOW, TMP = 1 if out = Int.MIN_VALUE
2564 // or TMP = 0 if out = Int.MAX_VALUE.
2565 __ Srl(TMP, TMP, 1); // TMP = 0 if out = Int.MIN_VALUE
2566 // or out = Int.MAX_VALUE.
2567 __ Beqz(TMP, &done);
Chris Larsenf09d5322016-04-22 12:06:34 -07002568
Lena Djokicf4e23a82017-05-09 15:43:45 +02002569 // TMP = (0.5f <= (in - out)) ? -1 : 0;
2570 __ Cvtsw(FTMP, FTMP); // Convert output of floor.w.s back to "float".
2571 __ LoadConst32(AT, bit_cast<int32_t, float>(0.5f));
2572 __ SubS(FTMP, in, FTMP);
2573 __ Mtc1(AT, half);
Chris Larsenf09d5322016-04-22 12:06:34 -07002574
Chris Larsenf09d5322016-04-22 12:06:34 -07002575 __ CmpLeS(FTMP, half, FTMP);
Chris Larsen07f712f2016-06-10 16:06:02 -07002576 __ Mfc1(TMP, FTMP);
Lena Djokicf4e23a82017-05-09 15:43:45 +02002577
2578 // Return out -= TMP.
2579 __ Subu(out, out, TMP);
Chris Larsenf09d5322016-04-22 12:06:34 -07002580 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02002581 // if (in.isNaN) {
2582 // return 0;
2583 // }
2584 //
2585 // out = floor.w.s(in);
2586 //
2587 // /*
2588 // * This "if" statement is only needed for the pre-R6 version of floor.w.s
2589 // * which outputs Integer.MAX_VALUE for negative numbers with magnitudes
2590 // * too large to fit in a 32-bit integer.
2591 // */
2592 // if (out == Integer.MAX_VALUE) {
2593 // TMP = (in < 0.0f) ? 1 : 0;
2594 // /*
2595 // * If TMP is 1, then adding it to out will wrap its value from
2596 // * Integer.MAX_VALUE to Integer.MIN_VALUE.
2597 // */
2598 // return out += TMP;
2599 // }
2600 //
2601 // /*
2602 // * For negative values not handled by the previous "if" statement the
2603 // * test here will correctly set the value of TMP.
2604 // */
2605 // TMP = ((in - out) >= 0.5f) ? 1 : 0;
2606 // return out += TMP;
2607
2608 MipsLabel finite;
2609 MipsLabel add;
2610
2611 // Test for NaN.
2612 __ CunS(in, in);
2613
2614 // Return zero for NaN.
2615 __ Move(out, ZERO);
2616 __ Bc1t(&done);
2617
2618 // out = floor(in);
2619 __ FloorWS(FTMP, in);
2620 __ Mfc1(out, FTMP);
2621
2622 __ LoadConst32(TMP, -1);
2623
2624 // TMP = (out = java.lang.Integer.MAX_VALUE) ? -1 : 0;
2625 __ LoadConst32(AT, std::numeric_limits<int32_t>::max());
2626 __ Bne(AT, out, &finite);
2627
2628 __ Mtc1(ZERO, FTMP);
2629 __ ColtS(in, FTMP);
2630
2631 __ B(&add);
2632
2633 __ Bind(&finite);
2634
2635 // TMP = (0.5f <= (in - out)) ? -1 : 0;
2636 __ Cvtsw(FTMP, FTMP); // Convert output of floor.w.s back to "float".
2637 __ LoadConst32(AT, bit_cast<int32_t, float>(0.5f));
2638 __ SubS(FTMP, in, FTMP);
2639 __ Mtc1(AT, half);
Chris Larsenf09d5322016-04-22 12:06:34 -07002640 __ ColeS(half, FTMP);
Chris Larsenf09d5322016-04-22 12:06:34 -07002641
Lena Djokicf4e23a82017-05-09 15:43:45 +02002642 __ Bind(&add);
Chris Larsenf09d5322016-04-22 12:06:34 -07002643
Chris Larsenf09d5322016-04-22 12:06:34 -07002644 __ Movf(TMP, ZERO);
Lena Djokicf4e23a82017-05-09 15:43:45 +02002645
2646 // Return out -= TMP.
2647 __ Subu(out, out, TMP);
Chris Larsenf09d5322016-04-22 12:06:34 -07002648 }
Chris Larsenf09d5322016-04-22 12:06:34 -07002649 __ Bind(&done);
2650}
2651
Chris Larsen692235e2016-11-21 16:04:53 -08002652// void java.lang.String.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
2653void IntrinsicLocationsBuilderMIPS::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002654 LocationSummary* locations =
2655 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Chris Larsen692235e2016-11-21 16:04:53 -08002656 locations->SetInAt(0, Location::RequiresRegister());
2657 locations->SetInAt(1, Location::RequiresRegister());
2658 locations->SetInAt(2, Location::RequiresRegister());
2659 locations->SetInAt(3, Location::RequiresRegister());
2660 locations->SetInAt(4, Location::RequiresRegister());
2661
Chris Larsenfe4ff442017-03-23 11:25:12 -07002662 locations->AddTemp(Location::RequiresRegister());
2663 locations->AddTemp(Location::RequiresRegister());
2664 locations->AddTemp(Location::RequiresRegister());
Chris Larsen692235e2016-11-21 16:04:53 -08002665}
2666
2667void IntrinsicCodeGeneratorMIPS::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2668 MipsAssembler* assembler = GetAssembler();
2669 LocationSummary* locations = invoke->GetLocations();
2670
2671 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Chris Larsen692235e2016-11-21 16:04:53 -08002673 DCHECK_EQ(char_size, 2u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002674 const size_t char_shift = DataType::SizeShift(DataType::Type::kUint16);
Chris Larsen692235e2016-11-21 16:04:53 -08002675
2676 Register srcObj = locations->InAt(0).AsRegister<Register>();
2677 Register srcBegin = locations->InAt(1).AsRegister<Register>();
2678 Register srcEnd = locations->InAt(2).AsRegister<Register>();
2679 Register dstObj = locations->InAt(3).AsRegister<Register>();
2680 Register dstBegin = locations->InAt(4).AsRegister<Register>();
2681
2682 Register dstPtr = locations->GetTemp(0).AsRegister<Register>();
Chris Larsen692235e2016-11-21 16:04:53 -08002683 Register srcPtr = locations->GetTemp(1).AsRegister<Register>();
Chris Larsen692235e2016-11-21 16:04:53 -08002684 Register numChrs = locations->GetTemp(2).AsRegister<Register>();
Chris Larsen692235e2016-11-21 16:04:53 -08002685
2686 MipsLabel done;
Chris Larsenfe4ff442017-03-23 11:25:12 -07002687 MipsLabel loop;
Chris Larsen692235e2016-11-21 16:04:53 -08002688
2689 // Location of data in char array buffer.
2690 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
2691
2692 // Get offset of value field within a string object.
2693 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
2694
2695 __ Beq(srcEnd, srcBegin, &done); // No characters to move.
2696
2697 // Calculate number of characters to be copied.
2698 __ Subu(numChrs, srcEnd, srcBegin);
2699
2700 // Calculate destination address.
2701 __ Addiu(dstPtr, dstObj, data_offset);
Chris Larsencd0295d2017-03-31 15:26:54 -07002702 __ ShiftAndAdd(dstPtr, dstBegin, dstPtr, char_shift);
Chris Larsen692235e2016-11-21 16:04:53 -08002703
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002704 if (mirror::kUseStringCompression) {
2705 MipsLabel uncompressed_copy, compressed_loop;
2706 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2707 // Load count field and extract compression flag.
2708 __ LoadFromOffset(kLoadWord, TMP, srcObj, count_offset);
2709 __ Sll(TMP, TMP, 31);
2710
Chris Larsenfe4ff442017-03-23 11:25:12 -07002711 // If string is uncompressed, use uncompressed path.
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002712 __ Bnez(TMP, &uncompressed_copy);
2713
2714 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
2715 __ Addu(srcPtr, srcObj, srcBegin);
2716 __ Bind(&compressed_loop);
2717 __ LoadFromOffset(kLoadUnsignedByte, TMP, srcPtr, value_offset);
2718 __ StoreToOffset(kStoreHalfword, TMP, dstPtr, 0);
2719 __ Addiu(numChrs, numChrs, -1);
2720 __ Addiu(srcPtr, srcPtr, 1);
2721 __ Addiu(dstPtr, dstPtr, 2);
2722 __ Bnez(numChrs, &compressed_loop);
2723
2724 __ B(&done);
2725 __ Bind(&uncompressed_copy);
2726 }
2727
Chris Larsen692235e2016-11-21 16:04:53 -08002728 // Calculate source address.
2729 __ Addiu(srcPtr, srcObj, value_offset);
Chris Larsencd0295d2017-03-31 15:26:54 -07002730 __ ShiftAndAdd(srcPtr, srcBegin, srcPtr, char_shift);
Chris Larsen692235e2016-11-21 16:04:53 -08002731
Chris Larsenfe4ff442017-03-23 11:25:12 -07002732 __ Bind(&loop);
2733 __ Lh(AT, srcPtr, 0);
2734 __ Addiu(numChrs, numChrs, -1);
2735 __ Addiu(srcPtr, srcPtr, char_size);
2736 __ Sh(AT, dstPtr, 0);
2737 __ Addiu(dstPtr, dstPtr, char_size);
2738 __ Bnez(numChrs, &loop);
Chris Larsen692235e2016-11-21 16:04:53 -08002739
2740 __ Bind(&done);
2741}
2742
Vladimir Markoca6fff82017-10-03 14:49:14 +01002743static void CreateFPToFPCallLocations(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)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002749 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kFloat64));
Chris Larsenb9005fa2017-03-24 12:11:54 -07002750}
2751
Vladimir Markoca6fff82017-10-03 14:49:14 +01002752static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
2753 LocationSummary* locations =
2754 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002755 InvokeRuntimeCallingConvention calling_convention;
2756
2757 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2758 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002759 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kFloat64));
Chris Larsenb9005fa2017-03-24 12:11:54 -07002760}
2761
2762static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorMIPS* codegen, QuickEntrypointEnum entry) {
2763 LocationSummary* locations = invoke->GetLocations();
2764 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
2765 DCHECK_EQ(in, F12);
2766 FRegister out = locations->Out().AsFpuRegister<FRegister>();
2767 DCHECK_EQ(out, F0);
2768
2769 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2770}
2771
2772static void GenFPFPToFPCall(HInvoke* invoke,
2773 CodeGeneratorMIPS* codegen,
2774 QuickEntrypointEnum entry) {
2775 LocationSummary* locations = invoke->GetLocations();
2776 FRegister in0 = locations->InAt(0).AsFpuRegister<FRegister>();
2777 DCHECK_EQ(in0, F12);
2778 FRegister in1 = locations->InAt(1).AsFpuRegister<FRegister>();
2779 DCHECK_EQ(in1, F14);
2780 FRegister out = locations->Out().AsFpuRegister<FRegister>();
2781 DCHECK_EQ(out, F0);
2782
2783 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2784}
2785
2786// static double java.lang.Math.cos(double a)
2787void IntrinsicLocationsBuilderMIPS::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002788 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002789}
2790
2791void IntrinsicCodeGeneratorMIPS::VisitMathCos(HInvoke* invoke) {
2792 GenFPToFPCall(invoke, codegen_, kQuickCos);
2793}
2794
2795// static double java.lang.Math.sin(double a)
2796void IntrinsicLocationsBuilderMIPS::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002797 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002798}
2799
2800void IntrinsicCodeGeneratorMIPS::VisitMathSin(HInvoke* invoke) {
2801 GenFPToFPCall(invoke, codegen_, kQuickSin);
2802}
2803
2804// static double java.lang.Math.acos(double a)
2805void IntrinsicLocationsBuilderMIPS::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002806 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002807}
2808
2809void IntrinsicCodeGeneratorMIPS::VisitMathAcos(HInvoke* invoke) {
2810 GenFPToFPCall(invoke, codegen_, kQuickAcos);
2811}
2812
2813// static double java.lang.Math.asin(double a)
2814void IntrinsicLocationsBuilderMIPS::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002815 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002816}
2817
2818void IntrinsicCodeGeneratorMIPS::VisitMathAsin(HInvoke* invoke) {
2819 GenFPToFPCall(invoke, codegen_, kQuickAsin);
2820}
2821
2822// static double java.lang.Math.atan(double a)
2823void IntrinsicLocationsBuilderMIPS::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002824 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002825}
2826
2827void IntrinsicCodeGeneratorMIPS::VisitMathAtan(HInvoke* invoke) {
2828 GenFPToFPCall(invoke, codegen_, kQuickAtan);
2829}
2830
2831// static double java.lang.Math.atan2(double y, double x)
2832void IntrinsicLocationsBuilderMIPS::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002833 CreateFPFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002834}
2835
2836void IntrinsicCodeGeneratorMIPS::VisitMathAtan2(HInvoke* invoke) {
2837 GenFPFPToFPCall(invoke, codegen_, kQuickAtan2);
2838}
2839
2840// static double java.lang.Math.cbrt(double a)
2841void IntrinsicLocationsBuilderMIPS::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002842 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002843}
2844
2845void IntrinsicCodeGeneratorMIPS::VisitMathCbrt(HInvoke* invoke) {
2846 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
2847}
2848
2849// static double java.lang.Math.cosh(double x)
2850void IntrinsicLocationsBuilderMIPS::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002851 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002852}
2853
2854void IntrinsicCodeGeneratorMIPS::VisitMathCosh(HInvoke* invoke) {
2855 GenFPToFPCall(invoke, codegen_, kQuickCosh);
2856}
2857
2858// static double java.lang.Math.exp(double a)
2859void IntrinsicLocationsBuilderMIPS::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002860 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002861}
2862
2863void IntrinsicCodeGeneratorMIPS::VisitMathExp(HInvoke* invoke) {
2864 GenFPToFPCall(invoke, codegen_, kQuickExp);
2865}
2866
2867// static double java.lang.Math.expm1(double x)
2868void IntrinsicLocationsBuilderMIPS::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002869 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002870}
2871
2872void IntrinsicCodeGeneratorMIPS::VisitMathExpm1(HInvoke* invoke) {
2873 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
2874}
2875
2876// static double java.lang.Math.hypot(double x, double y)
2877void IntrinsicLocationsBuilderMIPS::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002878 CreateFPFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002879}
2880
2881void IntrinsicCodeGeneratorMIPS::VisitMathHypot(HInvoke* invoke) {
2882 GenFPFPToFPCall(invoke, codegen_, kQuickHypot);
2883}
2884
2885// static double java.lang.Math.log(double a)
2886void IntrinsicLocationsBuilderMIPS::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002887 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002888}
2889
2890void IntrinsicCodeGeneratorMIPS::VisitMathLog(HInvoke* invoke) {
2891 GenFPToFPCall(invoke, codegen_, kQuickLog);
2892}
2893
2894// static double java.lang.Math.log10(double x)
2895void IntrinsicLocationsBuilderMIPS::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002896 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002897}
2898
2899void IntrinsicCodeGeneratorMIPS::VisitMathLog10(HInvoke* invoke) {
2900 GenFPToFPCall(invoke, codegen_, kQuickLog10);
2901}
2902
2903// static double java.lang.Math.nextAfter(double start, double direction)
2904void IntrinsicLocationsBuilderMIPS::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002905 CreateFPFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002906}
2907
2908void IntrinsicCodeGeneratorMIPS::VisitMathNextAfter(HInvoke* invoke) {
2909 GenFPFPToFPCall(invoke, codegen_, kQuickNextAfter);
2910}
2911
2912// static double java.lang.Math.sinh(double x)
2913void IntrinsicLocationsBuilderMIPS::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002914 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002915}
2916
2917void IntrinsicCodeGeneratorMIPS::VisitMathSinh(HInvoke* invoke) {
2918 GenFPToFPCall(invoke, codegen_, kQuickSinh);
2919}
2920
2921// static double java.lang.Math.tan(double a)
2922void IntrinsicLocationsBuilderMIPS::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002923 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002924}
2925
2926void IntrinsicCodeGeneratorMIPS::VisitMathTan(HInvoke* invoke) {
2927 GenFPToFPCall(invoke, codegen_, kQuickTan);
2928}
2929
2930// static double java.lang.Math.tanh(double x)
2931void IntrinsicLocationsBuilderMIPS::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002932 CreateFPToFPCallLocations(allocator_, invoke);
Chris Larsenb9005fa2017-03-24 12:11:54 -07002933}
2934
2935void IntrinsicCodeGeneratorMIPS::VisitMathTanh(HInvoke* invoke) {
2936 GenFPToFPCall(invoke, codegen_, kQuickTanh);
2937}
2938
Chris Larsen2f6ad9d2017-03-23 15:37:03 -07002939// static void java.lang.System.arraycopy(Object src, int srcPos,
2940// Object dest, int destPos,
2941// int length)
2942void IntrinsicLocationsBuilderMIPS::VisitSystemArrayCopyChar(HInvoke* invoke) {
2943 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2944 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2945 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2946
2947 // As long as we are checking, we might as well check to see if the src and dest
2948 // positions are >= 0.
2949 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2950 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
2951 // We will have to fail anyways.
2952 return;
2953 }
2954
2955 // And since we are already checking, check the length too.
2956 if (length != nullptr) {
2957 int32_t len = length->GetValue();
2958 if (len < 0) {
2959 // Just call as normal.
2960 return;
2961 }
2962 }
2963
2964 // Okay, it is safe to generate inline code.
2965 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002966 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
Chris Larsen2f6ad9d2017-03-23 15:37:03 -07002967 // arraycopy(Object src, int srcPos, Object dest, int destPos, int length).
2968 locations->SetInAt(0, Location::RequiresRegister());
2969 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
2970 locations->SetInAt(2, Location::RequiresRegister());
2971 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
2972 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
2973
2974 locations->AddTemp(Location::RequiresRegister());
2975 locations->AddTemp(Location::RequiresRegister());
2976 locations->AddTemp(Location::RequiresRegister());
2977}
2978
2979// Utility routine to verify that "length(input) - pos >= length"
2980static void EnoughItems(MipsAssembler* assembler,
2981 Register length_input_minus_pos,
2982 Location length,
2983 SlowPathCodeMIPS* slow_path) {
2984 if (length.IsConstant()) {
2985 int32_t length_constant = length.GetConstant()->AsIntConstant()->GetValue();
2986
2987 if (IsInt<16>(length_constant)) {
2988 __ Slti(TMP, length_input_minus_pos, length_constant);
2989 __ Bnez(TMP, slow_path->GetEntryLabel());
2990 } else {
2991 __ LoadConst32(TMP, length_constant);
2992 __ Blt(length_input_minus_pos, TMP, slow_path->GetEntryLabel());
2993 }
2994 } else {
2995 __ Blt(length_input_minus_pos, length.AsRegister<Register>(), slow_path->GetEntryLabel());
2996 }
2997}
2998
2999static void CheckPosition(MipsAssembler* assembler,
3000 Location pos,
3001 Register input,
3002 Location length,
3003 SlowPathCodeMIPS* slow_path,
3004 bool length_is_input_length = false) {
3005 // Where is the length in the Array?
3006 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
3007
3008 // Calculate length(input) - pos.
3009 if (pos.IsConstant()) {
3010 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
3011 if (pos_const == 0) {
3012 if (!length_is_input_length) {
3013 // Check that length(input) >= length.
3014 __ LoadFromOffset(kLoadWord, AT, input, length_offset);
3015 EnoughItems(assembler, AT, length, slow_path);
3016 }
3017 } else {
3018 // Check that (length(input) - pos) >= zero.
3019 __ LoadFromOffset(kLoadWord, AT, input, length_offset);
3020 DCHECK_GT(pos_const, 0);
3021 __ Addiu32(AT, AT, -pos_const, TMP);
3022 __ Bltz(AT, slow_path->GetEntryLabel());
3023
3024 // Verify that (length(input) - pos) >= length.
3025 EnoughItems(assembler, AT, length, slow_path);
3026 }
3027 } else if (length_is_input_length) {
3028 // The only way the copy can succeed is if pos is zero.
3029 Register pos_reg = pos.AsRegister<Register>();
3030 __ Bnez(pos_reg, slow_path->GetEntryLabel());
3031 } else {
3032 // Verify that pos >= 0.
3033 Register pos_reg = pos.AsRegister<Register>();
3034 __ Bltz(pos_reg, slow_path->GetEntryLabel());
3035
3036 // Check that (length(input) - pos) >= zero.
3037 __ LoadFromOffset(kLoadWord, AT, input, length_offset);
3038 __ Subu(AT, AT, pos_reg);
3039 __ Bltz(AT, slow_path->GetEntryLabel());
3040
3041 // Verify that (length(input) - pos) >= length.
3042 EnoughItems(assembler, AT, length, slow_path);
3043 }
3044}
3045
3046void IntrinsicCodeGeneratorMIPS::VisitSystemArrayCopyChar(HInvoke* invoke) {
3047 MipsAssembler* assembler = GetAssembler();
3048 LocationSummary* locations = invoke->GetLocations();
3049
3050 Register src = locations->InAt(0).AsRegister<Register>();
3051 Location src_pos = locations->InAt(1);
3052 Register dest = locations->InAt(2).AsRegister<Register>();
3053 Location dest_pos = locations->InAt(3);
3054 Location length = locations->InAt(4);
3055
3056 MipsLabel loop;
3057
3058 Register dest_base = locations->GetTemp(0).AsRegister<Register>();
3059 Register src_base = locations->GetTemp(1).AsRegister<Register>();
3060 Register count = locations->GetTemp(2).AsRegister<Register>();
3061
3062 SlowPathCodeMIPS* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS(invoke);
3063 codegen_->AddSlowPath(slow_path);
3064
3065 // Bail out if the source and destination are the same (to handle overlap).
3066 __ Beq(src, dest, slow_path->GetEntryLabel());
3067
3068 // Bail out if the source is null.
3069 __ Beqz(src, slow_path->GetEntryLabel());
3070
3071 // Bail out if the destination is null.
3072 __ Beqz(dest, slow_path->GetEntryLabel());
3073
3074 // Load length into register for count.
3075 if (length.IsConstant()) {
3076 __ LoadConst32(count, length.GetConstant()->AsIntConstant()->GetValue());
3077 } else {
3078 // If the length is negative, bail out.
3079 // We have already checked in the LocationsBuilder for the constant case.
3080 __ Bltz(length.AsRegister<Register>(), slow_path->GetEntryLabel());
3081
3082 __ Move(count, length.AsRegister<Register>());
3083 }
3084
3085 // Validity checks: source.
3086 CheckPosition(assembler, src_pos, src, Location::RegisterLocation(count), slow_path);
3087
3088 // Validity checks: dest.
3089 CheckPosition(assembler, dest_pos, dest, Location::RegisterLocation(count), slow_path);
3090
3091 // If count is zero, we're done.
3092 __ Beqz(count, slow_path->GetExitLabel());
3093
3094 // Okay, everything checks out. Finally time to do the copy.
3095 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003096 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Chris Larsen2f6ad9d2017-03-23 15:37:03 -07003097 DCHECK_EQ(char_size, 2u);
3098
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003099 const size_t char_shift = DataType::SizeShift(DataType::Type::kUint16);
Chris Larsen2f6ad9d2017-03-23 15:37:03 -07003100
3101 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
3102
3103 // Calculate source and destination addresses.
3104 if (src_pos.IsConstant()) {
3105 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
3106
3107 __ Addiu32(src_base, src, data_offset + char_size * src_pos_const, TMP);
3108 } else {
3109 __ Addiu32(src_base, src, data_offset, TMP);
3110 __ ShiftAndAdd(src_base, src_pos.AsRegister<Register>(), src_base, char_shift);
3111 }
3112 if (dest_pos.IsConstant()) {
3113 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
3114
3115 __ Addiu32(dest_base, dest, data_offset + char_size * dest_pos_const, TMP);
3116 } else {
3117 __ Addiu32(dest_base, dest, data_offset, TMP);
3118 __ ShiftAndAdd(dest_base, dest_pos.AsRegister<Register>(), dest_base, char_shift);
3119 }
3120
3121 __ Bind(&loop);
3122 __ Lh(TMP, src_base, 0);
3123 __ Addiu(src_base, src_base, char_size);
3124 __ Addiu(count, count, -1);
3125 __ Sh(TMP, dest_base, 0);
3126 __ Addiu(dest_base, dest_base, char_size);
3127 __ Bnez(count, &loop);
3128
3129 __ Bind(slow_path->GetExitLabel());
3130}
3131
Chris Larsen5633ce72017-04-10 15:47:40 -07003132// long java.lang.Integer.valueOf(long)
3133void IntrinsicLocationsBuilderMIPS::VisitIntegerValueOf(HInvoke* invoke) {
3134 InvokeRuntimeCallingConvention calling_convention;
3135 IntrinsicVisitor::ComputeIntegerValueOfLocations(
3136 invoke,
3137 codegen_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003138 calling_convention.GetReturnLocation(DataType::Type::kReference),
Chris Larsen5633ce72017-04-10 15:47:40 -07003139 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3140}
3141
3142void IntrinsicCodeGeneratorMIPS::VisitIntegerValueOf(HInvoke* invoke) {
3143 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
3144 LocationSummary* locations = invoke->GetLocations();
3145 MipsAssembler* assembler = GetAssembler();
3146 InstructionCodeGeneratorMIPS* icodegen =
3147 down_cast<InstructionCodeGeneratorMIPS*>(codegen_->GetInstructionVisitor());
3148
3149 Register out = locations->Out().AsRegister<Register>();
3150 InvokeRuntimeCallingConvention calling_convention;
3151 if (invoke->InputAt(0)->IsConstant()) {
3152 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
3153 if (value >= info.low && value <= info.high) {
3154 // Just embed the j.l.Integer in the code.
3155 ScopedObjectAccess soa(Thread::Current());
3156 mirror::Object* boxed = info.cache->Get(value + (-info.low));
3157 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
3158 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
3159 __ LoadConst32(out, address);
3160 } else {
3161 // Allocate and initialize a new j.l.Integer.
3162 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
3163 // JIT object table.
3164 uint32_t address =
3165 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3166 __ LoadConst32(calling_convention.GetRegisterAt(0), address);
3167 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3168 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3169 __ StoreConstToOffset(kStoreWord, value, out, info.value_offset, TMP);
3170 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3171 // one.
3172 icodegen->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3173 }
3174 } else {
3175 Register in = locations->InAt(0).AsRegister<Register>();
3176 MipsLabel allocate, done;
3177 int32_t count = static_cast<uint32_t>(info.high) - info.low + 1;
3178
3179 // Is (info.low <= in) && (in <= info.high)?
3180 __ Addiu32(out, in, -info.low);
3181 // As unsigned quantities is out < (info.high - info.low + 1)?
3182 if (IsInt<16>(count)) {
3183 __ Sltiu(AT, out, count);
3184 } else {
3185 __ LoadConst32(AT, count);
3186 __ Sltu(AT, out, AT);
3187 }
3188 // Branch if out >= (info.high - info.low + 1).
3189 // This means that "in" is outside of the range [info.low, info.high].
3190 __ Beqz(AT, &allocate);
3191
3192 // If the value is within the bounds, load the j.l.Integer directly from the array.
3193 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3194 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
3195 __ LoadConst32(TMP, data_offset + address);
3196 __ ShiftAndAdd(out, out, TMP, TIMES_4);
3197 __ Lw(out, out, 0);
3198 __ MaybeUnpoisonHeapReference(out);
3199 __ B(&done);
3200
3201 __ Bind(&allocate);
3202 // Otherwise allocate and initialize a new j.l.Integer.
3203 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3204 __ LoadConst32(calling_convention.GetRegisterAt(0), address);
3205 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3206 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3207 __ StoreToOffset(kStoreWord, in, out, info.value_offset);
3208 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3209 // one.
3210 icodegen->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3211 __ Bind(&done);
3212 }
3213}
3214
Chris Larsen2714fe62016-02-11 14:23:53 -08003215// Unimplemented intrinsics.
3216
Aart Bik2f9fcc92016-03-01 15:16:54 -08003217UNIMPLEMENTED_INTRINSIC(MIPS, MathCeil)
3218UNIMPLEMENTED_INTRINSIC(MIPS, MathFloor)
3219UNIMPLEMENTED_INTRINSIC(MIPS, MathRint)
3220UNIMPLEMENTED_INTRINSIC(MIPS, MathRoundDouble)
Alexey Frunze15958152017-02-09 19:08:30 -08003221UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetLongVolatile);
3222UNIMPLEMENTED_INTRINSIC(MIPS, UnsafePutLongVolatile);
Aart Bik2f9fcc92016-03-01 15:16:54 -08003223UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeCASLong)
Chris Larsen701566a2015-10-27 15:29:13 -07003224
Aart Bik2f9fcc92016-03-01 15:16:54 -08003225UNIMPLEMENTED_INTRINSIC(MIPS, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08003226UNIMPLEMENTED_INTRINSIC(MIPS, SystemArrayCopy)
Aart Bik3f67e692016-01-15 14:35:12 -08003227
Aart Bikff7d89c2016-11-07 08:49:28 -08003228UNIMPLEMENTED_INTRINSIC(MIPS, StringStringIndexOf);
3229UNIMPLEMENTED_INTRINSIC(MIPS, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003230UNIMPLEMENTED_INTRINSIC(MIPS, StringBufferAppend);
3231UNIMPLEMENTED_INTRINSIC(MIPS, StringBufferLength);
3232UNIMPLEMENTED_INTRINSIC(MIPS, StringBufferToString);
3233UNIMPLEMENTED_INTRINSIC(MIPS, StringBuilderAppend);
3234UNIMPLEMENTED_INTRINSIC(MIPS, StringBuilderLength);
3235UNIMPLEMENTED_INTRINSIC(MIPS, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003236
Aart Bik0e54c012016-03-04 12:08:31 -08003237// 1.8.
3238UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndAddInt)
3239UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndAddLong)
3240UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndSetInt)
3241UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndSetLong)
3242UNIMPLEMENTED_INTRINSIC(MIPS, UnsafeGetAndSetObject)
Chris Larsen701566a2015-10-27 15:29:13 -07003243
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003244UNIMPLEMENTED_INTRINSIC(MIPS, ThreadInterrupted)
3245
Aart Bik0e54c012016-03-04 12:08:31 -08003246UNREACHABLE_INTRINSICS(MIPS)
Chris Larsen2714fe62016-02-11 14:23:53 -08003247
Chris Larsen701566a2015-10-27 15:29:13 -07003248#undef __
3249
3250} // namespace mips
3251} // namespace art