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