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