blob: b42c053a2b68a33c68d42a13f1c0c73ff13404f8 [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:
447 explicit DeoptimizationSlowPathMIPS(HInstruction* instruction)
448 : instruction_(instruction) {}
449
450 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
451 __ Bind(GetEntryLabel());
452 SaveLiveRegisters(codegen, instruction_->GetLocations());
453 DCHECK(instruction_->IsDeoptimize());
454 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
455 uint32_t dex_pc = deoptimize->GetDexPc();
456 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
457 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
458 instruction_,
459 dex_pc,
460 this,
461 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000462 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200463 }
464
465 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
466
467 private:
468 HInstruction* const instruction_;
469 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
470};
471
472CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
473 const MipsInstructionSetFeatures& isa_features,
474 const CompilerOptions& compiler_options,
475 OptimizingCompilerStats* stats)
476 : CodeGenerator(graph,
477 kNumberOfCoreRegisters,
478 kNumberOfFRegisters,
479 kNumberOfRegisterPairs,
480 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
481 arraysize(kCoreCalleeSaves)),
482 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
483 arraysize(kFpuCalleeSaves)),
484 compiler_options,
485 stats),
486 block_labels_(nullptr),
487 location_builder_(graph, this),
488 instruction_visitor_(graph, this),
489 move_resolver_(graph->GetArena(), this),
490 assembler_(&isa_features),
491 isa_features_(isa_features) {
492 // Save RA (containing the return address) to mimic Quick.
493 AddAllocatedRegister(Location::RegisterLocation(RA));
494}
495
496#undef __
497#define __ down_cast<MipsAssembler*>(GetAssembler())->
498#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
499
500void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
501 // Ensure that we fix up branches.
502 __ FinalizeCode();
503
504 // Adjust native pc offsets in stack maps.
505 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
506 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
507 uint32_t new_position = __ GetAdjustedPosition(old_position);
508 DCHECK_GE(new_position, old_position);
509 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
510 }
511
512 // Adjust pc offsets for the disassembly information.
513 if (disasm_info_ != nullptr) {
514 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
515 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
516 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
517 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
518 it.second.start = __ GetAdjustedPosition(it.second.start);
519 it.second.end = __ GetAdjustedPosition(it.second.end);
520 }
521 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
522 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
523 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
524 }
525 }
526
527 CodeGenerator::Finalize(allocator);
528}
529
530MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
531 return codegen_->GetAssembler();
532}
533
534void ParallelMoveResolverMIPS::EmitMove(size_t index) {
535 DCHECK_LT(index, moves_.size());
536 MoveOperands* move = moves_[index];
537 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
538}
539
540void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
541 DCHECK_LT(index, moves_.size());
542 MoveOperands* move = moves_[index];
543 Primitive::Type type = move->GetType();
544 Location loc1 = move->GetDestination();
545 Location loc2 = move->GetSource();
546
547 DCHECK(!loc1.IsConstant());
548 DCHECK(!loc2.IsConstant());
549
550 if (loc1.Equals(loc2)) {
551 return;
552 }
553
554 if (loc1.IsRegister() && loc2.IsRegister()) {
555 // Swap 2 GPRs.
556 Register r1 = loc1.AsRegister<Register>();
557 Register r2 = loc2.AsRegister<Register>();
558 __ Move(TMP, r2);
559 __ Move(r2, r1);
560 __ Move(r1, TMP);
561 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
562 FRegister f1 = loc1.AsFpuRegister<FRegister>();
563 FRegister f2 = loc2.AsFpuRegister<FRegister>();
564 if (type == Primitive::kPrimFloat) {
565 __ MovS(FTMP, f2);
566 __ MovS(f2, f1);
567 __ MovS(f1, FTMP);
568 } else {
569 DCHECK_EQ(type, Primitive::kPrimDouble);
570 __ MovD(FTMP, f2);
571 __ MovD(f2, f1);
572 __ MovD(f1, FTMP);
573 }
574 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
575 (loc1.IsFpuRegister() && loc2.IsRegister())) {
576 // Swap FPR and GPR.
577 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
578 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
579 : loc2.AsFpuRegister<FRegister>();
580 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
581 : loc2.AsRegister<Register>();
582 __ Move(TMP, r2);
583 __ Mfc1(r2, f1);
584 __ Mtc1(TMP, f1);
585 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
586 // Swap 2 GPR register pairs.
587 Register r1 = loc1.AsRegisterPairLow<Register>();
588 Register r2 = loc2.AsRegisterPairLow<Register>();
589 __ Move(TMP, r2);
590 __ Move(r2, r1);
591 __ Move(r1, TMP);
592 r1 = loc1.AsRegisterPairHigh<Register>();
593 r2 = loc2.AsRegisterPairHigh<Register>();
594 __ Move(TMP, r2);
595 __ Move(r2, r1);
596 __ Move(r1, TMP);
597 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
598 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
599 // Swap FPR and GPR register pair.
600 DCHECK_EQ(type, Primitive::kPrimDouble);
601 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
602 : loc2.AsFpuRegister<FRegister>();
603 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
604 : loc2.AsRegisterPairLow<Register>();
605 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
606 : loc2.AsRegisterPairHigh<Register>();
607 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
608 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
609 // unpredictable and the following mfch1 will fail.
610 __ Mfc1(TMP, f1);
611 __ Mfhc1(AT, f1);
612 __ Mtc1(r2_l, f1);
613 __ Mthc1(r2_h, f1);
614 __ Move(r2_l, TMP);
615 __ Move(r2_h, AT);
616 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
617 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
618 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
619 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
620 } else {
621 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
622 }
623}
624
625void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
626 __ Pop(static_cast<Register>(reg));
627}
628
629void ParallelMoveResolverMIPS::SpillScratch(int reg) {
630 __ Push(static_cast<Register>(reg));
631}
632
633void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
634 // Allocate a scratch register other than TMP, if available.
635 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
636 // automatically unspilled when the scratch scope object is destroyed).
637 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
638 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
639 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
640 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
641 __ LoadFromOffset(kLoadWord,
642 Register(ensure_scratch.GetRegister()),
643 SP,
644 index1 + stack_offset);
645 __ LoadFromOffset(kLoadWord,
646 TMP,
647 SP,
648 index2 + stack_offset);
649 __ StoreToOffset(kStoreWord,
650 Register(ensure_scratch.GetRegister()),
651 SP,
652 index2 + stack_offset);
653 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
654 }
655}
656
657static dwarf::Reg DWARFReg(Register reg) {
658 return dwarf::Reg::MipsCore(static_cast<int>(reg));
659}
660
661// TODO: mapping of floating-point registers to DWARF.
662
663void CodeGeneratorMIPS::GenerateFrameEntry() {
664 __ Bind(&frame_entry_label_);
665
666 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
667
668 if (do_overflow_check) {
669 __ LoadFromOffset(kLoadWord,
670 ZERO,
671 SP,
672 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
673 RecordPcInfo(nullptr, 0);
674 }
675
676 if (HasEmptyFrame()) {
677 return;
678 }
679
680 // Make sure the frame size isn't unreasonably large.
681 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
682 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
683 }
684
685 // Spill callee-saved registers.
686 // Note that their cumulative size is small and they can be indexed using
687 // 16-bit offsets.
688
689 // TODO: increment/decrement SP in one step instead of two or remove this comment.
690
691 uint32_t ofs = FrameEntrySpillSize();
692 bool unaligned_float = ofs & 0x7;
693 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
694 __ IncreaseFrameSize(ofs);
695
696 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
697 Register reg = kCoreCalleeSaves[i];
698 if (allocated_registers_.ContainsCoreRegister(reg)) {
699 ofs -= kMipsWordSize;
700 __ Sw(reg, SP, ofs);
701 __ cfi().RelOffset(DWARFReg(reg), ofs);
702 }
703 }
704
705 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
706 FRegister reg = kFpuCalleeSaves[i];
707 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
708 ofs -= kMipsDoublewordSize;
709 // TODO: Change the frame to avoid unaligned accesses for fpu registers.
710 if (unaligned_float) {
711 if (fpu_32bit) {
712 __ Swc1(reg, SP, ofs);
713 __ Swc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
714 } else {
715 __ Mfhc1(TMP, reg);
716 __ Swc1(reg, SP, ofs);
717 __ Sw(TMP, SP, ofs + 4);
718 }
719 } else {
720 __ Sdc1(reg, SP, ofs);
721 }
722 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
723 }
724 }
725
726 // Allocate the rest of the frame and store the current method pointer
727 // at its end.
728
729 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
730
731 static_assert(IsInt<16>(kCurrentMethodStackOffset),
732 "kCurrentMethodStackOffset must fit into int16_t");
733 __ Sw(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
734}
735
736void CodeGeneratorMIPS::GenerateFrameExit() {
737 __ cfi().RememberState();
738
739 if (!HasEmptyFrame()) {
740 // Deallocate the rest of the frame.
741
742 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
743
744 // Restore callee-saved registers.
745 // Note that their cumulative size is small and they can be indexed using
746 // 16-bit offsets.
747
748 // TODO: increment/decrement SP in one step instead of two or remove this comment.
749
750 uint32_t ofs = 0;
751 bool unaligned_float = FrameEntrySpillSize() & 0x7;
752 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
753
754 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
755 FRegister reg = kFpuCalleeSaves[i];
756 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
757 if (unaligned_float) {
758 if (fpu_32bit) {
759 __ Lwc1(reg, SP, ofs);
760 __ Lwc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
761 } else {
762 __ Lwc1(reg, SP, ofs);
763 __ Lw(TMP, SP, ofs + 4);
764 __ Mthc1(TMP, reg);
765 }
766 } else {
767 __ Ldc1(reg, SP, ofs);
768 }
769 ofs += kMipsDoublewordSize;
770 // TODO: __ cfi().Restore(DWARFReg(reg));
771 }
772 }
773
774 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
775 Register reg = kCoreCalleeSaves[i];
776 if (allocated_registers_.ContainsCoreRegister(reg)) {
777 __ Lw(reg, SP, ofs);
778 ofs += kMipsWordSize;
779 __ cfi().Restore(DWARFReg(reg));
780 }
781 }
782
783 DCHECK_EQ(ofs, FrameEntrySpillSize());
784 __ DecreaseFrameSize(ofs);
785 }
786
787 __ Jr(RA);
788 __ Nop();
789
790 __ cfi().RestoreState();
791 __ cfi().DefCFAOffset(GetFrameSize());
792}
793
794void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
795 __ Bind(GetLabelOf(block));
796}
797
798void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
799 if (src.Equals(dst)) {
800 return;
801 }
802
803 if (src.IsConstant()) {
804 MoveConstant(dst, src.GetConstant());
805 } else {
806 if (Primitive::Is64BitType(dst_type)) {
807 Move64(dst, src);
808 } else {
809 Move32(dst, src);
810 }
811 }
812}
813
814void CodeGeneratorMIPS::Move32(Location destination, Location source) {
815 if (source.Equals(destination)) {
816 return;
817 }
818
819 if (destination.IsRegister()) {
820 if (source.IsRegister()) {
821 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
822 } else if (source.IsFpuRegister()) {
823 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
824 } else {
825 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
826 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
827 }
828 } else if (destination.IsFpuRegister()) {
829 if (source.IsRegister()) {
830 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
831 } else if (source.IsFpuRegister()) {
832 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
833 } else {
834 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
835 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
836 }
837 } else {
838 DCHECK(destination.IsStackSlot()) << destination;
839 if (source.IsRegister()) {
840 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
841 } else if (source.IsFpuRegister()) {
842 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
843 } else {
844 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
845 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
846 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
847 }
848 }
849}
850
851void CodeGeneratorMIPS::Move64(Location destination, Location source) {
852 if (source.Equals(destination)) {
853 return;
854 }
855
856 if (destination.IsRegisterPair()) {
857 if (source.IsRegisterPair()) {
858 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
859 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
860 } else if (source.IsFpuRegister()) {
861 Register dst_high = destination.AsRegisterPairHigh<Register>();
862 Register dst_low = destination.AsRegisterPairLow<Register>();
863 FRegister src = source.AsFpuRegister<FRegister>();
864 __ Mfc1(dst_low, src);
865 __ Mfhc1(dst_high, src);
866 } else {
867 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
868 int32_t off = source.GetStackIndex();
869 Register r = destination.AsRegisterPairLow<Register>();
870 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
871 }
872 } else if (destination.IsFpuRegister()) {
873 if (source.IsRegisterPair()) {
874 FRegister dst = destination.AsFpuRegister<FRegister>();
875 Register src_high = source.AsRegisterPairHigh<Register>();
876 Register src_low = source.AsRegisterPairLow<Register>();
877 __ Mtc1(src_low, dst);
878 __ Mthc1(src_high, dst);
879 } else if (source.IsFpuRegister()) {
880 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
881 } else {
882 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
883 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
884 }
885 } else {
886 DCHECK(destination.IsDoubleStackSlot()) << destination;
887 int32_t off = destination.GetStackIndex();
888 if (source.IsRegisterPair()) {
889 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
890 } else if (source.IsFpuRegister()) {
891 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
892 } else {
893 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
894 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
895 __ StoreToOffset(kStoreWord, TMP, SP, off);
896 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
897 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
898 }
899 }
900}
901
902void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
903 if (c->IsIntConstant() || c->IsNullConstant()) {
904 // Move 32 bit constant.
905 int32_t value = GetInt32ValueOf(c);
906 if (destination.IsRegister()) {
907 Register dst = destination.AsRegister<Register>();
908 __ LoadConst32(dst, value);
909 } else {
910 DCHECK(destination.IsStackSlot())
911 << "Cannot move " << c->DebugName() << " to " << destination;
912 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
913 }
914 } else if (c->IsLongConstant()) {
915 // Move 64 bit constant.
916 int64_t value = GetInt64ValueOf(c);
917 if (destination.IsRegisterPair()) {
918 Register r_h = destination.AsRegisterPairHigh<Register>();
919 Register r_l = destination.AsRegisterPairLow<Register>();
920 __ LoadConst64(r_h, r_l, value);
921 } else {
922 DCHECK(destination.IsDoubleStackSlot())
923 << "Cannot move " << c->DebugName() << " to " << destination;
924 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
925 }
926 } else if (c->IsFloatConstant()) {
927 // Move 32 bit float constant.
928 int32_t value = GetInt32ValueOf(c);
929 if (destination.IsFpuRegister()) {
930 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
931 } else {
932 DCHECK(destination.IsStackSlot())
933 << "Cannot move " << c->DebugName() << " to " << destination;
934 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
935 }
936 } else {
937 // Move 64 bit double constant.
938 DCHECK(c->IsDoubleConstant()) << c->DebugName();
939 int64_t value = GetInt64ValueOf(c);
940 if (destination.IsFpuRegister()) {
941 FRegister fd = destination.AsFpuRegister<FRegister>();
942 __ LoadDConst64(fd, value, TMP);
943 } else {
944 DCHECK(destination.IsDoubleStackSlot())
945 << "Cannot move " << c->DebugName() << " to " << destination;
946 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
947 }
948 }
949}
950
951void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
952 DCHECK(destination.IsRegister());
953 Register dst = destination.AsRegister<Register>();
954 __ LoadConst32(dst, value);
955}
956
957void CodeGeneratorMIPS::Move(HInstruction* instruction,
958 Location location,
959 HInstruction* move_for) {
960 LocationSummary* locations = instruction->GetLocations();
961 Primitive::Type type = instruction->GetType();
962 DCHECK_NE(type, Primitive::kPrimVoid);
963
964 if (instruction->IsCurrentMethod()) {
965 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
966 } else if (locations != nullptr && locations->Out().Equals(location)) {
967 return;
968 } else if (instruction->IsIntConstant()
969 || instruction->IsLongConstant()
970 || instruction->IsNullConstant()) {
971 MoveConstant(location, instruction->AsConstant());
972 } else if (instruction->IsTemporary()) {
973 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
974 if (temp_location.IsStackSlot()) {
975 Move32(location, temp_location);
976 } else {
977 DCHECK(temp_location.IsDoubleStackSlot());
978 Move64(location, temp_location);
979 }
980 } else if (instruction->IsLoadLocal()) {
981 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
982 if (Primitive::Is64BitType(type)) {
983 Move64(location, Location::DoubleStackSlot(stack_slot));
984 } else {
985 Move32(location, Location::StackSlot(stack_slot));
986 }
987 } else {
988 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
989 if (Primitive::Is64BitType(type)) {
990 Move64(location, locations->Out());
991 } else {
992 Move32(location, locations->Out());
993 }
994 }
995}
996
997void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
998 if (location.IsRegister()) {
999 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001000 } else if (location.IsRegisterPair()) {
1001 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1002 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001003 } else {
1004 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1005 }
1006}
1007
1008Location CodeGeneratorMIPS::GetStackLocation(HLoadLocal* load) const {
1009 Primitive::Type type = load->GetType();
1010
1011 switch (type) {
1012 case Primitive::kPrimNot:
1013 case Primitive::kPrimInt:
1014 case Primitive::kPrimFloat:
1015 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1016
1017 case Primitive::kPrimLong:
1018 case Primitive::kPrimDouble:
1019 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1020
1021 case Primitive::kPrimBoolean:
1022 case Primitive::kPrimByte:
1023 case Primitive::kPrimChar:
1024 case Primitive::kPrimShort:
1025 case Primitive::kPrimVoid:
1026 LOG(FATAL) << "Unexpected type " << type;
1027 }
1028
1029 LOG(FATAL) << "Unreachable";
1030 return Location::NoLocation();
1031}
1032
1033void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1034 MipsLabel done;
1035 Register card = AT;
1036 Register temp = TMP;
1037 __ Beqz(value, &done);
1038 __ LoadFromOffset(kLoadWord,
1039 card,
1040 TR,
1041 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
1042 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1043 __ Addu(temp, card, temp);
1044 __ Sb(card, temp, 0);
1045 __ Bind(&done);
1046}
1047
1048void CodeGeneratorMIPS::SetupBlockedRegisters(bool is_baseline) const {
1049 // Don't allocate the dalvik style register pair passing.
1050 blocked_register_pairs_[A1_A2] = true;
1051
1052 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1053 blocked_core_registers_[ZERO] = true;
1054 blocked_core_registers_[K0] = true;
1055 blocked_core_registers_[K1] = true;
1056 blocked_core_registers_[GP] = true;
1057 blocked_core_registers_[SP] = true;
1058 blocked_core_registers_[RA] = true;
1059
1060 // AT and TMP(T8) are used as temporary/scratch registers
1061 // (similar to how AT is used by MIPS assemblers).
1062 blocked_core_registers_[AT] = true;
1063 blocked_core_registers_[TMP] = true;
1064 blocked_fpu_registers_[FTMP] = true;
1065
1066 // Reserve suspend and thread registers.
1067 blocked_core_registers_[S0] = true;
1068 blocked_core_registers_[TR] = true;
1069
1070 // Reserve T9 for function calls
1071 blocked_core_registers_[T9] = true;
1072
1073 // Reserve odd-numbered FPU registers.
1074 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1075 blocked_fpu_registers_[i] = true;
1076 }
1077
1078 if (is_baseline) {
1079 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1080 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
1081 }
1082
1083 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1084 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1085 }
1086 }
1087
1088 UpdateBlockedPairRegisters();
1089}
1090
1091void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1092 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1093 MipsManagedRegister current =
1094 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1095 if (blocked_core_registers_[current.AsRegisterPairLow()]
1096 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1097 blocked_register_pairs_[i] = true;
1098 }
1099 }
1100}
1101
1102Location CodeGeneratorMIPS::AllocateFreeRegister(Primitive::Type type) const {
1103 switch (type) {
1104 case Primitive::kPrimLong: {
1105 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
1106 MipsManagedRegister pair =
1107 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
1108 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
1109 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
1110
1111 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
1112 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
1113 UpdateBlockedPairRegisters();
1114 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
1115 }
1116
1117 case Primitive::kPrimByte:
1118 case Primitive::kPrimBoolean:
1119 case Primitive::kPrimChar:
1120 case Primitive::kPrimShort:
1121 case Primitive::kPrimInt:
1122 case Primitive::kPrimNot: {
1123 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
1124 // Block all register pairs that contain `reg`.
1125 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1126 MipsManagedRegister current =
1127 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1128 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
1129 blocked_register_pairs_[i] = true;
1130 }
1131 }
1132 return Location::RegisterLocation(reg);
1133 }
1134
1135 case Primitive::kPrimFloat:
1136 case Primitive::kPrimDouble: {
1137 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFRegisters);
1138 return Location::FpuRegisterLocation(reg);
1139 }
1140
1141 case Primitive::kPrimVoid:
1142 LOG(FATAL) << "Unreachable type " << type;
1143 }
1144
1145 UNREACHABLE();
1146}
1147
1148size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1149 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1150 return kMipsWordSize;
1151}
1152
1153size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1154 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1155 return kMipsWordSize;
1156}
1157
1158size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1159 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1160 return kMipsDoublewordSize;
1161}
1162
1163size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1164 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1165 return kMipsDoublewordSize;
1166}
1167
1168void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
1169 stream << MipsManagedRegister::FromCoreRegister(Register(reg));
1170}
1171
1172void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1173 stream << MipsManagedRegister::FromFRegister(FRegister(reg));
1174}
1175
1176void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1177 HInstruction* instruction,
1178 uint32_t dex_pc,
1179 SlowPathCode* slow_path) {
1180 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1181 instruction,
1182 dex_pc,
1183 slow_path,
1184 IsDirectEntrypoint(entrypoint));
1185}
1186
1187constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1188
1189void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1190 HInstruction* instruction,
1191 uint32_t dex_pc,
1192 SlowPathCode* slow_path,
1193 bool is_direct_entrypoint) {
1194 if (is_direct_entrypoint) {
1195 // Reserve argument space on stack (for $a0-$a3) for
1196 // entrypoints that directly reference native implementations.
1197 // Called function may use this space to store $a0-$a3 regs.
1198 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
1199 }
1200 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1201 __ Jalr(T9);
1202 __ Nop();
1203 if (is_direct_entrypoint) {
1204 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
1205 }
1206 RecordPcInfo(instruction, dex_pc, slow_path);
1207}
1208
1209void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1210 Register class_reg) {
1211 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1212 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1213 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1214 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1215 __ Sync(0);
1216 __ Bind(slow_path->GetExitLabel());
1217}
1218
1219void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1220 __ Sync(0); // Only stype 0 is supported.
1221}
1222
1223void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1224 HBasicBlock* successor) {
1225 SuspendCheckSlowPathMIPS* slow_path =
1226 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1227 codegen_->AddSlowPath(slow_path);
1228
1229 __ LoadFromOffset(kLoadUnsignedHalfword,
1230 TMP,
1231 TR,
1232 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1233 if (successor == nullptr) {
1234 __ Bnez(TMP, slow_path->GetEntryLabel());
1235 __ Bind(slow_path->GetReturnLabel());
1236 } else {
1237 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1238 __ B(slow_path->GetEntryLabel());
1239 // slow_path will return to GetLabelOf(successor).
1240 }
1241}
1242
1243InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1244 CodeGeneratorMIPS* codegen)
1245 : HGraphVisitor(graph),
1246 assembler_(codegen->GetAssembler()),
1247 codegen_(codegen) {}
1248
1249void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1250 DCHECK_EQ(instruction->InputCount(), 2U);
1251 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1252 Primitive::Type type = instruction->GetResultType();
1253 switch (type) {
1254 case Primitive::kPrimInt: {
1255 locations->SetInAt(0, Location::RequiresRegister());
1256 HInstruction* right = instruction->InputAt(1);
1257 bool can_use_imm = false;
1258 if (right->IsConstant()) {
1259 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1260 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1261 can_use_imm = IsUint<16>(imm);
1262 } else if (instruction->IsAdd()) {
1263 can_use_imm = IsInt<16>(imm);
1264 } else {
1265 DCHECK(instruction->IsSub());
1266 can_use_imm = IsInt<16>(-imm);
1267 }
1268 }
1269 if (can_use_imm)
1270 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1271 else
1272 locations->SetInAt(1, Location::RequiresRegister());
1273 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1274 break;
1275 }
1276
1277 case Primitive::kPrimLong: {
1278 // TODO: can 2nd param be const?
1279 locations->SetInAt(0, Location::RequiresRegister());
1280 locations->SetInAt(1, Location::RequiresRegister());
1281 if (instruction->IsAdd() || instruction->IsSub()) {
1282 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1283 } else {
1284 DCHECK(instruction->IsAnd() || instruction->IsOr() || instruction->IsXor());
1285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1286 }
1287 break;
1288 }
1289
1290 case Primitive::kPrimFloat:
1291 case Primitive::kPrimDouble:
1292 DCHECK(instruction->IsAdd() || instruction->IsSub());
1293 locations->SetInAt(0, Location::RequiresFpuRegister());
1294 locations->SetInAt(1, Location::RequiresFpuRegister());
1295 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1296 break;
1297
1298 default:
1299 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1300 }
1301}
1302
1303void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1304 Primitive::Type type = instruction->GetType();
1305 LocationSummary* locations = instruction->GetLocations();
1306
1307 switch (type) {
1308 case Primitive::kPrimInt: {
1309 Register dst = locations->Out().AsRegister<Register>();
1310 Register lhs = locations->InAt(0).AsRegister<Register>();
1311 Location rhs_location = locations->InAt(1);
1312
1313 Register rhs_reg = ZERO;
1314 int32_t rhs_imm = 0;
1315 bool use_imm = rhs_location.IsConstant();
1316 if (use_imm) {
1317 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1318 } else {
1319 rhs_reg = rhs_location.AsRegister<Register>();
1320 }
1321
1322 if (instruction->IsAnd()) {
1323 if (use_imm)
1324 __ Andi(dst, lhs, rhs_imm);
1325 else
1326 __ And(dst, lhs, rhs_reg);
1327 } else if (instruction->IsOr()) {
1328 if (use_imm)
1329 __ Ori(dst, lhs, rhs_imm);
1330 else
1331 __ Or(dst, lhs, rhs_reg);
1332 } else if (instruction->IsXor()) {
1333 if (use_imm)
1334 __ Xori(dst, lhs, rhs_imm);
1335 else
1336 __ Xor(dst, lhs, rhs_reg);
1337 } else if (instruction->IsAdd()) {
1338 if (use_imm)
1339 __ Addiu(dst, lhs, rhs_imm);
1340 else
1341 __ Addu(dst, lhs, rhs_reg);
1342 } else {
1343 DCHECK(instruction->IsSub());
1344 if (use_imm)
1345 __ Addiu(dst, lhs, -rhs_imm);
1346 else
1347 __ Subu(dst, lhs, rhs_reg);
1348 }
1349 break;
1350 }
1351
1352 case Primitive::kPrimLong: {
1353 // TODO: can 2nd param be const?
1354 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1355 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1356 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1357 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1358 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
1359 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
1360
1361 if (instruction->IsAnd()) {
1362 __ And(dst_low, lhs_low, rhs_low);
1363 __ And(dst_high, lhs_high, rhs_high);
1364 } else if (instruction->IsOr()) {
1365 __ Or(dst_low, lhs_low, rhs_low);
1366 __ Or(dst_high, lhs_high, rhs_high);
1367 } else if (instruction->IsXor()) {
1368 __ Xor(dst_low, lhs_low, rhs_low);
1369 __ Xor(dst_high, lhs_high, rhs_high);
1370 } else if (instruction->IsAdd()) {
1371 __ Addu(dst_low, lhs_low, rhs_low);
1372 __ Sltu(TMP, dst_low, lhs_low);
1373 __ Addu(dst_high, lhs_high, rhs_high);
1374 __ Addu(dst_high, dst_high, TMP);
1375 } else {
1376 DCHECK(instruction->IsSub());
1377 __ Subu(dst_low, lhs_low, rhs_low);
1378 __ Sltu(TMP, lhs_low, dst_low);
1379 __ Subu(dst_high, lhs_high, rhs_high);
1380 __ Subu(dst_high, dst_high, TMP);
1381 }
1382 break;
1383 }
1384
1385 case Primitive::kPrimFloat:
1386 case Primitive::kPrimDouble: {
1387 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1388 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1389 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1390 if (instruction->IsAdd()) {
1391 if (type == Primitive::kPrimFloat) {
1392 __ AddS(dst, lhs, rhs);
1393 } else {
1394 __ AddD(dst, lhs, rhs);
1395 }
1396 } else {
1397 DCHECK(instruction->IsSub());
1398 if (type == Primitive::kPrimFloat) {
1399 __ SubS(dst, lhs, rhs);
1400 } else {
1401 __ SubD(dst, lhs, rhs);
1402 }
1403 }
1404 break;
1405 }
1406
1407 default:
1408 LOG(FATAL) << "Unexpected binary operation type " << type;
1409 }
1410}
1411
1412void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
1413 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1414
1415 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1416 Primitive::Type type = instr->GetResultType();
1417 switch (type) {
1418 case Primitive::kPrimInt:
1419 case Primitive::kPrimLong: {
1420 locations->SetInAt(0, Location::RequiresRegister());
1421 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1422 locations->SetOut(Location::RequiresRegister());
1423 break;
1424 }
1425 default:
1426 LOG(FATAL) << "Unexpected shift type " << type;
1427 }
1428}
1429
1430static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1431
1432void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
1433 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1434 LocationSummary* locations = instr->GetLocations();
1435 Primitive::Type type = instr->GetType();
1436
1437 Location rhs_location = locations->InAt(1);
1438 bool use_imm = rhs_location.IsConstant();
1439 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1440 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
1441 uint32_t shift_mask = (type == Primitive::kPrimInt) ? kMaxIntShiftValue : kMaxLongShiftValue;
1442 uint32_t shift_value = rhs_imm & shift_mask;
1443
1444 switch (type) {
1445 case Primitive::kPrimInt: {
1446 Register dst = locations->Out().AsRegister<Register>();
1447 Register lhs = locations->InAt(0).AsRegister<Register>();
1448 if (use_imm) {
1449 if (instr->IsShl()) {
1450 __ Sll(dst, lhs, shift_value);
1451 } else if (instr->IsShr()) {
1452 __ Sra(dst, lhs, shift_value);
1453 } else {
1454 __ Srl(dst, lhs, shift_value);
1455 }
1456 } else {
1457 if (instr->IsShl()) {
1458 __ Sllv(dst, lhs, rhs_reg);
1459 } else if (instr->IsShr()) {
1460 __ Srav(dst, lhs, rhs_reg);
1461 } else {
1462 __ Srlv(dst, lhs, rhs_reg);
1463 }
1464 }
1465 break;
1466 }
1467
1468 case Primitive::kPrimLong: {
1469 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1470 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1471 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1472 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1473 if (use_imm) {
1474 if (shift_value == 0) {
1475 codegen_->Move64(locations->Out(), locations->InAt(0));
1476 } else if (shift_value < kMipsBitsPerWord) {
1477 if (instr->IsShl()) {
1478 __ Sll(dst_low, lhs_low, shift_value);
1479 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1480 __ Sll(dst_high, lhs_high, shift_value);
1481 __ Or(dst_high, dst_high, TMP);
1482 } else if (instr->IsShr()) {
1483 __ Sra(dst_high, lhs_high, shift_value);
1484 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1485 __ Srl(dst_low, lhs_low, shift_value);
1486 __ Or(dst_low, dst_low, TMP);
1487 } else {
1488 __ Srl(dst_high, lhs_high, shift_value);
1489 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1490 __ Srl(dst_low, lhs_low, shift_value);
1491 __ Or(dst_low, dst_low, TMP);
1492 }
1493 } else {
1494 shift_value -= kMipsBitsPerWord;
1495 if (instr->IsShl()) {
1496 __ Sll(dst_high, lhs_low, shift_value);
1497 __ Move(dst_low, ZERO);
1498 } else if (instr->IsShr()) {
1499 __ Sra(dst_low, lhs_high, shift_value);
1500 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
1501 } else {
1502 __ Srl(dst_low, lhs_high, shift_value);
1503 __ Move(dst_high, ZERO);
1504 }
1505 }
1506 } else {
1507 MipsLabel done;
1508 if (instr->IsShl()) {
1509 __ Sllv(dst_low, lhs_low, rhs_reg);
1510 __ Nor(AT, ZERO, rhs_reg);
1511 __ Srl(TMP, lhs_low, 1);
1512 __ Srlv(TMP, TMP, AT);
1513 __ Sllv(dst_high, lhs_high, rhs_reg);
1514 __ Or(dst_high, dst_high, TMP);
1515 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1516 __ Beqz(TMP, &done);
1517 __ Move(dst_high, dst_low);
1518 __ Move(dst_low, ZERO);
1519 } else if (instr->IsShr()) {
1520 __ Srav(dst_high, lhs_high, rhs_reg);
1521 __ Nor(AT, ZERO, rhs_reg);
1522 __ Sll(TMP, lhs_high, 1);
1523 __ Sllv(TMP, TMP, AT);
1524 __ Srlv(dst_low, lhs_low, rhs_reg);
1525 __ Or(dst_low, dst_low, TMP);
1526 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1527 __ Beqz(TMP, &done);
1528 __ Move(dst_low, dst_high);
1529 __ Sra(dst_high, dst_high, 31);
1530 } else {
1531 __ Srlv(dst_high, lhs_high, rhs_reg);
1532 __ Nor(AT, ZERO, rhs_reg);
1533 __ Sll(TMP, lhs_high, 1);
1534 __ Sllv(TMP, TMP, AT);
1535 __ Srlv(dst_low, lhs_low, rhs_reg);
1536 __ Or(dst_low, dst_low, TMP);
1537 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1538 __ Beqz(TMP, &done);
1539 __ Move(dst_low, dst_high);
1540 __ Move(dst_high, ZERO);
1541 }
1542 __ Bind(&done);
1543 }
1544 break;
1545 }
1546
1547 default:
1548 LOG(FATAL) << "Unexpected shift operation type " << type;
1549 }
1550}
1551
1552void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1553 HandleBinaryOp(instruction);
1554}
1555
1556void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1557 HandleBinaryOp(instruction);
1558}
1559
1560void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1561 HandleBinaryOp(instruction);
1562}
1563
1564void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1565 HandleBinaryOp(instruction);
1566}
1567
1568void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1569 LocationSummary* locations =
1570 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1571 locations->SetInAt(0, Location::RequiresRegister());
1572 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1573 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1574 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1575 } else {
1576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1577 }
1578}
1579
1580void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1581 LocationSummary* locations = instruction->GetLocations();
1582 Register obj = locations->InAt(0).AsRegister<Register>();
1583 Location index = locations->InAt(1);
1584 Primitive::Type type = instruction->GetType();
1585
1586 switch (type) {
1587 case Primitive::kPrimBoolean: {
1588 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1589 Register out = locations->Out().AsRegister<Register>();
1590 if (index.IsConstant()) {
1591 size_t offset =
1592 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1593 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1594 } else {
1595 __ Addu(TMP, obj, index.AsRegister<Register>());
1596 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1597 }
1598 break;
1599 }
1600
1601 case Primitive::kPrimByte: {
1602 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1603 Register out = locations->Out().AsRegister<Register>();
1604 if (index.IsConstant()) {
1605 size_t offset =
1606 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1607 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1608 } else {
1609 __ Addu(TMP, obj, index.AsRegister<Register>());
1610 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1611 }
1612 break;
1613 }
1614
1615 case Primitive::kPrimShort: {
1616 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1617 Register out = locations->Out().AsRegister<Register>();
1618 if (index.IsConstant()) {
1619 size_t offset =
1620 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1621 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1622 } else {
1623 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1624 __ Addu(TMP, obj, TMP);
1625 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1626 }
1627 break;
1628 }
1629
1630 case Primitive::kPrimChar: {
1631 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1632 Register out = locations->Out().AsRegister<Register>();
1633 if (index.IsConstant()) {
1634 size_t offset =
1635 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1636 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1637 } else {
1638 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1639 __ Addu(TMP, obj, TMP);
1640 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1641 }
1642 break;
1643 }
1644
1645 case Primitive::kPrimInt:
1646 case Primitive::kPrimNot: {
1647 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1648 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1649 Register out = locations->Out().AsRegister<Register>();
1650 if (index.IsConstant()) {
1651 size_t offset =
1652 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1653 __ LoadFromOffset(kLoadWord, out, obj, offset);
1654 } else {
1655 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1656 __ Addu(TMP, obj, TMP);
1657 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1658 }
1659 break;
1660 }
1661
1662 case Primitive::kPrimLong: {
1663 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1664 Register out = locations->Out().AsRegisterPairLow<Register>();
1665 if (index.IsConstant()) {
1666 size_t offset =
1667 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1668 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1669 } else {
1670 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1671 __ Addu(TMP, obj, TMP);
1672 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1673 }
1674 break;
1675 }
1676
1677 case Primitive::kPrimFloat: {
1678 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1679 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1680 if (index.IsConstant()) {
1681 size_t offset =
1682 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1683 __ LoadSFromOffset(out, obj, offset);
1684 } else {
1685 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1686 __ Addu(TMP, obj, TMP);
1687 __ LoadSFromOffset(out, TMP, data_offset);
1688 }
1689 break;
1690 }
1691
1692 case Primitive::kPrimDouble: {
1693 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1694 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1695 if (index.IsConstant()) {
1696 size_t offset =
1697 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1698 __ LoadDFromOffset(out, obj, offset);
1699 } else {
1700 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1701 __ Addu(TMP, obj, TMP);
1702 __ LoadDFromOffset(out, TMP, data_offset);
1703 }
1704 break;
1705 }
1706
1707 case Primitive::kPrimVoid:
1708 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1709 UNREACHABLE();
1710 }
1711 codegen_->MaybeRecordImplicitNullCheck(instruction);
1712}
1713
1714void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1715 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1716 locations->SetInAt(0, Location::RequiresRegister());
1717 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1718}
1719
1720void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1721 LocationSummary* locations = instruction->GetLocations();
1722 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1723 Register obj = locations->InAt(0).AsRegister<Register>();
1724 Register out = locations->Out().AsRegister<Register>();
1725 __ LoadFromOffset(kLoadWord, out, obj, offset);
1726 codegen_->MaybeRecordImplicitNullCheck(instruction);
1727}
1728
1729void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001730 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001731 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1732 instruction,
Pavle Batuta934808f2015-11-03 13:23:54 +01001733 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1734 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001735 InvokeRuntimeCallingConvention calling_convention;
1736 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1737 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1738 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1739 } else {
1740 locations->SetInAt(0, Location::RequiresRegister());
1741 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1742 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1743 locations->SetInAt(2, Location::RequiresFpuRegister());
1744 } else {
1745 locations->SetInAt(2, Location::RequiresRegister());
1746 }
1747 }
1748}
1749
1750void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1751 LocationSummary* locations = instruction->GetLocations();
1752 Register obj = locations->InAt(0).AsRegister<Register>();
1753 Location index = locations->InAt(1);
1754 Primitive::Type value_type = instruction->GetComponentType();
1755 bool needs_runtime_call = locations->WillCall();
1756 bool needs_write_barrier =
1757 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1758
1759 switch (value_type) {
1760 case Primitive::kPrimBoolean:
1761 case Primitive::kPrimByte: {
1762 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1763 Register value = locations->InAt(2).AsRegister<Register>();
1764 if (index.IsConstant()) {
1765 size_t offset =
1766 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1767 __ StoreToOffset(kStoreByte, value, obj, offset);
1768 } else {
1769 __ Addu(TMP, obj, index.AsRegister<Register>());
1770 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1771 }
1772 break;
1773 }
1774
1775 case Primitive::kPrimShort:
1776 case Primitive::kPrimChar: {
1777 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1778 Register value = locations->InAt(2).AsRegister<Register>();
1779 if (index.IsConstant()) {
1780 size_t offset =
1781 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1782 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1783 } else {
1784 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1785 __ Addu(TMP, obj, TMP);
1786 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1787 }
1788 break;
1789 }
1790
1791 case Primitive::kPrimInt:
1792 case Primitive::kPrimNot: {
1793 if (!needs_runtime_call) {
1794 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1795 Register value = locations->InAt(2).AsRegister<Register>();
1796 if (index.IsConstant()) {
1797 size_t offset =
1798 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1799 __ StoreToOffset(kStoreWord, value, obj, offset);
1800 } else {
1801 DCHECK(index.IsRegister()) << index;
1802 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1803 __ Addu(TMP, obj, TMP);
1804 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1805 }
1806 codegen_->MaybeRecordImplicitNullCheck(instruction);
1807 if (needs_write_barrier) {
1808 DCHECK_EQ(value_type, Primitive::kPrimNot);
1809 codegen_->MarkGCCard(obj, value);
1810 }
1811 } else {
1812 DCHECK_EQ(value_type, Primitive::kPrimNot);
1813 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1814 instruction,
1815 instruction->GetDexPc(),
1816 nullptr,
1817 IsDirectEntrypoint(kQuickAputObject));
1818 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
1819 }
1820 break;
1821 }
1822
1823 case Primitive::kPrimLong: {
1824 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1825 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
1826 if (index.IsConstant()) {
1827 size_t offset =
1828 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1829 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1830 } else {
1831 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1832 __ Addu(TMP, obj, TMP);
1833 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1834 }
1835 break;
1836 }
1837
1838 case Primitive::kPrimFloat: {
1839 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1840 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1841 DCHECK(locations->InAt(2).IsFpuRegister());
1842 if (index.IsConstant()) {
1843 size_t offset =
1844 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1845 __ StoreSToOffset(value, obj, offset);
1846 } else {
1847 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1848 __ Addu(TMP, obj, TMP);
1849 __ StoreSToOffset(value, TMP, data_offset);
1850 }
1851 break;
1852 }
1853
1854 case Primitive::kPrimDouble: {
1855 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1856 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1857 DCHECK(locations->InAt(2).IsFpuRegister());
1858 if (index.IsConstant()) {
1859 size_t offset =
1860 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1861 __ StoreDToOffset(value, obj, offset);
1862 } else {
1863 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1864 __ Addu(TMP, obj, TMP);
1865 __ StoreDToOffset(value, TMP, data_offset);
1866 }
1867 break;
1868 }
1869
1870 case Primitive::kPrimVoid:
1871 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1872 UNREACHABLE();
1873 }
1874
1875 // Ints and objects are handled in the switch.
1876 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1877 codegen_->MaybeRecordImplicitNullCheck(instruction);
1878 }
1879}
1880
1881void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1882 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1883 ? LocationSummary::kCallOnSlowPath
1884 : LocationSummary::kNoCall;
1885 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1886 locations->SetInAt(0, Location::RequiresRegister());
1887 locations->SetInAt(1, Location::RequiresRegister());
1888 if (instruction->HasUses()) {
1889 locations->SetOut(Location::SameAsFirstInput());
1890 }
1891}
1892
1893void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1894 LocationSummary* locations = instruction->GetLocations();
1895 BoundsCheckSlowPathMIPS* slow_path =
1896 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
1897 codegen_->AddSlowPath(slow_path);
1898
1899 Register index = locations->InAt(0).AsRegister<Register>();
1900 Register length = locations->InAt(1).AsRegister<Register>();
1901
1902 // length is limited by the maximum positive signed 32-bit integer.
1903 // Unsigned comparison of length and index checks for index < 0
1904 // and for length <= index simultaneously.
1905 __ Bgeu(index, length, slow_path->GetEntryLabel());
1906}
1907
1908void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
1909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1910 instruction,
1911 LocationSummary::kCallOnSlowPath);
1912 locations->SetInAt(0, Location::RequiresRegister());
1913 locations->SetInAt(1, Location::RequiresRegister());
1914 // Note that TypeCheckSlowPathMIPS uses this register too.
1915 locations->AddTemp(Location::RequiresRegister());
1916}
1917
1918void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
1919 LocationSummary* locations = instruction->GetLocations();
1920 Register obj = locations->InAt(0).AsRegister<Register>();
1921 Register cls = locations->InAt(1).AsRegister<Register>();
1922 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
1923
1924 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
1925 codegen_->AddSlowPath(slow_path);
1926
1927 // TODO: avoid this check if we know obj is not null.
1928 __ Beqz(obj, slow_path->GetExitLabel());
1929 // Compare the class of `obj` with `cls`.
1930 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1931 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
1932 __ Bind(slow_path->GetExitLabel());
1933}
1934
1935void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
1936 LocationSummary* locations =
1937 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1938 locations->SetInAt(0, Location::RequiresRegister());
1939 if (check->HasUses()) {
1940 locations->SetOut(Location::SameAsFirstInput());
1941 }
1942}
1943
1944void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
1945 // We assume the class is not null.
1946 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
1947 check->GetLoadClass(),
1948 check,
1949 check->GetDexPc(),
1950 true);
1951 codegen_->AddSlowPath(slow_path);
1952 GenerateClassInitializationCheck(slow_path,
1953 check->GetLocations()->InAt(0).AsRegister<Register>());
1954}
1955
1956void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
1957 Primitive::Type in_type = compare->InputAt(0)->GetType();
1958
1959 LocationSummary::CallKind call_kind = Primitive::IsFloatingPointType(in_type)
1960 ? LocationSummary::kCall
1961 : LocationSummary::kNoCall;
1962
1963 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare, call_kind);
1964
1965 switch (in_type) {
1966 case Primitive::kPrimLong:
1967 locations->SetInAt(0, Location::RequiresRegister());
1968 locations->SetInAt(1, Location::RequiresRegister());
1969 // Output overlaps because it is written before doing the low comparison.
1970 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1971 break;
1972
1973 case Primitive::kPrimFloat:
1974 case Primitive::kPrimDouble: {
1975 InvokeRuntimeCallingConvention calling_convention;
1976 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
1977 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
1978 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1979 break;
1980 }
1981
1982 default:
1983 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1984 }
1985}
1986
1987void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
1988 LocationSummary* locations = instruction->GetLocations();
1989 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1990
1991 // 0 if: left == right
1992 // 1 if: left > right
1993 // -1 if: left < right
1994 switch (in_type) {
1995 case Primitive::kPrimLong: {
1996 MipsLabel done;
1997 Register res = locations->Out().AsRegister<Register>();
1998 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1999 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2000 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2001 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2002 // TODO: more efficient (direct) comparison with a constant.
2003 __ Slt(TMP, lhs_high, rhs_high);
2004 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2005 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2006 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2007 __ Sltu(TMP, lhs_low, rhs_low);
2008 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2009 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2010 __ Bind(&done);
2011 break;
2012 }
2013
2014 case Primitive::kPrimFloat:
2015 case Primitive::kPrimDouble: {
2016 int32_t entry_point_offset;
2017 bool direct;
2018 if (in_type == Primitive::kPrimFloat) {
2019 if (instruction->IsGtBias()) {
2020 entry_point_offset = QUICK_ENTRY_POINT(pCmpgFloat);
2021 direct = IsDirectEntrypoint(kQuickCmpgFloat);
2022 } else {
2023 entry_point_offset = QUICK_ENTRY_POINT(pCmplFloat);
2024 direct = IsDirectEntrypoint(kQuickCmplFloat);
2025 }
2026 } else {
2027 if (instruction->IsGtBias()) {
2028 entry_point_offset = QUICK_ENTRY_POINT(pCmpgDouble);
2029 direct = IsDirectEntrypoint(kQuickCmpgDouble);
2030 } else {
2031 entry_point_offset = QUICK_ENTRY_POINT(pCmplDouble);
2032 direct = IsDirectEntrypoint(kQuickCmplDouble);
2033 }
2034 }
2035 codegen_->InvokeRuntime(entry_point_offset,
2036 instruction,
2037 instruction->GetDexPc(),
2038 nullptr,
2039 direct);
2040 if (in_type == Primitive::kPrimFloat) {
2041 if (instruction->IsGtBias()) {
2042 CheckEntrypointTypes<kQuickCmpgFloat, int32_t, float, float>();
2043 } else {
2044 CheckEntrypointTypes<kQuickCmplFloat, int32_t, float, float>();
2045 }
2046 } else {
2047 if (instruction->IsGtBias()) {
2048 CheckEntrypointTypes<kQuickCmpgDouble, int32_t, double, double>();
2049 } else {
2050 CheckEntrypointTypes<kQuickCmplDouble, int32_t, double, double>();
2051 }
2052 }
2053 break;
2054 }
2055
2056 default:
2057 LOG(FATAL) << "Unimplemented compare type " << in_type;
2058 }
2059}
2060
2061void LocationsBuilderMIPS::VisitCondition(HCondition* instruction) {
2062 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2063 locations->SetInAt(0, Location::RequiresRegister());
2064 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2065 if (instruction->NeedsMaterialization()) {
2066 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2067 }
2068}
2069
2070void InstructionCodeGeneratorMIPS::VisitCondition(HCondition* instruction) {
2071 if (!instruction->NeedsMaterialization()) {
2072 return;
2073 }
2074 // TODO: generalize to long
2075 DCHECK_NE(instruction->InputAt(0)->GetType(), Primitive::kPrimLong);
2076
2077 LocationSummary* locations = instruction->GetLocations();
2078 Register dst = locations->Out().AsRegister<Register>();
2079
2080 Register lhs = locations->InAt(0).AsRegister<Register>();
2081 Location rhs_location = locations->InAt(1);
2082
2083 Register rhs_reg = ZERO;
2084 int64_t rhs_imm = 0;
2085 bool use_imm = rhs_location.IsConstant();
2086 if (use_imm) {
2087 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2088 } else {
2089 rhs_reg = rhs_location.AsRegister<Register>();
2090 }
2091
2092 IfCondition if_cond = instruction->GetCondition();
2093
2094 switch (if_cond) {
2095 case kCondEQ:
2096 case kCondNE:
2097 if (use_imm && IsUint<16>(rhs_imm)) {
2098 __ Xori(dst, lhs, rhs_imm);
2099 } else {
2100 if (use_imm) {
2101 rhs_reg = TMP;
2102 __ LoadConst32(rhs_reg, rhs_imm);
2103 }
2104 __ Xor(dst, lhs, rhs_reg);
2105 }
2106 if (if_cond == kCondEQ) {
2107 __ Sltiu(dst, dst, 1);
2108 } else {
2109 __ Sltu(dst, ZERO, dst);
2110 }
2111 break;
2112
2113 case kCondLT:
2114 case kCondGE:
2115 if (use_imm && IsInt<16>(rhs_imm)) {
2116 __ Slti(dst, lhs, rhs_imm);
2117 } else {
2118 if (use_imm) {
2119 rhs_reg = TMP;
2120 __ LoadConst32(rhs_reg, rhs_imm);
2121 }
2122 __ Slt(dst, lhs, rhs_reg);
2123 }
2124 if (if_cond == kCondGE) {
2125 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2126 // only the slt instruction but no sge.
2127 __ Xori(dst, dst, 1);
2128 }
2129 break;
2130
2131 case kCondLE:
2132 case kCondGT:
2133 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2134 // Simulate lhs <= rhs via lhs < rhs + 1.
2135 __ Slti(dst, lhs, rhs_imm + 1);
2136 if (if_cond == kCondGT) {
2137 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2138 // only the slti instruction but no sgti.
2139 __ Xori(dst, dst, 1);
2140 }
2141 } else {
2142 if (use_imm) {
2143 rhs_reg = TMP;
2144 __ LoadConst32(rhs_reg, rhs_imm);
2145 }
2146 __ Slt(dst, rhs_reg, lhs);
2147 if (if_cond == kCondLE) {
2148 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2149 // only the slt instruction but no sle.
2150 __ Xori(dst, dst, 1);
2151 }
2152 }
2153 break;
2154
2155 case kCondB:
2156 case kCondAE:
2157 // Use sltiu instruction if rhs_imm is in range [0, 32767] or in
2158 // [max_unsigned - 32767 = 0xffff8000, max_unsigned = 0xffffffff].
2159 if (use_imm &&
2160 (IsUint<15>(rhs_imm) ||
2161 IsUint<15>(rhs_imm - (MaxInt<uint64_t>(32) - MaxInt<uint64_t>(15))))) {
2162 if (IsUint<15>(rhs_imm)) {
2163 __ Sltiu(dst, lhs, rhs_imm);
2164 } else {
2165 // 16-bit value (in range [0x8000, 0xffff]) passed to sltiu is sign-extended
2166 // and then used as unsigned integer (range [0xffff8000, 0xffffffff]).
2167 __ Sltiu(dst, lhs, rhs_imm - (MaxInt<uint64_t>(32) - MaxInt<uint64_t>(16)));
2168 }
2169 } else {
2170 if (use_imm) {
2171 rhs_reg = TMP;
2172 __ LoadConst32(rhs_reg, rhs_imm);
2173 }
2174 __ Sltu(dst, lhs, rhs_reg);
2175 }
2176 if (if_cond == kCondAE) {
2177 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2178 // only the sltu instruction but no sgeu.
2179 __ Xori(dst, dst, 1);
2180 }
2181 break;
2182
2183 case kCondBE:
2184 case kCondA:
2185 // Use sltiu instruction if rhs_imm is in range [0, 32766] or in
2186 // [max_unsigned - 32767 - 1 = 0xffff7fff, max_unsigned - 1 = 0xfffffffe].
2187 // lhs <= rhs is simulated via lhs < rhs + 1.
2188 if (use_imm && (rhs_imm != -1) &&
2189 (IsUint<15>(rhs_imm + 1) ||
2190 IsUint<15>(rhs_imm + 1 - (MaxInt<uint64_t>(32) - MaxInt<uint64_t>(15))))) {
2191 if (IsUint<15>(rhs_imm + 1)) {
2192 // Simulate lhs <= rhs via lhs < rhs + 1.
2193 __ Sltiu(dst, lhs, rhs_imm + 1);
2194 } else {
2195 // 16-bit value (in range [0x8000, 0xffff]) passed to sltiu is sign-extended
2196 // and then used as unsigned integer (range [0xffff8000, 0xffffffff] where rhs_imm
2197 // is in range [0xffff7fff, 0xfffffffe] since lhs <= rhs is simulated via lhs < rhs + 1).
2198 __ Sltiu(dst, lhs, rhs_imm + 1 - (MaxInt<uint64_t>(32) - MaxInt<uint64_t>(16)));
2199 }
2200 if (if_cond == kCondA) {
2201 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2202 // only the sltiu instruction but no sgtiu.
2203 __ Xori(dst, dst, 1);
2204 }
2205 } else {
2206 if (use_imm) {
2207 rhs_reg = TMP;
2208 __ LoadConst32(rhs_reg, rhs_imm);
2209 }
2210 __ Sltu(dst, rhs_reg, lhs);
2211 if (if_cond == kCondBE) {
2212 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2213 // only the sltu instruction but no sleu.
2214 __ Xori(dst, dst, 1);
2215 }
2216 }
2217 break;
2218 }
2219}
2220
2221void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2222 Primitive::Type type = div->GetResultType();
2223 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
2224 ? LocationSummary::kCall
2225 : LocationSummary::kNoCall;
2226
2227 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2228
2229 switch (type) {
2230 case Primitive::kPrimInt:
2231 locations->SetInAt(0, Location::RequiresRegister());
2232 locations->SetInAt(1, Location::RequiresRegister());
2233 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2234 break;
2235
2236 case Primitive::kPrimLong: {
2237 InvokeRuntimeCallingConvention calling_convention;
2238 locations->SetInAt(0, Location::RegisterPairLocation(
2239 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2240 locations->SetInAt(1, Location::RegisterPairLocation(
2241 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2242 locations->SetOut(calling_convention.GetReturnLocation(type));
2243 break;
2244 }
2245
2246 case Primitive::kPrimFloat:
2247 case Primitive::kPrimDouble:
2248 locations->SetInAt(0, Location::RequiresFpuRegister());
2249 locations->SetInAt(1, Location::RequiresFpuRegister());
2250 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2251 break;
2252
2253 default:
2254 LOG(FATAL) << "Unexpected div type " << type;
2255 }
2256}
2257
2258void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2259 Primitive::Type type = instruction->GetType();
2260 LocationSummary* locations = instruction->GetLocations();
2261 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2262
2263 switch (type) {
2264 case Primitive::kPrimInt: {
2265 Register dst = locations->Out().AsRegister<Register>();
2266 Register lhs = locations->InAt(0).AsRegister<Register>();
2267 Register rhs = locations->InAt(1).AsRegister<Register>();
2268 if (isR6) {
2269 __ DivR6(dst, lhs, rhs);
2270 } else {
2271 __ DivR2(dst, lhs, rhs);
2272 }
2273 break;
2274 }
2275 case Primitive::kPrimLong: {
2276 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2277 instruction,
2278 instruction->GetDexPc(),
2279 nullptr,
2280 IsDirectEntrypoint(kQuickLdiv));
2281 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2282 break;
2283 }
2284 case Primitive::kPrimFloat:
2285 case Primitive::kPrimDouble: {
2286 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2287 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2288 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2289 if (type == Primitive::kPrimFloat) {
2290 __ DivS(dst, lhs, rhs);
2291 } else {
2292 __ DivD(dst, lhs, rhs);
2293 }
2294 break;
2295 }
2296 default:
2297 LOG(FATAL) << "Unexpected div type " << type;
2298 }
2299}
2300
2301void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2302 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2303 ? LocationSummary::kCallOnSlowPath
2304 : LocationSummary::kNoCall;
2305 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2306 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2307 if (instruction->HasUses()) {
2308 locations->SetOut(Location::SameAsFirstInput());
2309 }
2310}
2311
2312void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2313 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2314 codegen_->AddSlowPath(slow_path);
2315 Location value = instruction->GetLocations()->InAt(0);
2316 Primitive::Type type = instruction->GetType();
2317
2318 switch (type) {
2319 case Primitive::kPrimByte:
2320 case Primitive::kPrimChar:
2321 case Primitive::kPrimShort:
2322 case Primitive::kPrimInt: {
2323 if (value.IsConstant()) {
2324 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2325 __ B(slow_path->GetEntryLabel());
2326 } else {
2327 // A division by a non-null constant is valid. We don't need to perform
2328 // any check, so simply fall through.
2329 }
2330 } else {
2331 DCHECK(value.IsRegister()) << value;
2332 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2333 }
2334 break;
2335 }
2336 case Primitive::kPrimLong: {
2337 if (value.IsConstant()) {
2338 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2339 __ B(slow_path->GetEntryLabel());
2340 } else {
2341 // A division by a non-null constant is valid. We don't need to perform
2342 // any check, so simply fall through.
2343 }
2344 } else {
2345 DCHECK(value.IsRegisterPair()) << value;
2346 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2347 __ Beqz(TMP, slow_path->GetEntryLabel());
2348 }
2349 break;
2350 }
2351 default:
2352 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2353 }
2354}
2355
2356void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2357 LocationSummary* locations =
2358 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2359 locations->SetOut(Location::ConstantLocation(constant));
2360}
2361
2362void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2363 // Will be generated at use site.
2364}
2365
2366void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2367 exit->SetLocations(nullptr);
2368}
2369
2370void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2371}
2372
2373void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2374 LocationSummary* locations =
2375 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2376 locations->SetOut(Location::ConstantLocation(constant));
2377}
2378
2379void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2380 // Will be generated at use site.
2381}
2382
2383void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2384 got->SetLocations(nullptr);
2385}
2386
2387void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2388 DCHECK(!successor->IsExitBlock());
2389 HBasicBlock* block = got->GetBlock();
2390 HInstruction* previous = got->GetPrevious();
2391 HLoopInformation* info = block->GetLoopInformation();
2392
2393 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2394 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2395 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2396 return;
2397 }
2398 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2399 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2400 }
2401 if (!codegen_->GoesToNextBlock(block, successor)) {
2402 __ B(codegen_->GetLabelOf(successor));
2403 }
2404}
2405
2406void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2407 HandleGoto(got, got->GetSuccessor());
2408}
2409
2410void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2411 try_boundary->SetLocations(nullptr);
2412}
2413
2414void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2415 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2416 if (!successor->IsExitBlock()) {
2417 HandleGoto(try_boundary, successor);
2418 }
2419}
2420
2421void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002422 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002423 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002424 MipsLabel* false_target) {
2425 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002426
David Brazdil0debae72015-11-12 18:37:00 +00002427 if (true_target == nullptr && false_target == nullptr) {
2428 // Nothing to do. The code always falls through.
2429 return;
2430 } else if (cond->IsIntConstant()) {
2431 // Constant condition, statically compared against 1.
2432 if (cond->AsIntConstant()->IsOne()) {
2433 if (true_target != nullptr) {
2434 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002435 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002436 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002437 DCHECK(cond->AsIntConstant()->IsZero());
2438 if (false_target != nullptr) {
2439 __ B(false_target);
2440 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002441 }
David Brazdil0debae72015-11-12 18:37:00 +00002442 return;
2443 }
2444
2445 // The following code generates these patterns:
2446 // (1) true_target == nullptr && false_target != nullptr
2447 // - opposite condition true => branch to false_target
2448 // (2) true_target != nullptr && false_target == nullptr
2449 // - condition true => branch to true_target
2450 // (3) true_target != nullptr && false_target != nullptr
2451 // - condition true => branch to true_target
2452 // - branch to false_target
2453 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002454 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002455 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002456 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002457 if (true_target == nullptr) {
2458 __ Beqz(cond_val.AsRegister<Register>(), false_target);
2459 } else {
2460 __ Bnez(cond_val.AsRegister<Register>(), true_target);
2461 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002462 } else {
2463 // The condition instruction has not been materialized, use its inputs as
2464 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002465 HCondition* condition = cond->AsCondition();
2466
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002467 Register lhs = condition->GetLocations()->InAt(0).AsRegister<Register>();
2468 Location rhs_location = condition->GetLocations()->InAt(1);
2469 Register rhs_reg = ZERO;
2470 int32_t rhs_imm = 0;
2471 bool use_imm = rhs_location.IsConstant();
2472 if (use_imm) {
2473 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2474 } else {
2475 rhs_reg = rhs_location.AsRegister<Register>();
2476 }
2477
David Brazdil0debae72015-11-12 18:37:00 +00002478 IfCondition if_cond;
2479 MipsLabel* non_fallthrough_target;
2480 if (true_target == nullptr) {
2481 if_cond = condition->GetOppositeCondition();
2482 non_fallthrough_target = false_target;
2483 } else {
2484 if_cond = condition->GetCondition();
2485 non_fallthrough_target = true_target;
2486 }
2487
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002488 if (use_imm && rhs_imm == 0) {
2489 switch (if_cond) {
2490 case kCondEQ:
David Brazdil0debae72015-11-12 18:37:00 +00002491 __ Beqz(lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002492 break;
2493 case kCondNE:
David Brazdil0debae72015-11-12 18:37:00 +00002494 __ Bnez(lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002495 break;
2496 case kCondLT:
David Brazdil0debae72015-11-12 18:37:00 +00002497 __ Bltz(lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002498 break;
2499 case kCondGE:
David Brazdil0debae72015-11-12 18:37:00 +00002500 __ Bgez(lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002501 break;
2502 case kCondLE:
David Brazdil0debae72015-11-12 18:37:00 +00002503 __ Blez(lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002504 break;
2505 case kCondGT:
David Brazdil0debae72015-11-12 18:37:00 +00002506 __ Bgtz(lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002507 break;
2508 case kCondB:
2509 break; // always false
2510 case kCondBE:
David Brazdil0debae72015-11-12 18:37:00 +00002511 __ Beqz(lhs, non_fallthrough_target); // <= 0 if zero
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002512 break;
2513 case kCondA:
David Brazdil0debae72015-11-12 18:37:00 +00002514 __ Bnez(lhs, non_fallthrough_target); // > 0 if non-zero
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002515 break;
2516 case kCondAE:
David Brazdil0debae72015-11-12 18:37:00 +00002517 __ B(non_fallthrough_target); // always true
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002518 break;
2519 }
2520 } else {
2521 if (use_imm) {
2522 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2523 rhs_reg = TMP;
2524 __ LoadConst32(rhs_reg, rhs_imm);
2525 }
2526 switch (if_cond) {
2527 case kCondEQ:
David Brazdil0debae72015-11-12 18:37:00 +00002528 __ Beq(lhs, rhs_reg, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002529 break;
2530 case kCondNE:
David Brazdil0debae72015-11-12 18:37:00 +00002531 __ Bne(lhs, rhs_reg, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002532 break;
2533 case kCondLT:
David Brazdil0debae72015-11-12 18:37:00 +00002534 __ Blt(lhs, rhs_reg, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002535 break;
2536 case kCondGE:
David Brazdil0debae72015-11-12 18:37:00 +00002537 __ Bge(lhs, rhs_reg, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002538 break;
2539 case kCondLE:
David Brazdil0debae72015-11-12 18:37:00 +00002540 __ Bge(rhs_reg, lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002541 break;
2542 case kCondGT:
David Brazdil0debae72015-11-12 18:37:00 +00002543 __ Blt(rhs_reg, lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002544 break;
2545 case kCondB:
David Brazdil0debae72015-11-12 18:37:00 +00002546 __ Bltu(lhs, rhs_reg, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002547 break;
2548 case kCondAE:
David Brazdil0debae72015-11-12 18:37:00 +00002549 __ Bgeu(lhs, rhs_reg, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002550 break;
2551 case kCondBE:
David Brazdil0debae72015-11-12 18:37:00 +00002552 __ Bgeu(rhs_reg, lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002553 break;
2554 case kCondA:
David Brazdil0debae72015-11-12 18:37:00 +00002555 __ Bltu(rhs_reg, lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002556 break;
2557 }
2558 }
2559 }
David Brazdil0debae72015-11-12 18:37:00 +00002560
2561 // If neither branch falls through (case 3), the conditional branch to `true_target`
2562 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2563 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002564 __ B(false_target);
2565 }
2566}
2567
2568void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
2569 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002570 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002571 locations->SetInAt(0, Location::RequiresRegister());
2572 }
2573}
2574
2575void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002576 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2577 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2578 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2579 nullptr : codegen_->GetLabelOf(true_successor);
2580 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2581 nullptr : codegen_->GetLabelOf(false_successor);
2582 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002583}
2584
2585void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
2586 LocationSummary* locations = new (GetGraph()->GetArena())
2587 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002588 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002589 locations->SetInAt(0, Location::RequiresRegister());
2590 }
2591}
2592
2593void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
David Brazdil0debae72015-11-12 18:37:00 +00002594 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DeoptimizationSlowPathMIPS(deoptimize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002595 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00002596 GenerateTestAndBranch(deoptimize,
2597 /* condition_input_index */ 0,
2598 slow_path->GetEntryLabel(),
2599 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002600}
2601
2602void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2603 Primitive::Type field_type = field_info.GetFieldType();
2604 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
2605 bool generate_volatile = field_info.IsVolatile() && is_wide;
2606 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2607 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
2608
2609 locations->SetInAt(0, Location::RequiresRegister());
2610 if (generate_volatile) {
2611 InvokeRuntimeCallingConvention calling_convention;
2612 // need A0 to hold base + offset
2613 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2614 if (field_type == Primitive::kPrimLong) {
2615 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
2616 } else {
2617 locations->SetOut(Location::RequiresFpuRegister());
2618 // Need some temp core regs since FP results are returned in core registers
2619 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
2620 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
2621 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
2622 }
2623 } else {
2624 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2625 locations->SetOut(Location::RequiresFpuRegister());
2626 } else {
2627 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2628 }
2629 }
2630}
2631
2632void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
2633 const FieldInfo& field_info,
2634 uint32_t dex_pc) {
2635 Primitive::Type type = field_info.GetFieldType();
2636 LocationSummary* locations = instruction->GetLocations();
2637 Register obj = locations->InAt(0).AsRegister<Register>();
2638 LoadOperandType load_type = kLoadUnsignedByte;
2639 bool is_volatile = field_info.IsVolatile();
2640
2641 switch (type) {
2642 case Primitive::kPrimBoolean:
2643 load_type = kLoadUnsignedByte;
2644 break;
2645 case Primitive::kPrimByte:
2646 load_type = kLoadSignedByte;
2647 break;
2648 case Primitive::kPrimShort:
2649 load_type = kLoadSignedHalfword;
2650 break;
2651 case Primitive::kPrimChar:
2652 load_type = kLoadUnsignedHalfword;
2653 break;
2654 case Primitive::kPrimInt:
2655 case Primitive::kPrimFloat:
2656 case Primitive::kPrimNot:
2657 load_type = kLoadWord;
2658 break;
2659 case Primitive::kPrimLong:
2660 case Primitive::kPrimDouble:
2661 load_type = kLoadDoubleword;
2662 break;
2663 case Primitive::kPrimVoid:
2664 LOG(FATAL) << "Unreachable type " << type;
2665 UNREACHABLE();
2666 }
2667
2668 if (is_volatile && load_type == kLoadDoubleword) {
2669 InvokeRuntimeCallingConvention calling_convention;
2670 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(),
2671 obj, field_info.GetFieldOffset().Uint32Value());
2672 // Do implicit Null check
2673 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
2674 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2675 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
2676 instruction,
2677 dex_pc,
2678 nullptr,
2679 IsDirectEntrypoint(kQuickA64Load));
2680 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
2681 if (type == Primitive::kPrimDouble) {
2682 // Need to move to FP regs since FP results are returned in core registers.
2683 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
2684 locations->Out().AsFpuRegister<FRegister>());
2685 __ Mthc1(locations->GetTemp(2).AsRegister<Register>(),
2686 locations->Out().AsFpuRegister<FRegister>());
2687 }
2688 } else {
2689 if (!Primitive::IsFloatingPointType(type)) {
2690 Register dst;
2691 if (type == Primitive::kPrimLong) {
2692 DCHECK(locations->Out().IsRegisterPair());
2693 dst = locations->Out().AsRegisterPairLow<Register>();
2694 } else {
2695 DCHECK(locations->Out().IsRegister());
2696 dst = locations->Out().AsRegister<Register>();
2697 }
2698 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2699 } else {
2700 DCHECK(locations->Out().IsFpuRegister());
2701 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2702 if (type == Primitive::kPrimFloat) {
2703 __ LoadSFromOffset(dst, obj, field_info.GetFieldOffset().Uint32Value());
2704 } else {
2705 __ LoadDFromOffset(dst, obj, field_info.GetFieldOffset().Uint32Value());
2706 }
2707 }
2708 codegen_->MaybeRecordImplicitNullCheck(instruction);
2709 }
2710
2711 if (is_volatile) {
2712 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2713 }
2714}
2715
2716void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2717 Primitive::Type field_type = field_info.GetFieldType();
2718 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
2719 bool generate_volatile = field_info.IsVolatile() && is_wide;
2720 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2721 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
2722
2723 locations->SetInAt(0, Location::RequiresRegister());
2724 if (generate_volatile) {
2725 InvokeRuntimeCallingConvention calling_convention;
2726 // need A0 to hold base + offset
2727 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2728 if (field_type == Primitive::kPrimLong) {
2729 locations->SetInAt(1, Location::RegisterPairLocation(
2730 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2731 } else {
2732 locations->SetInAt(1, Location::RequiresFpuRegister());
2733 // Pass FP parameters in core registers.
2734 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2735 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
2736 }
2737 } else {
2738 if (Primitive::IsFloatingPointType(field_type)) {
2739 locations->SetInAt(1, Location::RequiresFpuRegister());
2740 } else {
2741 locations->SetInAt(1, Location::RequiresRegister());
2742 }
2743 }
2744}
2745
2746void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
2747 const FieldInfo& field_info,
2748 uint32_t dex_pc) {
2749 Primitive::Type type = field_info.GetFieldType();
2750 LocationSummary* locations = instruction->GetLocations();
2751 Register obj = locations->InAt(0).AsRegister<Register>();
2752 StoreOperandType store_type = kStoreByte;
2753 bool is_volatile = field_info.IsVolatile();
2754
2755 switch (type) {
2756 case Primitive::kPrimBoolean:
2757 case Primitive::kPrimByte:
2758 store_type = kStoreByte;
2759 break;
2760 case Primitive::kPrimShort:
2761 case Primitive::kPrimChar:
2762 store_type = kStoreHalfword;
2763 break;
2764 case Primitive::kPrimInt:
2765 case Primitive::kPrimFloat:
2766 case Primitive::kPrimNot:
2767 store_type = kStoreWord;
2768 break;
2769 case Primitive::kPrimLong:
2770 case Primitive::kPrimDouble:
2771 store_type = kStoreDoubleword;
2772 break;
2773 case Primitive::kPrimVoid:
2774 LOG(FATAL) << "Unreachable type " << type;
2775 UNREACHABLE();
2776 }
2777
2778 if (is_volatile) {
2779 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2780 }
2781
2782 if (is_volatile && store_type == kStoreDoubleword) {
2783 InvokeRuntimeCallingConvention calling_convention;
2784 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(),
2785 obj, field_info.GetFieldOffset().Uint32Value());
2786 // Do implicit Null check.
2787 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
2788 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2789 if (type == Primitive::kPrimDouble) {
2790 // Pass FP parameters in core registers.
2791 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
2792 locations->InAt(1).AsFpuRegister<FRegister>());
2793 __ Mfhc1(locations->GetTemp(2).AsRegister<Register>(),
2794 locations->InAt(1).AsFpuRegister<FRegister>());
2795 }
2796 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
2797 instruction,
2798 dex_pc,
2799 nullptr,
2800 IsDirectEntrypoint(kQuickA64Store));
2801 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
2802 } else {
2803 if (!Primitive::IsFloatingPointType(type)) {
2804 Register src;
2805 if (type == Primitive::kPrimLong) {
2806 DCHECK(locations->InAt(1).IsRegisterPair());
2807 src = locations->InAt(1).AsRegisterPairLow<Register>();
2808 } else {
2809 DCHECK(locations->InAt(1).IsRegister());
2810 src = locations->InAt(1).AsRegister<Register>();
2811 }
2812 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2813 } else {
2814 DCHECK(locations->InAt(1).IsFpuRegister());
2815 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
2816 if (type == Primitive::kPrimFloat) {
2817 __ StoreSToOffset(src, obj, field_info.GetFieldOffset().Uint32Value());
2818 } else {
2819 __ StoreDToOffset(src, obj, field_info.GetFieldOffset().Uint32Value());
2820 }
2821 }
2822 codegen_->MaybeRecordImplicitNullCheck(instruction);
2823 }
2824
2825 // TODO: memory barriers?
2826 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2827 DCHECK(locations->InAt(1).IsRegister());
2828 Register src = locations->InAt(1).AsRegister<Register>();
2829 codegen_->MarkGCCard(obj, src);
2830 }
2831
2832 if (is_volatile) {
2833 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2834 }
2835}
2836
2837void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2838 HandleFieldGet(instruction, instruction->GetFieldInfo());
2839}
2840
2841void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2842 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
2843}
2844
2845void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2846 HandleFieldSet(instruction, instruction->GetFieldInfo());
2847}
2848
2849void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2850 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
2851}
2852
2853void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
2854 LocationSummary::CallKind call_kind =
2855 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2856 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2857 locations->SetInAt(0, Location::RequiresRegister());
2858 locations->SetInAt(1, Location::RequiresRegister());
2859 // The output does overlap inputs.
2860 // Note that TypeCheckSlowPathMIPS uses this register too.
2861 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2862}
2863
2864void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
2865 LocationSummary* locations = instruction->GetLocations();
2866 Register obj = locations->InAt(0).AsRegister<Register>();
2867 Register cls = locations->InAt(1).AsRegister<Register>();
2868 Register out = locations->Out().AsRegister<Register>();
2869
2870 MipsLabel done;
2871
2872 // Return 0 if `obj` is null.
2873 // TODO: Avoid this check if we know `obj` is not null.
2874 __ Move(out, ZERO);
2875 __ Beqz(obj, &done);
2876
2877 // Compare the class of `obj` with `cls`.
2878 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
2879 if (instruction->IsExactCheck()) {
2880 // Classes must be equal for the instanceof to succeed.
2881 __ Xor(out, out, cls);
2882 __ Sltiu(out, out, 1);
2883 } else {
2884 // If the classes are not equal, we go into a slow path.
2885 DCHECK(locations->OnlyCallsOnSlowPath());
2886 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2887 codegen_->AddSlowPath(slow_path);
2888 __ Bne(out, cls, slow_path->GetEntryLabel());
2889 __ LoadConst32(out, 1);
2890 __ Bind(slow_path->GetExitLabel());
2891 }
2892
2893 __ Bind(&done);
2894}
2895
2896void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
2897 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2898 locations->SetOut(Location::ConstantLocation(constant));
2899}
2900
2901void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2902 // Will be generated at use site.
2903}
2904
2905void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
2906 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2907 locations->SetOut(Location::ConstantLocation(constant));
2908}
2909
2910void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2911 // Will be generated at use site.
2912}
2913
2914void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
2915 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
2916 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2917}
2918
2919void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
2920 HandleInvoke(invoke);
2921 // The register T0 is required to be used for the hidden argument in
2922 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2923 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2924}
2925
2926void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
2927 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2928 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
2929 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2930 invoke->GetImtIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
2931 Location receiver = invoke->GetLocations()->InAt(0);
2932 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2933 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
2934
2935 // Set the hidden argument.
2936 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
2937 invoke->GetDexMethodIndex());
2938
2939 // temp = object->GetClass();
2940 if (receiver.IsStackSlot()) {
2941 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
2942 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2943 } else {
2944 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
2945 }
2946 codegen_->MaybeRecordImplicitNullCheck(invoke);
2947 // temp = temp->GetImtEntryAt(method_offset);
2948 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
2949 // T9 = temp->GetEntryPoint();
2950 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
2951 // T9();
2952 __ Jalr(T9);
2953 __ Nop();
2954 DCHECK(!codegen_->IsLeafMethod());
2955 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2956}
2957
2958void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07002959 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
2960 if (intrinsic.TryDispatch(invoke)) {
2961 return;
2962 }
2963
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002964 HandleInvoke(invoke);
2965}
2966
2967void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2968 // When we do not run baseline, explicit clinit checks triggered by static
2969 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2970 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
2971
Chris Larsen701566a2015-10-27 15:29:13 -07002972 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
2973 if (intrinsic.TryDispatch(invoke)) {
2974 return;
2975 }
2976
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002977 HandleInvoke(invoke);
2978}
2979
Chris Larsen701566a2015-10-27 15:29:13 -07002980static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002981 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07002982 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
2983 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002984 return true;
2985 }
2986 return false;
2987}
2988
Vladimir Markodc151b22015-10-15 18:02:30 +01002989HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
2990 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
2991 MethodReference target_method ATTRIBUTE_UNUSED) {
2992 switch (desired_dispatch_info.method_load_kind) {
2993 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2994 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
2995 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
2996 return HInvokeStaticOrDirect::DispatchInfo {
2997 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
2998 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
2999 0u,
3000 0u
3001 };
3002 default:
3003 break;
3004 }
3005 switch (desired_dispatch_info.code_ptr_location) {
3006 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3007 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3008 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3009 return HInvokeStaticOrDirect::DispatchInfo {
3010 desired_dispatch_info.method_load_kind,
3011 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3012 desired_dispatch_info.method_load_data,
3013 0u
3014 };
3015 default:
3016 return desired_dispatch_info;
3017 }
3018}
3019
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003020void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3021 // All registers are assumed to be correctly set up per the calling convention.
3022
3023 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3024 switch (invoke->GetMethodLoadKind()) {
3025 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3026 // temp = thread->string_init_entrypoint
3027 __ LoadFromOffset(kLoadWord,
3028 temp.AsRegister<Register>(),
3029 TR,
3030 invoke->GetStringInitOffset());
3031 break;
3032 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003033 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003034 break;
3035 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3036 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3037 break;
3038 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003039 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003040 // TODO: Implement these types.
3041 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3042 LOG(FATAL) << "Unsupported";
3043 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003044 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003045 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003046 Register reg = temp.AsRegister<Register>();
3047 Register method_reg;
3048 if (current_method.IsRegister()) {
3049 method_reg = current_method.AsRegister<Register>();
3050 } else {
3051 // TODO: use the appropriate DCHECK() here if possible.
3052 // DCHECK(invoke->GetLocations()->Intrinsified());
3053 DCHECK(!current_method.IsValid());
3054 method_reg = reg;
3055 __ Lw(reg, SP, kCurrentMethodStackOffset);
3056 }
3057
3058 // temp = temp->dex_cache_resolved_methods_;
3059 __ LoadFromOffset(kLoadWord,
3060 reg,
3061 method_reg,
3062 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
3063 // temp = temp[index_in_cache]
3064 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3065 __ LoadFromOffset(kLoadWord,
3066 reg,
3067 reg,
3068 CodeGenerator::GetCachePointerOffset(index_in_cache));
3069 break;
3070 }
3071 }
3072
3073 switch (invoke->GetCodePtrLocation()) {
3074 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3075 __ Jalr(&frame_entry_label_, T9);
3076 break;
3077 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3078 // LR = invoke->GetDirectCodePtr();
3079 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3080 // LR()
3081 __ Jalr(T9);
3082 __ Nop();
3083 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003084 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003085 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3086 // TODO: Implement these types.
3087 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3088 LOG(FATAL) << "Unsupported";
3089 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003090 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3091 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01003092 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003093 T9,
3094 callee_method.AsRegister<Register>(),
3095 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3096 kMipsWordSize).Int32Value());
3097 // T9()
3098 __ Jalr(T9);
3099 __ Nop();
3100 break;
3101 }
3102 DCHECK(!IsLeafMethod());
3103}
3104
3105void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3106 // When we do not run baseline, explicit clinit checks triggered by static
3107 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3108 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
3109
3110 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3111 return;
3112 }
3113
3114 LocationSummary* locations = invoke->GetLocations();
3115 codegen_->GenerateStaticOrDirectCall(invoke,
3116 locations->HasTemps()
3117 ? locations->GetTemp(0)
3118 : Location::NoLocation());
3119 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3120}
3121
3122void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003123 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3124 return;
3125 }
3126
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003127 LocationSummary* locations = invoke->GetLocations();
3128 Location receiver = locations->InAt(0);
3129 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3130 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3131 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
3132 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3133 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3134
3135 // temp = object->GetClass();
3136 if (receiver.IsStackSlot()) {
3137 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3138 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3139 } else {
3140 DCHECK(receiver.IsRegister());
3141 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3142 }
3143 codegen_->MaybeRecordImplicitNullCheck(invoke);
3144 // temp = temp->GetMethodAt(method_offset);
3145 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3146 // T9 = temp->GetEntryPoint();
3147 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3148 // T9();
3149 __ Jalr(T9);
3150 __ Nop();
3151 DCHECK(!codegen_->IsLeafMethod());
3152 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3153}
3154
3155void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01003156 InvokeRuntimeCallingConvention calling_convention;
3157 CodeGenerator::CreateLoadClassLocationSummary(
3158 cls,
3159 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
3160 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003161}
3162
3163void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
3164 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01003165 if (cls->NeedsAccessCheck()) {
3166 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3167 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3168 cls,
3169 cls->GetDexPc(),
3170 nullptr,
3171 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00003172 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01003173 return;
3174 }
3175
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003176 Register out = locations->Out().AsRegister<Register>();
3177 Register current_method = locations->InAt(0).AsRegister<Register>();
3178 if (cls->IsReferrersClass()) {
3179 DCHECK(!cls->CanCallRuntime());
3180 DCHECK(!cls->MustGenerateClinitCheck());
3181 __ LoadFromOffset(kLoadWord, out, current_method,
3182 ArtMethod::DeclaringClassOffset().Int32Value());
3183 } else {
3184 DCHECK(cls->CanCallRuntime());
3185 __ LoadFromOffset(kLoadWord, out, current_method,
3186 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
3187 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
3188 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3189 cls,
3190 cls,
3191 cls->GetDexPc(),
3192 cls->MustGenerateClinitCheck());
3193 codegen_->AddSlowPath(slow_path);
3194 __ Beqz(out, slow_path->GetEntryLabel());
3195 if (cls->MustGenerateClinitCheck()) {
3196 GenerateClassInitializationCheck(slow_path, out);
3197 } else {
3198 __ Bind(slow_path->GetExitLabel());
3199 }
3200 }
3201}
3202
3203static int32_t GetExceptionTlsOffset() {
3204 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
3205}
3206
3207void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
3208 LocationSummary* locations =
3209 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3210 locations->SetOut(Location::RequiresRegister());
3211}
3212
3213void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
3214 Register out = load->GetLocations()->Out().AsRegister<Register>();
3215 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
3216}
3217
3218void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
3219 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3220}
3221
3222void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3223 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
3224}
3225
3226void LocationsBuilderMIPS::VisitLoadLocal(HLoadLocal* load) {
3227 load->SetLocations(nullptr);
3228}
3229
3230void InstructionCodeGeneratorMIPS::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3231 // Nothing to do, this is driven by the code generator.
3232}
3233
3234void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
3235 LocationSummary* locations =
3236 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3237 locations->SetInAt(0, Location::RequiresRegister());
3238 locations->SetOut(Location::RequiresRegister());
3239}
3240
3241void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
3242 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
3243 codegen_->AddSlowPath(slow_path);
3244
3245 LocationSummary* locations = load->GetLocations();
3246 Register out = locations->Out().AsRegister<Register>();
3247 Register current_method = locations->InAt(0).AsRegister<Register>();
3248 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
3249 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
3250 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
3251 __ Beqz(out, slow_path->GetEntryLabel());
3252 __ Bind(slow_path->GetExitLabel());
3253}
3254
3255void LocationsBuilderMIPS::VisitLocal(HLocal* local) {
3256 local->SetLocations(nullptr);
3257}
3258
3259void InstructionCodeGeneratorMIPS::VisitLocal(HLocal* local) {
3260 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3261}
3262
3263void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
3264 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3265 locations->SetOut(Location::ConstantLocation(constant));
3266}
3267
3268void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3269 // Will be generated at use site.
3270}
3271
3272void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
3273 LocationSummary* locations =
3274 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3275 InvokeRuntimeCallingConvention calling_convention;
3276 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3277}
3278
3279void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
3280 if (instruction->IsEnter()) {
3281 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
3282 instruction,
3283 instruction->GetDexPc(),
3284 nullptr,
3285 IsDirectEntrypoint(kQuickLockObject));
3286 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3287 } else {
3288 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
3289 instruction,
3290 instruction->GetDexPc(),
3291 nullptr,
3292 IsDirectEntrypoint(kQuickUnlockObject));
3293 }
3294 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3295}
3296
3297void LocationsBuilderMIPS::VisitMul(HMul* mul) {
3298 LocationSummary* locations =
3299 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3300 switch (mul->GetResultType()) {
3301 case Primitive::kPrimInt:
3302 case Primitive::kPrimLong:
3303 locations->SetInAt(0, Location::RequiresRegister());
3304 locations->SetInAt(1, Location::RequiresRegister());
3305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3306 break;
3307
3308 case Primitive::kPrimFloat:
3309 case Primitive::kPrimDouble:
3310 locations->SetInAt(0, Location::RequiresFpuRegister());
3311 locations->SetInAt(1, Location::RequiresFpuRegister());
3312 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3313 break;
3314
3315 default:
3316 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3317 }
3318}
3319
3320void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
3321 Primitive::Type type = instruction->GetType();
3322 LocationSummary* locations = instruction->GetLocations();
3323 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3324
3325 switch (type) {
3326 case Primitive::kPrimInt: {
3327 Register dst = locations->Out().AsRegister<Register>();
3328 Register lhs = locations->InAt(0).AsRegister<Register>();
3329 Register rhs = locations->InAt(1).AsRegister<Register>();
3330
3331 if (isR6) {
3332 __ MulR6(dst, lhs, rhs);
3333 } else {
3334 __ MulR2(dst, lhs, rhs);
3335 }
3336 break;
3337 }
3338 case Primitive::kPrimLong: {
3339 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3340 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
3341 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3342 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3343 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3344 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3345
3346 // Extra checks to protect caused by the existance of A1_A2.
3347 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
3348 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
3349 DCHECK_NE(dst_high, lhs_low);
3350 DCHECK_NE(dst_high, rhs_low);
3351
3352 // A_B * C_D
3353 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
3354 // dst_lo: [ low(B*D) ]
3355 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
3356
3357 if (isR6) {
3358 __ MulR6(TMP, lhs_high, rhs_low);
3359 __ MulR6(dst_high, lhs_low, rhs_high);
3360 __ Addu(dst_high, dst_high, TMP);
3361 __ MuhuR6(TMP, lhs_low, rhs_low);
3362 __ Addu(dst_high, dst_high, TMP);
3363 __ MulR6(dst_low, lhs_low, rhs_low);
3364 } else {
3365 __ MulR2(TMP, lhs_high, rhs_low);
3366 __ MulR2(dst_high, lhs_low, rhs_high);
3367 __ Addu(dst_high, dst_high, TMP);
3368 __ MultuR2(lhs_low, rhs_low);
3369 __ Mfhi(TMP);
3370 __ Addu(dst_high, dst_high, TMP);
3371 __ Mflo(dst_low);
3372 }
3373 break;
3374 }
3375 case Primitive::kPrimFloat:
3376 case Primitive::kPrimDouble: {
3377 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3378 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3379 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3380 if (type == Primitive::kPrimFloat) {
3381 __ MulS(dst, lhs, rhs);
3382 } else {
3383 __ MulD(dst, lhs, rhs);
3384 }
3385 break;
3386 }
3387 default:
3388 LOG(FATAL) << "Unexpected mul type " << type;
3389 }
3390}
3391
3392void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
3393 LocationSummary* locations =
3394 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3395 switch (neg->GetResultType()) {
3396 case Primitive::kPrimInt:
3397 case Primitive::kPrimLong:
3398 locations->SetInAt(0, Location::RequiresRegister());
3399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3400 break;
3401
3402 case Primitive::kPrimFloat:
3403 case Primitive::kPrimDouble:
3404 locations->SetInAt(0, Location::RequiresFpuRegister());
3405 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3406 break;
3407
3408 default:
3409 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3410 }
3411}
3412
3413void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
3414 Primitive::Type type = instruction->GetType();
3415 LocationSummary* locations = instruction->GetLocations();
3416
3417 switch (type) {
3418 case Primitive::kPrimInt: {
3419 Register dst = locations->Out().AsRegister<Register>();
3420 Register src = locations->InAt(0).AsRegister<Register>();
3421 __ Subu(dst, ZERO, src);
3422 break;
3423 }
3424 case Primitive::kPrimLong: {
3425 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3426 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
3427 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3428 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
3429 __ Subu(dst_low, ZERO, src_low);
3430 __ Sltu(TMP, ZERO, dst_low);
3431 __ Subu(dst_high, ZERO, src_high);
3432 __ Subu(dst_high, dst_high, TMP);
3433 break;
3434 }
3435 case Primitive::kPrimFloat:
3436 case Primitive::kPrimDouble: {
3437 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3438 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
3439 if (type == Primitive::kPrimFloat) {
3440 __ NegS(dst, src);
3441 } else {
3442 __ NegD(dst, src);
3443 }
3444 break;
3445 }
3446 default:
3447 LOG(FATAL) << "Unexpected neg type " << type;
3448 }
3449}
3450
3451void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
3452 LocationSummary* locations =
3453 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3454 InvokeRuntimeCallingConvention calling_convention;
3455 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3456 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3457 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3458 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3459}
3460
3461void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
3462 InvokeRuntimeCallingConvention calling_convention;
3463 Register current_method_register = calling_convention.GetRegisterAt(2);
3464 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
3465 // Move an uint16_t value to a register.
3466 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
3467 codegen_->InvokeRuntime(
3468 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
3469 instruction,
3470 instruction->GetDexPc(),
3471 nullptr,
3472 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
3473 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
3474 void*, uint32_t, int32_t, ArtMethod*>();
3475}
3476
3477void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
3478 LocationSummary* locations =
3479 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3480 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003481 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3482 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003483 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3484}
3485
3486void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003487 codegen_->InvokeRuntime(
3488 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
3489 instruction,
3490 instruction->GetDexPc(),
3491 nullptr,
3492 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
3493 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3494}
3495
3496void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
3497 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3498 locations->SetInAt(0, Location::RequiresRegister());
3499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3500}
3501
3502void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
3503 Primitive::Type type = instruction->GetType();
3504 LocationSummary* locations = instruction->GetLocations();
3505
3506 switch (type) {
3507 case Primitive::kPrimInt: {
3508 Register dst = locations->Out().AsRegister<Register>();
3509 Register src = locations->InAt(0).AsRegister<Register>();
3510 __ Nor(dst, src, ZERO);
3511 break;
3512 }
3513
3514 case Primitive::kPrimLong: {
3515 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3516 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
3517 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3518 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
3519 __ Nor(dst_high, src_high, ZERO);
3520 __ Nor(dst_low, src_low, ZERO);
3521 break;
3522 }
3523
3524 default:
3525 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3526 }
3527}
3528
3529void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
3530 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3531 locations->SetInAt(0, Location::RequiresRegister());
3532 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3533}
3534
3535void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
3536 LocationSummary* locations = instruction->GetLocations();
3537 __ Xori(locations->Out().AsRegister<Register>(),
3538 locations->InAt(0).AsRegister<Register>(),
3539 1);
3540}
3541
3542void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
3543 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3544 ? LocationSummary::kCallOnSlowPath
3545 : LocationSummary::kNoCall;
3546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3547 locations->SetInAt(0, Location::RequiresRegister());
3548 if (instruction->HasUses()) {
3549 locations->SetOut(Location::SameAsFirstInput());
3550 }
3551}
3552
3553void InstructionCodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
3554 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3555 return;
3556 }
3557 Location obj = instruction->GetLocations()->InAt(0);
3558
3559 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
3560 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3561}
3562
3563void InstructionCodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
3564 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
3565 codegen_->AddSlowPath(slow_path);
3566
3567 Location obj = instruction->GetLocations()->InAt(0);
3568
3569 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
3570}
3571
3572void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
3573 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
3574 GenerateImplicitNullCheck(instruction);
3575 } else {
3576 GenerateExplicitNullCheck(instruction);
3577 }
3578}
3579
3580void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
3581 HandleBinaryOp(instruction);
3582}
3583
3584void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
3585 HandleBinaryOp(instruction);
3586}
3587
3588void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3589 LOG(FATAL) << "Unreachable";
3590}
3591
3592void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
3593 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3594}
3595
3596void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
3597 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3598 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3599 if (location.IsStackSlot()) {
3600 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3601 } else if (location.IsDoubleStackSlot()) {
3602 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3603 }
3604 locations->SetOut(location);
3605}
3606
3607void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
3608 ATTRIBUTE_UNUSED) {
3609 // Nothing to do, the parameter is already at its location.
3610}
3611
3612void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
3613 LocationSummary* locations =
3614 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3615 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3616}
3617
3618void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
3619 ATTRIBUTE_UNUSED) {
3620 // Nothing to do, the method is already at its location.
3621}
3622
3623void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
3624 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3625 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3626 locations->SetInAt(i, Location::Any());
3627 }
3628 locations->SetOut(Location::Any());
3629}
3630
3631void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3632 LOG(FATAL) << "Unreachable";
3633}
3634
3635void LocationsBuilderMIPS::VisitRem(HRem* rem) {
3636 Primitive::Type type = rem->GetResultType();
3637 LocationSummary::CallKind call_kind =
3638 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCall;
3639 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3640
3641 switch (type) {
3642 case Primitive::kPrimInt:
3643 locations->SetInAt(0, Location::RequiresRegister());
3644 locations->SetInAt(1, Location::RequiresRegister());
3645 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3646 break;
3647
3648 case Primitive::kPrimLong: {
3649 InvokeRuntimeCallingConvention calling_convention;
3650 locations->SetInAt(0, Location::RegisterPairLocation(
3651 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3652 locations->SetInAt(1, Location::RegisterPairLocation(
3653 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3654 locations->SetOut(calling_convention.GetReturnLocation(type));
3655 break;
3656 }
3657
3658 case Primitive::kPrimFloat:
3659 case Primitive::kPrimDouble: {
3660 InvokeRuntimeCallingConvention calling_convention;
3661 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3662 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3663 locations->SetOut(calling_convention.GetReturnLocation(type));
3664 break;
3665 }
3666
3667 default:
3668 LOG(FATAL) << "Unexpected rem type " << type;
3669 }
3670}
3671
3672void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
3673 Primitive::Type type = instruction->GetType();
3674 LocationSummary* locations = instruction->GetLocations();
3675 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3676
3677 switch (type) {
3678 case Primitive::kPrimInt: {
3679 Register dst = locations->Out().AsRegister<Register>();
3680 Register lhs = locations->InAt(0).AsRegister<Register>();
3681 Register rhs = locations->InAt(1).AsRegister<Register>();
3682 if (isR6) {
3683 __ ModR6(dst, lhs, rhs);
3684 } else {
3685 __ ModR2(dst, lhs, rhs);
3686 }
3687 break;
3688 }
3689 case Primitive::kPrimLong: {
3690 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3691 instruction,
3692 instruction->GetDexPc(),
3693 nullptr,
3694 IsDirectEntrypoint(kQuickLmod));
3695 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
3696 break;
3697 }
3698 case Primitive::kPrimFloat: {
3699 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
3700 instruction, instruction->GetDexPc(),
3701 nullptr,
3702 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00003703 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003704 break;
3705 }
3706 case Primitive::kPrimDouble: {
3707 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
3708 instruction, instruction->GetDexPc(),
3709 nullptr,
3710 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00003711 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003712 break;
3713 }
3714 default:
3715 LOG(FATAL) << "Unexpected rem type " << type;
3716 }
3717}
3718
3719void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3720 memory_barrier->SetLocations(nullptr);
3721}
3722
3723void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3724 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3725}
3726
3727void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
3728 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3729 Primitive::Type return_type = ret->InputAt(0)->GetType();
3730 locations->SetInAt(0, MipsReturnLocation(return_type));
3731}
3732
3733void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3734 codegen_->GenerateFrameExit();
3735}
3736
3737void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
3738 ret->SetLocations(nullptr);
3739}
3740
3741void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3742 codegen_->GenerateFrameExit();
3743}
3744
3745void LocationsBuilderMIPS::VisitShl(HShl* shl) {
3746 HandleShift(shl);
3747}
3748
3749void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
3750 HandleShift(shl);
3751}
3752
3753void LocationsBuilderMIPS::VisitShr(HShr* shr) {
3754 HandleShift(shr);
3755}
3756
3757void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
3758 HandleShift(shr);
3759}
3760
3761void LocationsBuilderMIPS::VisitStoreLocal(HStoreLocal* store) {
3762 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3763 Primitive::Type field_type = store->InputAt(1)->GetType();
3764 switch (field_type) {
3765 case Primitive::kPrimNot:
3766 case Primitive::kPrimBoolean:
3767 case Primitive::kPrimByte:
3768 case Primitive::kPrimChar:
3769 case Primitive::kPrimShort:
3770 case Primitive::kPrimInt:
3771 case Primitive::kPrimFloat:
3772 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3773 break;
3774
3775 case Primitive::kPrimLong:
3776 case Primitive::kPrimDouble:
3777 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3778 break;
3779
3780 default:
3781 LOG(FATAL) << "Unimplemented local type " << field_type;
3782 }
3783}
3784
3785void InstructionCodeGeneratorMIPS::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3786}
3787
3788void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
3789 HandleBinaryOp(instruction);
3790}
3791
3792void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
3793 HandleBinaryOp(instruction);
3794}
3795
3796void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3797 HandleFieldGet(instruction, instruction->GetFieldInfo());
3798}
3799
3800void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3801 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3802}
3803
3804void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3805 HandleFieldSet(instruction, instruction->GetFieldInfo());
3806}
3807
3808void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3809 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3810}
3811
3812void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
3813 HUnresolvedInstanceFieldGet* instruction) {
3814 FieldAccessCallingConventionMIPS calling_convention;
3815 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
3816 instruction->GetFieldType(),
3817 calling_convention);
3818}
3819
3820void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
3821 HUnresolvedInstanceFieldGet* instruction) {
3822 FieldAccessCallingConventionMIPS calling_convention;
3823 codegen_->GenerateUnresolvedFieldAccess(instruction,
3824 instruction->GetFieldType(),
3825 instruction->GetFieldIndex(),
3826 instruction->GetDexPc(),
3827 calling_convention);
3828}
3829
3830void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
3831 HUnresolvedInstanceFieldSet* instruction) {
3832 FieldAccessCallingConventionMIPS calling_convention;
3833 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
3834 instruction->GetFieldType(),
3835 calling_convention);
3836}
3837
3838void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
3839 HUnresolvedInstanceFieldSet* instruction) {
3840 FieldAccessCallingConventionMIPS calling_convention;
3841 codegen_->GenerateUnresolvedFieldAccess(instruction,
3842 instruction->GetFieldType(),
3843 instruction->GetFieldIndex(),
3844 instruction->GetDexPc(),
3845 calling_convention);
3846}
3847
3848void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
3849 HUnresolvedStaticFieldGet* instruction) {
3850 FieldAccessCallingConventionMIPS calling_convention;
3851 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
3852 instruction->GetFieldType(),
3853 calling_convention);
3854}
3855
3856void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
3857 HUnresolvedStaticFieldGet* instruction) {
3858 FieldAccessCallingConventionMIPS calling_convention;
3859 codegen_->GenerateUnresolvedFieldAccess(instruction,
3860 instruction->GetFieldType(),
3861 instruction->GetFieldIndex(),
3862 instruction->GetDexPc(),
3863 calling_convention);
3864}
3865
3866void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
3867 HUnresolvedStaticFieldSet* instruction) {
3868 FieldAccessCallingConventionMIPS calling_convention;
3869 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
3870 instruction->GetFieldType(),
3871 calling_convention);
3872}
3873
3874void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
3875 HUnresolvedStaticFieldSet* instruction) {
3876 FieldAccessCallingConventionMIPS calling_convention;
3877 codegen_->GenerateUnresolvedFieldAccess(instruction,
3878 instruction->GetFieldType(),
3879 instruction->GetFieldIndex(),
3880 instruction->GetDexPc(),
3881 calling_convention);
3882}
3883
3884void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
3885 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3886}
3887
3888void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
3889 HBasicBlock* block = instruction->GetBlock();
3890 if (block->GetLoopInformation() != nullptr) {
3891 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3892 // The back edge will generate the suspend check.
3893 return;
3894 }
3895 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3896 // The goto will generate the suspend check.
3897 return;
3898 }
3899 GenerateSuspendCheck(instruction, nullptr);
3900}
3901
3902void LocationsBuilderMIPS::VisitTemporary(HTemporary* temp) {
3903 temp->SetLocations(nullptr);
3904}
3905
3906void InstructionCodeGeneratorMIPS::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
3907 // Nothing to do, this is driven by the code generator.
3908}
3909
3910void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
3911 LocationSummary* locations =
3912 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3913 InvokeRuntimeCallingConvention calling_convention;
3914 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3915}
3916
3917void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
3918 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3919 instruction,
3920 instruction->GetDexPc(),
3921 nullptr,
3922 IsDirectEntrypoint(kQuickDeliverException));
3923 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3924}
3925
3926void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
3927 Primitive::Type input_type = conversion->GetInputType();
3928 Primitive::Type result_type = conversion->GetResultType();
3929 DCHECK_NE(input_type, result_type);
3930
3931 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3932 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3933 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3934 }
3935
3936 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3937 if ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
3938 (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type))) {
3939 call_kind = LocationSummary::kCall;
3940 }
3941
3942 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3943
3944 if (call_kind == LocationSummary::kNoCall) {
3945 if (Primitive::IsFloatingPointType(input_type)) {
3946 locations->SetInAt(0, Location::RequiresFpuRegister());
3947 } else {
3948 locations->SetInAt(0, Location::RequiresRegister());
3949 }
3950
3951 if (Primitive::IsFloatingPointType(result_type)) {
3952 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3953 } else {
3954 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3955 }
3956 } else {
3957 InvokeRuntimeCallingConvention calling_convention;
3958
3959 if (Primitive::IsFloatingPointType(input_type)) {
3960 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3961 } else {
3962 DCHECK_EQ(input_type, Primitive::kPrimLong);
3963 locations->SetInAt(0, Location::RegisterPairLocation(
3964 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3965 }
3966
3967 locations->SetOut(calling_convention.GetReturnLocation(result_type));
3968 }
3969}
3970
3971void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
3972 LocationSummary* locations = conversion->GetLocations();
3973 Primitive::Type result_type = conversion->GetResultType();
3974 Primitive::Type input_type = conversion->GetInputType();
3975 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3976
3977 DCHECK_NE(input_type, result_type);
3978
3979 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
3980 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3981 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
3982 Register src = locations->InAt(0).AsRegister<Register>();
3983
3984 __ Move(dst_low, src);
3985 __ Sra(dst_high, src, 31);
3986 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3987 Register dst = locations->Out().AsRegister<Register>();
3988 Register src = (input_type == Primitive::kPrimLong)
3989 ? locations->InAt(0).AsRegisterPairLow<Register>()
3990 : locations->InAt(0).AsRegister<Register>();
3991
3992 switch (result_type) {
3993 case Primitive::kPrimChar:
3994 __ Andi(dst, src, 0xFFFF);
3995 break;
3996 case Primitive::kPrimByte:
3997 if (has_sign_extension) {
3998 __ Seb(dst, src);
3999 } else {
4000 __ Sll(dst, src, 24);
4001 __ Sra(dst, dst, 24);
4002 }
4003 break;
4004 case Primitive::kPrimShort:
4005 if (has_sign_extension) {
4006 __ Seh(dst, src);
4007 } else {
4008 __ Sll(dst, src, 16);
4009 __ Sra(dst, dst, 16);
4010 }
4011 break;
4012 case Primitive::kPrimInt:
4013 __ Move(dst, src);
4014 break;
4015
4016 default:
4017 LOG(FATAL) << "Unexpected type conversion from " << input_type
4018 << " to " << result_type;
4019 }
4020 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
4021 if (input_type != Primitive::kPrimLong) {
4022 Register src = locations->InAt(0).AsRegister<Register>();
4023 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4024 __ Mtc1(src, FTMP);
4025 if (result_type == Primitive::kPrimFloat) {
4026 __ Cvtsw(dst, FTMP);
4027 } else {
4028 __ Cvtdw(dst, FTMP);
4029 }
4030 } else {
4031 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4032 : QUICK_ENTRY_POINT(pL2d);
4033 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4034 : IsDirectEntrypoint(kQuickL2d);
4035 codegen_->InvokeRuntime(entry_offset,
4036 conversion,
4037 conversion->GetDexPc(),
4038 nullptr,
4039 direct);
4040 if (result_type == Primitive::kPrimFloat) {
4041 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4042 } else {
4043 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4044 }
4045 }
4046 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4047 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4048 int32_t entry_offset;
4049 bool direct;
4050 if (result_type != Primitive::kPrimLong) {
4051 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2iz)
4052 : QUICK_ENTRY_POINT(pD2iz);
4053 direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2iz)
4054 : IsDirectEntrypoint(kQuickD2iz);
4055 } else {
4056 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
4057 : QUICK_ENTRY_POINT(pD2l);
4058 direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
4059 : IsDirectEntrypoint(kQuickD2l);
4060 }
4061 codegen_->InvokeRuntime(entry_offset,
4062 conversion,
4063 conversion->GetDexPc(),
4064 nullptr,
4065 direct);
4066 if (result_type != Primitive::kPrimLong) {
4067 if (input_type == Primitive::kPrimFloat) {
4068 CheckEntrypointTypes<kQuickF2iz, int32_t, float>();
4069 } else {
4070 CheckEntrypointTypes<kQuickD2iz, int32_t, double>();
4071 }
4072 } else {
4073 if (input_type == Primitive::kPrimFloat) {
4074 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4075 } else {
4076 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4077 }
4078 }
4079 } else if (Primitive::IsFloatingPointType(result_type) &&
4080 Primitive::IsFloatingPointType(input_type)) {
4081 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4082 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4083 if (result_type == Primitive::kPrimFloat) {
4084 __ Cvtsd(dst, src);
4085 } else {
4086 __ Cvtds(dst, src);
4087 }
4088 } else {
4089 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4090 << " to " << result_type;
4091 }
4092}
4093
4094void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
4095 HandleShift(ushr);
4096}
4097
4098void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
4099 HandleShift(ushr);
4100}
4101
4102void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
4103 HandleBinaryOp(instruction);
4104}
4105
4106void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
4107 HandleBinaryOp(instruction);
4108}
4109
4110void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4111 // Nothing to do, this should be removed during prepare for register allocator.
4112 LOG(FATAL) << "Unreachable";
4113}
4114
4115void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4116 // Nothing to do, this should be removed during prepare for register allocator.
4117 LOG(FATAL) << "Unreachable";
4118}
4119
4120void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
4121 VisitCondition(comp);
4122}
4123
4124void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
4125 VisitCondition(comp);
4126}
4127
4128void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
4129 VisitCondition(comp);
4130}
4131
4132void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
4133 VisitCondition(comp);
4134}
4135
4136void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
4137 VisitCondition(comp);
4138}
4139
4140void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
4141 VisitCondition(comp);
4142}
4143
4144void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
4145 VisitCondition(comp);
4146}
4147
4148void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
4149 VisitCondition(comp);
4150}
4151
4152void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
4153 VisitCondition(comp);
4154}
4155
4156void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
4157 VisitCondition(comp);
4158}
4159
4160void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
4161 VisitCondition(comp);
4162}
4163
4164void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
4165 VisitCondition(comp);
4166}
4167
4168void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
4169 VisitCondition(comp);
4170}
4171
4172void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
4173 VisitCondition(comp);
4174}
4175
4176void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
4177 VisitCondition(comp);
4178}
4179
4180void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
4181 VisitCondition(comp);
4182}
4183
4184void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
4185 VisitCondition(comp);
4186}
4187
4188void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
4189 VisitCondition(comp);
4190}
4191
4192void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
4193 VisitCondition(comp);
4194}
4195
4196void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
4197 VisitCondition(comp);
4198}
4199
4200void LocationsBuilderMIPS::VisitFakeString(HFakeString* instruction) {
4201 DCHECK(codegen_->IsBaseline());
4202 LocationSummary* locations =
4203 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4204 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4205}
4206
4207void InstructionCodeGeneratorMIPS::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4208 DCHECK(codegen_->IsBaseline());
4209 // Will be generated at use site.
4210}
4211
4212void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4213 LocationSummary* locations =
4214 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4215 locations->SetInAt(0, Location::RequiresRegister());
4216}
4217
4218void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4219 int32_t lower_bound = switch_instr->GetStartValue();
4220 int32_t num_entries = switch_instr->GetNumEntries();
4221 LocationSummary* locations = switch_instr->GetLocations();
4222 Register value_reg = locations->InAt(0).AsRegister<Register>();
4223 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4224
4225 // Create a set of compare/jumps.
4226 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
4227 for (int32_t i = 0; i < num_entries; ++i) {
4228 int32_t case_value = lower_bound + i;
4229 MipsLabel* successor_label = codegen_->GetLabelOf(successors[i]);
4230 if (case_value == 0) {
4231 __ Beqz(value_reg, successor_label);
4232 } else {
4233 __ LoadConst32(TMP, case_value);
4234 __ Beq(value_reg, TMP, successor_label);
4235 }
4236 }
4237
4238 // Insert the default branch for every other value.
4239 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4240 __ B(codegen_->GetLabelOf(default_block));
4241 }
4242}
4243
4244void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4245 // The trampoline uses the same calling convention as dex calling conventions,
4246 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4247 // the method_idx.
4248 HandleInvoke(invoke);
4249}
4250
4251void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4252 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4253}
4254
4255#undef __
4256#undef QUICK_ENTRY_POINT
4257
4258} // namespace mips
4259} // namespace art