blob: ca9a9dd3665e934765becce5dfab210dc5254a25 [file] [log] [blame]
Chris Larsen701566a2015-10-27 15:29:13 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "intrinsics_mips.h"
18
19#include "arch/mips/instruction_set_features_mips.h"
20#include "art_method.h"
21#include "code_generator_mips.h"
22#include "entrypoints/quick/quick_entrypoints.h"
23#include "intrinsics.h"
24#include "mirror/array-inl.h"
25#include "mirror/string.h"
26#include "thread.h"
27#include "utils/mips/assembler_mips.h"
28#include "utils/mips/constants_mips.h"
29
30namespace art {
31
32namespace mips {
33
34IntrinsicLocationsBuilderMIPS::IntrinsicLocationsBuilderMIPS(CodeGeneratorMIPS* codegen)
35 : arena_(codegen->GetGraph()->GetArena()) {
36}
37
38MipsAssembler* IntrinsicCodeGeneratorMIPS::GetAssembler() {
39 return reinterpret_cast<MipsAssembler*>(codegen_->GetAssembler());
40}
41
42ArenaAllocator* IntrinsicCodeGeneratorMIPS::GetAllocator() {
43 return codegen_->GetGraph()->GetArena();
44}
45
Alexey Frunzebb9863a2016-01-11 15:51:16 -080046inline bool IntrinsicCodeGeneratorMIPS::IsR2OrNewer() const {
Chris Larsene16ce5a2015-11-18 12:30:20 -080047 return codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
48}
49
Alexey Frunzebb9863a2016-01-11 15:51:16 -080050inline bool IntrinsicCodeGeneratorMIPS::IsR6() const {
Chris Larsene16ce5a2015-11-18 12:30:20 -080051 return codegen_->GetInstructionSetFeatures().IsR6();
52}
53
Alexey Frunzebb9863a2016-01-11 15:51:16 -080054inline bool IntrinsicCodeGeneratorMIPS::Is32BitFPU() const {
55 return codegen_->GetInstructionSetFeatures().Is32BitFloatingPoint();
56}
57
Chris Larsen701566a2015-10-27 15:29:13 -070058#define __ codegen->GetAssembler()->
59
60static void MoveFromReturnRegister(Location trg,
61 Primitive::Type type,
62 CodeGeneratorMIPS* codegen) {
63 if (!trg.IsValid()) {
64 DCHECK_EQ(type, Primitive::kPrimVoid);
65 return;
66 }
67
68 DCHECK_NE(type, Primitive::kPrimVoid);
69
70 if (Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) {
71 Register trg_reg = trg.AsRegister<Register>();
72 if (trg_reg != V0) {
73 __ Move(V0, trg_reg);
74 }
75 } else {
76 FRegister trg_reg = trg.AsFpuRegister<FRegister>();
77 if (trg_reg != F0) {
78 if (type == Primitive::kPrimFloat) {
79 __ MovS(F0, trg_reg);
80 } else {
81 __ MovD(F0, trg_reg);
82 }
83 }
84 }
85}
86
87static void MoveArguments(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
88 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
89 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
90}
91
92// Slow-path for fallback (calling the managed code to handle the
93// intrinsic) in an intrinsified call. This will copy the arguments
94// into the positions for a regular call.
95//
96// Note: The actual parameters are required to be in the locations
97// given by the invoke's location summary. If an intrinsic
98// modifies those locations before a slowpath call, they must be
99// restored!
100class IntrinsicSlowPathMIPS : public SlowPathCodeMIPS {
101 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000102 explicit IntrinsicSlowPathMIPS(HInvoke* invoke) : SlowPathCodeMIPS(invoke), invoke_(invoke) { }
Chris Larsen701566a2015-10-27 15:29:13 -0700103
104 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
105 CodeGeneratorMIPS* codegen = down_cast<CodeGeneratorMIPS*>(codegen_in);
106
107 __ Bind(GetEntryLabel());
108
109 SaveLiveRegisters(codegen, invoke_->GetLocations());
110
111 MoveArguments(invoke_, codegen);
112
113 if (invoke_->IsInvokeStaticOrDirect()) {
114 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(),
115 Location::RegisterLocation(A0));
Chris Larsen701566a2015-10-27 15:29:13 -0700116 } else {
Chris Larsen3acee732015-11-18 13:31:08 -0800117 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), Location::RegisterLocation(A0));
Chris Larsen701566a2015-10-27 15:29:13 -0700118 }
Chris Larsen3acee732015-11-18 13:31:08 -0800119 codegen->RecordPcInfo(invoke_, invoke_->GetDexPc(), this);
Chris Larsen701566a2015-10-27 15:29:13 -0700120
121 // Copy the result back to the expected output.
122 Location out = invoke_->GetLocations()->Out();
123 if (out.IsValid()) {
124 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
125 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
126 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
127 }
128
129 RestoreLiveRegisters(codegen, invoke_->GetLocations());
130 __ B(GetExitLabel());
131 }
132
133 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathMIPS"; }
134
135 private:
136 // The instruction where this slow path is happening.
137 HInvoke* const invoke_;
138
139 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathMIPS);
140};
141
142#undef __
143
144bool IntrinsicLocationsBuilderMIPS::TryDispatch(HInvoke* invoke) {
145 Dispatch(invoke);
146 LocationSummary* res = invoke->GetLocations();
147 return res != nullptr && res->Intrinsified();
148}
149
150#define __ assembler->
151
Chris Larsen3f8bf652015-10-28 10:08:56 -0700152static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
153 LocationSummary* locations = new (arena) LocationSummary(invoke,
154 LocationSummary::kNoCall,
155 kIntrinsified);
156 locations->SetInAt(0, Location::RequiresFpuRegister());
157 locations->SetOut(Location::RequiresRegister());
158}
159
160static void MoveFPToInt(LocationSummary* locations, bool is64bit, MipsAssembler* assembler) {
161 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
162
163 if (is64bit) {
164 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
165 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
166
167 __ Mfc1(out_lo, in);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800168 __ MoveFromFpuHigh(out_hi, in);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700169 } else {
170 Register out = locations->Out().AsRegister<Register>();
171
172 __ Mfc1(out, in);
173 }
174}
175
176// long java.lang.Double.doubleToRawLongBits(double)
177void IntrinsicLocationsBuilderMIPS::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
178 CreateFPToIntLocations(arena_, invoke);
179}
180
181void IntrinsicCodeGeneratorMIPS::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000182 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700183}
184
185// int java.lang.Float.floatToRawIntBits(float)
186void IntrinsicLocationsBuilderMIPS::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
187 CreateFPToIntLocations(arena_, invoke);
188}
189
190void IntrinsicCodeGeneratorMIPS::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000191 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700192}
193
194static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
195 LocationSummary* locations = new (arena) LocationSummary(invoke,
196 LocationSummary::kNoCall,
197 kIntrinsified);
198 locations->SetInAt(0, Location::RequiresRegister());
199 locations->SetOut(Location::RequiresFpuRegister());
200}
201
202static void MoveIntToFP(LocationSummary* locations, bool is64bit, MipsAssembler* assembler) {
203 FRegister out = locations->Out().AsFpuRegister<FRegister>();
204
205 if (is64bit) {
206 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
207 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
208
209 __ Mtc1(in_lo, out);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800210 __ MoveToFpuHigh(in_hi, out);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700211 } else {
212 Register in = locations->InAt(0).AsRegister<Register>();
213
214 __ Mtc1(in, out);
215 }
216}
217
218// double java.lang.Double.longBitsToDouble(long)
219void IntrinsicLocationsBuilderMIPS::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
220 CreateIntToFPLocations(arena_, invoke);
221}
222
223void IntrinsicCodeGeneratorMIPS::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000224 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700225}
226
227// float java.lang.Float.intBitsToFloat(int)
228void IntrinsicLocationsBuilderMIPS::VisitFloatIntBitsToFloat(HInvoke* invoke) {
229 CreateIntToFPLocations(arena_, invoke);
230}
231
232void IntrinsicCodeGeneratorMIPS::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000233 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700234}
235
Chris Larsen86829602015-11-18 12:27:52 -0800236static void CreateIntToIntLocations(ArenaAllocator* arena,
237 HInvoke* invoke,
238 Location::OutputOverlap overlaps = Location::kNoOutputOverlap) {
Chris Larsen3f8bf652015-10-28 10:08:56 -0700239 LocationSummary* locations = new (arena) LocationSummary(invoke,
240 LocationSummary::kNoCall,
241 kIntrinsified);
242 locations->SetInAt(0, Location::RequiresRegister());
Chris Larsen86829602015-11-18 12:27:52 -0800243 locations->SetOut(Location::RequiresRegister(), overlaps);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700244}
245
Chris Larsen70014c82015-11-18 12:26:08 -0800246static void GenReverse(LocationSummary* locations,
247 Primitive::Type type,
248 bool isR2OrNewer,
249 bool isR6,
250 bool reverseBits,
251 MipsAssembler* assembler) {
Chris Larsen3f8bf652015-10-28 10:08:56 -0700252 DCHECK(type == Primitive::kPrimShort ||
253 type == Primitive::kPrimInt ||
254 type == Primitive::kPrimLong);
Chris Larsen70014c82015-11-18 12:26:08 -0800255 DCHECK(type != Primitive::kPrimShort || !reverseBits);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700256
257 if (type == Primitive::kPrimShort) {
258 Register in = locations->InAt(0).AsRegister<Register>();
259 Register out = locations->Out().AsRegister<Register>();
260
261 if (isR2OrNewer) {
262 __ Wsbh(out, in);
263 __ Seh(out, out);
264 } else {
265 __ Sll(TMP, in, 24);
266 __ Sra(TMP, TMP, 16);
267 __ Sll(out, in, 16);
268 __ Srl(out, out, 24);
269 __ Or(out, out, TMP);
270 }
271 } else if (type == Primitive::kPrimInt) {
272 Register in = locations->InAt(0).AsRegister<Register>();
273 Register out = locations->Out().AsRegister<Register>();
274
275 if (isR2OrNewer) {
276 __ Rotr(out, in, 16);
277 __ Wsbh(out, out);
278 } else {
279 // MIPS32r1
280 // __ Rotr(out, in, 16);
281 __ Sll(TMP, in, 16);
282 __ Srl(out, in, 16);
283 __ Or(out, out, TMP);
284 // __ Wsbh(out, out);
285 __ LoadConst32(AT, 0x00FF00FF);
286 __ And(TMP, out, AT);
287 __ Sll(TMP, TMP, 8);
288 __ Srl(out, out, 8);
289 __ And(out, out, AT);
290 __ Or(out, out, TMP);
291 }
Chris Larsen70014c82015-11-18 12:26:08 -0800292 if (reverseBits) {
293 if (isR6) {
294 __ Bitswap(out, out);
295 } else {
296 __ LoadConst32(AT, 0x0F0F0F0F);
297 __ And(TMP, out, AT);
298 __ Sll(TMP, TMP, 4);
299 __ Srl(out, out, 4);
300 __ And(out, out, AT);
301 __ Or(out, TMP, out);
302 __ LoadConst32(AT, 0x33333333);
303 __ And(TMP, out, AT);
304 __ Sll(TMP, TMP, 2);
305 __ Srl(out, out, 2);
306 __ And(out, out, AT);
307 __ Or(out, TMP, out);
308 __ LoadConst32(AT, 0x55555555);
309 __ And(TMP, out, AT);
310 __ Sll(TMP, TMP, 1);
311 __ Srl(out, out, 1);
312 __ And(out, out, AT);
313 __ Or(out, TMP, out);
314 }
315 }
Chris Larsen3f8bf652015-10-28 10:08:56 -0700316 } else if (type == Primitive::kPrimLong) {
317 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
318 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
319 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
320 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
321
322 if (isR2OrNewer) {
323 __ Rotr(AT, in_hi, 16);
324 __ Rotr(TMP, in_lo, 16);
325 __ Wsbh(out_lo, AT);
326 __ Wsbh(out_hi, TMP);
327 } else {
328 // When calling CreateIntToIntLocations() we promised that the
329 // use of the out_lo/out_hi wouldn't overlap with the use of
330 // in_lo/in_hi. Be very careful not to write to out_lo/out_hi
331 // until we're completely done reading from in_lo/in_hi.
332 // __ Rotr(TMP, in_lo, 16);
333 __ Sll(TMP, in_lo, 16);
334 __ Srl(AT, in_lo, 16);
335 __ Or(TMP, TMP, AT); // Hold in TMP until it's safe
336 // to write to out_hi.
337 // __ Rotr(out_lo, in_hi, 16);
338 __ Sll(AT, in_hi, 16);
339 __ Srl(out_lo, in_hi, 16); // Here we are finally done reading
340 // from in_lo/in_hi so it's okay to
341 // write to out_lo/out_hi.
342 __ Or(out_lo, out_lo, AT);
343 // __ Wsbh(out_hi, out_hi);
344 __ LoadConst32(AT, 0x00FF00FF);
345 __ And(out_hi, TMP, AT);
346 __ Sll(out_hi, out_hi, 8);
347 __ Srl(TMP, TMP, 8);
348 __ And(TMP, TMP, AT);
349 __ Or(out_hi, out_hi, TMP);
350 // __ Wsbh(out_lo, out_lo);
351 __ And(TMP, out_lo, AT); // AT already holds the correct mask value
352 __ Sll(TMP, TMP, 8);
353 __ Srl(out_lo, out_lo, 8);
354 __ And(out_lo, out_lo, AT);
355 __ Or(out_lo, out_lo, TMP);
356 }
Chris Larsen70014c82015-11-18 12:26:08 -0800357 if (reverseBits) {
358 if (isR6) {
359 __ Bitswap(out_hi, out_hi);
360 __ Bitswap(out_lo, out_lo);
361 } else {
362 __ LoadConst32(AT, 0x0F0F0F0F);
363 __ And(TMP, out_hi, AT);
364 __ Sll(TMP, TMP, 4);
365 __ Srl(out_hi, out_hi, 4);
366 __ And(out_hi, out_hi, AT);
367 __ Or(out_hi, TMP, out_hi);
368 __ And(TMP, out_lo, AT);
369 __ Sll(TMP, TMP, 4);
370 __ Srl(out_lo, out_lo, 4);
371 __ And(out_lo, out_lo, AT);
372 __ Or(out_lo, TMP, out_lo);
373 __ LoadConst32(AT, 0x33333333);
374 __ And(TMP, out_hi, AT);
375 __ Sll(TMP, TMP, 2);
376 __ Srl(out_hi, out_hi, 2);
377 __ And(out_hi, out_hi, AT);
378 __ Or(out_hi, TMP, out_hi);
379 __ And(TMP, out_lo, AT);
380 __ Sll(TMP, TMP, 2);
381 __ Srl(out_lo, out_lo, 2);
382 __ And(out_lo, out_lo, AT);
383 __ Or(out_lo, TMP, out_lo);
384 __ LoadConst32(AT, 0x55555555);
385 __ And(TMP, out_hi, AT);
386 __ Sll(TMP, TMP, 1);
387 __ Srl(out_hi, out_hi, 1);
388 __ And(out_hi, out_hi, AT);
389 __ Or(out_hi, TMP, out_hi);
390 __ And(TMP, out_lo, AT);
391 __ Sll(TMP, TMP, 1);
392 __ Srl(out_lo, out_lo, 1);
393 __ And(out_lo, out_lo, AT);
394 __ Or(out_lo, TMP, out_lo);
395 }
396 }
Chris Larsen3f8bf652015-10-28 10:08:56 -0700397 }
398}
399
400// int java.lang.Integer.reverseBytes(int)
401void IntrinsicLocationsBuilderMIPS::VisitIntegerReverseBytes(HInvoke* invoke) {
402 CreateIntToIntLocations(arena_, invoke);
403}
404
405void IntrinsicCodeGeneratorMIPS::VisitIntegerReverseBytes(HInvoke* invoke) {
Chris Larsen70014c82015-11-18 12:26:08 -0800406 GenReverse(invoke->GetLocations(),
407 Primitive::kPrimInt,
Chris Larsene16ce5a2015-11-18 12:30:20 -0800408 IsR2OrNewer(),
409 IsR6(),
Chris Larsen70014c82015-11-18 12:26:08 -0800410 false,
411 GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700412}
413
414// long java.lang.Long.reverseBytes(long)
415void IntrinsicLocationsBuilderMIPS::VisitLongReverseBytes(HInvoke* invoke) {
416 CreateIntToIntLocations(arena_, invoke);
417}
418
419void IntrinsicCodeGeneratorMIPS::VisitLongReverseBytes(HInvoke* invoke) {
Chris Larsen70014c82015-11-18 12:26:08 -0800420 GenReverse(invoke->GetLocations(),
421 Primitive::kPrimLong,
Chris Larsene16ce5a2015-11-18 12:30:20 -0800422 IsR2OrNewer(),
423 IsR6(),
Chris Larsen70014c82015-11-18 12:26:08 -0800424 false,
425 GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700426}
427
428// short java.lang.Short.reverseBytes(short)
429void IntrinsicLocationsBuilderMIPS::VisitShortReverseBytes(HInvoke* invoke) {
430 CreateIntToIntLocations(arena_, invoke);
431}
432
433void IntrinsicCodeGeneratorMIPS::VisitShortReverseBytes(HInvoke* invoke) {
Chris Larsen70014c82015-11-18 12:26:08 -0800434 GenReverse(invoke->GetLocations(),
435 Primitive::kPrimShort,
Chris Larsene16ce5a2015-11-18 12:30:20 -0800436 IsR2OrNewer(),
437 IsR6(),
Chris Larsen70014c82015-11-18 12:26:08 -0800438 false,
439 GetAssembler());
440}
441
Chris Larsene3845472015-11-18 12:27:15 -0800442static void GenNumberOfLeadingZeroes(LocationSummary* locations,
443 bool is64bit,
444 bool isR6,
445 MipsAssembler* assembler) {
446 Register out = locations->Out().AsRegister<Register>();
447 if (is64bit) {
448 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
449 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
450
451 if (isR6) {
452 __ ClzR6(AT, in_hi);
453 __ ClzR6(TMP, in_lo);
454 __ Seleqz(TMP, TMP, in_hi);
455 } else {
456 __ ClzR2(AT, in_hi);
457 __ ClzR2(TMP, in_lo);
458 __ Movn(TMP, ZERO, in_hi);
459 }
460 __ Addu(out, AT, TMP);
461 } else {
462 Register in = locations->InAt(0).AsRegister<Register>();
463
464 if (isR6) {
465 __ ClzR6(out, in);
466 } else {
467 __ ClzR2(out, in);
468 }
469 }
470}
471
472// int java.lang.Integer.numberOfLeadingZeros(int i)
473void IntrinsicLocationsBuilderMIPS::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
474 CreateIntToIntLocations(arena_, invoke);
475}
476
477void IntrinsicCodeGeneratorMIPS::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Chris Larsene16ce5a2015-11-18 12:30:20 -0800478 GenNumberOfLeadingZeroes(invoke->GetLocations(), false, IsR6(), GetAssembler());
Chris Larsene3845472015-11-18 12:27:15 -0800479}
480
481// int java.lang.Long.numberOfLeadingZeros(long i)
482void IntrinsicLocationsBuilderMIPS::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
483 CreateIntToIntLocations(arena_, invoke);
484}
485
486void IntrinsicCodeGeneratorMIPS::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Chris Larsene16ce5a2015-11-18 12:30:20 -0800487 GenNumberOfLeadingZeroes(invoke->GetLocations(), 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,
493 bool isR2OrNewer,
494 MipsAssembler* assembler) {
495 Register out = locations->Out().AsRegister<Register>();
496 Register in_lo;
497 Register in;
498
499 if (is64bit) {
500 MipsLabel done;
501 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
502
503 in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
504
505 // If in_lo is zero then count the number of trailing zeroes in in_hi;
506 // otherwise count the number of trailing zeroes in in_lo.
507 // AT = in_lo ? in_lo : in_hi;
508 if (isR6) {
509 __ Seleqz(out, in_hi, in_lo);
510 __ Selnez(TMP, in_lo, in_lo);
511 __ Or(out, out, TMP);
512 } else {
513 __ Movz(out, in_hi, in_lo);
514 __ Movn(out, in_lo, in_lo);
515 }
516
517 in = out;
518 } else {
519 in = locations->InAt(0).AsRegister<Register>();
520 // Give in_lo a dummy value to keep the compiler from complaining.
521 // Since we only get here in the 32-bit case, this value will never
522 // be used.
523 in_lo = in;
524 }
525
526 // We don't have an instruction to count the number of trailing zeroes.
527 // Start by flipping the bits end-for-end so we can count the number of
528 // leading zeroes instead.
529 if (isR2OrNewer) {
530 __ Rotr(out, in, 16);
531 __ Wsbh(out, out);
532 } else {
533 // MIPS32r1
534 // __ Rotr(out, in, 16);
535 __ Sll(TMP, in, 16);
536 __ Srl(out, in, 16);
537 __ Or(out, out, TMP);
538 // __ Wsbh(out, out);
539 __ LoadConst32(AT, 0x00FF00FF);
540 __ And(TMP, out, AT);
541 __ Sll(TMP, TMP, 8);
542 __ Srl(out, out, 8);
543 __ And(out, out, AT);
544 __ Or(out, out, TMP);
545 }
546
547 if (isR6) {
548 __ Bitswap(out, out);
549 __ ClzR6(out, out);
550 } else {
551 __ LoadConst32(AT, 0x0F0F0F0F);
552 __ And(TMP, out, AT);
553 __ Sll(TMP, TMP, 4);
554 __ Srl(out, out, 4);
555 __ And(out, out, AT);
556 __ Or(out, TMP, out);
557 __ LoadConst32(AT, 0x33333333);
558 __ And(TMP, out, AT);
559 __ Sll(TMP, TMP, 2);
560 __ Srl(out, out, 2);
561 __ And(out, out, AT);
562 __ Or(out, TMP, out);
563 __ LoadConst32(AT, 0x55555555);
564 __ And(TMP, out, AT);
565 __ Sll(TMP, TMP, 1);
566 __ Srl(out, out, 1);
567 __ And(out, out, AT);
568 __ Or(out, TMP, out);
569 __ ClzR2(out, out);
570 }
571
572 if (is64bit) {
573 // If in_lo is zero, then we counted the number of trailing zeroes in in_hi so we must add the
574 // number of trailing zeroes in in_lo (32) to get the correct final count
575 __ LoadConst32(TMP, 32);
576 if (isR6) {
577 __ Seleqz(TMP, TMP, in_lo);
578 } else {
579 __ Movn(TMP, ZERO, in_lo);
580 }
581 __ Addu(out, out, TMP);
582 }
583}
584
585// int java.lang.Integer.numberOfTrailingZeros(int i)
586void IntrinsicLocationsBuilderMIPS::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
587 CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap);
588}
589
590void IntrinsicCodeGeneratorMIPS::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Chris Larsene16ce5a2015-11-18 12:30:20 -0800591 GenNumberOfTrailingZeroes(invoke->GetLocations(), false, IsR6(), IsR2OrNewer(), GetAssembler());
Chris Larsen86829602015-11-18 12:27:52 -0800592}
593
594// int java.lang.Long.numberOfTrailingZeros(long i)
595void IntrinsicLocationsBuilderMIPS::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
596 CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap);
597}
598
599void IntrinsicCodeGeneratorMIPS::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Chris Larsene16ce5a2015-11-18 12:30:20 -0800600 GenNumberOfTrailingZeroes(invoke->GetLocations(), true, IsR6(), IsR2OrNewer(), GetAssembler());
601}
602
603enum RotationDirection {
604 kRotateRight,
605 kRotateLeft,
606};
607
608static void GenRotate(HInvoke* invoke,
609 Primitive::Type type,
610 bool isR2OrNewer,
611 RotationDirection direction,
612 MipsAssembler* assembler) {
613 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
614
615 LocationSummary* locations = invoke->GetLocations();
616 if (invoke->InputAt(1)->IsIntConstant()) {
617 int32_t shift = static_cast<int32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue());
618 if (type == Primitive::kPrimInt) {
619 Register in = locations->InAt(0).AsRegister<Register>();
620 Register out = locations->Out().AsRegister<Register>();
621
622 shift &= 0x1f;
623 if (direction == kRotateLeft) {
624 shift = (32 - shift) & 0x1F;
625 }
626
627 if (isR2OrNewer) {
628 if ((shift != 0) || (out != in)) {
629 __ Rotr(out, in, shift);
630 }
631 } else {
632 if (shift == 0) {
633 if (out != in) {
634 __ Move(out, in);
635 }
636 } else {
637 __ Srl(AT, in, shift);
638 __ Sll(out, in, 32 - shift);
639 __ Or(out, out, AT);
640 }
641 }
642 } else { // Primitive::kPrimLong
643 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
644 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
645 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
646 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
647
648 shift &= 0x3f;
649 if (direction == kRotateLeft) {
650 shift = (64 - shift) & 0x3F;
651 }
652
653 if (shift == 0) {
654 __ Move(out_lo, in_lo);
655 __ Move(out_hi, in_hi);
656 } else if (shift == 32) {
657 __ Move(out_lo, in_hi);
658 __ Move(out_hi, in_lo);
659 } else if (shift < 32) {
660 __ Srl(AT, in_lo, shift);
661 __ Sll(out_lo, in_hi, 32 - shift);
662 __ Or(out_lo, out_lo, AT);
663 __ Srl(AT, in_hi, shift);
664 __ Sll(out_hi, in_lo, 32 - shift);
665 __ Or(out_hi, out_hi, AT);
666 } else {
667 __ Sll(AT, in_lo, 64 - shift);
668 __ Srl(out_lo, in_hi, shift - 32);
669 __ Or(out_lo, out_lo, AT);
670 __ Sll(AT, in_hi, 64 - shift);
671 __ Srl(out_hi, in_lo, shift - 32);
672 __ Or(out_hi, out_hi, AT);
673 }
674 }
675 } else { // !invoke->InputAt(1)->IsIntConstant()
676 Register shamt = locations->InAt(1).AsRegister<Register>();
677 if (type == Primitive::kPrimInt) {
678 Register in = locations->InAt(0).AsRegister<Register>();
679 Register out = locations->Out().AsRegister<Register>();
680
681 if (isR2OrNewer) {
682 if (direction == kRotateRight) {
683 __ Rotrv(out, in, shamt);
684 } else {
685 // negu tmp, shamt
686 __ Subu(TMP, ZERO, shamt);
687 __ Rotrv(out, in, TMP);
688 }
689 } else {
690 if (direction == kRotateRight) {
691 __ Srlv(AT, in, shamt);
692 __ Subu(TMP, ZERO, shamt);
693 __ Sllv(out, in, TMP);
694 __ Or(out, out, AT);
695 } else {
696 __ Sllv(AT, in, shamt);
697 __ Subu(TMP, ZERO, shamt);
698 __ Srlv(out, in, TMP);
699 __ Or(out, out, AT);
700 }
701 }
702 } else { // Primitive::kPrimLong
703 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
704 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
705 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
706 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
707
708 MipsLabel done;
709
710 if (direction == kRotateRight) {
711 __ Nor(TMP, ZERO, shamt);
712 __ Srlv(AT, in_lo, shamt);
713 __ Sll(out_lo, in_hi, 1);
714 __ Sllv(out_lo, out_lo, TMP);
715 __ Or(out_lo, out_lo, AT);
716 __ Srlv(AT, in_hi, shamt);
717 __ Sll(out_hi, in_lo, 1);
718 __ Sllv(out_hi, out_hi, TMP);
719 __ Or(out_hi, out_hi, AT);
720 } else {
721 __ Nor(TMP, ZERO, shamt);
722 __ Sllv(AT, in_lo, shamt);
723 __ Srl(out_lo, in_hi, 1);
724 __ Srlv(out_lo, out_lo, TMP);
725 __ Or(out_lo, out_lo, AT);
726 __ Sllv(AT, in_hi, shamt);
727 __ Srl(out_hi, in_lo, 1);
728 __ Srlv(out_hi, out_hi, TMP);
729 __ Or(out_hi, out_hi, AT);
730 }
731
732 __ Andi(TMP, shamt, 32);
733 __ Beqz(TMP, &done);
734 __ Move(TMP, out_hi);
735 __ Move(out_hi, out_lo);
736 __ Move(out_lo, TMP);
737
738 __ Bind(&done);
739 }
740 }
741}
742
743// int java.lang.Integer.rotateRight(int i, int distance)
744void IntrinsicLocationsBuilderMIPS::VisitIntegerRotateRight(HInvoke* invoke) {
745 LocationSummary* locations = new (arena_) LocationSummary(invoke,
746 LocationSummary::kNoCall,
747 kIntrinsified);
748 locations->SetInAt(0, Location::RequiresRegister());
749 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
750 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
751}
752
753void IntrinsicCodeGeneratorMIPS::VisitIntegerRotateRight(HInvoke* invoke) {
754 GenRotate(invoke, Primitive::kPrimInt, IsR2OrNewer(), kRotateRight, GetAssembler());
755}
756
757// long java.lang.Long.rotateRight(long i, int distance)
758void IntrinsicLocationsBuilderMIPS::VisitLongRotateRight(HInvoke* invoke) {
759 LocationSummary* locations = new (arena_) LocationSummary(invoke,
760 LocationSummary::kNoCall,
761 kIntrinsified);
762 locations->SetInAt(0, Location::RequiresRegister());
763 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
764 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
765}
766
767void IntrinsicCodeGeneratorMIPS::VisitLongRotateRight(HInvoke* invoke) {
768 GenRotate(invoke, Primitive::kPrimLong, IsR2OrNewer(), kRotateRight, GetAssembler());
769}
770
771// int java.lang.Integer.rotateLeft(int i, int distance)
772void IntrinsicLocationsBuilderMIPS::VisitIntegerRotateLeft(HInvoke* invoke) {
773 LocationSummary* locations = new (arena_) LocationSummary(invoke,
774 LocationSummary::kNoCall,
775 kIntrinsified);
776 locations->SetInAt(0, Location::RequiresRegister());
777 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
778 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
779}
780
781void IntrinsicCodeGeneratorMIPS::VisitIntegerRotateLeft(HInvoke* invoke) {
782 GenRotate(invoke, Primitive::kPrimInt, IsR2OrNewer(), kRotateLeft, GetAssembler());
783}
784
785// long java.lang.Long.rotateLeft(long i, int distance)
786void IntrinsicLocationsBuilderMIPS::VisitLongRotateLeft(HInvoke* invoke) {
787 LocationSummary* locations = new (arena_) LocationSummary(invoke,
788 LocationSummary::kNoCall,
789 kIntrinsified);
790 locations->SetInAt(0, Location::RequiresRegister());
791 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
792 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
793}
794
795void IntrinsicCodeGeneratorMIPS::VisitLongRotateLeft(HInvoke* invoke) {
796 GenRotate(invoke, Primitive::kPrimLong, IsR2OrNewer(), kRotateLeft, GetAssembler());
Chris Larsen86829602015-11-18 12:27:52 -0800797}
798
Chris Larsen70014c82015-11-18 12:26:08 -0800799// int java.lang.Integer.reverse(int)
800void IntrinsicLocationsBuilderMIPS::VisitIntegerReverse(HInvoke* invoke) {
801 CreateIntToIntLocations(arena_, invoke);
802}
803
804void IntrinsicCodeGeneratorMIPS::VisitIntegerReverse(HInvoke* invoke) {
805 GenReverse(invoke->GetLocations(),
806 Primitive::kPrimInt,
Chris Larsene16ce5a2015-11-18 12:30:20 -0800807 IsR2OrNewer(),
808 IsR6(),
Chris Larsen70014c82015-11-18 12:26:08 -0800809 true,
810 GetAssembler());
811}
812
813// long java.lang.Long.reverse(long)
814void IntrinsicLocationsBuilderMIPS::VisitLongReverse(HInvoke* invoke) {
815 CreateIntToIntLocations(arena_, invoke);
816}
817
818void IntrinsicCodeGeneratorMIPS::VisitLongReverse(HInvoke* invoke) {
819 GenReverse(invoke->GetLocations(),
820 Primitive::kPrimLong,
Chris Larsene16ce5a2015-11-18 12:30:20 -0800821 IsR2OrNewer(),
822 IsR6(),
Chris Larsen70014c82015-11-18 12:26:08 -0800823 true,
824 GetAssembler());
Chris Larsen3f8bf652015-10-28 10:08:56 -0700825}
826
Chris Larsen3acee732015-11-18 13:31:08 -0800827// byte libcore.io.Memory.peekByte(long address)
828void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekByte(HInvoke* invoke) {
829 CreateIntToIntLocations(arena_, invoke);
830}
831
832void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekByte(HInvoke* invoke) {
833 MipsAssembler* assembler = GetAssembler();
834 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
835 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
836
837 __ Lb(out, adr, 0);
838}
839
840// short libcore.io.Memory.peekShort(long address)
841void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekShortNative(HInvoke* invoke) {
842 CreateIntToIntLocations(arena_, invoke);
843}
844
845void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekShortNative(HInvoke* invoke) {
846 MipsAssembler* assembler = GetAssembler();
847 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
848 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
849
850 if (IsR6()) {
851 __ Lh(out, adr, 0);
852 } else if (IsR2OrNewer()) {
853 // Unlike for words, there are no lhl/lhr instructions to load
854 // unaligned halfwords so the code loads individual bytes, in case
855 // the address isn't halfword-aligned, and assembles them into a
856 // signed halfword.
857 __ Lb(AT, adr, 1); // This byte must be sign-extended.
858 __ Lb(out, adr, 0); // This byte can be either sign-extended, or
859 // zero-extended because the following
860 // instruction overwrites the sign bits.
861 __ Ins(out, AT, 8, 24);
862 } else {
863 __ Lbu(AT, adr, 0); // This byte must be zero-extended. If it's not
864 // the "or" instruction below will destroy the upper
865 // 24 bits of the final result.
866 __ Lb(out, adr, 1); // This byte must be sign-extended.
867 __ Sll(out, out, 8);
868 __ Or(out, out, AT);
869 }
870}
871
872// int libcore.io.Memory.peekInt(long address)
873void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekIntNative(HInvoke* invoke) {
874 CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap);
875}
876
877void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekIntNative(HInvoke* invoke) {
878 MipsAssembler* assembler = GetAssembler();
879 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
880 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
881
882 if (IsR6()) {
883 __ Lw(out, adr, 0);
884 } else {
885 __ Lwr(out, adr, 0);
886 __ Lwl(out, adr, 3);
887 }
888}
889
890// long libcore.io.Memory.peekLong(long address)
891void IntrinsicLocationsBuilderMIPS::VisitMemoryPeekLongNative(HInvoke* invoke) {
892 CreateIntToIntLocations(arena_, invoke, Location::kOutputOverlap);
893}
894
895void IntrinsicCodeGeneratorMIPS::VisitMemoryPeekLongNative(HInvoke* invoke) {
896 MipsAssembler* assembler = GetAssembler();
897 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
898 Register out_lo = invoke->GetLocations()->Out().AsRegisterPairLow<Register>();
899 Register out_hi = invoke->GetLocations()->Out().AsRegisterPairHigh<Register>();
900
901 if (IsR6()) {
902 __ Lw(out_lo, adr, 0);
903 __ Lw(out_hi, adr, 4);
904 } else {
905 __ Lwr(out_lo, adr, 0);
906 __ Lwl(out_lo, adr, 3);
907 __ Lwr(out_hi, adr, 4);
908 __ Lwl(out_hi, adr, 7);
909 }
910}
911
912static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
913 LocationSummary* locations = new (arena) LocationSummary(invoke,
914 LocationSummary::kNoCall,
915 kIntrinsified);
916 locations->SetInAt(0, Location::RequiresRegister());
917 locations->SetInAt(1, Location::RequiresRegister());
918}
919
920// void libcore.io.Memory.pokeByte(long address, byte value)
921void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeByte(HInvoke* invoke) {
922 CreateIntIntToVoidLocations(arena_, invoke);
923}
924
925void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeByte(HInvoke* invoke) {
926 MipsAssembler* assembler = GetAssembler();
927 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
928 Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>();
929
930 __ Sb(val, adr, 0);
931}
932
933// void libcore.io.Memory.pokeShort(long address, short value)
934void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeShortNative(HInvoke* invoke) {
935 CreateIntIntToVoidLocations(arena_, invoke);
936}
937
938void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeShortNative(HInvoke* invoke) {
939 MipsAssembler* assembler = GetAssembler();
940 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
941 Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>();
942
943 if (IsR6()) {
944 __ Sh(val, adr, 0);
945 } else {
946 // Unlike for words, there are no shl/shr instructions to store
947 // unaligned halfwords so the code stores individual bytes, in case
948 // the address isn't halfword-aligned.
949 __ Sb(val, adr, 0);
950 __ Srl(AT, val, 8);
951 __ Sb(AT, adr, 1);
952 }
953}
954
955// void libcore.io.Memory.pokeInt(long address, int value)
956void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeIntNative(HInvoke* invoke) {
957 CreateIntIntToVoidLocations(arena_, invoke);
958}
959
960void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeIntNative(HInvoke* invoke) {
961 MipsAssembler* assembler = GetAssembler();
962 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
963 Register val = invoke->GetLocations()->InAt(1).AsRegister<Register>();
964
965 if (IsR6()) {
966 __ Sw(val, adr, 0);
967 } else {
968 __ Swr(val, adr, 0);
969 __ Swl(val, adr, 3);
970 }
971}
972
973// void libcore.io.Memory.pokeLong(long address, long value)
974void IntrinsicLocationsBuilderMIPS::VisitMemoryPokeLongNative(HInvoke* invoke) {
975 CreateIntIntToVoidLocations(arena_, invoke);
976}
977
978void IntrinsicCodeGeneratorMIPS::VisitMemoryPokeLongNative(HInvoke* invoke) {
979 MipsAssembler* assembler = GetAssembler();
980 Register adr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
981 Register val_lo = invoke->GetLocations()->InAt(1).AsRegisterPairLow<Register>();
982 Register val_hi = invoke->GetLocations()->InAt(1).AsRegisterPairHigh<Register>();
983
984 if (IsR6()) {
985 __ Sw(val_lo, adr, 0);
986 __ Sw(val_hi, adr, 4);
987 } else {
988 __ Swr(val_lo, adr, 0);
989 __ Swl(val_lo, adr, 3);
990 __ Swr(val_hi, adr, 4);
991 __ Swl(val_hi, adr, 7);
992 }
993}
994
995// char java.lang.String.charAt(int index)
996void IntrinsicLocationsBuilderMIPS::VisitStringCharAt(HInvoke* invoke) {
997 LocationSummary* locations = new (arena_) LocationSummary(invoke,
998 LocationSummary::kCallOnSlowPath,
999 kIntrinsified);
1000 locations->SetInAt(0, Location::RequiresRegister());
1001 locations->SetInAt(1, Location::RequiresRegister());
1002 locations->SetOut(Location::SameAsFirstInput());
1003}
1004
1005void IntrinsicCodeGeneratorMIPS::VisitStringCharAt(HInvoke* invoke) {
1006 LocationSummary* locations = invoke->GetLocations();
1007 MipsAssembler* assembler = GetAssembler();
1008
1009 // Location of reference to data array
1010 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1011 // Location of count
1012 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1013
1014 Register obj = locations->InAt(0).AsRegister<Register>();
1015 Register idx = locations->InAt(1).AsRegister<Register>();
1016 Register out = locations->Out().AsRegister<Register>();
1017
1018 // TODO: Maybe we can support range check elimination. Overall,
1019 // though, I think it's not worth the cost.
1020 // TODO: For simplicity, the index parameter is requested in a
1021 // register, so different from Quick we will not optimize the
1022 // code for constants (which would save a register).
1023
1024 SlowPathCodeMIPS* slow_path = new (GetAllocator()) IntrinsicSlowPathMIPS(invoke);
1025 codegen_->AddSlowPath(slow_path);
1026
1027 // Load the string size
1028 __ Lw(TMP, obj, count_offset);
1029 codegen_->MaybeRecordImplicitNullCheck(invoke);
1030 // Revert to slow path if idx is too large, or negative
1031 __ Bgeu(idx, TMP, slow_path->GetEntryLabel());
1032
1033 // out = obj[2*idx].
1034 __ Sll(TMP, idx, 1); // idx * 2
1035 __ Addu(TMP, TMP, obj); // Address of char at location idx
1036 __ Lhu(out, TMP, value_offset); // Load char at location idx
1037
1038 __ Bind(slow_path->GetExitLabel());
1039}
1040
Chris Larsen16ba2b42015-11-02 10:58:31 -08001041// boolean java.lang.String.equals(Object anObject)
1042void IntrinsicLocationsBuilderMIPS::VisitStringEquals(HInvoke* invoke) {
1043 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1044 LocationSummary::kNoCall,
1045 kIntrinsified);
1046 locations->SetInAt(0, Location::RequiresRegister());
1047 locations->SetInAt(1, Location::RequiresRegister());
1048 locations->SetOut(Location::RequiresRegister());
1049
1050 // Temporary registers to store lengths of strings and for calculations.
1051 locations->AddTemp(Location::RequiresRegister());
1052 locations->AddTemp(Location::RequiresRegister());
1053 locations->AddTemp(Location::RequiresRegister());
1054}
1055
1056void IntrinsicCodeGeneratorMIPS::VisitStringEquals(HInvoke* invoke) {
1057 MipsAssembler* assembler = GetAssembler();
1058 LocationSummary* locations = invoke->GetLocations();
1059
1060 Register str = locations->InAt(0).AsRegister<Register>();
1061 Register arg = locations->InAt(1).AsRegister<Register>();
1062 Register out = locations->Out().AsRegister<Register>();
1063
1064 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
1065 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
1066 Register temp3 = locations->GetTemp(2).AsRegister<Register>();
1067
1068 MipsLabel loop;
1069 MipsLabel end;
1070 MipsLabel return_true;
1071 MipsLabel return_false;
1072
1073 // Get offsets of count, value, and class fields within a string object.
1074 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1075 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1076 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1077
1078 // Note that the null check must have been done earlier.
1079 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1080
1081 // If the register containing the pointer to "this", and the register
1082 // containing the pointer to "anObject" are the same register then
1083 // "this", and "anObject" are the same object and we can
1084 // short-circuit the logic to a true result.
1085 if (str == arg) {
1086 __ LoadConst32(out, 1);
1087 return;
1088 }
1089
1090 // Check if input is null, return false if it is.
1091 __ Beqz(arg, &return_false);
1092
1093 // Reference equality check, return true if same reference.
1094 __ Beq(str, arg, &return_true);
1095
1096 // Instanceof check for the argument by comparing class fields.
1097 // All string objects must have the same type since String cannot be subclassed.
1098 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1099 // If the argument is a string object, its class field must be equal to receiver's class field.
1100 __ Lw(temp1, str, class_offset);
1101 __ Lw(temp2, arg, class_offset);
1102 __ Bne(temp1, temp2, &return_false);
1103
1104 // Load lengths of this and argument strings.
1105 __ Lw(temp1, str, count_offset);
1106 __ Lw(temp2, arg, count_offset);
1107 // Check if lengths are equal, return false if they're not.
1108 __ Bne(temp1, temp2, &return_false);
1109 // Return true if both strings are empty.
1110 __ Beqz(temp1, &return_true);
1111
1112 // Don't overwrite input registers
1113 __ Move(TMP, str);
1114 __ Move(temp3, arg);
1115
1116 // Assertions that must hold in order to compare strings 2 characters at a time.
1117 DCHECK_ALIGNED(value_offset, 4);
1118 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
1119
1120 // Loop to compare strings 2 characters at a time starting at the beginning of the string.
1121 // Ok to do this because strings are zero-padded.
1122 __ Bind(&loop);
1123 __ Lw(out, TMP, value_offset);
1124 __ Lw(temp2, temp3, value_offset);
1125 __ Bne(out, temp2, &return_false);
1126 __ Addiu(TMP, TMP, 4);
1127 __ Addiu(temp3, temp3, 4);
1128 __ Addiu(temp1, temp1, -2);
1129 __ Bgtz(temp1, &loop);
1130
1131 // Return true and exit the function.
1132 // If loop does not result in returning false, we return true.
1133 __ Bind(&return_true);
1134 __ LoadConst32(out, 1);
1135 __ B(&end);
1136
1137 // Return false and exit the function.
1138 __ Bind(&return_false);
1139 __ LoadConst32(out, 0);
1140 __ Bind(&end);
1141}
1142
Chris Larsen701566a2015-10-27 15:29:13 -07001143// Unimplemented intrinsics.
1144
1145#define UNIMPLEMENTED_INTRINSIC(Name) \
1146void IntrinsicLocationsBuilderMIPS::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1147} \
1148void IntrinsicCodeGeneratorMIPS::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1149}
1150
Aart Bik3f67e692016-01-15 14:35:12 -08001151UNIMPLEMENTED_INTRINSIC(IntegerBitCount)
1152UNIMPLEMENTED_INTRINSIC(LongBitCount)
1153
Chris Larsen701566a2015-10-27 15:29:13 -07001154UNIMPLEMENTED_INTRINSIC(MathAbsDouble)
1155UNIMPLEMENTED_INTRINSIC(MathAbsFloat)
1156UNIMPLEMENTED_INTRINSIC(MathAbsInt)
1157UNIMPLEMENTED_INTRINSIC(MathAbsLong)
1158UNIMPLEMENTED_INTRINSIC(MathMinDoubleDouble)
1159UNIMPLEMENTED_INTRINSIC(MathMinFloatFloat)
1160UNIMPLEMENTED_INTRINSIC(MathMaxDoubleDouble)
1161UNIMPLEMENTED_INTRINSIC(MathMaxFloatFloat)
1162UNIMPLEMENTED_INTRINSIC(MathMinIntInt)
1163UNIMPLEMENTED_INTRINSIC(MathMinLongLong)
1164UNIMPLEMENTED_INTRINSIC(MathMaxIntInt)
1165UNIMPLEMENTED_INTRINSIC(MathMaxLongLong)
1166UNIMPLEMENTED_INTRINSIC(MathSqrt)
1167UNIMPLEMENTED_INTRINSIC(MathCeil)
1168UNIMPLEMENTED_INTRINSIC(MathFloor)
1169UNIMPLEMENTED_INTRINSIC(MathRint)
1170UNIMPLEMENTED_INTRINSIC(MathRoundDouble)
1171UNIMPLEMENTED_INTRINSIC(MathRoundFloat)
Chris Larsen701566a2015-10-27 15:29:13 -07001172UNIMPLEMENTED_INTRINSIC(ThreadCurrentThread)
1173UNIMPLEMENTED_INTRINSIC(UnsafeGet)
1174UNIMPLEMENTED_INTRINSIC(UnsafeGetVolatile)
1175UNIMPLEMENTED_INTRINSIC(UnsafeGetLong)
1176UNIMPLEMENTED_INTRINSIC(UnsafeGetLongVolatile)
1177UNIMPLEMENTED_INTRINSIC(UnsafeGetObject)
1178UNIMPLEMENTED_INTRINSIC(UnsafeGetObjectVolatile)
1179UNIMPLEMENTED_INTRINSIC(UnsafePut)
1180UNIMPLEMENTED_INTRINSIC(UnsafePutOrdered)
1181UNIMPLEMENTED_INTRINSIC(UnsafePutVolatile)
1182UNIMPLEMENTED_INTRINSIC(UnsafePutObject)
1183UNIMPLEMENTED_INTRINSIC(UnsafePutObjectOrdered)
1184UNIMPLEMENTED_INTRINSIC(UnsafePutObjectVolatile)
1185UNIMPLEMENTED_INTRINSIC(UnsafePutLong)
1186UNIMPLEMENTED_INTRINSIC(UnsafePutLongOrdered)
1187UNIMPLEMENTED_INTRINSIC(UnsafePutLongVolatile)
1188UNIMPLEMENTED_INTRINSIC(UnsafeCASInt)
1189UNIMPLEMENTED_INTRINSIC(UnsafeCASLong)
1190UNIMPLEMENTED_INTRINSIC(UnsafeCASObject)
Chris Larsen701566a2015-10-27 15:29:13 -07001191UNIMPLEMENTED_INTRINSIC(StringCompareTo)
Chris Larsen701566a2015-10-27 15:29:13 -07001192UNIMPLEMENTED_INTRINSIC(StringIndexOf)
1193UNIMPLEMENTED_INTRINSIC(StringIndexOfAfter)
1194UNIMPLEMENTED_INTRINSIC(StringNewStringFromBytes)
1195UNIMPLEMENTED_INTRINSIC(StringNewStringFromChars)
1196UNIMPLEMENTED_INTRINSIC(StringNewStringFromString)
Chris Larsen701566a2015-10-27 15:29:13 -07001197
1198UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
1199UNIMPLEMENTED_INTRINSIC(StringGetCharsNoCheck)
1200UNIMPLEMENTED_INTRINSIC(SystemArrayCopyChar)
1201UNIMPLEMENTED_INTRINSIC(SystemArrayCopy)
1202
Mark Mendella4f12202015-08-06 15:23:34 -04001203UNIMPLEMENTED_INTRINSIC(MathCos)
1204UNIMPLEMENTED_INTRINSIC(MathSin)
1205UNIMPLEMENTED_INTRINSIC(MathAcos)
1206UNIMPLEMENTED_INTRINSIC(MathAsin)
1207UNIMPLEMENTED_INTRINSIC(MathAtan)
1208UNIMPLEMENTED_INTRINSIC(MathAtan2)
1209UNIMPLEMENTED_INTRINSIC(MathCbrt)
1210UNIMPLEMENTED_INTRINSIC(MathCosh)
1211UNIMPLEMENTED_INTRINSIC(MathExp)
1212UNIMPLEMENTED_INTRINSIC(MathExpm1)
1213UNIMPLEMENTED_INTRINSIC(MathHypot)
1214UNIMPLEMENTED_INTRINSIC(MathLog)
1215UNIMPLEMENTED_INTRINSIC(MathLog10)
1216UNIMPLEMENTED_INTRINSIC(MathNextAfter)
1217UNIMPLEMENTED_INTRINSIC(MathSinh)
1218UNIMPLEMENTED_INTRINSIC(MathTan)
1219UNIMPLEMENTED_INTRINSIC(MathTanh)
Aart Bik59c94542016-01-25 14:20:58 -08001220
1221UNIMPLEMENTED_INTRINSIC(FloatIsInfinite)
1222UNIMPLEMENTED_INTRINSIC(DoubleIsInfinite)
Aart Bik59c94542016-01-25 14:20:58 -08001223
Aart Bik59c94542016-01-25 14:20:58 -08001224UNIMPLEMENTED_INTRINSIC(IntegerHighestOneBit)
1225UNIMPLEMENTED_INTRINSIC(LongHighestOneBit)
1226UNIMPLEMENTED_INTRINSIC(IntegerLowestOneBit)
1227UNIMPLEMENTED_INTRINSIC(LongLowestOneBit)
Aart Bika19616e2016-02-01 18:57:58 -08001228
1229// Handled as HIR instructions.
Aart Bik75a38b22016-02-17 10:41:50 -08001230UNIMPLEMENTED_INTRINSIC(FloatIsNaN)
1231UNIMPLEMENTED_INTRINSIC(DoubleIsNaN)
Aart Bika19616e2016-02-01 18:57:58 -08001232UNIMPLEMENTED_INTRINSIC(IntegerCompare)
1233UNIMPLEMENTED_INTRINSIC(LongCompare)
Aart Bik59c94542016-01-25 14:20:58 -08001234UNIMPLEMENTED_INTRINSIC(IntegerSignum)
1235UNIMPLEMENTED_INTRINSIC(LongSignum)
1236
Chris Larsen701566a2015-10-27 15:29:13 -07001237#undef UNIMPLEMENTED_INTRINSIC
1238
1239#undef __
1240
1241} // namespace mips
1242} // namespace art