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