blob: fa119bbeb1034cc511cad1b9721acfbe5f48dca9 [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
979void CodeGeneratorMIPS::Move(HInstruction* instruction,
980 Location location,
981 HInstruction* move_for) {
982 LocationSummary* locations = instruction->GetLocations();
983 Primitive::Type type = instruction->GetType();
984 DCHECK_NE(type, Primitive::kPrimVoid);
985
986 if (instruction->IsCurrentMethod()) {
987 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
988 } else if (locations != nullptr && locations->Out().Equals(location)) {
989 return;
990 } else if (instruction->IsIntConstant()
991 || instruction->IsLongConstant()
992 || instruction->IsNullConstant()) {
993 MoveConstant(location, instruction->AsConstant());
994 } else if (instruction->IsTemporary()) {
995 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
996 if (temp_location.IsStackSlot()) {
997 Move32(location, temp_location);
998 } else {
999 DCHECK(temp_location.IsDoubleStackSlot());
1000 Move64(location, temp_location);
1001 }
1002 } else if (instruction->IsLoadLocal()) {
1003 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
1004 if (Primitive::Is64BitType(type)) {
1005 Move64(location, Location::DoubleStackSlot(stack_slot));
1006 } else {
1007 Move32(location, Location::StackSlot(stack_slot));
1008 }
1009 } else {
1010 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
1011 if (Primitive::Is64BitType(type)) {
1012 Move64(location, locations->Out());
1013 } else {
1014 Move32(location, locations->Out());
1015 }
1016 }
1017}
1018
1019void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1020 if (location.IsRegister()) {
1021 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001022 } else if (location.IsRegisterPair()) {
1023 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1024 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001025 } else {
1026 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1027 }
1028}
1029
1030Location CodeGeneratorMIPS::GetStackLocation(HLoadLocal* load) const {
1031 Primitive::Type type = load->GetType();
1032
1033 switch (type) {
1034 case Primitive::kPrimNot:
1035 case Primitive::kPrimInt:
1036 case Primitive::kPrimFloat:
1037 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1038
1039 case Primitive::kPrimLong:
1040 case Primitive::kPrimDouble:
1041 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1042
1043 case Primitive::kPrimBoolean:
1044 case Primitive::kPrimByte:
1045 case Primitive::kPrimChar:
1046 case Primitive::kPrimShort:
1047 case Primitive::kPrimVoid:
1048 LOG(FATAL) << "Unexpected type " << type;
1049 }
1050
1051 LOG(FATAL) << "Unreachable";
1052 return Location::NoLocation();
1053}
1054
1055void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1056 MipsLabel done;
1057 Register card = AT;
1058 Register temp = TMP;
1059 __ Beqz(value, &done);
1060 __ LoadFromOffset(kLoadWord,
1061 card,
1062 TR,
1063 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
1064 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1065 __ Addu(temp, card, temp);
1066 __ Sb(card, temp, 0);
1067 __ Bind(&done);
1068}
1069
David Brazdil58282f42016-01-14 12:45:10 +00001070void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001071 // Don't allocate the dalvik style register pair passing.
1072 blocked_register_pairs_[A1_A2] = true;
1073
1074 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1075 blocked_core_registers_[ZERO] = true;
1076 blocked_core_registers_[K0] = true;
1077 blocked_core_registers_[K1] = true;
1078 blocked_core_registers_[GP] = true;
1079 blocked_core_registers_[SP] = true;
1080 blocked_core_registers_[RA] = true;
1081
1082 // AT and TMP(T8) are used as temporary/scratch registers
1083 // (similar to how AT is used by MIPS assemblers).
1084 blocked_core_registers_[AT] = true;
1085 blocked_core_registers_[TMP] = true;
1086 blocked_fpu_registers_[FTMP] = true;
1087
1088 // Reserve suspend and thread registers.
1089 blocked_core_registers_[S0] = true;
1090 blocked_core_registers_[TR] = true;
1091
1092 // Reserve T9 for function calls
1093 blocked_core_registers_[T9] = true;
1094
1095 // Reserve odd-numbered FPU registers.
1096 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1097 blocked_fpu_registers_[i] = true;
1098 }
1099
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001100 UpdateBlockedPairRegisters();
1101}
1102
1103void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1104 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1105 MipsManagedRegister current =
1106 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1107 if (blocked_core_registers_[current.AsRegisterPairLow()]
1108 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1109 blocked_register_pairs_[i] = true;
1110 }
1111 }
1112}
1113
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001114size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1115 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1116 return kMipsWordSize;
1117}
1118
1119size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1120 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1121 return kMipsWordSize;
1122}
1123
1124size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1125 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1126 return kMipsDoublewordSize;
1127}
1128
1129size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1130 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1131 return kMipsDoublewordSize;
1132}
1133
1134void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
1135 stream << MipsManagedRegister::FromCoreRegister(Register(reg));
1136}
1137
1138void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1139 stream << MipsManagedRegister::FromFRegister(FRegister(reg));
1140}
1141
1142void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1143 HInstruction* instruction,
1144 uint32_t dex_pc,
1145 SlowPathCode* slow_path) {
1146 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1147 instruction,
1148 dex_pc,
1149 slow_path,
1150 IsDirectEntrypoint(entrypoint));
1151}
1152
1153constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1154
1155void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1156 HInstruction* instruction,
1157 uint32_t dex_pc,
1158 SlowPathCode* slow_path,
1159 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001160 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1161 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001162 if (is_direct_entrypoint) {
1163 // Reserve argument space on stack (for $a0-$a3) for
1164 // entrypoints that directly reference native implementations.
1165 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001166 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001167 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001168 } else {
1169 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001170 }
1171 RecordPcInfo(instruction, dex_pc, slow_path);
1172}
1173
1174void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1175 Register class_reg) {
1176 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1177 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1178 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1179 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1180 __ Sync(0);
1181 __ Bind(slow_path->GetExitLabel());
1182}
1183
1184void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1185 __ Sync(0); // Only stype 0 is supported.
1186}
1187
1188void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1189 HBasicBlock* successor) {
1190 SuspendCheckSlowPathMIPS* slow_path =
1191 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1192 codegen_->AddSlowPath(slow_path);
1193
1194 __ LoadFromOffset(kLoadUnsignedHalfword,
1195 TMP,
1196 TR,
1197 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1198 if (successor == nullptr) {
1199 __ Bnez(TMP, slow_path->GetEntryLabel());
1200 __ Bind(slow_path->GetReturnLabel());
1201 } else {
1202 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1203 __ B(slow_path->GetEntryLabel());
1204 // slow_path will return to GetLabelOf(successor).
1205 }
1206}
1207
1208InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1209 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001210 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001211 assembler_(codegen->GetAssembler()),
1212 codegen_(codegen) {}
1213
1214void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1215 DCHECK_EQ(instruction->InputCount(), 2U);
1216 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1217 Primitive::Type type = instruction->GetResultType();
1218 switch (type) {
1219 case Primitive::kPrimInt: {
1220 locations->SetInAt(0, Location::RequiresRegister());
1221 HInstruction* right = instruction->InputAt(1);
1222 bool can_use_imm = false;
1223 if (right->IsConstant()) {
1224 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1225 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1226 can_use_imm = IsUint<16>(imm);
1227 } else if (instruction->IsAdd()) {
1228 can_use_imm = IsInt<16>(imm);
1229 } else {
1230 DCHECK(instruction->IsSub());
1231 can_use_imm = IsInt<16>(-imm);
1232 }
1233 }
1234 if (can_use_imm)
1235 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1236 else
1237 locations->SetInAt(1, Location::RequiresRegister());
1238 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1239 break;
1240 }
1241
1242 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001243 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001244 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1245 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001246 break;
1247 }
1248
1249 case Primitive::kPrimFloat:
1250 case Primitive::kPrimDouble:
1251 DCHECK(instruction->IsAdd() || instruction->IsSub());
1252 locations->SetInAt(0, Location::RequiresFpuRegister());
1253 locations->SetInAt(1, Location::RequiresFpuRegister());
1254 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1255 break;
1256
1257 default:
1258 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1259 }
1260}
1261
1262void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1263 Primitive::Type type = instruction->GetType();
1264 LocationSummary* locations = instruction->GetLocations();
1265
1266 switch (type) {
1267 case Primitive::kPrimInt: {
1268 Register dst = locations->Out().AsRegister<Register>();
1269 Register lhs = locations->InAt(0).AsRegister<Register>();
1270 Location rhs_location = locations->InAt(1);
1271
1272 Register rhs_reg = ZERO;
1273 int32_t rhs_imm = 0;
1274 bool use_imm = rhs_location.IsConstant();
1275 if (use_imm) {
1276 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1277 } else {
1278 rhs_reg = rhs_location.AsRegister<Register>();
1279 }
1280
1281 if (instruction->IsAnd()) {
1282 if (use_imm)
1283 __ Andi(dst, lhs, rhs_imm);
1284 else
1285 __ And(dst, lhs, rhs_reg);
1286 } else if (instruction->IsOr()) {
1287 if (use_imm)
1288 __ Ori(dst, lhs, rhs_imm);
1289 else
1290 __ Or(dst, lhs, rhs_reg);
1291 } else if (instruction->IsXor()) {
1292 if (use_imm)
1293 __ Xori(dst, lhs, rhs_imm);
1294 else
1295 __ Xor(dst, lhs, rhs_reg);
1296 } else if (instruction->IsAdd()) {
1297 if (use_imm)
1298 __ Addiu(dst, lhs, rhs_imm);
1299 else
1300 __ Addu(dst, lhs, rhs_reg);
1301 } else {
1302 DCHECK(instruction->IsSub());
1303 if (use_imm)
1304 __ Addiu(dst, lhs, -rhs_imm);
1305 else
1306 __ Subu(dst, lhs, rhs_reg);
1307 }
1308 break;
1309 }
1310
1311 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001312 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1313 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1314 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1315 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001316 Location rhs_location = locations->InAt(1);
1317 bool use_imm = rhs_location.IsConstant();
1318 if (!use_imm) {
1319 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1320 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1321 if (instruction->IsAnd()) {
1322 __ And(dst_low, lhs_low, rhs_low);
1323 __ And(dst_high, lhs_high, rhs_high);
1324 } else if (instruction->IsOr()) {
1325 __ Or(dst_low, lhs_low, rhs_low);
1326 __ Or(dst_high, lhs_high, rhs_high);
1327 } else if (instruction->IsXor()) {
1328 __ Xor(dst_low, lhs_low, rhs_low);
1329 __ Xor(dst_high, lhs_high, rhs_high);
1330 } else if (instruction->IsAdd()) {
1331 if (lhs_low == rhs_low) {
1332 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1333 __ Slt(TMP, lhs_low, ZERO);
1334 __ Addu(dst_low, lhs_low, rhs_low);
1335 } else {
1336 __ Addu(dst_low, lhs_low, rhs_low);
1337 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1338 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1339 }
1340 __ Addu(dst_high, lhs_high, rhs_high);
1341 __ Addu(dst_high, dst_high, TMP);
1342 } else {
1343 DCHECK(instruction->IsSub());
1344 __ Sltu(TMP, lhs_low, rhs_low);
1345 __ Subu(dst_low, lhs_low, rhs_low);
1346 __ Subu(dst_high, lhs_high, rhs_high);
1347 __ Subu(dst_high, dst_high, TMP);
1348 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001349 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001350 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1351 if (instruction->IsOr()) {
1352 uint32_t low = Low32Bits(value);
1353 uint32_t high = High32Bits(value);
1354 if (IsUint<16>(low)) {
1355 if (dst_low != lhs_low || low != 0) {
1356 __ Ori(dst_low, lhs_low, low);
1357 }
1358 } else {
1359 __ LoadConst32(TMP, low);
1360 __ Or(dst_low, lhs_low, TMP);
1361 }
1362 if (IsUint<16>(high)) {
1363 if (dst_high != lhs_high || high != 0) {
1364 __ Ori(dst_high, lhs_high, high);
1365 }
1366 } else {
1367 if (high != low) {
1368 __ LoadConst32(TMP, high);
1369 }
1370 __ Or(dst_high, lhs_high, TMP);
1371 }
1372 } else if (instruction->IsXor()) {
1373 uint32_t low = Low32Bits(value);
1374 uint32_t high = High32Bits(value);
1375 if (IsUint<16>(low)) {
1376 if (dst_low != lhs_low || low != 0) {
1377 __ Xori(dst_low, lhs_low, low);
1378 }
1379 } else {
1380 __ LoadConst32(TMP, low);
1381 __ Xor(dst_low, lhs_low, TMP);
1382 }
1383 if (IsUint<16>(high)) {
1384 if (dst_high != lhs_high || high != 0) {
1385 __ Xori(dst_high, lhs_high, high);
1386 }
1387 } else {
1388 if (high != low) {
1389 __ LoadConst32(TMP, high);
1390 }
1391 __ Xor(dst_high, lhs_high, TMP);
1392 }
1393 } else if (instruction->IsAnd()) {
1394 uint32_t low = Low32Bits(value);
1395 uint32_t high = High32Bits(value);
1396 if (IsUint<16>(low)) {
1397 __ Andi(dst_low, lhs_low, low);
1398 } else if (low != 0xFFFFFFFF) {
1399 __ LoadConst32(TMP, low);
1400 __ And(dst_low, lhs_low, TMP);
1401 } else if (dst_low != lhs_low) {
1402 __ Move(dst_low, lhs_low);
1403 }
1404 if (IsUint<16>(high)) {
1405 __ Andi(dst_high, lhs_high, high);
1406 } else if (high != 0xFFFFFFFF) {
1407 if (high != low) {
1408 __ LoadConst32(TMP, high);
1409 }
1410 __ And(dst_high, lhs_high, TMP);
1411 } else if (dst_high != lhs_high) {
1412 __ Move(dst_high, lhs_high);
1413 }
1414 } else {
1415 if (instruction->IsSub()) {
1416 value = -value;
1417 } else {
1418 DCHECK(instruction->IsAdd());
1419 }
1420 int32_t low = Low32Bits(value);
1421 int32_t high = High32Bits(value);
1422 if (IsInt<16>(low)) {
1423 if (dst_low != lhs_low || low != 0) {
1424 __ Addiu(dst_low, lhs_low, low);
1425 }
1426 if (low != 0) {
1427 __ Sltiu(AT, dst_low, low);
1428 }
1429 } else {
1430 __ LoadConst32(TMP, low);
1431 __ Addu(dst_low, lhs_low, TMP);
1432 __ Sltu(AT, dst_low, TMP);
1433 }
1434 if (IsInt<16>(high)) {
1435 if (dst_high != lhs_high || high != 0) {
1436 __ Addiu(dst_high, lhs_high, high);
1437 }
1438 } else {
1439 if (high != low) {
1440 __ LoadConst32(TMP, high);
1441 }
1442 __ Addu(dst_high, lhs_high, TMP);
1443 }
1444 if (low != 0) {
1445 __ Addu(dst_high, dst_high, AT);
1446 }
1447 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001448 }
1449 break;
1450 }
1451
1452 case Primitive::kPrimFloat:
1453 case Primitive::kPrimDouble: {
1454 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1455 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1456 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1457 if (instruction->IsAdd()) {
1458 if (type == Primitive::kPrimFloat) {
1459 __ AddS(dst, lhs, rhs);
1460 } else {
1461 __ AddD(dst, lhs, rhs);
1462 }
1463 } else {
1464 DCHECK(instruction->IsSub());
1465 if (type == Primitive::kPrimFloat) {
1466 __ SubS(dst, lhs, rhs);
1467 } else {
1468 __ SubD(dst, lhs, rhs);
1469 }
1470 }
1471 break;
1472 }
1473
1474 default:
1475 LOG(FATAL) << "Unexpected binary operation type " << type;
1476 }
1477}
1478
1479void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001480 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001481
1482 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1483 Primitive::Type type = instr->GetResultType();
1484 switch (type) {
1485 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001486 locations->SetInAt(0, Location::RequiresRegister());
1487 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1488 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1489 break;
1490 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001491 locations->SetInAt(0, Location::RequiresRegister());
1492 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1493 locations->SetOut(Location::RequiresRegister());
1494 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001495 default:
1496 LOG(FATAL) << "Unexpected shift type " << type;
1497 }
1498}
1499
1500static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1501
1502void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001503 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001504 LocationSummary* locations = instr->GetLocations();
1505 Primitive::Type type = instr->GetType();
1506
1507 Location rhs_location = locations->InAt(1);
1508 bool use_imm = rhs_location.IsConstant();
1509 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1510 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001511 const uint32_t shift_mask = (type == Primitive::kPrimInt)
1512 ? kMaxIntShiftValue
1513 : kMaxLongShiftValue;
1514 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001515 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1516 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001517
1518 switch (type) {
1519 case Primitive::kPrimInt: {
1520 Register dst = locations->Out().AsRegister<Register>();
1521 Register lhs = locations->InAt(0).AsRegister<Register>();
1522 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001523 if (shift_value == 0) {
1524 if (dst != lhs) {
1525 __ Move(dst, lhs);
1526 }
1527 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001528 __ Sll(dst, lhs, shift_value);
1529 } else if (instr->IsShr()) {
1530 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001531 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001532 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001533 } else {
1534 if (has_ins_rotr) {
1535 __ Rotr(dst, lhs, shift_value);
1536 } else {
1537 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1538 __ Srl(dst, lhs, shift_value);
1539 __ Or(dst, dst, TMP);
1540 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001541 }
1542 } else {
1543 if (instr->IsShl()) {
1544 __ Sllv(dst, lhs, rhs_reg);
1545 } else if (instr->IsShr()) {
1546 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001547 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001548 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001549 } else {
1550 if (has_ins_rotr) {
1551 __ Rotrv(dst, lhs, rhs_reg);
1552 } else {
1553 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001554 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1555 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1556 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1557 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1558 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001559 __ Sllv(TMP, lhs, TMP);
1560 __ Srlv(dst, lhs, rhs_reg);
1561 __ Or(dst, dst, TMP);
1562 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001563 }
1564 }
1565 break;
1566 }
1567
1568 case Primitive::kPrimLong: {
1569 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1570 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1571 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1572 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1573 if (use_imm) {
1574 if (shift_value == 0) {
1575 codegen_->Move64(locations->Out(), locations->InAt(0));
1576 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001577 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001578 if (instr->IsShl()) {
1579 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1580 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1581 __ Sll(dst_low, lhs_low, shift_value);
1582 } else if (instr->IsShr()) {
1583 __ Srl(dst_low, lhs_low, shift_value);
1584 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1585 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001586 } else if (instr->IsUShr()) {
1587 __ Srl(dst_low, lhs_low, shift_value);
1588 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1589 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001590 } else {
1591 __ Srl(dst_low, lhs_low, shift_value);
1592 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1593 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001594 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001595 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001596 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001597 if (instr->IsShl()) {
1598 __ Sll(dst_low, lhs_low, shift_value);
1599 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1600 __ Sll(dst_high, lhs_high, shift_value);
1601 __ Or(dst_high, dst_high, TMP);
1602 } else if (instr->IsShr()) {
1603 __ Sra(dst_high, lhs_high, shift_value);
1604 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1605 __ Srl(dst_low, lhs_low, shift_value);
1606 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001607 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001608 __ Srl(dst_high, lhs_high, shift_value);
1609 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1610 __ Srl(dst_low, lhs_low, shift_value);
1611 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001612 } else {
1613 __ Srl(TMP, lhs_low, shift_value);
1614 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1615 __ Or(dst_low, dst_low, TMP);
1616 __ Srl(TMP, lhs_high, shift_value);
1617 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1618 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001619 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001620 }
1621 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001622 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001623 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001624 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001625 __ Move(dst_low, ZERO);
1626 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001627 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001628 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001629 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001630 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001631 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001632 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001633 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001634 // 64-bit rotation by 32 is just a swap.
1635 __ Move(dst_low, lhs_high);
1636 __ Move(dst_high, lhs_low);
1637 } else {
1638 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001639 __ Srl(dst_low, lhs_high, shift_value_high);
1640 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1641 __ Srl(dst_high, lhs_low, shift_value_high);
1642 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001643 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001644 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1645 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001646 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001647 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1648 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001649 __ Or(dst_high, dst_high, TMP);
1650 }
1651 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001652 }
1653 }
1654 } else {
1655 MipsLabel done;
1656 if (instr->IsShl()) {
1657 __ Sllv(dst_low, lhs_low, rhs_reg);
1658 __ Nor(AT, ZERO, rhs_reg);
1659 __ Srl(TMP, lhs_low, 1);
1660 __ Srlv(TMP, TMP, AT);
1661 __ Sllv(dst_high, lhs_high, rhs_reg);
1662 __ Or(dst_high, dst_high, TMP);
1663 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1664 __ Beqz(TMP, &done);
1665 __ Move(dst_high, dst_low);
1666 __ Move(dst_low, ZERO);
1667 } else if (instr->IsShr()) {
1668 __ Srav(dst_high, lhs_high, rhs_reg);
1669 __ Nor(AT, ZERO, rhs_reg);
1670 __ Sll(TMP, lhs_high, 1);
1671 __ Sllv(TMP, TMP, AT);
1672 __ Srlv(dst_low, lhs_low, rhs_reg);
1673 __ Or(dst_low, dst_low, TMP);
1674 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1675 __ Beqz(TMP, &done);
1676 __ Move(dst_low, dst_high);
1677 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001678 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001679 __ Srlv(dst_high, lhs_high, rhs_reg);
1680 __ Nor(AT, ZERO, rhs_reg);
1681 __ Sll(TMP, lhs_high, 1);
1682 __ Sllv(TMP, TMP, AT);
1683 __ Srlv(dst_low, lhs_low, rhs_reg);
1684 __ Or(dst_low, dst_low, TMP);
1685 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1686 __ Beqz(TMP, &done);
1687 __ Move(dst_low, dst_high);
1688 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001689 } else {
1690 __ Nor(AT, ZERO, rhs_reg);
1691 __ Srlv(TMP, lhs_low, rhs_reg);
1692 __ Sll(dst_low, lhs_high, 1);
1693 __ Sllv(dst_low, dst_low, AT);
1694 __ Or(dst_low, dst_low, TMP);
1695 __ Srlv(TMP, lhs_high, rhs_reg);
1696 __ Sll(dst_high, lhs_low, 1);
1697 __ Sllv(dst_high, dst_high, AT);
1698 __ Or(dst_high, dst_high, TMP);
1699 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1700 __ Beqz(TMP, &done);
1701 __ Move(TMP, dst_high);
1702 __ Move(dst_high, dst_low);
1703 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001704 }
1705 __ Bind(&done);
1706 }
1707 break;
1708 }
1709
1710 default:
1711 LOG(FATAL) << "Unexpected shift operation type " << type;
1712 }
1713}
1714
1715void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1716 HandleBinaryOp(instruction);
1717}
1718
1719void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1720 HandleBinaryOp(instruction);
1721}
1722
1723void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1724 HandleBinaryOp(instruction);
1725}
1726
1727void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1728 HandleBinaryOp(instruction);
1729}
1730
1731void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1732 LocationSummary* locations =
1733 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1734 locations->SetInAt(0, Location::RequiresRegister());
1735 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1736 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1737 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1738 } else {
1739 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1740 }
1741}
1742
1743void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1744 LocationSummary* locations = instruction->GetLocations();
1745 Register obj = locations->InAt(0).AsRegister<Register>();
1746 Location index = locations->InAt(1);
1747 Primitive::Type type = instruction->GetType();
1748
1749 switch (type) {
1750 case Primitive::kPrimBoolean: {
1751 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1752 Register out = locations->Out().AsRegister<Register>();
1753 if (index.IsConstant()) {
1754 size_t offset =
1755 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1756 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1757 } else {
1758 __ Addu(TMP, obj, index.AsRegister<Register>());
1759 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1760 }
1761 break;
1762 }
1763
1764 case Primitive::kPrimByte: {
1765 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1766 Register out = locations->Out().AsRegister<Register>();
1767 if (index.IsConstant()) {
1768 size_t offset =
1769 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1770 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1771 } else {
1772 __ Addu(TMP, obj, index.AsRegister<Register>());
1773 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1774 }
1775 break;
1776 }
1777
1778 case Primitive::kPrimShort: {
1779 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1780 Register out = locations->Out().AsRegister<Register>();
1781 if (index.IsConstant()) {
1782 size_t offset =
1783 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1784 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1785 } else {
1786 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1787 __ Addu(TMP, obj, TMP);
1788 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1789 }
1790 break;
1791 }
1792
1793 case Primitive::kPrimChar: {
1794 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1795 Register out = locations->Out().AsRegister<Register>();
1796 if (index.IsConstant()) {
1797 size_t offset =
1798 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1799 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1800 } else {
1801 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1802 __ Addu(TMP, obj, TMP);
1803 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1804 }
1805 break;
1806 }
1807
1808 case Primitive::kPrimInt:
1809 case Primitive::kPrimNot: {
1810 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1811 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1812 Register out = locations->Out().AsRegister<Register>();
1813 if (index.IsConstant()) {
1814 size_t offset =
1815 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1816 __ LoadFromOffset(kLoadWord, out, obj, offset);
1817 } else {
1818 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1819 __ Addu(TMP, obj, TMP);
1820 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1821 }
1822 break;
1823 }
1824
1825 case Primitive::kPrimLong: {
1826 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1827 Register out = locations->Out().AsRegisterPairLow<Register>();
1828 if (index.IsConstant()) {
1829 size_t offset =
1830 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1831 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1832 } else {
1833 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1834 __ Addu(TMP, obj, TMP);
1835 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1836 }
1837 break;
1838 }
1839
1840 case Primitive::kPrimFloat: {
1841 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1842 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1843 if (index.IsConstant()) {
1844 size_t offset =
1845 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1846 __ LoadSFromOffset(out, obj, offset);
1847 } else {
1848 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1849 __ Addu(TMP, obj, TMP);
1850 __ LoadSFromOffset(out, TMP, data_offset);
1851 }
1852 break;
1853 }
1854
1855 case Primitive::kPrimDouble: {
1856 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1857 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1858 if (index.IsConstant()) {
1859 size_t offset =
1860 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1861 __ LoadDFromOffset(out, obj, offset);
1862 } else {
1863 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1864 __ Addu(TMP, obj, TMP);
1865 __ LoadDFromOffset(out, TMP, data_offset);
1866 }
1867 break;
1868 }
1869
1870 case Primitive::kPrimVoid:
1871 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1872 UNREACHABLE();
1873 }
1874 codegen_->MaybeRecordImplicitNullCheck(instruction);
1875}
1876
1877void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1878 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1879 locations->SetInAt(0, Location::RequiresRegister());
1880 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1881}
1882
1883void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1884 LocationSummary* locations = instruction->GetLocations();
1885 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1886 Register obj = locations->InAt(0).AsRegister<Register>();
1887 Register out = locations->Out().AsRegister<Register>();
1888 __ LoadFromOffset(kLoadWord, out, obj, offset);
1889 codegen_->MaybeRecordImplicitNullCheck(instruction);
1890}
1891
1892void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001893 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001894 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1895 instruction,
Pavle Batuta934808f2015-11-03 13:23:54 +01001896 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1897 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001898 InvokeRuntimeCallingConvention calling_convention;
1899 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1900 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1901 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1902 } else {
1903 locations->SetInAt(0, Location::RequiresRegister());
1904 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1905 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1906 locations->SetInAt(2, Location::RequiresFpuRegister());
1907 } else {
1908 locations->SetInAt(2, Location::RequiresRegister());
1909 }
1910 }
1911}
1912
1913void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1914 LocationSummary* locations = instruction->GetLocations();
1915 Register obj = locations->InAt(0).AsRegister<Register>();
1916 Location index = locations->InAt(1);
1917 Primitive::Type value_type = instruction->GetComponentType();
1918 bool needs_runtime_call = locations->WillCall();
1919 bool needs_write_barrier =
1920 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1921
1922 switch (value_type) {
1923 case Primitive::kPrimBoolean:
1924 case Primitive::kPrimByte: {
1925 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1926 Register value = locations->InAt(2).AsRegister<Register>();
1927 if (index.IsConstant()) {
1928 size_t offset =
1929 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1930 __ StoreToOffset(kStoreByte, value, obj, offset);
1931 } else {
1932 __ Addu(TMP, obj, index.AsRegister<Register>());
1933 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1934 }
1935 break;
1936 }
1937
1938 case Primitive::kPrimShort:
1939 case Primitive::kPrimChar: {
1940 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1941 Register value = locations->InAt(2).AsRegister<Register>();
1942 if (index.IsConstant()) {
1943 size_t offset =
1944 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1945 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1946 } else {
1947 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1948 __ Addu(TMP, obj, TMP);
1949 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1950 }
1951 break;
1952 }
1953
1954 case Primitive::kPrimInt:
1955 case Primitive::kPrimNot: {
1956 if (!needs_runtime_call) {
1957 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1958 Register value = locations->InAt(2).AsRegister<Register>();
1959 if (index.IsConstant()) {
1960 size_t offset =
1961 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1962 __ StoreToOffset(kStoreWord, value, obj, offset);
1963 } else {
1964 DCHECK(index.IsRegister()) << index;
1965 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1966 __ Addu(TMP, obj, TMP);
1967 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1968 }
1969 codegen_->MaybeRecordImplicitNullCheck(instruction);
1970 if (needs_write_barrier) {
1971 DCHECK_EQ(value_type, Primitive::kPrimNot);
1972 codegen_->MarkGCCard(obj, value);
1973 }
1974 } else {
1975 DCHECK_EQ(value_type, Primitive::kPrimNot);
1976 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1977 instruction,
1978 instruction->GetDexPc(),
1979 nullptr,
1980 IsDirectEntrypoint(kQuickAputObject));
1981 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
1982 }
1983 break;
1984 }
1985
1986 case Primitive::kPrimLong: {
1987 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1988 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
1989 if (index.IsConstant()) {
1990 size_t offset =
1991 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1992 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1993 } else {
1994 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1995 __ Addu(TMP, obj, TMP);
1996 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1997 }
1998 break;
1999 }
2000
2001 case Primitive::kPrimFloat: {
2002 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2003 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2004 DCHECK(locations->InAt(2).IsFpuRegister());
2005 if (index.IsConstant()) {
2006 size_t offset =
2007 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2008 __ StoreSToOffset(value, obj, offset);
2009 } else {
2010 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2011 __ Addu(TMP, obj, TMP);
2012 __ StoreSToOffset(value, TMP, data_offset);
2013 }
2014 break;
2015 }
2016
2017 case Primitive::kPrimDouble: {
2018 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2019 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2020 DCHECK(locations->InAt(2).IsFpuRegister());
2021 if (index.IsConstant()) {
2022 size_t offset =
2023 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2024 __ StoreDToOffset(value, obj, offset);
2025 } else {
2026 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2027 __ Addu(TMP, obj, TMP);
2028 __ StoreDToOffset(value, TMP, data_offset);
2029 }
2030 break;
2031 }
2032
2033 case Primitive::kPrimVoid:
2034 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2035 UNREACHABLE();
2036 }
2037
2038 // Ints and objects are handled in the switch.
2039 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
2040 codegen_->MaybeRecordImplicitNullCheck(instruction);
2041 }
2042}
2043
2044void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2045 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2046 ? LocationSummary::kCallOnSlowPath
2047 : LocationSummary::kNoCall;
2048 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2049 locations->SetInAt(0, Location::RequiresRegister());
2050 locations->SetInAt(1, Location::RequiresRegister());
2051 if (instruction->HasUses()) {
2052 locations->SetOut(Location::SameAsFirstInput());
2053 }
2054}
2055
2056void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2057 LocationSummary* locations = instruction->GetLocations();
2058 BoundsCheckSlowPathMIPS* slow_path =
2059 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2060 codegen_->AddSlowPath(slow_path);
2061
2062 Register index = locations->InAt(0).AsRegister<Register>();
2063 Register length = locations->InAt(1).AsRegister<Register>();
2064
2065 // length is limited by the maximum positive signed 32-bit integer.
2066 // Unsigned comparison of length and index checks for index < 0
2067 // and for length <= index simultaneously.
2068 __ Bgeu(index, length, slow_path->GetEntryLabel());
2069}
2070
2071void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2072 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2073 instruction,
2074 LocationSummary::kCallOnSlowPath);
2075 locations->SetInAt(0, Location::RequiresRegister());
2076 locations->SetInAt(1, Location::RequiresRegister());
2077 // Note that TypeCheckSlowPathMIPS uses this register too.
2078 locations->AddTemp(Location::RequiresRegister());
2079}
2080
2081void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2082 LocationSummary* locations = instruction->GetLocations();
2083 Register obj = locations->InAt(0).AsRegister<Register>();
2084 Register cls = locations->InAt(1).AsRegister<Register>();
2085 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2086
2087 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2088 codegen_->AddSlowPath(slow_path);
2089
2090 // TODO: avoid this check if we know obj is not null.
2091 __ Beqz(obj, slow_path->GetExitLabel());
2092 // Compare the class of `obj` with `cls`.
2093 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2094 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2095 __ Bind(slow_path->GetExitLabel());
2096}
2097
2098void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2099 LocationSummary* locations =
2100 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2101 locations->SetInAt(0, Location::RequiresRegister());
2102 if (check->HasUses()) {
2103 locations->SetOut(Location::SameAsFirstInput());
2104 }
2105}
2106
2107void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2108 // We assume the class is not null.
2109 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2110 check->GetLoadClass(),
2111 check,
2112 check->GetDexPc(),
2113 true);
2114 codegen_->AddSlowPath(slow_path);
2115 GenerateClassInitializationCheck(slow_path,
2116 check->GetLocations()->InAt(0).AsRegister<Register>());
2117}
2118
2119void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2120 Primitive::Type in_type = compare->InputAt(0)->GetType();
2121
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002122 LocationSummary* locations =
2123 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002124
2125 switch (in_type) {
2126 case Primitive::kPrimLong:
2127 locations->SetInAt(0, Location::RequiresRegister());
2128 locations->SetInAt(1, Location::RequiresRegister());
2129 // Output overlaps because it is written before doing the low comparison.
2130 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2131 break;
2132
2133 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002134 case Primitive::kPrimDouble:
2135 locations->SetInAt(0, Location::RequiresFpuRegister());
2136 locations->SetInAt(1, Location::RequiresFpuRegister());
2137 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002138 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002139
2140 default:
2141 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2142 }
2143}
2144
2145void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2146 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002147 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002148 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002149 bool gt_bias = instruction->IsGtBias();
2150 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002151
2152 // 0 if: left == right
2153 // 1 if: left > right
2154 // -1 if: left < right
2155 switch (in_type) {
2156 case Primitive::kPrimLong: {
2157 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002158 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2159 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2160 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2161 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2162 // TODO: more efficient (direct) comparison with a constant.
2163 __ Slt(TMP, lhs_high, rhs_high);
2164 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2165 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2166 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2167 __ Sltu(TMP, lhs_low, rhs_low);
2168 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2169 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2170 __ Bind(&done);
2171 break;
2172 }
2173
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002174 case Primitive::kPrimFloat: {
2175 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2176 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2177 MipsLabel done;
2178 if (isR6) {
2179 __ CmpEqS(FTMP, lhs, rhs);
2180 __ LoadConst32(res, 0);
2181 __ Bc1nez(FTMP, &done);
2182 if (gt_bias) {
2183 __ CmpLtS(FTMP, lhs, rhs);
2184 __ LoadConst32(res, -1);
2185 __ Bc1nez(FTMP, &done);
2186 __ LoadConst32(res, 1);
2187 } else {
2188 __ CmpLtS(FTMP, rhs, lhs);
2189 __ LoadConst32(res, 1);
2190 __ Bc1nez(FTMP, &done);
2191 __ LoadConst32(res, -1);
2192 }
2193 } else {
2194 if (gt_bias) {
2195 __ ColtS(0, lhs, rhs);
2196 __ LoadConst32(res, -1);
2197 __ Bc1t(0, &done);
2198 __ CeqS(0, lhs, rhs);
2199 __ LoadConst32(res, 1);
2200 __ Movt(res, ZERO, 0);
2201 } else {
2202 __ ColtS(0, rhs, lhs);
2203 __ LoadConst32(res, 1);
2204 __ Bc1t(0, &done);
2205 __ CeqS(0, lhs, rhs);
2206 __ LoadConst32(res, -1);
2207 __ Movt(res, ZERO, 0);
2208 }
2209 }
2210 __ Bind(&done);
2211 break;
2212 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002213 case Primitive::kPrimDouble: {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002214 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2215 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2216 MipsLabel done;
2217 if (isR6) {
2218 __ CmpEqD(FTMP, lhs, rhs);
2219 __ LoadConst32(res, 0);
2220 __ Bc1nez(FTMP, &done);
2221 if (gt_bias) {
2222 __ CmpLtD(FTMP, lhs, rhs);
2223 __ LoadConst32(res, -1);
2224 __ Bc1nez(FTMP, &done);
2225 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002226 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002227 __ CmpLtD(FTMP, rhs, lhs);
2228 __ LoadConst32(res, 1);
2229 __ Bc1nez(FTMP, &done);
2230 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002231 }
2232 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002233 if (gt_bias) {
2234 __ ColtD(0, lhs, rhs);
2235 __ LoadConst32(res, -1);
2236 __ Bc1t(0, &done);
2237 __ CeqD(0, lhs, rhs);
2238 __ LoadConst32(res, 1);
2239 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002240 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002241 __ ColtD(0, rhs, lhs);
2242 __ LoadConst32(res, 1);
2243 __ Bc1t(0, &done);
2244 __ CeqD(0, lhs, rhs);
2245 __ LoadConst32(res, -1);
2246 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002247 }
2248 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002249 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002250 break;
2251 }
2252
2253 default:
2254 LOG(FATAL) << "Unimplemented compare type " << in_type;
2255 }
2256}
2257
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002258void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002259 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002260 switch (instruction->InputAt(0)->GetType()) {
2261 default:
2262 case Primitive::kPrimLong:
2263 locations->SetInAt(0, Location::RequiresRegister());
2264 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2265 break;
2266
2267 case Primitive::kPrimFloat:
2268 case Primitive::kPrimDouble:
2269 locations->SetInAt(0, Location::RequiresFpuRegister());
2270 locations->SetInAt(1, Location::RequiresFpuRegister());
2271 break;
2272 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002273 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2275 }
2276}
2277
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002278void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002279 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002280 return;
2281 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002282
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002283 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002284 LocationSummary* locations = instruction->GetLocations();
2285 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002286 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002287
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002288 switch (type) {
2289 default:
2290 // Integer case.
2291 GenerateIntCompare(instruction->GetCondition(), locations);
2292 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002293
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002294 case Primitive::kPrimLong:
2295 // TODO: don't use branches.
2296 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002297 break;
2298
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002299 case Primitive::kPrimFloat:
2300 case Primitive::kPrimDouble:
2301 // TODO: don't use branches.
2302 GenerateFpCompareAndBranch(instruction->GetCondition(),
2303 instruction->IsGtBias(),
2304 type,
2305 locations,
2306 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002307 break;
2308 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002309
2310 // Convert the branches into the result.
2311 MipsLabel done;
2312
2313 // False case: result = 0.
2314 __ LoadConst32(dst, 0);
2315 __ B(&done);
2316
2317 // True case: result = 1.
2318 __ Bind(&true_label);
2319 __ LoadConst32(dst, 1);
2320 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002321}
2322
Alexey Frunze7e99e052015-11-24 19:28:01 -08002323void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2324 DCHECK(instruction->IsDiv() || instruction->IsRem());
2325 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2326
2327 LocationSummary* locations = instruction->GetLocations();
2328 Location second = locations->InAt(1);
2329 DCHECK(second.IsConstant());
2330
2331 Register out = locations->Out().AsRegister<Register>();
2332 Register dividend = locations->InAt(0).AsRegister<Register>();
2333 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2334 DCHECK(imm == 1 || imm == -1);
2335
2336 if (instruction->IsRem()) {
2337 __ Move(out, ZERO);
2338 } else {
2339 if (imm == -1) {
2340 __ Subu(out, ZERO, dividend);
2341 } else if (out != dividend) {
2342 __ Move(out, dividend);
2343 }
2344 }
2345}
2346
2347void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2348 DCHECK(instruction->IsDiv() || instruction->IsRem());
2349 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2350
2351 LocationSummary* locations = instruction->GetLocations();
2352 Location second = locations->InAt(1);
2353 DCHECK(second.IsConstant());
2354
2355 Register out = locations->Out().AsRegister<Register>();
2356 Register dividend = locations->InAt(0).AsRegister<Register>();
2357 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002358 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002359 int ctz_imm = CTZ(abs_imm);
2360
2361 if (instruction->IsDiv()) {
2362 if (ctz_imm == 1) {
2363 // Fast path for division by +/-2, which is very common.
2364 __ Srl(TMP, dividend, 31);
2365 } else {
2366 __ Sra(TMP, dividend, 31);
2367 __ Srl(TMP, TMP, 32 - ctz_imm);
2368 }
2369 __ Addu(out, dividend, TMP);
2370 __ Sra(out, out, ctz_imm);
2371 if (imm < 0) {
2372 __ Subu(out, ZERO, out);
2373 }
2374 } else {
2375 if (ctz_imm == 1) {
2376 // Fast path for modulo +/-2, which is very common.
2377 __ Sra(TMP, dividend, 31);
2378 __ Subu(out, dividend, TMP);
2379 __ Andi(out, out, 1);
2380 __ Addu(out, out, TMP);
2381 } else {
2382 __ Sra(TMP, dividend, 31);
2383 __ Srl(TMP, TMP, 32 - ctz_imm);
2384 __ Addu(out, dividend, TMP);
2385 if (IsUint<16>(abs_imm - 1)) {
2386 __ Andi(out, out, abs_imm - 1);
2387 } else {
2388 __ Sll(out, out, 32 - ctz_imm);
2389 __ Srl(out, out, 32 - ctz_imm);
2390 }
2391 __ Subu(out, out, TMP);
2392 }
2393 }
2394}
2395
2396void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2397 DCHECK(instruction->IsDiv() || instruction->IsRem());
2398 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2399
2400 LocationSummary* locations = instruction->GetLocations();
2401 Location second = locations->InAt(1);
2402 DCHECK(second.IsConstant());
2403
2404 Register out = locations->Out().AsRegister<Register>();
2405 Register dividend = locations->InAt(0).AsRegister<Register>();
2406 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2407
2408 int64_t magic;
2409 int shift;
2410 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2411
2412 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2413
2414 __ LoadConst32(TMP, magic);
2415 if (isR6) {
2416 __ MuhR6(TMP, dividend, TMP);
2417 } else {
2418 __ MultR2(dividend, TMP);
2419 __ Mfhi(TMP);
2420 }
2421 if (imm > 0 && magic < 0) {
2422 __ Addu(TMP, TMP, dividend);
2423 } else if (imm < 0 && magic > 0) {
2424 __ Subu(TMP, TMP, dividend);
2425 }
2426
2427 if (shift != 0) {
2428 __ Sra(TMP, TMP, shift);
2429 }
2430
2431 if (instruction->IsDiv()) {
2432 __ Sra(out, TMP, 31);
2433 __ Subu(out, TMP, out);
2434 } else {
2435 __ Sra(AT, TMP, 31);
2436 __ Subu(AT, TMP, AT);
2437 __ LoadConst32(TMP, imm);
2438 if (isR6) {
2439 __ MulR6(TMP, AT, TMP);
2440 } else {
2441 __ MulR2(TMP, AT, TMP);
2442 }
2443 __ Subu(out, dividend, TMP);
2444 }
2445}
2446
2447void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2448 DCHECK(instruction->IsDiv() || instruction->IsRem());
2449 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2450
2451 LocationSummary* locations = instruction->GetLocations();
2452 Register out = locations->Out().AsRegister<Register>();
2453 Location second = locations->InAt(1);
2454
2455 if (second.IsConstant()) {
2456 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2457 if (imm == 0) {
2458 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2459 } else if (imm == 1 || imm == -1) {
2460 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002461 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002462 DivRemByPowerOfTwo(instruction);
2463 } else {
2464 DCHECK(imm <= -2 || imm >= 2);
2465 GenerateDivRemWithAnyConstant(instruction);
2466 }
2467 } else {
2468 Register dividend = locations->InAt(0).AsRegister<Register>();
2469 Register divisor = second.AsRegister<Register>();
2470 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2471 if (instruction->IsDiv()) {
2472 if (isR6) {
2473 __ DivR6(out, dividend, divisor);
2474 } else {
2475 __ DivR2(out, dividend, divisor);
2476 }
2477 } else {
2478 if (isR6) {
2479 __ ModR6(out, dividend, divisor);
2480 } else {
2481 __ ModR2(out, dividend, divisor);
2482 }
2483 }
2484 }
2485}
2486
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002487void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2488 Primitive::Type type = div->GetResultType();
2489 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
2490 ? LocationSummary::kCall
2491 : LocationSummary::kNoCall;
2492
2493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2494
2495 switch (type) {
2496 case Primitive::kPrimInt:
2497 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002498 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2500 break;
2501
2502 case Primitive::kPrimLong: {
2503 InvokeRuntimeCallingConvention calling_convention;
2504 locations->SetInAt(0, Location::RegisterPairLocation(
2505 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2506 locations->SetInAt(1, Location::RegisterPairLocation(
2507 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2508 locations->SetOut(calling_convention.GetReturnLocation(type));
2509 break;
2510 }
2511
2512 case Primitive::kPrimFloat:
2513 case Primitive::kPrimDouble:
2514 locations->SetInAt(0, Location::RequiresFpuRegister());
2515 locations->SetInAt(1, Location::RequiresFpuRegister());
2516 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2517 break;
2518
2519 default:
2520 LOG(FATAL) << "Unexpected div type " << type;
2521 }
2522}
2523
2524void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2525 Primitive::Type type = instruction->GetType();
2526 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002527
2528 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002529 case Primitive::kPrimInt:
2530 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002531 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002532 case Primitive::kPrimLong: {
2533 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2534 instruction,
2535 instruction->GetDexPc(),
2536 nullptr,
2537 IsDirectEntrypoint(kQuickLdiv));
2538 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2539 break;
2540 }
2541 case Primitive::kPrimFloat:
2542 case Primitive::kPrimDouble: {
2543 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2544 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2545 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2546 if (type == Primitive::kPrimFloat) {
2547 __ DivS(dst, lhs, rhs);
2548 } else {
2549 __ DivD(dst, lhs, rhs);
2550 }
2551 break;
2552 }
2553 default:
2554 LOG(FATAL) << "Unexpected div type " << type;
2555 }
2556}
2557
2558void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2559 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2560 ? LocationSummary::kCallOnSlowPath
2561 : LocationSummary::kNoCall;
2562 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2563 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2564 if (instruction->HasUses()) {
2565 locations->SetOut(Location::SameAsFirstInput());
2566 }
2567}
2568
2569void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2570 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2571 codegen_->AddSlowPath(slow_path);
2572 Location value = instruction->GetLocations()->InAt(0);
2573 Primitive::Type type = instruction->GetType();
2574
2575 switch (type) {
2576 case Primitive::kPrimByte:
2577 case Primitive::kPrimChar:
2578 case Primitive::kPrimShort:
2579 case Primitive::kPrimInt: {
2580 if (value.IsConstant()) {
2581 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2582 __ B(slow_path->GetEntryLabel());
2583 } else {
2584 // A division by a non-null constant is valid. We don't need to perform
2585 // any check, so simply fall through.
2586 }
2587 } else {
2588 DCHECK(value.IsRegister()) << value;
2589 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2590 }
2591 break;
2592 }
2593 case Primitive::kPrimLong: {
2594 if (value.IsConstant()) {
2595 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2596 __ B(slow_path->GetEntryLabel());
2597 } else {
2598 // A division by a non-null constant is valid. We don't need to perform
2599 // any check, so simply fall through.
2600 }
2601 } else {
2602 DCHECK(value.IsRegisterPair()) << value;
2603 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2604 __ Beqz(TMP, slow_path->GetEntryLabel());
2605 }
2606 break;
2607 }
2608 default:
2609 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2610 }
2611}
2612
2613void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2614 LocationSummary* locations =
2615 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2616 locations->SetOut(Location::ConstantLocation(constant));
2617}
2618
2619void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2620 // Will be generated at use site.
2621}
2622
2623void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2624 exit->SetLocations(nullptr);
2625}
2626
2627void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2628}
2629
2630void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2631 LocationSummary* locations =
2632 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2633 locations->SetOut(Location::ConstantLocation(constant));
2634}
2635
2636void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2637 // Will be generated at use site.
2638}
2639
2640void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2641 got->SetLocations(nullptr);
2642}
2643
2644void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2645 DCHECK(!successor->IsExitBlock());
2646 HBasicBlock* block = got->GetBlock();
2647 HInstruction* previous = got->GetPrevious();
2648 HLoopInformation* info = block->GetLoopInformation();
2649
2650 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2651 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2652 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2653 return;
2654 }
2655 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2656 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2657 }
2658 if (!codegen_->GoesToNextBlock(block, successor)) {
2659 __ B(codegen_->GetLabelOf(successor));
2660 }
2661}
2662
2663void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2664 HandleGoto(got, got->GetSuccessor());
2665}
2666
2667void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2668 try_boundary->SetLocations(nullptr);
2669}
2670
2671void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2672 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2673 if (!successor->IsExitBlock()) {
2674 HandleGoto(try_boundary, successor);
2675 }
2676}
2677
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002678void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2679 LocationSummary* locations) {
2680 Register dst = locations->Out().AsRegister<Register>();
2681 Register lhs = locations->InAt(0).AsRegister<Register>();
2682 Location rhs_location = locations->InAt(1);
2683 Register rhs_reg = ZERO;
2684 int64_t rhs_imm = 0;
2685 bool use_imm = rhs_location.IsConstant();
2686 if (use_imm) {
2687 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2688 } else {
2689 rhs_reg = rhs_location.AsRegister<Register>();
2690 }
2691
2692 switch (cond) {
2693 case kCondEQ:
2694 case kCondNE:
2695 if (use_imm && IsUint<16>(rhs_imm)) {
2696 __ Xori(dst, lhs, rhs_imm);
2697 } else {
2698 if (use_imm) {
2699 rhs_reg = TMP;
2700 __ LoadConst32(rhs_reg, rhs_imm);
2701 }
2702 __ Xor(dst, lhs, rhs_reg);
2703 }
2704 if (cond == kCondEQ) {
2705 __ Sltiu(dst, dst, 1);
2706 } else {
2707 __ Sltu(dst, ZERO, dst);
2708 }
2709 break;
2710
2711 case kCondLT:
2712 case kCondGE:
2713 if (use_imm && IsInt<16>(rhs_imm)) {
2714 __ Slti(dst, lhs, rhs_imm);
2715 } else {
2716 if (use_imm) {
2717 rhs_reg = TMP;
2718 __ LoadConst32(rhs_reg, rhs_imm);
2719 }
2720 __ Slt(dst, lhs, rhs_reg);
2721 }
2722 if (cond == kCondGE) {
2723 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2724 // only the slt instruction but no sge.
2725 __ Xori(dst, dst, 1);
2726 }
2727 break;
2728
2729 case kCondLE:
2730 case kCondGT:
2731 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2732 // Simulate lhs <= rhs via lhs < rhs + 1.
2733 __ Slti(dst, lhs, rhs_imm + 1);
2734 if (cond == kCondGT) {
2735 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2736 // only the slti instruction but no sgti.
2737 __ Xori(dst, dst, 1);
2738 }
2739 } else {
2740 if (use_imm) {
2741 rhs_reg = TMP;
2742 __ LoadConst32(rhs_reg, rhs_imm);
2743 }
2744 __ Slt(dst, rhs_reg, lhs);
2745 if (cond == kCondLE) {
2746 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2747 // only the slt instruction but no sle.
2748 __ Xori(dst, dst, 1);
2749 }
2750 }
2751 break;
2752
2753 case kCondB:
2754 case kCondAE:
2755 if (use_imm && IsInt<16>(rhs_imm)) {
2756 // Sltiu sign-extends its 16-bit immediate operand before
2757 // the comparison and thus lets us compare directly with
2758 // unsigned values in the ranges [0, 0x7fff] and
2759 // [0xffff8000, 0xffffffff].
2760 __ Sltiu(dst, lhs, rhs_imm);
2761 } else {
2762 if (use_imm) {
2763 rhs_reg = TMP;
2764 __ LoadConst32(rhs_reg, rhs_imm);
2765 }
2766 __ Sltu(dst, lhs, rhs_reg);
2767 }
2768 if (cond == kCondAE) {
2769 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2770 // only the sltu instruction but no sgeu.
2771 __ Xori(dst, dst, 1);
2772 }
2773 break;
2774
2775 case kCondBE:
2776 case kCondA:
2777 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2778 // Simulate lhs <= rhs via lhs < rhs + 1.
2779 // Note that this only works if rhs + 1 does not overflow
2780 // to 0, hence the check above.
2781 // Sltiu sign-extends its 16-bit immediate operand before
2782 // the comparison and thus lets us compare directly with
2783 // unsigned values in the ranges [0, 0x7fff] and
2784 // [0xffff8000, 0xffffffff].
2785 __ Sltiu(dst, lhs, rhs_imm + 1);
2786 if (cond == kCondA) {
2787 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2788 // only the sltiu instruction but no sgtiu.
2789 __ Xori(dst, dst, 1);
2790 }
2791 } else {
2792 if (use_imm) {
2793 rhs_reg = TMP;
2794 __ LoadConst32(rhs_reg, rhs_imm);
2795 }
2796 __ Sltu(dst, rhs_reg, lhs);
2797 if (cond == kCondBE) {
2798 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2799 // only the sltu instruction but no sleu.
2800 __ Xori(dst, dst, 1);
2801 }
2802 }
2803 break;
2804 }
2805}
2806
2807void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2808 LocationSummary* locations,
2809 MipsLabel* label) {
2810 Register lhs = locations->InAt(0).AsRegister<Register>();
2811 Location rhs_location = locations->InAt(1);
2812 Register rhs_reg = ZERO;
2813 int32_t rhs_imm = 0;
2814 bool use_imm = rhs_location.IsConstant();
2815 if (use_imm) {
2816 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2817 } else {
2818 rhs_reg = rhs_location.AsRegister<Register>();
2819 }
2820
2821 if (use_imm && rhs_imm == 0) {
2822 switch (cond) {
2823 case kCondEQ:
2824 case kCondBE: // <= 0 if zero
2825 __ Beqz(lhs, label);
2826 break;
2827 case kCondNE:
2828 case kCondA: // > 0 if non-zero
2829 __ Bnez(lhs, label);
2830 break;
2831 case kCondLT:
2832 __ Bltz(lhs, label);
2833 break;
2834 case kCondGE:
2835 __ Bgez(lhs, label);
2836 break;
2837 case kCondLE:
2838 __ Blez(lhs, label);
2839 break;
2840 case kCondGT:
2841 __ Bgtz(lhs, label);
2842 break;
2843 case kCondB: // always false
2844 break;
2845 case kCondAE: // always true
2846 __ B(label);
2847 break;
2848 }
2849 } else {
2850 if (use_imm) {
2851 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2852 rhs_reg = TMP;
2853 __ LoadConst32(rhs_reg, rhs_imm);
2854 }
2855 switch (cond) {
2856 case kCondEQ:
2857 __ Beq(lhs, rhs_reg, label);
2858 break;
2859 case kCondNE:
2860 __ Bne(lhs, rhs_reg, label);
2861 break;
2862 case kCondLT:
2863 __ Blt(lhs, rhs_reg, label);
2864 break;
2865 case kCondGE:
2866 __ Bge(lhs, rhs_reg, label);
2867 break;
2868 case kCondLE:
2869 __ Bge(rhs_reg, lhs, label);
2870 break;
2871 case kCondGT:
2872 __ Blt(rhs_reg, lhs, label);
2873 break;
2874 case kCondB:
2875 __ Bltu(lhs, rhs_reg, label);
2876 break;
2877 case kCondAE:
2878 __ Bgeu(lhs, rhs_reg, label);
2879 break;
2880 case kCondBE:
2881 __ Bgeu(rhs_reg, lhs, label);
2882 break;
2883 case kCondA:
2884 __ Bltu(rhs_reg, lhs, label);
2885 break;
2886 }
2887 }
2888}
2889
2890void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2891 LocationSummary* locations,
2892 MipsLabel* label) {
2893 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2894 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2895 Location rhs_location = locations->InAt(1);
2896 Register rhs_high = ZERO;
2897 Register rhs_low = ZERO;
2898 int64_t imm = 0;
2899 uint32_t imm_high = 0;
2900 uint32_t imm_low = 0;
2901 bool use_imm = rhs_location.IsConstant();
2902 if (use_imm) {
2903 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2904 imm_high = High32Bits(imm);
2905 imm_low = Low32Bits(imm);
2906 } else {
2907 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2908 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2909 }
2910
2911 if (use_imm && imm == 0) {
2912 switch (cond) {
2913 case kCondEQ:
2914 case kCondBE: // <= 0 if zero
2915 __ Or(TMP, lhs_high, lhs_low);
2916 __ Beqz(TMP, label);
2917 break;
2918 case kCondNE:
2919 case kCondA: // > 0 if non-zero
2920 __ Or(TMP, lhs_high, lhs_low);
2921 __ Bnez(TMP, label);
2922 break;
2923 case kCondLT:
2924 __ Bltz(lhs_high, label);
2925 break;
2926 case kCondGE:
2927 __ Bgez(lhs_high, label);
2928 break;
2929 case kCondLE:
2930 __ Or(TMP, lhs_high, lhs_low);
2931 __ Sra(AT, lhs_high, 31);
2932 __ Bgeu(AT, TMP, label);
2933 break;
2934 case kCondGT:
2935 __ Or(TMP, lhs_high, lhs_low);
2936 __ Sra(AT, lhs_high, 31);
2937 __ Bltu(AT, TMP, label);
2938 break;
2939 case kCondB: // always false
2940 break;
2941 case kCondAE: // always true
2942 __ B(label);
2943 break;
2944 }
2945 } else if (use_imm) {
2946 // TODO: more efficient comparison with constants without loading them into TMP/AT.
2947 switch (cond) {
2948 case kCondEQ:
2949 __ LoadConst32(TMP, imm_high);
2950 __ Xor(TMP, TMP, lhs_high);
2951 __ LoadConst32(AT, imm_low);
2952 __ Xor(AT, AT, lhs_low);
2953 __ Or(TMP, TMP, AT);
2954 __ Beqz(TMP, label);
2955 break;
2956 case kCondNE:
2957 __ LoadConst32(TMP, imm_high);
2958 __ Xor(TMP, TMP, lhs_high);
2959 __ LoadConst32(AT, imm_low);
2960 __ Xor(AT, AT, lhs_low);
2961 __ Or(TMP, TMP, AT);
2962 __ Bnez(TMP, label);
2963 break;
2964 case kCondLT:
2965 __ LoadConst32(TMP, imm_high);
2966 __ Blt(lhs_high, TMP, label);
2967 __ Slt(TMP, TMP, lhs_high);
2968 __ LoadConst32(AT, imm_low);
2969 __ Sltu(AT, lhs_low, AT);
2970 __ Blt(TMP, AT, label);
2971 break;
2972 case kCondGE:
2973 __ LoadConst32(TMP, imm_high);
2974 __ Blt(TMP, lhs_high, label);
2975 __ Slt(TMP, lhs_high, TMP);
2976 __ LoadConst32(AT, imm_low);
2977 __ Sltu(AT, lhs_low, AT);
2978 __ Or(TMP, TMP, AT);
2979 __ Beqz(TMP, label);
2980 break;
2981 case kCondLE:
2982 __ LoadConst32(TMP, imm_high);
2983 __ Blt(lhs_high, TMP, label);
2984 __ Slt(TMP, TMP, lhs_high);
2985 __ LoadConst32(AT, imm_low);
2986 __ Sltu(AT, AT, lhs_low);
2987 __ Or(TMP, TMP, AT);
2988 __ Beqz(TMP, label);
2989 break;
2990 case kCondGT:
2991 __ LoadConst32(TMP, imm_high);
2992 __ Blt(TMP, lhs_high, label);
2993 __ Slt(TMP, lhs_high, TMP);
2994 __ LoadConst32(AT, imm_low);
2995 __ Sltu(AT, AT, lhs_low);
2996 __ Blt(TMP, AT, label);
2997 break;
2998 case kCondB:
2999 __ LoadConst32(TMP, imm_high);
3000 __ Bltu(lhs_high, TMP, label);
3001 __ Sltu(TMP, TMP, lhs_high);
3002 __ LoadConst32(AT, imm_low);
3003 __ Sltu(AT, lhs_low, AT);
3004 __ Blt(TMP, AT, label);
3005 break;
3006 case kCondAE:
3007 __ LoadConst32(TMP, imm_high);
3008 __ Bltu(TMP, lhs_high, label);
3009 __ Sltu(TMP, lhs_high, TMP);
3010 __ LoadConst32(AT, imm_low);
3011 __ Sltu(AT, lhs_low, AT);
3012 __ Or(TMP, TMP, AT);
3013 __ Beqz(TMP, label);
3014 break;
3015 case kCondBE:
3016 __ LoadConst32(TMP, imm_high);
3017 __ Bltu(lhs_high, TMP, label);
3018 __ Sltu(TMP, TMP, lhs_high);
3019 __ LoadConst32(AT, imm_low);
3020 __ Sltu(AT, AT, lhs_low);
3021 __ Or(TMP, TMP, AT);
3022 __ Beqz(TMP, label);
3023 break;
3024 case kCondA:
3025 __ LoadConst32(TMP, imm_high);
3026 __ Bltu(TMP, lhs_high, label);
3027 __ Sltu(TMP, lhs_high, TMP);
3028 __ LoadConst32(AT, imm_low);
3029 __ Sltu(AT, AT, lhs_low);
3030 __ Blt(TMP, AT, label);
3031 break;
3032 }
3033 } else {
3034 switch (cond) {
3035 case kCondEQ:
3036 __ Xor(TMP, lhs_high, rhs_high);
3037 __ Xor(AT, lhs_low, rhs_low);
3038 __ Or(TMP, TMP, AT);
3039 __ Beqz(TMP, label);
3040 break;
3041 case kCondNE:
3042 __ Xor(TMP, lhs_high, rhs_high);
3043 __ Xor(AT, lhs_low, rhs_low);
3044 __ Or(TMP, TMP, AT);
3045 __ Bnez(TMP, label);
3046 break;
3047 case kCondLT:
3048 __ Blt(lhs_high, rhs_high, label);
3049 __ Slt(TMP, rhs_high, lhs_high);
3050 __ Sltu(AT, lhs_low, rhs_low);
3051 __ Blt(TMP, AT, label);
3052 break;
3053 case kCondGE:
3054 __ Blt(rhs_high, lhs_high, label);
3055 __ Slt(TMP, lhs_high, rhs_high);
3056 __ Sltu(AT, lhs_low, rhs_low);
3057 __ Or(TMP, TMP, AT);
3058 __ Beqz(TMP, label);
3059 break;
3060 case kCondLE:
3061 __ Blt(lhs_high, rhs_high, label);
3062 __ Slt(TMP, rhs_high, lhs_high);
3063 __ Sltu(AT, rhs_low, lhs_low);
3064 __ Or(TMP, TMP, AT);
3065 __ Beqz(TMP, label);
3066 break;
3067 case kCondGT:
3068 __ Blt(rhs_high, lhs_high, label);
3069 __ Slt(TMP, lhs_high, rhs_high);
3070 __ Sltu(AT, rhs_low, lhs_low);
3071 __ Blt(TMP, AT, label);
3072 break;
3073 case kCondB:
3074 __ Bltu(lhs_high, rhs_high, label);
3075 __ Sltu(TMP, rhs_high, lhs_high);
3076 __ Sltu(AT, lhs_low, rhs_low);
3077 __ Blt(TMP, AT, label);
3078 break;
3079 case kCondAE:
3080 __ Bltu(rhs_high, lhs_high, label);
3081 __ Sltu(TMP, lhs_high, rhs_high);
3082 __ Sltu(AT, lhs_low, rhs_low);
3083 __ Or(TMP, TMP, AT);
3084 __ Beqz(TMP, label);
3085 break;
3086 case kCondBE:
3087 __ Bltu(lhs_high, rhs_high, label);
3088 __ Sltu(TMP, rhs_high, lhs_high);
3089 __ Sltu(AT, rhs_low, lhs_low);
3090 __ Or(TMP, TMP, AT);
3091 __ Beqz(TMP, label);
3092 break;
3093 case kCondA:
3094 __ Bltu(rhs_high, lhs_high, label);
3095 __ Sltu(TMP, lhs_high, rhs_high);
3096 __ Sltu(AT, rhs_low, lhs_low);
3097 __ Blt(TMP, AT, label);
3098 break;
3099 }
3100 }
3101}
3102
3103void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3104 bool gt_bias,
3105 Primitive::Type type,
3106 LocationSummary* locations,
3107 MipsLabel* label) {
3108 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3109 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3110 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3111 if (type == Primitive::kPrimFloat) {
3112 if (isR6) {
3113 switch (cond) {
3114 case kCondEQ:
3115 __ CmpEqS(FTMP, lhs, rhs);
3116 __ Bc1nez(FTMP, label);
3117 break;
3118 case kCondNE:
3119 __ CmpEqS(FTMP, lhs, rhs);
3120 __ Bc1eqz(FTMP, label);
3121 break;
3122 case kCondLT:
3123 if (gt_bias) {
3124 __ CmpLtS(FTMP, lhs, rhs);
3125 } else {
3126 __ CmpUltS(FTMP, lhs, rhs);
3127 }
3128 __ Bc1nez(FTMP, label);
3129 break;
3130 case kCondLE:
3131 if (gt_bias) {
3132 __ CmpLeS(FTMP, lhs, rhs);
3133 } else {
3134 __ CmpUleS(FTMP, lhs, rhs);
3135 }
3136 __ Bc1nez(FTMP, label);
3137 break;
3138 case kCondGT:
3139 if (gt_bias) {
3140 __ CmpUltS(FTMP, rhs, lhs);
3141 } else {
3142 __ CmpLtS(FTMP, rhs, lhs);
3143 }
3144 __ Bc1nez(FTMP, label);
3145 break;
3146 case kCondGE:
3147 if (gt_bias) {
3148 __ CmpUleS(FTMP, rhs, lhs);
3149 } else {
3150 __ CmpLeS(FTMP, rhs, lhs);
3151 }
3152 __ Bc1nez(FTMP, label);
3153 break;
3154 default:
3155 LOG(FATAL) << "Unexpected non-floating-point condition";
3156 }
3157 } else {
3158 switch (cond) {
3159 case kCondEQ:
3160 __ CeqS(0, lhs, rhs);
3161 __ Bc1t(0, label);
3162 break;
3163 case kCondNE:
3164 __ CeqS(0, lhs, rhs);
3165 __ Bc1f(0, label);
3166 break;
3167 case kCondLT:
3168 if (gt_bias) {
3169 __ ColtS(0, lhs, rhs);
3170 } else {
3171 __ CultS(0, lhs, rhs);
3172 }
3173 __ Bc1t(0, label);
3174 break;
3175 case kCondLE:
3176 if (gt_bias) {
3177 __ ColeS(0, lhs, rhs);
3178 } else {
3179 __ CuleS(0, lhs, rhs);
3180 }
3181 __ Bc1t(0, label);
3182 break;
3183 case kCondGT:
3184 if (gt_bias) {
3185 __ CultS(0, rhs, lhs);
3186 } else {
3187 __ ColtS(0, rhs, lhs);
3188 }
3189 __ Bc1t(0, label);
3190 break;
3191 case kCondGE:
3192 if (gt_bias) {
3193 __ CuleS(0, rhs, lhs);
3194 } else {
3195 __ ColeS(0, rhs, lhs);
3196 }
3197 __ Bc1t(0, label);
3198 break;
3199 default:
3200 LOG(FATAL) << "Unexpected non-floating-point condition";
3201 }
3202 }
3203 } else {
3204 DCHECK_EQ(type, Primitive::kPrimDouble);
3205 if (isR6) {
3206 switch (cond) {
3207 case kCondEQ:
3208 __ CmpEqD(FTMP, lhs, rhs);
3209 __ Bc1nez(FTMP, label);
3210 break;
3211 case kCondNE:
3212 __ CmpEqD(FTMP, lhs, rhs);
3213 __ Bc1eqz(FTMP, label);
3214 break;
3215 case kCondLT:
3216 if (gt_bias) {
3217 __ CmpLtD(FTMP, lhs, rhs);
3218 } else {
3219 __ CmpUltD(FTMP, lhs, rhs);
3220 }
3221 __ Bc1nez(FTMP, label);
3222 break;
3223 case kCondLE:
3224 if (gt_bias) {
3225 __ CmpLeD(FTMP, lhs, rhs);
3226 } else {
3227 __ CmpUleD(FTMP, lhs, rhs);
3228 }
3229 __ Bc1nez(FTMP, label);
3230 break;
3231 case kCondGT:
3232 if (gt_bias) {
3233 __ CmpUltD(FTMP, rhs, lhs);
3234 } else {
3235 __ CmpLtD(FTMP, rhs, lhs);
3236 }
3237 __ Bc1nez(FTMP, label);
3238 break;
3239 case kCondGE:
3240 if (gt_bias) {
3241 __ CmpUleD(FTMP, rhs, lhs);
3242 } else {
3243 __ CmpLeD(FTMP, rhs, lhs);
3244 }
3245 __ Bc1nez(FTMP, label);
3246 break;
3247 default:
3248 LOG(FATAL) << "Unexpected non-floating-point condition";
3249 }
3250 } else {
3251 switch (cond) {
3252 case kCondEQ:
3253 __ CeqD(0, lhs, rhs);
3254 __ Bc1t(0, label);
3255 break;
3256 case kCondNE:
3257 __ CeqD(0, lhs, rhs);
3258 __ Bc1f(0, label);
3259 break;
3260 case kCondLT:
3261 if (gt_bias) {
3262 __ ColtD(0, lhs, rhs);
3263 } else {
3264 __ CultD(0, lhs, rhs);
3265 }
3266 __ Bc1t(0, label);
3267 break;
3268 case kCondLE:
3269 if (gt_bias) {
3270 __ ColeD(0, lhs, rhs);
3271 } else {
3272 __ CuleD(0, lhs, rhs);
3273 }
3274 __ Bc1t(0, label);
3275 break;
3276 case kCondGT:
3277 if (gt_bias) {
3278 __ CultD(0, rhs, lhs);
3279 } else {
3280 __ ColtD(0, rhs, lhs);
3281 }
3282 __ Bc1t(0, label);
3283 break;
3284 case kCondGE:
3285 if (gt_bias) {
3286 __ CuleD(0, rhs, lhs);
3287 } else {
3288 __ ColeD(0, rhs, lhs);
3289 }
3290 __ Bc1t(0, label);
3291 break;
3292 default:
3293 LOG(FATAL) << "Unexpected non-floating-point condition";
3294 }
3295 }
3296 }
3297}
3298
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003299void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003300 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003301 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003302 MipsLabel* false_target) {
3303 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003304
David Brazdil0debae72015-11-12 18:37:00 +00003305 if (true_target == nullptr && false_target == nullptr) {
3306 // Nothing to do. The code always falls through.
3307 return;
3308 } else if (cond->IsIntConstant()) {
3309 // Constant condition, statically compared against 1.
3310 if (cond->AsIntConstant()->IsOne()) {
3311 if (true_target != nullptr) {
3312 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003313 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003314 } else {
David Brazdil0debae72015-11-12 18:37:00 +00003315 DCHECK(cond->AsIntConstant()->IsZero());
3316 if (false_target != nullptr) {
3317 __ B(false_target);
3318 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003319 }
David Brazdil0debae72015-11-12 18:37:00 +00003320 return;
3321 }
3322
3323 // The following code generates these patterns:
3324 // (1) true_target == nullptr && false_target != nullptr
3325 // - opposite condition true => branch to false_target
3326 // (2) true_target != nullptr && false_target == nullptr
3327 // - condition true => branch to true_target
3328 // (3) true_target != nullptr && false_target != nullptr
3329 // - condition true => branch to true_target
3330 // - branch to false_target
3331 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003332 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003333 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003334 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003335 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003336 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3337 } else {
3338 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3339 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003340 } else {
3341 // The condition instruction has not been materialized, use its inputs as
3342 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003343 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003344 Primitive::Type type = condition->InputAt(0)->GetType();
3345 LocationSummary* locations = cond->GetLocations();
3346 IfCondition if_cond = condition->GetCondition();
3347 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003348
David Brazdil0debae72015-11-12 18:37:00 +00003349 if (true_target == nullptr) {
3350 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003351 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003352 }
3353
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003354 switch (type) {
3355 default:
3356 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3357 break;
3358 case Primitive::kPrimLong:
3359 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3360 break;
3361 case Primitive::kPrimFloat:
3362 case Primitive::kPrimDouble:
3363 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3364 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003365 }
3366 }
David Brazdil0debae72015-11-12 18:37:00 +00003367
3368 // If neither branch falls through (case 3), the conditional branch to `true_target`
3369 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3370 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003371 __ B(false_target);
3372 }
3373}
3374
3375void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3376 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003377 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003378 locations->SetInAt(0, Location::RequiresRegister());
3379 }
3380}
3381
3382void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003383 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3384 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3385 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3386 nullptr : codegen_->GetLabelOf(true_successor);
3387 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3388 nullptr : codegen_->GetLabelOf(false_successor);
3389 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003390}
3391
3392void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3393 LocationSummary* locations = new (GetGraph()->GetArena())
3394 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003395 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003396 locations->SetInAt(0, Location::RequiresRegister());
3397 }
3398}
3399
3400void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003401 SlowPathCodeMIPS* slow_path =
3402 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003403 GenerateTestAndBranch(deoptimize,
3404 /* condition_input_index */ 0,
3405 slow_path->GetEntryLabel(),
3406 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003407}
3408
David Brazdil74eb1b22015-12-14 11:44:01 +00003409void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3410 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3411 if (Primitive::IsFloatingPointType(select->GetType())) {
3412 locations->SetInAt(0, Location::RequiresFpuRegister());
3413 locations->SetInAt(1, Location::RequiresFpuRegister());
3414 } else {
3415 locations->SetInAt(0, Location::RequiresRegister());
3416 locations->SetInAt(1, Location::RequiresRegister());
3417 }
3418 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3419 locations->SetInAt(2, Location::RequiresRegister());
3420 }
3421 locations->SetOut(Location::SameAsFirstInput());
3422}
3423
3424void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3425 LocationSummary* locations = select->GetLocations();
3426 MipsLabel false_target;
3427 GenerateTestAndBranch(select,
3428 /* condition_input_index */ 2,
3429 /* true_target */ nullptr,
3430 &false_target);
3431 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3432 __ Bind(&false_target);
3433}
3434
David Srbecky0cf44932015-12-09 14:09:59 +00003435void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3436 new (GetGraph()->GetArena()) LocationSummary(info);
3437}
3438
3439void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00003440 if (codegen_->HasStackMapAtCurrentPc()) {
3441 // Ensure that we do not collide with the stack map of the previous instruction.
3442 __ Nop();
3443 }
David Srbecky0cf44932015-12-09 14:09:59 +00003444 codegen_->RecordPcInfo(info, info->GetDexPc());
3445}
3446
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003447void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3448 Primitive::Type field_type = field_info.GetFieldType();
3449 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3450 bool generate_volatile = field_info.IsVolatile() && is_wide;
3451 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3452 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3453
3454 locations->SetInAt(0, Location::RequiresRegister());
3455 if (generate_volatile) {
3456 InvokeRuntimeCallingConvention calling_convention;
3457 // need A0 to hold base + offset
3458 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3459 if (field_type == Primitive::kPrimLong) {
3460 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3461 } else {
3462 locations->SetOut(Location::RequiresFpuRegister());
3463 // Need some temp core regs since FP results are returned in core registers
3464 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3465 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3466 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3467 }
3468 } else {
3469 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3470 locations->SetOut(Location::RequiresFpuRegister());
3471 } else {
3472 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3473 }
3474 }
3475}
3476
3477void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3478 const FieldInfo& field_info,
3479 uint32_t dex_pc) {
3480 Primitive::Type type = field_info.GetFieldType();
3481 LocationSummary* locations = instruction->GetLocations();
3482 Register obj = locations->InAt(0).AsRegister<Register>();
3483 LoadOperandType load_type = kLoadUnsignedByte;
3484 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003485 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003486
3487 switch (type) {
3488 case Primitive::kPrimBoolean:
3489 load_type = kLoadUnsignedByte;
3490 break;
3491 case Primitive::kPrimByte:
3492 load_type = kLoadSignedByte;
3493 break;
3494 case Primitive::kPrimShort:
3495 load_type = kLoadSignedHalfword;
3496 break;
3497 case Primitive::kPrimChar:
3498 load_type = kLoadUnsignedHalfword;
3499 break;
3500 case Primitive::kPrimInt:
3501 case Primitive::kPrimFloat:
3502 case Primitive::kPrimNot:
3503 load_type = kLoadWord;
3504 break;
3505 case Primitive::kPrimLong:
3506 case Primitive::kPrimDouble:
3507 load_type = kLoadDoubleword;
3508 break;
3509 case Primitive::kPrimVoid:
3510 LOG(FATAL) << "Unreachable type " << type;
3511 UNREACHABLE();
3512 }
3513
3514 if (is_volatile && load_type == kLoadDoubleword) {
3515 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003516 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003517 // Do implicit Null check
3518 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3519 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3520 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3521 instruction,
3522 dex_pc,
3523 nullptr,
3524 IsDirectEntrypoint(kQuickA64Load));
3525 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3526 if (type == Primitive::kPrimDouble) {
3527 // Need to move to FP regs since FP results are returned in core registers.
3528 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
3529 locations->Out().AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003530 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3531 locations->Out().AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003532 }
3533 } else {
3534 if (!Primitive::IsFloatingPointType(type)) {
3535 Register dst;
3536 if (type == Primitive::kPrimLong) {
3537 DCHECK(locations->Out().IsRegisterPair());
3538 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003539 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3540 if (obj == dst) {
3541 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3542 codegen_->MaybeRecordImplicitNullCheck(instruction);
3543 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3544 } else {
3545 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3546 codegen_->MaybeRecordImplicitNullCheck(instruction);
3547 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3548 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003549 } else {
3550 DCHECK(locations->Out().IsRegister());
3551 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003552 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003553 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003554 } else {
3555 DCHECK(locations->Out().IsFpuRegister());
3556 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3557 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003558 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003559 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003560 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003561 }
3562 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003563 // Longs are handled earlier.
3564 if (type != Primitive::kPrimLong) {
3565 codegen_->MaybeRecordImplicitNullCheck(instruction);
3566 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003567 }
3568
3569 if (is_volatile) {
3570 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3571 }
3572}
3573
3574void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3575 Primitive::Type field_type = field_info.GetFieldType();
3576 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3577 bool generate_volatile = field_info.IsVolatile() && is_wide;
3578 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3579 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3580
3581 locations->SetInAt(0, Location::RequiresRegister());
3582 if (generate_volatile) {
3583 InvokeRuntimeCallingConvention calling_convention;
3584 // need A0 to hold base + offset
3585 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3586 if (field_type == Primitive::kPrimLong) {
3587 locations->SetInAt(1, Location::RegisterPairLocation(
3588 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3589 } else {
3590 locations->SetInAt(1, Location::RequiresFpuRegister());
3591 // Pass FP parameters in core registers.
3592 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3593 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3594 }
3595 } else {
3596 if (Primitive::IsFloatingPointType(field_type)) {
3597 locations->SetInAt(1, Location::RequiresFpuRegister());
3598 } else {
3599 locations->SetInAt(1, Location::RequiresRegister());
3600 }
3601 }
3602}
3603
3604void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3605 const FieldInfo& field_info,
3606 uint32_t dex_pc) {
3607 Primitive::Type type = field_info.GetFieldType();
3608 LocationSummary* locations = instruction->GetLocations();
3609 Register obj = locations->InAt(0).AsRegister<Register>();
3610 StoreOperandType store_type = kStoreByte;
3611 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003612 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003613
3614 switch (type) {
3615 case Primitive::kPrimBoolean:
3616 case Primitive::kPrimByte:
3617 store_type = kStoreByte;
3618 break;
3619 case Primitive::kPrimShort:
3620 case Primitive::kPrimChar:
3621 store_type = kStoreHalfword;
3622 break;
3623 case Primitive::kPrimInt:
3624 case Primitive::kPrimFloat:
3625 case Primitive::kPrimNot:
3626 store_type = kStoreWord;
3627 break;
3628 case Primitive::kPrimLong:
3629 case Primitive::kPrimDouble:
3630 store_type = kStoreDoubleword;
3631 break;
3632 case Primitive::kPrimVoid:
3633 LOG(FATAL) << "Unreachable type " << type;
3634 UNREACHABLE();
3635 }
3636
3637 if (is_volatile) {
3638 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3639 }
3640
3641 if (is_volatile && store_type == kStoreDoubleword) {
3642 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003643 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003644 // Do implicit Null check.
3645 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3646 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3647 if (type == Primitive::kPrimDouble) {
3648 // Pass FP parameters in core registers.
3649 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
3650 locations->InAt(1).AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003651 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3652 locations->InAt(1).AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003653 }
3654 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3655 instruction,
3656 dex_pc,
3657 nullptr,
3658 IsDirectEntrypoint(kQuickA64Store));
3659 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3660 } else {
3661 if (!Primitive::IsFloatingPointType(type)) {
3662 Register src;
3663 if (type == Primitive::kPrimLong) {
3664 DCHECK(locations->InAt(1).IsRegisterPair());
3665 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003666 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3667 __ StoreToOffset(kStoreWord, src, obj, offset);
3668 codegen_->MaybeRecordImplicitNullCheck(instruction);
3669 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003670 } else {
3671 DCHECK(locations->InAt(1).IsRegister());
3672 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003673 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003674 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003675 } else {
3676 DCHECK(locations->InAt(1).IsFpuRegister());
3677 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3678 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003679 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003680 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003681 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003682 }
3683 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003684 // Longs are handled earlier.
3685 if (type != Primitive::kPrimLong) {
3686 codegen_->MaybeRecordImplicitNullCheck(instruction);
3687 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003688 }
3689
3690 // TODO: memory barriers?
3691 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3692 DCHECK(locations->InAt(1).IsRegister());
3693 Register src = locations->InAt(1).AsRegister<Register>();
3694 codegen_->MarkGCCard(obj, src);
3695 }
3696
3697 if (is_volatile) {
3698 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3699 }
3700}
3701
3702void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3703 HandleFieldGet(instruction, instruction->GetFieldInfo());
3704}
3705
3706void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3707 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3708}
3709
3710void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3711 HandleFieldSet(instruction, instruction->GetFieldInfo());
3712}
3713
3714void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3715 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3716}
3717
3718void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3719 LocationSummary::CallKind call_kind =
3720 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3721 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3722 locations->SetInAt(0, Location::RequiresRegister());
3723 locations->SetInAt(1, Location::RequiresRegister());
3724 // The output does overlap inputs.
3725 // Note that TypeCheckSlowPathMIPS uses this register too.
3726 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3727}
3728
3729void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3730 LocationSummary* locations = instruction->GetLocations();
3731 Register obj = locations->InAt(0).AsRegister<Register>();
3732 Register cls = locations->InAt(1).AsRegister<Register>();
3733 Register out = locations->Out().AsRegister<Register>();
3734
3735 MipsLabel done;
3736
3737 // Return 0 if `obj` is null.
3738 // TODO: Avoid this check if we know `obj` is not null.
3739 __ Move(out, ZERO);
3740 __ Beqz(obj, &done);
3741
3742 // Compare the class of `obj` with `cls`.
3743 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3744 if (instruction->IsExactCheck()) {
3745 // Classes must be equal for the instanceof to succeed.
3746 __ Xor(out, out, cls);
3747 __ Sltiu(out, out, 1);
3748 } else {
3749 // If the classes are not equal, we go into a slow path.
3750 DCHECK(locations->OnlyCallsOnSlowPath());
3751 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3752 codegen_->AddSlowPath(slow_path);
3753 __ Bne(out, cls, slow_path->GetEntryLabel());
3754 __ LoadConst32(out, 1);
3755 __ Bind(slow_path->GetExitLabel());
3756 }
3757
3758 __ Bind(&done);
3759}
3760
3761void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3762 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3763 locations->SetOut(Location::ConstantLocation(constant));
3764}
3765
3766void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3767 // Will be generated at use site.
3768}
3769
3770void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3771 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3772 locations->SetOut(Location::ConstantLocation(constant));
3773}
3774
3775void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3776 // Will be generated at use site.
3777}
3778
3779void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3780 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3781 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3782}
3783
3784void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3785 HandleInvoke(invoke);
3786 // The register T0 is required to be used for the hidden argument in
3787 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3788 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3789}
3790
3791void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3792 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3793 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3794 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3795 invoke->GetImtIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
3796 Location receiver = invoke->GetLocations()->InAt(0);
3797 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3798 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3799
3800 // Set the hidden argument.
3801 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3802 invoke->GetDexMethodIndex());
3803
3804 // temp = object->GetClass();
3805 if (receiver.IsStackSlot()) {
3806 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3807 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3808 } else {
3809 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3810 }
3811 codegen_->MaybeRecordImplicitNullCheck(invoke);
3812 // temp = temp->GetImtEntryAt(method_offset);
3813 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3814 // T9 = temp->GetEntryPoint();
3815 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3816 // T9();
3817 __ Jalr(T9);
3818 __ Nop();
3819 DCHECK(!codegen_->IsLeafMethod());
3820 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3821}
3822
3823void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003824 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3825 if (intrinsic.TryDispatch(invoke)) {
3826 return;
3827 }
3828
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003829 HandleInvoke(invoke);
3830}
3831
3832void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003833 // Explicit clinit checks triggered by static invokes must have been pruned by
3834 // art::PrepareForRegisterAllocation.
3835 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003836
Chris Larsen701566a2015-10-27 15:29:13 -07003837 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3838 if (intrinsic.TryDispatch(invoke)) {
3839 return;
3840 }
3841
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003842 HandleInvoke(invoke);
3843}
3844
Chris Larsen701566a2015-10-27 15:29:13 -07003845static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003846 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003847 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3848 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003849 return true;
3850 }
3851 return false;
3852}
3853
Vladimir Markodc151b22015-10-15 18:02:30 +01003854HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3855 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3856 MethodReference target_method ATTRIBUTE_UNUSED) {
3857 switch (desired_dispatch_info.method_load_kind) {
3858 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3859 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3860 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3861 return HInvokeStaticOrDirect::DispatchInfo {
3862 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3863 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3864 0u,
3865 0u
3866 };
3867 default:
3868 break;
3869 }
3870 switch (desired_dispatch_info.code_ptr_location) {
3871 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3872 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3873 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3874 return HInvokeStaticOrDirect::DispatchInfo {
3875 desired_dispatch_info.method_load_kind,
3876 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3877 desired_dispatch_info.method_load_data,
3878 0u
3879 };
3880 default:
3881 return desired_dispatch_info;
3882 }
3883}
3884
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003885void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3886 // All registers are assumed to be correctly set up per the calling convention.
3887
3888 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3889 switch (invoke->GetMethodLoadKind()) {
3890 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3891 // temp = thread->string_init_entrypoint
3892 __ LoadFromOffset(kLoadWord,
3893 temp.AsRegister<Register>(),
3894 TR,
3895 invoke->GetStringInitOffset());
3896 break;
3897 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003898 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003899 break;
3900 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3901 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3902 break;
3903 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003904 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003905 // TODO: Implement these types.
3906 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3907 LOG(FATAL) << "Unsupported";
3908 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003909 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003910 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003911 Register reg = temp.AsRegister<Register>();
3912 Register method_reg;
3913 if (current_method.IsRegister()) {
3914 method_reg = current_method.AsRegister<Register>();
3915 } else {
3916 // TODO: use the appropriate DCHECK() here if possible.
3917 // DCHECK(invoke->GetLocations()->Intrinsified());
3918 DCHECK(!current_method.IsValid());
3919 method_reg = reg;
3920 __ Lw(reg, SP, kCurrentMethodStackOffset);
3921 }
3922
3923 // temp = temp->dex_cache_resolved_methods_;
3924 __ LoadFromOffset(kLoadWord,
3925 reg,
3926 method_reg,
3927 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
3928 // temp = temp[index_in_cache]
3929 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3930 __ LoadFromOffset(kLoadWord,
3931 reg,
3932 reg,
3933 CodeGenerator::GetCachePointerOffset(index_in_cache));
3934 break;
3935 }
3936 }
3937
3938 switch (invoke->GetCodePtrLocation()) {
3939 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3940 __ Jalr(&frame_entry_label_, T9);
3941 break;
3942 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3943 // LR = invoke->GetDirectCodePtr();
3944 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3945 // LR()
3946 __ Jalr(T9);
3947 __ Nop();
3948 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003949 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003950 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3951 // TODO: Implement these types.
3952 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3953 LOG(FATAL) << "Unsupported";
3954 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003955 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3956 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01003957 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003958 T9,
3959 callee_method.AsRegister<Register>(),
3960 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3961 kMipsWordSize).Int32Value());
3962 // T9()
3963 __ Jalr(T9);
3964 __ Nop();
3965 break;
3966 }
3967 DCHECK(!IsLeafMethod());
3968}
3969
3970void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003971 // Explicit clinit checks triggered by static invokes must have been pruned by
3972 // art::PrepareForRegisterAllocation.
3973 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003974
3975 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3976 return;
3977 }
3978
3979 LocationSummary* locations = invoke->GetLocations();
3980 codegen_->GenerateStaticOrDirectCall(invoke,
3981 locations->HasTemps()
3982 ? locations->GetTemp(0)
3983 : Location::NoLocation());
3984 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3985}
3986
3987void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003988 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3989 return;
3990 }
3991
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003992 LocationSummary* locations = invoke->GetLocations();
3993 Location receiver = locations->InAt(0);
3994 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3995 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3996 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
3997 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3998 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3999
4000 // temp = object->GetClass();
4001 if (receiver.IsStackSlot()) {
4002 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
4003 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
4004 } else {
4005 DCHECK(receiver.IsRegister());
4006 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4007 }
4008 codegen_->MaybeRecordImplicitNullCheck(invoke);
4009 // temp = temp->GetMethodAt(method_offset);
4010 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4011 // T9 = temp->GetEntryPoint();
4012 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4013 // T9();
4014 __ Jalr(T9);
4015 __ Nop();
4016 DCHECK(!codegen_->IsLeafMethod());
4017 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4018}
4019
4020void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01004021 InvokeRuntimeCallingConvention calling_convention;
4022 CodeGenerator::CreateLoadClassLocationSummary(
4023 cls,
4024 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4025 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004026}
4027
4028void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4029 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004030 if (cls->NeedsAccessCheck()) {
4031 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4032 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4033 cls,
4034 cls->GetDexPc(),
4035 nullptr,
4036 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00004037 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004038 return;
4039 }
4040
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004041 Register out = locations->Out().AsRegister<Register>();
4042 Register current_method = locations->InAt(0).AsRegister<Register>();
4043 if (cls->IsReferrersClass()) {
4044 DCHECK(!cls->CanCallRuntime());
4045 DCHECK(!cls->MustGenerateClinitCheck());
4046 __ LoadFromOffset(kLoadWord, out, current_method,
4047 ArtMethod::DeclaringClassOffset().Int32Value());
4048 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004049 __ LoadFromOffset(kLoadWord, out, current_method,
4050 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
4051 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004052
4053 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
4054 DCHECK(cls->CanCallRuntime());
4055 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4056 cls,
4057 cls,
4058 cls->GetDexPc(),
4059 cls->MustGenerateClinitCheck());
4060 codegen_->AddSlowPath(slow_path);
4061 if (!cls->IsInDexCache()) {
4062 __ Beqz(out, slow_path->GetEntryLabel());
4063 }
4064 if (cls->MustGenerateClinitCheck()) {
4065 GenerateClassInitializationCheck(slow_path, out);
4066 } else {
4067 __ Bind(slow_path->GetExitLabel());
4068 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004069 }
4070 }
4071}
4072
4073static int32_t GetExceptionTlsOffset() {
4074 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
4075}
4076
4077void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4078 LocationSummary* locations =
4079 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4080 locations->SetOut(Location::RequiresRegister());
4081}
4082
4083void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4084 Register out = load->GetLocations()->Out().AsRegister<Register>();
4085 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4086}
4087
4088void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4089 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4090}
4091
4092void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4093 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4094}
4095
4096void LocationsBuilderMIPS::VisitLoadLocal(HLoadLocal* load) {
4097 load->SetLocations(nullptr);
4098}
4099
4100void InstructionCodeGeneratorMIPS::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
4101 // Nothing to do, this is driven by the code generator.
4102}
4103
4104void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00004105 LocationSummary::CallKind call_kind = load->IsInDexCache()
4106 ? LocationSummary::kNoCall
4107 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004108 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004109 locations->SetInAt(0, Location::RequiresRegister());
4110 locations->SetOut(Location::RequiresRegister());
4111}
4112
4113void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004114 LocationSummary* locations = load->GetLocations();
4115 Register out = locations->Out().AsRegister<Register>();
4116 Register current_method = locations->InAt(0).AsRegister<Register>();
4117 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4118 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4119 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004120
4121 if (!load->IsInDexCache()) {
4122 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4123 codegen_->AddSlowPath(slow_path);
4124 __ Beqz(out, slow_path->GetEntryLabel());
4125 __ Bind(slow_path->GetExitLabel());
4126 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004127}
4128
4129void LocationsBuilderMIPS::VisitLocal(HLocal* local) {
4130 local->SetLocations(nullptr);
4131}
4132
4133void InstructionCodeGeneratorMIPS::VisitLocal(HLocal* local) {
4134 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
4135}
4136
4137void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4138 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4139 locations->SetOut(Location::ConstantLocation(constant));
4140}
4141
4142void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4143 // Will be generated at use site.
4144}
4145
4146void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4147 LocationSummary* locations =
4148 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4149 InvokeRuntimeCallingConvention calling_convention;
4150 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4151}
4152
4153void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4154 if (instruction->IsEnter()) {
4155 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4156 instruction,
4157 instruction->GetDexPc(),
4158 nullptr,
4159 IsDirectEntrypoint(kQuickLockObject));
4160 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4161 } else {
4162 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4163 instruction,
4164 instruction->GetDexPc(),
4165 nullptr,
4166 IsDirectEntrypoint(kQuickUnlockObject));
4167 }
4168 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4169}
4170
4171void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4172 LocationSummary* locations =
4173 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4174 switch (mul->GetResultType()) {
4175 case Primitive::kPrimInt:
4176 case Primitive::kPrimLong:
4177 locations->SetInAt(0, Location::RequiresRegister());
4178 locations->SetInAt(1, Location::RequiresRegister());
4179 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4180 break;
4181
4182 case Primitive::kPrimFloat:
4183 case Primitive::kPrimDouble:
4184 locations->SetInAt(0, Location::RequiresFpuRegister());
4185 locations->SetInAt(1, Location::RequiresFpuRegister());
4186 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4187 break;
4188
4189 default:
4190 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4191 }
4192}
4193
4194void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4195 Primitive::Type type = instruction->GetType();
4196 LocationSummary* locations = instruction->GetLocations();
4197 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4198
4199 switch (type) {
4200 case Primitive::kPrimInt: {
4201 Register dst = locations->Out().AsRegister<Register>();
4202 Register lhs = locations->InAt(0).AsRegister<Register>();
4203 Register rhs = locations->InAt(1).AsRegister<Register>();
4204
4205 if (isR6) {
4206 __ MulR6(dst, lhs, rhs);
4207 } else {
4208 __ MulR2(dst, lhs, rhs);
4209 }
4210 break;
4211 }
4212 case Primitive::kPrimLong: {
4213 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4214 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4215 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4216 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4217 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4218 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4219
4220 // Extra checks to protect caused by the existance of A1_A2.
4221 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4222 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4223 DCHECK_NE(dst_high, lhs_low);
4224 DCHECK_NE(dst_high, rhs_low);
4225
4226 // A_B * C_D
4227 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4228 // dst_lo: [ low(B*D) ]
4229 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4230
4231 if (isR6) {
4232 __ MulR6(TMP, lhs_high, rhs_low);
4233 __ MulR6(dst_high, lhs_low, rhs_high);
4234 __ Addu(dst_high, dst_high, TMP);
4235 __ MuhuR6(TMP, lhs_low, rhs_low);
4236 __ Addu(dst_high, dst_high, TMP);
4237 __ MulR6(dst_low, lhs_low, rhs_low);
4238 } else {
4239 __ MulR2(TMP, lhs_high, rhs_low);
4240 __ MulR2(dst_high, lhs_low, rhs_high);
4241 __ Addu(dst_high, dst_high, TMP);
4242 __ MultuR2(lhs_low, rhs_low);
4243 __ Mfhi(TMP);
4244 __ Addu(dst_high, dst_high, TMP);
4245 __ Mflo(dst_low);
4246 }
4247 break;
4248 }
4249 case Primitive::kPrimFloat:
4250 case Primitive::kPrimDouble: {
4251 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4252 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4253 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4254 if (type == Primitive::kPrimFloat) {
4255 __ MulS(dst, lhs, rhs);
4256 } else {
4257 __ MulD(dst, lhs, rhs);
4258 }
4259 break;
4260 }
4261 default:
4262 LOG(FATAL) << "Unexpected mul type " << type;
4263 }
4264}
4265
4266void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4267 LocationSummary* locations =
4268 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4269 switch (neg->GetResultType()) {
4270 case Primitive::kPrimInt:
4271 case Primitive::kPrimLong:
4272 locations->SetInAt(0, Location::RequiresRegister());
4273 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4274 break;
4275
4276 case Primitive::kPrimFloat:
4277 case Primitive::kPrimDouble:
4278 locations->SetInAt(0, Location::RequiresFpuRegister());
4279 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4280 break;
4281
4282 default:
4283 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4284 }
4285}
4286
4287void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4288 Primitive::Type type = instruction->GetType();
4289 LocationSummary* locations = instruction->GetLocations();
4290
4291 switch (type) {
4292 case Primitive::kPrimInt: {
4293 Register dst = locations->Out().AsRegister<Register>();
4294 Register src = locations->InAt(0).AsRegister<Register>();
4295 __ Subu(dst, ZERO, src);
4296 break;
4297 }
4298 case Primitive::kPrimLong: {
4299 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4300 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4301 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4302 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4303 __ Subu(dst_low, ZERO, src_low);
4304 __ Sltu(TMP, ZERO, dst_low);
4305 __ Subu(dst_high, ZERO, src_high);
4306 __ Subu(dst_high, dst_high, TMP);
4307 break;
4308 }
4309 case Primitive::kPrimFloat:
4310 case Primitive::kPrimDouble: {
4311 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4312 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4313 if (type == Primitive::kPrimFloat) {
4314 __ NegS(dst, src);
4315 } else {
4316 __ NegD(dst, src);
4317 }
4318 break;
4319 }
4320 default:
4321 LOG(FATAL) << "Unexpected neg type " << type;
4322 }
4323}
4324
4325void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4326 LocationSummary* locations =
4327 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4328 InvokeRuntimeCallingConvention calling_convention;
4329 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4330 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4331 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4332 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4333}
4334
4335void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4336 InvokeRuntimeCallingConvention calling_convention;
4337 Register current_method_register = calling_convention.GetRegisterAt(2);
4338 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4339 // Move an uint16_t value to a register.
4340 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4341 codegen_->InvokeRuntime(
4342 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4343 instruction,
4344 instruction->GetDexPc(),
4345 nullptr,
4346 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4347 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4348 void*, uint32_t, int32_t, ArtMethod*>();
4349}
4350
4351void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4352 LocationSummary* locations =
4353 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4354 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004355 if (instruction->IsStringAlloc()) {
4356 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4357 } else {
4358 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4359 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4360 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004361 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4362}
4363
4364void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004365 if (instruction->IsStringAlloc()) {
4366 // String is allocated through StringFactory. Call NewEmptyString entry point.
4367 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4368 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4369 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4370 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4371 __ Jalr(T9);
4372 __ Nop();
4373 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4374 } else {
4375 codegen_->InvokeRuntime(
4376 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4377 instruction,
4378 instruction->GetDexPc(),
4379 nullptr,
4380 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4381 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4382 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004383}
4384
4385void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4386 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4387 locations->SetInAt(0, Location::RequiresRegister());
4388 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4389}
4390
4391void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4392 Primitive::Type type = instruction->GetType();
4393 LocationSummary* locations = instruction->GetLocations();
4394
4395 switch (type) {
4396 case Primitive::kPrimInt: {
4397 Register dst = locations->Out().AsRegister<Register>();
4398 Register src = locations->InAt(0).AsRegister<Register>();
4399 __ Nor(dst, src, ZERO);
4400 break;
4401 }
4402
4403 case Primitive::kPrimLong: {
4404 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4405 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4406 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4407 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4408 __ Nor(dst_high, src_high, ZERO);
4409 __ Nor(dst_low, src_low, ZERO);
4410 break;
4411 }
4412
4413 default:
4414 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4415 }
4416}
4417
4418void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4419 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4420 locations->SetInAt(0, Location::RequiresRegister());
4421 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4422}
4423
4424void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4425 LocationSummary* locations = instruction->GetLocations();
4426 __ Xori(locations->Out().AsRegister<Register>(),
4427 locations->InAt(0).AsRegister<Register>(),
4428 1);
4429}
4430
4431void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4432 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4433 ? LocationSummary::kCallOnSlowPath
4434 : LocationSummary::kNoCall;
4435 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4436 locations->SetInAt(0, Location::RequiresRegister());
4437 if (instruction->HasUses()) {
4438 locations->SetOut(Location::SameAsFirstInput());
4439 }
4440}
4441
4442void InstructionCodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4443 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4444 return;
4445 }
4446 Location obj = instruction->GetLocations()->InAt(0);
4447
4448 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
4449 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4450}
4451
4452void InstructionCodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
4453 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
4454 codegen_->AddSlowPath(slow_path);
4455
4456 Location obj = instruction->GetLocations()->InAt(0);
4457
4458 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4459}
4460
4461void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
4462 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
4463 GenerateImplicitNullCheck(instruction);
4464 } else {
4465 GenerateExplicitNullCheck(instruction);
4466 }
4467}
4468
4469void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4470 HandleBinaryOp(instruction);
4471}
4472
4473void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4474 HandleBinaryOp(instruction);
4475}
4476
4477void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4478 LOG(FATAL) << "Unreachable";
4479}
4480
4481void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4482 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4483}
4484
4485void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4486 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4487 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4488 if (location.IsStackSlot()) {
4489 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4490 } else if (location.IsDoubleStackSlot()) {
4491 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4492 }
4493 locations->SetOut(location);
4494}
4495
4496void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4497 ATTRIBUTE_UNUSED) {
4498 // Nothing to do, the parameter is already at its location.
4499}
4500
4501void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4502 LocationSummary* locations =
4503 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4504 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4505}
4506
4507void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4508 ATTRIBUTE_UNUSED) {
4509 // Nothing to do, the method is already at its location.
4510}
4511
4512void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4514 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4515 locations->SetInAt(i, Location::Any());
4516 }
4517 locations->SetOut(Location::Any());
4518}
4519
4520void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4521 LOG(FATAL) << "Unreachable";
4522}
4523
4524void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4525 Primitive::Type type = rem->GetResultType();
4526 LocationSummary::CallKind call_kind =
4527 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCall;
4528 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4529
4530 switch (type) {
4531 case Primitive::kPrimInt:
4532 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004533 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004534 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4535 break;
4536
4537 case Primitive::kPrimLong: {
4538 InvokeRuntimeCallingConvention calling_convention;
4539 locations->SetInAt(0, Location::RegisterPairLocation(
4540 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4541 locations->SetInAt(1, Location::RegisterPairLocation(
4542 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4543 locations->SetOut(calling_convention.GetReturnLocation(type));
4544 break;
4545 }
4546
4547 case Primitive::kPrimFloat:
4548 case Primitive::kPrimDouble: {
4549 InvokeRuntimeCallingConvention calling_convention;
4550 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4551 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4552 locations->SetOut(calling_convention.GetReturnLocation(type));
4553 break;
4554 }
4555
4556 default:
4557 LOG(FATAL) << "Unexpected rem type " << type;
4558 }
4559}
4560
4561void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4562 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004563
4564 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004565 case Primitive::kPrimInt:
4566 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004567 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004568 case Primitive::kPrimLong: {
4569 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
4570 instruction,
4571 instruction->GetDexPc(),
4572 nullptr,
4573 IsDirectEntrypoint(kQuickLmod));
4574 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4575 break;
4576 }
4577 case Primitive::kPrimFloat: {
4578 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
4579 instruction, instruction->GetDexPc(),
4580 nullptr,
4581 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00004582 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004583 break;
4584 }
4585 case Primitive::kPrimDouble: {
4586 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
4587 instruction, instruction->GetDexPc(),
4588 nullptr,
4589 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00004590 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004591 break;
4592 }
4593 default:
4594 LOG(FATAL) << "Unexpected rem type " << type;
4595 }
4596}
4597
4598void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4599 memory_barrier->SetLocations(nullptr);
4600}
4601
4602void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4603 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4604}
4605
4606void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
4607 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4608 Primitive::Type return_type = ret->InputAt(0)->GetType();
4609 locations->SetInAt(0, MipsReturnLocation(return_type));
4610}
4611
4612void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4613 codegen_->GenerateFrameExit();
4614}
4615
4616void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
4617 ret->SetLocations(nullptr);
4618}
4619
4620void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4621 codegen_->GenerateFrameExit();
4622}
4623
Alexey Frunze92d90602015-12-18 18:16:36 -08004624void LocationsBuilderMIPS::VisitRor(HRor* ror) {
4625 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004626}
4627
Alexey Frunze92d90602015-12-18 18:16:36 -08004628void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
4629 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004630}
4631
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004632void LocationsBuilderMIPS::VisitShl(HShl* shl) {
4633 HandleShift(shl);
4634}
4635
4636void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
4637 HandleShift(shl);
4638}
4639
4640void LocationsBuilderMIPS::VisitShr(HShr* shr) {
4641 HandleShift(shr);
4642}
4643
4644void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
4645 HandleShift(shr);
4646}
4647
4648void LocationsBuilderMIPS::VisitStoreLocal(HStoreLocal* store) {
4649 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4650 Primitive::Type field_type = store->InputAt(1)->GetType();
4651 switch (field_type) {
4652 case Primitive::kPrimNot:
4653 case Primitive::kPrimBoolean:
4654 case Primitive::kPrimByte:
4655 case Primitive::kPrimChar:
4656 case Primitive::kPrimShort:
4657 case Primitive::kPrimInt:
4658 case Primitive::kPrimFloat:
4659 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4660 break;
4661
4662 case Primitive::kPrimLong:
4663 case Primitive::kPrimDouble:
4664 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4665 break;
4666
4667 default:
4668 LOG(FATAL) << "Unimplemented local type " << field_type;
4669 }
4670}
4671
4672void InstructionCodeGeneratorMIPS::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
4673}
4674
4675void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
4676 HandleBinaryOp(instruction);
4677}
4678
4679void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
4680 HandleBinaryOp(instruction);
4681}
4682
4683void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4684 HandleFieldGet(instruction, instruction->GetFieldInfo());
4685}
4686
4687void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4688 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4689}
4690
4691void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4692 HandleFieldSet(instruction, instruction->GetFieldInfo());
4693}
4694
4695void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4696 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4697}
4698
4699void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
4700 HUnresolvedInstanceFieldGet* instruction) {
4701 FieldAccessCallingConventionMIPS calling_convention;
4702 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4703 instruction->GetFieldType(),
4704 calling_convention);
4705}
4706
4707void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
4708 HUnresolvedInstanceFieldGet* instruction) {
4709 FieldAccessCallingConventionMIPS calling_convention;
4710 codegen_->GenerateUnresolvedFieldAccess(instruction,
4711 instruction->GetFieldType(),
4712 instruction->GetFieldIndex(),
4713 instruction->GetDexPc(),
4714 calling_convention);
4715}
4716
4717void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4718 HUnresolvedInstanceFieldSet* instruction) {
4719 FieldAccessCallingConventionMIPS calling_convention;
4720 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4721 instruction->GetFieldType(),
4722 calling_convention);
4723}
4724
4725void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4726 HUnresolvedInstanceFieldSet* instruction) {
4727 FieldAccessCallingConventionMIPS calling_convention;
4728 codegen_->GenerateUnresolvedFieldAccess(instruction,
4729 instruction->GetFieldType(),
4730 instruction->GetFieldIndex(),
4731 instruction->GetDexPc(),
4732 calling_convention);
4733}
4734
4735void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4736 HUnresolvedStaticFieldGet* instruction) {
4737 FieldAccessCallingConventionMIPS calling_convention;
4738 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4739 instruction->GetFieldType(),
4740 calling_convention);
4741}
4742
4743void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4744 HUnresolvedStaticFieldGet* instruction) {
4745 FieldAccessCallingConventionMIPS calling_convention;
4746 codegen_->GenerateUnresolvedFieldAccess(instruction,
4747 instruction->GetFieldType(),
4748 instruction->GetFieldIndex(),
4749 instruction->GetDexPc(),
4750 calling_convention);
4751}
4752
4753void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4754 HUnresolvedStaticFieldSet* instruction) {
4755 FieldAccessCallingConventionMIPS calling_convention;
4756 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4757 instruction->GetFieldType(),
4758 calling_convention);
4759}
4760
4761void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4762 HUnresolvedStaticFieldSet* instruction) {
4763 FieldAccessCallingConventionMIPS calling_convention;
4764 codegen_->GenerateUnresolvedFieldAccess(instruction,
4765 instruction->GetFieldType(),
4766 instruction->GetFieldIndex(),
4767 instruction->GetDexPc(),
4768 calling_convention);
4769}
4770
4771void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4772 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4773}
4774
4775void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4776 HBasicBlock* block = instruction->GetBlock();
4777 if (block->GetLoopInformation() != nullptr) {
4778 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4779 // The back edge will generate the suspend check.
4780 return;
4781 }
4782 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4783 // The goto will generate the suspend check.
4784 return;
4785 }
4786 GenerateSuspendCheck(instruction, nullptr);
4787}
4788
4789void LocationsBuilderMIPS::VisitTemporary(HTemporary* temp) {
4790 temp->SetLocations(nullptr);
4791}
4792
4793void InstructionCodeGeneratorMIPS::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
4794 // Nothing to do, this is driven by the code generator.
4795}
4796
4797void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4798 LocationSummary* locations =
4799 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4800 InvokeRuntimeCallingConvention calling_convention;
4801 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4802}
4803
4804void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4805 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4806 instruction,
4807 instruction->GetDexPc(),
4808 nullptr,
4809 IsDirectEntrypoint(kQuickDeliverException));
4810 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4811}
4812
4813void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4814 Primitive::Type input_type = conversion->GetInputType();
4815 Primitive::Type result_type = conversion->GetResultType();
4816 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004817 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004818
4819 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4820 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4821 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4822 }
4823
4824 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004825 if (!isR6 &&
4826 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4827 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004828 call_kind = LocationSummary::kCall;
4829 }
4830
4831 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4832
4833 if (call_kind == LocationSummary::kNoCall) {
4834 if (Primitive::IsFloatingPointType(input_type)) {
4835 locations->SetInAt(0, Location::RequiresFpuRegister());
4836 } else {
4837 locations->SetInAt(0, Location::RequiresRegister());
4838 }
4839
4840 if (Primitive::IsFloatingPointType(result_type)) {
4841 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4842 } else {
4843 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4844 }
4845 } else {
4846 InvokeRuntimeCallingConvention calling_convention;
4847
4848 if (Primitive::IsFloatingPointType(input_type)) {
4849 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4850 } else {
4851 DCHECK_EQ(input_type, Primitive::kPrimLong);
4852 locations->SetInAt(0, Location::RegisterPairLocation(
4853 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4854 }
4855
4856 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4857 }
4858}
4859
4860void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4861 LocationSummary* locations = conversion->GetLocations();
4862 Primitive::Type result_type = conversion->GetResultType();
4863 Primitive::Type input_type = conversion->GetInputType();
4864 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004865 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4866 bool fpu_32bit = codegen_->GetInstructionSetFeatures().Is32BitFloatingPoint();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004867
4868 DCHECK_NE(input_type, result_type);
4869
4870 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4871 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4872 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4873 Register src = locations->InAt(0).AsRegister<Register>();
4874
4875 __ Move(dst_low, src);
4876 __ Sra(dst_high, src, 31);
4877 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4878 Register dst = locations->Out().AsRegister<Register>();
4879 Register src = (input_type == Primitive::kPrimLong)
4880 ? locations->InAt(0).AsRegisterPairLow<Register>()
4881 : locations->InAt(0).AsRegister<Register>();
4882
4883 switch (result_type) {
4884 case Primitive::kPrimChar:
4885 __ Andi(dst, src, 0xFFFF);
4886 break;
4887 case Primitive::kPrimByte:
4888 if (has_sign_extension) {
4889 __ Seb(dst, src);
4890 } else {
4891 __ Sll(dst, src, 24);
4892 __ Sra(dst, dst, 24);
4893 }
4894 break;
4895 case Primitive::kPrimShort:
4896 if (has_sign_extension) {
4897 __ Seh(dst, src);
4898 } else {
4899 __ Sll(dst, src, 16);
4900 __ Sra(dst, dst, 16);
4901 }
4902 break;
4903 case Primitive::kPrimInt:
4904 __ Move(dst, src);
4905 break;
4906
4907 default:
4908 LOG(FATAL) << "Unexpected type conversion from " << input_type
4909 << " to " << result_type;
4910 }
4911 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004912 if (input_type == Primitive::kPrimLong) {
4913 if (isR6) {
4914 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4915 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4916 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4917 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4918 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4919 __ Mtc1(src_low, FTMP);
4920 __ Mthc1(src_high, FTMP);
4921 if (result_type == Primitive::kPrimFloat) {
4922 __ Cvtsl(dst, FTMP);
4923 } else {
4924 __ Cvtdl(dst, FTMP);
4925 }
4926 } else {
4927 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4928 : QUICK_ENTRY_POINT(pL2d);
4929 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4930 : IsDirectEntrypoint(kQuickL2d);
4931 codegen_->InvokeRuntime(entry_offset,
4932 conversion,
4933 conversion->GetDexPc(),
4934 nullptr,
4935 direct);
4936 if (result_type == Primitive::kPrimFloat) {
4937 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4938 } else {
4939 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4940 }
4941 }
4942 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004943 Register src = locations->InAt(0).AsRegister<Register>();
4944 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4945 __ Mtc1(src, FTMP);
4946 if (result_type == Primitive::kPrimFloat) {
4947 __ Cvtsw(dst, FTMP);
4948 } else {
4949 __ Cvtdw(dst, FTMP);
4950 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004951 }
4952 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4953 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004954 if (result_type == Primitive::kPrimLong) {
4955 if (isR6) {
4956 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4957 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4958 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4959 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4960 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4961 MipsLabel truncate;
4962 MipsLabel done;
4963
4964 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4965 // value when the input is either a NaN or is outside of the range of the output type
4966 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4967 // the same result.
4968 //
4969 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4970 // value of the output type if the input is outside of the range after the truncation or
4971 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4972 // results. This matches the desired float/double-to-int/long conversion exactly.
4973 //
4974 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4975 //
4976 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4977 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4978 // even though it must be NAN2008=1 on R6.
4979 //
4980 // The code takes care of the different behaviors by first comparing the input to the
4981 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4982 // If the input is greater than or equal to the minimum, it procedes to the truncate
4983 // instruction, which will handle such an input the same way irrespective of NAN2008.
4984 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4985 // in order to return either zero or the minimum value.
4986 //
4987 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4988 // truncate instruction for MIPS64R6.
4989 if (input_type == Primitive::kPrimFloat) {
4990 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
4991 __ LoadConst32(TMP, min_val);
4992 __ Mtc1(TMP, FTMP);
4993 __ CmpLeS(FTMP, FTMP, src);
4994 } else {
4995 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
4996 __ LoadConst32(TMP, High32Bits(min_val));
4997 __ Mtc1(ZERO, FTMP);
4998 __ Mthc1(TMP, FTMP);
4999 __ CmpLeD(FTMP, FTMP, src);
5000 }
5001
5002 __ Bc1nez(FTMP, &truncate);
5003
5004 if (input_type == Primitive::kPrimFloat) {
5005 __ CmpEqS(FTMP, src, src);
5006 } else {
5007 __ CmpEqD(FTMP, src, src);
5008 }
5009 __ Move(dst_low, ZERO);
5010 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5011 __ Mfc1(TMP, FTMP);
5012 __ And(dst_high, dst_high, TMP);
5013
5014 __ B(&done);
5015
5016 __ Bind(&truncate);
5017
5018 if (input_type == Primitive::kPrimFloat) {
5019 __ TruncLS(FTMP, src);
5020 } else {
5021 __ TruncLD(FTMP, src);
5022 }
5023 __ Mfc1(dst_low, FTMP);
5024 __ Mfhc1(dst_high, FTMP);
5025
5026 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005027 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005028 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
5029 : QUICK_ENTRY_POINT(pD2l);
5030 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
5031 : IsDirectEntrypoint(kQuickD2l);
5032 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
5033 if (input_type == Primitive::kPrimFloat) {
5034 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5035 } else {
5036 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5037 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005038 }
5039 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005040 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5041 Register dst = locations->Out().AsRegister<Register>();
5042 MipsLabel truncate;
5043 MipsLabel done;
5044
5045 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5046 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5047 // even though it must be NAN2008=1 on R6.
5048 //
5049 // For details see the large comment above for the truncation of float/double to long on R6.
5050 //
5051 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5052 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005053 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005054 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5055 __ LoadConst32(TMP, min_val);
5056 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005057 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005058 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5059 __ LoadConst32(TMP, High32Bits(min_val));
5060 __ Mtc1(ZERO, FTMP);
5061 if (fpu_32bit) {
5062 __ Mtc1(TMP, static_cast<FRegister>(FTMP + 1));
5063 } else {
5064 __ Mthc1(TMP, FTMP);
5065 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005066 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005067
5068 if (isR6) {
5069 if (input_type == Primitive::kPrimFloat) {
5070 __ CmpLeS(FTMP, FTMP, src);
5071 } else {
5072 __ CmpLeD(FTMP, FTMP, src);
5073 }
5074 __ Bc1nez(FTMP, &truncate);
5075
5076 if (input_type == Primitive::kPrimFloat) {
5077 __ CmpEqS(FTMP, src, src);
5078 } else {
5079 __ CmpEqD(FTMP, src, src);
5080 }
5081 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5082 __ Mfc1(TMP, FTMP);
5083 __ And(dst, dst, TMP);
5084 } else {
5085 if (input_type == Primitive::kPrimFloat) {
5086 __ ColeS(0, FTMP, src);
5087 } else {
5088 __ ColeD(0, FTMP, src);
5089 }
5090 __ Bc1t(0, &truncate);
5091
5092 if (input_type == Primitive::kPrimFloat) {
5093 __ CeqS(0, src, src);
5094 } else {
5095 __ CeqD(0, src, src);
5096 }
5097 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5098 __ Movf(dst, ZERO, 0);
5099 }
5100
5101 __ B(&done);
5102
5103 __ Bind(&truncate);
5104
5105 if (input_type == Primitive::kPrimFloat) {
5106 __ TruncWS(FTMP, src);
5107 } else {
5108 __ TruncWD(FTMP, src);
5109 }
5110 __ Mfc1(dst, FTMP);
5111
5112 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005113 }
5114 } else if (Primitive::IsFloatingPointType(result_type) &&
5115 Primitive::IsFloatingPointType(input_type)) {
5116 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5117 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5118 if (result_type == Primitive::kPrimFloat) {
5119 __ Cvtsd(dst, src);
5120 } else {
5121 __ Cvtds(dst, src);
5122 }
5123 } else {
5124 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5125 << " to " << result_type;
5126 }
5127}
5128
5129void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5130 HandleShift(ushr);
5131}
5132
5133void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5134 HandleShift(ushr);
5135}
5136
5137void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5138 HandleBinaryOp(instruction);
5139}
5140
5141void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5142 HandleBinaryOp(instruction);
5143}
5144
5145void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5146 // Nothing to do, this should be removed during prepare for register allocator.
5147 LOG(FATAL) << "Unreachable";
5148}
5149
5150void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5151 // Nothing to do, this should be removed during prepare for register allocator.
5152 LOG(FATAL) << "Unreachable";
5153}
5154
5155void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005156 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005157}
5158
5159void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005160 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005161}
5162
5163void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005164 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005165}
5166
5167void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005168 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005169}
5170
5171void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005172 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005173}
5174
5175void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005176 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005177}
5178
5179void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005180 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005181}
5182
5183void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005184 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005185}
5186
5187void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005188 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005189}
5190
5191void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005192 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005193}
5194
5195void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005196 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005197}
5198
5199void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005200 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005201}
5202
5203void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005204 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005205}
5206
5207void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005208 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005209}
5210
5211void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005212 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005213}
5214
5215void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005216 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005217}
5218
5219void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005220 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005221}
5222
5223void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005224 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005225}
5226
5227void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005228 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005229}
5230
5231void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005232 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005233}
5234
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005235void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5236 LocationSummary* locations =
5237 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5238 locations->SetInAt(0, Location::RequiresRegister());
5239}
5240
5241void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5242 int32_t lower_bound = switch_instr->GetStartValue();
5243 int32_t num_entries = switch_instr->GetNumEntries();
5244 LocationSummary* locations = switch_instr->GetLocations();
5245 Register value_reg = locations->InAt(0).AsRegister<Register>();
5246 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5247
5248 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005249 Register temp_reg = TMP;
5250 __ Addiu32(temp_reg, value_reg, -lower_bound);
5251 // Jump to default if index is negative
5252 // Note: We don't check the case that index is positive while value < lower_bound, because in
5253 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5254 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5255
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005256 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005257 // Jump to successors[0] if value == lower_bound.
5258 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5259 int32_t last_index = 0;
5260 for (; num_entries - last_index > 2; last_index += 2) {
5261 __ Addiu(temp_reg, temp_reg, -2);
5262 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5263 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5264 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5265 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5266 }
5267 if (num_entries - last_index == 2) {
5268 // The last missing case_value.
5269 __ Addiu(temp_reg, temp_reg, -1);
5270 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005271 }
5272
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005273 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005274 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5275 __ B(codegen_->GetLabelOf(default_block));
5276 }
5277}
5278
5279void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5280 // The trampoline uses the same calling convention as dex calling conventions,
5281 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5282 // the method_idx.
5283 HandleInvoke(invoke);
5284}
5285
5286void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5287 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5288}
5289
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005290void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet*) {
5291 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips";
5292}
5293
5294void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet*) {
5295 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips";
5296}
5297
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005298#undef __
5299#undef QUICK_ENTRY_POINT
5300
5301} // namespace mips
5302} // namespace art