blob: fdca0b301b7916c855d1cfe296e703d43b261c75 [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);
608 __ Mfhc1(AT, f1);
609 __ Mtc1(r2_l, f1);
610 __ Mthc1(r2_h, f1);
611 __ Move(r2_l, TMP);
612 __ Move(r2_h, AT);
613 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
614 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
615 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
616 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
617 } else {
618 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
619 }
620}
621
622void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
623 __ Pop(static_cast<Register>(reg));
624}
625
626void ParallelMoveResolverMIPS::SpillScratch(int reg) {
627 __ Push(static_cast<Register>(reg));
628}
629
630void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
631 // Allocate a scratch register other than TMP, if available.
632 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
633 // automatically unspilled when the scratch scope object is destroyed).
634 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
635 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
636 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
637 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
638 __ LoadFromOffset(kLoadWord,
639 Register(ensure_scratch.GetRegister()),
640 SP,
641 index1 + stack_offset);
642 __ LoadFromOffset(kLoadWord,
643 TMP,
644 SP,
645 index2 + stack_offset);
646 __ StoreToOffset(kStoreWord,
647 Register(ensure_scratch.GetRegister()),
648 SP,
649 index2 + stack_offset);
650 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
651 }
652}
653
654static dwarf::Reg DWARFReg(Register reg) {
655 return dwarf::Reg::MipsCore(static_cast<int>(reg));
656}
657
658// TODO: mapping of floating-point registers to DWARF.
659
660void CodeGeneratorMIPS::GenerateFrameEntry() {
661 __ Bind(&frame_entry_label_);
662
663 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
664
665 if (do_overflow_check) {
666 __ LoadFromOffset(kLoadWord,
667 ZERO,
668 SP,
669 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
670 RecordPcInfo(nullptr, 0);
671 }
672
673 if (HasEmptyFrame()) {
674 return;
675 }
676
677 // Make sure the frame size isn't unreasonably large.
678 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
679 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
680 }
681
682 // Spill callee-saved registers.
683 // Note that their cumulative size is small and they can be indexed using
684 // 16-bit offsets.
685
686 // TODO: increment/decrement SP in one step instead of two or remove this comment.
687
688 uint32_t ofs = FrameEntrySpillSize();
689 bool unaligned_float = ofs & 0x7;
690 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
691 __ IncreaseFrameSize(ofs);
692
693 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
694 Register reg = kCoreCalleeSaves[i];
695 if (allocated_registers_.ContainsCoreRegister(reg)) {
696 ofs -= kMipsWordSize;
697 __ Sw(reg, SP, ofs);
698 __ cfi().RelOffset(DWARFReg(reg), ofs);
699 }
700 }
701
702 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
703 FRegister reg = kFpuCalleeSaves[i];
704 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
705 ofs -= kMipsDoublewordSize;
706 // TODO: Change the frame to avoid unaligned accesses for fpu registers.
707 if (unaligned_float) {
708 if (fpu_32bit) {
709 __ Swc1(reg, SP, ofs);
710 __ Swc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
711 } else {
712 __ Mfhc1(TMP, reg);
713 __ Swc1(reg, SP, ofs);
714 __ Sw(TMP, SP, ofs + 4);
715 }
716 } else {
717 __ Sdc1(reg, SP, ofs);
718 }
719 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
720 }
721 }
722
723 // Allocate the rest of the frame and store the current method pointer
724 // at its end.
725
726 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
727
728 static_assert(IsInt<16>(kCurrentMethodStackOffset),
729 "kCurrentMethodStackOffset must fit into int16_t");
730 __ Sw(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
731}
732
733void CodeGeneratorMIPS::GenerateFrameExit() {
734 __ cfi().RememberState();
735
736 if (!HasEmptyFrame()) {
737 // Deallocate the rest of the frame.
738
739 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
740
741 // Restore callee-saved registers.
742 // Note that their cumulative size is small and they can be indexed using
743 // 16-bit offsets.
744
745 // TODO: increment/decrement SP in one step instead of two or remove this comment.
746
747 uint32_t ofs = 0;
748 bool unaligned_float = FrameEntrySpillSize() & 0x7;
749 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
750
751 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
752 FRegister reg = kFpuCalleeSaves[i];
753 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
754 if (unaligned_float) {
755 if (fpu_32bit) {
756 __ Lwc1(reg, SP, ofs);
757 __ Lwc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
758 } else {
759 __ Lwc1(reg, SP, ofs);
760 __ Lw(TMP, SP, ofs + 4);
761 __ Mthc1(TMP, reg);
762 }
763 } else {
764 __ Ldc1(reg, SP, ofs);
765 }
766 ofs += kMipsDoublewordSize;
767 // TODO: __ cfi().Restore(DWARFReg(reg));
768 }
769 }
770
771 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
772 Register reg = kCoreCalleeSaves[i];
773 if (allocated_registers_.ContainsCoreRegister(reg)) {
774 __ Lw(reg, SP, ofs);
775 ofs += kMipsWordSize;
776 __ cfi().Restore(DWARFReg(reg));
777 }
778 }
779
780 DCHECK_EQ(ofs, FrameEntrySpillSize());
781 __ DecreaseFrameSize(ofs);
782 }
783
784 __ Jr(RA);
785 __ Nop();
786
787 __ cfi().RestoreState();
788 __ cfi().DefCFAOffset(GetFrameSize());
789}
790
791void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
792 __ Bind(GetLabelOf(block));
793}
794
795void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
796 if (src.Equals(dst)) {
797 return;
798 }
799
800 if (src.IsConstant()) {
801 MoveConstant(dst, src.GetConstant());
802 } else {
803 if (Primitive::Is64BitType(dst_type)) {
804 Move64(dst, src);
805 } else {
806 Move32(dst, src);
807 }
808 }
809}
810
811void CodeGeneratorMIPS::Move32(Location destination, Location source) {
812 if (source.Equals(destination)) {
813 return;
814 }
815
816 if (destination.IsRegister()) {
817 if (source.IsRegister()) {
818 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
819 } else if (source.IsFpuRegister()) {
820 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
821 } else {
822 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
823 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
824 }
825 } else if (destination.IsFpuRegister()) {
826 if (source.IsRegister()) {
827 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
828 } else if (source.IsFpuRegister()) {
829 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
830 } else {
831 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
832 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
833 }
834 } else {
835 DCHECK(destination.IsStackSlot()) << destination;
836 if (source.IsRegister()) {
837 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
838 } else if (source.IsFpuRegister()) {
839 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
840 } else {
841 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
842 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
843 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
844 }
845 }
846}
847
848void CodeGeneratorMIPS::Move64(Location destination, Location source) {
849 if (source.Equals(destination)) {
850 return;
851 }
852
853 if (destination.IsRegisterPair()) {
854 if (source.IsRegisterPair()) {
855 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
856 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
857 } else if (source.IsFpuRegister()) {
858 Register dst_high = destination.AsRegisterPairHigh<Register>();
859 Register dst_low = destination.AsRegisterPairLow<Register>();
860 FRegister src = source.AsFpuRegister<FRegister>();
861 __ Mfc1(dst_low, src);
862 __ Mfhc1(dst_high, src);
863 } else {
864 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
865 int32_t off = source.GetStackIndex();
866 Register r = destination.AsRegisterPairLow<Register>();
867 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
868 }
869 } else if (destination.IsFpuRegister()) {
870 if (source.IsRegisterPair()) {
871 FRegister dst = destination.AsFpuRegister<FRegister>();
872 Register src_high = source.AsRegisterPairHigh<Register>();
873 Register src_low = source.AsRegisterPairLow<Register>();
874 __ Mtc1(src_low, dst);
875 __ Mthc1(src_high, dst);
876 } else if (source.IsFpuRegister()) {
877 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
878 } else {
879 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
880 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
881 }
882 } else {
883 DCHECK(destination.IsDoubleStackSlot()) << destination;
884 int32_t off = destination.GetStackIndex();
885 if (source.IsRegisterPair()) {
886 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
887 } else if (source.IsFpuRegister()) {
888 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
889 } else {
890 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
891 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
892 __ StoreToOffset(kStoreWord, TMP, SP, off);
893 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
894 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
895 }
896 }
897}
898
899void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
900 if (c->IsIntConstant() || c->IsNullConstant()) {
901 // Move 32 bit constant.
902 int32_t value = GetInt32ValueOf(c);
903 if (destination.IsRegister()) {
904 Register dst = destination.AsRegister<Register>();
905 __ LoadConst32(dst, value);
906 } else {
907 DCHECK(destination.IsStackSlot())
908 << "Cannot move " << c->DebugName() << " to " << destination;
909 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
910 }
911 } else if (c->IsLongConstant()) {
912 // Move 64 bit constant.
913 int64_t value = GetInt64ValueOf(c);
914 if (destination.IsRegisterPair()) {
915 Register r_h = destination.AsRegisterPairHigh<Register>();
916 Register r_l = destination.AsRegisterPairLow<Register>();
917 __ LoadConst64(r_h, r_l, value);
918 } else {
919 DCHECK(destination.IsDoubleStackSlot())
920 << "Cannot move " << c->DebugName() << " to " << destination;
921 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
922 }
923 } else if (c->IsFloatConstant()) {
924 // Move 32 bit float constant.
925 int32_t value = GetInt32ValueOf(c);
926 if (destination.IsFpuRegister()) {
927 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
928 } else {
929 DCHECK(destination.IsStackSlot())
930 << "Cannot move " << c->DebugName() << " to " << destination;
931 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
932 }
933 } else {
934 // Move 64 bit double constant.
935 DCHECK(c->IsDoubleConstant()) << c->DebugName();
936 int64_t value = GetInt64ValueOf(c);
937 if (destination.IsFpuRegister()) {
938 FRegister fd = destination.AsFpuRegister<FRegister>();
939 __ LoadDConst64(fd, value, TMP);
940 } else {
941 DCHECK(destination.IsDoubleStackSlot())
942 << "Cannot move " << c->DebugName() << " to " << destination;
943 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
944 }
945 }
946}
947
948void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
949 DCHECK(destination.IsRegister());
950 Register dst = destination.AsRegister<Register>();
951 __ LoadConst32(dst, value);
952}
953
954void CodeGeneratorMIPS::Move(HInstruction* instruction,
955 Location location,
956 HInstruction* move_for) {
957 LocationSummary* locations = instruction->GetLocations();
958 Primitive::Type type = instruction->GetType();
959 DCHECK_NE(type, Primitive::kPrimVoid);
960
961 if (instruction->IsCurrentMethod()) {
962 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
963 } else if (locations != nullptr && locations->Out().Equals(location)) {
964 return;
965 } else if (instruction->IsIntConstant()
966 || instruction->IsLongConstant()
967 || instruction->IsNullConstant()) {
968 MoveConstant(location, instruction->AsConstant());
969 } else if (instruction->IsTemporary()) {
970 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
971 if (temp_location.IsStackSlot()) {
972 Move32(location, temp_location);
973 } else {
974 DCHECK(temp_location.IsDoubleStackSlot());
975 Move64(location, temp_location);
976 }
977 } else if (instruction->IsLoadLocal()) {
978 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
979 if (Primitive::Is64BitType(type)) {
980 Move64(location, Location::DoubleStackSlot(stack_slot));
981 } else {
982 Move32(location, Location::StackSlot(stack_slot));
983 }
984 } else {
985 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
986 if (Primitive::Is64BitType(type)) {
987 Move64(location, locations->Out());
988 } else {
989 Move32(location, locations->Out());
990 }
991 }
992}
993
994void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
995 if (location.IsRegister()) {
996 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700997 } else if (location.IsRegisterPair()) {
998 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
999 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001000 } else {
1001 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1002 }
1003}
1004
1005Location CodeGeneratorMIPS::GetStackLocation(HLoadLocal* load) const {
1006 Primitive::Type type = load->GetType();
1007
1008 switch (type) {
1009 case Primitive::kPrimNot:
1010 case Primitive::kPrimInt:
1011 case Primitive::kPrimFloat:
1012 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1013
1014 case Primitive::kPrimLong:
1015 case Primitive::kPrimDouble:
1016 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1017
1018 case Primitive::kPrimBoolean:
1019 case Primitive::kPrimByte:
1020 case Primitive::kPrimChar:
1021 case Primitive::kPrimShort:
1022 case Primitive::kPrimVoid:
1023 LOG(FATAL) << "Unexpected type " << type;
1024 }
1025
1026 LOG(FATAL) << "Unreachable";
1027 return Location::NoLocation();
1028}
1029
1030void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1031 MipsLabel done;
1032 Register card = AT;
1033 Register temp = TMP;
1034 __ Beqz(value, &done);
1035 __ LoadFromOffset(kLoadWord,
1036 card,
1037 TR,
1038 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
1039 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1040 __ Addu(temp, card, temp);
1041 __ Sb(card, temp, 0);
1042 __ Bind(&done);
1043}
1044
1045void CodeGeneratorMIPS::SetupBlockedRegisters(bool is_baseline) const {
1046 // Don't allocate the dalvik style register pair passing.
1047 blocked_register_pairs_[A1_A2] = true;
1048
1049 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1050 blocked_core_registers_[ZERO] = true;
1051 blocked_core_registers_[K0] = true;
1052 blocked_core_registers_[K1] = true;
1053 blocked_core_registers_[GP] = true;
1054 blocked_core_registers_[SP] = true;
1055 blocked_core_registers_[RA] = true;
1056
1057 // AT and TMP(T8) are used as temporary/scratch registers
1058 // (similar to how AT is used by MIPS assemblers).
1059 blocked_core_registers_[AT] = true;
1060 blocked_core_registers_[TMP] = true;
1061 blocked_fpu_registers_[FTMP] = true;
1062
1063 // Reserve suspend and thread registers.
1064 blocked_core_registers_[S0] = true;
1065 blocked_core_registers_[TR] = true;
1066
1067 // Reserve T9 for function calls
1068 blocked_core_registers_[T9] = true;
1069
1070 // Reserve odd-numbered FPU registers.
1071 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1072 blocked_fpu_registers_[i] = true;
1073 }
1074
1075 if (is_baseline) {
1076 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1077 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
1078 }
1079
1080 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1081 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1082 }
1083 }
1084
1085 UpdateBlockedPairRegisters();
1086}
1087
1088void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1089 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1090 MipsManagedRegister current =
1091 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1092 if (blocked_core_registers_[current.AsRegisterPairLow()]
1093 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1094 blocked_register_pairs_[i] = true;
1095 }
1096 }
1097}
1098
1099Location CodeGeneratorMIPS::AllocateFreeRegister(Primitive::Type type) const {
1100 switch (type) {
1101 case Primitive::kPrimLong: {
1102 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
1103 MipsManagedRegister pair =
1104 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
1105 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
1106 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
1107
1108 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
1109 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
1110 UpdateBlockedPairRegisters();
1111 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
1112 }
1113
1114 case Primitive::kPrimByte:
1115 case Primitive::kPrimBoolean:
1116 case Primitive::kPrimChar:
1117 case Primitive::kPrimShort:
1118 case Primitive::kPrimInt:
1119 case Primitive::kPrimNot: {
1120 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
1121 // Block all register pairs that contain `reg`.
1122 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1123 MipsManagedRegister current =
1124 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1125 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
1126 blocked_register_pairs_[i] = true;
1127 }
1128 }
1129 return Location::RegisterLocation(reg);
1130 }
1131
1132 case Primitive::kPrimFloat:
1133 case Primitive::kPrimDouble: {
1134 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFRegisters);
1135 return Location::FpuRegisterLocation(reg);
1136 }
1137
1138 case Primitive::kPrimVoid:
1139 LOG(FATAL) << "Unreachable type " << type;
1140 }
1141
1142 UNREACHABLE();
1143}
1144
1145size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1146 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1147 return kMipsWordSize;
1148}
1149
1150size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1151 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1152 return kMipsWordSize;
1153}
1154
1155size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1156 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1157 return kMipsDoublewordSize;
1158}
1159
1160size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1161 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1162 return kMipsDoublewordSize;
1163}
1164
1165void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
1166 stream << MipsManagedRegister::FromCoreRegister(Register(reg));
1167}
1168
1169void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1170 stream << MipsManagedRegister::FromFRegister(FRegister(reg));
1171}
1172
1173void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1174 HInstruction* instruction,
1175 uint32_t dex_pc,
1176 SlowPathCode* slow_path) {
1177 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1178 instruction,
1179 dex_pc,
1180 slow_path,
1181 IsDirectEntrypoint(entrypoint));
1182}
1183
1184constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1185
1186void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1187 HInstruction* instruction,
1188 uint32_t dex_pc,
1189 SlowPathCode* slow_path,
1190 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001191 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1192 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001193 if (is_direct_entrypoint) {
1194 // Reserve argument space on stack (for $a0-$a3) for
1195 // entrypoints that directly reference native implementations.
1196 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001197 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001198 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001199 } else {
1200 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001201 }
1202 RecordPcInfo(instruction, dex_pc, slow_path);
1203}
1204
1205void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1206 Register class_reg) {
1207 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1208 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1209 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1210 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1211 __ Sync(0);
1212 __ Bind(slow_path->GetExitLabel());
1213}
1214
1215void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1216 __ Sync(0); // Only stype 0 is supported.
1217}
1218
1219void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1220 HBasicBlock* successor) {
1221 SuspendCheckSlowPathMIPS* slow_path =
1222 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1223 codegen_->AddSlowPath(slow_path);
1224
1225 __ LoadFromOffset(kLoadUnsignedHalfword,
1226 TMP,
1227 TR,
1228 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1229 if (successor == nullptr) {
1230 __ Bnez(TMP, slow_path->GetEntryLabel());
1231 __ Bind(slow_path->GetReturnLabel());
1232 } else {
1233 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1234 __ B(slow_path->GetEntryLabel());
1235 // slow_path will return to GetLabelOf(successor).
1236 }
1237}
1238
1239InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1240 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001241 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001242 assembler_(codegen->GetAssembler()),
1243 codegen_(codegen) {}
1244
1245void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1246 DCHECK_EQ(instruction->InputCount(), 2U);
1247 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1248 Primitive::Type type = instruction->GetResultType();
1249 switch (type) {
1250 case Primitive::kPrimInt: {
1251 locations->SetInAt(0, Location::RequiresRegister());
1252 HInstruction* right = instruction->InputAt(1);
1253 bool can_use_imm = false;
1254 if (right->IsConstant()) {
1255 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1256 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1257 can_use_imm = IsUint<16>(imm);
1258 } else if (instruction->IsAdd()) {
1259 can_use_imm = IsInt<16>(imm);
1260 } else {
1261 DCHECK(instruction->IsSub());
1262 can_use_imm = IsInt<16>(-imm);
1263 }
1264 }
1265 if (can_use_imm)
1266 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1267 else
1268 locations->SetInAt(1, Location::RequiresRegister());
1269 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1270 break;
1271 }
1272
1273 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001274 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001275 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001277 break;
1278 }
1279
1280 case Primitive::kPrimFloat:
1281 case Primitive::kPrimDouble:
1282 DCHECK(instruction->IsAdd() || instruction->IsSub());
1283 locations->SetInAt(0, Location::RequiresFpuRegister());
1284 locations->SetInAt(1, Location::RequiresFpuRegister());
1285 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1286 break;
1287
1288 default:
1289 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1290 }
1291}
1292
1293void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1294 Primitive::Type type = instruction->GetType();
1295 LocationSummary* locations = instruction->GetLocations();
1296
1297 switch (type) {
1298 case Primitive::kPrimInt: {
1299 Register dst = locations->Out().AsRegister<Register>();
1300 Register lhs = locations->InAt(0).AsRegister<Register>();
1301 Location rhs_location = locations->InAt(1);
1302
1303 Register rhs_reg = ZERO;
1304 int32_t rhs_imm = 0;
1305 bool use_imm = rhs_location.IsConstant();
1306 if (use_imm) {
1307 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1308 } else {
1309 rhs_reg = rhs_location.AsRegister<Register>();
1310 }
1311
1312 if (instruction->IsAnd()) {
1313 if (use_imm)
1314 __ Andi(dst, lhs, rhs_imm);
1315 else
1316 __ And(dst, lhs, rhs_reg);
1317 } else if (instruction->IsOr()) {
1318 if (use_imm)
1319 __ Ori(dst, lhs, rhs_imm);
1320 else
1321 __ Or(dst, lhs, rhs_reg);
1322 } else if (instruction->IsXor()) {
1323 if (use_imm)
1324 __ Xori(dst, lhs, rhs_imm);
1325 else
1326 __ Xor(dst, lhs, rhs_reg);
1327 } else if (instruction->IsAdd()) {
1328 if (use_imm)
1329 __ Addiu(dst, lhs, rhs_imm);
1330 else
1331 __ Addu(dst, lhs, rhs_reg);
1332 } else {
1333 DCHECK(instruction->IsSub());
1334 if (use_imm)
1335 __ Addiu(dst, lhs, -rhs_imm);
1336 else
1337 __ Subu(dst, lhs, rhs_reg);
1338 }
1339 break;
1340 }
1341
1342 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001343 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1344 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1345 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1346 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001347 Location rhs_location = locations->InAt(1);
1348 bool use_imm = rhs_location.IsConstant();
1349 if (!use_imm) {
1350 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1351 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1352 if (instruction->IsAnd()) {
1353 __ And(dst_low, lhs_low, rhs_low);
1354 __ And(dst_high, lhs_high, rhs_high);
1355 } else if (instruction->IsOr()) {
1356 __ Or(dst_low, lhs_low, rhs_low);
1357 __ Or(dst_high, lhs_high, rhs_high);
1358 } else if (instruction->IsXor()) {
1359 __ Xor(dst_low, lhs_low, rhs_low);
1360 __ Xor(dst_high, lhs_high, rhs_high);
1361 } else if (instruction->IsAdd()) {
1362 if (lhs_low == rhs_low) {
1363 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1364 __ Slt(TMP, lhs_low, ZERO);
1365 __ Addu(dst_low, lhs_low, rhs_low);
1366 } else {
1367 __ Addu(dst_low, lhs_low, rhs_low);
1368 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1369 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1370 }
1371 __ Addu(dst_high, lhs_high, rhs_high);
1372 __ Addu(dst_high, dst_high, TMP);
1373 } else {
1374 DCHECK(instruction->IsSub());
1375 __ Sltu(TMP, lhs_low, rhs_low);
1376 __ Subu(dst_low, lhs_low, rhs_low);
1377 __ Subu(dst_high, lhs_high, rhs_high);
1378 __ Subu(dst_high, dst_high, TMP);
1379 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001380 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001381 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1382 if (instruction->IsOr()) {
1383 uint32_t low = Low32Bits(value);
1384 uint32_t high = High32Bits(value);
1385 if (IsUint<16>(low)) {
1386 if (dst_low != lhs_low || low != 0) {
1387 __ Ori(dst_low, lhs_low, low);
1388 }
1389 } else {
1390 __ LoadConst32(TMP, low);
1391 __ Or(dst_low, lhs_low, TMP);
1392 }
1393 if (IsUint<16>(high)) {
1394 if (dst_high != lhs_high || high != 0) {
1395 __ Ori(dst_high, lhs_high, high);
1396 }
1397 } else {
1398 if (high != low) {
1399 __ LoadConst32(TMP, high);
1400 }
1401 __ Or(dst_high, lhs_high, TMP);
1402 }
1403 } else if (instruction->IsXor()) {
1404 uint32_t low = Low32Bits(value);
1405 uint32_t high = High32Bits(value);
1406 if (IsUint<16>(low)) {
1407 if (dst_low != lhs_low || low != 0) {
1408 __ Xori(dst_low, lhs_low, low);
1409 }
1410 } else {
1411 __ LoadConst32(TMP, low);
1412 __ Xor(dst_low, lhs_low, TMP);
1413 }
1414 if (IsUint<16>(high)) {
1415 if (dst_high != lhs_high || high != 0) {
1416 __ Xori(dst_high, lhs_high, high);
1417 }
1418 } else {
1419 if (high != low) {
1420 __ LoadConst32(TMP, high);
1421 }
1422 __ Xor(dst_high, lhs_high, TMP);
1423 }
1424 } else if (instruction->IsAnd()) {
1425 uint32_t low = Low32Bits(value);
1426 uint32_t high = High32Bits(value);
1427 if (IsUint<16>(low)) {
1428 __ Andi(dst_low, lhs_low, low);
1429 } else if (low != 0xFFFFFFFF) {
1430 __ LoadConst32(TMP, low);
1431 __ And(dst_low, lhs_low, TMP);
1432 } else if (dst_low != lhs_low) {
1433 __ Move(dst_low, lhs_low);
1434 }
1435 if (IsUint<16>(high)) {
1436 __ Andi(dst_high, lhs_high, high);
1437 } else if (high != 0xFFFFFFFF) {
1438 if (high != low) {
1439 __ LoadConst32(TMP, high);
1440 }
1441 __ And(dst_high, lhs_high, TMP);
1442 } else if (dst_high != lhs_high) {
1443 __ Move(dst_high, lhs_high);
1444 }
1445 } else {
1446 if (instruction->IsSub()) {
1447 value = -value;
1448 } else {
1449 DCHECK(instruction->IsAdd());
1450 }
1451 int32_t low = Low32Bits(value);
1452 int32_t high = High32Bits(value);
1453 if (IsInt<16>(low)) {
1454 if (dst_low != lhs_low || low != 0) {
1455 __ Addiu(dst_low, lhs_low, low);
1456 }
1457 if (low != 0) {
1458 __ Sltiu(AT, dst_low, low);
1459 }
1460 } else {
1461 __ LoadConst32(TMP, low);
1462 __ Addu(dst_low, lhs_low, TMP);
1463 __ Sltu(AT, dst_low, TMP);
1464 }
1465 if (IsInt<16>(high)) {
1466 if (dst_high != lhs_high || high != 0) {
1467 __ Addiu(dst_high, lhs_high, high);
1468 }
1469 } else {
1470 if (high != low) {
1471 __ LoadConst32(TMP, high);
1472 }
1473 __ Addu(dst_high, lhs_high, TMP);
1474 }
1475 if (low != 0) {
1476 __ Addu(dst_high, dst_high, AT);
1477 }
1478 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001479 }
1480 break;
1481 }
1482
1483 case Primitive::kPrimFloat:
1484 case Primitive::kPrimDouble: {
1485 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1486 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1487 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1488 if (instruction->IsAdd()) {
1489 if (type == Primitive::kPrimFloat) {
1490 __ AddS(dst, lhs, rhs);
1491 } else {
1492 __ AddD(dst, lhs, rhs);
1493 }
1494 } else {
1495 DCHECK(instruction->IsSub());
1496 if (type == Primitive::kPrimFloat) {
1497 __ SubS(dst, lhs, rhs);
1498 } else {
1499 __ SubD(dst, lhs, rhs);
1500 }
1501 }
1502 break;
1503 }
1504
1505 default:
1506 LOG(FATAL) << "Unexpected binary operation type " << type;
1507 }
1508}
1509
1510void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001511 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001512
1513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1514 Primitive::Type type = instr->GetResultType();
1515 switch (type) {
1516 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001517 locations->SetInAt(0, Location::RequiresRegister());
1518 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1519 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1520 break;
1521 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001522 locations->SetInAt(0, Location::RequiresRegister());
1523 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1524 locations->SetOut(Location::RequiresRegister());
1525 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001526 default:
1527 LOG(FATAL) << "Unexpected shift type " << type;
1528 }
1529}
1530
1531static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1532
1533void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001534 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001535 LocationSummary* locations = instr->GetLocations();
1536 Primitive::Type type = instr->GetType();
1537
1538 Location rhs_location = locations->InAt(1);
1539 bool use_imm = rhs_location.IsConstant();
1540 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1541 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001542 const uint32_t shift_mask = (type == Primitive::kPrimInt)
1543 ? kMaxIntShiftValue
1544 : kMaxLongShiftValue;
1545 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001546 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1547 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001548
1549 switch (type) {
1550 case Primitive::kPrimInt: {
1551 Register dst = locations->Out().AsRegister<Register>();
1552 Register lhs = locations->InAt(0).AsRegister<Register>();
1553 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001554 if (shift_value == 0) {
1555 if (dst != lhs) {
1556 __ Move(dst, lhs);
1557 }
1558 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001559 __ Sll(dst, lhs, shift_value);
1560 } else if (instr->IsShr()) {
1561 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001562 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001563 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001564 } else {
1565 if (has_ins_rotr) {
1566 __ Rotr(dst, lhs, shift_value);
1567 } else {
1568 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1569 __ Srl(dst, lhs, shift_value);
1570 __ Or(dst, dst, TMP);
1571 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001572 }
1573 } else {
1574 if (instr->IsShl()) {
1575 __ Sllv(dst, lhs, rhs_reg);
1576 } else if (instr->IsShr()) {
1577 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001578 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001579 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001580 } else {
1581 if (has_ins_rotr) {
1582 __ Rotrv(dst, lhs, rhs_reg);
1583 } else {
1584 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001585 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1586 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1587 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1588 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1589 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001590 __ Sllv(TMP, lhs, TMP);
1591 __ Srlv(dst, lhs, rhs_reg);
1592 __ Or(dst, dst, TMP);
1593 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001594 }
1595 }
1596 break;
1597 }
1598
1599 case Primitive::kPrimLong: {
1600 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1601 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1602 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1603 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1604 if (use_imm) {
1605 if (shift_value == 0) {
1606 codegen_->Move64(locations->Out(), locations->InAt(0));
1607 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001608 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001609 if (instr->IsShl()) {
1610 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1611 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1612 __ Sll(dst_low, lhs_low, shift_value);
1613 } else if (instr->IsShr()) {
1614 __ Srl(dst_low, lhs_low, shift_value);
1615 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1616 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001617 } else if (instr->IsUShr()) {
1618 __ Srl(dst_low, lhs_low, shift_value);
1619 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1620 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001621 } else {
1622 __ Srl(dst_low, lhs_low, shift_value);
1623 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1624 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001625 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001626 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001627 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001628 if (instr->IsShl()) {
1629 __ Sll(dst_low, lhs_low, shift_value);
1630 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1631 __ Sll(dst_high, lhs_high, shift_value);
1632 __ Or(dst_high, dst_high, TMP);
1633 } else if (instr->IsShr()) {
1634 __ Sra(dst_high, lhs_high, shift_value);
1635 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1636 __ Srl(dst_low, lhs_low, shift_value);
1637 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001638 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001639 __ Srl(dst_high, lhs_high, shift_value);
1640 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1641 __ Srl(dst_low, lhs_low, shift_value);
1642 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001643 } else {
1644 __ Srl(TMP, lhs_low, shift_value);
1645 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1646 __ Or(dst_low, dst_low, TMP);
1647 __ Srl(TMP, lhs_high, shift_value);
1648 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1649 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001650 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001651 }
1652 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001653 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001654 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001655 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001656 __ Move(dst_low, ZERO);
1657 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001658 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001659 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001660 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001661 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001662 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001663 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001664 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001665 // 64-bit rotation by 32 is just a swap.
1666 __ Move(dst_low, lhs_high);
1667 __ Move(dst_high, lhs_low);
1668 } else {
1669 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001670 __ Srl(dst_low, lhs_high, shift_value_high);
1671 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1672 __ Srl(dst_high, lhs_low, shift_value_high);
1673 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001674 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001675 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1676 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001677 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001678 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1679 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001680 __ Or(dst_high, dst_high, TMP);
1681 }
1682 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001683 }
1684 }
1685 } else {
1686 MipsLabel done;
1687 if (instr->IsShl()) {
1688 __ Sllv(dst_low, lhs_low, rhs_reg);
1689 __ Nor(AT, ZERO, rhs_reg);
1690 __ Srl(TMP, lhs_low, 1);
1691 __ Srlv(TMP, TMP, AT);
1692 __ Sllv(dst_high, lhs_high, rhs_reg);
1693 __ Or(dst_high, dst_high, TMP);
1694 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1695 __ Beqz(TMP, &done);
1696 __ Move(dst_high, dst_low);
1697 __ Move(dst_low, ZERO);
1698 } else if (instr->IsShr()) {
1699 __ Srav(dst_high, lhs_high, rhs_reg);
1700 __ Nor(AT, ZERO, rhs_reg);
1701 __ Sll(TMP, lhs_high, 1);
1702 __ Sllv(TMP, TMP, AT);
1703 __ Srlv(dst_low, lhs_low, rhs_reg);
1704 __ Or(dst_low, dst_low, TMP);
1705 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1706 __ Beqz(TMP, &done);
1707 __ Move(dst_low, dst_high);
1708 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001709 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001710 __ Srlv(dst_high, lhs_high, rhs_reg);
1711 __ Nor(AT, ZERO, rhs_reg);
1712 __ Sll(TMP, lhs_high, 1);
1713 __ Sllv(TMP, TMP, AT);
1714 __ Srlv(dst_low, lhs_low, rhs_reg);
1715 __ Or(dst_low, dst_low, TMP);
1716 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1717 __ Beqz(TMP, &done);
1718 __ Move(dst_low, dst_high);
1719 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001720 } else {
1721 __ Nor(AT, ZERO, rhs_reg);
1722 __ Srlv(TMP, lhs_low, rhs_reg);
1723 __ Sll(dst_low, lhs_high, 1);
1724 __ Sllv(dst_low, dst_low, AT);
1725 __ Or(dst_low, dst_low, TMP);
1726 __ Srlv(TMP, lhs_high, rhs_reg);
1727 __ Sll(dst_high, lhs_low, 1);
1728 __ Sllv(dst_high, dst_high, AT);
1729 __ Or(dst_high, dst_high, TMP);
1730 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1731 __ Beqz(TMP, &done);
1732 __ Move(TMP, dst_high);
1733 __ Move(dst_high, dst_low);
1734 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001735 }
1736 __ Bind(&done);
1737 }
1738 break;
1739 }
1740
1741 default:
1742 LOG(FATAL) << "Unexpected shift operation type " << type;
1743 }
1744}
1745
1746void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1747 HandleBinaryOp(instruction);
1748}
1749
1750void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1751 HandleBinaryOp(instruction);
1752}
1753
1754void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1755 HandleBinaryOp(instruction);
1756}
1757
1758void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1759 HandleBinaryOp(instruction);
1760}
1761
1762void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1763 LocationSummary* locations =
1764 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1765 locations->SetInAt(0, Location::RequiresRegister());
1766 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1767 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1768 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1769 } else {
1770 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1771 }
1772}
1773
1774void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1775 LocationSummary* locations = instruction->GetLocations();
1776 Register obj = locations->InAt(0).AsRegister<Register>();
1777 Location index = locations->InAt(1);
1778 Primitive::Type type = instruction->GetType();
1779
1780 switch (type) {
1781 case Primitive::kPrimBoolean: {
1782 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1783 Register out = locations->Out().AsRegister<Register>();
1784 if (index.IsConstant()) {
1785 size_t offset =
1786 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1787 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1788 } else {
1789 __ Addu(TMP, obj, index.AsRegister<Register>());
1790 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1791 }
1792 break;
1793 }
1794
1795 case Primitive::kPrimByte: {
1796 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1797 Register out = locations->Out().AsRegister<Register>();
1798 if (index.IsConstant()) {
1799 size_t offset =
1800 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1801 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1802 } else {
1803 __ Addu(TMP, obj, index.AsRegister<Register>());
1804 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1805 }
1806 break;
1807 }
1808
1809 case Primitive::kPrimShort: {
1810 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1811 Register out = locations->Out().AsRegister<Register>();
1812 if (index.IsConstant()) {
1813 size_t offset =
1814 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1815 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1816 } else {
1817 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1818 __ Addu(TMP, obj, TMP);
1819 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1820 }
1821 break;
1822 }
1823
1824 case Primitive::kPrimChar: {
1825 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1826 Register out = locations->Out().AsRegister<Register>();
1827 if (index.IsConstant()) {
1828 size_t offset =
1829 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1830 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1831 } else {
1832 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1833 __ Addu(TMP, obj, TMP);
1834 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1835 }
1836 break;
1837 }
1838
1839 case Primitive::kPrimInt:
1840 case Primitive::kPrimNot: {
1841 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1842 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1843 Register out = locations->Out().AsRegister<Register>();
1844 if (index.IsConstant()) {
1845 size_t offset =
1846 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1847 __ LoadFromOffset(kLoadWord, out, obj, offset);
1848 } else {
1849 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1850 __ Addu(TMP, obj, TMP);
1851 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1852 }
1853 break;
1854 }
1855
1856 case Primitive::kPrimLong: {
1857 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1858 Register out = locations->Out().AsRegisterPairLow<Register>();
1859 if (index.IsConstant()) {
1860 size_t offset =
1861 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1862 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1863 } else {
1864 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1865 __ Addu(TMP, obj, TMP);
1866 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1867 }
1868 break;
1869 }
1870
1871 case Primitive::kPrimFloat: {
1872 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1873 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1874 if (index.IsConstant()) {
1875 size_t offset =
1876 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1877 __ LoadSFromOffset(out, obj, offset);
1878 } else {
1879 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1880 __ Addu(TMP, obj, TMP);
1881 __ LoadSFromOffset(out, TMP, data_offset);
1882 }
1883 break;
1884 }
1885
1886 case Primitive::kPrimDouble: {
1887 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1888 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1889 if (index.IsConstant()) {
1890 size_t offset =
1891 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1892 __ LoadDFromOffset(out, obj, offset);
1893 } else {
1894 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1895 __ Addu(TMP, obj, TMP);
1896 __ LoadDFromOffset(out, TMP, data_offset);
1897 }
1898 break;
1899 }
1900
1901 case Primitive::kPrimVoid:
1902 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1903 UNREACHABLE();
1904 }
1905 codegen_->MaybeRecordImplicitNullCheck(instruction);
1906}
1907
1908void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1910 locations->SetInAt(0, Location::RequiresRegister());
1911 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1912}
1913
1914void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1915 LocationSummary* locations = instruction->GetLocations();
1916 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1917 Register obj = locations->InAt(0).AsRegister<Register>();
1918 Register out = locations->Out().AsRegister<Register>();
1919 __ LoadFromOffset(kLoadWord, out, obj, offset);
1920 codegen_->MaybeRecordImplicitNullCheck(instruction);
1921}
1922
1923void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001924 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001925 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1926 instruction,
Pavle Batuta934808f2015-11-03 13:23:54 +01001927 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1928 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929 InvokeRuntimeCallingConvention calling_convention;
1930 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1931 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1932 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1933 } else {
1934 locations->SetInAt(0, Location::RequiresRegister());
1935 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1936 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1937 locations->SetInAt(2, Location::RequiresFpuRegister());
1938 } else {
1939 locations->SetInAt(2, Location::RequiresRegister());
1940 }
1941 }
1942}
1943
1944void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1945 LocationSummary* locations = instruction->GetLocations();
1946 Register obj = locations->InAt(0).AsRegister<Register>();
1947 Location index = locations->InAt(1);
1948 Primitive::Type value_type = instruction->GetComponentType();
1949 bool needs_runtime_call = locations->WillCall();
1950 bool needs_write_barrier =
1951 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1952
1953 switch (value_type) {
1954 case Primitive::kPrimBoolean:
1955 case Primitive::kPrimByte: {
1956 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1957 Register value = locations->InAt(2).AsRegister<Register>();
1958 if (index.IsConstant()) {
1959 size_t offset =
1960 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1961 __ StoreToOffset(kStoreByte, value, obj, offset);
1962 } else {
1963 __ Addu(TMP, obj, index.AsRegister<Register>());
1964 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1965 }
1966 break;
1967 }
1968
1969 case Primitive::kPrimShort:
1970 case Primitive::kPrimChar: {
1971 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1972 Register value = locations->InAt(2).AsRegister<Register>();
1973 if (index.IsConstant()) {
1974 size_t offset =
1975 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1976 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1977 } else {
1978 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1979 __ Addu(TMP, obj, TMP);
1980 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1981 }
1982 break;
1983 }
1984
1985 case Primitive::kPrimInt:
1986 case Primitive::kPrimNot: {
1987 if (!needs_runtime_call) {
1988 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1989 Register value = locations->InAt(2).AsRegister<Register>();
1990 if (index.IsConstant()) {
1991 size_t offset =
1992 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1993 __ StoreToOffset(kStoreWord, value, obj, offset);
1994 } else {
1995 DCHECK(index.IsRegister()) << index;
1996 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1997 __ Addu(TMP, obj, TMP);
1998 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1999 }
2000 codegen_->MaybeRecordImplicitNullCheck(instruction);
2001 if (needs_write_barrier) {
2002 DCHECK_EQ(value_type, Primitive::kPrimNot);
2003 codegen_->MarkGCCard(obj, value);
2004 }
2005 } else {
2006 DCHECK_EQ(value_type, Primitive::kPrimNot);
2007 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
2008 instruction,
2009 instruction->GetDexPc(),
2010 nullptr,
2011 IsDirectEntrypoint(kQuickAputObject));
2012 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2013 }
2014 break;
2015 }
2016
2017 case Primitive::kPrimLong: {
2018 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
2019 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
2020 if (index.IsConstant()) {
2021 size_t offset =
2022 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2023 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
2024 } else {
2025 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2026 __ Addu(TMP, obj, TMP);
2027 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
2028 }
2029 break;
2030 }
2031
2032 case Primitive::kPrimFloat: {
2033 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2034 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2035 DCHECK(locations->InAt(2).IsFpuRegister());
2036 if (index.IsConstant()) {
2037 size_t offset =
2038 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2039 __ StoreSToOffset(value, obj, offset);
2040 } else {
2041 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2042 __ Addu(TMP, obj, TMP);
2043 __ StoreSToOffset(value, TMP, data_offset);
2044 }
2045 break;
2046 }
2047
2048 case Primitive::kPrimDouble: {
2049 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2050 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2051 DCHECK(locations->InAt(2).IsFpuRegister());
2052 if (index.IsConstant()) {
2053 size_t offset =
2054 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2055 __ StoreDToOffset(value, obj, offset);
2056 } else {
2057 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2058 __ Addu(TMP, obj, TMP);
2059 __ StoreDToOffset(value, TMP, data_offset);
2060 }
2061 break;
2062 }
2063
2064 case Primitive::kPrimVoid:
2065 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2066 UNREACHABLE();
2067 }
2068
2069 // Ints and objects are handled in the switch.
2070 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
2071 codegen_->MaybeRecordImplicitNullCheck(instruction);
2072 }
2073}
2074
2075void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2076 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2077 ? LocationSummary::kCallOnSlowPath
2078 : LocationSummary::kNoCall;
2079 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2080 locations->SetInAt(0, Location::RequiresRegister());
2081 locations->SetInAt(1, Location::RequiresRegister());
2082 if (instruction->HasUses()) {
2083 locations->SetOut(Location::SameAsFirstInput());
2084 }
2085}
2086
2087void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2088 LocationSummary* locations = instruction->GetLocations();
2089 BoundsCheckSlowPathMIPS* slow_path =
2090 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2091 codegen_->AddSlowPath(slow_path);
2092
2093 Register index = locations->InAt(0).AsRegister<Register>();
2094 Register length = locations->InAt(1).AsRegister<Register>();
2095
2096 // length is limited by the maximum positive signed 32-bit integer.
2097 // Unsigned comparison of length and index checks for index < 0
2098 // and for length <= index simultaneously.
2099 __ Bgeu(index, length, slow_path->GetEntryLabel());
2100}
2101
2102void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2103 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2104 instruction,
2105 LocationSummary::kCallOnSlowPath);
2106 locations->SetInAt(0, Location::RequiresRegister());
2107 locations->SetInAt(1, Location::RequiresRegister());
2108 // Note that TypeCheckSlowPathMIPS uses this register too.
2109 locations->AddTemp(Location::RequiresRegister());
2110}
2111
2112void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2113 LocationSummary* locations = instruction->GetLocations();
2114 Register obj = locations->InAt(0).AsRegister<Register>();
2115 Register cls = locations->InAt(1).AsRegister<Register>();
2116 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2117
2118 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2119 codegen_->AddSlowPath(slow_path);
2120
2121 // TODO: avoid this check if we know obj is not null.
2122 __ Beqz(obj, slow_path->GetExitLabel());
2123 // Compare the class of `obj` with `cls`.
2124 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2125 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2126 __ Bind(slow_path->GetExitLabel());
2127}
2128
2129void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2130 LocationSummary* locations =
2131 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2132 locations->SetInAt(0, Location::RequiresRegister());
2133 if (check->HasUses()) {
2134 locations->SetOut(Location::SameAsFirstInput());
2135 }
2136}
2137
2138void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2139 // We assume the class is not null.
2140 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2141 check->GetLoadClass(),
2142 check,
2143 check->GetDexPc(),
2144 true);
2145 codegen_->AddSlowPath(slow_path);
2146 GenerateClassInitializationCheck(slow_path,
2147 check->GetLocations()->InAt(0).AsRegister<Register>());
2148}
2149
2150void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2151 Primitive::Type in_type = compare->InputAt(0)->GetType();
2152
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002153 LocationSummary* locations =
2154 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002155
2156 switch (in_type) {
2157 case Primitive::kPrimLong:
2158 locations->SetInAt(0, Location::RequiresRegister());
2159 locations->SetInAt(1, Location::RequiresRegister());
2160 // Output overlaps because it is written before doing the low comparison.
2161 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2162 break;
2163
2164 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002165 case Primitive::kPrimDouble:
2166 locations->SetInAt(0, Location::RequiresFpuRegister());
2167 locations->SetInAt(1, Location::RequiresFpuRegister());
2168 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002169 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002170
2171 default:
2172 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2173 }
2174}
2175
2176void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2177 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002178 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002179 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002180 bool gt_bias = instruction->IsGtBias();
2181 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002182
2183 // 0 if: left == right
2184 // 1 if: left > right
2185 // -1 if: left < right
2186 switch (in_type) {
2187 case Primitive::kPrimLong: {
2188 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002189 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2190 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2191 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2192 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2193 // TODO: more efficient (direct) comparison with a constant.
2194 __ Slt(TMP, lhs_high, rhs_high);
2195 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2196 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2197 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2198 __ Sltu(TMP, lhs_low, rhs_low);
2199 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2200 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2201 __ Bind(&done);
2202 break;
2203 }
2204
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002205 case Primitive::kPrimFloat: {
2206 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2207 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2208 MipsLabel done;
2209 if (isR6) {
2210 __ CmpEqS(FTMP, lhs, rhs);
2211 __ LoadConst32(res, 0);
2212 __ Bc1nez(FTMP, &done);
2213 if (gt_bias) {
2214 __ CmpLtS(FTMP, lhs, rhs);
2215 __ LoadConst32(res, -1);
2216 __ Bc1nez(FTMP, &done);
2217 __ LoadConst32(res, 1);
2218 } else {
2219 __ CmpLtS(FTMP, rhs, lhs);
2220 __ LoadConst32(res, 1);
2221 __ Bc1nez(FTMP, &done);
2222 __ LoadConst32(res, -1);
2223 }
2224 } else {
2225 if (gt_bias) {
2226 __ ColtS(0, lhs, rhs);
2227 __ LoadConst32(res, -1);
2228 __ Bc1t(0, &done);
2229 __ CeqS(0, lhs, rhs);
2230 __ LoadConst32(res, 1);
2231 __ Movt(res, ZERO, 0);
2232 } else {
2233 __ ColtS(0, rhs, lhs);
2234 __ LoadConst32(res, 1);
2235 __ Bc1t(0, &done);
2236 __ CeqS(0, lhs, rhs);
2237 __ LoadConst32(res, -1);
2238 __ Movt(res, ZERO, 0);
2239 }
2240 }
2241 __ Bind(&done);
2242 break;
2243 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002244 case Primitive::kPrimDouble: {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002245 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2246 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2247 MipsLabel done;
2248 if (isR6) {
2249 __ CmpEqD(FTMP, lhs, rhs);
2250 __ LoadConst32(res, 0);
2251 __ Bc1nez(FTMP, &done);
2252 if (gt_bias) {
2253 __ CmpLtD(FTMP, lhs, rhs);
2254 __ LoadConst32(res, -1);
2255 __ Bc1nez(FTMP, &done);
2256 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002257 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002258 __ CmpLtD(FTMP, rhs, lhs);
2259 __ LoadConst32(res, 1);
2260 __ Bc1nez(FTMP, &done);
2261 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262 }
2263 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002264 if (gt_bias) {
2265 __ ColtD(0, lhs, rhs);
2266 __ LoadConst32(res, -1);
2267 __ Bc1t(0, &done);
2268 __ CeqD(0, lhs, rhs);
2269 __ LoadConst32(res, 1);
2270 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002271 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002272 __ ColtD(0, rhs, lhs);
2273 __ LoadConst32(res, 1);
2274 __ Bc1t(0, &done);
2275 __ CeqD(0, lhs, rhs);
2276 __ LoadConst32(res, -1);
2277 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002278 }
2279 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002280 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002281 break;
2282 }
2283
2284 default:
2285 LOG(FATAL) << "Unimplemented compare type " << in_type;
2286 }
2287}
2288
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002289void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002290 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002291 switch (instruction->InputAt(0)->GetType()) {
2292 default:
2293 case Primitive::kPrimLong:
2294 locations->SetInAt(0, Location::RequiresRegister());
2295 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2296 break;
2297
2298 case Primitive::kPrimFloat:
2299 case Primitive::kPrimDouble:
2300 locations->SetInAt(0, Location::RequiresFpuRegister());
2301 locations->SetInAt(1, Location::RequiresFpuRegister());
2302 break;
2303 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002304 if (instruction->NeedsMaterialization()) {
2305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2306 }
2307}
2308
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002309void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002310 if (!instruction->NeedsMaterialization()) {
2311 return;
2312 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002313
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002314 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002315 LocationSummary* locations = instruction->GetLocations();
2316 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002317 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002318
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002319 switch (type) {
2320 default:
2321 // Integer case.
2322 GenerateIntCompare(instruction->GetCondition(), locations);
2323 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002324
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002325 case Primitive::kPrimLong:
2326 // TODO: don't use branches.
2327 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002328 break;
2329
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002330 case Primitive::kPrimFloat:
2331 case Primitive::kPrimDouble:
2332 // TODO: don't use branches.
2333 GenerateFpCompareAndBranch(instruction->GetCondition(),
2334 instruction->IsGtBias(),
2335 type,
2336 locations,
2337 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002338 break;
2339 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002340
2341 // Convert the branches into the result.
2342 MipsLabel done;
2343
2344 // False case: result = 0.
2345 __ LoadConst32(dst, 0);
2346 __ B(&done);
2347
2348 // True case: result = 1.
2349 __ Bind(&true_label);
2350 __ LoadConst32(dst, 1);
2351 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002352}
2353
Alexey Frunze7e99e052015-11-24 19:28:01 -08002354void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2355 DCHECK(instruction->IsDiv() || instruction->IsRem());
2356 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2357
2358 LocationSummary* locations = instruction->GetLocations();
2359 Location second = locations->InAt(1);
2360 DCHECK(second.IsConstant());
2361
2362 Register out = locations->Out().AsRegister<Register>();
2363 Register dividend = locations->InAt(0).AsRegister<Register>();
2364 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2365 DCHECK(imm == 1 || imm == -1);
2366
2367 if (instruction->IsRem()) {
2368 __ Move(out, ZERO);
2369 } else {
2370 if (imm == -1) {
2371 __ Subu(out, ZERO, dividend);
2372 } else if (out != dividend) {
2373 __ Move(out, dividend);
2374 }
2375 }
2376}
2377
2378void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2379 DCHECK(instruction->IsDiv() || instruction->IsRem());
2380 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2381
2382 LocationSummary* locations = instruction->GetLocations();
2383 Location second = locations->InAt(1);
2384 DCHECK(second.IsConstant());
2385
2386 Register out = locations->Out().AsRegister<Register>();
2387 Register dividend = locations->InAt(0).AsRegister<Register>();
2388 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002389 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002390 int ctz_imm = CTZ(abs_imm);
2391
2392 if (instruction->IsDiv()) {
2393 if (ctz_imm == 1) {
2394 // Fast path for division by +/-2, which is very common.
2395 __ Srl(TMP, dividend, 31);
2396 } else {
2397 __ Sra(TMP, dividend, 31);
2398 __ Srl(TMP, TMP, 32 - ctz_imm);
2399 }
2400 __ Addu(out, dividend, TMP);
2401 __ Sra(out, out, ctz_imm);
2402 if (imm < 0) {
2403 __ Subu(out, ZERO, out);
2404 }
2405 } else {
2406 if (ctz_imm == 1) {
2407 // Fast path for modulo +/-2, which is very common.
2408 __ Sra(TMP, dividend, 31);
2409 __ Subu(out, dividend, TMP);
2410 __ Andi(out, out, 1);
2411 __ Addu(out, out, TMP);
2412 } else {
2413 __ Sra(TMP, dividend, 31);
2414 __ Srl(TMP, TMP, 32 - ctz_imm);
2415 __ Addu(out, dividend, TMP);
2416 if (IsUint<16>(abs_imm - 1)) {
2417 __ Andi(out, out, abs_imm - 1);
2418 } else {
2419 __ Sll(out, out, 32 - ctz_imm);
2420 __ Srl(out, out, 32 - ctz_imm);
2421 }
2422 __ Subu(out, out, TMP);
2423 }
2424 }
2425}
2426
2427void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2428 DCHECK(instruction->IsDiv() || instruction->IsRem());
2429 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2430
2431 LocationSummary* locations = instruction->GetLocations();
2432 Location second = locations->InAt(1);
2433 DCHECK(second.IsConstant());
2434
2435 Register out = locations->Out().AsRegister<Register>();
2436 Register dividend = locations->InAt(0).AsRegister<Register>();
2437 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2438
2439 int64_t magic;
2440 int shift;
2441 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2442
2443 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2444
2445 __ LoadConst32(TMP, magic);
2446 if (isR6) {
2447 __ MuhR6(TMP, dividend, TMP);
2448 } else {
2449 __ MultR2(dividend, TMP);
2450 __ Mfhi(TMP);
2451 }
2452 if (imm > 0 && magic < 0) {
2453 __ Addu(TMP, TMP, dividend);
2454 } else if (imm < 0 && magic > 0) {
2455 __ Subu(TMP, TMP, dividend);
2456 }
2457
2458 if (shift != 0) {
2459 __ Sra(TMP, TMP, shift);
2460 }
2461
2462 if (instruction->IsDiv()) {
2463 __ Sra(out, TMP, 31);
2464 __ Subu(out, TMP, out);
2465 } else {
2466 __ Sra(AT, TMP, 31);
2467 __ Subu(AT, TMP, AT);
2468 __ LoadConst32(TMP, imm);
2469 if (isR6) {
2470 __ MulR6(TMP, AT, TMP);
2471 } else {
2472 __ MulR2(TMP, AT, TMP);
2473 }
2474 __ Subu(out, dividend, TMP);
2475 }
2476}
2477
2478void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2479 DCHECK(instruction->IsDiv() || instruction->IsRem());
2480 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2481
2482 LocationSummary* locations = instruction->GetLocations();
2483 Register out = locations->Out().AsRegister<Register>();
2484 Location second = locations->InAt(1);
2485
2486 if (second.IsConstant()) {
2487 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2488 if (imm == 0) {
2489 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2490 } else if (imm == 1 || imm == -1) {
2491 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002492 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002493 DivRemByPowerOfTwo(instruction);
2494 } else {
2495 DCHECK(imm <= -2 || imm >= 2);
2496 GenerateDivRemWithAnyConstant(instruction);
2497 }
2498 } else {
2499 Register dividend = locations->InAt(0).AsRegister<Register>();
2500 Register divisor = second.AsRegister<Register>();
2501 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2502 if (instruction->IsDiv()) {
2503 if (isR6) {
2504 __ DivR6(out, dividend, divisor);
2505 } else {
2506 __ DivR2(out, dividend, divisor);
2507 }
2508 } else {
2509 if (isR6) {
2510 __ ModR6(out, dividend, divisor);
2511 } else {
2512 __ ModR2(out, dividend, divisor);
2513 }
2514 }
2515 }
2516}
2517
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002518void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2519 Primitive::Type type = div->GetResultType();
2520 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
2521 ? LocationSummary::kCall
2522 : LocationSummary::kNoCall;
2523
2524 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2525
2526 switch (type) {
2527 case Primitive::kPrimInt:
2528 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002529 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002530 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2531 break;
2532
2533 case Primitive::kPrimLong: {
2534 InvokeRuntimeCallingConvention calling_convention;
2535 locations->SetInAt(0, Location::RegisterPairLocation(
2536 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2537 locations->SetInAt(1, Location::RegisterPairLocation(
2538 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2539 locations->SetOut(calling_convention.GetReturnLocation(type));
2540 break;
2541 }
2542
2543 case Primitive::kPrimFloat:
2544 case Primitive::kPrimDouble:
2545 locations->SetInAt(0, Location::RequiresFpuRegister());
2546 locations->SetInAt(1, Location::RequiresFpuRegister());
2547 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2548 break;
2549
2550 default:
2551 LOG(FATAL) << "Unexpected div type " << type;
2552 }
2553}
2554
2555void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2556 Primitive::Type type = instruction->GetType();
2557 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002558
2559 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002560 case Primitive::kPrimInt:
2561 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002562 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002563 case Primitive::kPrimLong: {
2564 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2565 instruction,
2566 instruction->GetDexPc(),
2567 nullptr,
2568 IsDirectEntrypoint(kQuickLdiv));
2569 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2570 break;
2571 }
2572 case Primitive::kPrimFloat:
2573 case Primitive::kPrimDouble: {
2574 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2575 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2576 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2577 if (type == Primitive::kPrimFloat) {
2578 __ DivS(dst, lhs, rhs);
2579 } else {
2580 __ DivD(dst, lhs, rhs);
2581 }
2582 break;
2583 }
2584 default:
2585 LOG(FATAL) << "Unexpected div type " << type;
2586 }
2587}
2588
2589void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2590 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2591 ? LocationSummary::kCallOnSlowPath
2592 : LocationSummary::kNoCall;
2593 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2594 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2595 if (instruction->HasUses()) {
2596 locations->SetOut(Location::SameAsFirstInput());
2597 }
2598}
2599
2600void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2601 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2602 codegen_->AddSlowPath(slow_path);
2603 Location value = instruction->GetLocations()->InAt(0);
2604 Primitive::Type type = instruction->GetType();
2605
2606 switch (type) {
2607 case Primitive::kPrimByte:
2608 case Primitive::kPrimChar:
2609 case Primitive::kPrimShort:
2610 case Primitive::kPrimInt: {
2611 if (value.IsConstant()) {
2612 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2613 __ B(slow_path->GetEntryLabel());
2614 } else {
2615 // A division by a non-null constant is valid. We don't need to perform
2616 // any check, so simply fall through.
2617 }
2618 } else {
2619 DCHECK(value.IsRegister()) << value;
2620 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2621 }
2622 break;
2623 }
2624 case Primitive::kPrimLong: {
2625 if (value.IsConstant()) {
2626 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2627 __ B(slow_path->GetEntryLabel());
2628 } else {
2629 // A division by a non-null constant is valid. We don't need to perform
2630 // any check, so simply fall through.
2631 }
2632 } else {
2633 DCHECK(value.IsRegisterPair()) << value;
2634 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2635 __ Beqz(TMP, slow_path->GetEntryLabel());
2636 }
2637 break;
2638 }
2639 default:
2640 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2641 }
2642}
2643
2644void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2645 LocationSummary* locations =
2646 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2647 locations->SetOut(Location::ConstantLocation(constant));
2648}
2649
2650void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2651 // Will be generated at use site.
2652}
2653
2654void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2655 exit->SetLocations(nullptr);
2656}
2657
2658void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2659}
2660
2661void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2662 LocationSummary* locations =
2663 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2664 locations->SetOut(Location::ConstantLocation(constant));
2665}
2666
2667void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2668 // Will be generated at use site.
2669}
2670
2671void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2672 got->SetLocations(nullptr);
2673}
2674
2675void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2676 DCHECK(!successor->IsExitBlock());
2677 HBasicBlock* block = got->GetBlock();
2678 HInstruction* previous = got->GetPrevious();
2679 HLoopInformation* info = block->GetLoopInformation();
2680
2681 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2682 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2683 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2684 return;
2685 }
2686 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2687 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2688 }
2689 if (!codegen_->GoesToNextBlock(block, successor)) {
2690 __ B(codegen_->GetLabelOf(successor));
2691 }
2692}
2693
2694void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2695 HandleGoto(got, got->GetSuccessor());
2696}
2697
2698void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2699 try_boundary->SetLocations(nullptr);
2700}
2701
2702void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2703 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2704 if (!successor->IsExitBlock()) {
2705 HandleGoto(try_boundary, successor);
2706 }
2707}
2708
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002709void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2710 LocationSummary* locations) {
2711 Register dst = locations->Out().AsRegister<Register>();
2712 Register lhs = locations->InAt(0).AsRegister<Register>();
2713 Location rhs_location = locations->InAt(1);
2714 Register rhs_reg = ZERO;
2715 int64_t rhs_imm = 0;
2716 bool use_imm = rhs_location.IsConstant();
2717 if (use_imm) {
2718 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2719 } else {
2720 rhs_reg = rhs_location.AsRegister<Register>();
2721 }
2722
2723 switch (cond) {
2724 case kCondEQ:
2725 case kCondNE:
2726 if (use_imm && IsUint<16>(rhs_imm)) {
2727 __ Xori(dst, lhs, rhs_imm);
2728 } else {
2729 if (use_imm) {
2730 rhs_reg = TMP;
2731 __ LoadConst32(rhs_reg, rhs_imm);
2732 }
2733 __ Xor(dst, lhs, rhs_reg);
2734 }
2735 if (cond == kCondEQ) {
2736 __ Sltiu(dst, dst, 1);
2737 } else {
2738 __ Sltu(dst, ZERO, dst);
2739 }
2740 break;
2741
2742 case kCondLT:
2743 case kCondGE:
2744 if (use_imm && IsInt<16>(rhs_imm)) {
2745 __ Slti(dst, lhs, rhs_imm);
2746 } else {
2747 if (use_imm) {
2748 rhs_reg = TMP;
2749 __ LoadConst32(rhs_reg, rhs_imm);
2750 }
2751 __ Slt(dst, lhs, rhs_reg);
2752 }
2753 if (cond == kCondGE) {
2754 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2755 // only the slt instruction but no sge.
2756 __ Xori(dst, dst, 1);
2757 }
2758 break;
2759
2760 case kCondLE:
2761 case kCondGT:
2762 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2763 // Simulate lhs <= rhs via lhs < rhs + 1.
2764 __ Slti(dst, lhs, rhs_imm + 1);
2765 if (cond == kCondGT) {
2766 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2767 // only the slti instruction but no sgti.
2768 __ Xori(dst, dst, 1);
2769 }
2770 } else {
2771 if (use_imm) {
2772 rhs_reg = TMP;
2773 __ LoadConst32(rhs_reg, rhs_imm);
2774 }
2775 __ Slt(dst, rhs_reg, lhs);
2776 if (cond == kCondLE) {
2777 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2778 // only the slt instruction but no sle.
2779 __ Xori(dst, dst, 1);
2780 }
2781 }
2782 break;
2783
2784 case kCondB:
2785 case kCondAE:
2786 if (use_imm && IsInt<16>(rhs_imm)) {
2787 // Sltiu sign-extends its 16-bit immediate operand before
2788 // the comparison and thus lets us compare directly with
2789 // unsigned values in the ranges [0, 0x7fff] and
2790 // [0xffff8000, 0xffffffff].
2791 __ Sltiu(dst, lhs, rhs_imm);
2792 } else {
2793 if (use_imm) {
2794 rhs_reg = TMP;
2795 __ LoadConst32(rhs_reg, rhs_imm);
2796 }
2797 __ Sltu(dst, lhs, rhs_reg);
2798 }
2799 if (cond == kCondAE) {
2800 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2801 // only the sltu instruction but no sgeu.
2802 __ Xori(dst, dst, 1);
2803 }
2804 break;
2805
2806 case kCondBE:
2807 case kCondA:
2808 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2809 // Simulate lhs <= rhs via lhs < rhs + 1.
2810 // Note that this only works if rhs + 1 does not overflow
2811 // to 0, hence the check above.
2812 // Sltiu sign-extends its 16-bit immediate operand before
2813 // the comparison and thus lets us compare directly with
2814 // unsigned values in the ranges [0, 0x7fff] and
2815 // [0xffff8000, 0xffffffff].
2816 __ Sltiu(dst, lhs, rhs_imm + 1);
2817 if (cond == kCondA) {
2818 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2819 // only the sltiu instruction but no sgtiu.
2820 __ Xori(dst, dst, 1);
2821 }
2822 } else {
2823 if (use_imm) {
2824 rhs_reg = TMP;
2825 __ LoadConst32(rhs_reg, rhs_imm);
2826 }
2827 __ Sltu(dst, rhs_reg, lhs);
2828 if (cond == kCondBE) {
2829 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2830 // only the sltu instruction but no sleu.
2831 __ Xori(dst, dst, 1);
2832 }
2833 }
2834 break;
2835 }
2836}
2837
2838void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2839 LocationSummary* locations,
2840 MipsLabel* label) {
2841 Register lhs = locations->InAt(0).AsRegister<Register>();
2842 Location rhs_location = locations->InAt(1);
2843 Register rhs_reg = ZERO;
2844 int32_t rhs_imm = 0;
2845 bool use_imm = rhs_location.IsConstant();
2846 if (use_imm) {
2847 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2848 } else {
2849 rhs_reg = rhs_location.AsRegister<Register>();
2850 }
2851
2852 if (use_imm && rhs_imm == 0) {
2853 switch (cond) {
2854 case kCondEQ:
2855 case kCondBE: // <= 0 if zero
2856 __ Beqz(lhs, label);
2857 break;
2858 case kCondNE:
2859 case kCondA: // > 0 if non-zero
2860 __ Bnez(lhs, label);
2861 break;
2862 case kCondLT:
2863 __ Bltz(lhs, label);
2864 break;
2865 case kCondGE:
2866 __ Bgez(lhs, label);
2867 break;
2868 case kCondLE:
2869 __ Blez(lhs, label);
2870 break;
2871 case kCondGT:
2872 __ Bgtz(lhs, label);
2873 break;
2874 case kCondB: // always false
2875 break;
2876 case kCondAE: // always true
2877 __ B(label);
2878 break;
2879 }
2880 } else {
2881 if (use_imm) {
2882 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2883 rhs_reg = TMP;
2884 __ LoadConst32(rhs_reg, rhs_imm);
2885 }
2886 switch (cond) {
2887 case kCondEQ:
2888 __ Beq(lhs, rhs_reg, label);
2889 break;
2890 case kCondNE:
2891 __ Bne(lhs, rhs_reg, label);
2892 break;
2893 case kCondLT:
2894 __ Blt(lhs, rhs_reg, label);
2895 break;
2896 case kCondGE:
2897 __ Bge(lhs, rhs_reg, label);
2898 break;
2899 case kCondLE:
2900 __ Bge(rhs_reg, lhs, label);
2901 break;
2902 case kCondGT:
2903 __ Blt(rhs_reg, lhs, label);
2904 break;
2905 case kCondB:
2906 __ Bltu(lhs, rhs_reg, label);
2907 break;
2908 case kCondAE:
2909 __ Bgeu(lhs, rhs_reg, label);
2910 break;
2911 case kCondBE:
2912 __ Bgeu(rhs_reg, lhs, label);
2913 break;
2914 case kCondA:
2915 __ Bltu(rhs_reg, lhs, label);
2916 break;
2917 }
2918 }
2919}
2920
2921void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2922 LocationSummary* locations,
2923 MipsLabel* label) {
2924 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2925 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2926 Location rhs_location = locations->InAt(1);
2927 Register rhs_high = ZERO;
2928 Register rhs_low = ZERO;
2929 int64_t imm = 0;
2930 uint32_t imm_high = 0;
2931 uint32_t imm_low = 0;
2932 bool use_imm = rhs_location.IsConstant();
2933 if (use_imm) {
2934 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2935 imm_high = High32Bits(imm);
2936 imm_low = Low32Bits(imm);
2937 } else {
2938 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2939 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2940 }
2941
2942 if (use_imm && imm == 0) {
2943 switch (cond) {
2944 case kCondEQ:
2945 case kCondBE: // <= 0 if zero
2946 __ Or(TMP, lhs_high, lhs_low);
2947 __ Beqz(TMP, label);
2948 break;
2949 case kCondNE:
2950 case kCondA: // > 0 if non-zero
2951 __ Or(TMP, lhs_high, lhs_low);
2952 __ Bnez(TMP, label);
2953 break;
2954 case kCondLT:
2955 __ Bltz(lhs_high, label);
2956 break;
2957 case kCondGE:
2958 __ Bgez(lhs_high, label);
2959 break;
2960 case kCondLE:
2961 __ Or(TMP, lhs_high, lhs_low);
2962 __ Sra(AT, lhs_high, 31);
2963 __ Bgeu(AT, TMP, label);
2964 break;
2965 case kCondGT:
2966 __ Or(TMP, lhs_high, lhs_low);
2967 __ Sra(AT, lhs_high, 31);
2968 __ Bltu(AT, TMP, label);
2969 break;
2970 case kCondB: // always false
2971 break;
2972 case kCondAE: // always true
2973 __ B(label);
2974 break;
2975 }
2976 } else if (use_imm) {
2977 // TODO: more efficient comparison with constants without loading them into TMP/AT.
2978 switch (cond) {
2979 case kCondEQ:
2980 __ LoadConst32(TMP, imm_high);
2981 __ Xor(TMP, TMP, lhs_high);
2982 __ LoadConst32(AT, imm_low);
2983 __ Xor(AT, AT, lhs_low);
2984 __ Or(TMP, TMP, AT);
2985 __ Beqz(TMP, label);
2986 break;
2987 case kCondNE:
2988 __ LoadConst32(TMP, imm_high);
2989 __ Xor(TMP, TMP, lhs_high);
2990 __ LoadConst32(AT, imm_low);
2991 __ Xor(AT, AT, lhs_low);
2992 __ Or(TMP, TMP, AT);
2993 __ Bnez(TMP, label);
2994 break;
2995 case kCondLT:
2996 __ LoadConst32(TMP, imm_high);
2997 __ Blt(lhs_high, TMP, label);
2998 __ Slt(TMP, TMP, lhs_high);
2999 __ LoadConst32(AT, imm_low);
3000 __ Sltu(AT, lhs_low, AT);
3001 __ Blt(TMP, AT, label);
3002 break;
3003 case kCondGE:
3004 __ LoadConst32(TMP, imm_high);
3005 __ Blt(TMP, lhs_high, label);
3006 __ Slt(TMP, lhs_high, TMP);
3007 __ LoadConst32(AT, imm_low);
3008 __ Sltu(AT, lhs_low, AT);
3009 __ Or(TMP, TMP, AT);
3010 __ Beqz(TMP, label);
3011 break;
3012 case kCondLE:
3013 __ LoadConst32(TMP, imm_high);
3014 __ Blt(lhs_high, TMP, label);
3015 __ Slt(TMP, TMP, lhs_high);
3016 __ LoadConst32(AT, imm_low);
3017 __ Sltu(AT, AT, lhs_low);
3018 __ Or(TMP, TMP, AT);
3019 __ Beqz(TMP, label);
3020 break;
3021 case kCondGT:
3022 __ LoadConst32(TMP, imm_high);
3023 __ Blt(TMP, lhs_high, label);
3024 __ Slt(TMP, lhs_high, TMP);
3025 __ LoadConst32(AT, imm_low);
3026 __ Sltu(AT, AT, lhs_low);
3027 __ Blt(TMP, AT, label);
3028 break;
3029 case kCondB:
3030 __ LoadConst32(TMP, imm_high);
3031 __ Bltu(lhs_high, TMP, label);
3032 __ Sltu(TMP, TMP, lhs_high);
3033 __ LoadConst32(AT, imm_low);
3034 __ Sltu(AT, lhs_low, AT);
3035 __ Blt(TMP, AT, label);
3036 break;
3037 case kCondAE:
3038 __ LoadConst32(TMP, imm_high);
3039 __ Bltu(TMP, lhs_high, label);
3040 __ Sltu(TMP, lhs_high, TMP);
3041 __ LoadConst32(AT, imm_low);
3042 __ Sltu(AT, lhs_low, AT);
3043 __ Or(TMP, TMP, AT);
3044 __ Beqz(TMP, label);
3045 break;
3046 case kCondBE:
3047 __ LoadConst32(TMP, imm_high);
3048 __ Bltu(lhs_high, TMP, label);
3049 __ Sltu(TMP, TMP, lhs_high);
3050 __ LoadConst32(AT, imm_low);
3051 __ Sltu(AT, AT, lhs_low);
3052 __ Or(TMP, TMP, AT);
3053 __ Beqz(TMP, label);
3054 break;
3055 case kCondA:
3056 __ LoadConst32(TMP, imm_high);
3057 __ Bltu(TMP, lhs_high, label);
3058 __ Sltu(TMP, lhs_high, TMP);
3059 __ LoadConst32(AT, imm_low);
3060 __ Sltu(AT, AT, lhs_low);
3061 __ Blt(TMP, AT, label);
3062 break;
3063 }
3064 } else {
3065 switch (cond) {
3066 case kCondEQ:
3067 __ Xor(TMP, lhs_high, rhs_high);
3068 __ Xor(AT, lhs_low, rhs_low);
3069 __ Or(TMP, TMP, AT);
3070 __ Beqz(TMP, label);
3071 break;
3072 case kCondNE:
3073 __ Xor(TMP, lhs_high, rhs_high);
3074 __ Xor(AT, lhs_low, rhs_low);
3075 __ Or(TMP, TMP, AT);
3076 __ Bnez(TMP, label);
3077 break;
3078 case kCondLT:
3079 __ Blt(lhs_high, rhs_high, label);
3080 __ Slt(TMP, rhs_high, lhs_high);
3081 __ Sltu(AT, lhs_low, rhs_low);
3082 __ Blt(TMP, AT, label);
3083 break;
3084 case kCondGE:
3085 __ Blt(rhs_high, lhs_high, label);
3086 __ Slt(TMP, lhs_high, rhs_high);
3087 __ Sltu(AT, lhs_low, rhs_low);
3088 __ Or(TMP, TMP, AT);
3089 __ Beqz(TMP, label);
3090 break;
3091 case kCondLE:
3092 __ Blt(lhs_high, rhs_high, label);
3093 __ Slt(TMP, rhs_high, lhs_high);
3094 __ Sltu(AT, rhs_low, lhs_low);
3095 __ Or(TMP, TMP, AT);
3096 __ Beqz(TMP, label);
3097 break;
3098 case kCondGT:
3099 __ Blt(rhs_high, lhs_high, label);
3100 __ Slt(TMP, lhs_high, rhs_high);
3101 __ Sltu(AT, rhs_low, lhs_low);
3102 __ Blt(TMP, AT, label);
3103 break;
3104 case kCondB:
3105 __ Bltu(lhs_high, rhs_high, label);
3106 __ Sltu(TMP, rhs_high, lhs_high);
3107 __ Sltu(AT, lhs_low, rhs_low);
3108 __ Blt(TMP, AT, label);
3109 break;
3110 case kCondAE:
3111 __ Bltu(rhs_high, lhs_high, label);
3112 __ Sltu(TMP, lhs_high, rhs_high);
3113 __ Sltu(AT, lhs_low, rhs_low);
3114 __ Or(TMP, TMP, AT);
3115 __ Beqz(TMP, label);
3116 break;
3117 case kCondBE:
3118 __ Bltu(lhs_high, rhs_high, label);
3119 __ Sltu(TMP, rhs_high, lhs_high);
3120 __ Sltu(AT, rhs_low, lhs_low);
3121 __ Or(TMP, TMP, AT);
3122 __ Beqz(TMP, label);
3123 break;
3124 case kCondA:
3125 __ Bltu(rhs_high, lhs_high, label);
3126 __ Sltu(TMP, lhs_high, rhs_high);
3127 __ Sltu(AT, rhs_low, lhs_low);
3128 __ Blt(TMP, AT, label);
3129 break;
3130 }
3131 }
3132}
3133
3134void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3135 bool gt_bias,
3136 Primitive::Type type,
3137 LocationSummary* locations,
3138 MipsLabel* label) {
3139 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3140 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3141 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3142 if (type == Primitive::kPrimFloat) {
3143 if (isR6) {
3144 switch (cond) {
3145 case kCondEQ:
3146 __ CmpEqS(FTMP, lhs, rhs);
3147 __ Bc1nez(FTMP, label);
3148 break;
3149 case kCondNE:
3150 __ CmpEqS(FTMP, lhs, rhs);
3151 __ Bc1eqz(FTMP, label);
3152 break;
3153 case kCondLT:
3154 if (gt_bias) {
3155 __ CmpLtS(FTMP, lhs, rhs);
3156 } else {
3157 __ CmpUltS(FTMP, lhs, rhs);
3158 }
3159 __ Bc1nez(FTMP, label);
3160 break;
3161 case kCondLE:
3162 if (gt_bias) {
3163 __ CmpLeS(FTMP, lhs, rhs);
3164 } else {
3165 __ CmpUleS(FTMP, lhs, rhs);
3166 }
3167 __ Bc1nez(FTMP, label);
3168 break;
3169 case kCondGT:
3170 if (gt_bias) {
3171 __ CmpUltS(FTMP, rhs, lhs);
3172 } else {
3173 __ CmpLtS(FTMP, rhs, lhs);
3174 }
3175 __ Bc1nez(FTMP, label);
3176 break;
3177 case kCondGE:
3178 if (gt_bias) {
3179 __ CmpUleS(FTMP, rhs, lhs);
3180 } else {
3181 __ CmpLeS(FTMP, rhs, lhs);
3182 }
3183 __ Bc1nez(FTMP, label);
3184 break;
3185 default:
3186 LOG(FATAL) << "Unexpected non-floating-point condition";
3187 }
3188 } else {
3189 switch (cond) {
3190 case kCondEQ:
3191 __ CeqS(0, lhs, rhs);
3192 __ Bc1t(0, label);
3193 break;
3194 case kCondNE:
3195 __ CeqS(0, lhs, rhs);
3196 __ Bc1f(0, label);
3197 break;
3198 case kCondLT:
3199 if (gt_bias) {
3200 __ ColtS(0, lhs, rhs);
3201 } else {
3202 __ CultS(0, lhs, rhs);
3203 }
3204 __ Bc1t(0, label);
3205 break;
3206 case kCondLE:
3207 if (gt_bias) {
3208 __ ColeS(0, lhs, rhs);
3209 } else {
3210 __ CuleS(0, lhs, rhs);
3211 }
3212 __ Bc1t(0, label);
3213 break;
3214 case kCondGT:
3215 if (gt_bias) {
3216 __ CultS(0, rhs, lhs);
3217 } else {
3218 __ ColtS(0, rhs, lhs);
3219 }
3220 __ Bc1t(0, label);
3221 break;
3222 case kCondGE:
3223 if (gt_bias) {
3224 __ CuleS(0, rhs, lhs);
3225 } else {
3226 __ ColeS(0, rhs, lhs);
3227 }
3228 __ Bc1t(0, label);
3229 break;
3230 default:
3231 LOG(FATAL) << "Unexpected non-floating-point condition";
3232 }
3233 }
3234 } else {
3235 DCHECK_EQ(type, Primitive::kPrimDouble);
3236 if (isR6) {
3237 switch (cond) {
3238 case kCondEQ:
3239 __ CmpEqD(FTMP, lhs, rhs);
3240 __ Bc1nez(FTMP, label);
3241 break;
3242 case kCondNE:
3243 __ CmpEqD(FTMP, lhs, rhs);
3244 __ Bc1eqz(FTMP, label);
3245 break;
3246 case kCondLT:
3247 if (gt_bias) {
3248 __ CmpLtD(FTMP, lhs, rhs);
3249 } else {
3250 __ CmpUltD(FTMP, lhs, rhs);
3251 }
3252 __ Bc1nez(FTMP, label);
3253 break;
3254 case kCondLE:
3255 if (gt_bias) {
3256 __ CmpLeD(FTMP, lhs, rhs);
3257 } else {
3258 __ CmpUleD(FTMP, lhs, rhs);
3259 }
3260 __ Bc1nez(FTMP, label);
3261 break;
3262 case kCondGT:
3263 if (gt_bias) {
3264 __ CmpUltD(FTMP, rhs, lhs);
3265 } else {
3266 __ CmpLtD(FTMP, rhs, lhs);
3267 }
3268 __ Bc1nez(FTMP, label);
3269 break;
3270 case kCondGE:
3271 if (gt_bias) {
3272 __ CmpUleD(FTMP, rhs, lhs);
3273 } else {
3274 __ CmpLeD(FTMP, rhs, lhs);
3275 }
3276 __ Bc1nez(FTMP, label);
3277 break;
3278 default:
3279 LOG(FATAL) << "Unexpected non-floating-point condition";
3280 }
3281 } else {
3282 switch (cond) {
3283 case kCondEQ:
3284 __ CeqD(0, lhs, rhs);
3285 __ Bc1t(0, label);
3286 break;
3287 case kCondNE:
3288 __ CeqD(0, lhs, rhs);
3289 __ Bc1f(0, label);
3290 break;
3291 case kCondLT:
3292 if (gt_bias) {
3293 __ ColtD(0, lhs, rhs);
3294 } else {
3295 __ CultD(0, lhs, rhs);
3296 }
3297 __ Bc1t(0, label);
3298 break;
3299 case kCondLE:
3300 if (gt_bias) {
3301 __ ColeD(0, lhs, rhs);
3302 } else {
3303 __ CuleD(0, lhs, rhs);
3304 }
3305 __ Bc1t(0, label);
3306 break;
3307 case kCondGT:
3308 if (gt_bias) {
3309 __ CultD(0, rhs, lhs);
3310 } else {
3311 __ ColtD(0, rhs, lhs);
3312 }
3313 __ Bc1t(0, label);
3314 break;
3315 case kCondGE:
3316 if (gt_bias) {
3317 __ CuleD(0, rhs, lhs);
3318 } else {
3319 __ ColeD(0, rhs, lhs);
3320 }
3321 __ Bc1t(0, label);
3322 break;
3323 default:
3324 LOG(FATAL) << "Unexpected non-floating-point condition";
3325 }
3326 }
3327 }
3328}
3329
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003330void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003331 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003332 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003333 MipsLabel* false_target) {
3334 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003335
David Brazdil0debae72015-11-12 18:37:00 +00003336 if (true_target == nullptr && false_target == nullptr) {
3337 // Nothing to do. The code always falls through.
3338 return;
3339 } else if (cond->IsIntConstant()) {
3340 // Constant condition, statically compared against 1.
3341 if (cond->AsIntConstant()->IsOne()) {
3342 if (true_target != nullptr) {
3343 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003344 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003345 } else {
David Brazdil0debae72015-11-12 18:37:00 +00003346 DCHECK(cond->AsIntConstant()->IsZero());
3347 if (false_target != nullptr) {
3348 __ B(false_target);
3349 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003350 }
David Brazdil0debae72015-11-12 18:37:00 +00003351 return;
3352 }
3353
3354 // The following code generates these patterns:
3355 // (1) true_target == nullptr && false_target != nullptr
3356 // - opposite condition true => branch to false_target
3357 // (2) true_target != nullptr && false_target == nullptr
3358 // - condition true => branch to true_target
3359 // (3) true_target != nullptr && false_target != nullptr
3360 // - condition true => branch to true_target
3361 // - branch to false_target
3362 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003363 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003364 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003365 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003366 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003367 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3368 } else {
3369 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3370 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003371 } else {
3372 // The condition instruction has not been materialized, use its inputs as
3373 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003374 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003375 Primitive::Type type = condition->InputAt(0)->GetType();
3376 LocationSummary* locations = cond->GetLocations();
3377 IfCondition if_cond = condition->GetCondition();
3378 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003379
David Brazdil0debae72015-11-12 18:37:00 +00003380 if (true_target == nullptr) {
3381 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003382 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003383 }
3384
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003385 switch (type) {
3386 default:
3387 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3388 break;
3389 case Primitive::kPrimLong:
3390 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3391 break;
3392 case Primitive::kPrimFloat:
3393 case Primitive::kPrimDouble:
3394 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3395 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003396 }
3397 }
David Brazdil0debae72015-11-12 18:37:00 +00003398
3399 // If neither branch falls through (case 3), the conditional branch to `true_target`
3400 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3401 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003402 __ B(false_target);
3403 }
3404}
3405
3406void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3407 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003408 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003409 locations->SetInAt(0, Location::RequiresRegister());
3410 }
3411}
3412
3413void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003414 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3415 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3416 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3417 nullptr : codegen_->GetLabelOf(true_successor);
3418 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3419 nullptr : codegen_->GetLabelOf(false_successor);
3420 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003421}
3422
3423void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3424 LocationSummary* locations = new (GetGraph()->GetArena())
3425 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003426 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003427 locations->SetInAt(0, Location::RequiresRegister());
3428 }
3429}
3430
3431void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003432 SlowPathCodeMIPS* slow_path =
3433 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003434 GenerateTestAndBranch(deoptimize,
3435 /* condition_input_index */ 0,
3436 slow_path->GetEntryLabel(),
3437 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003438}
3439
David Srbecky0cf44932015-12-09 14:09:59 +00003440void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3441 new (GetGraph()->GetArena()) LocationSummary(info);
3442}
3443
3444void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00003445 if (codegen_->HasStackMapAtCurrentPc()) {
3446 // Ensure that we do not collide with the stack map of the previous instruction.
3447 __ Nop();
3448 }
David Srbecky0cf44932015-12-09 14:09:59 +00003449 codegen_->RecordPcInfo(info, info->GetDexPc());
3450}
3451
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003452void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3453 Primitive::Type field_type = field_info.GetFieldType();
3454 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3455 bool generate_volatile = field_info.IsVolatile() && is_wide;
3456 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3457 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3458
3459 locations->SetInAt(0, Location::RequiresRegister());
3460 if (generate_volatile) {
3461 InvokeRuntimeCallingConvention calling_convention;
3462 // need A0 to hold base + offset
3463 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3464 if (field_type == Primitive::kPrimLong) {
3465 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3466 } else {
3467 locations->SetOut(Location::RequiresFpuRegister());
3468 // Need some temp core regs since FP results are returned in core registers
3469 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3470 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3471 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3472 }
3473 } else {
3474 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3475 locations->SetOut(Location::RequiresFpuRegister());
3476 } else {
3477 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3478 }
3479 }
3480}
3481
3482void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3483 const FieldInfo& field_info,
3484 uint32_t dex_pc) {
3485 Primitive::Type type = field_info.GetFieldType();
3486 LocationSummary* locations = instruction->GetLocations();
3487 Register obj = locations->InAt(0).AsRegister<Register>();
3488 LoadOperandType load_type = kLoadUnsignedByte;
3489 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003490 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003491
3492 switch (type) {
3493 case Primitive::kPrimBoolean:
3494 load_type = kLoadUnsignedByte;
3495 break;
3496 case Primitive::kPrimByte:
3497 load_type = kLoadSignedByte;
3498 break;
3499 case Primitive::kPrimShort:
3500 load_type = kLoadSignedHalfword;
3501 break;
3502 case Primitive::kPrimChar:
3503 load_type = kLoadUnsignedHalfword;
3504 break;
3505 case Primitive::kPrimInt:
3506 case Primitive::kPrimFloat:
3507 case Primitive::kPrimNot:
3508 load_type = kLoadWord;
3509 break;
3510 case Primitive::kPrimLong:
3511 case Primitive::kPrimDouble:
3512 load_type = kLoadDoubleword;
3513 break;
3514 case Primitive::kPrimVoid:
3515 LOG(FATAL) << "Unreachable type " << type;
3516 UNREACHABLE();
3517 }
3518
3519 if (is_volatile && load_type == kLoadDoubleword) {
3520 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003521 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003522 // Do implicit Null check
3523 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3524 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3525 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3526 instruction,
3527 dex_pc,
3528 nullptr,
3529 IsDirectEntrypoint(kQuickA64Load));
3530 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3531 if (type == Primitive::kPrimDouble) {
3532 // Need to move to FP regs since FP results are returned in core registers.
3533 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
3534 locations->Out().AsFpuRegister<FRegister>());
3535 __ Mthc1(locations->GetTemp(2).AsRegister<Register>(),
3536 locations->Out().AsFpuRegister<FRegister>());
3537 }
3538 } else {
3539 if (!Primitive::IsFloatingPointType(type)) {
3540 Register dst;
3541 if (type == Primitive::kPrimLong) {
3542 DCHECK(locations->Out().IsRegisterPair());
3543 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003544 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3545 if (obj == dst) {
3546 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3547 codegen_->MaybeRecordImplicitNullCheck(instruction);
3548 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3549 } else {
3550 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3551 codegen_->MaybeRecordImplicitNullCheck(instruction);
3552 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3553 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003554 } else {
3555 DCHECK(locations->Out().IsRegister());
3556 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003557 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003558 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003559 } else {
3560 DCHECK(locations->Out().IsFpuRegister());
3561 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3562 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003563 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003564 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003565 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003566 }
3567 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003568 // Longs are handled earlier.
3569 if (type != Primitive::kPrimLong) {
3570 codegen_->MaybeRecordImplicitNullCheck(instruction);
3571 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003572 }
3573
3574 if (is_volatile) {
3575 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3576 }
3577}
3578
3579void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3580 Primitive::Type field_type = field_info.GetFieldType();
3581 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3582 bool generate_volatile = field_info.IsVolatile() && is_wide;
3583 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3584 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3585
3586 locations->SetInAt(0, Location::RequiresRegister());
3587 if (generate_volatile) {
3588 InvokeRuntimeCallingConvention calling_convention;
3589 // need A0 to hold base + offset
3590 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3591 if (field_type == Primitive::kPrimLong) {
3592 locations->SetInAt(1, Location::RegisterPairLocation(
3593 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3594 } else {
3595 locations->SetInAt(1, Location::RequiresFpuRegister());
3596 // Pass FP parameters in core registers.
3597 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3598 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3599 }
3600 } else {
3601 if (Primitive::IsFloatingPointType(field_type)) {
3602 locations->SetInAt(1, Location::RequiresFpuRegister());
3603 } else {
3604 locations->SetInAt(1, Location::RequiresRegister());
3605 }
3606 }
3607}
3608
3609void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3610 const FieldInfo& field_info,
3611 uint32_t dex_pc) {
3612 Primitive::Type type = field_info.GetFieldType();
3613 LocationSummary* locations = instruction->GetLocations();
3614 Register obj = locations->InAt(0).AsRegister<Register>();
3615 StoreOperandType store_type = kStoreByte;
3616 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003617 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003618
3619 switch (type) {
3620 case Primitive::kPrimBoolean:
3621 case Primitive::kPrimByte:
3622 store_type = kStoreByte;
3623 break;
3624 case Primitive::kPrimShort:
3625 case Primitive::kPrimChar:
3626 store_type = kStoreHalfword;
3627 break;
3628 case Primitive::kPrimInt:
3629 case Primitive::kPrimFloat:
3630 case Primitive::kPrimNot:
3631 store_type = kStoreWord;
3632 break;
3633 case Primitive::kPrimLong:
3634 case Primitive::kPrimDouble:
3635 store_type = kStoreDoubleword;
3636 break;
3637 case Primitive::kPrimVoid:
3638 LOG(FATAL) << "Unreachable type " << type;
3639 UNREACHABLE();
3640 }
3641
3642 if (is_volatile) {
3643 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3644 }
3645
3646 if (is_volatile && store_type == kStoreDoubleword) {
3647 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003648 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003649 // Do implicit Null check.
3650 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3651 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3652 if (type == Primitive::kPrimDouble) {
3653 // Pass FP parameters in core registers.
3654 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
3655 locations->InAt(1).AsFpuRegister<FRegister>());
3656 __ Mfhc1(locations->GetTemp(2).AsRegister<Register>(),
3657 locations->InAt(1).AsFpuRegister<FRegister>());
3658 }
3659 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3660 instruction,
3661 dex_pc,
3662 nullptr,
3663 IsDirectEntrypoint(kQuickA64Store));
3664 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3665 } else {
3666 if (!Primitive::IsFloatingPointType(type)) {
3667 Register src;
3668 if (type == Primitive::kPrimLong) {
3669 DCHECK(locations->InAt(1).IsRegisterPair());
3670 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003671 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3672 __ StoreToOffset(kStoreWord, src, obj, offset);
3673 codegen_->MaybeRecordImplicitNullCheck(instruction);
3674 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003675 } else {
3676 DCHECK(locations->InAt(1).IsRegister());
3677 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003678 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003679 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003680 } else {
3681 DCHECK(locations->InAt(1).IsFpuRegister());
3682 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3683 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003684 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003685 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003686 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003687 }
3688 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003689 // Longs are handled earlier.
3690 if (type != Primitive::kPrimLong) {
3691 codegen_->MaybeRecordImplicitNullCheck(instruction);
3692 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003693 }
3694
3695 // TODO: memory barriers?
3696 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3697 DCHECK(locations->InAt(1).IsRegister());
3698 Register src = locations->InAt(1).AsRegister<Register>();
3699 codegen_->MarkGCCard(obj, src);
3700 }
3701
3702 if (is_volatile) {
3703 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3704 }
3705}
3706
3707void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3708 HandleFieldGet(instruction, instruction->GetFieldInfo());
3709}
3710
3711void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3712 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3713}
3714
3715void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3716 HandleFieldSet(instruction, instruction->GetFieldInfo());
3717}
3718
3719void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3720 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3721}
3722
3723void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3724 LocationSummary::CallKind call_kind =
3725 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3726 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3727 locations->SetInAt(0, Location::RequiresRegister());
3728 locations->SetInAt(1, Location::RequiresRegister());
3729 // The output does overlap inputs.
3730 // Note that TypeCheckSlowPathMIPS uses this register too.
3731 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3732}
3733
3734void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3735 LocationSummary* locations = instruction->GetLocations();
3736 Register obj = locations->InAt(0).AsRegister<Register>();
3737 Register cls = locations->InAt(1).AsRegister<Register>();
3738 Register out = locations->Out().AsRegister<Register>();
3739
3740 MipsLabel done;
3741
3742 // Return 0 if `obj` is null.
3743 // TODO: Avoid this check if we know `obj` is not null.
3744 __ Move(out, ZERO);
3745 __ Beqz(obj, &done);
3746
3747 // Compare the class of `obj` with `cls`.
3748 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3749 if (instruction->IsExactCheck()) {
3750 // Classes must be equal for the instanceof to succeed.
3751 __ Xor(out, out, cls);
3752 __ Sltiu(out, out, 1);
3753 } else {
3754 // If the classes are not equal, we go into a slow path.
3755 DCHECK(locations->OnlyCallsOnSlowPath());
3756 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3757 codegen_->AddSlowPath(slow_path);
3758 __ Bne(out, cls, slow_path->GetEntryLabel());
3759 __ LoadConst32(out, 1);
3760 __ Bind(slow_path->GetExitLabel());
3761 }
3762
3763 __ Bind(&done);
3764}
3765
3766void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3767 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3768 locations->SetOut(Location::ConstantLocation(constant));
3769}
3770
3771void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3772 // Will be generated at use site.
3773}
3774
3775void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3776 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3777 locations->SetOut(Location::ConstantLocation(constant));
3778}
3779
3780void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3781 // Will be generated at use site.
3782}
3783
3784void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3785 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3786 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3787}
3788
3789void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3790 HandleInvoke(invoke);
3791 // The register T0 is required to be used for the hidden argument in
3792 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3793 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3794}
3795
3796void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3797 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3798 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3799 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3800 invoke->GetImtIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
3801 Location receiver = invoke->GetLocations()->InAt(0);
3802 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3803 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3804
3805 // Set the hidden argument.
3806 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3807 invoke->GetDexMethodIndex());
3808
3809 // temp = object->GetClass();
3810 if (receiver.IsStackSlot()) {
3811 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3812 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3813 } else {
3814 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3815 }
3816 codegen_->MaybeRecordImplicitNullCheck(invoke);
3817 // temp = temp->GetImtEntryAt(method_offset);
3818 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3819 // T9 = temp->GetEntryPoint();
3820 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3821 // T9();
3822 __ Jalr(T9);
3823 __ Nop();
3824 DCHECK(!codegen_->IsLeafMethod());
3825 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3826}
3827
3828void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003829 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3830 if (intrinsic.TryDispatch(invoke)) {
3831 return;
3832 }
3833
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003834 HandleInvoke(invoke);
3835}
3836
3837void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3838 // When we do not run baseline, explicit clinit checks triggered by static
3839 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3840 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
3841
Chris Larsen701566a2015-10-27 15:29:13 -07003842 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3843 if (intrinsic.TryDispatch(invoke)) {
3844 return;
3845 }
3846
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003847 HandleInvoke(invoke);
3848}
3849
Chris Larsen701566a2015-10-27 15:29:13 -07003850static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003851 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003852 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3853 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003854 return true;
3855 }
3856 return false;
3857}
3858
Vladimir Markodc151b22015-10-15 18:02:30 +01003859HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3860 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3861 MethodReference target_method ATTRIBUTE_UNUSED) {
3862 switch (desired_dispatch_info.method_load_kind) {
3863 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3864 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3865 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3866 return HInvokeStaticOrDirect::DispatchInfo {
3867 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3868 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3869 0u,
3870 0u
3871 };
3872 default:
3873 break;
3874 }
3875 switch (desired_dispatch_info.code_ptr_location) {
3876 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3877 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3878 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3879 return HInvokeStaticOrDirect::DispatchInfo {
3880 desired_dispatch_info.method_load_kind,
3881 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3882 desired_dispatch_info.method_load_data,
3883 0u
3884 };
3885 default:
3886 return desired_dispatch_info;
3887 }
3888}
3889
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003890void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3891 // All registers are assumed to be correctly set up per the calling convention.
3892
3893 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3894 switch (invoke->GetMethodLoadKind()) {
3895 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3896 // temp = thread->string_init_entrypoint
3897 __ LoadFromOffset(kLoadWord,
3898 temp.AsRegister<Register>(),
3899 TR,
3900 invoke->GetStringInitOffset());
3901 break;
3902 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003903 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003904 break;
3905 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3906 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3907 break;
3908 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003909 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003910 // TODO: Implement these types.
3911 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3912 LOG(FATAL) << "Unsupported";
3913 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003914 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003915 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003916 Register reg = temp.AsRegister<Register>();
3917 Register method_reg;
3918 if (current_method.IsRegister()) {
3919 method_reg = current_method.AsRegister<Register>();
3920 } else {
3921 // TODO: use the appropriate DCHECK() here if possible.
3922 // DCHECK(invoke->GetLocations()->Intrinsified());
3923 DCHECK(!current_method.IsValid());
3924 method_reg = reg;
3925 __ Lw(reg, SP, kCurrentMethodStackOffset);
3926 }
3927
3928 // temp = temp->dex_cache_resolved_methods_;
3929 __ LoadFromOffset(kLoadWord,
3930 reg,
3931 method_reg,
3932 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
3933 // temp = temp[index_in_cache]
3934 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3935 __ LoadFromOffset(kLoadWord,
3936 reg,
3937 reg,
3938 CodeGenerator::GetCachePointerOffset(index_in_cache));
3939 break;
3940 }
3941 }
3942
3943 switch (invoke->GetCodePtrLocation()) {
3944 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3945 __ Jalr(&frame_entry_label_, T9);
3946 break;
3947 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3948 // LR = invoke->GetDirectCodePtr();
3949 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3950 // LR()
3951 __ Jalr(T9);
3952 __ Nop();
3953 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003954 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003955 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3956 // TODO: Implement these types.
3957 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3958 LOG(FATAL) << "Unsupported";
3959 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003960 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3961 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01003962 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003963 T9,
3964 callee_method.AsRegister<Register>(),
3965 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3966 kMipsWordSize).Int32Value());
3967 // T9()
3968 __ Jalr(T9);
3969 __ Nop();
3970 break;
3971 }
3972 DCHECK(!IsLeafMethod());
3973}
3974
3975void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3976 // When we do not run baseline, explicit clinit checks triggered by static
3977 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3978 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
3979
3980 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3981 return;
3982 }
3983
3984 LocationSummary* locations = invoke->GetLocations();
3985 codegen_->GenerateStaticOrDirectCall(invoke,
3986 locations->HasTemps()
3987 ? locations->GetTemp(0)
3988 : Location::NoLocation());
3989 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3990}
3991
3992void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003993 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3994 return;
3995 }
3996
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003997 LocationSummary* locations = invoke->GetLocations();
3998 Location receiver = locations->InAt(0);
3999 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
4000 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4001 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4002 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4003 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4004
4005 // temp = object->GetClass();
4006 if (receiver.IsStackSlot()) {
4007 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
4008 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
4009 } else {
4010 DCHECK(receiver.IsRegister());
4011 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4012 }
4013 codegen_->MaybeRecordImplicitNullCheck(invoke);
4014 // temp = temp->GetMethodAt(method_offset);
4015 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4016 // T9 = temp->GetEntryPoint();
4017 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4018 // T9();
4019 __ Jalr(T9);
4020 __ Nop();
4021 DCHECK(!codegen_->IsLeafMethod());
4022 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4023}
4024
4025void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01004026 InvokeRuntimeCallingConvention calling_convention;
4027 CodeGenerator::CreateLoadClassLocationSummary(
4028 cls,
4029 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4030 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004031}
4032
4033void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4034 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004035 if (cls->NeedsAccessCheck()) {
4036 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4037 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4038 cls,
4039 cls->GetDexPc(),
4040 nullptr,
4041 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00004042 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004043 return;
4044 }
4045
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004046 Register out = locations->Out().AsRegister<Register>();
4047 Register current_method = locations->InAt(0).AsRegister<Register>();
4048 if (cls->IsReferrersClass()) {
4049 DCHECK(!cls->CanCallRuntime());
4050 DCHECK(!cls->MustGenerateClinitCheck());
4051 __ LoadFromOffset(kLoadWord, out, current_method,
4052 ArtMethod::DeclaringClassOffset().Int32Value());
4053 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004054 __ LoadFromOffset(kLoadWord, out, current_method,
4055 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
4056 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004057
4058 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
4059 DCHECK(cls->CanCallRuntime());
4060 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4061 cls,
4062 cls,
4063 cls->GetDexPc(),
4064 cls->MustGenerateClinitCheck());
4065 codegen_->AddSlowPath(slow_path);
4066 if (!cls->IsInDexCache()) {
4067 __ Beqz(out, slow_path->GetEntryLabel());
4068 }
4069 if (cls->MustGenerateClinitCheck()) {
4070 GenerateClassInitializationCheck(slow_path, out);
4071 } else {
4072 __ Bind(slow_path->GetExitLabel());
4073 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004074 }
4075 }
4076}
4077
4078static int32_t GetExceptionTlsOffset() {
4079 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
4080}
4081
4082void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4083 LocationSummary* locations =
4084 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4085 locations->SetOut(Location::RequiresRegister());
4086}
4087
4088void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4089 Register out = load->GetLocations()->Out().AsRegister<Register>();
4090 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4091}
4092
4093void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4094 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4095}
4096
4097void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4098 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4099}
4100
4101void LocationsBuilderMIPS::VisitLoadLocal(HLoadLocal* load) {
4102 load->SetLocations(nullptr);
4103}
4104
4105void InstructionCodeGeneratorMIPS::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
4106 // Nothing to do, this is driven by the code generator.
4107}
4108
4109void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00004110 LocationSummary::CallKind call_kind = load->IsInDexCache()
4111 ? LocationSummary::kNoCall
4112 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004114 locations->SetInAt(0, Location::RequiresRegister());
4115 locations->SetOut(Location::RequiresRegister());
4116}
4117
4118void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004119 LocationSummary* locations = load->GetLocations();
4120 Register out = locations->Out().AsRegister<Register>();
4121 Register current_method = locations->InAt(0).AsRegister<Register>();
4122 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4123 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4124 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004125
4126 if (!load->IsInDexCache()) {
4127 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4128 codegen_->AddSlowPath(slow_path);
4129 __ Beqz(out, slow_path->GetEntryLabel());
4130 __ Bind(slow_path->GetExitLabel());
4131 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004132}
4133
4134void LocationsBuilderMIPS::VisitLocal(HLocal* local) {
4135 local->SetLocations(nullptr);
4136}
4137
4138void InstructionCodeGeneratorMIPS::VisitLocal(HLocal* local) {
4139 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
4140}
4141
4142void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4143 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4144 locations->SetOut(Location::ConstantLocation(constant));
4145}
4146
4147void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4148 // Will be generated at use site.
4149}
4150
4151void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4152 LocationSummary* locations =
4153 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4154 InvokeRuntimeCallingConvention calling_convention;
4155 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4156}
4157
4158void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4159 if (instruction->IsEnter()) {
4160 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4161 instruction,
4162 instruction->GetDexPc(),
4163 nullptr,
4164 IsDirectEntrypoint(kQuickLockObject));
4165 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4166 } else {
4167 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4168 instruction,
4169 instruction->GetDexPc(),
4170 nullptr,
4171 IsDirectEntrypoint(kQuickUnlockObject));
4172 }
4173 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4174}
4175
4176void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4177 LocationSummary* locations =
4178 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4179 switch (mul->GetResultType()) {
4180 case Primitive::kPrimInt:
4181 case Primitive::kPrimLong:
4182 locations->SetInAt(0, Location::RequiresRegister());
4183 locations->SetInAt(1, Location::RequiresRegister());
4184 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4185 break;
4186
4187 case Primitive::kPrimFloat:
4188 case Primitive::kPrimDouble:
4189 locations->SetInAt(0, Location::RequiresFpuRegister());
4190 locations->SetInAt(1, Location::RequiresFpuRegister());
4191 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4192 break;
4193
4194 default:
4195 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4196 }
4197}
4198
4199void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4200 Primitive::Type type = instruction->GetType();
4201 LocationSummary* locations = instruction->GetLocations();
4202 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4203
4204 switch (type) {
4205 case Primitive::kPrimInt: {
4206 Register dst = locations->Out().AsRegister<Register>();
4207 Register lhs = locations->InAt(0).AsRegister<Register>();
4208 Register rhs = locations->InAt(1).AsRegister<Register>();
4209
4210 if (isR6) {
4211 __ MulR6(dst, lhs, rhs);
4212 } else {
4213 __ MulR2(dst, lhs, rhs);
4214 }
4215 break;
4216 }
4217 case Primitive::kPrimLong: {
4218 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4219 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4220 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4221 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4222 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4223 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4224
4225 // Extra checks to protect caused by the existance of A1_A2.
4226 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4227 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4228 DCHECK_NE(dst_high, lhs_low);
4229 DCHECK_NE(dst_high, rhs_low);
4230
4231 // A_B * C_D
4232 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4233 // dst_lo: [ low(B*D) ]
4234 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4235
4236 if (isR6) {
4237 __ MulR6(TMP, lhs_high, rhs_low);
4238 __ MulR6(dst_high, lhs_low, rhs_high);
4239 __ Addu(dst_high, dst_high, TMP);
4240 __ MuhuR6(TMP, lhs_low, rhs_low);
4241 __ Addu(dst_high, dst_high, TMP);
4242 __ MulR6(dst_low, lhs_low, rhs_low);
4243 } else {
4244 __ MulR2(TMP, lhs_high, rhs_low);
4245 __ MulR2(dst_high, lhs_low, rhs_high);
4246 __ Addu(dst_high, dst_high, TMP);
4247 __ MultuR2(lhs_low, rhs_low);
4248 __ Mfhi(TMP);
4249 __ Addu(dst_high, dst_high, TMP);
4250 __ Mflo(dst_low);
4251 }
4252 break;
4253 }
4254 case Primitive::kPrimFloat:
4255 case Primitive::kPrimDouble: {
4256 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4257 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4258 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4259 if (type == Primitive::kPrimFloat) {
4260 __ MulS(dst, lhs, rhs);
4261 } else {
4262 __ MulD(dst, lhs, rhs);
4263 }
4264 break;
4265 }
4266 default:
4267 LOG(FATAL) << "Unexpected mul type " << type;
4268 }
4269}
4270
4271void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4272 LocationSummary* locations =
4273 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4274 switch (neg->GetResultType()) {
4275 case Primitive::kPrimInt:
4276 case Primitive::kPrimLong:
4277 locations->SetInAt(0, Location::RequiresRegister());
4278 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4279 break;
4280
4281 case Primitive::kPrimFloat:
4282 case Primitive::kPrimDouble:
4283 locations->SetInAt(0, Location::RequiresFpuRegister());
4284 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4285 break;
4286
4287 default:
4288 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4289 }
4290}
4291
4292void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4293 Primitive::Type type = instruction->GetType();
4294 LocationSummary* locations = instruction->GetLocations();
4295
4296 switch (type) {
4297 case Primitive::kPrimInt: {
4298 Register dst = locations->Out().AsRegister<Register>();
4299 Register src = locations->InAt(0).AsRegister<Register>();
4300 __ Subu(dst, ZERO, src);
4301 break;
4302 }
4303 case Primitive::kPrimLong: {
4304 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4305 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4306 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4307 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4308 __ Subu(dst_low, ZERO, src_low);
4309 __ Sltu(TMP, ZERO, dst_low);
4310 __ Subu(dst_high, ZERO, src_high);
4311 __ Subu(dst_high, dst_high, TMP);
4312 break;
4313 }
4314 case Primitive::kPrimFloat:
4315 case Primitive::kPrimDouble: {
4316 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4317 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4318 if (type == Primitive::kPrimFloat) {
4319 __ NegS(dst, src);
4320 } else {
4321 __ NegD(dst, src);
4322 }
4323 break;
4324 }
4325 default:
4326 LOG(FATAL) << "Unexpected neg type " << type;
4327 }
4328}
4329
4330void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4331 LocationSummary* locations =
4332 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4333 InvokeRuntimeCallingConvention calling_convention;
4334 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4335 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4336 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4337 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4338}
4339
4340void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4341 InvokeRuntimeCallingConvention calling_convention;
4342 Register current_method_register = calling_convention.GetRegisterAt(2);
4343 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4344 // Move an uint16_t value to a register.
4345 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4346 codegen_->InvokeRuntime(
4347 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4348 instruction,
4349 instruction->GetDexPc(),
4350 nullptr,
4351 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4352 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4353 void*, uint32_t, int32_t, ArtMethod*>();
4354}
4355
4356void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4357 LocationSummary* locations =
4358 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4359 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00004360 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4361 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004362 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4363}
4364
4365void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004366 codegen_->InvokeRuntime(
4367 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4368 instruction,
4369 instruction->GetDexPc(),
4370 nullptr,
4371 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4372 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4373}
4374
4375void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4376 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4377 locations->SetInAt(0, Location::RequiresRegister());
4378 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4379}
4380
4381void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4382 Primitive::Type type = instruction->GetType();
4383 LocationSummary* locations = instruction->GetLocations();
4384
4385 switch (type) {
4386 case Primitive::kPrimInt: {
4387 Register dst = locations->Out().AsRegister<Register>();
4388 Register src = locations->InAt(0).AsRegister<Register>();
4389 __ Nor(dst, src, ZERO);
4390 break;
4391 }
4392
4393 case Primitive::kPrimLong: {
4394 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4395 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4396 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4397 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4398 __ Nor(dst_high, src_high, ZERO);
4399 __ Nor(dst_low, src_low, ZERO);
4400 break;
4401 }
4402
4403 default:
4404 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4405 }
4406}
4407
4408void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4409 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4410 locations->SetInAt(0, Location::RequiresRegister());
4411 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4412}
4413
4414void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4415 LocationSummary* locations = instruction->GetLocations();
4416 __ Xori(locations->Out().AsRegister<Register>(),
4417 locations->InAt(0).AsRegister<Register>(),
4418 1);
4419}
4420
4421void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4422 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4423 ? LocationSummary::kCallOnSlowPath
4424 : LocationSummary::kNoCall;
4425 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4426 locations->SetInAt(0, Location::RequiresRegister());
4427 if (instruction->HasUses()) {
4428 locations->SetOut(Location::SameAsFirstInput());
4429 }
4430}
4431
4432void InstructionCodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4433 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4434 return;
4435 }
4436 Location obj = instruction->GetLocations()->InAt(0);
4437
4438 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
4439 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4440}
4441
4442void InstructionCodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
4443 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
4444 codegen_->AddSlowPath(slow_path);
4445
4446 Location obj = instruction->GetLocations()->InAt(0);
4447
4448 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4449}
4450
4451void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
4452 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
4453 GenerateImplicitNullCheck(instruction);
4454 } else {
4455 GenerateExplicitNullCheck(instruction);
4456 }
4457}
4458
4459void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4460 HandleBinaryOp(instruction);
4461}
4462
4463void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4464 HandleBinaryOp(instruction);
4465}
4466
4467void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4468 LOG(FATAL) << "Unreachable";
4469}
4470
4471void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4472 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4473}
4474
4475void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4476 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4477 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4478 if (location.IsStackSlot()) {
4479 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4480 } else if (location.IsDoubleStackSlot()) {
4481 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4482 }
4483 locations->SetOut(location);
4484}
4485
4486void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4487 ATTRIBUTE_UNUSED) {
4488 // Nothing to do, the parameter is already at its location.
4489}
4490
4491void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4492 LocationSummary* locations =
4493 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4494 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4495}
4496
4497void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4498 ATTRIBUTE_UNUSED) {
4499 // Nothing to do, the method is already at its location.
4500}
4501
4502void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4503 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4504 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4505 locations->SetInAt(i, Location::Any());
4506 }
4507 locations->SetOut(Location::Any());
4508}
4509
4510void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4511 LOG(FATAL) << "Unreachable";
4512}
4513
4514void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4515 Primitive::Type type = rem->GetResultType();
4516 LocationSummary::CallKind call_kind =
4517 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCall;
4518 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4519
4520 switch (type) {
4521 case Primitive::kPrimInt:
4522 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004523 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004524 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4525 break;
4526
4527 case Primitive::kPrimLong: {
4528 InvokeRuntimeCallingConvention calling_convention;
4529 locations->SetInAt(0, Location::RegisterPairLocation(
4530 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4531 locations->SetInAt(1, Location::RegisterPairLocation(
4532 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4533 locations->SetOut(calling_convention.GetReturnLocation(type));
4534 break;
4535 }
4536
4537 case Primitive::kPrimFloat:
4538 case Primitive::kPrimDouble: {
4539 InvokeRuntimeCallingConvention calling_convention;
4540 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4541 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4542 locations->SetOut(calling_convention.GetReturnLocation(type));
4543 break;
4544 }
4545
4546 default:
4547 LOG(FATAL) << "Unexpected rem type " << type;
4548 }
4549}
4550
4551void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4552 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004553
4554 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004555 case Primitive::kPrimInt:
4556 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004557 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004558 case Primitive::kPrimLong: {
4559 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
4560 instruction,
4561 instruction->GetDexPc(),
4562 nullptr,
4563 IsDirectEntrypoint(kQuickLmod));
4564 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4565 break;
4566 }
4567 case Primitive::kPrimFloat: {
4568 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
4569 instruction, instruction->GetDexPc(),
4570 nullptr,
4571 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00004572 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004573 break;
4574 }
4575 case Primitive::kPrimDouble: {
4576 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
4577 instruction, instruction->GetDexPc(),
4578 nullptr,
4579 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00004580 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004581 break;
4582 }
4583 default:
4584 LOG(FATAL) << "Unexpected rem type " << type;
4585 }
4586}
4587
4588void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4589 memory_barrier->SetLocations(nullptr);
4590}
4591
4592void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4593 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4594}
4595
4596void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
4597 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4598 Primitive::Type return_type = ret->InputAt(0)->GetType();
4599 locations->SetInAt(0, MipsReturnLocation(return_type));
4600}
4601
4602void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4603 codegen_->GenerateFrameExit();
4604}
4605
4606void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
4607 ret->SetLocations(nullptr);
4608}
4609
4610void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4611 codegen_->GenerateFrameExit();
4612}
4613
Alexey Frunze92d90602015-12-18 18:16:36 -08004614void LocationsBuilderMIPS::VisitRor(HRor* ror) {
4615 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004616}
4617
Alexey Frunze92d90602015-12-18 18:16:36 -08004618void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
4619 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004620}
4621
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004622void LocationsBuilderMIPS::VisitShl(HShl* shl) {
4623 HandleShift(shl);
4624}
4625
4626void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
4627 HandleShift(shl);
4628}
4629
4630void LocationsBuilderMIPS::VisitShr(HShr* shr) {
4631 HandleShift(shr);
4632}
4633
4634void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
4635 HandleShift(shr);
4636}
4637
4638void LocationsBuilderMIPS::VisitStoreLocal(HStoreLocal* store) {
4639 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4640 Primitive::Type field_type = store->InputAt(1)->GetType();
4641 switch (field_type) {
4642 case Primitive::kPrimNot:
4643 case Primitive::kPrimBoolean:
4644 case Primitive::kPrimByte:
4645 case Primitive::kPrimChar:
4646 case Primitive::kPrimShort:
4647 case Primitive::kPrimInt:
4648 case Primitive::kPrimFloat:
4649 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4650 break;
4651
4652 case Primitive::kPrimLong:
4653 case Primitive::kPrimDouble:
4654 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4655 break;
4656
4657 default:
4658 LOG(FATAL) << "Unimplemented local type " << field_type;
4659 }
4660}
4661
4662void InstructionCodeGeneratorMIPS::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
4663}
4664
4665void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
4666 HandleBinaryOp(instruction);
4667}
4668
4669void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
4670 HandleBinaryOp(instruction);
4671}
4672
4673void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4674 HandleFieldGet(instruction, instruction->GetFieldInfo());
4675}
4676
4677void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4678 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4679}
4680
4681void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4682 HandleFieldSet(instruction, instruction->GetFieldInfo());
4683}
4684
4685void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4686 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4687}
4688
4689void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
4690 HUnresolvedInstanceFieldGet* instruction) {
4691 FieldAccessCallingConventionMIPS calling_convention;
4692 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4693 instruction->GetFieldType(),
4694 calling_convention);
4695}
4696
4697void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
4698 HUnresolvedInstanceFieldGet* instruction) {
4699 FieldAccessCallingConventionMIPS calling_convention;
4700 codegen_->GenerateUnresolvedFieldAccess(instruction,
4701 instruction->GetFieldType(),
4702 instruction->GetFieldIndex(),
4703 instruction->GetDexPc(),
4704 calling_convention);
4705}
4706
4707void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4708 HUnresolvedInstanceFieldSet* instruction) {
4709 FieldAccessCallingConventionMIPS calling_convention;
4710 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4711 instruction->GetFieldType(),
4712 calling_convention);
4713}
4714
4715void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4716 HUnresolvedInstanceFieldSet* instruction) {
4717 FieldAccessCallingConventionMIPS calling_convention;
4718 codegen_->GenerateUnresolvedFieldAccess(instruction,
4719 instruction->GetFieldType(),
4720 instruction->GetFieldIndex(),
4721 instruction->GetDexPc(),
4722 calling_convention);
4723}
4724
4725void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4726 HUnresolvedStaticFieldGet* instruction) {
4727 FieldAccessCallingConventionMIPS calling_convention;
4728 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4729 instruction->GetFieldType(),
4730 calling_convention);
4731}
4732
4733void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4734 HUnresolvedStaticFieldGet* instruction) {
4735 FieldAccessCallingConventionMIPS calling_convention;
4736 codegen_->GenerateUnresolvedFieldAccess(instruction,
4737 instruction->GetFieldType(),
4738 instruction->GetFieldIndex(),
4739 instruction->GetDexPc(),
4740 calling_convention);
4741}
4742
4743void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4744 HUnresolvedStaticFieldSet* instruction) {
4745 FieldAccessCallingConventionMIPS calling_convention;
4746 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4747 instruction->GetFieldType(),
4748 calling_convention);
4749}
4750
4751void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4752 HUnresolvedStaticFieldSet* instruction) {
4753 FieldAccessCallingConventionMIPS calling_convention;
4754 codegen_->GenerateUnresolvedFieldAccess(instruction,
4755 instruction->GetFieldType(),
4756 instruction->GetFieldIndex(),
4757 instruction->GetDexPc(),
4758 calling_convention);
4759}
4760
4761void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4762 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4763}
4764
4765void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4766 HBasicBlock* block = instruction->GetBlock();
4767 if (block->GetLoopInformation() != nullptr) {
4768 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4769 // The back edge will generate the suspend check.
4770 return;
4771 }
4772 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4773 // The goto will generate the suspend check.
4774 return;
4775 }
4776 GenerateSuspendCheck(instruction, nullptr);
4777}
4778
4779void LocationsBuilderMIPS::VisitTemporary(HTemporary* temp) {
4780 temp->SetLocations(nullptr);
4781}
4782
4783void InstructionCodeGeneratorMIPS::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
4784 // Nothing to do, this is driven by the code generator.
4785}
4786
4787void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4788 LocationSummary* locations =
4789 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4790 InvokeRuntimeCallingConvention calling_convention;
4791 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4792}
4793
4794void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4795 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4796 instruction,
4797 instruction->GetDexPc(),
4798 nullptr,
4799 IsDirectEntrypoint(kQuickDeliverException));
4800 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4801}
4802
4803void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4804 Primitive::Type input_type = conversion->GetInputType();
4805 Primitive::Type result_type = conversion->GetResultType();
4806 DCHECK_NE(input_type, result_type);
4807
4808 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4809 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4810 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4811 }
4812
4813 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4814 if ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4815 (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type))) {
4816 call_kind = LocationSummary::kCall;
4817 }
4818
4819 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4820
4821 if (call_kind == LocationSummary::kNoCall) {
4822 if (Primitive::IsFloatingPointType(input_type)) {
4823 locations->SetInAt(0, Location::RequiresFpuRegister());
4824 } else {
4825 locations->SetInAt(0, Location::RequiresRegister());
4826 }
4827
4828 if (Primitive::IsFloatingPointType(result_type)) {
4829 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4830 } else {
4831 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4832 }
4833 } else {
4834 InvokeRuntimeCallingConvention calling_convention;
4835
4836 if (Primitive::IsFloatingPointType(input_type)) {
4837 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4838 } else {
4839 DCHECK_EQ(input_type, Primitive::kPrimLong);
4840 locations->SetInAt(0, Location::RegisterPairLocation(
4841 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4842 }
4843
4844 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4845 }
4846}
4847
4848void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4849 LocationSummary* locations = conversion->GetLocations();
4850 Primitive::Type result_type = conversion->GetResultType();
4851 Primitive::Type input_type = conversion->GetInputType();
4852 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
4853
4854 DCHECK_NE(input_type, result_type);
4855
4856 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4857 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4858 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4859 Register src = locations->InAt(0).AsRegister<Register>();
4860
4861 __ Move(dst_low, src);
4862 __ Sra(dst_high, src, 31);
4863 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4864 Register dst = locations->Out().AsRegister<Register>();
4865 Register src = (input_type == Primitive::kPrimLong)
4866 ? locations->InAt(0).AsRegisterPairLow<Register>()
4867 : locations->InAt(0).AsRegister<Register>();
4868
4869 switch (result_type) {
4870 case Primitive::kPrimChar:
4871 __ Andi(dst, src, 0xFFFF);
4872 break;
4873 case Primitive::kPrimByte:
4874 if (has_sign_extension) {
4875 __ Seb(dst, src);
4876 } else {
4877 __ Sll(dst, src, 24);
4878 __ Sra(dst, dst, 24);
4879 }
4880 break;
4881 case Primitive::kPrimShort:
4882 if (has_sign_extension) {
4883 __ Seh(dst, src);
4884 } else {
4885 __ Sll(dst, src, 16);
4886 __ Sra(dst, dst, 16);
4887 }
4888 break;
4889 case Primitive::kPrimInt:
4890 __ Move(dst, src);
4891 break;
4892
4893 default:
4894 LOG(FATAL) << "Unexpected type conversion from " << input_type
4895 << " to " << result_type;
4896 }
4897 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
4898 if (input_type != Primitive::kPrimLong) {
4899 Register src = locations->InAt(0).AsRegister<Register>();
4900 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4901 __ Mtc1(src, FTMP);
4902 if (result_type == Primitive::kPrimFloat) {
4903 __ Cvtsw(dst, FTMP);
4904 } else {
4905 __ Cvtdw(dst, FTMP);
4906 }
4907 } else {
4908 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4909 : QUICK_ENTRY_POINT(pL2d);
4910 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4911 : IsDirectEntrypoint(kQuickL2d);
4912 codegen_->InvokeRuntime(entry_offset,
4913 conversion,
4914 conversion->GetDexPc(),
4915 nullptr,
4916 direct);
4917 if (result_type == Primitive::kPrimFloat) {
4918 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4919 } else {
4920 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4921 }
4922 }
4923 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4924 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4925 int32_t entry_offset;
4926 bool direct;
4927 if (result_type != Primitive::kPrimLong) {
4928 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2iz)
4929 : QUICK_ENTRY_POINT(pD2iz);
4930 direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2iz)
4931 : IsDirectEntrypoint(kQuickD2iz);
4932 } else {
4933 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
4934 : QUICK_ENTRY_POINT(pD2l);
4935 direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
4936 : IsDirectEntrypoint(kQuickD2l);
4937 }
4938 codegen_->InvokeRuntime(entry_offset,
4939 conversion,
4940 conversion->GetDexPc(),
4941 nullptr,
4942 direct);
4943 if (result_type != Primitive::kPrimLong) {
4944 if (input_type == Primitive::kPrimFloat) {
4945 CheckEntrypointTypes<kQuickF2iz, int32_t, float>();
4946 } else {
4947 CheckEntrypointTypes<kQuickD2iz, int32_t, double>();
4948 }
4949 } else {
4950 if (input_type == Primitive::kPrimFloat) {
4951 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4952 } else {
4953 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4954 }
4955 }
4956 } else if (Primitive::IsFloatingPointType(result_type) &&
4957 Primitive::IsFloatingPointType(input_type)) {
4958 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4959 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4960 if (result_type == Primitive::kPrimFloat) {
4961 __ Cvtsd(dst, src);
4962 } else {
4963 __ Cvtds(dst, src);
4964 }
4965 } else {
4966 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4967 << " to " << result_type;
4968 }
4969}
4970
4971void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
4972 HandleShift(ushr);
4973}
4974
4975void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
4976 HandleShift(ushr);
4977}
4978
4979void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
4980 HandleBinaryOp(instruction);
4981}
4982
4983void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
4984 HandleBinaryOp(instruction);
4985}
4986
4987void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4988 // Nothing to do, this should be removed during prepare for register allocator.
4989 LOG(FATAL) << "Unreachable";
4990}
4991
4992void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4993 // Nothing to do, this should be removed during prepare for register allocator.
4994 LOG(FATAL) << "Unreachable";
4995}
4996
4997void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004998 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004999}
5000
5001void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005002 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005003}
5004
5005void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005006 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005007}
5008
5009void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005010 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005011}
5012
5013void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005014 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005015}
5016
5017void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005018 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005019}
5020
5021void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005022 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005023}
5024
5025void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005026 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005027}
5028
5029void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005030 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005031}
5032
5033void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005034 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005035}
5036
5037void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005038 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005039}
5040
5041void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005042 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005043}
5044
5045void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005046 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005047}
5048
5049void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005050 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005051}
5052
5053void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005054 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005055}
5056
5057void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005058 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005059}
5060
5061void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005062 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005063}
5064
5065void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005066 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005067}
5068
5069void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005070 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005071}
5072
5073void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005074 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005075}
5076
5077void LocationsBuilderMIPS::VisitFakeString(HFakeString* instruction) {
5078 DCHECK(codegen_->IsBaseline());
5079 LocationSummary* locations =
5080 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5081 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5082}
5083
5084void InstructionCodeGeneratorMIPS::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5085 DCHECK(codegen_->IsBaseline());
5086 // Will be generated at use site.
5087}
5088
5089void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5090 LocationSummary* locations =
5091 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5092 locations->SetInAt(0, Location::RequiresRegister());
5093}
5094
5095void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5096 int32_t lower_bound = switch_instr->GetStartValue();
5097 int32_t num_entries = switch_instr->GetNumEntries();
5098 LocationSummary* locations = switch_instr->GetLocations();
5099 Register value_reg = locations->InAt(0).AsRegister<Register>();
5100 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5101
5102 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005103 Register temp_reg = TMP;
5104 __ Addiu32(temp_reg, value_reg, -lower_bound);
5105 // Jump to default if index is negative
5106 // Note: We don't check the case that index is positive while value < lower_bound, because in
5107 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5108 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5109
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005110 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005111 // Jump to successors[0] if value == lower_bound.
5112 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5113 int32_t last_index = 0;
5114 for (; num_entries - last_index > 2; last_index += 2) {
5115 __ Addiu(temp_reg, temp_reg, -2);
5116 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5117 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5118 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5119 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5120 }
5121 if (num_entries - last_index == 2) {
5122 // The last missing case_value.
5123 __ Addiu(temp_reg, temp_reg, -1);
5124 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005125 }
5126
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005127 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005128 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5129 __ B(codegen_->GetLabelOf(default_block));
5130 }
5131}
5132
5133void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5134 // The trampoline uses the same calling convention as dex calling conventions,
5135 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5136 // the method_idx.
5137 HandleInvoke(invoke);
5138}
5139
5140void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5141 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5142}
5143
5144#undef __
5145#undef QUICK_ENTRY_POINT
5146
5147} // namespace mips
5148} // namespace art