blob: a2f85fe05c877b1f42876e9675e41685d1f55c6d [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
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 "code_generator_mips.h"
18
19#include "arch/mips/entrypoints_direct_mips.h"
20#include "arch/mips/instruction_set_features_mips.h"
21#include "art_method.h"
Chris Larsen701566a2015-10-27 15:29:13 -070022#include "code_generator_utils.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020023#include "entrypoints/quick/quick_entrypoints.h"
24#include "entrypoints/quick/quick_entrypoints_enum.h"
25#include "gc/accounting/card_table.h"
26#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070027#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020028#include "mirror/array-inl.h"
29#include "mirror/class-inl.h"
30#include "offsets.h"
31#include "thread.h"
32#include "utils/assembler.h"
33#include "utils/mips/assembler_mips.h"
34#include "utils/stack_checks.h"
35
36namespace art {
37namespace mips {
38
39static constexpr int kCurrentMethodStackOffset = 0;
40static constexpr Register kMethodRegisterArgument = A0;
41
42// We need extra temporary/scratch registers (in addition to AT) in some cases.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020043static constexpr FRegister FTMP = F8;
44
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020045Location MipsReturnLocation(Primitive::Type return_type) {
46 switch (return_type) {
47 case Primitive::kPrimBoolean:
48 case Primitive::kPrimByte:
49 case Primitive::kPrimChar:
50 case Primitive::kPrimShort:
51 case Primitive::kPrimInt:
52 case Primitive::kPrimNot:
53 return Location::RegisterLocation(V0);
54
55 case Primitive::kPrimLong:
56 return Location::RegisterPairLocation(V0, V1);
57
58 case Primitive::kPrimFloat:
59 case Primitive::kPrimDouble:
60 return Location::FpuRegisterLocation(F0);
61
62 case Primitive::kPrimVoid:
63 return Location();
64 }
65 UNREACHABLE();
66}
67
68Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
69 return MipsReturnLocation(type);
70}
71
72Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
73 return Location::RegisterLocation(kMethodRegisterArgument);
74}
75
76Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
77 Location next_location;
78
79 switch (type) {
80 case Primitive::kPrimBoolean:
81 case Primitive::kPrimByte:
82 case Primitive::kPrimChar:
83 case Primitive::kPrimShort:
84 case Primitive::kPrimInt:
85 case Primitive::kPrimNot: {
86 uint32_t gp_index = gp_index_++;
87 if (gp_index < calling_convention.GetNumberOfRegisters()) {
88 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
89 } else {
90 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
91 next_location = Location::StackSlot(stack_offset);
92 }
93 break;
94 }
95
96 case Primitive::kPrimLong: {
97 uint32_t gp_index = gp_index_;
98 gp_index_ += 2;
99 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
100 if (calling_convention.GetRegisterAt(gp_index) == A1) {
101 gp_index_++; // Skip A1, and use A2_A3 instead.
102 gp_index++;
103 }
104 Register low_even = calling_convention.GetRegisterAt(gp_index);
105 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
106 DCHECK_EQ(low_even + 1, high_odd);
107 next_location = Location::RegisterPairLocation(low_even, high_odd);
108 } else {
109 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
110 next_location = Location::DoubleStackSlot(stack_offset);
111 }
112 break;
113 }
114
115 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
116 // will take up the even/odd pair, while floats are stored in even regs only.
117 // On 64 bit FPU, both double and float are stored in even registers only.
118 case Primitive::kPrimFloat:
119 case Primitive::kPrimDouble: {
120 uint32_t float_index = float_index_++;
121 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
122 next_location = Location::FpuRegisterLocation(
123 calling_convention.GetFpuRegisterAt(float_index));
124 } else {
125 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
126 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
127 : Location::StackSlot(stack_offset);
128 }
129 break;
130 }
131
132 case Primitive::kPrimVoid:
133 LOG(FATAL) << "Unexpected parameter type " << type;
134 break;
135 }
136
137 // Space on the stack is reserved for all arguments.
138 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
139
140 return next_location;
141}
142
143Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
144 return MipsReturnLocation(type);
145}
146
147#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()->
148#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
149
150class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
151 public:
152 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : instruction_(instruction) {}
153
154 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
155 LocationSummary* locations = instruction_->GetLocations();
156 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
157 __ Bind(GetEntryLabel());
158 if (instruction_->CanThrowIntoCatchBlock()) {
159 // Live registers will be restored in the catch block if caught.
160 SaveLiveRegisters(codegen, instruction_->GetLocations());
161 }
162 // We're moving two locations to locations that could overlap, so we need a parallel
163 // move resolver.
164 InvokeRuntimeCallingConvention calling_convention;
165 codegen->EmitParallelMoves(locations->InAt(0),
166 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
167 Primitive::kPrimInt,
168 locations->InAt(1),
169 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
170 Primitive::kPrimInt);
171 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
172 instruction_,
173 instruction_->GetDexPc(),
174 this,
175 IsDirectEntrypoint(kQuickThrowArrayBounds));
176 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
177 }
178
179 bool IsFatal() const OVERRIDE { return true; }
180
181 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
182
183 private:
184 HBoundsCheck* const instruction_;
185
186 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
187};
188
189class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
190 public:
191 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : instruction_(instruction) {}
192
193 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
194 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
195 __ Bind(GetEntryLabel());
196 if (instruction_->CanThrowIntoCatchBlock()) {
197 // Live registers will be restored in the catch block if caught.
198 SaveLiveRegisters(codegen, instruction_->GetLocations());
199 }
200 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
201 instruction_,
202 instruction_->GetDexPc(),
203 this,
204 IsDirectEntrypoint(kQuickThrowDivZero));
205 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
206 }
207
208 bool IsFatal() const OVERRIDE { return true; }
209
210 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
211
212 private:
213 HDivZeroCheck* const instruction_;
214 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
215};
216
217class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
218 public:
219 LoadClassSlowPathMIPS(HLoadClass* cls,
220 HInstruction* at,
221 uint32_t dex_pc,
222 bool do_clinit)
223 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
224 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
225 }
226
227 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
228 LocationSummary* locations = at_->GetLocations();
229 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
230
231 __ Bind(GetEntryLabel());
232 SaveLiveRegisters(codegen, locations);
233
234 InvokeRuntimeCallingConvention calling_convention;
235 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
236
237 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
238 : QUICK_ENTRY_POINT(pInitializeType);
239 bool direct = do_clinit_ ? IsDirectEntrypoint(kQuickInitializeStaticStorage)
240 : IsDirectEntrypoint(kQuickInitializeType);
241
242 mips_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this, direct);
243 if (do_clinit_) {
244 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
245 } else {
246 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
247 }
248
249 // Move the class to the desired location.
250 Location out = locations->Out();
251 if (out.IsValid()) {
252 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
253 Primitive::Type type = at_->GetType();
254 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
255 }
256
257 RestoreLiveRegisters(codegen, locations);
258 __ B(GetExitLabel());
259 }
260
261 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
262
263 private:
264 // The class this slow path will load.
265 HLoadClass* const cls_;
266
267 // The instruction where this slow path is happening.
268 // (Might be the load class or an initialization check).
269 HInstruction* const at_;
270
271 // The dex PC of `at_`.
272 const uint32_t dex_pc_;
273
274 // Whether to initialize the class.
275 const bool do_clinit_;
276
277 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
278};
279
280class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
281 public:
282 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : instruction_(instruction) {}
283
284 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
285 LocationSummary* locations = instruction_->GetLocations();
286 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
287 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
288
289 __ Bind(GetEntryLabel());
290 SaveLiveRegisters(codegen, locations);
291
292 InvokeRuntimeCallingConvention calling_convention;
293 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
294 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
295 instruction_,
296 instruction_->GetDexPc(),
297 this,
298 IsDirectEntrypoint(kQuickResolveString));
299 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
300 Primitive::Type type = instruction_->GetType();
301 mips_codegen->MoveLocation(locations->Out(),
302 calling_convention.GetReturnLocation(type),
303 type);
304
305 RestoreLiveRegisters(codegen, locations);
306 __ B(GetExitLabel());
307 }
308
309 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
310
311 private:
312 HLoadString* const instruction_;
313
314 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
315};
316
317class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
318 public:
319 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : instruction_(instr) {}
320
321 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
322 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
323 __ Bind(GetEntryLabel());
324 if (instruction_->CanThrowIntoCatchBlock()) {
325 // Live registers will be restored in the catch block if caught.
326 SaveLiveRegisters(codegen, instruction_->GetLocations());
327 }
328 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
329 instruction_,
330 instruction_->GetDexPc(),
331 this,
332 IsDirectEntrypoint(kQuickThrowNullPointer));
333 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
334 }
335
336 bool IsFatal() const OVERRIDE { return true; }
337
338 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
339
340 private:
341 HNullCheck* const instruction_;
342
343 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
344};
345
346class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
347 public:
348 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
349 : instruction_(instruction), successor_(successor) {}
350
351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
352 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
353 __ Bind(GetEntryLabel());
354 SaveLiveRegisters(codegen, instruction_->GetLocations());
355 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
356 instruction_,
357 instruction_->GetDexPc(),
358 this,
359 IsDirectEntrypoint(kQuickTestSuspend));
360 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
361 RestoreLiveRegisters(codegen, instruction_->GetLocations());
362 if (successor_ == nullptr) {
363 __ B(GetReturnLabel());
364 } else {
365 __ B(mips_codegen->GetLabelOf(successor_));
366 }
367 }
368
369 MipsLabel* GetReturnLabel() {
370 DCHECK(successor_ == nullptr);
371 return &return_label_;
372 }
373
374 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
375
376 private:
377 HSuspendCheck* const instruction_;
378 // If not null, the block to branch to after the suspend check.
379 HBasicBlock* const successor_;
380
381 // If `successor_` is null, the label to branch to after the suspend check.
382 MipsLabel return_label_;
383
384 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
385};
386
387class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
388 public:
389 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : instruction_(instruction) {}
390
391 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
392 LocationSummary* locations = instruction_->GetLocations();
393 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
394 uint32_t dex_pc = instruction_->GetDexPc();
395 DCHECK(instruction_->IsCheckCast()
396 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
397 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
398
399 __ Bind(GetEntryLabel());
400 SaveLiveRegisters(codegen, locations);
401
402 // We're moving two locations to locations that could overlap, so we need a parallel
403 // move resolver.
404 InvokeRuntimeCallingConvention calling_convention;
405 codegen->EmitParallelMoves(locations->InAt(1),
406 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
407 Primitive::kPrimNot,
408 object_class,
409 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
410 Primitive::kPrimNot);
411
412 if (instruction_->IsInstanceOf()) {
413 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
414 instruction_,
415 dex_pc,
416 this,
417 IsDirectEntrypoint(kQuickInstanceofNonTrivial));
Roland Levillain888d0672015-11-23 18:53:50 +0000418 CheckEntrypointTypes<
419 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200420 Primitive::Type ret_type = instruction_->GetType();
421 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
422 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200423 } else {
424 DCHECK(instruction_->IsCheckCast());
425 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
426 instruction_,
427 dex_pc,
428 this,
429 IsDirectEntrypoint(kQuickCheckCast));
430 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
431 }
432
433 RestoreLiveRegisters(codegen, locations);
434 __ B(GetExitLabel());
435 }
436
437 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
438
439 private:
440 HInstruction* const instruction_;
441
442 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
443};
444
445class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
446 public:
Aart Bik42249c32016-01-07 15:33:50 -0800447 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200448 : instruction_(instruction) {}
449
450 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800451 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200452 __ Bind(GetEntryLabel());
453 SaveLiveRegisters(codegen, instruction_->GetLocations());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200454 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
455 instruction_,
Aart Bik42249c32016-01-07 15:33:50 -0800456 instruction_->GetDexPc(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200457 this,
458 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000459 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200460 }
461
462 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
463
464 private:
Aart Bik42249c32016-01-07 15:33:50 -0800465 HDeoptimize* const instruction_;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200466 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
467};
468
469CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
470 const MipsInstructionSetFeatures& isa_features,
471 const CompilerOptions& compiler_options,
472 OptimizingCompilerStats* stats)
473 : CodeGenerator(graph,
474 kNumberOfCoreRegisters,
475 kNumberOfFRegisters,
476 kNumberOfRegisterPairs,
477 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
478 arraysize(kCoreCalleeSaves)),
479 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
480 arraysize(kFpuCalleeSaves)),
481 compiler_options,
482 stats),
483 block_labels_(nullptr),
484 location_builder_(graph, this),
485 instruction_visitor_(graph, this),
486 move_resolver_(graph->GetArena(), this),
487 assembler_(&isa_features),
488 isa_features_(isa_features) {
489 // Save RA (containing the return address) to mimic Quick.
490 AddAllocatedRegister(Location::RegisterLocation(RA));
491}
492
493#undef __
494#define __ down_cast<MipsAssembler*>(GetAssembler())->
495#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
496
497void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
498 // Ensure that we fix up branches.
499 __ FinalizeCode();
500
501 // Adjust native pc offsets in stack maps.
502 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
503 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
504 uint32_t new_position = __ GetAdjustedPosition(old_position);
505 DCHECK_GE(new_position, old_position);
506 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
507 }
508
509 // Adjust pc offsets for the disassembly information.
510 if (disasm_info_ != nullptr) {
511 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
512 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
513 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
514 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
515 it.second.start = __ GetAdjustedPosition(it.second.start);
516 it.second.end = __ GetAdjustedPosition(it.second.end);
517 }
518 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
519 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
520 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
521 }
522 }
523
524 CodeGenerator::Finalize(allocator);
525}
526
527MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
528 return codegen_->GetAssembler();
529}
530
531void ParallelMoveResolverMIPS::EmitMove(size_t index) {
532 DCHECK_LT(index, moves_.size());
533 MoveOperands* move = moves_[index];
534 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
535}
536
537void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
538 DCHECK_LT(index, moves_.size());
539 MoveOperands* move = moves_[index];
540 Primitive::Type type = move->GetType();
541 Location loc1 = move->GetDestination();
542 Location loc2 = move->GetSource();
543
544 DCHECK(!loc1.IsConstant());
545 DCHECK(!loc2.IsConstant());
546
547 if (loc1.Equals(loc2)) {
548 return;
549 }
550
551 if (loc1.IsRegister() && loc2.IsRegister()) {
552 // Swap 2 GPRs.
553 Register r1 = loc1.AsRegister<Register>();
554 Register r2 = loc2.AsRegister<Register>();
555 __ Move(TMP, r2);
556 __ Move(r2, r1);
557 __ Move(r1, TMP);
558 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
559 FRegister f1 = loc1.AsFpuRegister<FRegister>();
560 FRegister f2 = loc2.AsFpuRegister<FRegister>();
561 if (type == Primitive::kPrimFloat) {
562 __ MovS(FTMP, f2);
563 __ MovS(f2, f1);
564 __ MovS(f1, FTMP);
565 } else {
566 DCHECK_EQ(type, Primitive::kPrimDouble);
567 __ MovD(FTMP, f2);
568 __ MovD(f2, f1);
569 __ MovD(f1, FTMP);
570 }
571 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
572 (loc1.IsFpuRegister() && loc2.IsRegister())) {
573 // Swap FPR and GPR.
574 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
575 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
576 : loc2.AsFpuRegister<FRegister>();
577 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
578 : loc2.AsRegister<Register>();
579 __ Move(TMP, r2);
580 __ Mfc1(r2, f1);
581 __ Mtc1(TMP, f1);
582 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
583 // Swap 2 GPR register pairs.
584 Register r1 = loc1.AsRegisterPairLow<Register>();
585 Register r2 = loc2.AsRegisterPairLow<Register>();
586 __ Move(TMP, r2);
587 __ Move(r2, r1);
588 __ Move(r1, TMP);
589 r1 = loc1.AsRegisterPairHigh<Register>();
590 r2 = loc2.AsRegisterPairHigh<Register>();
591 __ Move(TMP, r2);
592 __ Move(r2, r1);
593 __ Move(r1, TMP);
594 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
595 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
596 // Swap FPR and GPR register pair.
597 DCHECK_EQ(type, Primitive::kPrimDouble);
598 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
599 : loc2.AsFpuRegister<FRegister>();
600 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
601 : loc2.AsRegisterPairLow<Register>();
602 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
603 : loc2.AsRegisterPairHigh<Register>();
604 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
605 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
606 // unpredictable and the following mfch1 will fail.
607 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800608 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200609 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800610 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200611 __ Move(r2_l, TMP);
612 __ Move(r2_h, AT);
613 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
614 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
615 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
616 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
617 } else {
618 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
619 }
620}
621
622void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
623 __ Pop(static_cast<Register>(reg));
624}
625
626void ParallelMoveResolverMIPS::SpillScratch(int reg) {
627 __ Push(static_cast<Register>(reg));
628}
629
630void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
631 // Allocate a scratch register other than TMP, if available.
632 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
633 // automatically unspilled when the scratch scope object is destroyed).
634 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
635 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
636 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
637 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
638 __ LoadFromOffset(kLoadWord,
639 Register(ensure_scratch.GetRegister()),
640 SP,
641 index1 + stack_offset);
642 __ LoadFromOffset(kLoadWord,
643 TMP,
644 SP,
645 index2 + stack_offset);
646 __ StoreToOffset(kStoreWord,
647 Register(ensure_scratch.GetRegister()),
648 SP,
649 index2 + stack_offset);
650 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
651 }
652}
653
654static dwarf::Reg DWARFReg(Register reg) {
655 return dwarf::Reg::MipsCore(static_cast<int>(reg));
656}
657
658// TODO: mapping of floating-point registers to DWARF.
659
660void CodeGeneratorMIPS::GenerateFrameEntry() {
661 __ Bind(&frame_entry_label_);
662
663 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
664
665 if (do_overflow_check) {
666 __ LoadFromOffset(kLoadWord,
667 ZERO,
668 SP,
669 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
670 RecordPcInfo(nullptr, 0);
671 }
672
673 if (HasEmptyFrame()) {
674 return;
675 }
676
677 // Make sure the frame size isn't unreasonably large.
678 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
679 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
680 }
681
682 // Spill callee-saved registers.
683 // Note that their cumulative size is small and they can be indexed using
684 // 16-bit offsets.
685
686 // TODO: increment/decrement SP in one step instead of two or remove this comment.
687
688 uint32_t ofs = FrameEntrySpillSize();
689 bool unaligned_float = ofs & 0x7;
690 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
691 __ IncreaseFrameSize(ofs);
692
693 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
694 Register reg = kCoreCalleeSaves[i];
695 if (allocated_registers_.ContainsCoreRegister(reg)) {
696 ofs -= kMipsWordSize;
697 __ Sw(reg, SP, ofs);
698 __ cfi().RelOffset(DWARFReg(reg), ofs);
699 }
700 }
701
702 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
703 FRegister reg = kFpuCalleeSaves[i];
704 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
705 ofs -= kMipsDoublewordSize;
706 // TODO: Change the frame to avoid unaligned accesses for fpu registers.
707 if (unaligned_float) {
708 if (fpu_32bit) {
709 __ Swc1(reg, SP, ofs);
710 __ Swc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
711 } else {
712 __ Mfhc1(TMP, reg);
713 __ Swc1(reg, SP, ofs);
714 __ Sw(TMP, SP, ofs + 4);
715 }
716 } else {
717 __ Sdc1(reg, SP, ofs);
718 }
719 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
720 }
721 }
722
723 // Allocate the rest of the frame and store the current method pointer
724 // at its end.
725
726 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
727
728 static_assert(IsInt<16>(kCurrentMethodStackOffset),
729 "kCurrentMethodStackOffset must fit into int16_t");
730 __ Sw(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
731}
732
733void CodeGeneratorMIPS::GenerateFrameExit() {
734 __ cfi().RememberState();
735
736 if (!HasEmptyFrame()) {
737 // Deallocate the rest of the frame.
738
739 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
740
741 // Restore callee-saved registers.
742 // Note that their cumulative size is small and they can be indexed using
743 // 16-bit offsets.
744
745 // TODO: increment/decrement SP in one step instead of two or remove this comment.
746
747 uint32_t ofs = 0;
748 bool unaligned_float = FrameEntrySpillSize() & 0x7;
749 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
750
751 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
752 FRegister reg = kFpuCalleeSaves[i];
753 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
754 if (unaligned_float) {
755 if (fpu_32bit) {
756 __ Lwc1(reg, SP, ofs);
757 __ Lwc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
758 } else {
759 __ Lwc1(reg, SP, ofs);
760 __ Lw(TMP, SP, ofs + 4);
761 __ Mthc1(TMP, reg);
762 }
763 } else {
764 __ Ldc1(reg, SP, ofs);
765 }
766 ofs += kMipsDoublewordSize;
767 // TODO: __ cfi().Restore(DWARFReg(reg));
768 }
769 }
770
771 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
772 Register reg = kCoreCalleeSaves[i];
773 if (allocated_registers_.ContainsCoreRegister(reg)) {
774 __ Lw(reg, SP, ofs);
775 ofs += kMipsWordSize;
776 __ cfi().Restore(DWARFReg(reg));
777 }
778 }
779
780 DCHECK_EQ(ofs, FrameEntrySpillSize());
781 __ DecreaseFrameSize(ofs);
782 }
783
784 __ Jr(RA);
785 __ Nop();
786
787 __ cfi().RestoreState();
788 __ cfi().DefCFAOffset(GetFrameSize());
789}
790
791void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
792 __ Bind(GetLabelOf(block));
793}
794
795void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
796 if (src.Equals(dst)) {
797 return;
798 }
799
800 if (src.IsConstant()) {
801 MoveConstant(dst, src.GetConstant());
802 } else {
803 if (Primitive::Is64BitType(dst_type)) {
804 Move64(dst, src);
805 } else {
806 Move32(dst, src);
807 }
808 }
809}
810
811void CodeGeneratorMIPS::Move32(Location destination, Location source) {
812 if (source.Equals(destination)) {
813 return;
814 }
815
816 if (destination.IsRegister()) {
817 if (source.IsRegister()) {
818 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
819 } else if (source.IsFpuRegister()) {
820 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
821 } else {
822 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
823 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
824 }
825 } else if (destination.IsFpuRegister()) {
826 if (source.IsRegister()) {
827 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
828 } else if (source.IsFpuRegister()) {
829 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
830 } else {
831 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
832 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
833 }
834 } else {
835 DCHECK(destination.IsStackSlot()) << destination;
836 if (source.IsRegister()) {
837 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
838 } else if (source.IsFpuRegister()) {
839 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
840 } else {
841 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
842 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
843 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
844 }
845 }
846}
847
848void CodeGeneratorMIPS::Move64(Location destination, Location source) {
849 if (source.Equals(destination)) {
850 return;
851 }
852
853 if (destination.IsRegisterPair()) {
854 if (source.IsRegisterPair()) {
855 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
856 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
857 } else if (source.IsFpuRegister()) {
858 Register dst_high = destination.AsRegisterPairHigh<Register>();
859 Register dst_low = destination.AsRegisterPairLow<Register>();
860 FRegister src = source.AsFpuRegister<FRegister>();
861 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800862 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200863 } else {
864 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
865 int32_t off = source.GetStackIndex();
866 Register r = destination.AsRegisterPairLow<Register>();
867 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
868 }
869 } else if (destination.IsFpuRegister()) {
870 if (source.IsRegisterPair()) {
871 FRegister dst = destination.AsFpuRegister<FRegister>();
872 Register src_high = source.AsRegisterPairHigh<Register>();
873 Register src_low = source.AsRegisterPairLow<Register>();
874 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800875 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200876 } else if (source.IsFpuRegister()) {
877 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
878 } else {
879 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
880 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
881 }
882 } else {
883 DCHECK(destination.IsDoubleStackSlot()) << destination;
884 int32_t off = destination.GetStackIndex();
885 if (source.IsRegisterPair()) {
886 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
887 } else if (source.IsFpuRegister()) {
888 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
889 } else {
890 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
891 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
892 __ StoreToOffset(kStoreWord, TMP, SP, off);
893 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
894 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
895 }
896 }
897}
898
899void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
900 if (c->IsIntConstant() || c->IsNullConstant()) {
901 // Move 32 bit constant.
902 int32_t value = GetInt32ValueOf(c);
903 if (destination.IsRegister()) {
904 Register dst = destination.AsRegister<Register>();
905 __ LoadConst32(dst, value);
906 } else {
907 DCHECK(destination.IsStackSlot())
908 << "Cannot move " << c->DebugName() << " to " << destination;
909 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
910 }
911 } else if (c->IsLongConstant()) {
912 // Move 64 bit constant.
913 int64_t value = GetInt64ValueOf(c);
914 if (destination.IsRegisterPair()) {
915 Register r_h = destination.AsRegisterPairHigh<Register>();
916 Register r_l = destination.AsRegisterPairLow<Register>();
917 __ LoadConst64(r_h, r_l, value);
918 } else {
919 DCHECK(destination.IsDoubleStackSlot())
920 << "Cannot move " << c->DebugName() << " to " << destination;
921 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
922 }
923 } else if (c->IsFloatConstant()) {
924 // Move 32 bit float constant.
925 int32_t value = GetInt32ValueOf(c);
926 if (destination.IsFpuRegister()) {
927 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
928 } else {
929 DCHECK(destination.IsStackSlot())
930 << "Cannot move " << c->DebugName() << " to " << destination;
931 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
932 }
933 } else {
934 // Move 64 bit double constant.
935 DCHECK(c->IsDoubleConstant()) << c->DebugName();
936 int64_t value = GetInt64ValueOf(c);
937 if (destination.IsFpuRegister()) {
938 FRegister fd = destination.AsFpuRegister<FRegister>();
939 __ LoadDConst64(fd, value, TMP);
940 } else {
941 DCHECK(destination.IsDoubleStackSlot())
942 << "Cannot move " << c->DebugName() << " to " << destination;
943 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
944 }
945 }
946}
947
948void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
949 DCHECK(destination.IsRegister());
950 Register dst = destination.AsRegister<Register>();
951 __ LoadConst32(dst, value);
952}
953
954void CodeGeneratorMIPS::Move(HInstruction* instruction,
955 Location location,
956 HInstruction* move_for) {
957 LocationSummary* locations = instruction->GetLocations();
958 Primitive::Type type = instruction->GetType();
959 DCHECK_NE(type, Primitive::kPrimVoid);
960
961 if (instruction->IsCurrentMethod()) {
962 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
963 } else if (locations != nullptr && locations->Out().Equals(location)) {
964 return;
965 } else if (instruction->IsIntConstant()
966 || instruction->IsLongConstant()
967 || instruction->IsNullConstant()) {
968 MoveConstant(location, instruction->AsConstant());
969 } else if (instruction->IsTemporary()) {
970 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
971 if (temp_location.IsStackSlot()) {
972 Move32(location, temp_location);
973 } else {
974 DCHECK(temp_location.IsDoubleStackSlot());
975 Move64(location, temp_location);
976 }
977 } else if (instruction->IsLoadLocal()) {
978 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
979 if (Primitive::Is64BitType(type)) {
980 Move64(location, Location::DoubleStackSlot(stack_slot));
981 } else {
982 Move32(location, Location::StackSlot(stack_slot));
983 }
984 } else {
985 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
986 if (Primitive::Is64BitType(type)) {
987 Move64(location, locations->Out());
988 } else {
989 Move32(location, locations->Out());
990 }
991 }
992}
993
994void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
995 if (location.IsRegister()) {
996 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700997 } else if (location.IsRegisterPair()) {
998 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
999 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001000 } else {
1001 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1002 }
1003}
1004
1005Location CodeGeneratorMIPS::GetStackLocation(HLoadLocal* load) const {
1006 Primitive::Type type = load->GetType();
1007
1008 switch (type) {
1009 case Primitive::kPrimNot:
1010 case Primitive::kPrimInt:
1011 case Primitive::kPrimFloat:
1012 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1013
1014 case Primitive::kPrimLong:
1015 case Primitive::kPrimDouble:
1016 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1017
1018 case Primitive::kPrimBoolean:
1019 case Primitive::kPrimByte:
1020 case Primitive::kPrimChar:
1021 case Primitive::kPrimShort:
1022 case Primitive::kPrimVoid:
1023 LOG(FATAL) << "Unexpected type " << type;
1024 }
1025
1026 LOG(FATAL) << "Unreachable";
1027 return Location::NoLocation();
1028}
1029
1030void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1031 MipsLabel done;
1032 Register card = AT;
1033 Register temp = TMP;
1034 __ Beqz(value, &done);
1035 __ LoadFromOffset(kLoadWord,
1036 card,
1037 TR,
1038 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
1039 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1040 __ Addu(temp, card, temp);
1041 __ Sb(card, temp, 0);
1042 __ Bind(&done);
1043}
1044
David Brazdil58282f42016-01-14 12:45:10 +00001045void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001046 // Don't allocate the dalvik style register pair passing.
1047 blocked_register_pairs_[A1_A2] = true;
1048
1049 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1050 blocked_core_registers_[ZERO] = true;
1051 blocked_core_registers_[K0] = true;
1052 blocked_core_registers_[K1] = true;
1053 blocked_core_registers_[GP] = true;
1054 blocked_core_registers_[SP] = true;
1055 blocked_core_registers_[RA] = true;
1056
1057 // AT and TMP(T8) are used as temporary/scratch registers
1058 // (similar to how AT is used by MIPS assemblers).
1059 blocked_core_registers_[AT] = true;
1060 blocked_core_registers_[TMP] = true;
1061 blocked_fpu_registers_[FTMP] = true;
1062
1063 // Reserve suspend and thread registers.
1064 blocked_core_registers_[S0] = true;
1065 blocked_core_registers_[TR] = true;
1066
1067 // Reserve T9 for function calls
1068 blocked_core_registers_[T9] = true;
1069
1070 // Reserve odd-numbered FPU registers.
1071 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1072 blocked_fpu_registers_[i] = true;
1073 }
1074
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001075 UpdateBlockedPairRegisters();
1076}
1077
1078void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1079 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1080 MipsManagedRegister current =
1081 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1082 if (blocked_core_registers_[current.AsRegisterPairLow()]
1083 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1084 blocked_register_pairs_[i] = true;
1085 }
1086 }
1087}
1088
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001089size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1090 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1091 return kMipsWordSize;
1092}
1093
1094size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1095 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1096 return kMipsWordSize;
1097}
1098
1099size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1100 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1101 return kMipsDoublewordSize;
1102}
1103
1104size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1105 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1106 return kMipsDoublewordSize;
1107}
1108
1109void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
1110 stream << MipsManagedRegister::FromCoreRegister(Register(reg));
1111}
1112
1113void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1114 stream << MipsManagedRegister::FromFRegister(FRegister(reg));
1115}
1116
1117void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1118 HInstruction* instruction,
1119 uint32_t dex_pc,
1120 SlowPathCode* slow_path) {
1121 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1122 instruction,
1123 dex_pc,
1124 slow_path,
1125 IsDirectEntrypoint(entrypoint));
1126}
1127
1128constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1129
1130void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1131 HInstruction* instruction,
1132 uint32_t dex_pc,
1133 SlowPathCode* slow_path,
1134 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001135 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1136 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001137 if (is_direct_entrypoint) {
1138 // Reserve argument space on stack (for $a0-$a3) for
1139 // entrypoints that directly reference native implementations.
1140 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001141 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001142 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001143 } else {
1144 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001145 }
1146 RecordPcInfo(instruction, dex_pc, slow_path);
1147}
1148
1149void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1150 Register class_reg) {
1151 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1152 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1153 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1154 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1155 __ Sync(0);
1156 __ Bind(slow_path->GetExitLabel());
1157}
1158
1159void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1160 __ Sync(0); // Only stype 0 is supported.
1161}
1162
1163void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1164 HBasicBlock* successor) {
1165 SuspendCheckSlowPathMIPS* slow_path =
1166 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1167 codegen_->AddSlowPath(slow_path);
1168
1169 __ LoadFromOffset(kLoadUnsignedHalfword,
1170 TMP,
1171 TR,
1172 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1173 if (successor == nullptr) {
1174 __ Bnez(TMP, slow_path->GetEntryLabel());
1175 __ Bind(slow_path->GetReturnLabel());
1176 } else {
1177 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1178 __ B(slow_path->GetEntryLabel());
1179 // slow_path will return to GetLabelOf(successor).
1180 }
1181}
1182
1183InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1184 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001185 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001186 assembler_(codegen->GetAssembler()),
1187 codegen_(codegen) {}
1188
1189void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1190 DCHECK_EQ(instruction->InputCount(), 2U);
1191 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1192 Primitive::Type type = instruction->GetResultType();
1193 switch (type) {
1194 case Primitive::kPrimInt: {
1195 locations->SetInAt(0, Location::RequiresRegister());
1196 HInstruction* right = instruction->InputAt(1);
1197 bool can_use_imm = false;
1198 if (right->IsConstant()) {
1199 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1200 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1201 can_use_imm = IsUint<16>(imm);
1202 } else if (instruction->IsAdd()) {
1203 can_use_imm = IsInt<16>(imm);
1204 } else {
1205 DCHECK(instruction->IsSub());
1206 can_use_imm = IsInt<16>(-imm);
1207 }
1208 }
1209 if (can_use_imm)
1210 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1211 else
1212 locations->SetInAt(1, Location::RequiresRegister());
1213 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1214 break;
1215 }
1216
1217 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001218 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001219 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1220 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001221 break;
1222 }
1223
1224 case Primitive::kPrimFloat:
1225 case Primitive::kPrimDouble:
1226 DCHECK(instruction->IsAdd() || instruction->IsSub());
1227 locations->SetInAt(0, Location::RequiresFpuRegister());
1228 locations->SetInAt(1, Location::RequiresFpuRegister());
1229 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1230 break;
1231
1232 default:
1233 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1234 }
1235}
1236
1237void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1238 Primitive::Type type = instruction->GetType();
1239 LocationSummary* locations = instruction->GetLocations();
1240
1241 switch (type) {
1242 case Primitive::kPrimInt: {
1243 Register dst = locations->Out().AsRegister<Register>();
1244 Register lhs = locations->InAt(0).AsRegister<Register>();
1245 Location rhs_location = locations->InAt(1);
1246
1247 Register rhs_reg = ZERO;
1248 int32_t rhs_imm = 0;
1249 bool use_imm = rhs_location.IsConstant();
1250 if (use_imm) {
1251 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1252 } else {
1253 rhs_reg = rhs_location.AsRegister<Register>();
1254 }
1255
1256 if (instruction->IsAnd()) {
1257 if (use_imm)
1258 __ Andi(dst, lhs, rhs_imm);
1259 else
1260 __ And(dst, lhs, rhs_reg);
1261 } else if (instruction->IsOr()) {
1262 if (use_imm)
1263 __ Ori(dst, lhs, rhs_imm);
1264 else
1265 __ Or(dst, lhs, rhs_reg);
1266 } else if (instruction->IsXor()) {
1267 if (use_imm)
1268 __ Xori(dst, lhs, rhs_imm);
1269 else
1270 __ Xor(dst, lhs, rhs_reg);
1271 } else if (instruction->IsAdd()) {
1272 if (use_imm)
1273 __ Addiu(dst, lhs, rhs_imm);
1274 else
1275 __ Addu(dst, lhs, rhs_reg);
1276 } else {
1277 DCHECK(instruction->IsSub());
1278 if (use_imm)
1279 __ Addiu(dst, lhs, -rhs_imm);
1280 else
1281 __ Subu(dst, lhs, rhs_reg);
1282 }
1283 break;
1284 }
1285
1286 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001287 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1288 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1289 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1290 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001291 Location rhs_location = locations->InAt(1);
1292 bool use_imm = rhs_location.IsConstant();
1293 if (!use_imm) {
1294 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1295 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1296 if (instruction->IsAnd()) {
1297 __ And(dst_low, lhs_low, rhs_low);
1298 __ And(dst_high, lhs_high, rhs_high);
1299 } else if (instruction->IsOr()) {
1300 __ Or(dst_low, lhs_low, rhs_low);
1301 __ Or(dst_high, lhs_high, rhs_high);
1302 } else if (instruction->IsXor()) {
1303 __ Xor(dst_low, lhs_low, rhs_low);
1304 __ Xor(dst_high, lhs_high, rhs_high);
1305 } else if (instruction->IsAdd()) {
1306 if (lhs_low == rhs_low) {
1307 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1308 __ Slt(TMP, lhs_low, ZERO);
1309 __ Addu(dst_low, lhs_low, rhs_low);
1310 } else {
1311 __ Addu(dst_low, lhs_low, rhs_low);
1312 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1313 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1314 }
1315 __ Addu(dst_high, lhs_high, rhs_high);
1316 __ Addu(dst_high, dst_high, TMP);
1317 } else {
1318 DCHECK(instruction->IsSub());
1319 __ Sltu(TMP, lhs_low, rhs_low);
1320 __ Subu(dst_low, lhs_low, rhs_low);
1321 __ Subu(dst_high, lhs_high, rhs_high);
1322 __ Subu(dst_high, dst_high, TMP);
1323 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001324 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001325 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1326 if (instruction->IsOr()) {
1327 uint32_t low = Low32Bits(value);
1328 uint32_t high = High32Bits(value);
1329 if (IsUint<16>(low)) {
1330 if (dst_low != lhs_low || low != 0) {
1331 __ Ori(dst_low, lhs_low, low);
1332 }
1333 } else {
1334 __ LoadConst32(TMP, low);
1335 __ Or(dst_low, lhs_low, TMP);
1336 }
1337 if (IsUint<16>(high)) {
1338 if (dst_high != lhs_high || high != 0) {
1339 __ Ori(dst_high, lhs_high, high);
1340 }
1341 } else {
1342 if (high != low) {
1343 __ LoadConst32(TMP, high);
1344 }
1345 __ Or(dst_high, lhs_high, TMP);
1346 }
1347 } else if (instruction->IsXor()) {
1348 uint32_t low = Low32Bits(value);
1349 uint32_t high = High32Bits(value);
1350 if (IsUint<16>(low)) {
1351 if (dst_low != lhs_low || low != 0) {
1352 __ Xori(dst_low, lhs_low, low);
1353 }
1354 } else {
1355 __ LoadConst32(TMP, low);
1356 __ Xor(dst_low, lhs_low, TMP);
1357 }
1358 if (IsUint<16>(high)) {
1359 if (dst_high != lhs_high || high != 0) {
1360 __ Xori(dst_high, lhs_high, high);
1361 }
1362 } else {
1363 if (high != low) {
1364 __ LoadConst32(TMP, high);
1365 }
1366 __ Xor(dst_high, lhs_high, TMP);
1367 }
1368 } else if (instruction->IsAnd()) {
1369 uint32_t low = Low32Bits(value);
1370 uint32_t high = High32Bits(value);
1371 if (IsUint<16>(low)) {
1372 __ Andi(dst_low, lhs_low, low);
1373 } else if (low != 0xFFFFFFFF) {
1374 __ LoadConst32(TMP, low);
1375 __ And(dst_low, lhs_low, TMP);
1376 } else if (dst_low != lhs_low) {
1377 __ Move(dst_low, lhs_low);
1378 }
1379 if (IsUint<16>(high)) {
1380 __ Andi(dst_high, lhs_high, high);
1381 } else if (high != 0xFFFFFFFF) {
1382 if (high != low) {
1383 __ LoadConst32(TMP, high);
1384 }
1385 __ And(dst_high, lhs_high, TMP);
1386 } else if (dst_high != lhs_high) {
1387 __ Move(dst_high, lhs_high);
1388 }
1389 } else {
1390 if (instruction->IsSub()) {
1391 value = -value;
1392 } else {
1393 DCHECK(instruction->IsAdd());
1394 }
1395 int32_t low = Low32Bits(value);
1396 int32_t high = High32Bits(value);
1397 if (IsInt<16>(low)) {
1398 if (dst_low != lhs_low || low != 0) {
1399 __ Addiu(dst_low, lhs_low, low);
1400 }
1401 if (low != 0) {
1402 __ Sltiu(AT, dst_low, low);
1403 }
1404 } else {
1405 __ LoadConst32(TMP, low);
1406 __ Addu(dst_low, lhs_low, TMP);
1407 __ Sltu(AT, dst_low, TMP);
1408 }
1409 if (IsInt<16>(high)) {
1410 if (dst_high != lhs_high || high != 0) {
1411 __ Addiu(dst_high, lhs_high, high);
1412 }
1413 } else {
1414 if (high != low) {
1415 __ LoadConst32(TMP, high);
1416 }
1417 __ Addu(dst_high, lhs_high, TMP);
1418 }
1419 if (low != 0) {
1420 __ Addu(dst_high, dst_high, AT);
1421 }
1422 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001423 }
1424 break;
1425 }
1426
1427 case Primitive::kPrimFloat:
1428 case Primitive::kPrimDouble: {
1429 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1430 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1431 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1432 if (instruction->IsAdd()) {
1433 if (type == Primitive::kPrimFloat) {
1434 __ AddS(dst, lhs, rhs);
1435 } else {
1436 __ AddD(dst, lhs, rhs);
1437 }
1438 } else {
1439 DCHECK(instruction->IsSub());
1440 if (type == Primitive::kPrimFloat) {
1441 __ SubS(dst, lhs, rhs);
1442 } else {
1443 __ SubD(dst, lhs, rhs);
1444 }
1445 }
1446 break;
1447 }
1448
1449 default:
1450 LOG(FATAL) << "Unexpected binary operation type " << type;
1451 }
1452}
1453
1454void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001455 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001456
1457 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1458 Primitive::Type type = instr->GetResultType();
1459 switch (type) {
1460 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001461 locations->SetInAt(0, Location::RequiresRegister());
1462 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1463 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1464 break;
1465 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001466 locations->SetInAt(0, Location::RequiresRegister());
1467 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1468 locations->SetOut(Location::RequiresRegister());
1469 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001470 default:
1471 LOG(FATAL) << "Unexpected shift type " << type;
1472 }
1473}
1474
1475static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1476
1477void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001478 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001479 LocationSummary* locations = instr->GetLocations();
1480 Primitive::Type type = instr->GetType();
1481
1482 Location rhs_location = locations->InAt(1);
1483 bool use_imm = rhs_location.IsConstant();
1484 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1485 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001486 const uint32_t shift_mask = (type == Primitive::kPrimInt)
1487 ? kMaxIntShiftValue
1488 : kMaxLongShiftValue;
1489 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001490 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1491 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001492
1493 switch (type) {
1494 case Primitive::kPrimInt: {
1495 Register dst = locations->Out().AsRegister<Register>();
1496 Register lhs = locations->InAt(0).AsRegister<Register>();
1497 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001498 if (shift_value == 0) {
1499 if (dst != lhs) {
1500 __ Move(dst, lhs);
1501 }
1502 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001503 __ Sll(dst, lhs, shift_value);
1504 } else if (instr->IsShr()) {
1505 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001506 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001507 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001508 } else {
1509 if (has_ins_rotr) {
1510 __ Rotr(dst, lhs, shift_value);
1511 } else {
1512 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1513 __ Srl(dst, lhs, shift_value);
1514 __ Or(dst, dst, TMP);
1515 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001516 }
1517 } else {
1518 if (instr->IsShl()) {
1519 __ Sllv(dst, lhs, rhs_reg);
1520 } else if (instr->IsShr()) {
1521 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001522 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001523 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001524 } else {
1525 if (has_ins_rotr) {
1526 __ Rotrv(dst, lhs, rhs_reg);
1527 } else {
1528 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001529 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1530 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1531 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1532 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1533 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001534 __ Sllv(TMP, lhs, TMP);
1535 __ Srlv(dst, lhs, rhs_reg);
1536 __ Or(dst, dst, TMP);
1537 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001538 }
1539 }
1540 break;
1541 }
1542
1543 case Primitive::kPrimLong: {
1544 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1545 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1546 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1547 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1548 if (use_imm) {
1549 if (shift_value == 0) {
1550 codegen_->Move64(locations->Out(), locations->InAt(0));
1551 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001552 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001553 if (instr->IsShl()) {
1554 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1555 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1556 __ Sll(dst_low, lhs_low, shift_value);
1557 } else if (instr->IsShr()) {
1558 __ Srl(dst_low, lhs_low, shift_value);
1559 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1560 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001561 } else if (instr->IsUShr()) {
1562 __ Srl(dst_low, lhs_low, shift_value);
1563 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1564 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001565 } else {
1566 __ Srl(dst_low, lhs_low, shift_value);
1567 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1568 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001569 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001570 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001571 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001572 if (instr->IsShl()) {
1573 __ Sll(dst_low, lhs_low, shift_value);
1574 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1575 __ Sll(dst_high, lhs_high, shift_value);
1576 __ Or(dst_high, dst_high, TMP);
1577 } else if (instr->IsShr()) {
1578 __ Sra(dst_high, lhs_high, shift_value);
1579 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1580 __ Srl(dst_low, lhs_low, shift_value);
1581 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001582 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001583 __ Srl(dst_high, lhs_high, shift_value);
1584 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1585 __ Srl(dst_low, lhs_low, shift_value);
1586 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001587 } else {
1588 __ Srl(TMP, lhs_low, shift_value);
1589 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1590 __ Or(dst_low, dst_low, TMP);
1591 __ Srl(TMP, lhs_high, shift_value);
1592 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1593 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001594 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001595 }
1596 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001597 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001598 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001599 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001600 __ Move(dst_low, ZERO);
1601 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001602 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001603 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001604 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001605 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001606 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001607 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001608 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001609 // 64-bit rotation by 32 is just a swap.
1610 __ Move(dst_low, lhs_high);
1611 __ Move(dst_high, lhs_low);
1612 } else {
1613 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001614 __ Srl(dst_low, lhs_high, shift_value_high);
1615 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1616 __ Srl(dst_high, lhs_low, shift_value_high);
1617 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001618 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001619 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1620 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001621 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001622 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1623 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001624 __ Or(dst_high, dst_high, TMP);
1625 }
1626 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001627 }
1628 }
1629 } else {
1630 MipsLabel done;
1631 if (instr->IsShl()) {
1632 __ Sllv(dst_low, lhs_low, rhs_reg);
1633 __ Nor(AT, ZERO, rhs_reg);
1634 __ Srl(TMP, lhs_low, 1);
1635 __ Srlv(TMP, TMP, AT);
1636 __ Sllv(dst_high, lhs_high, rhs_reg);
1637 __ Or(dst_high, dst_high, TMP);
1638 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1639 __ Beqz(TMP, &done);
1640 __ Move(dst_high, dst_low);
1641 __ Move(dst_low, ZERO);
1642 } else if (instr->IsShr()) {
1643 __ Srav(dst_high, lhs_high, rhs_reg);
1644 __ Nor(AT, ZERO, rhs_reg);
1645 __ Sll(TMP, lhs_high, 1);
1646 __ Sllv(TMP, TMP, AT);
1647 __ Srlv(dst_low, lhs_low, rhs_reg);
1648 __ Or(dst_low, dst_low, TMP);
1649 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1650 __ Beqz(TMP, &done);
1651 __ Move(dst_low, dst_high);
1652 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001653 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001654 __ Srlv(dst_high, lhs_high, rhs_reg);
1655 __ Nor(AT, ZERO, rhs_reg);
1656 __ Sll(TMP, lhs_high, 1);
1657 __ Sllv(TMP, TMP, AT);
1658 __ Srlv(dst_low, lhs_low, rhs_reg);
1659 __ Or(dst_low, dst_low, TMP);
1660 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1661 __ Beqz(TMP, &done);
1662 __ Move(dst_low, dst_high);
1663 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001664 } else {
1665 __ Nor(AT, ZERO, rhs_reg);
1666 __ Srlv(TMP, lhs_low, rhs_reg);
1667 __ Sll(dst_low, lhs_high, 1);
1668 __ Sllv(dst_low, dst_low, AT);
1669 __ Or(dst_low, dst_low, TMP);
1670 __ Srlv(TMP, lhs_high, rhs_reg);
1671 __ Sll(dst_high, lhs_low, 1);
1672 __ Sllv(dst_high, dst_high, AT);
1673 __ Or(dst_high, dst_high, TMP);
1674 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1675 __ Beqz(TMP, &done);
1676 __ Move(TMP, dst_high);
1677 __ Move(dst_high, dst_low);
1678 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001679 }
1680 __ Bind(&done);
1681 }
1682 break;
1683 }
1684
1685 default:
1686 LOG(FATAL) << "Unexpected shift operation type " << type;
1687 }
1688}
1689
1690void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1691 HandleBinaryOp(instruction);
1692}
1693
1694void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1695 HandleBinaryOp(instruction);
1696}
1697
1698void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1699 HandleBinaryOp(instruction);
1700}
1701
1702void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1703 HandleBinaryOp(instruction);
1704}
1705
1706void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1707 LocationSummary* locations =
1708 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1709 locations->SetInAt(0, Location::RequiresRegister());
1710 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1711 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1712 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1713 } else {
1714 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1715 }
1716}
1717
1718void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1719 LocationSummary* locations = instruction->GetLocations();
1720 Register obj = locations->InAt(0).AsRegister<Register>();
1721 Location index = locations->InAt(1);
1722 Primitive::Type type = instruction->GetType();
1723
1724 switch (type) {
1725 case Primitive::kPrimBoolean: {
1726 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1727 Register out = locations->Out().AsRegister<Register>();
1728 if (index.IsConstant()) {
1729 size_t offset =
1730 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1731 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1732 } else {
1733 __ Addu(TMP, obj, index.AsRegister<Register>());
1734 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1735 }
1736 break;
1737 }
1738
1739 case Primitive::kPrimByte: {
1740 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1741 Register out = locations->Out().AsRegister<Register>();
1742 if (index.IsConstant()) {
1743 size_t offset =
1744 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1745 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1746 } else {
1747 __ Addu(TMP, obj, index.AsRegister<Register>());
1748 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1749 }
1750 break;
1751 }
1752
1753 case Primitive::kPrimShort: {
1754 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1755 Register out = locations->Out().AsRegister<Register>();
1756 if (index.IsConstant()) {
1757 size_t offset =
1758 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1759 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1760 } else {
1761 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1762 __ Addu(TMP, obj, TMP);
1763 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1764 }
1765 break;
1766 }
1767
1768 case Primitive::kPrimChar: {
1769 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1770 Register out = locations->Out().AsRegister<Register>();
1771 if (index.IsConstant()) {
1772 size_t offset =
1773 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1774 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1775 } else {
1776 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1777 __ Addu(TMP, obj, TMP);
1778 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1779 }
1780 break;
1781 }
1782
1783 case Primitive::kPrimInt:
1784 case Primitive::kPrimNot: {
1785 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1786 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1787 Register out = locations->Out().AsRegister<Register>();
1788 if (index.IsConstant()) {
1789 size_t offset =
1790 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1791 __ LoadFromOffset(kLoadWord, out, obj, offset);
1792 } else {
1793 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1794 __ Addu(TMP, obj, TMP);
1795 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1796 }
1797 break;
1798 }
1799
1800 case Primitive::kPrimLong: {
1801 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1802 Register out = locations->Out().AsRegisterPairLow<Register>();
1803 if (index.IsConstant()) {
1804 size_t offset =
1805 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1806 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1807 } else {
1808 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1809 __ Addu(TMP, obj, TMP);
1810 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1811 }
1812 break;
1813 }
1814
1815 case Primitive::kPrimFloat: {
1816 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1817 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1818 if (index.IsConstant()) {
1819 size_t offset =
1820 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1821 __ LoadSFromOffset(out, obj, offset);
1822 } else {
1823 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1824 __ Addu(TMP, obj, TMP);
1825 __ LoadSFromOffset(out, TMP, data_offset);
1826 }
1827 break;
1828 }
1829
1830 case Primitive::kPrimDouble: {
1831 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1832 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1833 if (index.IsConstant()) {
1834 size_t offset =
1835 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1836 __ LoadDFromOffset(out, obj, offset);
1837 } else {
1838 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1839 __ Addu(TMP, obj, TMP);
1840 __ LoadDFromOffset(out, TMP, data_offset);
1841 }
1842 break;
1843 }
1844
1845 case Primitive::kPrimVoid:
1846 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1847 UNREACHABLE();
1848 }
1849 codegen_->MaybeRecordImplicitNullCheck(instruction);
1850}
1851
1852void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1853 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1854 locations->SetInAt(0, Location::RequiresRegister());
1855 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1856}
1857
1858void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1859 LocationSummary* locations = instruction->GetLocations();
1860 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1861 Register obj = locations->InAt(0).AsRegister<Register>();
1862 Register out = locations->Out().AsRegister<Register>();
1863 __ LoadFromOffset(kLoadWord, out, obj, offset);
1864 codegen_->MaybeRecordImplicitNullCheck(instruction);
1865}
1866
1867void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001868 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001869 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1870 instruction,
Pavle Batuta934808f2015-11-03 13:23:54 +01001871 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1872 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001873 InvokeRuntimeCallingConvention calling_convention;
1874 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1875 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1876 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1877 } else {
1878 locations->SetInAt(0, Location::RequiresRegister());
1879 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1880 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1881 locations->SetInAt(2, Location::RequiresFpuRegister());
1882 } else {
1883 locations->SetInAt(2, Location::RequiresRegister());
1884 }
1885 }
1886}
1887
1888void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1889 LocationSummary* locations = instruction->GetLocations();
1890 Register obj = locations->InAt(0).AsRegister<Register>();
1891 Location index = locations->InAt(1);
1892 Primitive::Type value_type = instruction->GetComponentType();
1893 bool needs_runtime_call = locations->WillCall();
1894 bool needs_write_barrier =
1895 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1896
1897 switch (value_type) {
1898 case Primitive::kPrimBoolean:
1899 case Primitive::kPrimByte: {
1900 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1901 Register value = locations->InAt(2).AsRegister<Register>();
1902 if (index.IsConstant()) {
1903 size_t offset =
1904 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1905 __ StoreToOffset(kStoreByte, value, obj, offset);
1906 } else {
1907 __ Addu(TMP, obj, index.AsRegister<Register>());
1908 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1909 }
1910 break;
1911 }
1912
1913 case Primitive::kPrimShort:
1914 case Primitive::kPrimChar: {
1915 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1916 Register value = locations->InAt(2).AsRegister<Register>();
1917 if (index.IsConstant()) {
1918 size_t offset =
1919 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1920 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1921 } else {
1922 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1923 __ Addu(TMP, obj, TMP);
1924 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1925 }
1926 break;
1927 }
1928
1929 case Primitive::kPrimInt:
1930 case Primitive::kPrimNot: {
1931 if (!needs_runtime_call) {
1932 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1933 Register value = locations->InAt(2).AsRegister<Register>();
1934 if (index.IsConstant()) {
1935 size_t offset =
1936 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1937 __ StoreToOffset(kStoreWord, value, obj, offset);
1938 } else {
1939 DCHECK(index.IsRegister()) << index;
1940 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1941 __ Addu(TMP, obj, TMP);
1942 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1943 }
1944 codegen_->MaybeRecordImplicitNullCheck(instruction);
1945 if (needs_write_barrier) {
1946 DCHECK_EQ(value_type, Primitive::kPrimNot);
1947 codegen_->MarkGCCard(obj, value);
1948 }
1949 } else {
1950 DCHECK_EQ(value_type, Primitive::kPrimNot);
1951 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1952 instruction,
1953 instruction->GetDexPc(),
1954 nullptr,
1955 IsDirectEntrypoint(kQuickAputObject));
1956 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
1957 }
1958 break;
1959 }
1960
1961 case Primitive::kPrimLong: {
1962 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1963 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
1964 if (index.IsConstant()) {
1965 size_t offset =
1966 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1967 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1968 } else {
1969 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1970 __ Addu(TMP, obj, TMP);
1971 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1972 }
1973 break;
1974 }
1975
1976 case Primitive::kPrimFloat: {
1977 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1978 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1979 DCHECK(locations->InAt(2).IsFpuRegister());
1980 if (index.IsConstant()) {
1981 size_t offset =
1982 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1983 __ StoreSToOffset(value, obj, offset);
1984 } else {
1985 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1986 __ Addu(TMP, obj, TMP);
1987 __ StoreSToOffset(value, TMP, data_offset);
1988 }
1989 break;
1990 }
1991
1992 case Primitive::kPrimDouble: {
1993 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1994 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1995 DCHECK(locations->InAt(2).IsFpuRegister());
1996 if (index.IsConstant()) {
1997 size_t offset =
1998 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1999 __ StoreDToOffset(value, obj, offset);
2000 } else {
2001 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2002 __ Addu(TMP, obj, TMP);
2003 __ StoreDToOffset(value, TMP, data_offset);
2004 }
2005 break;
2006 }
2007
2008 case Primitive::kPrimVoid:
2009 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2010 UNREACHABLE();
2011 }
2012
2013 // Ints and objects are handled in the switch.
2014 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
2015 codegen_->MaybeRecordImplicitNullCheck(instruction);
2016 }
2017}
2018
2019void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2020 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2021 ? LocationSummary::kCallOnSlowPath
2022 : LocationSummary::kNoCall;
2023 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2024 locations->SetInAt(0, Location::RequiresRegister());
2025 locations->SetInAt(1, Location::RequiresRegister());
2026 if (instruction->HasUses()) {
2027 locations->SetOut(Location::SameAsFirstInput());
2028 }
2029}
2030
2031void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2032 LocationSummary* locations = instruction->GetLocations();
2033 BoundsCheckSlowPathMIPS* slow_path =
2034 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2035 codegen_->AddSlowPath(slow_path);
2036
2037 Register index = locations->InAt(0).AsRegister<Register>();
2038 Register length = locations->InAt(1).AsRegister<Register>();
2039
2040 // length is limited by the maximum positive signed 32-bit integer.
2041 // Unsigned comparison of length and index checks for index < 0
2042 // and for length <= index simultaneously.
2043 __ Bgeu(index, length, slow_path->GetEntryLabel());
2044}
2045
2046void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2047 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2048 instruction,
2049 LocationSummary::kCallOnSlowPath);
2050 locations->SetInAt(0, Location::RequiresRegister());
2051 locations->SetInAt(1, Location::RequiresRegister());
2052 // Note that TypeCheckSlowPathMIPS uses this register too.
2053 locations->AddTemp(Location::RequiresRegister());
2054}
2055
2056void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2057 LocationSummary* locations = instruction->GetLocations();
2058 Register obj = locations->InAt(0).AsRegister<Register>();
2059 Register cls = locations->InAt(1).AsRegister<Register>();
2060 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2061
2062 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2063 codegen_->AddSlowPath(slow_path);
2064
2065 // TODO: avoid this check if we know obj is not null.
2066 __ Beqz(obj, slow_path->GetExitLabel());
2067 // Compare the class of `obj` with `cls`.
2068 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2069 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2070 __ Bind(slow_path->GetExitLabel());
2071}
2072
2073void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2074 LocationSummary* locations =
2075 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2076 locations->SetInAt(0, Location::RequiresRegister());
2077 if (check->HasUses()) {
2078 locations->SetOut(Location::SameAsFirstInput());
2079 }
2080}
2081
2082void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2083 // We assume the class is not null.
2084 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2085 check->GetLoadClass(),
2086 check,
2087 check->GetDexPc(),
2088 true);
2089 codegen_->AddSlowPath(slow_path);
2090 GenerateClassInitializationCheck(slow_path,
2091 check->GetLocations()->InAt(0).AsRegister<Register>());
2092}
2093
2094void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2095 Primitive::Type in_type = compare->InputAt(0)->GetType();
2096
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002097 LocationSummary* locations =
2098 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002099
2100 switch (in_type) {
2101 case Primitive::kPrimLong:
2102 locations->SetInAt(0, Location::RequiresRegister());
2103 locations->SetInAt(1, Location::RequiresRegister());
2104 // Output overlaps because it is written before doing the low comparison.
2105 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2106 break;
2107
2108 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002109 case Primitive::kPrimDouble:
2110 locations->SetInAt(0, Location::RequiresFpuRegister());
2111 locations->SetInAt(1, Location::RequiresFpuRegister());
2112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002113 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002114
2115 default:
2116 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2117 }
2118}
2119
2120void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2121 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002122 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002123 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002124 bool gt_bias = instruction->IsGtBias();
2125 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002126
2127 // 0 if: left == right
2128 // 1 if: left > right
2129 // -1 if: left < right
2130 switch (in_type) {
2131 case Primitive::kPrimLong: {
2132 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002133 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2134 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2135 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2136 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2137 // TODO: more efficient (direct) comparison with a constant.
2138 __ Slt(TMP, lhs_high, rhs_high);
2139 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2140 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2141 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2142 __ Sltu(TMP, lhs_low, rhs_low);
2143 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2144 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2145 __ Bind(&done);
2146 break;
2147 }
2148
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002149 case Primitive::kPrimFloat: {
2150 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2151 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2152 MipsLabel done;
2153 if (isR6) {
2154 __ CmpEqS(FTMP, lhs, rhs);
2155 __ LoadConst32(res, 0);
2156 __ Bc1nez(FTMP, &done);
2157 if (gt_bias) {
2158 __ CmpLtS(FTMP, lhs, rhs);
2159 __ LoadConst32(res, -1);
2160 __ Bc1nez(FTMP, &done);
2161 __ LoadConst32(res, 1);
2162 } else {
2163 __ CmpLtS(FTMP, rhs, lhs);
2164 __ LoadConst32(res, 1);
2165 __ Bc1nez(FTMP, &done);
2166 __ LoadConst32(res, -1);
2167 }
2168 } else {
2169 if (gt_bias) {
2170 __ ColtS(0, lhs, rhs);
2171 __ LoadConst32(res, -1);
2172 __ Bc1t(0, &done);
2173 __ CeqS(0, lhs, rhs);
2174 __ LoadConst32(res, 1);
2175 __ Movt(res, ZERO, 0);
2176 } else {
2177 __ ColtS(0, rhs, lhs);
2178 __ LoadConst32(res, 1);
2179 __ Bc1t(0, &done);
2180 __ CeqS(0, lhs, rhs);
2181 __ LoadConst32(res, -1);
2182 __ Movt(res, ZERO, 0);
2183 }
2184 }
2185 __ Bind(&done);
2186 break;
2187 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002188 case Primitive::kPrimDouble: {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002189 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2190 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2191 MipsLabel done;
2192 if (isR6) {
2193 __ CmpEqD(FTMP, lhs, rhs);
2194 __ LoadConst32(res, 0);
2195 __ Bc1nez(FTMP, &done);
2196 if (gt_bias) {
2197 __ CmpLtD(FTMP, lhs, rhs);
2198 __ LoadConst32(res, -1);
2199 __ Bc1nez(FTMP, &done);
2200 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002201 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002202 __ CmpLtD(FTMP, rhs, lhs);
2203 __ LoadConst32(res, 1);
2204 __ Bc1nez(FTMP, &done);
2205 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002206 }
2207 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002208 if (gt_bias) {
2209 __ ColtD(0, lhs, rhs);
2210 __ LoadConst32(res, -1);
2211 __ Bc1t(0, &done);
2212 __ CeqD(0, lhs, rhs);
2213 __ LoadConst32(res, 1);
2214 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002215 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002216 __ ColtD(0, rhs, lhs);
2217 __ LoadConst32(res, 1);
2218 __ Bc1t(0, &done);
2219 __ CeqD(0, lhs, rhs);
2220 __ LoadConst32(res, -1);
2221 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002222 }
2223 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002224 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002225 break;
2226 }
2227
2228 default:
2229 LOG(FATAL) << "Unimplemented compare type " << in_type;
2230 }
2231}
2232
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002233void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002234 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002235 switch (instruction->InputAt(0)->GetType()) {
2236 default:
2237 case Primitive::kPrimLong:
2238 locations->SetInAt(0, Location::RequiresRegister());
2239 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2240 break;
2241
2242 case Primitive::kPrimFloat:
2243 case Primitive::kPrimDouble:
2244 locations->SetInAt(0, Location::RequiresFpuRegister());
2245 locations->SetInAt(1, Location::RequiresFpuRegister());
2246 break;
2247 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002248 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002249 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2250 }
2251}
2252
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002253void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002254 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002255 return;
2256 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002257
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002258 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002259 LocationSummary* locations = instruction->GetLocations();
2260 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002261 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002263 switch (type) {
2264 default:
2265 // Integer case.
2266 GenerateIntCompare(instruction->GetCondition(), locations);
2267 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002268
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002269 case Primitive::kPrimLong:
2270 // TODO: don't use branches.
2271 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002272 break;
2273
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002274 case Primitive::kPrimFloat:
2275 case Primitive::kPrimDouble:
2276 // TODO: don't use branches.
2277 GenerateFpCompareAndBranch(instruction->GetCondition(),
2278 instruction->IsGtBias(),
2279 type,
2280 locations,
2281 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002282 break;
2283 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002284
2285 // Convert the branches into the result.
2286 MipsLabel done;
2287
2288 // False case: result = 0.
2289 __ LoadConst32(dst, 0);
2290 __ B(&done);
2291
2292 // True case: result = 1.
2293 __ Bind(&true_label);
2294 __ LoadConst32(dst, 1);
2295 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002296}
2297
Alexey Frunze7e99e052015-11-24 19:28:01 -08002298void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2299 DCHECK(instruction->IsDiv() || instruction->IsRem());
2300 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2301
2302 LocationSummary* locations = instruction->GetLocations();
2303 Location second = locations->InAt(1);
2304 DCHECK(second.IsConstant());
2305
2306 Register out = locations->Out().AsRegister<Register>();
2307 Register dividend = locations->InAt(0).AsRegister<Register>();
2308 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2309 DCHECK(imm == 1 || imm == -1);
2310
2311 if (instruction->IsRem()) {
2312 __ Move(out, ZERO);
2313 } else {
2314 if (imm == -1) {
2315 __ Subu(out, ZERO, dividend);
2316 } else if (out != dividend) {
2317 __ Move(out, dividend);
2318 }
2319 }
2320}
2321
2322void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2323 DCHECK(instruction->IsDiv() || instruction->IsRem());
2324 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2325
2326 LocationSummary* locations = instruction->GetLocations();
2327 Location second = locations->InAt(1);
2328 DCHECK(second.IsConstant());
2329
2330 Register out = locations->Out().AsRegister<Register>();
2331 Register dividend = locations->InAt(0).AsRegister<Register>();
2332 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002333 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002334 int ctz_imm = CTZ(abs_imm);
2335
2336 if (instruction->IsDiv()) {
2337 if (ctz_imm == 1) {
2338 // Fast path for division by +/-2, which is very common.
2339 __ Srl(TMP, dividend, 31);
2340 } else {
2341 __ Sra(TMP, dividend, 31);
2342 __ Srl(TMP, TMP, 32 - ctz_imm);
2343 }
2344 __ Addu(out, dividend, TMP);
2345 __ Sra(out, out, ctz_imm);
2346 if (imm < 0) {
2347 __ Subu(out, ZERO, out);
2348 }
2349 } else {
2350 if (ctz_imm == 1) {
2351 // Fast path for modulo +/-2, which is very common.
2352 __ Sra(TMP, dividend, 31);
2353 __ Subu(out, dividend, TMP);
2354 __ Andi(out, out, 1);
2355 __ Addu(out, out, TMP);
2356 } else {
2357 __ Sra(TMP, dividend, 31);
2358 __ Srl(TMP, TMP, 32 - ctz_imm);
2359 __ Addu(out, dividend, TMP);
2360 if (IsUint<16>(abs_imm - 1)) {
2361 __ Andi(out, out, abs_imm - 1);
2362 } else {
2363 __ Sll(out, out, 32 - ctz_imm);
2364 __ Srl(out, out, 32 - ctz_imm);
2365 }
2366 __ Subu(out, out, TMP);
2367 }
2368 }
2369}
2370
2371void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2372 DCHECK(instruction->IsDiv() || instruction->IsRem());
2373 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2374
2375 LocationSummary* locations = instruction->GetLocations();
2376 Location second = locations->InAt(1);
2377 DCHECK(second.IsConstant());
2378
2379 Register out = locations->Out().AsRegister<Register>();
2380 Register dividend = locations->InAt(0).AsRegister<Register>();
2381 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2382
2383 int64_t magic;
2384 int shift;
2385 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2386
2387 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2388
2389 __ LoadConst32(TMP, magic);
2390 if (isR6) {
2391 __ MuhR6(TMP, dividend, TMP);
2392 } else {
2393 __ MultR2(dividend, TMP);
2394 __ Mfhi(TMP);
2395 }
2396 if (imm > 0 && magic < 0) {
2397 __ Addu(TMP, TMP, dividend);
2398 } else if (imm < 0 && magic > 0) {
2399 __ Subu(TMP, TMP, dividend);
2400 }
2401
2402 if (shift != 0) {
2403 __ Sra(TMP, TMP, shift);
2404 }
2405
2406 if (instruction->IsDiv()) {
2407 __ Sra(out, TMP, 31);
2408 __ Subu(out, TMP, out);
2409 } else {
2410 __ Sra(AT, TMP, 31);
2411 __ Subu(AT, TMP, AT);
2412 __ LoadConst32(TMP, imm);
2413 if (isR6) {
2414 __ MulR6(TMP, AT, TMP);
2415 } else {
2416 __ MulR2(TMP, AT, TMP);
2417 }
2418 __ Subu(out, dividend, TMP);
2419 }
2420}
2421
2422void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2423 DCHECK(instruction->IsDiv() || instruction->IsRem());
2424 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2425
2426 LocationSummary* locations = instruction->GetLocations();
2427 Register out = locations->Out().AsRegister<Register>();
2428 Location second = locations->InAt(1);
2429
2430 if (second.IsConstant()) {
2431 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2432 if (imm == 0) {
2433 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2434 } else if (imm == 1 || imm == -1) {
2435 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002436 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002437 DivRemByPowerOfTwo(instruction);
2438 } else {
2439 DCHECK(imm <= -2 || imm >= 2);
2440 GenerateDivRemWithAnyConstant(instruction);
2441 }
2442 } else {
2443 Register dividend = locations->InAt(0).AsRegister<Register>();
2444 Register divisor = second.AsRegister<Register>();
2445 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2446 if (instruction->IsDiv()) {
2447 if (isR6) {
2448 __ DivR6(out, dividend, divisor);
2449 } else {
2450 __ DivR2(out, dividend, divisor);
2451 }
2452 } else {
2453 if (isR6) {
2454 __ ModR6(out, dividend, divisor);
2455 } else {
2456 __ ModR2(out, dividend, divisor);
2457 }
2458 }
2459 }
2460}
2461
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002462void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2463 Primitive::Type type = div->GetResultType();
2464 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
2465 ? LocationSummary::kCall
2466 : LocationSummary::kNoCall;
2467
2468 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2469
2470 switch (type) {
2471 case Primitive::kPrimInt:
2472 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002473 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2475 break;
2476
2477 case Primitive::kPrimLong: {
2478 InvokeRuntimeCallingConvention calling_convention;
2479 locations->SetInAt(0, Location::RegisterPairLocation(
2480 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2481 locations->SetInAt(1, Location::RegisterPairLocation(
2482 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2483 locations->SetOut(calling_convention.GetReturnLocation(type));
2484 break;
2485 }
2486
2487 case Primitive::kPrimFloat:
2488 case Primitive::kPrimDouble:
2489 locations->SetInAt(0, Location::RequiresFpuRegister());
2490 locations->SetInAt(1, Location::RequiresFpuRegister());
2491 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2492 break;
2493
2494 default:
2495 LOG(FATAL) << "Unexpected div type " << type;
2496 }
2497}
2498
2499void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2500 Primitive::Type type = instruction->GetType();
2501 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002502
2503 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002504 case Primitive::kPrimInt:
2505 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002506 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002507 case Primitive::kPrimLong: {
2508 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2509 instruction,
2510 instruction->GetDexPc(),
2511 nullptr,
2512 IsDirectEntrypoint(kQuickLdiv));
2513 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2514 break;
2515 }
2516 case Primitive::kPrimFloat:
2517 case Primitive::kPrimDouble: {
2518 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2519 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2520 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2521 if (type == Primitive::kPrimFloat) {
2522 __ DivS(dst, lhs, rhs);
2523 } else {
2524 __ DivD(dst, lhs, rhs);
2525 }
2526 break;
2527 }
2528 default:
2529 LOG(FATAL) << "Unexpected div type " << type;
2530 }
2531}
2532
2533void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2534 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2535 ? LocationSummary::kCallOnSlowPath
2536 : LocationSummary::kNoCall;
2537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2538 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2539 if (instruction->HasUses()) {
2540 locations->SetOut(Location::SameAsFirstInput());
2541 }
2542}
2543
2544void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2545 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2546 codegen_->AddSlowPath(slow_path);
2547 Location value = instruction->GetLocations()->InAt(0);
2548 Primitive::Type type = instruction->GetType();
2549
2550 switch (type) {
2551 case Primitive::kPrimByte:
2552 case Primitive::kPrimChar:
2553 case Primitive::kPrimShort:
2554 case Primitive::kPrimInt: {
2555 if (value.IsConstant()) {
2556 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2557 __ B(slow_path->GetEntryLabel());
2558 } else {
2559 // A division by a non-null constant is valid. We don't need to perform
2560 // any check, so simply fall through.
2561 }
2562 } else {
2563 DCHECK(value.IsRegister()) << value;
2564 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2565 }
2566 break;
2567 }
2568 case Primitive::kPrimLong: {
2569 if (value.IsConstant()) {
2570 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2571 __ B(slow_path->GetEntryLabel());
2572 } else {
2573 // A division by a non-null constant is valid. We don't need to perform
2574 // any check, so simply fall through.
2575 }
2576 } else {
2577 DCHECK(value.IsRegisterPair()) << value;
2578 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2579 __ Beqz(TMP, slow_path->GetEntryLabel());
2580 }
2581 break;
2582 }
2583 default:
2584 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2585 }
2586}
2587
2588void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2589 LocationSummary* locations =
2590 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2591 locations->SetOut(Location::ConstantLocation(constant));
2592}
2593
2594void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2595 // Will be generated at use site.
2596}
2597
2598void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2599 exit->SetLocations(nullptr);
2600}
2601
2602void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2603}
2604
2605void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2606 LocationSummary* locations =
2607 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2608 locations->SetOut(Location::ConstantLocation(constant));
2609}
2610
2611void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2612 // Will be generated at use site.
2613}
2614
2615void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2616 got->SetLocations(nullptr);
2617}
2618
2619void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2620 DCHECK(!successor->IsExitBlock());
2621 HBasicBlock* block = got->GetBlock();
2622 HInstruction* previous = got->GetPrevious();
2623 HLoopInformation* info = block->GetLoopInformation();
2624
2625 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2626 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2627 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2628 return;
2629 }
2630 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2631 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2632 }
2633 if (!codegen_->GoesToNextBlock(block, successor)) {
2634 __ B(codegen_->GetLabelOf(successor));
2635 }
2636}
2637
2638void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2639 HandleGoto(got, got->GetSuccessor());
2640}
2641
2642void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2643 try_boundary->SetLocations(nullptr);
2644}
2645
2646void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2647 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2648 if (!successor->IsExitBlock()) {
2649 HandleGoto(try_boundary, successor);
2650 }
2651}
2652
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002653void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2654 LocationSummary* locations) {
2655 Register dst = locations->Out().AsRegister<Register>();
2656 Register lhs = locations->InAt(0).AsRegister<Register>();
2657 Location rhs_location = locations->InAt(1);
2658 Register rhs_reg = ZERO;
2659 int64_t rhs_imm = 0;
2660 bool use_imm = rhs_location.IsConstant();
2661 if (use_imm) {
2662 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2663 } else {
2664 rhs_reg = rhs_location.AsRegister<Register>();
2665 }
2666
2667 switch (cond) {
2668 case kCondEQ:
2669 case kCondNE:
2670 if (use_imm && IsUint<16>(rhs_imm)) {
2671 __ Xori(dst, lhs, rhs_imm);
2672 } else {
2673 if (use_imm) {
2674 rhs_reg = TMP;
2675 __ LoadConst32(rhs_reg, rhs_imm);
2676 }
2677 __ Xor(dst, lhs, rhs_reg);
2678 }
2679 if (cond == kCondEQ) {
2680 __ Sltiu(dst, dst, 1);
2681 } else {
2682 __ Sltu(dst, ZERO, dst);
2683 }
2684 break;
2685
2686 case kCondLT:
2687 case kCondGE:
2688 if (use_imm && IsInt<16>(rhs_imm)) {
2689 __ Slti(dst, lhs, rhs_imm);
2690 } else {
2691 if (use_imm) {
2692 rhs_reg = TMP;
2693 __ LoadConst32(rhs_reg, rhs_imm);
2694 }
2695 __ Slt(dst, lhs, rhs_reg);
2696 }
2697 if (cond == kCondGE) {
2698 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2699 // only the slt instruction but no sge.
2700 __ Xori(dst, dst, 1);
2701 }
2702 break;
2703
2704 case kCondLE:
2705 case kCondGT:
2706 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2707 // Simulate lhs <= rhs via lhs < rhs + 1.
2708 __ Slti(dst, lhs, rhs_imm + 1);
2709 if (cond == kCondGT) {
2710 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2711 // only the slti instruction but no sgti.
2712 __ Xori(dst, dst, 1);
2713 }
2714 } else {
2715 if (use_imm) {
2716 rhs_reg = TMP;
2717 __ LoadConst32(rhs_reg, rhs_imm);
2718 }
2719 __ Slt(dst, rhs_reg, lhs);
2720 if (cond == kCondLE) {
2721 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2722 // only the slt instruction but no sle.
2723 __ Xori(dst, dst, 1);
2724 }
2725 }
2726 break;
2727
2728 case kCondB:
2729 case kCondAE:
2730 if (use_imm && IsInt<16>(rhs_imm)) {
2731 // Sltiu sign-extends its 16-bit immediate operand before
2732 // the comparison and thus lets us compare directly with
2733 // unsigned values in the ranges [0, 0x7fff] and
2734 // [0xffff8000, 0xffffffff].
2735 __ Sltiu(dst, lhs, rhs_imm);
2736 } else {
2737 if (use_imm) {
2738 rhs_reg = TMP;
2739 __ LoadConst32(rhs_reg, rhs_imm);
2740 }
2741 __ Sltu(dst, lhs, rhs_reg);
2742 }
2743 if (cond == kCondAE) {
2744 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2745 // only the sltu instruction but no sgeu.
2746 __ Xori(dst, dst, 1);
2747 }
2748 break;
2749
2750 case kCondBE:
2751 case kCondA:
2752 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2753 // Simulate lhs <= rhs via lhs < rhs + 1.
2754 // Note that this only works if rhs + 1 does not overflow
2755 // to 0, hence the check above.
2756 // Sltiu sign-extends its 16-bit immediate operand before
2757 // the comparison and thus lets us compare directly with
2758 // unsigned values in the ranges [0, 0x7fff] and
2759 // [0xffff8000, 0xffffffff].
2760 __ Sltiu(dst, lhs, rhs_imm + 1);
2761 if (cond == kCondA) {
2762 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2763 // only the sltiu instruction but no sgtiu.
2764 __ Xori(dst, dst, 1);
2765 }
2766 } else {
2767 if (use_imm) {
2768 rhs_reg = TMP;
2769 __ LoadConst32(rhs_reg, rhs_imm);
2770 }
2771 __ Sltu(dst, rhs_reg, lhs);
2772 if (cond == kCondBE) {
2773 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2774 // only the sltu instruction but no sleu.
2775 __ Xori(dst, dst, 1);
2776 }
2777 }
2778 break;
2779 }
2780}
2781
2782void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2783 LocationSummary* locations,
2784 MipsLabel* label) {
2785 Register lhs = locations->InAt(0).AsRegister<Register>();
2786 Location rhs_location = locations->InAt(1);
2787 Register rhs_reg = ZERO;
2788 int32_t rhs_imm = 0;
2789 bool use_imm = rhs_location.IsConstant();
2790 if (use_imm) {
2791 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2792 } else {
2793 rhs_reg = rhs_location.AsRegister<Register>();
2794 }
2795
2796 if (use_imm && rhs_imm == 0) {
2797 switch (cond) {
2798 case kCondEQ:
2799 case kCondBE: // <= 0 if zero
2800 __ Beqz(lhs, label);
2801 break;
2802 case kCondNE:
2803 case kCondA: // > 0 if non-zero
2804 __ Bnez(lhs, label);
2805 break;
2806 case kCondLT:
2807 __ Bltz(lhs, label);
2808 break;
2809 case kCondGE:
2810 __ Bgez(lhs, label);
2811 break;
2812 case kCondLE:
2813 __ Blez(lhs, label);
2814 break;
2815 case kCondGT:
2816 __ Bgtz(lhs, label);
2817 break;
2818 case kCondB: // always false
2819 break;
2820 case kCondAE: // always true
2821 __ B(label);
2822 break;
2823 }
2824 } else {
2825 if (use_imm) {
2826 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2827 rhs_reg = TMP;
2828 __ LoadConst32(rhs_reg, rhs_imm);
2829 }
2830 switch (cond) {
2831 case kCondEQ:
2832 __ Beq(lhs, rhs_reg, label);
2833 break;
2834 case kCondNE:
2835 __ Bne(lhs, rhs_reg, label);
2836 break;
2837 case kCondLT:
2838 __ Blt(lhs, rhs_reg, label);
2839 break;
2840 case kCondGE:
2841 __ Bge(lhs, rhs_reg, label);
2842 break;
2843 case kCondLE:
2844 __ Bge(rhs_reg, lhs, label);
2845 break;
2846 case kCondGT:
2847 __ Blt(rhs_reg, lhs, label);
2848 break;
2849 case kCondB:
2850 __ Bltu(lhs, rhs_reg, label);
2851 break;
2852 case kCondAE:
2853 __ Bgeu(lhs, rhs_reg, label);
2854 break;
2855 case kCondBE:
2856 __ Bgeu(rhs_reg, lhs, label);
2857 break;
2858 case kCondA:
2859 __ Bltu(rhs_reg, lhs, label);
2860 break;
2861 }
2862 }
2863}
2864
2865void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2866 LocationSummary* locations,
2867 MipsLabel* label) {
2868 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2869 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2870 Location rhs_location = locations->InAt(1);
2871 Register rhs_high = ZERO;
2872 Register rhs_low = ZERO;
2873 int64_t imm = 0;
2874 uint32_t imm_high = 0;
2875 uint32_t imm_low = 0;
2876 bool use_imm = rhs_location.IsConstant();
2877 if (use_imm) {
2878 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2879 imm_high = High32Bits(imm);
2880 imm_low = Low32Bits(imm);
2881 } else {
2882 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2883 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2884 }
2885
2886 if (use_imm && imm == 0) {
2887 switch (cond) {
2888 case kCondEQ:
2889 case kCondBE: // <= 0 if zero
2890 __ Or(TMP, lhs_high, lhs_low);
2891 __ Beqz(TMP, label);
2892 break;
2893 case kCondNE:
2894 case kCondA: // > 0 if non-zero
2895 __ Or(TMP, lhs_high, lhs_low);
2896 __ Bnez(TMP, label);
2897 break;
2898 case kCondLT:
2899 __ Bltz(lhs_high, label);
2900 break;
2901 case kCondGE:
2902 __ Bgez(lhs_high, label);
2903 break;
2904 case kCondLE:
2905 __ Or(TMP, lhs_high, lhs_low);
2906 __ Sra(AT, lhs_high, 31);
2907 __ Bgeu(AT, TMP, label);
2908 break;
2909 case kCondGT:
2910 __ Or(TMP, lhs_high, lhs_low);
2911 __ Sra(AT, lhs_high, 31);
2912 __ Bltu(AT, TMP, label);
2913 break;
2914 case kCondB: // always false
2915 break;
2916 case kCondAE: // always true
2917 __ B(label);
2918 break;
2919 }
2920 } else if (use_imm) {
2921 // TODO: more efficient comparison with constants without loading them into TMP/AT.
2922 switch (cond) {
2923 case kCondEQ:
2924 __ LoadConst32(TMP, imm_high);
2925 __ Xor(TMP, TMP, lhs_high);
2926 __ LoadConst32(AT, imm_low);
2927 __ Xor(AT, AT, lhs_low);
2928 __ Or(TMP, TMP, AT);
2929 __ Beqz(TMP, label);
2930 break;
2931 case kCondNE:
2932 __ LoadConst32(TMP, imm_high);
2933 __ Xor(TMP, TMP, lhs_high);
2934 __ LoadConst32(AT, imm_low);
2935 __ Xor(AT, AT, lhs_low);
2936 __ Or(TMP, TMP, AT);
2937 __ Bnez(TMP, label);
2938 break;
2939 case kCondLT:
2940 __ LoadConst32(TMP, imm_high);
2941 __ Blt(lhs_high, TMP, label);
2942 __ Slt(TMP, TMP, lhs_high);
2943 __ LoadConst32(AT, imm_low);
2944 __ Sltu(AT, lhs_low, AT);
2945 __ Blt(TMP, AT, label);
2946 break;
2947 case kCondGE:
2948 __ LoadConst32(TMP, imm_high);
2949 __ Blt(TMP, lhs_high, label);
2950 __ Slt(TMP, lhs_high, TMP);
2951 __ LoadConst32(AT, imm_low);
2952 __ Sltu(AT, lhs_low, AT);
2953 __ Or(TMP, TMP, AT);
2954 __ Beqz(TMP, label);
2955 break;
2956 case kCondLE:
2957 __ LoadConst32(TMP, imm_high);
2958 __ Blt(lhs_high, TMP, label);
2959 __ Slt(TMP, TMP, lhs_high);
2960 __ LoadConst32(AT, imm_low);
2961 __ Sltu(AT, AT, lhs_low);
2962 __ Or(TMP, TMP, AT);
2963 __ Beqz(TMP, label);
2964 break;
2965 case kCondGT:
2966 __ LoadConst32(TMP, imm_high);
2967 __ Blt(TMP, lhs_high, label);
2968 __ Slt(TMP, lhs_high, TMP);
2969 __ LoadConst32(AT, imm_low);
2970 __ Sltu(AT, AT, lhs_low);
2971 __ Blt(TMP, AT, label);
2972 break;
2973 case kCondB:
2974 __ LoadConst32(TMP, imm_high);
2975 __ Bltu(lhs_high, TMP, label);
2976 __ Sltu(TMP, TMP, lhs_high);
2977 __ LoadConst32(AT, imm_low);
2978 __ Sltu(AT, lhs_low, AT);
2979 __ Blt(TMP, AT, label);
2980 break;
2981 case kCondAE:
2982 __ LoadConst32(TMP, imm_high);
2983 __ Bltu(TMP, lhs_high, label);
2984 __ Sltu(TMP, lhs_high, TMP);
2985 __ LoadConst32(AT, imm_low);
2986 __ Sltu(AT, lhs_low, AT);
2987 __ Or(TMP, TMP, AT);
2988 __ Beqz(TMP, label);
2989 break;
2990 case kCondBE:
2991 __ LoadConst32(TMP, imm_high);
2992 __ Bltu(lhs_high, TMP, label);
2993 __ Sltu(TMP, TMP, lhs_high);
2994 __ LoadConst32(AT, imm_low);
2995 __ Sltu(AT, AT, lhs_low);
2996 __ Or(TMP, TMP, AT);
2997 __ Beqz(TMP, label);
2998 break;
2999 case kCondA:
3000 __ LoadConst32(TMP, imm_high);
3001 __ Bltu(TMP, lhs_high, label);
3002 __ Sltu(TMP, lhs_high, TMP);
3003 __ LoadConst32(AT, imm_low);
3004 __ Sltu(AT, AT, lhs_low);
3005 __ Blt(TMP, AT, label);
3006 break;
3007 }
3008 } else {
3009 switch (cond) {
3010 case kCondEQ:
3011 __ Xor(TMP, lhs_high, rhs_high);
3012 __ Xor(AT, lhs_low, rhs_low);
3013 __ Or(TMP, TMP, AT);
3014 __ Beqz(TMP, label);
3015 break;
3016 case kCondNE:
3017 __ Xor(TMP, lhs_high, rhs_high);
3018 __ Xor(AT, lhs_low, rhs_low);
3019 __ Or(TMP, TMP, AT);
3020 __ Bnez(TMP, label);
3021 break;
3022 case kCondLT:
3023 __ Blt(lhs_high, rhs_high, label);
3024 __ Slt(TMP, rhs_high, lhs_high);
3025 __ Sltu(AT, lhs_low, rhs_low);
3026 __ Blt(TMP, AT, label);
3027 break;
3028 case kCondGE:
3029 __ Blt(rhs_high, lhs_high, label);
3030 __ Slt(TMP, lhs_high, rhs_high);
3031 __ Sltu(AT, lhs_low, rhs_low);
3032 __ Or(TMP, TMP, AT);
3033 __ Beqz(TMP, label);
3034 break;
3035 case kCondLE:
3036 __ Blt(lhs_high, rhs_high, label);
3037 __ Slt(TMP, rhs_high, lhs_high);
3038 __ Sltu(AT, rhs_low, lhs_low);
3039 __ Or(TMP, TMP, AT);
3040 __ Beqz(TMP, label);
3041 break;
3042 case kCondGT:
3043 __ Blt(rhs_high, lhs_high, label);
3044 __ Slt(TMP, lhs_high, rhs_high);
3045 __ Sltu(AT, rhs_low, lhs_low);
3046 __ Blt(TMP, AT, label);
3047 break;
3048 case kCondB:
3049 __ Bltu(lhs_high, rhs_high, label);
3050 __ Sltu(TMP, rhs_high, lhs_high);
3051 __ Sltu(AT, lhs_low, rhs_low);
3052 __ Blt(TMP, AT, label);
3053 break;
3054 case kCondAE:
3055 __ Bltu(rhs_high, lhs_high, label);
3056 __ Sltu(TMP, lhs_high, rhs_high);
3057 __ Sltu(AT, lhs_low, rhs_low);
3058 __ Or(TMP, TMP, AT);
3059 __ Beqz(TMP, label);
3060 break;
3061 case kCondBE:
3062 __ Bltu(lhs_high, rhs_high, label);
3063 __ Sltu(TMP, rhs_high, lhs_high);
3064 __ Sltu(AT, rhs_low, lhs_low);
3065 __ Or(TMP, TMP, AT);
3066 __ Beqz(TMP, label);
3067 break;
3068 case kCondA:
3069 __ Bltu(rhs_high, lhs_high, label);
3070 __ Sltu(TMP, lhs_high, rhs_high);
3071 __ Sltu(AT, rhs_low, lhs_low);
3072 __ Blt(TMP, AT, label);
3073 break;
3074 }
3075 }
3076}
3077
3078void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3079 bool gt_bias,
3080 Primitive::Type type,
3081 LocationSummary* locations,
3082 MipsLabel* label) {
3083 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3084 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3085 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3086 if (type == Primitive::kPrimFloat) {
3087 if (isR6) {
3088 switch (cond) {
3089 case kCondEQ:
3090 __ CmpEqS(FTMP, lhs, rhs);
3091 __ Bc1nez(FTMP, label);
3092 break;
3093 case kCondNE:
3094 __ CmpEqS(FTMP, lhs, rhs);
3095 __ Bc1eqz(FTMP, label);
3096 break;
3097 case kCondLT:
3098 if (gt_bias) {
3099 __ CmpLtS(FTMP, lhs, rhs);
3100 } else {
3101 __ CmpUltS(FTMP, lhs, rhs);
3102 }
3103 __ Bc1nez(FTMP, label);
3104 break;
3105 case kCondLE:
3106 if (gt_bias) {
3107 __ CmpLeS(FTMP, lhs, rhs);
3108 } else {
3109 __ CmpUleS(FTMP, lhs, rhs);
3110 }
3111 __ Bc1nez(FTMP, label);
3112 break;
3113 case kCondGT:
3114 if (gt_bias) {
3115 __ CmpUltS(FTMP, rhs, lhs);
3116 } else {
3117 __ CmpLtS(FTMP, rhs, lhs);
3118 }
3119 __ Bc1nez(FTMP, label);
3120 break;
3121 case kCondGE:
3122 if (gt_bias) {
3123 __ CmpUleS(FTMP, rhs, lhs);
3124 } else {
3125 __ CmpLeS(FTMP, rhs, lhs);
3126 }
3127 __ Bc1nez(FTMP, label);
3128 break;
3129 default:
3130 LOG(FATAL) << "Unexpected non-floating-point condition";
3131 }
3132 } else {
3133 switch (cond) {
3134 case kCondEQ:
3135 __ CeqS(0, lhs, rhs);
3136 __ Bc1t(0, label);
3137 break;
3138 case kCondNE:
3139 __ CeqS(0, lhs, rhs);
3140 __ Bc1f(0, label);
3141 break;
3142 case kCondLT:
3143 if (gt_bias) {
3144 __ ColtS(0, lhs, rhs);
3145 } else {
3146 __ CultS(0, lhs, rhs);
3147 }
3148 __ Bc1t(0, label);
3149 break;
3150 case kCondLE:
3151 if (gt_bias) {
3152 __ ColeS(0, lhs, rhs);
3153 } else {
3154 __ CuleS(0, lhs, rhs);
3155 }
3156 __ Bc1t(0, label);
3157 break;
3158 case kCondGT:
3159 if (gt_bias) {
3160 __ CultS(0, rhs, lhs);
3161 } else {
3162 __ ColtS(0, rhs, lhs);
3163 }
3164 __ Bc1t(0, label);
3165 break;
3166 case kCondGE:
3167 if (gt_bias) {
3168 __ CuleS(0, rhs, lhs);
3169 } else {
3170 __ ColeS(0, rhs, lhs);
3171 }
3172 __ Bc1t(0, label);
3173 break;
3174 default:
3175 LOG(FATAL) << "Unexpected non-floating-point condition";
3176 }
3177 }
3178 } else {
3179 DCHECK_EQ(type, Primitive::kPrimDouble);
3180 if (isR6) {
3181 switch (cond) {
3182 case kCondEQ:
3183 __ CmpEqD(FTMP, lhs, rhs);
3184 __ Bc1nez(FTMP, label);
3185 break;
3186 case kCondNE:
3187 __ CmpEqD(FTMP, lhs, rhs);
3188 __ Bc1eqz(FTMP, label);
3189 break;
3190 case kCondLT:
3191 if (gt_bias) {
3192 __ CmpLtD(FTMP, lhs, rhs);
3193 } else {
3194 __ CmpUltD(FTMP, lhs, rhs);
3195 }
3196 __ Bc1nez(FTMP, label);
3197 break;
3198 case kCondLE:
3199 if (gt_bias) {
3200 __ CmpLeD(FTMP, lhs, rhs);
3201 } else {
3202 __ CmpUleD(FTMP, lhs, rhs);
3203 }
3204 __ Bc1nez(FTMP, label);
3205 break;
3206 case kCondGT:
3207 if (gt_bias) {
3208 __ CmpUltD(FTMP, rhs, lhs);
3209 } else {
3210 __ CmpLtD(FTMP, rhs, lhs);
3211 }
3212 __ Bc1nez(FTMP, label);
3213 break;
3214 case kCondGE:
3215 if (gt_bias) {
3216 __ CmpUleD(FTMP, rhs, lhs);
3217 } else {
3218 __ CmpLeD(FTMP, rhs, lhs);
3219 }
3220 __ Bc1nez(FTMP, label);
3221 break;
3222 default:
3223 LOG(FATAL) << "Unexpected non-floating-point condition";
3224 }
3225 } else {
3226 switch (cond) {
3227 case kCondEQ:
3228 __ CeqD(0, lhs, rhs);
3229 __ Bc1t(0, label);
3230 break;
3231 case kCondNE:
3232 __ CeqD(0, lhs, rhs);
3233 __ Bc1f(0, label);
3234 break;
3235 case kCondLT:
3236 if (gt_bias) {
3237 __ ColtD(0, lhs, rhs);
3238 } else {
3239 __ CultD(0, lhs, rhs);
3240 }
3241 __ Bc1t(0, label);
3242 break;
3243 case kCondLE:
3244 if (gt_bias) {
3245 __ ColeD(0, lhs, rhs);
3246 } else {
3247 __ CuleD(0, lhs, rhs);
3248 }
3249 __ Bc1t(0, label);
3250 break;
3251 case kCondGT:
3252 if (gt_bias) {
3253 __ CultD(0, rhs, lhs);
3254 } else {
3255 __ ColtD(0, rhs, lhs);
3256 }
3257 __ Bc1t(0, label);
3258 break;
3259 case kCondGE:
3260 if (gt_bias) {
3261 __ CuleD(0, rhs, lhs);
3262 } else {
3263 __ ColeD(0, rhs, lhs);
3264 }
3265 __ Bc1t(0, label);
3266 break;
3267 default:
3268 LOG(FATAL) << "Unexpected non-floating-point condition";
3269 }
3270 }
3271 }
3272}
3273
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003274void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003275 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003276 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003277 MipsLabel* false_target) {
3278 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003279
David Brazdil0debae72015-11-12 18:37:00 +00003280 if (true_target == nullptr && false_target == nullptr) {
3281 // Nothing to do. The code always falls through.
3282 return;
3283 } else if (cond->IsIntConstant()) {
3284 // Constant condition, statically compared against 1.
3285 if (cond->AsIntConstant()->IsOne()) {
3286 if (true_target != nullptr) {
3287 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003288 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003289 } else {
David Brazdil0debae72015-11-12 18:37:00 +00003290 DCHECK(cond->AsIntConstant()->IsZero());
3291 if (false_target != nullptr) {
3292 __ B(false_target);
3293 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003294 }
David Brazdil0debae72015-11-12 18:37:00 +00003295 return;
3296 }
3297
3298 // The following code generates these patterns:
3299 // (1) true_target == nullptr && false_target != nullptr
3300 // - opposite condition true => branch to false_target
3301 // (2) true_target != nullptr && false_target == nullptr
3302 // - condition true => branch to true_target
3303 // (3) true_target != nullptr && false_target != nullptr
3304 // - condition true => branch to true_target
3305 // - branch to false_target
3306 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003307 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003308 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003309 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003310 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003311 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3312 } else {
3313 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3314 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003315 } else {
3316 // The condition instruction has not been materialized, use its inputs as
3317 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003318 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003319 Primitive::Type type = condition->InputAt(0)->GetType();
3320 LocationSummary* locations = cond->GetLocations();
3321 IfCondition if_cond = condition->GetCondition();
3322 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003323
David Brazdil0debae72015-11-12 18:37:00 +00003324 if (true_target == nullptr) {
3325 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003326 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003327 }
3328
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003329 switch (type) {
3330 default:
3331 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3332 break;
3333 case Primitive::kPrimLong:
3334 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3335 break;
3336 case Primitive::kPrimFloat:
3337 case Primitive::kPrimDouble:
3338 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3339 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003340 }
3341 }
David Brazdil0debae72015-11-12 18:37:00 +00003342
3343 // If neither branch falls through (case 3), the conditional branch to `true_target`
3344 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3345 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003346 __ B(false_target);
3347 }
3348}
3349
3350void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003352 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003353 locations->SetInAt(0, Location::RequiresRegister());
3354 }
3355}
3356
3357void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003358 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3359 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3360 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3361 nullptr : codegen_->GetLabelOf(true_successor);
3362 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3363 nullptr : codegen_->GetLabelOf(false_successor);
3364 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003365}
3366
3367void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3368 LocationSummary* locations = new (GetGraph()->GetArena())
3369 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003370 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003371 locations->SetInAt(0, Location::RequiresRegister());
3372 }
3373}
3374
3375void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003376 SlowPathCodeMIPS* slow_path =
3377 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003378 GenerateTestAndBranch(deoptimize,
3379 /* condition_input_index */ 0,
3380 slow_path->GetEntryLabel(),
3381 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003382}
3383
David Brazdil74eb1b22015-12-14 11:44:01 +00003384void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3385 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3386 if (Primitive::IsFloatingPointType(select->GetType())) {
3387 locations->SetInAt(0, Location::RequiresFpuRegister());
3388 locations->SetInAt(1, Location::RequiresFpuRegister());
3389 } else {
3390 locations->SetInAt(0, Location::RequiresRegister());
3391 locations->SetInAt(1, Location::RequiresRegister());
3392 }
3393 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3394 locations->SetInAt(2, Location::RequiresRegister());
3395 }
3396 locations->SetOut(Location::SameAsFirstInput());
3397}
3398
3399void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3400 LocationSummary* locations = select->GetLocations();
3401 MipsLabel false_target;
3402 GenerateTestAndBranch(select,
3403 /* condition_input_index */ 2,
3404 /* true_target */ nullptr,
3405 &false_target);
3406 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3407 __ Bind(&false_target);
3408}
3409
David Srbecky0cf44932015-12-09 14:09:59 +00003410void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3411 new (GetGraph()->GetArena()) LocationSummary(info);
3412}
3413
3414void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00003415 if (codegen_->HasStackMapAtCurrentPc()) {
3416 // Ensure that we do not collide with the stack map of the previous instruction.
3417 __ Nop();
3418 }
David Srbecky0cf44932015-12-09 14:09:59 +00003419 codegen_->RecordPcInfo(info, info->GetDexPc());
3420}
3421
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003422void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3423 Primitive::Type field_type = field_info.GetFieldType();
3424 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3425 bool generate_volatile = field_info.IsVolatile() && is_wide;
3426 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3427 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3428
3429 locations->SetInAt(0, Location::RequiresRegister());
3430 if (generate_volatile) {
3431 InvokeRuntimeCallingConvention calling_convention;
3432 // need A0 to hold base + offset
3433 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3434 if (field_type == Primitive::kPrimLong) {
3435 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3436 } else {
3437 locations->SetOut(Location::RequiresFpuRegister());
3438 // Need some temp core regs since FP results are returned in core registers
3439 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3440 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3441 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3442 }
3443 } else {
3444 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3445 locations->SetOut(Location::RequiresFpuRegister());
3446 } else {
3447 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3448 }
3449 }
3450}
3451
3452void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3453 const FieldInfo& field_info,
3454 uint32_t dex_pc) {
3455 Primitive::Type type = field_info.GetFieldType();
3456 LocationSummary* locations = instruction->GetLocations();
3457 Register obj = locations->InAt(0).AsRegister<Register>();
3458 LoadOperandType load_type = kLoadUnsignedByte;
3459 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003460 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003461
3462 switch (type) {
3463 case Primitive::kPrimBoolean:
3464 load_type = kLoadUnsignedByte;
3465 break;
3466 case Primitive::kPrimByte:
3467 load_type = kLoadSignedByte;
3468 break;
3469 case Primitive::kPrimShort:
3470 load_type = kLoadSignedHalfword;
3471 break;
3472 case Primitive::kPrimChar:
3473 load_type = kLoadUnsignedHalfword;
3474 break;
3475 case Primitive::kPrimInt:
3476 case Primitive::kPrimFloat:
3477 case Primitive::kPrimNot:
3478 load_type = kLoadWord;
3479 break;
3480 case Primitive::kPrimLong:
3481 case Primitive::kPrimDouble:
3482 load_type = kLoadDoubleword;
3483 break;
3484 case Primitive::kPrimVoid:
3485 LOG(FATAL) << "Unreachable type " << type;
3486 UNREACHABLE();
3487 }
3488
3489 if (is_volatile && load_type == kLoadDoubleword) {
3490 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003491 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003492 // Do implicit Null check
3493 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3494 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3495 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3496 instruction,
3497 dex_pc,
3498 nullptr,
3499 IsDirectEntrypoint(kQuickA64Load));
3500 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3501 if (type == Primitive::kPrimDouble) {
3502 // Need to move to FP regs since FP results are returned in core registers.
3503 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
3504 locations->Out().AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003505 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3506 locations->Out().AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003507 }
3508 } else {
3509 if (!Primitive::IsFloatingPointType(type)) {
3510 Register dst;
3511 if (type == Primitive::kPrimLong) {
3512 DCHECK(locations->Out().IsRegisterPair());
3513 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003514 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3515 if (obj == dst) {
3516 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3517 codegen_->MaybeRecordImplicitNullCheck(instruction);
3518 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3519 } else {
3520 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3521 codegen_->MaybeRecordImplicitNullCheck(instruction);
3522 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3523 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003524 } else {
3525 DCHECK(locations->Out().IsRegister());
3526 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003527 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003528 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003529 } else {
3530 DCHECK(locations->Out().IsFpuRegister());
3531 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3532 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003533 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003534 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003535 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003536 }
3537 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003538 // Longs are handled earlier.
3539 if (type != Primitive::kPrimLong) {
3540 codegen_->MaybeRecordImplicitNullCheck(instruction);
3541 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003542 }
3543
3544 if (is_volatile) {
3545 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3546 }
3547}
3548
3549void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3550 Primitive::Type field_type = field_info.GetFieldType();
3551 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3552 bool generate_volatile = field_info.IsVolatile() && is_wide;
3553 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3554 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3555
3556 locations->SetInAt(0, Location::RequiresRegister());
3557 if (generate_volatile) {
3558 InvokeRuntimeCallingConvention calling_convention;
3559 // need A0 to hold base + offset
3560 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3561 if (field_type == Primitive::kPrimLong) {
3562 locations->SetInAt(1, Location::RegisterPairLocation(
3563 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3564 } else {
3565 locations->SetInAt(1, Location::RequiresFpuRegister());
3566 // Pass FP parameters in core registers.
3567 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3568 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3569 }
3570 } else {
3571 if (Primitive::IsFloatingPointType(field_type)) {
3572 locations->SetInAt(1, Location::RequiresFpuRegister());
3573 } else {
3574 locations->SetInAt(1, Location::RequiresRegister());
3575 }
3576 }
3577}
3578
3579void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3580 const FieldInfo& field_info,
3581 uint32_t dex_pc) {
3582 Primitive::Type type = field_info.GetFieldType();
3583 LocationSummary* locations = instruction->GetLocations();
3584 Register obj = locations->InAt(0).AsRegister<Register>();
3585 StoreOperandType store_type = kStoreByte;
3586 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003587 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003588
3589 switch (type) {
3590 case Primitive::kPrimBoolean:
3591 case Primitive::kPrimByte:
3592 store_type = kStoreByte;
3593 break;
3594 case Primitive::kPrimShort:
3595 case Primitive::kPrimChar:
3596 store_type = kStoreHalfword;
3597 break;
3598 case Primitive::kPrimInt:
3599 case Primitive::kPrimFloat:
3600 case Primitive::kPrimNot:
3601 store_type = kStoreWord;
3602 break;
3603 case Primitive::kPrimLong:
3604 case Primitive::kPrimDouble:
3605 store_type = kStoreDoubleword;
3606 break;
3607 case Primitive::kPrimVoid:
3608 LOG(FATAL) << "Unreachable type " << type;
3609 UNREACHABLE();
3610 }
3611
3612 if (is_volatile) {
3613 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3614 }
3615
3616 if (is_volatile && store_type == kStoreDoubleword) {
3617 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003618 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003619 // Do implicit Null check.
3620 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3621 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3622 if (type == Primitive::kPrimDouble) {
3623 // Pass FP parameters in core registers.
3624 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
3625 locations->InAt(1).AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003626 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3627 locations->InAt(1).AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003628 }
3629 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3630 instruction,
3631 dex_pc,
3632 nullptr,
3633 IsDirectEntrypoint(kQuickA64Store));
3634 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3635 } else {
3636 if (!Primitive::IsFloatingPointType(type)) {
3637 Register src;
3638 if (type == Primitive::kPrimLong) {
3639 DCHECK(locations->InAt(1).IsRegisterPair());
3640 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003641 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3642 __ StoreToOffset(kStoreWord, src, obj, offset);
3643 codegen_->MaybeRecordImplicitNullCheck(instruction);
3644 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003645 } else {
3646 DCHECK(locations->InAt(1).IsRegister());
3647 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003648 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003649 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003650 } else {
3651 DCHECK(locations->InAt(1).IsFpuRegister());
3652 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3653 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003654 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003655 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003656 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003657 }
3658 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003659 // Longs are handled earlier.
3660 if (type != Primitive::kPrimLong) {
3661 codegen_->MaybeRecordImplicitNullCheck(instruction);
3662 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003663 }
3664
3665 // TODO: memory barriers?
3666 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3667 DCHECK(locations->InAt(1).IsRegister());
3668 Register src = locations->InAt(1).AsRegister<Register>();
3669 codegen_->MarkGCCard(obj, src);
3670 }
3671
3672 if (is_volatile) {
3673 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3674 }
3675}
3676
3677void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3678 HandleFieldGet(instruction, instruction->GetFieldInfo());
3679}
3680
3681void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3682 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3683}
3684
3685void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3686 HandleFieldSet(instruction, instruction->GetFieldInfo());
3687}
3688
3689void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3690 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3691}
3692
3693void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3694 LocationSummary::CallKind call_kind =
3695 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3696 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3697 locations->SetInAt(0, Location::RequiresRegister());
3698 locations->SetInAt(1, Location::RequiresRegister());
3699 // The output does overlap inputs.
3700 // Note that TypeCheckSlowPathMIPS uses this register too.
3701 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3702}
3703
3704void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3705 LocationSummary* locations = instruction->GetLocations();
3706 Register obj = locations->InAt(0).AsRegister<Register>();
3707 Register cls = locations->InAt(1).AsRegister<Register>();
3708 Register out = locations->Out().AsRegister<Register>();
3709
3710 MipsLabel done;
3711
3712 // Return 0 if `obj` is null.
3713 // TODO: Avoid this check if we know `obj` is not null.
3714 __ Move(out, ZERO);
3715 __ Beqz(obj, &done);
3716
3717 // Compare the class of `obj` with `cls`.
3718 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3719 if (instruction->IsExactCheck()) {
3720 // Classes must be equal for the instanceof to succeed.
3721 __ Xor(out, out, cls);
3722 __ Sltiu(out, out, 1);
3723 } else {
3724 // If the classes are not equal, we go into a slow path.
3725 DCHECK(locations->OnlyCallsOnSlowPath());
3726 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3727 codegen_->AddSlowPath(slow_path);
3728 __ Bne(out, cls, slow_path->GetEntryLabel());
3729 __ LoadConst32(out, 1);
3730 __ Bind(slow_path->GetExitLabel());
3731 }
3732
3733 __ Bind(&done);
3734}
3735
3736void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3737 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3738 locations->SetOut(Location::ConstantLocation(constant));
3739}
3740
3741void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3742 // Will be generated at use site.
3743}
3744
3745void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3746 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3747 locations->SetOut(Location::ConstantLocation(constant));
3748}
3749
3750void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3751 // Will be generated at use site.
3752}
3753
3754void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3755 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3756 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3757}
3758
3759void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3760 HandleInvoke(invoke);
3761 // The register T0 is required to be used for the hidden argument in
3762 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3763 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3764}
3765
3766void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3767 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3768 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3769 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3770 invoke->GetImtIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
3771 Location receiver = invoke->GetLocations()->InAt(0);
3772 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3773 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3774
3775 // Set the hidden argument.
3776 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3777 invoke->GetDexMethodIndex());
3778
3779 // temp = object->GetClass();
3780 if (receiver.IsStackSlot()) {
3781 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3782 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3783 } else {
3784 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3785 }
3786 codegen_->MaybeRecordImplicitNullCheck(invoke);
3787 // temp = temp->GetImtEntryAt(method_offset);
3788 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3789 // T9 = temp->GetEntryPoint();
3790 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3791 // T9();
3792 __ Jalr(T9);
3793 __ Nop();
3794 DCHECK(!codegen_->IsLeafMethod());
3795 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3796}
3797
3798void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003799 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3800 if (intrinsic.TryDispatch(invoke)) {
3801 return;
3802 }
3803
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003804 HandleInvoke(invoke);
3805}
3806
3807void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003808 // Explicit clinit checks triggered by static invokes must have been pruned by
3809 // art::PrepareForRegisterAllocation.
3810 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003811
Chris Larsen701566a2015-10-27 15:29:13 -07003812 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3813 if (intrinsic.TryDispatch(invoke)) {
3814 return;
3815 }
3816
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003817 HandleInvoke(invoke);
3818}
3819
Chris Larsen701566a2015-10-27 15:29:13 -07003820static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003821 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003822 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3823 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003824 return true;
3825 }
3826 return false;
3827}
3828
Vladimir Markodc151b22015-10-15 18:02:30 +01003829HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3830 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3831 MethodReference target_method ATTRIBUTE_UNUSED) {
3832 switch (desired_dispatch_info.method_load_kind) {
3833 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3834 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3835 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3836 return HInvokeStaticOrDirect::DispatchInfo {
3837 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3838 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3839 0u,
3840 0u
3841 };
3842 default:
3843 break;
3844 }
3845 switch (desired_dispatch_info.code_ptr_location) {
3846 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3847 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3848 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3849 return HInvokeStaticOrDirect::DispatchInfo {
3850 desired_dispatch_info.method_load_kind,
3851 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3852 desired_dispatch_info.method_load_data,
3853 0u
3854 };
3855 default:
3856 return desired_dispatch_info;
3857 }
3858}
3859
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003860void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3861 // All registers are assumed to be correctly set up per the calling convention.
3862
3863 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3864 switch (invoke->GetMethodLoadKind()) {
3865 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3866 // temp = thread->string_init_entrypoint
3867 __ LoadFromOffset(kLoadWord,
3868 temp.AsRegister<Register>(),
3869 TR,
3870 invoke->GetStringInitOffset());
3871 break;
3872 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003873 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003874 break;
3875 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3876 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3877 break;
3878 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003879 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003880 // TODO: Implement these types.
3881 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3882 LOG(FATAL) << "Unsupported";
3883 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003884 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003885 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003886 Register reg = temp.AsRegister<Register>();
3887 Register method_reg;
3888 if (current_method.IsRegister()) {
3889 method_reg = current_method.AsRegister<Register>();
3890 } else {
3891 // TODO: use the appropriate DCHECK() here if possible.
3892 // DCHECK(invoke->GetLocations()->Intrinsified());
3893 DCHECK(!current_method.IsValid());
3894 method_reg = reg;
3895 __ Lw(reg, SP, kCurrentMethodStackOffset);
3896 }
3897
3898 // temp = temp->dex_cache_resolved_methods_;
3899 __ LoadFromOffset(kLoadWord,
3900 reg,
3901 method_reg,
3902 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
3903 // temp = temp[index_in_cache]
3904 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3905 __ LoadFromOffset(kLoadWord,
3906 reg,
3907 reg,
3908 CodeGenerator::GetCachePointerOffset(index_in_cache));
3909 break;
3910 }
3911 }
3912
3913 switch (invoke->GetCodePtrLocation()) {
3914 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3915 __ Jalr(&frame_entry_label_, T9);
3916 break;
3917 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3918 // LR = invoke->GetDirectCodePtr();
3919 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3920 // LR()
3921 __ Jalr(T9);
3922 __ Nop();
3923 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003924 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003925 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3926 // TODO: Implement these types.
3927 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3928 LOG(FATAL) << "Unsupported";
3929 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003930 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3931 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01003932 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003933 T9,
3934 callee_method.AsRegister<Register>(),
3935 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3936 kMipsWordSize).Int32Value());
3937 // T9()
3938 __ Jalr(T9);
3939 __ Nop();
3940 break;
3941 }
3942 DCHECK(!IsLeafMethod());
3943}
3944
3945void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003946 // Explicit clinit checks triggered by static invokes must have been pruned by
3947 // art::PrepareForRegisterAllocation.
3948 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003949
3950 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3951 return;
3952 }
3953
3954 LocationSummary* locations = invoke->GetLocations();
3955 codegen_->GenerateStaticOrDirectCall(invoke,
3956 locations->HasTemps()
3957 ? locations->GetTemp(0)
3958 : Location::NoLocation());
3959 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3960}
3961
3962void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003963 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3964 return;
3965 }
3966
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003967 LocationSummary* locations = invoke->GetLocations();
3968 Location receiver = locations->InAt(0);
3969 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3970 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3971 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
3972 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3973 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3974
3975 // temp = object->GetClass();
3976 if (receiver.IsStackSlot()) {
3977 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3978 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3979 } else {
3980 DCHECK(receiver.IsRegister());
3981 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3982 }
3983 codegen_->MaybeRecordImplicitNullCheck(invoke);
3984 // temp = temp->GetMethodAt(method_offset);
3985 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3986 // T9 = temp->GetEntryPoint();
3987 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3988 // T9();
3989 __ Jalr(T9);
3990 __ Nop();
3991 DCHECK(!codegen_->IsLeafMethod());
3992 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3993}
3994
3995void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01003996 InvokeRuntimeCallingConvention calling_convention;
3997 CodeGenerator::CreateLoadClassLocationSummary(
3998 cls,
3999 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4000 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004001}
4002
4003void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4004 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004005 if (cls->NeedsAccessCheck()) {
4006 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4007 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4008 cls,
4009 cls->GetDexPc(),
4010 nullptr,
4011 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00004012 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004013 return;
4014 }
4015
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004016 Register out = locations->Out().AsRegister<Register>();
4017 Register current_method = locations->InAt(0).AsRegister<Register>();
4018 if (cls->IsReferrersClass()) {
4019 DCHECK(!cls->CanCallRuntime());
4020 DCHECK(!cls->MustGenerateClinitCheck());
4021 __ LoadFromOffset(kLoadWord, out, current_method,
4022 ArtMethod::DeclaringClassOffset().Int32Value());
4023 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004024 __ LoadFromOffset(kLoadWord, out, current_method,
4025 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
4026 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004027
4028 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
4029 DCHECK(cls->CanCallRuntime());
4030 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4031 cls,
4032 cls,
4033 cls->GetDexPc(),
4034 cls->MustGenerateClinitCheck());
4035 codegen_->AddSlowPath(slow_path);
4036 if (!cls->IsInDexCache()) {
4037 __ Beqz(out, slow_path->GetEntryLabel());
4038 }
4039 if (cls->MustGenerateClinitCheck()) {
4040 GenerateClassInitializationCheck(slow_path, out);
4041 } else {
4042 __ Bind(slow_path->GetExitLabel());
4043 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004044 }
4045 }
4046}
4047
4048static int32_t GetExceptionTlsOffset() {
4049 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
4050}
4051
4052void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4053 LocationSummary* locations =
4054 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4055 locations->SetOut(Location::RequiresRegister());
4056}
4057
4058void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4059 Register out = load->GetLocations()->Out().AsRegister<Register>();
4060 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4061}
4062
4063void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4064 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4065}
4066
4067void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4068 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4069}
4070
4071void LocationsBuilderMIPS::VisitLoadLocal(HLoadLocal* load) {
4072 load->SetLocations(nullptr);
4073}
4074
4075void InstructionCodeGeneratorMIPS::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
4076 // Nothing to do, this is driven by the code generator.
4077}
4078
4079void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00004080 LocationSummary::CallKind call_kind = load->IsInDexCache()
4081 ? LocationSummary::kNoCall
4082 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004083 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004084 locations->SetInAt(0, Location::RequiresRegister());
4085 locations->SetOut(Location::RequiresRegister());
4086}
4087
4088void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004089 LocationSummary* locations = load->GetLocations();
4090 Register out = locations->Out().AsRegister<Register>();
4091 Register current_method = locations->InAt(0).AsRegister<Register>();
4092 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4093 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4094 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004095
4096 if (!load->IsInDexCache()) {
4097 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4098 codegen_->AddSlowPath(slow_path);
4099 __ Beqz(out, slow_path->GetEntryLabel());
4100 __ Bind(slow_path->GetExitLabel());
4101 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004102}
4103
4104void LocationsBuilderMIPS::VisitLocal(HLocal* local) {
4105 local->SetLocations(nullptr);
4106}
4107
4108void InstructionCodeGeneratorMIPS::VisitLocal(HLocal* local) {
4109 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
4110}
4111
4112void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4114 locations->SetOut(Location::ConstantLocation(constant));
4115}
4116
4117void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4118 // Will be generated at use site.
4119}
4120
4121void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4122 LocationSummary* locations =
4123 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4124 InvokeRuntimeCallingConvention calling_convention;
4125 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4126}
4127
4128void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4129 if (instruction->IsEnter()) {
4130 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4131 instruction,
4132 instruction->GetDexPc(),
4133 nullptr,
4134 IsDirectEntrypoint(kQuickLockObject));
4135 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4136 } else {
4137 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4138 instruction,
4139 instruction->GetDexPc(),
4140 nullptr,
4141 IsDirectEntrypoint(kQuickUnlockObject));
4142 }
4143 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4144}
4145
4146void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4147 LocationSummary* locations =
4148 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4149 switch (mul->GetResultType()) {
4150 case Primitive::kPrimInt:
4151 case Primitive::kPrimLong:
4152 locations->SetInAt(0, Location::RequiresRegister());
4153 locations->SetInAt(1, Location::RequiresRegister());
4154 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4155 break;
4156
4157 case Primitive::kPrimFloat:
4158 case Primitive::kPrimDouble:
4159 locations->SetInAt(0, Location::RequiresFpuRegister());
4160 locations->SetInAt(1, Location::RequiresFpuRegister());
4161 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4162 break;
4163
4164 default:
4165 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4166 }
4167}
4168
4169void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4170 Primitive::Type type = instruction->GetType();
4171 LocationSummary* locations = instruction->GetLocations();
4172 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4173
4174 switch (type) {
4175 case Primitive::kPrimInt: {
4176 Register dst = locations->Out().AsRegister<Register>();
4177 Register lhs = locations->InAt(0).AsRegister<Register>();
4178 Register rhs = locations->InAt(1).AsRegister<Register>();
4179
4180 if (isR6) {
4181 __ MulR6(dst, lhs, rhs);
4182 } else {
4183 __ MulR2(dst, lhs, rhs);
4184 }
4185 break;
4186 }
4187 case Primitive::kPrimLong: {
4188 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4189 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4190 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4191 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4192 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4193 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4194
4195 // Extra checks to protect caused by the existance of A1_A2.
4196 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4197 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4198 DCHECK_NE(dst_high, lhs_low);
4199 DCHECK_NE(dst_high, rhs_low);
4200
4201 // A_B * C_D
4202 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4203 // dst_lo: [ low(B*D) ]
4204 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4205
4206 if (isR6) {
4207 __ MulR6(TMP, lhs_high, rhs_low);
4208 __ MulR6(dst_high, lhs_low, rhs_high);
4209 __ Addu(dst_high, dst_high, TMP);
4210 __ MuhuR6(TMP, lhs_low, rhs_low);
4211 __ Addu(dst_high, dst_high, TMP);
4212 __ MulR6(dst_low, lhs_low, rhs_low);
4213 } else {
4214 __ MulR2(TMP, lhs_high, rhs_low);
4215 __ MulR2(dst_high, lhs_low, rhs_high);
4216 __ Addu(dst_high, dst_high, TMP);
4217 __ MultuR2(lhs_low, rhs_low);
4218 __ Mfhi(TMP);
4219 __ Addu(dst_high, dst_high, TMP);
4220 __ Mflo(dst_low);
4221 }
4222 break;
4223 }
4224 case Primitive::kPrimFloat:
4225 case Primitive::kPrimDouble: {
4226 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4227 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4228 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4229 if (type == Primitive::kPrimFloat) {
4230 __ MulS(dst, lhs, rhs);
4231 } else {
4232 __ MulD(dst, lhs, rhs);
4233 }
4234 break;
4235 }
4236 default:
4237 LOG(FATAL) << "Unexpected mul type " << type;
4238 }
4239}
4240
4241void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4242 LocationSummary* locations =
4243 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4244 switch (neg->GetResultType()) {
4245 case Primitive::kPrimInt:
4246 case Primitive::kPrimLong:
4247 locations->SetInAt(0, Location::RequiresRegister());
4248 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4249 break;
4250
4251 case Primitive::kPrimFloat:
4252 case Primitive::kPrimDouble:
4253 locations->SetInAt(0, Location::RequiresFpuRegister());
4254 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4255 break;
4256
4257 default:
4258 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4259 }
4260}
4261
4262void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4263 Primitive::Type type = instruction->GetType();
4264 LocationSummary* locations = instruction->GetLocations();
4265
4266 switch (type) {
4267 case Primitive::kPrimInt: {
4268 Register dst = locations->Out().AsRegister<Register>();
4269 Register src = locations->InAt(0).AsRegister<Register>();
4270 __ Subu(dst, ZERO, src);
4271 break;
4272 }
4273 case Primitive::kPrimLong: {
4274 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4275 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4276 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4277 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4278 __ Subu(dst_low, ZERO, src_low);
4279 __ Sltu(TMP, ZERO, dst_low);
4280 __ Subu(dst_high, ZERO, src_high);
4281 __ Subu(dst_high, dst_high, TMP);
4282 break;
4283 }
4284 case Primitive::kPrimFloat:
4285 case Primitive::kPrimDouble: {
4286 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4287 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4288 if (type == Primitive::kPrimFloat) {
4289 __ NegS(dst, src);
4290 } else {
4291 __ NegD(dst, src);
4292 }
4293 break;
4294 }
4295 default:
4296 LOG(FATAL) << "Unexpected neg type " << type;
4297 }
4298}
4299
4300void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4301 LocationSummary* locations =
4302 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4303 InvokeRuntimeCallingConvention calling_convention;
4304 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4305 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4306 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4307 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4308}
4309
4310void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4311 InvokeRuntimeCallingConvention calling_convention;
4312 Register current_method_register = calling_convention.GetRegisterAt(2);
4313 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4314 // Move an uint16_t value to a register.
4315 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4316 codegen_->InvokeRuntime(
4317 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4318 instruction,
4319 instruction->GetDexPc(),
4320 nullptr,
4321 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4322 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4323 void*, uint32_t, int32_t, ArtMethod*>();
4324}
4325
4326void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4327 LocationSummary* locations =
4328 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4329 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004330 if (instruction->IsStringAlloc()) {
4331 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4332 } else {
4333 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4334 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4335 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004336 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4337}
4338
4339void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004340 if (instruction->IsStringAlloc()) {
4341 // String is allocated through StringFactory. Call NewEmptyString entry point.
4342 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4343 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4344 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4345 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4346 __ Jalr(T9);
4347 __ Nop();
4348 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4349 } else {
4350 codegen_->InvokeRuntime(
4351 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4352 instruction,
4353 instruction->GetDexPc(),
4354 nullptr,
4355 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4356 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4357 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004358}
4359
4360void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4361 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4362 locations->SetInAt(0, Location::RequiresRegister());
4363 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4364}
4365
4366void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4367 Primitive::Type type = instruction->GetType();
4368 LocationSummary* locations = instruction->GetLocations();
4369
4370 switch (type) {
4371 case Primitive::kPrimInt: {
4372 Register dst = locations->Out().AsRegister<Register>();
4373 Register src = locations->InAt(0).AsRegister<Register>();
4374 __ Nor(dst, src, ZERO);
4375 break;
4376 }
4377
4378 case Primitive::kPrimLong: {
4379 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4380 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4381 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4382 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4383 __ Nor(dst_high, src_high, ZERO);
4384 __ Nor(dst_low, src_low, ZERO);
4385 break;
4386 }
4387
4388 default:
4389 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4390 }
4391}
4392
4393void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4394 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4395 locations->SetInAt(0, Location::RequiresRegister());
4396 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4397}
4398
4399void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4400 LocationSummary* locations = instruction->GetLocations();
4401 __ Xori(locations->Out().AsRegister<Register>(),
4402 locations->InAt(0).AsRegister<Register>(),
4403 1);
4404}
4405
4406void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4407 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4408 ? LocationSummary::kCallOnSlowPath
4409 : LocationSummary::kNoCall;
4410 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4411 locations->SetInAt(0, Location::RequiresRegister());
4412 if (instruction->HasUses()) {
4413 locations->SetOut(Location::SameAsFirstInput());
4414 }
4415}
4416
4417void InstructionCodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4418 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4419 return;
4420 }
4421 Location obj = instruction->GetLocations()->InAt(0);
4422
4423 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
4424 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4425}
4426
4427void InstructionCodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
4428 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
4429 codegen_->AddSlowPath(slow_path);
4430
4431 Location obj = instruction->GetLocations()->InAt(0);
4432
4433 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4434}
4435
4436void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
4437 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
4438 GenerateImplicitNullCheck(instruction);
4439 } else {
4440 GenerateExplicitNullCheck(instruction);
4441 }
4442}
4443
4444void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4445 HandleBinaryOp(instruction);
4446}
4447
4448void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4449 HandleBinaryOp(instruction);
4450}
4451
4452void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4453 LOG(FATAL) << "Unreachable";
4454}
4455
4456void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4457 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4458}
4459
4460void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4461 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4462 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4463 if (location.IsStackSlot()) {
4464 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4465 } else if (location.IsDoubleStackSlot()) {
4466 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4467 }
4468 locations->SetOut(location);
4469}
4470
4471void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4472 ATTRIBUTE_UNUSED) {
4473 // Nothing to do, the parameter is already at its location.
4474}
4475
4476void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4477 LocationSummary* locations =
4478 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4479 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4480}
4481
4482void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4483 ATTRIBUTE_UNUSED) {
4484 // Nothing to do, the method is already at its location.
4485}
4486
4487void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4488 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4489 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4490 locations->SetInAt(i, Location::Any());
4491 }
4492 locations->SetOut(Location::Any());
4493}
4494
4495void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4496 LOG(FATAL) << "Unreachable";
4497}
4498
4499void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4500 Primitive::Type type = rem->GetResultType();
4501 LocationSummary::CallKind call_kind =
4502 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCall;
4503 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4504
4505 switch (type) {
4506 case Primitive::kPrimInt:
4507 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004508 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004509 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4510 break;
4511
4512 case Primitive::kPrimLong: {
4513 InvokeRuntimeCallingConvention calling_convention;
4514 locations->SetInAt(0, Location::RegisterPairLocation(
4515 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4516 locations->SetInAt(1, Location::RegisterPairLocation(
4517 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4518 locations->SetOut(calling_convention.GetReturnLocation(type));
4519 break;
4520 }
4521
4522 case Primitive::kPrimFloat:
4523 case Primitive::kPrimDouble: {
4524 InvokeRuntimeCallingConvention calling_convention;
4525 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4526 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4527 locations->SetOut(calling_convention.GetReturnLocation(type));
4528 break;
4529 }
4530
4531 default:
4532 LOG(FATAL) << "Unexpected rem type " << type;
4533 }
4534}
4535
4536void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4537 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004538
4539 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004540 case Primitive::kPrimInt:
4541 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004542 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004543 case Primitive::kPrimLong: {
4544 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
4545 instruction,
4546 instruction->GetDexPc(),
4547 nullptr,
4548 IsDirectEntrypoint(kQuickLmod));
4549 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4550 break;
4551 }
4552 case Primitive::kPrimFloat: {
4553 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
4554 instruction, instruction->GetDexPc(),
4555 nullptr,
4556 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00004557 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004558 break;
4559 }
4560 case Primitive::kPrimDouble: {
4561 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
4562 instruction, instruction->GetDexPc(),
4563 nullptr,
4564 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00004565 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004566 break;
4567 }
4568 default:
4569 LOG(FATAL) << "Unexpected rem type " << type;
4570 }
4571}
4572
4573void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4574 memory_barrier->SetLocations(nullptr);
4575}
4576
4577void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4578 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4579}
4580
4581void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
4582 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4583 Primitive::Type return_type = ret->InputAt(0)->GetType();
4584 locations->SetInAt(0, MipsReturnLocation(return_type));
4585}
4586
4587void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4588 codegen_->GenerateFrameExit();
4589}
4590
4591void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
4592 ret->SetLocations(nullptr);
4593}
4594
4595void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4596 codegen_->GenerateFrameExit();
4597}
4598
Alexey Frunze92d90602015-12-18 18:16:36 -08004599void LocationsBuilderMIPS::VisitRor(HRor* ror) {
4600 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004601}
4602
Alexey Frunze92d90602015-12-18 18:16:36 -08004603void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
4604 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004605}
4606
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004607void LocationsBuilderMIPS::VisitShl(HShl* shl) {
4608 HandleShift(shl);
4609}
4610
4611void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
4612 HandleShift(shl);
4613}
4614
4615void LocationsBuilderMIPS::VisitShr(HShr* shr) {
4616 HandleShift(shr);
4617}
4618
4619void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
4620 HandleShift(shr);
4621}
4622
4623void LocationsBuilderMIPS::VisitStoreLocal(HStoreLocal* store) {
4624 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4625 Primitive::Type field_type = store->InputAt(1)->GetType();
4626 switch (field_type) {
4627 case Primitive::kPrimNot:
4628 case Primitive::kPrimBoolean:
4629 case Primitive::kPrimByte:
4630 case Primitive::kPrimChar:
4631 case Primitive::kPrimShort:
4632 case Primitive::kPrimInt:
4633 case Primitive::kPrimFloat:
4634 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4635 break;
4636
4637 case Primitive::kPrimLong:
4638 case Primitive::kPrimDouble:
4639 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4640 break;
4641
4642 default:
4643 LOG(FATAL) << "Unimplemented local type " << field_type;
4644 }
4645}
4646
4647void InstructionCodeGeneratorMIPS::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
4648}
4649
4650void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
4651 HandleBinaryOp(instruction);
4652}
4653
4654void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
4655 HandleBinaryOp(instruction);
4656}
4657
4658void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4659 HandleFieldGet(instruction, instruction->GetFieldInfo());
4660}
4661
4662void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4663 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4664}
4665
4666void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4667 HandleFieldSet(instruction, instruction->GetFieldInfo());
4668}
4669
4670void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4671 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4672}
4673
4674void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
4675 HUnresolvedInstanceFieldGet* instruction) {
4676 FieldAccessCallingConventionMIPS calling_convention;
4677 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4678 instruction->GetFieldType(),
4679 calling_convention);
4680}
4681
4682void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
4683 HUnresolvedInstanceFieldGet* instruction) {
4684 FieldAccessCallingConventionMIPS calling_convention;
4685 codegen_->GenerateUnresolvedFieldAccess(instruction,
4686 instruction->GetFieldType(),
4687 instruction->GetFieldIndex(),
4688 instruction->GetDexPc(),
4689 calling_convention);
4690}
4691
4692void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4693 HUnresolvedInstanceFieldSet* instruction) {
4694 FieldAccessCallingConventionMIPS calling_convention;
4695 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4696 instruction->GetFieldType(),
4697 calling_convention);
4698}
4699
4700void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4701 HUnresolvedInstanceFieldSet* instruction) {
4702 FieldAccessCallingConventionMIPS calling_convention;
4703 codegen_->GenerateUnresolvedFieldAccess(instruction,
4704 instruction->GetFieldType(),
4705 instruction->GetFieldIndex(),
4706 instruction->GetDexPc(),
4707 calling_convention);
4708}
4709
4710void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4711 HUnresolvedStaticFieldGet* instruction) {
4712 FieldAccessCallingConventionMIPS calling_convention;
4713 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4714 instruction->GetFieldType(),
4715 calling_convention);
4716}
4717
4718void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4719 HUnresolvedStaticFieldGet* instruction) {
4720 FieldAccessCallingConventionMIPS calling_convention;
4721 codegen_->GenerateUnresolvedFieldAccess(instruction,
4722 instruction->GetFieldType(),
4723 instruction->GetFieldIndex(),
4724 instruction->GetDexPc(),
4725 calling_convention);
4726}
4727
4728void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4729 HUnresolvedStaticFieldSet* instruction) {
4730 FieldAccessCallingConventionMIPS calling_convention;
4731 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4732 instruction->GetFieldType(),
4733 calling_convention);
4734}
4735
4736void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4737 HUnresolvedStaticFieldSet* instruction) {
4738 FieldAccessCallingConventionMIPS calling_convention;
4739 codegen_->GenerateUnresolvedFieldAccess(instruction,
4740 instruction->GetFieldType(),
4741 instruction->GetFieldIndex(),
4742 instruction->GetDexPc(),
4743 calling_convention);
4744}
4745
4746void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4747 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4748}
4749
4750void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4751 HBasicBlock* block = instruction->GetBlock();
4752 if (block->GetLoopInformation() != nullptr) {
4753 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4754 // The back edge will generate the suspend check.
4755 return;
4756 }
4757 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4758 // The goto will generate the suspend check.
4759 return;
4760 }
4761 GenerateSuspendCheck(instruction, nullptr);
4762}
4763
4764void LocationsBuilderMIPS::VisitTemporary(HTemporary* temp) {
4765 temp->SetLocations(nullptr);
4766}
4767
4768void InstructionCodeGeneratorMIPS::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
4769 // Nothing to do, this is driven by the code generator.
4770}
4771
4772void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4773 LocationSummary* locations =
4774 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4775 InvokeRuntimeCallingConvention calling_convention;
4776 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4777}
4778
4779void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4780 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4781 instruction,
4782 instruction->GetDexPc(),
4783 nullptr,
4784 IsDirectEntrypoint(kQuickDeliverException));
4785 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4786}
4787
4788void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4789 Primitive::Type input_type = conversion->GetInputType();
4790 Primitive::Type result_type = conversion->GetResultType();
4791 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004792 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004793
4794 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4795 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4796 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4797 }
4798
4799 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004800 if (!isR6 &&
4801 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4802 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004803 call_kind = LocationSummary::kCall;
4804 }
4805
4806 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4807
4808 if (call_kind == LocationSummary::kNoCall) {
4809 if (Primitive::IsFloatingPointType(input_type)) {
4810 locations->SetInAt(0, Location::RequiresFpuRegister());
4811 } else {
4812 locations->SetInAt(0, Location::RequiresRegister());
4813 }
4814
4815 if (Primitive::IsFloatingPointType(result_type)) {
4816 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4817 } else {
4818 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4819 }
4820 } else {
4821 InvokeRuntimeCallingConvention calling_convention;
4822
4823 if (Primitive::IsFloatingPointType(input_type)) {
4824 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4825 } else {
4826 DCHECK_EQ(input_type, Primitive::kPrimLong);
4827 locations->SetInAt(0, Location::RegisterPairLocation(
4828 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4829 }
4830
4831 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4832 }
4833}
4834
4835void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4836 LocationSummary* locations = conversion->GetLocations();
4837 Primitive::Type result_type = conversion->GetResultType();
4838 Primitive::Type input_type = conversion->GetInputType();
4839 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004840 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4841 bool fpu_32bit = codegen_->GetInstructionSetFeatures().Is32BitFloatingPoint();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004842
4843 DCHECK_NE(input_type, result_type);
4844
4845 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4846 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4847 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4848 Register src = locations->InAt(0).AsRegister<Register>();
4849
4850 __ Move(dst_low, src);
4851 __ Sra(dst_high, src, 31);
4852 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4853 Register dst = locations->Out().AsRegister<Register>();
4854 Register src = (input_type == Primitive::kPrimLong)
4855 ? locations->InAt(0).AsRegisterPairLow<Register>()
4856 : locations->InAt(0).AsRegister<Register>();
4857
4858 switch (result_type) {
4859 case Primitive::kPrimChar:
4860 __ Andi(dst, src, 0xFFFF);
4861 break;
4862 case Primitive::kPrimByte:
4863 if (has_sign_extension) {
4864 __ Seb(dst, src);
4865 } else {
4866 __ Sll(dst, src, 24);
4867 __ Sra(dst, dst, 24);
4868 }
4869 break;
4870 case Primitive::kPrimShort:
4871 if (has_sign_extension) {
4872 __ Seh(dst, src);
4873 } else {
4874 __ Sll(dst, src, 16);
4875 __ Sra(dst, dst, 16);
4876 }
4877 break;
4878 case Primitive::kPrimInt:
4879 __ Move(dst, src);
4880 break;
4881
4882 default:
4883 LOG(FATAL) << "Unexpected type conversion from " << input_type
4884 << " to " << result_type;
4885 }
4886 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004887 if (input_type == Primitive::kPrimLong) {
4888 if (isR6) {
4889 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4890 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4891 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4892 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4893 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4894 __ Mtc1(src_low, FTMP);
4895 __ Mthc1(src_high, FTMP);
4896 if (result_type == Primitive::kPrimFloat) {
4897 __ Cvtsl(dst, FTMP);
4898 } else {
4899 __ Cvtdl(dst, FTMP);
4900 }
4901 } else {
4902 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4903 : QUICK_ENTRY_POINT(pL2d);
4904 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4905 : IsDirectEntrypoint(kQuickL2d);
4906 codegen_->InvokeRuntime(entry_offset,
4907 conversion,
4908 conversion->GetDexPc(),
4909 nullptr,
4910 direct);
4911 if (result_type == Primitive::kPrimFloat) {
4912 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4913 } else {
4914 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4915 }
4916 }
4917 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004918 Register src = locations->InAt(0).AsRegister<Register>();
4919 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4920 __ Mtc1(src, FTMP);
4921 if (result_type == Primitive::kPrimFloat) {
4922 __ Cvtsw(dst, FTMP);
4923 } else {
4924 __ Cvtdw(dst, FTMP);
4925 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004926 }
4927 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4928 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004929 if (result_type == Primitive::kPrimLong) {
4930 if (isR6) {
4931 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4932 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4933 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4934 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4935 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4936 MipsLabel truncate;
4937 MipsLabel done;
4938
4939 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4940 // value when the input is either a NaN or is outside of the range of the output type
4941 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4942 // the same result.
4943 //
4944 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4945 // value of the output type if the input is outside of the range after the truncation or
4946 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4947 // results. This matches the desired float/double-to-int/long conversion exactly.
4948 //
4949 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4950 //
4951 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4952 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4953 // even though it must be NAN2008=1 on R6.
4954 //
4955 // The code takes care of the different behaviors by first comparing the input to the
4956 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4957 // If the input is greater than or equal to the minimum, it procedes to the truncate
4958 // instruction, which will handle such an input the same way irrespective of NAN2008.
4959 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4960 // in order to return either zero or the minimum value.
4961 //
4962 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4963 // truncate instruction for MIPS64R6.
4964 if (input_type == Primitive::kPrimFloat) {
4965 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
4966 __ LoadConst32(TMP, min_val);
4967 __ Mtc1(TMP, FTMP);
4968 __ CmpLeS(FTMP, FTMP, src);
4969 } else {
4970 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
4971 __ LoadConst32(TMP, High32Bits(min_val));
4972 __ Mtc1(ZERO, FTMP);
4973 __ Mthc1(TMP, FTMP);
4974 __ CmpLeD(FTMP, FTMP, src);
4975 }
4976
4977 __ Bc1nez(FTMP, &truncate);
4978
4979 if (input_type == Primitive::kPrimFloat) {
4980 __ CmpEqS(FTMP, src, src);
4981 } else {
4982 __ CmpEqD(FTMP, src, src);
4983 }
4984 __ Move(dst_low, ZERO);
4985 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
4986 __ Mfc1(TMP, FTMP);
4987 __ And(dst_high, dst_high, TMP);
4988
4989 __ B(&done);
4990
4991 __ Bind(&truncate);
4992
4993 if (input_type == Primitive::kPrimFloat) {
4994 __ TruncLS(FTMP, src);
4995 } else {
4996 __ TruncLD(FTMP, src);
4997 }
4998 __ Mfc1(dst_low, FTMP);
4999 __ Mfhc1(dst_high, FTMP);
5000
5001 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005002 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005003 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
5004 : QUICK_ENTRY_POINT(pD2l);
5005 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
5006 : IsDirectEntrypoint(kQuickD2l);
5007 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
5008 if (input_type == Primitive::kPrimFloat) {
5009 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5010 } else {
5011 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5012 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005013 }
5014 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005015 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5016 Register dst = locations->Out().AsRegister<Register>();
5017 MipsLabel truncate;
5018 MipsLabel done;
5019
5020 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5021 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5022 // even though it must be NAN2008=1 on R6.
5023 //
5024 // For details see the large comment above for the truncation of float/double to long on R6.
5025 //
5026 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5027 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005028 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005029 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5030 __ LoadConst32(TMP, min_val);
5031 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005032 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005033 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5034 __ LoadConst32(TMP, High32Bits(min_val));
5035 __ Mtc1(ZERO, FTMP);
5036 if (fpu_32bit) {
5037 __ Mtc1(TMP, static_cast<FRegister>(FTMP + 1));
5038 } else {
5039 __ Mthc1(TMP, FTMP);
5040 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005041 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005042
5043 if (isR6) {
5044 if (input_type == Primitive::kPrimFloat) {
5045 __ CmpLeS(FTMP, FTMP, src);
5046 } else {
5047 __ CmpLeD(FTMP, FTMP, src);
5048 }
5049 __ Bc1nez(FTMP, &truncate);
5050
5051 if (input_type == Primitive::kPrimFloat) {
5052 __ CmpEqS(FTMP, src, src);
5053 } else {
5054 __ CmpEqD(FTMP, src, src);
5055 }
5056 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5057 __ Mfc1(TMP, FTMP);
5058 __ And(dst, dst, TMP);
5059 } else {
5060 if (input_type == Primitive::kPrimFloat) {
5061 __ ColeS(0, FTMP, src);
5062 } else {
5063 __ ColeD(0, FTMP, src);
5064 }
5065 __ Bc1t(0, &truncate);
5066
5067 if (input_type == Primitive::kPrimFloat) {
5068 __ CeqS(0, src, src);
5069 } else {
5070 __ CeqD(0, src, src);
5071 }
5072 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5073 __ Movf(dst, ZERO, 0);
5074 }
5075
5076 __ B(&done);
5077
5078 __ Bind(&truncate);
5079
5080 if (input_type == Primitive::kPrimFloat) {
5081 __ TruncWS(FTMP, src);
5082 } else {
5083 __ TruncWD(FTMP, src);
5084 }
5085 __ Mfc1(dst, FTMP);
5086
5087 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005088 }
5089 } else if (Primitive::IsFloatingPointType(result_type) &&
5090 Primitive::IsFloatingPointType(input_type)) {
5091 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5092 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5093 if (result_type == Primitive::kPrimFloat) {
5094 __ Cvtsd(dst, src);
5095 } else {
5096 __ Cvtds(dst, src);
5097 }
5098 } else {
5099 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5100 << " to " << result_type;
5101 }
5102}
5103
5104void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5105 HandleShift(ushr);
5106}
5107
5108void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5109 HandleShift(ushr);
5110}
5111
5112void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5113 HandleBinaryOp(instruction);
5114}
5115
5116void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5117 HandleBinaryOp(instruction);
5118}
5119
5120void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5121 // Nothing to do, this should be removed during prepare for register allocator.
5122 LOG(FATAL) << "Unreachable";
5123}
5124
5125void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5126 // Nothing to do, this should be removed during prepare for register allocator.
5127 LOG(FATAL) << "Unreachable";
5128}
5129
5130void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005131 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005132}
5133
5134void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005135 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005136}
5137
5138void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005139 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005140}
5141
5142void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005143 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005144}
5145
5146void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005147 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005148}
5149
5150void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005151 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005152}
5153
5154void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005155 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005156}
5157
5158void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005159 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005160}
5161
5162void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005163 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005164}
5165
5166void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005167 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005168}
5169
5170void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005171 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005172}
5173
5174void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005175 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005176}
5177
5178void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005179 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005180}
5181
5182void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005183 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005184}
5185
5186void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005187 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005188}
5189
5190void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005191 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005192}
5193
5194void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005195 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005196}
5197
5198void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005199 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005200}
5201
5202void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005203 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005204}
5205
5206void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005207 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005208}
5209
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005210void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5211 LocationSummary* locations =
5212 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5213 locations->SetInAt(0, Location::RequiresRegister());
5214}
5215
5216void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5217 int32_t lower_bound = switch_instr->GetStartValue();
5218 int32_t num_entries = switch_instr->GetNumEntries();
5219 LocationSummary* locations = switch_instr->GetLocations();
5220 Register value_reg = locations->InAt(0).AsRegister<Register>();
5221 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5222
5223 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005224 Register temp_reg = TMP;
5225 __ Addiu32(temp_reg, value_reg, -lower_bound);
5226 // Jump to default if index is negative
5227 // Note: We don't check the case that index is positive while value < lower_bound, because in
5228 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5229 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5230
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005231 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005232 // Jump to successors[0] if value == lower_bound.
5233 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5234 int32_t last_index = 0;
5235 for (; num_entries - last_index > 2; last_index += 2) {
5236 __ Addiu(temp_reg, temp_reg, -2);
5237 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5238 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5239 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5240 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5241 }
5242 if (num_entries - last_index == 2) {
5243 // The last missing case_value.
5244 __ Addiu(temp_reg, temp_reg, -1);
5245 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005246 }
5247
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005248 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005249 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5250 __ B(codegen_->GetLabelOf(default_block));
5251 }
5252}
5253
5254void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5255 // The trampoline uses the same calling convention as dex calling conventions,
5256 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5257 // the method_idx.
5258 HandleInvoke(invoke);
5259}
5260
5261void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5262 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5263}
5264
5265#undef __
5266#undef QUICK_ENTRY_POINT
5267
5268} // namespace mips
5269} // namespace art