blob: 65af9ee2dcc129fecb11d8e54f2db8af2b966a7c [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
Alexey Frunze7e99e052015-11-24 19:28:01 -08002221void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2222 DCHECK(instruction->IsDiv() || instruction->IsRem());
2223 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2224
2225 LocationSummary* locations = instruction->GetLocations();
2226 Location second = locations->InAt(1);
2227 DCHECK(second.IsConstant());
2228
2229 Register out = locations->Out().AsRegister<Register>();
2230 Register dividend = locations->InAt(0).AsRegister<Register>();
2231 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2232 DCHECK(imm == 1 || imm == -1);
2233
2234 if (instruction->IsRem()) {
2235 __ Move(out, ZERO);
2236 } else {
2237 if (imm == -1) {
2238 __ Subu(out, ZERO, dividend);
2239 } else if (out != dividend) {
2240 __ Move(out, dividend);
2241 }
2242 }
2243}
2244
2245void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2246 DCHECK(instruction->IsDiv() || instruction->IsRem());
2247 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2248
2249 LocationSummary* locations = instruction->GetLocations();
2250 Location second = locations->InAt(1);
2251 DCHECK(second.IsConstant());
2252
2253 Register out = locations->Out().AsRegister<Register>();
2254 Register dividend = locations->InAt(0).AsRegister<Register>();
2255 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2256 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
2257 DCHECK(IsPowerOfTwo(abs_imm));
2258 int ctz_imm = CTZ(abs_imm);
2259
2260 if (instruction->IsDiv()) {
2261 if (ctz_imm == 1) {
2262 // Fast path for division by +/-2, which is very common.
2263 __ Srl(TMP, dividend, 31);
2264 } else {
2265 __ Sra(TMP, dividend, 31);
2266 __ Srl(TMP, TMP, 32 - ctz_imm);
2267 }
2268 __ Addu(out, dividend, TMP);
2269 __ Sra(out, out, ctz_imm);
2270 if (imm < 0) {
2271 __ Subu(out, ZERO, out);
2272 }
2273 } else {
2274 if (ctz_imm == 1) {
2275 // Fast path for modulo +/-2, which is very common.
2276 __ Sra(TMP, dividend, 31);
2277 __ Subu(out, dividend, TMP);
2278 __ Andi(out, out, 1);
2279 __ Addu(out, out, TMP);
2280 } else {
2281 __ Sra(TMP, dividend, 31);
2282 __ Srl(TMP, TMP, 32 - ctz_imm);
2283 __ Addu(out, dividend, TMP);
2284 if (IsUint<16>(abs_imm - 1)) {
2285 __ Andi(out, out, abs_imm - 1);
2286 } else {
2287 __ Sll(out, out, 32 - ctz_imm);
2288 __ Srl(out, out, 32 - ctz_imm);
2289 }
2290 __ Subu(out, out, TMP);
2291 }
2292 }
2293}
2294
2295void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2296 DCHECK(instruction->IsDiv() || instruction->IsRem());
2297 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2298
2299 LocationSummary* locations = instruction->GetLocations();
2300 Location second = locations->InAt(1);
2301 DCHECK(second.IsConstant());
2302
2303 Register out = locations->Out().AsRegister<Register>();
2304 Register dividend = locations->InAt(0).AsRegister<Register>();
2305 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2306
2307 int64_t magic;
2308 int shift;
2309 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2310
2311 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2312
2313 __ LoadConst32(TMP, magic);
2314 if (isR6) {
2315 __ MuhR6(TMP, dividend, TMP);
2316 } else {
2317 __ MultR2(dividend, TMP);
2318 __ Mfhi(TMP);
2319 }
2320 if (imm > 0 && magic < 0) {
2321 __ Addu(TMP, TMP, dividend);
2322 } else if (imm < 0 && magic > 0) {
2323 __ Subu(TMP, TMP, dividend);
2324 }
2325
2326 if (shift != 0) {
2327 __ Sra(TMP, TMP, shift);
2328 }
2329
2330 if (instruction->IsDiv()) {
2331 __ Sra(out, TMP, 31);
2332 __ Subu(out, TMP, out);
2333 } else {
2334 __ Sra(AT, TMP, 31);
2335 __ Subu(AT, TMP, AT);
2336 __ LoadConst32(TMP, imm);
2337 if (isR6) {
2338 __ MulR6(TMP, AT, TMP);
2339 } else {
2340 __ MulR2(TMP, AT, TMP);
2341 }
2342 __ Subu(out, dividend, TMP);
2343 }
2344}
2345
2346void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2347 DCHECK(instruction->IsDiv() || instruction->IsRem());
2348 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2349
2350 LocationSummary* locations = instruction->GetLocations();
2351 Register out = locations->Out().AsRegister<Register>();
2352 Location second = locations->InAt(1);
2353
2354 if (second.IsConstant()) {
2355 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2356 if (imm == 0) {
2357 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2358 } else if (imm == 1 || imm == -1) {
2359 DivRemOneOrMinusOne(instruction);
2360 } else if (IsPowerOfTwo(std::abs(imm))) {
2361 DivRemByPowerOfTwo(instruction);
2362 } else {
2363 DCHECK(imm <= -2 || imm >= 2);
2364 GenerateDivRemWithAnyConstant(instruction);
2365 }
2366 } else {
2367 Register dividend = locations->InAt(0).AsRegister<Register>();
2368 Register divisor = second.AsRegister<Register>();
2369 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2370 if (instruction->IsDiv()) {
2371 if (isR6) {
2372 __ DivR6(out, dividend, divisor);
2373 } else {
2374 __ DivR2(out, dividend, divisor);
2375 }
2376 } else {
2377 if (isR6) {
2378 __ ModR6(out, dividend, divisor);
2379 } else {
2380 __ ModR2(out, dividend, divisor);
2381 }
2382 }
2383 }
2384}
2385
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002386void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2387 Primitive::Type type = div->GetResultType();
2388 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
2389 ? LocationSummary::kCall
2390 : LocationSummary::kNoCall;
2391
2392 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2393
2394 switch (type) {
2395 case Primitive::kPrimInt:
2396 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002397 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2399 break;
2400
2401 case Primitive::kPrimLong: {
2402 InvokeRuntimeCallingConvention calling_convention;
2403 locations->SetInAt(0, Location::RegisterPairLocation(
2404 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2405 locations->SetInAt(1, Location::RegisterPairLocation(
2406 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2407 locations->SetOut(calling_convention.GetReturnLocation(type));
2408 break;
2409 }
2410
2411 case Primitive::kPrimFloat:
2412 case Primitive::kPrimDouble:
2413 locations->SetInAt(0, Location::RequiresFpuRegister());
2414 locations->SetInAt(1, Location::RequiresFpuRegister());
2415 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2416 break;
2417
2418 default:
2419 LOG(FATAL) << "Unexpected div type " << type;
2420 }
2421}
2422
2423void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2424 Primitive::Type type = instruction->GetType();
2425 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002426
2427 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002428 case Primitive::kPrimInt:
2429 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002430 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002431 case Primitive::kPrimLong: {
2432 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2433 instruction,
2434 instruction->GetDexPc(),
2435 nullptr,
2436 IsDirectEntrypoint(kQuickLdiv));
2437 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2438 break;
2439 }
2440 case Primitive::kPrimFloat:
2441 case Primitive::kPrimDouble: {
2442 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2443 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2444 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2445 if (type == Primitive::kPrimFloat) {
2446 __ DivS(dst, lhs, rhs);
2447 } else {
2448 __ DivD(dst, lhs, rhs);
2449 }
2450 break;
2451 }
2452 default:
2453 LOG(FATAL) << "Unexpected div type " << type;
2454 }
2455}
2456
2457void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2458 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2459 ? LocationSummary::kCallOnSlowPath
2460 : LocationSummary::kNoCall;
2461 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2462 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2463 if (instruction->HasUses()) {
2464 locations->SetOut(Location::SameAsFirstInput());
2465 }
2466}
2467
2468void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2469 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2470 codegen_->AddSlowPath(slow_path);
2471 Location value = instruction->GetLocations()->InAt(0);
2472 Primitive::Type type = instruction->GetType();
2473
2474 switch (type) {
2475 case Primitive::kPrimByte:
2476 case Primitive::kPrimChar:
2477 case Primitive::kPrimShort:
2478 case Primitive::kPrimInt: {
2479 if (value.IsConstant()) {
2480 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2481 __ B(slow_path->GetEntryLabel());
2482 } else {
2483 // A division by a non-null constant is valid. We don't need to perform
2484 // any check, so simply fall through.
2485 }
2486 } else {
2487 DCHECK(value.IsRegister()) << value;
2488 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2489 }
2490 break;
2491 }
2492 case Primitive::kPrimLong: {
2493 if (value.IsConstant()) {
2494 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2495 __ B(slow_path->GetEntryLabel());
2496 } else {
2497 // A division by a non-null constant is valid. We don't need to perform
2498 // any check, so simply fall through.
2499 }
2500 } else {
2501 DCHECK(value.IsRegisterPair()) << value;
2502 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2503 __ Beqz(TMP, slow_path->GetEntryLabel());
2504 }
2505 break;
2506 }
2507 default:
2508 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2509 }
2510}
2511
2512void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2513 LocationSummary* locations =
2514 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2515 locations->SetOut(Location::ConstantLocation(constant));
2516}
2517
2518void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2519 // Will be generated at use site.
2520}
2521
2522void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2523 exit->SetLocations(nullptr);
2524}
2525
2526void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2527}
2528
2529void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2530 LocationSummary* locations =
2531 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2532 locations->SetOut(Location::ConstantLocation(constant));
2533}
2534
2535void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2536 // Will be generated at use site.
2537}
2538
2539void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2540 got->SetLocations(nullptr);
2541}
2542
2543void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2544 DCHECK(!successor->IsExitBlock());
2545 HBasicBlock* block = got->GetBlock();
2546 HInstruction* previous = got->GetPrevious();
2547 HLoopInformation* info = block->GetLoopInformation();
2548
2549 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2550 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2551 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2552 return;
2553 }
2554 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2555 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2556 }
2557 if (!codegen_->GoesToNextBlock(block, successor)) {
2558 __ B(codegen_->GetLabelOf(successor));
2559 }
2560}
2561
2562void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2563 HandleGoto(got, got->GetSuccessor());
2564}
2565
2566void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2567 try_boundary->SetLocations(nullptr);
2568}
2569
2570void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2571 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2572 if (!successor->IsExitBlock()) {
2573 HandleGoto(try_boundary, successor);
2574 }
2575}
2576
2577void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002578 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002579 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002580 MipsLabel* false_target) {
2581 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002582
David Brazdil0debae72015-11-12 18:37:00 +00002583 if (true_target == nullptr && false_target == nullptr) {
2584 // Nothing to do. The code always falls through.
2585 return;
2586 } else if (cond->IsIntConstant()) {
2587 // Constant condition, statically compared against 1.
2588 if (cond->AsIntConstant()->IsOne()) {
2589 if (true_target != nullptr) {
2590 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002591 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002592 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002593 DCHECK(cond->AsIntConstant()->IsZero());
2594 if (false_target != nullptr) {
2595 __ B(false_target);
2596 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002597 }
David Brazdil0debae72015-11-12 18:37:00 +00002598 return;
2599 }
2600
2601 // The following code generates these patterns:
2602 // (1) true_target == nullptr && false_target != nullptr
2603 // - opposite condition true => branch to false_target
2604 // (2) true_target != nullptr && false_target == nullptr
2605 // - condition true => branch to true_target
2606 // (3) true_target != nullptr && false_target != nullptr
2607 // - condition true => branch to true_target
2608 // - branch to false_target
2609 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002610 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002611 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002612 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002613 if (true_target == nullptr) {
2614 __ Beqz(cond_val.AsRegister<Register>(), false_target);
2615 } else {
2616 __ Bnez(cond_val.AsRegister<Register>(), true_target);
2617 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002618 } else {
2619 // The condition instruction has not been materialized, use its inputs as
2620 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002621 HCondition* condition = cond->AsCondition();
2622
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002623 Register lhs = condition->GetLocations()->InAt(0).AsRegister<Register>();
2624 Location rhs_location = condition->GetLocations()->InAt(1);
2625 Register rhs_reg = ZERO;
2626 int32_t rhs_imm = 0;
2627 bool use_imm = rhs_location.IsConstant();
2628 if (use_imm) {
2629 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2630 } else {
2631 rhs_reg = rhs_location.AsRegister<Register>();
2632 }
2633
David Brazdil0debae72015-11-12 18:37:00 +00002634 IfCondition if_cond;
2635 MipsLabel* non_fallthrough_target;
2636 if (true_target == nullptr) {
2637 if_cond = condition->GetOppositeCondition();
2638 non_fallthrough_target = false_target;
2639 } else {
2640 if_cond = condition->GetCondition();
2641 non_fallthrough_target = true_target;
2642 }
2643
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002644 if (use_imm && rhs_imm == 0) {
2645 switch (if_cond) {
2646 case kCondEQ:
David Brazdil0debae72015-11-12 18:37:00 +00002647 __ Beqz(lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002648 break;
2649 case kCondNE:
David Brazdil0debae72015-11-12 18:37:00 +00002650 __ Bnez(lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002651 break;
2652 case kCondLT:
David Brazdil0debae72015-11-12 18:37:00 +00002653 __ Bltz(lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002654 break;
2655 case kCondGE:
David Brazdil0debae72015-11-12 18:37:00 +00002656 __ Bgez(lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002657 break;
2658 case kCondLE:
David Brazdil0debae72015-11-12 18:37:00 +00002659 __ Blez(lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002660 break;
2661 case kCondGT:
David Brazdil0debae72015-11-12 18:37:00 +00002662 __ Bgtz(lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002663 break;
2664 case kCondB:
2665 break; // always false
2666 case kCondBE:
David Brazdil0debae72015-11-12 18:37:00 +00002667 __ Beqz(lhs, non_fallthrough_target); // <= 0 if zero
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002668 break;
2669 case kCondA:
David Brazdil0debae72015-11-12 18:37:00 +00002670 __ Bnez(lhs, non_fallthrough_target); // > 0 if non-zero
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002671 break;
2672 case kCondAE:
David Brazdil0debae72015-11-12 18:37:00 +00002673 __ B(non_fallthrough_target); // always true
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002674 break;
2675 }
2676 } else {
2677 if (use_imm) {
2678 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2679 rhs_reg = TMP;
2680 __ LoadConst32(rhs_reg, rhs_imm);
2681 }
2682 switch (if_cond) {
2683 case kCondEQ:
David Brazdil0debae72015-11-12 18:37:00 +00002684 __ Beq(lhs, rhs_reg, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002685 break;
2686 case kCondNE:
David Brazdil0debae72015-11-12 18:37:00 +00002687 __ Bne(lhs, rhs_reg, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002688 break;
2689 case kCondLT:
David Brazdil0debae72015-11-12 18:37:00 +00002690 __ Blt(lhs, rhs_reg, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002691 break;
2692 case kCondGE:
David Brazdil0debae72015-11-12 18:37:00 +00002693 __ Bge(lhs, rhs_reg, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002694 break;
2695 case kCondLE:
David Brazdil0debae72015-11-12 18:37:00 +00002696 __ Bge(rhs_reg, lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002697 break;
2698 case kCondGT:
David Brazdil0debae72015-11-12 18:37:00 +00002699 __ Blt(rhs_reg, lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002700 break;
2701 case kCondB:
David Brazdil0debae72015-11-12 18:37:00 +00002702 __ Bltu(lhs, rhs_reg, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002703 break;
2704 case kCondAE:
David Brazdil0debae72015-11-12 18:37:00 +00002705 __ Bgeu(lhs, rhs_reg, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002706 break;
2707 case kCondBE:
David Brazdil0debae72015-11-12 18:37:00 +00002708 __ Bgeu(rhs_reg, lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002709 break;
2710 case kCondA:
David Brazdil0debae72015-11-12 18:37:00 +00002711 __ Bltu(rhs_reg, lhs, non_fallthrough_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002712 break;
2713 }
2714 }
2715 }
David Brazdil0debae72015-11-12 18:37:00 +00002716
2717 // If neither branch falls through (case 3), the conditional branch to `true_target`
2718 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2719 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002720 __ B(false_target);
2721 }
2722}
2723
2724void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
2725 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002726 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002727 locations->SetInAt(0, Location::RequiresRegister());
2728 }
2729}
2730
2731void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002732 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2733 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2734 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2735 nullptr : codegen_->GetLabelOf(true_successor);
2736 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2737 nullptr : codegen_->GetLabelOf(false_successor);
2738 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002739}
2740
2741void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
2742 LocationSummary* locations = new (GetGraph()->GetArena())
2743 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002744 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002745 locations->SetInAt(0, Location::RequiresRegister());
2746 }
2747}
2748
2749void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
David Brazdil0debae72015-11-12 18:37:00 +00002750 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DeoptimizationSlowPathMIPS(deoptimize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002751 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00002752 GenerateTestAndBranch(deoptimize,
2753 /* condition_input_index */ 0,
2754 slow_path->GetEntryLabel(),
2755 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002756}
2757
2758void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2759 Primitive::Type field_type = field_info.GetFieldType();
2760 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
2761 bool generate_volatile = field_info.IsVolatile() && is_wide;
2762 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2763 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
2764
2765 locations->SetInAt(0, Location::RequiresRegister());
2766 if (generate_volatile) {
2767 InvokeRuntimeCallingConvention calling_convention;
2768 // need A0 to hold base + offset
2769 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2770 if (field_type == Primitive::kPrimLong) {
2771 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
2772 } else {
2773 locations->SetOut(Location::RequiresFpuRegister());
2774 // Need some temp core regs since FP results are returned in core registers
2775 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
2776 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
2777 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
2778 }
2779 } else {
2780 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2781 locations->SetOut(Location::RequiresFpuRegister());
2782 } else {
2783 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2784 }
2785 }
2786}
2787
2788void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
2789 const FieldInfo& field_info,
2790 uint32_t dex_pc) {
2791 Primitive::Type type = field_info.GetFieldType();
2792 LocationSummary* locations = instruction->GetLocations();
2793 Register obj = locations->InAt(0).AsRegister<Register>();
2794 LoadOperandType load_type = kLoadUnsignedByte;
2795 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002796 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002797
2798 switch (type) {
2799 case Primitive::kPrimBoolean:
2800 load_type = kLoadUnsignedByte;
2801 break;
2802 case Primitive::kPrimByte:
2803 load_type = kLoadSignedByte;
2804 break;
2805 case Primitive::kPrimShort:
2806 load_type = kLoadSignedHalfword;
2807 break;
2808 case Primitive::kPrimChar:
2809 load_type = kLoadUnsignedHalfword;
2810 break;
2811 case Primitive::kPrimInt:
2812 case Primitive::kPrimFloat:
2813 case Primitive::kPrimNot:
2814 load_type = kLoadWord;
2815 break;
2816 case Primitive::kPrimLong:
2817 case Primitive::kPrimDouble:
2818 load_type = kLoadDoubleword;
2819 break;
2820 case Primitive::kPrimVoid:
2821 LOG(FATAL) << "Unreachable type " << type;
2822 UNREACHABLE();
2823 }
2824
2825 if (is_volatile && load_type == kLoadDoubleword) {
2826 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002827 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002828 // Do implicit Null check
2829 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
2830 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2831 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
2832 instruction,
2833 dex_pc,
2834 nullptr,
2835 IsDirectEntrypoint(kQuickA64Load));
2836 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
2837 if (type == Primitive::kPrimDouble) {
2838 // Need to move to FP regs since FP results are returned in core registers.
2839 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
2840 locations->Out().AsFpuRegister<FRegister>());
2841 __ Mthc1(locations->GetTemp(2).AsRegister<Register>(),
2842 locations->Out().AsFpuRegister<FRegister>());
2843 }
2844 } else {
2845 if (!Primitive::IsFloatingPointType(type)) {
2846 Register dst;
2847 if (type == Primitive::kPrimLong) {
2848 DCHECK(locations->Out().IsRegisterPair());
2849 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002850 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2851 if (obj == dst) {
2852 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
2853 codegen_->MaybeRecordImplicitNullCheck(instruction);
2854 __ LoadFromOffset(kLoadWord, dst, obj, offset);
2855 } else {
2856 __ LoadFromOffset(kLoadWord, dst, obj, offset);
2857 codegen_->MaybeRecordImplicitNullCheck(instruction);
2858 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
2859 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002860 } else {
2861 DCHECK(locations->Out().IsRegister());
2862 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002863 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002864 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002865 } else {
2866 DCHECK(locations->Out().IsFpuRegister());
2867 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2868 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002869 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002870 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002871 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002872 }
2873 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002874 // Longs are handled earlier.
2875 if (type != Primitive::kPrimLong) {
2876 codegen_->MaybeRecordImplicitNullCheck(instruction);
2877 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002878 }
2879
2880 if (is_volatile) {
2881 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2882 }
2883}
2884
2885void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2886 Primitive::Type field_type = field_info.GetFieldType();
2887 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
2888 bool generate_volatile = field_info.IsVolatile() && is_wide;
2889 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2890 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
2891
2892 locations->SetInAt(0, Location::RequiresRegister());
2893 if (generate_volatile) {
2894 InvokeRuntimeCallingConvention calling_convention;
2895 // need A0 to hold base + offset
2896 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2897 if (field_type == Primitive::kPrimLong) {
2898 locations->SetInAt(1, Location::RegisterPairLocation(
2899 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2900 } else {
2901 locations->SetInAt(1, Location::RequiresFpuRegister());
2902 // Pass FP parameters in core registers.
2903 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2904 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
2905 }
2906 } else {
2907 if (Primitive::IsFloatingPointType(field_type)) {
2908 locations->SetInAt(1, Location::RequiresFpuRegister());
2909 } else {
2910 locations->SetInAt(1, Location::RequiresRegister());
2911 }
2912 }
2913}
2914
2915void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
2916 const FieldInfo& field_info,
2917 uint32_t dex_pc) {
2918 Primitive::Type type = field_info.GetFieldType();
2919 LocationSummary* locations = instruction->GetLocations();
2920 Register obj = locations->InAt(0).AsRegister<Register>();
2921 StoreOperandType store_type = kStoreByte;
2922 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002923 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002924
2925 switch (type) {
2926 case Primitive::kPrimBoolean:
2927 case Primitive::kPrimByte:
2928 store_type = kStoreByte;
2929 break;
2930 case Primitive::kPrimShort:
2931 case Primitive::kPrimChar:
2932 store_type = kStoreHalfword;
2933 break;
2934 case Primitive::kPrimInt:
2935 case Primitive::kPrimFloat:
2936 case Primitive::kPrimNot:
2937 store_type = kStoreWord;
2938 break;
2939 case Primitive::kPrimLong:
2940 case Primitive::kPrimDouble:
2941 store_type = kStoreDoubleword;
2942 break;
2943 case Primitive::kPrimVoid:
2944 LOG(FATAL) << "Unreachable type " << type;
2945 UNREACHABLE();
2946 }
2947
2948 if (is_volatile) {
2949 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2950 }
2951
2952 if (is_volatile && store_type == kStoreDoubleword) {
2953 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002954 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002955 // Do implicit Null check.
2956 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
2957 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2958 if (type == Primitive::kPrimDouble) {
2959 // Pass FP parameters in core registers.
2960 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
2961 locations->InAt(1).AsFpuRegister<FRegister>());
2962 __ Mfhc1(locations->GetTemp(2).AsRegister<Register>(),
2963 locations->InAt(1).AsFpuRegister<FRegister>());
2964 }
2965 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
2966 instruction,
2967 dex_pc,
2968 nullptr,
2969 IsDirectEntrypoint(kQuickA64Store));
2970 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
2971 } else {
2972 if (!Primitive::IsFloatingPointType(type)) {
2973 Register src;
2974 if (type == Primitive::kPrimLong) {
2975 DCHECK(locations->InAt(1).IsRegisterPair());
2976 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002977 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2978 __ StoreToOffset(kStoreWord, src, obj, offset);
2979 codegen_->MaybeRecordImplicitNullCheck(instruction);
2980 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002981 } else {
2982 DCHECK(locations->InAt(1).IsRegister());
2983 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002984 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002985 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002986 } else {
2987 DCHECK(locations->InAt(1).IsFpuRegister());
2988 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
2989 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002990 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002991 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002992 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002993 }
2994 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01002995 // Longs are handled earlier.
2996 if (type != Primitive::kPrimLong) {
2997 codegen_->MaybeRecordImplicitNullCheck(instruction);
2998 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002999 }
3000
3001 // TODO: memory barriers?
3002 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3003 DCHECK(locations->InAt(1).IsRegister());
3004 Register src = locations->InAt(1).AsRegister<Register>();
3005 codegen_->MarkGCCard(obj, src);
3006 }
3007
3008 if (is_volatile) {
3009 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3010 }
3011}
3012
3013void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3014 HandleFieldGet(instruction, instruction->GetFieldInfo());
3015}
3016
3017void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3018 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3019}
3020
3021void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3022 HandleFieldSet(instruction, instruction->GetFieldInfo());
3023}
3024
3025void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3026 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3027}
3028
3029void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3030 LocationSummary::CallKind call_kind =
3031 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3032 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3033 locations->SetInAt(0, Location::RequiresRegister());
3034 locations->SetInAt(1, Location::RequiresRegister());
3035 // The output does overlap inputs.
3036 // Note that TypeCheckSlowPathMIPS uses this register too.
3037 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3038}
3039
3040void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3041 LocationSummary* locations = instruction->GetLocations();
3042 Register obj = locations->InAt(0).AsRegister<Register>();
3043 Register cls = locations->InAt(1).AsRegister<Register>();
3044 Register out = locations->Out().AsRegister<Register>();
3045
3046 MipsLabel done;
3047
3048 // Return 0 if `obj` is null.
3049 // TODO: Avoid this check if we know `obj` is not null.
3050 __ Move(out, ZERO);
3051 __ Beqz(obj, &done);
3052
3053 // Compare the class of `obj` with `cls`.
3054 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3055 if (instruction->IsExactCheck()) {
3056 // Classes must be equal for the instanceof to succeed.
3057 __ Xor(out, out, cls);
3058 __ Sltiu(out, out, 1);
3059 } else {
3060 // If the classes are not equal, we go into a slow path.
3061 DCHECK(locations->OnlyCallsOnSlowPath());
3062 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3063 codegen_->AddSlowPath(slow_path);
3064 __ Bne(out, cls, slow_path->GetEntryLabel());
3065 __ LoadConst32(out, 1);
3066 __ Bind(slow_path->GetExitLabel());
3067 }
3068
3069 __ Bind(&done);
3070}
3071
3072void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3073 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3074 locations->SetOut(Location::ConstantLocation(constant));
3075}
3076
3077void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3078 // Will be generated at use site.
3079}
3080
3081void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3082 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3083 locations->SetOut(Location::ConstantLocation(constant));
3084}
3085
3086void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3087 // Will be generated at use site.
3088}
3089
3090void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3091 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3092 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3093}
3094
3095void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3096 HandleInvoke(invoke);
3097 // The register T0 is required to be used for the hidden argument in
3098 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3099 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3100}
3101
3102void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3103 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3104 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3105 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3106 invoke->GetImtIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
3107 Location receiver = invoke->GetLocations()->InAt(0);
3108 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3109 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3110
3111 // Set the hidden argument.
3112 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3113 invoke->GetDexMethodIndex());
3114
3115 // temp = object->GetClass();
3116 if (receiver.IsStackSlot()) {
3117 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3118 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3119 } else {
3120 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3121 }
3122 codegen_->MaybeRecordImplicitNullCheck(invoke);
3123 // temp = temp->GetImtEntryAt(method_offset);
3124 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3125 // T9 = temp->GetEntryPoint();
3126 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3127 // T9();
3128 __ Jalr(T9);
3129 __ Nop();
3130 DCHECK(!codegen_->IsLeafMethod());
3131 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3132}
3133
3134void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003135 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3136 if (intrinsic.TryDispatch(invoke)) {
3137 return;
3138 }
3139
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003140 HandleInvoke(invoke);
3141}
3142
3143void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3144 // When we do not run baseline, explicit clinit checks triggered by static
3145 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3146 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
3147
Chris Larsen701566a2015-10-27 15:29:13 -07003148 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3149 if (intrinsic.TryDispatch(invoke)) {
3150 return;
3151 }
3152
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003153 HandleInvoke(invoke);
3154}
3155
Chris Larsen701566a2015-10-27 15:29:13 -07003156static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003157 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003158 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3159 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003160 return true;
3161 }
3162 return false;
3163}
3164
Vladimir Markodc151b22015-10-15 18:02:30 +01003165HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3166 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3167 MethodReference target_method ATTRIBUTE_UNUSED) {
3168 switch (desired_dispatch_info.method_load_kind) {
3169 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3170 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3171 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3172 return HInvokeStaticOrDirect::DispatchInfo {
3173 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3174 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3175 0u,
3176 0u
3177 };
3178 default:
3179 break;
3180 }
3181 switch (desired_dispatch_info.code_ptr_location) {
3182 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3183 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3184 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3185 return HInvokeStaticOrDirect::DispatchInfo {
3186 desired_dispatch_info.method_load_kind,
3187 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3188 desired_dispatch_info.method_load_data,
3189 0u
3190 };
3191 default:
3192 return desired_dispatch_info;
3193 }
3194}
3195
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003196void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3197 // All registers are assumed to be correctly set up per the calling convention.
3198
3199 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3200 switch (invoke->GetMethodLoadKind()) {
3201 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3202 // temp = thread->string_init_entrypoint
3203 __ LoadFromOffset(kLoadWord,
3204 temp.AsRegister<Register>(),
3205 TR,
3206 invoke->GetStringInitOffset());
3207 break;
3208 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003209 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003210 break;
3211 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3212 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3213 break;
3214 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003215 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003216 // TODO: Implement these types.
3217 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3218 LOG(FATAL) << "Unsupported";
3219 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003220 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003221 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003222 Register reg = temp.AsRegister<Register>();
3223 Register method_reg;
3224 if (current_method.IsRegister()) {
3225 method_reg = current_method.AsRegister<Register>();
3226 } else {
3227 // TODO: use the appropriate DCHECK() here if possible.
3228 // DCHECK(invoke->GetLocations()->Intrinsified());
3229 DCHECK(!current_method.IsValid());
3230 method_reg = reg;
3231 __ Lw(reg, SP, kCurrentMethodStackOffset);
3232 }
3233
3234 // temp = temp->dex_cache_resolved_methods_;
3235 __ LoadFromOffset(kLoadWord,
3236 reg,
3237 method_reg,
3238 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
3239 // temp = temp[index_in_cache]
3240 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3241 __ LoadFromOffset(kLoadWord,
3242 reg,
3243 reg,
3244 CodeGenerator::GetCachePointerOffset(index_in_cache));
3245 break;
3246 }
3247 }
3248
3249 switch (invoke->GetCodePtrLocation()) {
3250 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3251 __ Jalr(&frame_entry_label_, T9);
3252 break;
3253 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3254 // LR = invoke->GetDirectCodePtr();
3255 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3256 // LR()
3257 __ Jalr(T9);
3258 __ Nop();
3259 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003260 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003261 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3262 // TODO: Implement these types.
3263 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3264 LOG(FATAL) << "Unsupported";
3265 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003266 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3267 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01003268 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003269 T9,
3270 callee_method.AsRegister<Register>(),
3271 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3272 kMipsWordSize).Int32Value());
3273 // T9()
3274 __ Jalr(T9);
3275 __ Nop();
3276 break;
3277 }
3278 DCHECK(!IsLeafMethod());
3279}
3280
3281void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3282 // When we do not run baseline, explicit clinit checks triggered by static
3283 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3284 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
3285
3286 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3287 return;
3288 }
3289
3290 LocationSummary* locations = invoke->GetLocations();
3291 codegen_->GenerateStaticOrDirectCall(invoke,
3292 locations->HasTemps()
3293 ? locations->GetTemp(0)
3294 : Location::NoLocation());
3295 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3296}
3297
3298void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003299 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3300 return;
3301 }
3302
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003303 LocationSummary* locations = invoke->GetLocations();
3304 Location receiver = locations->InAt(0);
3305 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3306 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3307 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
3308 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3309 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3310
3311 // temp = object->GetClass();
3312 if (receiver.IsStackSlot()) {
3313 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3314 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3315 } else {
3316 DCHECK(receiver.IsRegister());
3317 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3318 }
3319 codegen_->MaybeRecordImplicitNullCheck(invoke);
3320 // temp = temp->GetMethodAt(method_offset);
3321 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3322 // T9 = temp->GetEntryPoint();
3323 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3324 // T9();
3325 __ Jalr(T9);
3326 __ Nop();
3327 DCHECK(!codegen_->IsLeafMethod());
3328 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3329}
3330
3331void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01003332 InvokeRuntimeCallingConvention calling_convention;
3333 CodeGenerator::CreateLoadClassLocationSummary(
3334 cls,
3335 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
3336 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003337}
3338
3339void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
3340 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01003341 if (cls->NeedsAccessCheck()) {
3342 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3343 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3344 cls,
3345 cls->GetDexPc(),
3346 nullptr,
3347 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00003348 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01003349 return;
3350 }
3351
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003352 Register out = locations->Out().AsRegister<Register>();
3353 Register current_method = locations->InAt(0).AsRegister<Register>();
3354 if (cls->IsReferrersClass()) {
3355 DCHECK(!cls->CanCallRuntime());
3356 DCHECK(!cls->MustGenerateClinitCheck());
3357 __ LoadFromOffset(kLoadWord, out, current_method,
3358 ArtMethod::DeclaringClassOffset().Int32Value());
3359 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003360 __ LoadFromOffset(kLoadWord, out, current_method,
3361 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
3362 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003363
3364 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3365 DCHECK(cls->CanCallRuntime());
3366 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3367 cls,
3368 cls,
3369 cls->GetDexPc(),
3370 cls->MustGenerateClinitCheck());
3371 codegen_->AddSlowPath(slow_path);
3372 if (!cls->IsInDexCache()) {
3373 __ Beqz(out, slow_path->GetEntryLabel());
3374 }
3375 if (cls->MustGenerateClinitCheck()) {
3376 GenerateClassInitializationCheck(slow_path, out);
3377 } else {
3378 __ Bind(slow_path->GetExitLabel());
3379 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003380 }
3381 }
3382}
3383
3384static int32_t GetExceptionTlsOffset() {
3385 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
3386}
3387
3388void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
3389 LocationSummary* locations =
3390 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3391 locations->SetOut(Location::RequiresRegister());
3392}
3393
3394void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
3395 Register out = load->GetLocations()->Out().AsRegister<Register>();
3396 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
3397}
3398
3399void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
3400 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3401}
3402
3403void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3404 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
3405}
3406
3407void LocationsBuilderMIPS::VisitLoadLocal(HLoadLocal* load) {
3408 load->SetLocations(nullptr);
3409}
3410
3411void InstructionCodeGeneratorMIPS::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3412 // Nothing to do, this is driven by the code generator.
3413}
3414
3415void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
3416 LocationSummary* locations =
3417 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3418 locations->SetInAt(0, Location::RequiresRegister());
3419 locations->SetOut(Location::RequiresRegister());
3420}
3421
3422void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
3423 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
3424 codegen_->AddSlowPath(slow_path);
3425
3426 LocationSummary* locations = load->GetLocations();
3427 Register out = locations->Out().AsRegister<Register>();
3428 Register current_method = locations->InAt(0).AsRegister<Register>();
3429 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
3430 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
3431 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
3432 __ Beqz(out, slow_path->GetEntryLabel());
3433 __ Bind(slow_path->GetExitLabel());
3434}
3435
3436void LocationsBuilderMIPS::VisitLocal(HLocal* local) {
3437 local->SetLocations(nullptr);
3438}
3439
3440void InstructionCodeGeneratorMIPS::VisitLocal(HLocal* local) {
3441 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3442}
3443
3444void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
3445 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3446 locations->SetOut(Location::ConstantLocation(constant));
3447}
3448
3449void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3450 // Will be generated at use site.
3451}
3452
3453void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
3454 LocationSummary* locations =
3455 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3456 InvokeRuntimeCallingConvention calling_convention;
3457 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3458}
3459
3460void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
3461 if (instruction->IsEnter()) {
3462 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
3463 instruction,
3464 instruction->GetDexPc(),
3465 nullptr,
3466 IsDirectEntrypoint(kQuickLockObject));
3467 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3468 } else {
3469 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
3470 instruction,
3471 instruction->GetDexPc(),
3472 nullptr,
3473 IsDirectEntrypoint(kQuickUnlockObject));
3474 }
3475 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3476}
3477
3478void LocationsBuilderMIPS::VisitMul(HMul* mul) {
3479 LocationSummary* locations =
3480 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3481 switch (mul->GetResultType()) {
3482 case Primitive::kPrimInt:
3483 case Primitive::kPrimLong:
3484 locations->SetInAt(0, Location::RequiresRegister());
3485 locations->SetInAt(1, Location::RequiresRegister());
3486 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3487 break;
3488
3489 case Primitive::kPrimFloat:
3490 case Primitive::kPrimDouble:
3491 locations->SetInAt(0, Location::RequiresFpuRegister());
3492 locations->SetInAt(1, Location::RequiresFpuRegister());
3493 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3494 break;
3495
3496 default:
3497 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3498 }
3499}
3500
3501void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
3502 Primitive::Type type = instruction->GetType();
3503 LocationSummary* locations = instruction->GetLocations();
3504 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3505
3506 switch (type) {
3507 case Primitive::kPrimInt: {
3508 Register dst = locations->Out().AsRegister<Register>();
3509 Register lhs = locations->InAt(0).AsRegister<Register>();
3510 Register rhs = locations->InAt(1).AsRegister<Register>();
3511
3512 if (isR6) {
3513 __ MulR6(dst, lhs, rhs);
3514 } else {
3515 __ MulR2(dst, lhs, rhs);
3516 }
3517 break;
3518 }
3519 case Primitive::kPrimLong: {
3520 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3521 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
3522 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3523 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3524 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3525 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3526
3527 // Extra checks to protect caused by the existance of A1_A2.
3528 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
3529 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
3530 DCHECK_NE(dst_high, lhs_low);
3531 DCHECK_NE(dst_high, rhs_low);
3532
3533 // A_B * C_D
3534 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
3535 // dst_lo: [ low(B*D) ]
3536 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
3537
3538 if (isR6) {
3539 __ MulR6(TMP, lhs_high, rhs_low);
3540 __ MulR6(dst_high, lhs_low, rhs_high);
3541 __ Addu(dst_high, dst_high, TMP);
3542 __ MuhuR6(TMP, lhs_low, rhs_low);
3543 __ Addu(dst_high, dst_high, TMP);
3544 __ MulR6(dst_low, lhs_low, rhs_low);
3545 } else {
3546 __ MulR2(TMP, lhs_high, rhs_low);
3547 __ MulR2(dst_high, lhs_low, rhs_high);
3548 __ Addu(dst_high, dst_high, TMP);
3549 __ MultuR2(lhs_low, rhs_low);
3550 __ Mfhi(TMP);
3551 __ Addu(dst_high, dst_high, TMP);
3552 __ Mflo(dst_low);
3553 }
3554 break;
3555 }
3556 case Primitive::kPrimFloat:
3557 case Primitive::kPrimDouble: {
3558 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3559 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3560 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3561 if (type == Primitive::kPrimFloat) {
3562 __ MulS(dst, lhs, rhs);
3563 } else {
3564 __ MulD(dst, lhs, rhs);
3565 }
3566 break;
3567 }
3568 default:
3569 LOG(FATAL) << "Unexpected mul type " << type;
3570 }
3571}
3572
3573void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
3574 LocationSummary* locations =
3575 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3576 switch (neg->GetResultType()) {
3577 case Primitive::kPrimInt:
3578 case Primitive::kPrimLong:
3579 locations->SetInAt(0, Location::RequiresRegister());
3580 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3581 break;
3582
3583 case Primitive::kPrimFloat:
3584 case Primitive::kPrimDouble:
3585 locations->SetInAt(0, Location::RequiresFpuRegister());
3586 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3587 break;
3588
3589 default:
3590 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3591 }
3592}
3593
3594void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
3595 Primitive::Type type = instruction->GetType();
3596 LocationSummary* locations = instruction->GetLocations();
3597
3598 switch (type) {
3599 case Primitive::kPrimInt: {
3600 Register dst = locations->Out().AsRegister<Register>();
3601 Register src = locations->InAt(0).AsRegister<Register>();
3602 __ Subu(dst, ZERO, src);
3603 break;
3604 }
3605 case Primitive::kPrimLong: {
3606 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3607 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
3608 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3609 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
3610 __ Subu(dst_low, ZERO, src_low);
3611 __ Sltu(TMP, ZERO, dst_low);
3612 __ Subu(dst_high, ZERO, src_high);
3613 __ Subu(dst_high, dst_high, TMP);
3614 break;
3615 }
3616 case Primitive::kPrimFloat:
3617 case Primitive::kPrimDouble: {
3618 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3619 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
3620 if (type == Primitive::kPrimFloat) {
3621 __ NegS(dst, src);
3622 } else {
3623 __ NegD(dst, src);
3624 }
3625 break;
3626 }
3627 default:
3628 LOG(FATAL) << "Unexpected neg type " << type;
3629 }
3630}
3631
3632void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
3633 LocationSummary* locations =
3634 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3635 InvokeRuntimeCallingConvention calling_convention;
3636 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3637 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3638 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3639 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3640}
3641
3642void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
3643 InvokeRuntimeCallingConvention calling_convention;
3644 Register current_method_register = calling_convention.GetRegisterAt(2);
3645 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
3646 // Move an uint16_t value to a register.
3647 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
3648 codegen_->InvokeRuntime(
3649 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
3650 instruction,
3651 instruction->GetDexPc(),
3652 nullptr,
3653 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
3654 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
3655 void*, uint32_t, int32_t, ArtMethod*>();
3656}
3657
3658void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
3659 LocationSummary* locations =
3660 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3661 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003662 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3663 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003664 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3665}
3666
3667void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003668 codegen_->InvokeRuntime(
3669 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
3670 instruction,
3671 instruction->GetDexPc(),
3672 nullptr,
3673 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
3674 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3675}
3676
3677void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
3678 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3679 locations->SetInAt(0, Location::RequiresRegister());
3680 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3681}
3682
3683void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
3684 Primitive::Type type = instruction->GetType();
3685 LocationSummary* locations = instruction->GetLocations();
3686
3687 switch (type) {
3688 case Primitive::kPrimInt: {
3689 Register dst = locations->Out().AsRegister<Register>();
3690 Register src = locations->InAt(0).AsRegister<Register>();
3691 __ Nor(dst, src, ZERO);
3692 break;
3693 }
3694
3695 case Primitive::kPrimLong: {
3696 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3697 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
3698 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3699 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
3700 __ Nor(dst_high, src_high, ZERO);
3701 __ Nor(dst_low, src_low, ZERO);
3702 break;
3703 }
3704
3705 default:
3706 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3707 }
3708}
3709
3710void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
3711 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3712 locations->SetInAt(0, Location::RequiresRegister());
3713 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3714}
3715
3716void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
3717 LocationSummary* locations = instruction->GetLocations();
3718 __ Xori(locations->Out().AsRegister<Register>(),
3719 locations->InAt(0).AsRegister<Register>(),
3720 1);
3721}
3722
3723void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
3724 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3725 ? LocationSummary::kCallOnSlowPath
3726 : LocationSummary::kNoCall;
3727 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3728 locations->SetInAt(0, Location::RequiresRegister());
3729 if (instruction->HasUses()) {
3730 locations->SetOut(Location::SameAsFirstInput());
3731 }
3732}
3733
3734void InstructionCodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
3735 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3736 return;
3737 }
3738 Location obj = instruction->GetLocations()->InAt(0);
3739
3740 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
3741 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3742}
3743
3744void InstructionCodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
3745 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
3746 codegen_->AddSlowPath(slow_path);
3747
3748 Location obj = instruction->GetLocations()->InAt(0);
3749
3750 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
3751}
3752
3753void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
3754 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
3755 GenerateImplicitNullCheck(instruction);
3756 } else {
3757 GenerateExplicitNullCheck(instruction);
3758 }
3759}
3760
3761void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
3762 HandleBinaryOp(instruction);
3763}
3764
3765void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
3766 HandleBinaryOp(instruction);
3767}
3768
3769void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3770 LOG(FATAL) << "Unreachable";
3771}
3772
3773void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
3774 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3775}
3776
3777void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
3778 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3779 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3780 if (location.IsStackSlot()) {
3781 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3782 } else if (location.IsDoubleStackSlot()) {
3783 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3784 }
3785 locations->SetOut(location);
3786}
3787
3788void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
3789 ATTRIBUTE_UNUSED) {
3790 // Nothing to do, the parameter is already at its location.
3791}
3792
3793void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
3794 LocationSummary* locations =
3795 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3796 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3797}
3798
3799void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
3800 ATTRIBUTE_UNUSED) {
3801 // Nothing to do, the method is already at its location.
3802}
3803
3804void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
3805 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3806 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3807 locations->SetInAt(i, Location::Any());
3808 }
3809 locations->SetOut(Location::Any());
3810}
3811
3812void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3813 LOG(FATAL) << "Unreachable";
3814}
3815
3816void LocationsBuilderMIPS::VisitRem(HRem* rem) {
3817 Primitive::Type type = rem->GetResultType();
3818 LocationSummary::CallKind call_kind =
3819 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCall;
3820 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3821
3822 switch (type) {
3823 case Primitive::kPrimInt:
3824 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003825 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003826 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3827 break;
3828
3829 case Primitive::kPrimLong: {
3830 InvokeRuntimeCallingConvention calling_convention;
3831 locations->SetInAt(0, Location::RegisterPairLocation(
3832 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3833 locations->SetInAt(1, Location::RegisterPairLocation(
3834 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3835 locations->SetOut(calling_convention.GetReturnLocation(type));
3836 break;
3837 }
3838
3839 case Primitive::kPrimFloat:
3840 case Primitive::kPrimDouble: {
3841 InvokeRuntimeCallingConvention calling_convention;
3842 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3843 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3844 locations->SetOut(calling_convention.GetReturnLocation(type));
3845 break;
3846 }
3847
3848 default:
3849 LOG(FATAL) << "Unexpected rem type " << type;
3850 }
3851}
3852
3853void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
3854 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003855
3856 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003857 case Primitive::kPrimInt:
3858 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003859 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003860 case Primitive::kPrimLong: {
3861 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3862 instruction,
3863 instruction->GetDexPc(),
3864 nullptr,
3865 IsDirectEntrypoint(kQuickLmod));
3866 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
3867 break;
3868 }
3869 case Primitive::kPrimFloat: {
3870 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
3871 instruction, instruction->GetDexPc(),
3872 nullptr,
3873 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00003874 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003875 break;
3876 }
3877 case Primitive::kPrimDouble: {
3878 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
3879 instruction, instruction->GetDexPc(),
3880 nullptr,
3881 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00003882 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003883 break;
3884 }
3885 default:
3886 LOG(FATAL) << "Unexpected rem type " << type;
3887 }
3888}
3889
3890void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3891 memory_barrier->SetLocations(nullptr);
3892}
3893
3894void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3895 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3896}
3897
3898void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
3899 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3900 Primitive::Type return_type = ret->InputAt(0)->GetType();
3901 locations->SetInAt(0, MipsReturnLocation(return_type));
3902}
3903
3904void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3905 codegen_->GenerateFrameExit();
3906}
3907
3908void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
3909 ret->SetLocations(nullptr);
3910}
3911
3912void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3913 codegen_->GenerateFrameExit();
3914}
3915
3916void LocationsBuilderMIPS::VisitShl(HShl* shl) {
3917 HandleShift(shl);
3918}
3919
3920void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
3921 HandleShift(shl);
3922}
3923
3924void LocationsBuilderMIPS::VisitShr(HShr* shr) {
3925 HandleShift(shr);
3926}
3927
3928void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
3929 HandleShift(shr);
3930}
3931
3932void LocationsBuilderMIPS::VisitStoreLocal(HStoreLocal* store) {
3933 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3934 Primitive::Type field_type = store->InputAt(1)->GetType();
3935 switch (field_type) {
3936 case Primitive::kPrimNot:
3937 case Primitive::kPrimBoolean:
3938 case Primitive::kPrimByte:
3939 case Primitive::kPrimChar:
3940 case Primitive::kPrimShort:
3941 case Primitive::kPrimInt:
3942 case Primitive::kPrimFloat:
3943 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3944 break;
3945
3946 case Primitive::kPrimLong:
3947 case Primitive::kPrimDouble:
3948 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3949 break;
3950
3951 default:
3952 LOG(FATAL) << "Unimplemented local type " << field_type;
3953 }
3954}
3955
3956void InstructionCodeGeneratorMIPS::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3957}
3958
3959void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
3960 HandleBinaryOp(instruction);
3961}
3962
3963void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
3964 HandleBinaryOp(instruction);
3965}
3966
3967void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3968 HandleFieldGet(instruction, instruction->GetFieldInfo());
3969}
3970
3971void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3972 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3973}
3974
3975void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3976 HandleFieldSet(instruction, instruction->GetFieldInfo());
3977}
3978
3979void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3980 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3981}
3982
3983void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
3984 HUnresolvedInstanceFieldGet* instruction) {
3985 FieldAccessCallingConventionMIPS calling_convention;
3986 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
3987 instruction->GetFieldType(),
3988 calling_convention);
3989}
3990
3991void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
3992 HUnresolvedInstanceFieldGet* instruction) {
3993 FieldAccessCallingConventionMIPS calling_convention;
3994 codegen_->GenerateUnresolvedFieldAccess(instruction,
3995 instruction->GetFieldType(),
3996 instruction->GetFieldIndex(),
3997 instruction->GetDexPc(),
3998 calling_convention);
3999}
4000
4001void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4002 HUnresolvedInstanceFieldSet* instruction) {
4003 FieldAccessCallingConventionMIPS calling_convention;
4004 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4005 instruction->GetFieldType(),
4006 calling_convention);
4007}
4008
4009void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4010 HUnresolvedInstanceFieldSet* instruction) {
4011 FieldAccessCallingConventionMIPS calling_convention;
4012 codegen_->GenerateUnresolvedFieldAccess(instruction,
4013 instruction->GetFieldType(),
4014 instruction->GetFieldIndex(),
4015 instruction->GetDexPc(),
4016 calling_convention);
4017}
4018
4019void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4020 HUnresolvedStaticFieldGet* instruction) {
4021 FieldAccessCallingConventionMIPS calling_convention;
4022 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4023 instruction->GetFieldType(),
4024 calling_convention);
4025}
4026
4027void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4028 HUnresolvedStaticFieldGet* instruction) {
4029 FieldAccessCallingConventionMIPS calling_convention;
4030 codegen_->GenerateUnresolvedFieldAccess(instruction,
4031 instruction->GetFieldType(),
4032 instruction->GetFieldIndex(),
4033 instruction->GetDexPc(),
4034 calling_convention);
4035}
4036
4037void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4038 HUnresolvedStaticFieldSet* instruction) {
4039 FieldAccessCallingConventionMIPS calling_convention;
4040 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4041 instruction->GetFieldType(),
4042 calling_convention);
4043}
4044
4045void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4046 HUnresolvedStaticFieldSet* instruction) {
4047 FieldAccessCallingConventionMIPS calling_convention;
4048 codegen_->GenerateUnresolvedFieldAccess(instruction,
4049 instruction->GetFieldType(),
4050 instruction->GetFieldIndex(),
4051 instruction->GetDexPc(),
4052 calling_convention);
4053}
4054
4055void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4056 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4057}
4058
4059void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4060 HBasicBlock* block = instruction->GetBlock();
4061 if (block->GetLoopInformation() != nullptr) {
4062 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4063 // The back edge will generate the suspend check.
4064 return;
4065 }
4066 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4067 // The goto will generate the suspend check.
4068 return;
4069 }
4070 GenerateSuspendCheck(instruction, nullptr);
4071}
4072
4073void LocationsBuilderMIPS::VisitTemporary(HTemporary* temp) {
4074 temp->SetLocations(nullptr);
4075}
4076
4077void InstructionCodeGeneratorMIPS::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
4078 // Nothing to do, this is driven by the code generator.
4079}
4080
4081void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4082 LocationSummary* locations =
4083 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4084 InvokeRuntimeCallingConvention calling_convention;
4085 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4086}
4087
4088void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4089 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4090 instruction,
4091 instruction->GetDexPc(),
4092 nullptr,
4093 IsDirectEntrypoint(kQuickDeliverException));
4094 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4095}
4096
4097void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4098 Primitive::Type input_type = conversion->GetInputType();
4099 Primitive::Type result_type = conversion->GetResultType();
4100 DCHECK_NE(input_type, result_type);
4101
4102 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4103 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4104 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4105 }
4106
4107 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4108 if ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4109 (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type))) {
4110 call_kind = LocationSummary::kCall;
4111 }
4112
4113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4114
4115 if (call_kind == LocationSummary::kNoCall) {
4116 if (Primitive::IsFloatingPointType(input_type)) {
4117 locations->SetInAt(0, Location::RequiresFpuRegister());
4118 } else {
4119 locations->SetInAt(0, Location::RequiresRegister());
4120 }
4121
4122 if (Primitive::IsFloatingPointType(result_type)) {
4123 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4124 } else {
4125 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4126 }
4127 } else {
4128 InvokeRuntimeCallingConvention calling_convention;
4129
4130 if (Primitive::IsFloatingPointType(input_type)) {
4131 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4132 } else {
4133 DCHECK_EQ(input_type, Primitive::kPrimLong);
4134 locations->SetInAt(0, Location::RegisterPairLocation(
4135 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4136 }
4137
4138 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4139 }
4140}
4141
4142void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4143 LocationSummary* locations = conversion->GetLocations();
4144 Primitive::Type result_type = conversion->GetResultType();
4145 Primitive::Type input_type = conversion->GetInputType();
4146 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
4147
4148 DCHECK_NE(input_type, result_type);
4149
4150 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4151 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4152 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4153 Register src = locations->InAt(0).AsRegister<Register>();
4154
4155 __ Move(dst_low, src);
4156 __ Sra(dst_high, src, 31);
4157 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4158 Register dst = locations->Out().AsRegister<Register>();
4159 Register src = (input_type == Primitive::kPrimLong)
4160 ? locations->InAt(0).AsRegisterPairLow<Register>()
4161 : locations->InAt(0).AsRegister<Register>();
4162
4163 switch (result_type) {
4164 case Primitive::kPrimChar:
4165 __ Andi(dst, src, 0xFFFF);
4166 break;
4167 case Primitive::kPrimByte:
4168 if (has_sign_extension) {
4169 __ Seb(dst, src);
4170 } else {
4171 __ Sll(dst, src, 24);
4172 __ Sra(dst, dst, 24);
4173 }
4174 break;
4175 case Primitive::kPrimShort:
4176 if (has_sign_extension) {
4177 __ Seh(dst, src);
4178 } else {
4179 __ Sll(dst, src, 16);
4180 __ Sra(dst, dst, 16);
4181 }
4182 break;
4183 case Primitive::kPrimInt:
4184 __ Move(dst, src);
4185 break;
4186
4187 default:
4188 LOG(FATAL) << "Unexpected type conversion from " << input_type
4189 << " to " << result_type;
4190 }
4191 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
4192 if (input_type != Primitive::kPrimLong) {
4193 Register src = locations->InAt(0).AsRegister<Register>();
4194 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4195 __ Mtc1(src, FTMP);
4196 if (result_type == Primitive::kPrimFloat) {
4197 __ Cvtsw(dst, FTMP);
4198 } else {
4199 __ Cvtdw(dst, FTMP);
4200 }
4201 } else {
4202 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4203 : QUICK_ENTRY_POINT(pL2d);
4204 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4205 : IsDirectEntrypoint(kQuickL2d);
4206 codegen_->InvokeRuntime(entry_offset,
4207 conversion,
4208 conversion->GetDexPc(),
4209 nullptr,
4210 direct);
4211 if (result_type == Primitive::kPrimFloat) {
4212 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4213 } else {
4214 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4215 }
4216 }
4217 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4218 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4219 int32_t entry_offset;
4220 bool direct;
4221 if (result_type != Primitive::kPrimLong) {
4222 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2iz)
4223 : QUICK_ENTRY_POINT(pD2iz);
4224 direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2iz)
4225 : IsDirectEntrypoint(kQuickD2iz);
4226 } else {
4227 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
4228 : QUICK_ENTRY_POINT(pD2l);
4229 direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
4230 : IsDirectEntrypoint(kQuickD2l);
4231 }
4232 codegen_->InvokeRuntime(entry_offset,
4233 conversion,
4234 conversion->GetDexPc(),
4235 nullptr,
4236 direct);
4237 if (result_type != Primitive::kPrimLong) {
4238 if (input_type == Primitive::kPrimFloat) {
4239 CheckEntrypointTypes<kQuickF2iz, int32_t, float>();
4240 } else {
4241 CheckEntrypointTypes<kQuickD2iz, int32_t, double>();
4242 }
4243 } else {
4244 if (input_type == Primitive::kPrimFloat) {
4245 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4246 } else {
4247 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4248 }
4249 }
4250 } else if (Primitive::IsFloatingPointType(result_type) &&
4251 Primitive::IsFloatingPointType(input_type)) {
4252 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4253 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4254 if (result_type == Primitive::kPrimFloat) {
4255 __ Cvtsd(dst, src);
4256 } else {
4257 __ Cvtds(dst, src);
4258 }
4259 } else {
4260 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4261 << " to " << result_type;
4262 }
4263}
4264
4265void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
4266 HandleShift(ushr);
4267}
4268
4269void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
4270 HandleShift(ushr);
4271}
4272
4273void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
4274 HandleBinaryOp(instruction);
4275}
4276
4277void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
4278 HandleBinaryOp(instruction);
4279}
4280
4281void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4282 // Nothing to do, this should be removed during prepare for register allocator.
4283 LOG(FATAL) << "Unreachable";
4284}
4285
4286void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4287 // Nothing to do, this should be removed during prepare for register allocator.
4288 LOG(FATAL) << "Unreachable";
4289}
4290
4291void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
4292 VisitCondition(comp);
4293}
4294
4295void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
4296 VisitCondition(comp);
4297}
4298
4299void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
4300 VisitCondition(comp);
4301}
4302
4303void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
4304 VisitCondition(comp);
4305}
4306
4307void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
4308 VisitCondition(comp);
4309}
4310
4311void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
4312 VisitCondition(comp);
4313}
4314
4315void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
4316 VisitCondition(comp);
4317}
4318
4319void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
4320 VisitCondition(comp);
4321}
4322
4323void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
4324 VisitCondition(comp);
4325}
4326
4327void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
4328 VisitCondition(comp);
4329}
4330
4331void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
4332 VisitCondition(comp);
4333}
4334
4335void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
4336 VisitCondition(comp);
4337}
4338
4339void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
4340 VisitCondition(comp);
4341}
4342
4343void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
4344 VisitCondition(comp);
4345}
4346
4347void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
4348 VisitCondition(comp);
4349}
4350
4351void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
4352 VisitCondition(comp);
4353}
4354
4355void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
4356 VisitCondition(comp);
4357}
4358
4359void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
4360 VisitCondition(comp);
4361}
4362
4363void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
4364 VisitCondition(comp);
4365}
4366
4367void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
4368 VisitCondition(comp);
4369}
4370
4371void LocationsBuilderMIPS::VisitFakeString(HFakeString* instruction) {
4372 DCHECK(codegen_->IsBaseline());
4373 LocationSummary* locations =
4374 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4375 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4376}
4377
4378void InstructionCodeGeneratorMIPS::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4379 DCHECK(codegen_->IsBaseline());
4380 // Will be generated at use site.
4381}
4382
4383void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4384 LocationSummary* locations =
4385 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4386 locations->SetInAt(0, Location::RequiresRegister());
4387}
4388
4389void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4390 int32_t lower_bound = switch_instr->GetStartValue();
4391 int32_t num_entries = switch_instr->GetNumEntries();
4392 LocationSummary* locations = switch_instr->GetLocations();
4393 Register value_reg = locations->InAt(0).AsRegister<Register>();
4394 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4395
4396 // Create a set of compare/jumps.
4397 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
4398 for (int32_t i = 0; i < num_entries; ++i) {
4399 int32_t case_value = lower_bound + i;
4400 MipsLabel* successor_label = codegen_->GetLabelOf(successors[i]);
4401 if (case_value == 0) {
4402 __ Beqz(value_reg, successor_label);
4403 } else {
4404 __ LoadConst32(TMP, case_value);
4405 __ Beq(value_reg, TMP, successor_label);
4406 }
4407 }
4408
4409 // Insert the default branch for every other value.
4410 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4411 __ B(codegen_->GetLabelOf(default_block));
4412 }
4413}
4414
4415void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4416 // The trampoline uses the same calling convention as dex calling conventions,
4417 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4418 // the method_idx.
4419 HandleInvoke(invoke);
4420}
4421
4422void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4423 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4424}
4425
4426#undef __
4427#undef QUICK_ENTRY_POINT
4428
4429} // namespace mips
4430} // namespace art