blob: 12d1164d03fb3209448f423b217e85d70f9bd39f [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
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020042Location MipsReturnLocation(Primitive::Type return_type) {
43 switch (return_type) {
44 case Primitive::kPrimBoolean:
45 case Primitive::kPrimByte:
46 case Primitive::kPrimChar:
47 case Primitive::kPrimShort:
48 case Primitive::kPrimInt:
49 case Primitive::kPrimNot:
50 return Location::RegisterLocation(V0);
51
52 case Primitive::kPrimLong:
53 return Location::RegisterPairLocation(V0, V1);
54
55 case Primitive::kPrimFloat:
56 case Primitive::kPrimDouble:
57 return Location::FpuRegisterLocation(F0);
58
59 case Primitive::kPrimVoid:
60 return Location();
61 }
62 UNREACHABLE();
63}
64
65Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
66 return MipsReturnLocation(type);
67}
68
69Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
70 return Location::RegisterLocation(kMethodRegisterArgument);
71}
72
73Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
74 Location next_location;
75
76 switch (type) {
77 case Primitive::kPrimBoolean:
78 case Primitive::kPrimByte:
79 case Primitive::kPrimChar:
80 case Primitive::kPrimShort:
81 case Primitive::kPrimInt:
82 case Primitive::kPrimNot: {
83 uint32_t gp_index = gp_index_++;
84 if (gp_index < calling_convention.GetNumberOfRegisters()) {
85 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
86 } else {
87 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
88 next_location = Location::StackSlot(stack_offset);
89 }
90 break;
91 }
92
93 case Primitive::kPrimLong: {
94 uint32_t gp_index = gp_index_;
95 gp_index_ += 2;
96 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
97 if (calling_convention.GetRegisterAt(gp_index) == A1) {
98 gp_index_++; // Skip A1, and use A2_A3 instead.
99 gp_index++;
100 }
101 Register low_even = calling_convention.GetRegisterAt(gp_index);
102 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
103 DCHECK_EQ(low_even + 1, high_odd);
104 next_location = Location::RegisterPairLocation(low_even, high_odd);
105 } else {
106 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
107 next_location = Location::DoubleStackSlot(stack_offset);
108 }
109 break;
110 }
111
112 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
113 // will take up the even/odd pair, while floats are stored in even regs only.
114 // On 64 bit FPU, both double and float are stored in even registers only.
115 case Primitive::kPrimFloat:
116 case Primitive::kPrimDouble: {
117 uint32_t float_index = float_index_++;
118 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
119 next_location = Location::FpuRegisterLocation(
120 calling_convention.GetFpuRegisterAt(float_index));
121 } else {
122 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
123 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
124 : Location::StackSlot(stack_offset);
125 }
126 break;
127 }
128
129 case Primitive::kPrimVoid:
130 LOG(FATAL) << "Unexpected parameter type " << type;
131 break;
132 }
133
134 // Space on the stack is reserved for all arguments.
135 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
136
137 return next_location;
138}
139
140Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
141 return MipsReturnLocation(type);
142}
143
144#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()->
145#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
146
147class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
148 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000149 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200150
151 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
152 LocationSummary* locations = instruction_->GetLocations();
153 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
154 __ Bind(GetEntryLabel());
155 if (instruction_->CanThrowIntoCatchBlock()) {
156 // Live registers will be restored in the catch block if caught.
157 SaveLiveRegisters(codegen, instruction_->GetLocations());
158 }
159 // We're moving two locations to locations that could overlap, so we need a parallel
160 // move resolver.
161 InvokeRuntimeCallingConvention calling_convention;
162 codegen->EmitParallelMoves(locations->InAt(0),
163 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
164 Primitive::kPrimInt,
165 locations->InAt(1),
166 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
167 Primitive::kPrimInt);
168 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
169 instruction_,
170 instruction_->GetDexPc(),
171 this,
172 IsDirectEntrypoint(kQuickThrowArrayBounds));
173 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
174 }
175
176 bool IsFatal() const OVERRIDE { return true; }
177
178 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
179
180 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200181 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
182};
183
184class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
185 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000186 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200187
188 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
189 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
190 __ Bind(GetEntryLabel());
191 if (instruction_->CanThrowIntoCatchBlock()) {
192 // Live registers will be restored in the catch block if caught.
193 SaveLiveRegisters(codegen, instruction_->GetLocations());
194 }
195 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
196 instruction_,
197 instruction_->GetDexPc(),
198 this,
199 IsDirectEntrypoint(kQuickThrowDivZero));
200 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
201 }
202
203 bool IsFatal() const OVERRIDE { return true; }
204
205 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
206
207 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200208 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
209};
210
211class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
212 public:
213 LoadClassSlowPathMIPS(HLoadClass* cls,
214 HInstruction* at,
215 uint32_t dex_pc,
216 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000217 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200218 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
219 }
220
221 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
222 LocationSummary* locations = at_->GetLocations();
223 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
224
225 __ Bind(GetEntryLabel());
226 SaveLiveRegisters(codegen, locations);
227
228 InvokeRuntimeCallingConvention calling_convention;
229 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
230
231 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
232 : QUICK_ENTRY_POINT(pInitializeType);
233 bool direct = do_clinit_ ? IsDirectEntrypoint(kQuickInitializeStaticStorage)
234 : IsDirectEntrypoint(kQuickInitializeType);
235
236 mips_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this, direct);
237 if (do_clinit_) {
238 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
239 } else {
240 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
241 }
242
243 // Move the class to the desired location.
244 Location out = locations->Out();
245 if (out.IsValid()) {
246 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
247 Primitive::Type type = at_->GetType();
248 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
249 }
250
251 RestoreLiveRegisters(codegen, locations);
252 __ B(GetExitLabel());
253 }
254
255 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
256
257 private:
258 // The class this slow path will load.
259 HLoadClass* const cls_;
260
261 // The instruction where this slow path is happening.
262 // (Might be the load class or an initialization check).
263 HInstruction* const at_;
264
265 // The dex PC of `at_`.
266 const uint32_t dex_pc_;
267
268 // Whether to initialize the class.
269 const bool do_clinit_;
270
271 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
272};
273
274class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
275 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000276 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200277
278 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
279 LocationSummary* locations = instruction_->GetLocations();
280 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
281 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
282
283 __ Bind(GetEntryLabel());
284 SaveLiveRegisters(codegen, locations);
285
286 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000287 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
288 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200289 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
290 instruction_,
291 instruction_->GetDexPc(),
292 this,
293 IsDirectEntrypoint(kQuickResolveString));
294 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
295 Primitive::Type type = instruction_->GetType();
296 mips_codegen->MoveLocation(locations->Out(),
297 calling_convention.GetReturnLocation(type),
298 type);
299
300 RestoreLiveRegisters(codegen, locations);
301 __ B(GetExitLabel());
302 }
303
304 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
305
306 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
308};
309
310class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
311 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000312 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200313
314 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
315 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
316 __ Bind(GetEntryLabel());
317 if (instruction_->CanThrowIntoCatchBlock()) {
318 // Live registers will be restored in the catch block if caught.
319 SaveLiveRegisters(codegen, instruction_->GetLocations());
320 }
321 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
322 instruction_,
323 instruction_->GetDexPc(),
324 this,
325 IsDirectEntrypoint(kQuickThrowNullPointer));
326 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
327 }
328
329 bool IsFatal() const OVERRIDE { return true; }
330
331 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
332
333 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200334 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
335};
336
337class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
338 public:
339 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000340 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200341
342 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
343 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
344 __ Bind(GetEntryLabel());
345 SaveLiveRegisters(codegen, instruction_->GetLocations());
346 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
347 instruction_,
348 instruction_->GetDexPc(),
349 this,
350 IsDirectEntrypoint(kQuickTestSuspend));
351 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
352 RestoreLiveRegisters(codegen, instruction_->GetLocations());
353 if (successor_ == nullptr) {
354 __ B(GetReturnLabel());
355 } else {
356 __ B(mips_codegen->GetLabelOf(successor_));
357 }
358 }
359
360 MipsLabel* GetReturnLabel() {
361 DCHECK(successor_ == nullptr);
362 return &return_label_;
363 }
364
365 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
366
367 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200368 // If not null, the block to branch to after the suspend check.
369 HBasicBlock* const successor_;
370
371 // If `successor_` is null, the label to branch to after the suspend check.
372 MipsLabel return_label_;
373
374 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
375};
376
377class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
378 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000379 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200380
381 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
382 LocationSummary* locations = instruction_->GetLocations();
383 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
384 uint32_t dex_pc = instruction_->GetDexPc();
385 DCHECK(instruction_->IsCheckCast()
386 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
387 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
388
389 __ Bind(GetEntryLabel());
390 SaveLiveRegisters(codegen, locations);
391
392 // We're moving two locations to locations that could overlap, so we need a parallel
393 // move resolver.
394 InvokeRuntimeCallingConvention calling_convention;
395 codegen->EmitParallelMoves(locations->InAt(1),
396 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
397 Primitive::kPrimNot,
398 object_class,
399 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
400 Primitive::kPrimNot);
401
402 if (instruction_->IsInstanceOf()) {
403 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
404 instruction_,
405 dex_pc,
406 this,
407 IsDirectEntrypoint(kQuickInstanceofNonTrivial));
Roland Levillain888d0672015-11-23 18:53:50 +0000408 CheckEntrypointTypes<
409 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 Primitive::Type ret_type = instruction_->GetType();
411 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
412 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200413 } else {
414 DCHECK(instruction_->IsCheckCast());
415 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
416 instruction_,
417 dex_pc,
418 this,
419 IsDirectEntrypoint(kQuickCheckCast));
420 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
421 }
422
423 RestoreLiveRegisters(codegen, locations);
424 __ B(GetExitLabel());
425 }
426
427 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
428
429 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200430 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
431};
432
433class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
434 public:
Aart Bik42249c32016-01-07 15:33:50 -0800435 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000436 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200437
438 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800439 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200440 __ Bind(GetEntryLabel());
441 SaveLiveRegisters(codegen, instruction_->GetLocations());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200442 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
443 instruction_,
Aart Bik42249c32016-01-07 15:33:50 -0800444 instruction_->GetDexPc(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200445 this,
446 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000447 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200448 }
449
450 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
451
452 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200453 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
454};
455
456CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
457 const MipsInstructionSetFeatures& isa_features,
458 const CompilerOptions& compiler_options,
459 OptimizingCompilerStats* stats)
460 : CodeGenerator(graph,
461 kNumberOfCoreRegisters,
462 kNumberOfFRegisters,
463 kNumberOfRegisterPairs,
464 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
465 arraysize(kCoreCalleeSaves)),
466 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
467 arraysize(kFpuCalleeSaves)),
468 compiler_options,
469 stats),
470 block_labels_(nullptr),
471 location_builder_(graph, this),
472 instruction_visitor_(graph, this),
473 move_resolver_(graph->GetArena(), this),
Vladimir Markod1ee8092016-04-13 11:59:46 +0100474 assembler_(graph->GetArena(), &isa_features),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200475 isa_features_(isa_features) {
476 // Save RA (containing the return address) to mimic Quick.
477 AddAllocatedRegister(Location::RegisterLocation(RA));
478}
479
480#undef __
481#define __ down_cast<MipsAssembler*>(GetAssembler())->
482#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
483
484void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
485 // Ensure that we fix up branches.
486 __ FinalizeCode();
487
488 // Adjust native pc offsets in stack maps.
489 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
490 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
491 uint32_t new_position = __ GetAdjustedPosition(old_position);
492 DCHECK_GE(new_position, old_position);
493 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
494 }
495
496 // Adjust pc offsets for the disassembly information.
497 if (disasm_info_ != nullptr) {
498 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
499 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
500 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
501 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
502 it.second.start = __ GetAdjustedPosition(it.second.start);
503 it.second.end = __ GetAdjustedPosition(it.second.end);
504 }
505 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
506 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
507 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
508 }
509 }
510
511 CodeGenerator::Finalize(allocator);
512}
513
514MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
515 return codegen_->GetAssembler();
516}
517
518void ParallelMoveResolverMIPS::EmitMove(size_t index) {
519 DCHECK_LT(index, moves_.size());
520 MoveOperands* move = moves_[index];
521 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
522}
523
524void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
525 DCHECK_LT(index, moves_.size());
526 MoveOperands* move = moves_[index];
527 Primitive::Type type = move->GetType();
528 Location loc1 = move->GetDestination();
529 Location loc2 = move->GetSource();
530
531 DCHECK(!loc1.IsConstant());
532 DCHECK(!loc2.IsConstant());
533
534 if (loc1.Equals(loc2)) {
535 return;
536 }
537
538 if (loc1.IsRegister() && loc2.IsRegister()) {
539 // Swap 2 GPRs.
540 Register r1 = loc1.AsRegister<Register>();
541 Register r2 = loc2.AsRegister<Register>();
542 __ Move(TMP, r2);
543 __ Move(r2, r1);
544 __ Move(r1, TMP);
545 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
546 FRegister f1 = loc1.AsFpuRegister<FRegister>();
547 FRegister f2 = loc2.AsFpuRegister<FRegister>();
548 if (type == Primitive::kPrimFloat) {
549 __ MovS(FTMP, f2);
550 __ MovS(f2, f1);
551 __ MovS(f1, FTMP);
552 } else {
553 DCHECK_EQ(type, Primitive::kPrimDouble);
554 __ MovD(FTMP, f2);
555 __ MovD(f2, f1);
556 __ MovD(f1, FTMP);
557 }
558 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
559 (loc1.IsFpuRegister() && loc2.IsRegister())) {
560 // Swap FPR and GPR.
561 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
562 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
563 : loc2.AsFpuRegister<FRegister>();
564 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
565 : loc2.AsRegister<Register>();
566 __ Move(TMP, r2);
567 __ Mfc1(r2, f1);
568 __ Mtc1(TMP, f1);
569 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
570 // Swap 2 GPR register pairs.
571 Register r1 = loc1.AsRegisterPairLow<Register>();
572 Register r2 = loc2.AsRegisterPairLow<Register>();
573 __ Move(TMP, r2);
574 __ Move(r2, r1);
575 __ Move(r1, TMP);
576 r1 = loc1.AsRegisterPairHigh<Register>();
577 r2 = loc2.AsRegisterPairHigh<Register>();
578 __ Move(TMP, r2);
579 __ Move(r2, r1);
580 __ Move(r1, TMP);
581 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
582 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
583 // Swap FPR and GPR register pair.
584 DCHECK_EQ(type, Primitive::kPrimDouble);
585 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
586 : loc2.AsFpuRegister<FRegister>();
587 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
588 : loc2.AsRegisterPairLow<Register>();
589 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
590 : loc2.AsRegisterPairHigh<Register>();
591 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
592 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
593 // unpredictable and the following mfch1 will fail.
594 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800595 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200596 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800597 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200598 __ Move(r2_l, TMP);
599 __ Move(r2_h, AT);
600 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
601 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
602 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
603 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000604 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
605 (loc1.IsStackSlot() && loc2.IsRegister())) {
606 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
607 : loc2.AsRegister<Register>();
608 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
609 : loc2.GetStackIndex();
610 __ Move(TMP, reg);
611 __ LoadFromOffset(kLoadWord, reg, SP, offset);
612 __ StoreToOffset(kStoreWord, TMP, SP, offset);
613 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
614 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
615 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
616 : loc2.AsRegisterPairLow<Register>();
617 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
618 : loc2.AsRegisterPairHigh<Register>();
619 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
620 : loc2.GetStackIndex();
621 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
622 : loc2.GetHighStackIndex(kMipsWordSize);
623 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000624 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000625 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000626 __ Move(TMP, reg_h);
627 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
628 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200629 } else {
630 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
631 }
632}
633
634void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
635 __ Pop(static_cast<Register>(reg));
636}
637
638void ParallelMoveResolverMIPS::SpillScratch(int reg) {
639 __ Push(static_cast<Register>(reg));
640}
641
642void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
643 // Allocate a scratch register other than TMP, if available.
644 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
645 // automatically unspilled when the scratch scope object is destroyed).
646 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
647 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
648 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
649 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
650 __ LoadFromOffset(kLoadWord,
651 Register(ensure_scratch.GetRegister()),
652 SP,
653 index1 + stack_offset);
654 __ LoadFromOffset(kLoadWord,
655 TMP,
656 SP,
657 index2 + stack_offset);
658 __ StoreToOffset(kStoreWord,
659 Register(ensure_scratch.GetRegister()),
660 SP,
661 index2 + stack_offset);
662 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
663 }
664}
665
666static dwarf::Reg DWARFReg(Register reg) {
667 return dwarf::Reg::MipsCore(static_cast<int>(reg));
668}
669
670// TODO: mapping of floating-point registers to DWARF.
671
672void CodeGeneratorMIPS::GenerateFrameEntry() {
673 __ Bind(&frame_entry_label_);
674
675 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
676
677 if (do_overflow_check) {
678 __ LoadFromOffset(kLoadWord,
679 ZERO,
680 SP,
681 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
682 RecordPcInfo(nullptr, 0);
683 }
684
685 if (HasEmptyFrame()) {
686 return;
687 }
688
689 // Make sure the frame size isn't unreasonably large.
690 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
691 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
692 }
693
694 // Spill callee-saved registers.
695 // Note that their cumulative size is small and they can be indexed using
696 // 16-bit offsets.
697
698 // TODO: increment/decrement SP in one step instead of two or remove this comment.
699
700 uint32_t ofs = FrameEntrySpillSize();
701 bool unaligned_float = ofs & 0x7;
702 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
703 __ IncreaseFrameSize(ofs);
704
705 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
706 Register reg = kCoreCalleeSaves[i];
707 if (allocated_registers_.ContainsCoreRegister(reg)) {
708 ofs -= kMipsWordSize;
709 __ Sw(reg, SP, ofs);
710 __ cfi().RelOffset(DWARFReg(reg), ofs);
711 }
712 }
713
714 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
715 FRegister reg = kFpuCalleeSaves[i];
716 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
717 ofs -= kMipsDoublewordSize;
718 // TODO: Change the frame to avoid unaligned accesses for fpu registers.
719 if (unaligned_float) {
720 if (fpu_32bit) {
721 __ Swc1(reg, SP, ofs);
722 __ Swc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
723 } else {
724 __ Mfhc1(TMP, reg);
725 __ Swc1(reg, SP, ofs);
726 __ Sw(TMP, SP, ofs + 4);
727 }
728 } else {
729 __ Sdc1(reg, SP, ofs);
730 }
731 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
732 }
733 }
734
735 // Allocate the rest of the frame and store the current method pointer
736 // at its end.
737
738 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
739
740 static_assert(IsInt<16>(kCurrentMethodStackOffset),
741 "kCurrentMethodStackOffset must fit into int16_t");
742 __ Sw(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
743}
744
745void CodeGeneratorMIPS::GenerateFrameExit() {
746 __ cfi().RememberState();
747
748 if (!HasEmptyFrame()) {
749 // Deallocate the rest of the frame.
750
751 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
752
753 // Restore callee-saved registers.
754 // Note that their cumulative size is small and they can be indexed using
755 // 16-bit offsets.
756
757 // TODO: increment/decrement SP in one step instead of two or remove this comment.
758
759 uint32_t ofs = 0;
760 bool unaligned_float = FrameEntrySpillSize() & 0x7;
761 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
762
763 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
764 FRegister reg = kFpuCalleeSaves[i];
765 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
766 if (unaligned_float) {
767 if (fpu_32bit) {
768 __ Lwc1(reg, SP, ofs);
769 __ Lwc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
770 } else {
771 __ Lwc1(reg, SP, ofs);
772 __ Lw(TMP, SP, ofs + 4);
773 __ Mthc1(TMP, reg);
774 }
775 } else {
776 __ Ldc1(reg, SP, ofs);
777 }
778 ofs += kMipsDoublewordSize;
779 // TODO: __ cfi().Restore(DWARFReg(reg));
780 }
781 }
782
783 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
784 Register reg = kCoreCalleeSaves[i];
785 if (allocated_registers_.ContainsCoreRegister(reg)) {
786 __ Lw(reg, SP, ofs);
787 ofs += kMipsWordSize;
788 __ cfi().Restore(DWARFReg(reg));
789 }
790 }
791
792 DCHECK_EQ(ofs, FrameEntrySpillSize());
793 __ DecreaseFrameSize(ofs);
794 }
795
796 __ Jr(RA);
797 __ Nop();
798
799 __ cfi().RestoreState();
800 __ cfi().DefCFAOffset(GetFrameSize());
801}
802
803void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
804 __ Bind(GetLabelOf(block));
805}
806
807void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
808 if (src.Equals(dst)) {
809 return;
810 }
811
812 if (src.IsConstant()) {
813 MoveConstant(dst, src.GetConstant());
814 } else {
815 if (Primitive::Is64BitType(dst_type)) {
816 Move64(dst, src);
817 } else {
818 Move32(dst, src);
819 }
820 }
821}
822
823void CodeGeneratorMIPS::Move32(Location destination, Location source) {
824 if (source.Equals(destination)) {
825 return;
826 }
827
828 if (destination.IsRegister()) {
829 if (source.IsRegister()) {
830 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
831 } else if (source.IsFpuRegister()) {
832 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
833 } else {
834 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
835 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
836 }
837 } else if (destination.IsFpuRegister()) {
838 if (source.IsRegister()) {
839 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
840 } else if (source.IsFpuRegister()) {
841 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
842 } else {
843 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
844 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
845 }
846 } else {
847 DCHECK(destination.IsStackSlot()) << destination;
848 if (source.IsRegister()) {
849 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
850 } else if (source.IsFpuRegister()) {
851 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
852 } else {
853 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
854 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
855 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
856 }
857 }
858}
859
860void CodeGeneratorMIPS::Move64(Location destination, Location source) {
861 if (source.Equals(destination)) {
862 return;
863 }
864
865 if (destination.IsRegisterPair()) {
866 if (source.IsRegisterPair()) {
867 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
868 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
869 } else if (source.IsFpuRegister()) {
870 Register dst_high = destination.AsRegisterPairHigh<Register>();
871 Register dst_low = destination.AsRegisterPairLow<Register>();
872 FRegister src = source.AsFpuRegister<FRegister>();
873 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800874 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200875 } else {
876 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
877 int32_t off = source.GetStackIndex();
878 Register r = destination.AsRegisterPairLow<Register>();
879 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
880 }
881 } else if (destination.IsFpuRegister()) {
882 if (source.IsRegisterPair()) {
883 FRegister dst = destination.AsFpuRegister<FRegister>();
884 Register src_high = source.AsRegisterPairHigh<Register>();
885 Register src_low = source.AsRegisterPairLow<Register>();
886 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800887 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200888 } else if (source.IsFpuRegister()) {
889 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
890 } else {
891 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
892 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
893 }
894 } else {
895 DCHECK(destination.IsDoubleStackSlot()) << destination;
896 int32_t off = destination.GetStackIndex();
897 if (source.IsRegisterPair()) {
898 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
899 } else if (source.IsFpuRegister()) {
900 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
901 } else {
902 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
903 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
904 __ StoreToOffset(kStoreWord, TMP, SP, off);
905 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
906 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
907 }
908 }
909}
910
911void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
912 if (c->IsIntConstant() || c->IsNullConstant()) {
913 // Move 32 bit constant.
914 int32_t value = GetInt32ValueOf(c);
915 if (destination.IsRegister()) {
916 Register dst = destination.AsRegister<Register>();
917 __ LoadConst32(dst, value);
918 } else {
919 DCHECK(destination.IsStackSlot())
920 << "Cannot move " << c->DebugName() << " to " << destination;
921 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
922 }
923 } else if (c->IsLongConstant()) {
924 // Move 64 bit constant.
925 int64_t value = GetInt64ValueOf(c);
926 if (destination.IsRegisterPair()) {
927 Register r_h = destination.AsRegisterPairHigh<Register>();
928 Register r_l = destination.AsRegisterPairLow<Register>();
929 __ LoadConst64(r_h, r_l, value);
930 } else {
931 DCHECK(destination.IsDoubleStackSlot())
932 << "Cannot move " << c->DebugName() << " to " << destination;
933 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
934 }
935 } else if (c->IsFloatConstant()) {
936 // Move 32 bit float constant.
937 int32_t value = GetInt32ValueOf(c);
938 if (destination.IsFpuRegister()) {
939 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
940 } else {
941 DCHECK(destination.IsStackSlot())
942 << "Cannot move " << c->DebugName() << " to " << destination;
943 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
944 }
945 } else {
946 // Move 64 bit double constant.
947 DCHECK(c->IsDoubleConstant()) << c->DebugName();
948 int64_t value = GetInt64ValueOf(c);
949 if (destination.IsFpuRegister()) {
950 FRegister fd = destination.AsFpuRegister<FRegister>();
951 __ LoadDConst64(fd, value, TMP);
952 } else {
953 DCHECK(destination.IsDoubleStackSlot())
954 << "Cannot move " << c->DebugName() << " to " << destination;
955 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
956 }
957 }
958}
959
960void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
961 DCHECK(destination.IsRegister());
962 Register dst = destination.AsRegister<Register>();
963 __ LoadConst32(dst, value);
964}
965
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200966void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
967 if (location.IsRegister()) {
968 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700969 } else if (location.IsRegisterPair()) {
970 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
971 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200972 } else {
973 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
974 }
975}
976
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200977void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
978 MipsLabel done;
979 Register card = AT;
980 Register temp = TMP;
981 __ Beqz(value, &done);
982 __ LoadFromOffset(kLoadWord,
983 card,
984 TR,
985 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
986 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
987 __ Addu(temp, card, temp);
988 __ Sb(card, temp, 0);
989 __ Bind(&done);
990}
991
David Brazdil58282f42016-01-14 12:45:10 +0000992void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200993 // Don't allocate the dalvik style register pair passing.
994 blocked_register_pairs_[A1_A2] = true;
995
996 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
997 blocked_core_registers_[ZERO] = true;
998 blocked_core_registers_[K0] = true;
999 blocked_core_registers_[K1] = true;
1000 blocked_core_registers_[GP] = true;
1001 blocked_core_registers_[SP] = true;
1002 blocked_core_registers_[RA] = true;
1003
1004 // AT and TMP(T8) are used as temporary/scratch registers
1005 // (similar to how AT is used by MIPS assemblers).
1006 blocked_core_registers_[AT] = true;
1007 blocked_core_registers_[TMP] = true;
1008 blocked_fpu_registers_[FTMP] = true;
1009
1010 // Reserve suspend and thread registers.
1011 blocked_core_registers_[S0] = true;
1012 blocked_core_registers_[TR] = true;
1013
1014 // Reserve T9 for function calls
1015 blocked_core_registers_[T9] = true;
1016
1017 // Reserve odd-numbered FPU registers.
1018 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1019 blocked_fpu_registers_[i] = true;
1020 }
1021
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001022 UpdateBlockedPairRegisters();
1023}
1024
1025void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1026 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1027 MipsManagedRegister current =
1028 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1029 if (blocked_core_registers_[current.AsRegisterPairLow()]
1030 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1031 blocked_register_pairs_[i] = true;
1032 }
1033 }
1034}
1035
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001036size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1037 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1038 return kMipsWordSize;
1039}
1040
1041size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1042 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1043 return kMipsWordSize;
1044}
1045
1046size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1047 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1048 return kMipsDoublewordSize;
1049}
1050
1051size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1052 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1053 return kMipsDoublewordSize;
1054}
1055
1056void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001057 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001058}
1059
1060void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001061 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001062}
1063
1064void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1065 HInstruction* instruction,
1066 uint32_t dex_pc,
1067 SlowPathCode* slow_path) {
1068 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1069 instruction,
1070 dex_pc,
1071 slow_path,
1072 IsDirectEntrypoint(entrypoint));
1073}
1074
1075constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1076
1077void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1078 HInstruction* instruction,
1079 uint32_t dex_pc,
1080 SlowPathCode* slow_path,
1081 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001082 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1083 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001084 if (is_direct_entrypoint) {
1085 // Reserve argument space on stack (for $a0-$a3) for
1086 // entrypoints that directly reference native implementations.
1087 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001088 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001089 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001090 } else {
1091 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001092 }
1093 RecordPcInfo(instruction, dex_pc, slow_path);
1094}
1095
1096void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1097 Register class_reg) {
1098 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1099 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1100 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1101 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1102 __ Sync(0);
1103 __ Bind(slow_path->GetExitLabel());
1104}
1105
1106void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1107 __ Sync(0); // Only stype 0 is supported.
1108}
1109
1110void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1111 HBasicBlock* successor) {
1112 SuspendCheckSlowPathMIPS* slow_path =
1113 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1114 codegen_->AddSlowPath(slow_path);
1115
1116 __ LoadFromOffset(kLoadUnsignedHalfword,
1117 TMP,
1118 TR,
1119 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1120 if (successor == nullptr) {
1121 __ Bnez(TMP, slow_path->GetEntryLabel());
1122 __ Bind(slow_path->GetReturnLabel());
1123 } else {
1124 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1125 __ B(slow_path->GetEntryLabel());
1126 // slow_path will return to GetLabelOf(successor).
1127 }
1128}
1129
1130InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1131 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001132 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001133 assembler_(codegen->GetAssembler()),
1134 codegen_(codegen) {}
1135
1136void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1137 DCHECK_EQ(instruction->InputCount(), 2U);
1138 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1139 Primitive::Type type = instruction->GetResultType();
1140 switch (type) {
1141 case Primitive::kPrimInt: {
1142 locations->SetInAt(0, Location::RequiresRegister());
1143 HInstruction* right = instruction->InputAt(1);
1144 bool can_use_imm = false;
1145 if (right->IsConstant()) {
1146 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1147 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1148 can_use_imm = IsUint<16>(imm);
1149 } else if (instruction->IsAdd()) {
1150 can_use_imm = IsInt<16>(imm);
1151 } else {
1152 DCHECK(instruction->IsSub());
1153 can_use_imm = IsInt<16>(-imm);
1154 }
1155 }
1156 if (can_use_imm)
1157 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1158 else
1159 locations->SetInAt(1, Location::RequiresRegister());
1160 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1161 break;
1162 }
1163
1164 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001165 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001166 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1167 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001168 break;
1169 }
1170
1171 case Primitive::kPrimFloat:
1172 case Primitive::kPrimDouble:
1173 DCHECK(instruction->IsAdd() || instruction->IsSub());
1174 locations->SetInAt(0, Location::RequiresFpuRegister());
1175 locations->SetInAt(1, Location::RequiresFpuRegister());
1176 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1177 break;
1178
1179 default:
1180 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1181 }
1182}
1183
1184void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1185 Primitive::Type type = instruction->GetType();
1186 LocationSummary* locations = instruction->GetLocations();
1187
1188 switch (type) {
1189 case Primitive::kPrimInt: {
1190 Register dst = locations->Out().AsRegister<Register>();
1191 Register lhs = locations->InAt(0).AsRegister<Register>();
1192 Location rhs_location = locations->InAt(1);
1193
1194 Register rhs_reg = ZERO;
1195 int32_t rhs_imm = 0;
1196 bool use_imm = rhs_location.IsConstant();
1197 if (use_imm) {
1198 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1199 } else {
1200 rhs_reg = rhs_location.AsRegister<Register>();
1201 }
1202
1203 if (instruction->IsAnd()) {
1204 if (use_imm)
1205 __ Andi(dst, lhs, rhs_imm);
1206 else
1207 __ And(dst, lhs, rhs_reg);
1208 } else if (instruction->IsOr()) {
1209 if (use_imm)
1210 __ Ori(dst, lhs, rhs_imm);
1211 else
1212 __ Or(dst, lhs, rhs_reg);
1213 } else if (instruction->IsXor()) {
1214 if (use_imm)
1215 __ Xori(dst, lhs, rhs_imm);
1216 else
1217 __ Xor(dst, lhs, rhs_reg);
1218 } else if (instruction->IsAdd()) {
1219 if (use_imm)
1220 __ Addiu(dst, lhs, rhs_imm);
1221 else
1222 __ Addu(dst, lhs, rhs_reg);
1223 } else {
1224 DCHECK(instruction->IsSub());
1225 if (use_imm)
1226 __ Addiu(dst, lhs, -rhs_imm);
1227 else
1228 __ Subu(dst, lhs, rhs_reg);
1229 }
1230 break;
1231 }
1232
1233 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001234 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1235 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1236 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1237 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001238 Location rhs_location = locations->InAt(1);
1239 bool use_imm = rhs_location.IsConstant();
1240 if (!use_imm) {
1241 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1242 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1243 if (instruction->IsAnd()) {
1244 __ And(dst_low, lhs_low, rhs_low);
1245 __ And(dst_high, lhs_high, rhs_high);
1246 } else if (instruction->IsOr()) {
1247 __ Or(dst_low, lhs_low, rhs_low);
1248 __ Or(dst_high, lhs_high, rhs_high);
1249 } else if (instruction->IsXor()) {
1250 __ Xor(dst_low, lhs_low, rhs_low);
1251 __ Xor(dst_high, lhs_high, rhs_high);
1252 } else if (instruction->IsAdd()) {
1253 if (lhs_low == rhs_low) {
1254 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1255 __ Slt(TMP, lhs_low, ZERO);
1256 __ Addu(dst_low, lhs_low, rhs_low);
1257 } else {
1258 __ Addu(dst_low, lhs_low, rhs_low);
1259 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1260 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1261 }
1262 __ Addu(dst_high, lhs_high, rhs_high);
1263 __ Addu(dst_high, dst_high, TMP);
1264 } else {
1265 DCHECK(instruction->IsSub());
1266 __ Sltu(TMP, lhs_low, rhs_low);
1267 __ Subu(dst_low, lhs_low, rhs_low);
1268 __ Subu(dst_high, lhs_high, rhs_high);
1269 __ Subu(dst_high, dst_high, TMP);
1270 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001271 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001272 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1273 if (instruction->IsOr()) {
1274 uint32_t low = Low32Bits(value);
1275 uint32_t high = High32Bits(value);
1276 if (IsUint<16>(low)) {
1277 if (dst_low != lhs_low || low != 0) {
1278 __ Ori(dst_low, lhs_low, low);
1279 }
1280 } else {
1281 __ LoadConst32(TMP, low);
1282 __ Or(dst_low, lhs_low, TMP);
1283 }
1284 if (IsUint<16>(high)) {
1285 if (dst_high != lhs_high || high != 0) {
1286 __ Ori(dst_high, lhs_high, high);
1287 }
1288 } else {
1289 if (high != low) {
1290 __ LoadConst32(TMP, high);
1291 }
1292 __ Or(dst_high, lhs_high, TMP);
1293 }
1294 } else if (instruction->IsXor()) {
1295 uint32_t low = Low32Bits(value);
1296 uint32_t high = High32Bits(value);
1297 if (IsUint<16>(low)) {
1298 if (dst_low != lhs_low || low != 0) {
1299 __ Xori(dst_low, lhs_low, low);
1300 }
1301 } else {
1302 __ LoadConst32(TMP, low);
1303 __ Xor(dst_low, lhs_low, TMP);
1304 }
1305 if (IsUint<16>(high)) {
1306 if (dst_high != lhs_high || high != 0) {
1307 __ Xori(dst_high, lhs_high, high);
1308 }
1309 } else {
1310 if (high != low) {
1311 __ LoadConst32(TMP, high);
1312 }
1313 __ Xor(dst_high, lhs_high, TMP);
1314 }
1315 } else if (instruction->IsAnd()) {
1316 uint32_t low = Low32Bits(value);
1317 uint32_t high = High32Bits(value);
1318 if (IsUint<16>(low)) {
1319 __ Andi(dst_low, lhs_low, low);
1320 } else if (low != 0xFFFFFFFF) {
1321 __ LoadConst32(TMP, low);
1322 __ And(dst_low, lhs_low, TMP);
1323 } else if (dst_low != lhs_low) {
1324 __ Move(dst_low, lhs_low);
1325 }
1326 if (IsUint<16>(high)) {
1327 __ Andi(dst_high, lhs_high, high);
1328 } else if (high != 0xFFFFFFFF) {
1329 if (high != low) {
1330 __ LoadConst32(TMP, high);
1331 }
1332 __ And(dst_high, lhs_high, TMP);
1333 } else if (dst_high != lhs_high) {
1334 __ Move(dst_high, lhs_high);
1335 }
1336 } else {
1337 if (instruction->IsSub()) {
1338 value = -value;
1339 } else {
1340 DCHECK(instruction->IsAdd());
1341 }
1342 int32_t low = Low32Bits(value);
1343 int32_t high = High32Bits(value);
1344 if (IsInt<16>(low)) {
1345 if (dst_low != lhs_low || low != 0) {
1346 __ Addiu(dst_low, lhs_low, low);
1347 }
1348 if (low != 0) {
1349 __ Sltiu(AT, dst_low, low);
1350 }
1351 } else {
1352 __ LoadConst32(TMP, low);
1353 __ Addu(dst_low, lhs_low, TMP);
1354 __ Sltu(AT, dst_low, TMP);
1355 }
1356 if (IsInt<16>(high)) {
1357 if (dst_high != lhs_high || high != 0) {
1358 __ Addiu(dst_high, lhs_high, high);
1359 }
1360 } else {
1361 if (high != low) {
1362 __ LoadConst32(TMP, high);
1363 }
1364 __ Addu(dst_high, lhs_high, TMP);
1365 }
1366 if (low != 0) {
1367 __ Addu(dst_high, dst_high, AT);
1368 }
1369 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001370 }
1371 break;
1372 }
1373
1374 case Primitive::kPrimFloat:
1375 case Primitive::kPrimDouble: {
1376 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1377 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1378 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1379 if (instruction->IsAdd()) {
1380 if (type == Primitive::kPrimFloat) {
1381 __ AddS(dst, lhs, rhs);
1382 } else {
1383 __ AddD(dst, lhs, rhs);
1384 }
1385 } else {
1386 DCHECK(instruction->IsSub());
1387 if (type == Primitive::kPrimFloat) {
1388 __ SubS(dst, lhs, rhs);
1389 } else {
1390 __ SubD(dst, lhs, rhs);
1391 }
1392 }
1393 break;
1394 }
1395
1396 default:
1397 LOG(FATAL) << "Unexpected binary operation type " << type;
1398 }
1399}
1400
1401void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001402 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001403
1404 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1405 Primitive::Type type = instr->GetResultType();
1406 switch (type) {
1407 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001408 locations->SetInAt(0, Location::RequiresRegister());
1409 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1411 break;
1412 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001413 locations->SetInAt(0, Location::RequiresRegister());
1414 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1415 locations->SetOut(Location::RequiresRegister());
1416 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001417 default:
1418 LOG(FATAL) << "Unexpected shift type " << type;
1419 }
1420}
1421
1422static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1423
1424void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001425 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001426 LocationSummary* locations = instr->GetLocations();
1427 Primitive::Type type = instr->GetType();
1428
1429 Location rhs_location = locations->InAt(1);
1430 bool use_imm = rhs_location.IsConstant();
1431 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1432 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001433 const uint32_t shift_mask =
1434 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001435 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001436 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1437 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001438
1439 switch (type) {
1440 case Primitive::kPrimInt: {
1441 Register dst = locations->Out().AsRegister<Register>();
1442 Register lhs = locations->InAt(0).AsRegister<Register>();
1443 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001444 if (shift_value == 0) {
1445 if (dst != lhs) {
1446 __ Move(dst, lhs);
1447 }
1448 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001449 __ Sll(dst, lhs, shift_value);
1450 } else if (instr->IsShr()) {
1451 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001452 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001453 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001454 } else {
1455 if (has_ins_rotr) {
1456 __ Rotr(dst, lhs, shift_value);
1457 } else {
1458 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1459 __ Srl(dst, lhs, shift_value);
1460 __ Or(dst, dst, TMP);
1461 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001462 }
1463 } else {
1464 if (instr->IsShl()) {
1465 __ Sllv(dst, lhs, rhs_reg);
1466 } else if (instr->IsShr()) {
1467 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001468 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001469 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001470 } else {
1471 if (has_ins_rotr) {
1472 __ Rotrv(dst, lhs, rhs_reg);
1473 } else {
1474 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001475 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1476 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1477 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1478 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1479 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001480 __ Sllv(TMP, lhs, TMP);
1481 __ Srlv(dst, lhs, rhs_reg);
1482 __ Or(dst, dst, TMP);
1483 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001484 }
1485 }
1486 break;
1487 }
1488
1489 case Primitive::kPrimLong: {
1490 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1491 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1492 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1493 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1494 if (use_imm) {
1495 if (shift_value == 0) {
1496 codegen_->Move64(locations->Out(), locations->InAt(0));
1497 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001498 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001499 if (instr->IsShl()) {
1500 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1501 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1502 __ Sll(dst_low, lhs_low, shift_value);
1503 } else if (instr->IsShr()) {
1504 __ Srl(dst_low, lhs_low, shift_value);
1505 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1506 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001507 } else if (instr->IsUShr()) {
1508 __ Srl(dst_low, lhs_low, shift_value);
1509 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1510 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001511 } else {
1512 __ Srl(dst_low, lhs_low, shift_value);
1513 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1514 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001515 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001516 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001517 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001518 if (instr->IsShl()) {
1519 __ Sll(dst_low, lhs_low, shift_value);
1520 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1521 __ Sll(dst_high, lhs_high, shift_value);
1522 __ Or(dst_high, dst_high, TMP);
1523 } else if (instr->IsShr()) {
1524 __ Sra(dst_high, lhs_high, shift_value);
1525 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1526 __ Srl(dst_low, lhs_low, shift_value);
1527 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001528 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001529 __ Srl(dst_high, lhs_high, shift_value);
1530 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1531 __ Srl(dst_low, lhs_low, shift_value);
1532 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001533 } else {
1534 __ Srl(TMP, lhs_low, shift_value);
1535 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1536 __ Or(dst_low, dst_low, TMP);
1537 __ Srl(TMP, lhs_high, shift_value);
1538 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1539 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001540 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001541 }
1542 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001543 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001544 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001545 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001546 __ Move(dst_low, ZERO);
1547 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001548 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001549 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001550 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001551 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001552 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001553 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001554 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001555 // 64-bit rotation by 32 is just a swap.
1556 __ Move(dst_low, lhs_high);
1557 __ Move(dst_high, lhs_low);
1558 } else {
1559 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001560 __ Srl(dst_low, lhs_high, shift_value_high);
1561 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1562 __ Srl(dst_high, lhs_low, shift_value_high);
1563 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001564 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001565 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1566 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001567 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001568 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1569 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001570 __ Or(dst_high, dst_high, TMP);
1571 }
1572 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001573 }
1574 }
1575 } else {
1576 MipsLabel done;
1577 if (instr->IsShl()) {
1578 __ Sllv(dst_low, lhs_low, rhs_reg);
1579 __ Nor(AT, ZERO, rhs_reg);
1580 __ Srl(TMP, lhs_low, 1);
1581 __ Srlv(TMP, TMP, AT);
1582 __ Sllv(dst_high, lhs_high, rhs_reg);
1583 __ Or(dst_high, dst_high, TMP);
1584 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1585 __ Beqz(TMP, &done);
1586 __ Move(dst_high, dst_low);
1587 __ Move(dst_low, ZERO);
1588 } else if (instr->IsShr()) {
1589 __ Srav(dst_high, lhs_high, rhs_reg);
1590 __ Nor(AT, ZERO, rhs_reg);
1591 __ Sll(TMP, lhs_high, 1);
1592 __ Sllv(TMP, TMP, AT);
1593 __ Srlv(dst_low, lhs_low, rhs_reg);
1594 __ Or(dst_low, dst_low, TMP);
1595 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1596 __ Beqz(TMP, &done);
1597 __ Move(dst_low, dst_high);
1598 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001599 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001600 __ Srlv(dst_high, lhs_high, rhs_reg);
1601 __ Nor(AT, ZERO, rhs_reg);
1602 __ Sll(TMP, lhs_high, 1);
1603 __ Sllv(TMP, TMP, AT);
1604 __ Srlv(dst_low, lhs_low, rhs_reg);
1605 __ Or(dst_low, dst_low, TMP);
1606 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1607 __ Beqz(TMP, &done);
1608 __ Move(dst_low, dst_high);
1609 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001610 } else {
1611 __ Nor(AT, ZERO, rhs_reg);
1612 __ Srlv(TMP, lhs_low, rhs_reg);
1613 __ Sll(dst_low, lhs_high, 1);
1614 __ Sllv(dst_low, dst_low, AT);
1615 __ Or(dst_low, dst_low, TMP);
1616 __ Srlv(TMP, lhs_high, rhs_reg);
1617 __ Sll(dst_high, lhs_low, 1);
1618 __ Sllv(dst_high, dst_high, AT);
1619 __ Or(dst_high, dst_high, TMP);
1620 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1621 __ Beqz(TMP, &done);
1622 __ Move(TMP, dst_high);
1623 __ Move(dst_high, dst_low);
1624 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001625 }
1626 __ Bind(&done);
1627 }
1628 break;
1629 }
1630
1631 default:
1632 LOG(FATAL) << "Unexpected shift operation type " << type;
1633 }
1634}
1635
1636void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1637 HandleBinaryOp(instruction);
1638}
1639
1640void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1641 HandleBinaryOp(instruction);
1642}
1643
1644void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1645 HandleBinaryOp(instruction);
1646}
1647
1648void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1649 HandleBinaryOp(instruction);
1650}
1651
1652void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1653 LocationSummary* locations =
1654 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1655 locations->SetInAt(0, Location::RequiresRegister());
1656 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1657 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1658 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1659 } else {
1660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1661 }
1662}
1663
1664void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1665 LocationSummary* locations = instruction->GetLocations();
1666 Register obj = locations->InAt(0).AsRegister<Register>();
1667 Location index = locations->InAt(1);
1668 Primitive::Type type = instruction->GetType();
1669
1670 switch (type) {
1671 case Primitive::kPrimBoolean: {
1672 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1673 Register out = locations->Out().AsRegister<Register>();
1674 if (index.IsConstant()) {
1675 size_t offset =
1676 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1677 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1678 } else {
1679 __ Addu(TMP, obj, index.AsRegister<Register>());
1680 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1681 }
1682 break;
1683 }
1684
1685 case Primitive::kPrimByte: {
1686 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1687 Register out = locations->Out().AsRegister<Register>();
1688 if (index.IsConstant()) {
1689 size_t offset =
1690 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1691 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1692 } else {
1693 __ Addu(TMP, obj, index.AsRegister<Register>());
1694 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1695 }
1696 break;
1697 }
1698
1699 case Primitive::kPrimShort: {
1700 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1701 Register out = locations->Out().AsRegister<Register>();
1702 if (index.IsConstant()) {
1703 size_t offset =
1704 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1705 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1706 } else {
1707 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1708 __ Addu(TMP, obj, TMP);
1709 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1710 }
1711 break;
1712 }
1713
1714 case Primitive::kPrimChar: {
1715 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1716 Register out = locations->Out().AsRegister<Register>();
1717 if (index.IsConstant()) {
1718 size_t offset =
1719 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1720 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1721 } else {
1722 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1723 __ Addu(TMP, obj, TMP);
1724 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1725 }
1726 break;
1727 }
1728
1729 case Primitive::kPrimInt:
1730 case Primitive::kPrimNot: {
1731 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1732 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1733 Register out = locations->Out().AsRegister<Register>();
1734 if (index.IsConstant()) {
1735 size_t offset =
1736 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1737 __ LoadFromOffset(kLoadWord, out, obj, offset);
1738 } else {
1739 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1740 __ Addu(TMP, obj, TMP);
1741 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1742 }
1743 break;
1744 }
1745
1746 case Primitive::kPrimLong: {
1747 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1748 Register out = locations->Out().AsRegisterPairLow<Register>();
1749 if (index.IsConstant()) {
1750 size_t offset =
1751 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1752 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1753 } else {
1754 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1755 __ Addu(TMP, obj, TMP);
1756 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1757 }
1758 break;
1759 }
1760
1761 case Primitive::kPrimFloat: {
1762 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1763 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1764 if (index.IsConstant()) {
1765 size_t offset =
1766 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1767 __ LoadSFromOffset(out, obj, offset);
1768 } else {
1769 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1770 __ Addu(TMP, obj, TMP);
1771 __ LoadSFromOffset(out, TMP, data_offset);
1772 }
1773 break;
1774 }
1775
1776 case Primitive::kPrimDouble: {
1777 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1778 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1779 if (index.IsConstant()) {
1780 size_t offset =
1781 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1782 __ LoadDFromOffset(out, obj, offset);
1783 } else {
1784 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1785 __ Addu(TMP, obj, TMP);
1786 __ LoadDFromOffset(out, TMP, data_offset);
1787 }
1788 break;
1789 }
1790
1791 case Primitive::kPrimVoid:
1792 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1793 UNREACHABLE();
1794 }
1795 codegen_->MaybeRecordImplicitNullCheck(instruction);
1796}
1797
1798void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1799 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1800 locations->SetInAt(0, Location::RequiresRegister());
1801 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1802}
1803
1804void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1805 LocationSummary* locations = instruction->GetLocations();
1806 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1807 Register obj = locations->InAt(0).AsRegister<Register>();
1808 Register out = locations->Out().AsRegister<Register>();
1809 __ LoadFromOffset(kLoadWord, out, obj, offset);
1810 codegen_->MaybeRecordImplicitNullCheck(instruction);
1811}
1812
1813void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001814 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001815 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1816 instruction,
Pavle Batuta934808f2015-11-03 13:23:54 +01001817 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1818 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001819 InvokeRuntimeCallingConvention calling_convention;
1820 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1821 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1822 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1823 } else {
1824 locations->SetInAt(0, Location::RequiresRegister());
1825 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1826 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1827 locations->SetInAt(2, Location::RequiresFpuRegister());
1828 } else {
1829 locations->SetInAt(2, Location::RequiresRegister());
1830 }
1831 }
1832}
1833
1834void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1835 LocationSummary* locations = instruction->GetLocations();
1836 Register obj = locations->InAt(0).AsRegister<Register>();
1837 Location index = locations->InAt(1);
1838 Primitive::Type value_type = instruction->GetComponentType();
1839 bool needs_runtime_call = locations->WillCall();
1840 bool needs_write_barrier =
1841 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1842
1843 switch (value_type) {
1844 case Primitive::kPrimBoolean:
1845 case Primitive::kPrimByte: {
1846 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1847 Register value = locations->InAt(2).AsRegister<Register>();
1848 if (index.IsConstant()) {
1849 size_t offset =
1850 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1851 __ StoreToOffset(kStoreByte, value, obj, offset);
1852 } else {
1853 __ Addu(TMP, obj, index.AsRegister<Register>());
1854 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1855 }
1856 break;
1857 }
1858
1859 case Primitive::kPrimShort:
1860 case Primitive::kPrimChar: {
1861 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1862 Register value = locations->InAt(2).AsRegister<Register>();
1863 if (index.IsConstant()) {
1864 size_t offset =
1865 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1866 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1867 } else {
1868 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1869 __ Addu(TMP, obj, TMP);
1870 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1871 }
1872 break;
1873 }
1874
1875 case Primitive::kPrimInt:
1876 case Primitive::kPrimNot: {
1877 if (!needs_runtime_call) {
1878 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1879 Register value = locations->InAt(2).AsRegister<Register>();
1880 if (index.IsConstant()) {
1881 size_t offset =
1882 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1883 __ StoreToOffset(kStoreWord, value, obj, offset);
1884 } else {
1885 DCHECK(index.IsRegister()) << index;
1886 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1887 __ Addu(TMP, obj, TMP);
1888 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1889 }
1890 codegen_->MaybeRecordImplicitNullCheck(instruction);
1891 if (needs_write_barrier) {
1892 DCHECK_EQ(value_type, Primitive::kPrimNot);
1893 codegen_->MarkGCCard(obj, value);
1894 }
1895 } else {
1896 DCHECK_EQ(value_type, Primitive::kPrimNot);
1897 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1898 instruction,
1899 instruction->GetDexPc(),
1900 nullptr,
1901 IsDirectEntrypoint(kQuickAputObject));
1902 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
1903 }
1904 break;
1905 }
1906
1907 case Primitive::kPrimLong: {
1908 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1909 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
1910 if (index.IsConstant()) {
1911 size_t offset =
1912 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1913 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1914 } else {
1915 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1916 __ Addu(TMP, obj, TMP);
1917 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1918 }
1919 break;
1920 }
1921
1922 case Primitive::kPrimFloat: {
1923 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1924 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1925 DCHECK(locations->InAt(2).IsFpuRegister());
1926 if (index.IsConstant()) {
1927 size_t offset =
1928 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1929 __ StoreSToOffset(value, obj, offset);
1930 } else {
1931 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1932 __ Addu(TMP, obj, TMP);
1933 __ StoreSToOffset(value, TMP, data_offset);
1934 }
1935 break;
1936 }
1937
1938 case Primitive::kPrimDouble: {
1939 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1940 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1941 DCHECK(locations->InAt(2).IsFpuRegister());
1942 if (index.IsConstant()) {
1943 size_t offset =
1944 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1945 __ StoreDToOffset(value, obj, offset);
1946 } else {
1947 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1948 __ Addu(TMP, obj, TMP);
1949 __ StoreDToOffset(value, TMP, data_offset);
1950 }
1951 break;
1952 }
1953
1954 case Primitive::kPrimVoid:
1955 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1956 UNREACHABLE();
1957 }
1958
1959 // Ints and objects are handled in the switch.
1960 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1961 codegen_->MaybeRecordImplicitNullCheck(instruction);
1962 }
1963}
1964
1965void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1966 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1967 ? LocationSummary::kCallOnSlowPath
1968 : LocationSummary::kNoCall;
1969 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1970 locations->SetInAt(0, Location::RequiresRegister());
1971 locations->SetInAt(1, Location::RequiresRegister());
1972 if (instruction->HasUses()) {
1973 locations->SetOut(Location::SameAsFirstInput());
1974 }
1975}
1976
1977void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1978 LocationSummary* locations = instruction->GetLocations();
1979 BoundsCheckSlowPathMIPS* slow_path =
1980 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
1981 codegen_->AddSlowPath(slow_path);
1982
1983 Register index = locations->InAt(0).AsRegister<Register>();
1984 Register length = locations->InAt(1).AsRegister<Register>();
1985
1986 // length is limited by the maximum positive signed 32-bit integer.
1987 // Unsigned comparison of length and index checks for index < 0
1988 // and for length <= index simultaneously.
1989 __ Bgeu(index, length, slow_path->GetEntryLabel());
1990}
1991
1992void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
1993 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1994 instruction,
1995 LocationSummary::kCallOnSlowPath);
1996 locations->SetInAt(0, Location::RequiresRegister());
1997 locations->SetInAt(1, Location::RequiresRegister());
1998 // Note that TypeCheckSlowPathMIPS uses this register too.
1999 locations->AddTemp(Location::RequiresRegister());
2000}
2001
2002void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2003 LocationSummary* locations = instruction->GetLocations();
2004 Register obj = locations->InAt(0).AsRegister<Register>();
2005 Register cls = locations->InAt(1).AsRegister<Register>();
2006 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2007
2008 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2009 codegen_->AddSlowPath(slow_path);
2010
2011 // TODO: avoid this check if we know obj is not null.
2012 __ Beqz(obj, slow_path->GetExitLabel());
2013 // Compare the class of `obj` with `cls`.
2014 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2015 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2016 __ Bind(slow_path->GetExitLabel());
2017}
2018
2019void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2020 LocationSummary* locations =
2021 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2022 locations->SetInAt(0, Location::RequiresRegister());
2023 if (check->HasUses()) {
2024 locations->SetOut(Location::SameAsFirstInput());
2025 }
2026}
2027
2028void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2029 // We assume the class is not null.
2030 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2031 check->GetLoadClass(),
2032 check,
2033 check->GetDexPc(),
2034 true);
2035 codegen_->AddSlowPath(slow_path);
2036 GenerateClassInitializationCheck(slow_path,
2037 check->GetLocations()->InAt(0).AsRegister<Register>());
2038}
2039
2040void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2041 Primitive::Type in_type = compare->InputAt(0)->GetType();
2042
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002043 LocationSummary* locations =
2044 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002045
2046 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002047 case Primitive::kPrimBoolean:
2048 case Primitive::kPrimByte:
2049 case Primitive::kPrimShort:
2050 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002051 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002052 case Primitive::kPrimLong:
2053 locations->SetInAt(0, Location::RequiresRegister());
2054 locations->SetInAt(1, Location::RequiresRegister());
2055 // Output overlaps because it is written before doing the low comparison.
2056 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2057 break;
2058
2059 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002060 case Primitive::kPrimDouble:
2061 locations->SetInAt(0, Location::RequiresFpuRegister());
2062 locations->SetInAt(1, Location::RequiresFpuRegister());
2063 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002064 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002065
2066 default:
2067 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2068 }
2069}
2070
2071void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2072 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002073 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002074 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002075 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002076
2077 // 0 if: left == right
2078 // 1 if: left > right
2079 // -1 if: left < right
2080 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002081 case Primitive::kPrimBoolean:
2082 case Primitive::kPrimByte:
2083 case Primitive::kPrimShort:
2084 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002085 case Primitive::kPrimInt: {
2086 Register lhs = locations->InAt(0).AsRegister<Register>();
2087 Register rhs = locations->InAt(1).AsRegister<Register>();
2088 __ Slt(TMP, lhs, rhs);
2089 __ Slt(res, rhs, lhs);
2090 __ Subu(res, res, TMP);
2091 break;
2092 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002093 case Primitive::kPrimLong: {
2094 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002095 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2096 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2097 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2098 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2099 // TODO: more efficient (direct) comparison with a constant.
2100 __ Slt(TMP, lhs_high, rhs_high);
2101 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2102 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2103 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2104 __ Sltu(TMP, lhs_low, rhs_low);
2105 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2106 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2107 __ Bind(&done);
2108 break;
2109 }
2110
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002111 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002112 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002113 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2114 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2115 MipsLabel done;
2116 if (isR6) {
2117 __ CmpEqS(FTMP, lhs, rhs);
2118 __ LoadConst32(res, 0);
2119 __ Bc1nez(FTMP, &done);
2120 if (gt_bias) {
2121 __ CmpLtS(FTMP, lhs, rhs);
2122 __ LoadConst32(res, -1);
2123 __ Bc1nez(FTMP, &done);
2124 __ LoadConst32(res, 1);
2125 } else {
2126 __ CmpLtS(FTMP, rhs, lhs);
2127 __ LoadConst32(res, 1);
2128 __ Bc1nez(FTMP, &done);
2129 __ LoadConst32(res, -1);
2130 }
2131 } else {
2132 if (gt_bias) {
2133 __ ColtS(0, lhs, rhs);
2134 __ LoadConst32(res, -1);
2135 __ Bc1t(0, &done);
2136 __ CeqS(0, lhs, rhs);
2137 __ LoadConst32(res, 1);
2138 __ Movt(res, ZERO, 0);
2139 } else {
2140 __ ColtS(0, rhs, lhs);
2141 __ LoadConst32(res, 1);
2142 __ Bc1t(0, &done);
2143 __ CeqS(0, lhs, rhs);
2144 __ LoadConst32(res, -1);
2145 __ Movt(res, ZERO, 0);
2146 }
2147 }
2148 __ Bind(&done);
2149 break;
2150 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002151 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002152 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002153 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2154 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2155 MipsLabel done;
2156 if (isR6) {
2157 __ CmpEqD(FTMP, lhs, rhs);
2158 __ LoadConst32(res, 0);
2159 __ Bc1nez(FTMP, &done);
2160 if (gt_bias) {
2161 __ CmpLtD(FTMP, lhs, rhs);
2162 __ LoadConst32(res, -1);
2163 __ Bc1nez(FTMP, &done);
2164 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002165 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002166 __ CmpLtD(FTMP, rhs, lhs);
2167 __ LoadConst32(res, 1);
2168 __ Bc1nez(FTMP, &done);
2169 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002170 }
2171 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002172 if (gt_bias) {
2173 __ ColtD(0, lhs, rhs);
2174 __ LoadConst32(res, -1);
2175 __ Bc1t(0, &done);
2176 __ CeqD(0, lhs, rhs);
2177 __ LoadConst32(res, 1);
2178 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002179 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002180 __ ColtD(0, rhs, lhs);
2181 __ LoadConst32(res, 1);
2182 __ Bc1t(0, &done);
2183 __ CeqD(0, lhs, rhs);
2184 __ LoadConst32(res, -1);
2185 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002186 }
2187 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002188 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002189 break;
2190 }
2191
2192 default:
2193 LOG(FATAL) << "Unimplemented compare type " << in_type;
2194 }
2195}
2196
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002197void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002198 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002199 switch (instruction->InputAt(0)->GetType()) {
2200 default:
2201 case Primitive::kPrimLong:
2202 locations->SetInAt(0, Location::RequiresRegister());
2203 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2204 break;
2205
2206 case Primitive::kPrimFloat:
2207 case Primitive::kPrimDouble:
2208 locations->SetInAt(0, Location::RequiresFpuRegister());
2209 locations->SetInAt(1, Location::RequiresFpuRegister());
2210 break;
2211 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002212 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002213 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2214 }
2215}
2216
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002217void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002218 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002219 return;
2220 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002221
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002222 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002223 LocationSummary* locations = instruction->GetLocations();
2224 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002225 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002226
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002227 switch (type) {
2228 default:
2229 // Integer case.
2230 GenerateIntCompare(instruction->GetCondition(), locations);
2231 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002232
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002233 case Primitive::kPrimLong:
2234 // TODO: don't use branches.
2235 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002236 break;
2237
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002238 case Primitive::kPrimFloat:
2239 case Primitive::kPrimDouble:
2240 // TODO: don't use branches.
2241 GenerateFpCompareAndBranch(instruction->GetCondition(),
2242 instruction->IsGtBias(),
2243 type,
2244 locations,
2245 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002246 break;
2247 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002248
2249 // Convert the branches into the result.
2250 MipsLabel done;
2251
2252 // False case: result = 0.
2253 __ LoadConst32(dst, 0);
2254 __ B(&done);
2255
2256 // True case: result = 1.
2257 __ Bind(&true_label);
2258 __ LoadConst32(dst, 1);
2259 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260}
2261
Alexey Frunze7e99e052015-11-24 19:28:01 -08002262void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2263 DCHECK(instruction->IsDiv() || instruction->IsRem());
2264 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2265
2266 LocationSummary* locations = instruction->GetLocations();
2267 Location second = locations->InAt(1);
2268 DCHECK(second.IsConstant());
2269
2270 Register out = locations->Out().AsRegister<Register>();
2271 Register dividend = locations->InAt(0).AsRegister<Register>();
2272 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2273 DCHECK(imm == 1 || imm == -1);
2274
2275 if (instruction->IsRem()) {
2276 __ Move(out, ZERO);
2277 } else {
2278 if (imm == -1) {
2279 __ Subu(out, ZERO, dividend);
2280 } else if (out != dividend) {
2281 __ Move(out, dividend);
2282 }
2283 }
2284}
2285
2286void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2287 DCHECK(instruction->IsDiv() || instruction->IsRem());
2288 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2289
2290 LocationSummary* locations = instruction->GetLocations();
2291 Location second = locations->InAt(1);
2292 DCHECK(second.IsConstant());
2293
2294 Register out = locations->Out().AsRegister<Register>();
2295 Register dividend = locations->InAt(0).AsRegister<Register>();
2296 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002297 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002298 int ctz_imm = CTZ(abs_imm);
2299
2300 if (instruction->IsDiv()) {
2301 if (ctz_imm == 1) {
2302 // Fast path for division by +/-2, which is very common.
2303 __ Srl(TMP, dividend, 31);
2304 } else {
2305 __ Sra(TMP, dividend, 31);
2306 __ Srl(TMP, TMP, 32 - ctz_imm);
2307 }
2308 __ Addu(out, dividend, TMP);
2309 __ Sra(out, out, ctz_imm);
2310 if (imm < 0) {
2311 __ Subu(out, ZERO, out);
2312 }
2313 } else {
2314 if (ctz_imm == 1) {
2315 // Fast path for modulo +/-2, which is very common.
2316 __ Sra(TMP, dividend, 31);
2317 __ Subu(out, dividend, TMP);
2318 __ Andi(out, out, 1);
2319 __ Addu(out, out, TMP);
2320 } else {
2321 __ Sra(TMP, dividend, 31);
2322 __ Srl(TMP, TMP, 32 - ctz_imm);
2323 __ Addu(out, dividend, TMP);
2324 if (IsUint<16>(abs_imm - 1)) {
2325 __ Andi(out, out, abs_imm - 1);
2326 } else {
2327 __ Sll(out, out, 32 - ctz_imm);
2328 __ Srl(out, out, 32 - ctz_imm);
2329 }
2330 __ Subu(out, out, TMP);
2331 }
2332 }
2333}
2334
2335void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2336 DCHECK(instruction->IsDiv() || instruction->IsRem());
2337 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2338
2339 LocationSummary* locations = instruction->GetLocations();
2340 Location second = locations->InAt(1);
2341 DCHECK(second.IsConstant());
2342
2343 Register out = locations->Out().AsRegister<Register>();
2344 Register dividend = locations->InAt(0).AsRegister<Register>();
2345 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2346
2347 int64_t magic;
2348 int shift;
2349 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2350
2351 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2352
2353 __ LoadConst32(TMP, magic);
2354 if (isR6) {
2355 __ MuhR6(TMP, dividend, TMP);
2356 } else {
2357 __ MultR2(dividend, TMP);
2358 __ Mfhi(TMP);
2359 }
2360 if (imm > 0 && magic < 0) {
2361 __ Addu(TMP, TMP, dividend);
2362 } else if (imm < 0 && magic > 0) {
2363 __ Subu(TMP, TMP, dividend);
2364 }
2365
2366 if (shift != 0) {
2367 __ Sra(TMP, TMP, shift);
2368 }
2369
2370 if (instruction->IsDiv()) {
2371 __ Sra(out, TMP, 31);
2372 __ Subu(out, TMP, out);
2373 } else {
2374 __ Sra(AT, TMP, 31);
2375 __ Subu(AT, TMP, AT);
2376 __ LoadConst32(TMP, imm);
2377 if (isR6) {
2378 __ MulR6(TMP, AT, TMP);
2379 } else {
2380 __ MulR2(TMP, AT, TMP);
2381 }
2382 __ Subu(out, dividend, TMP);
2383 }
2384}
2385
2386void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2387 DCHECK(instruction->IsDiv() || instruction->IsRem());
2388 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2389
2390 LocationSummary* locations = instruction->GetLocations();
2391 Register out = locations->Out().AsRegister<Register>();
2392 Location second = locations->InAt(1);
2393
2394 if (second.IsConstant()) {
2395 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2396 if (imm == 0) {
2397 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2398 } else if (imm == 1 || imm == -1) {
2399 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002400 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002401 DivRemByPowerOfTwo(instruction);
2402 } else {
2403 DCHECK(imm <= -2 || imm >= 2);
2404 GenerateDivRemWithAnyConstant(instruction);
2405 }
2406 } else {
2407 Register dividend = locations->InAt(0).AsRegister<Register>();
2408 Register divisor = second.AsRegister<Register>();
2409 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2410 if (instruction->IsDiv()) {
2411 if (isR6) {
2412 __ DivR6(out, dividend, divisor);
2413 } else {
2414 __ DivR2(out, dividend, divisor);
2415 }
2416 } else {
2417 if (isR6) {
2418 __ ModR6(out, dividend, divisor);
2419 } else {
2420 __ ModR2(out, dividend, divisor);
2421 }
2422 }
2423 }
2424}
2425
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002426void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2427 Primitive::Type type = div->GetResultType();
2428 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
2429 ? LocationSummary::kCall
2430 : LocationSummary::kNoCall;
2431
2432 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2433
2434 switch (type) {
2435 case Primitive::kPrimInt:
2436 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002437 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2439 break;
2440
2441 case Primitive::kPrimLong: {
2442 InvokeRuntimeCallingConvention calling_convention;
2443 locations->SetInAt(0, Location::RegisterPairLocation(
2444 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2445 locations->SetInAt(1, Location::RegisterPairLocation(
2446 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2447 locations->SetOut(calling_convention.GetReturnLocation(type));
2448 break;
2449 }
2450
2451 case Primitive::kPrimFloat:
2452 case Primitive::kPrimDouble:
2453 locations->SetInAt(0, Location::RequiresFpuRegister());
2454 locations->SetInAt(1, Location::RequiresFpuRegister());
2455 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2456 break;
2457
2458 default:
2459 LOG(FATAL) << "Unexpected div type " << type;
2460 }
2461}
2462
2463void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2464 Primitive::Type type = instruction->GetType();
2465 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002466
2467 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002468 case Primitive::kPrimInt:
2469 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002470 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002471 case Primitive::kPrimLong: {
2472 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2473 instruction,
2474 instruction->GetDexPc(),
2475 nullptr,
2476 IsDirectEntrypoint(kQuickLdiv));
2477 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2478 break;
2479 }
2480 case Primitive::kPrimFloat:
2481 case Primitive::kPrimDouble: {
2482 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2483 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2484 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2485 if (type == Primitive::kPrimFloat) {
2486 __ DivS(dst, lhs, rhs);
2487 } else {
2488 __ DivD(dst, lhs, rhs);
2489 }
2490 break;
2491 }
2492 default:
2493 LOG(FATAL) << "Unexpected div type " << type;
2494 }
2495}
2496
2497void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2498 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2499 ? LocationSummary::kCallOnSlowPath
2500 : LocationSummary::kNoCall;
2501 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2502 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2503 if (instruction->HasUses()) {
2504 locations->SetOut(Location::SameAsFirstInput());
2505 }
2506}
2507
2508void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2509 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2510 codegen_->AddSlowPath(slow_path);
2511 Location value = instruction->GetLocations()->InAt(0);
2512 Primitive::Type type = instruction->GetType();
2513
2514 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002515 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002516 case Primitive::kPrimByte:
2517 case Primitive::kPrimChar:
2518 case Primitive::kPrimShort:
2519 case Primitive::kPrimInt: {
2520 if (value.IsConstant()) {
2521 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2522 __ B(slow_path->GetEntryLabel());
2523 } else {
2524 // A division by a non-null constant is valid. We don't need to perform
2525 // any check, so simply fall through.
2526 }
2527 } else {
2528 DCHECK(value.IsRegister()) << value;
2529 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2530 }
2531 break;
2532 }
2533 case Primitive::kPrimLong: {
2534 if (value.IsConstant()) {
2535 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2536 __ B(slow_path->GetEntryLabel());
2537 } else {
2538 // A division by a non-null constant is valid. We don't need to perform
2539 // any check, so simply fall through.
2540 }
2541 } else {
2542 DCHECK(value.IsRegisterPair()) << value;
2543 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2544 __ Beqz(TMP, slow_path->GetEntryLabel());
2545 }
2546 break;
2547 }
2548 default:
2549 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2550 }
2551}
2552
2553void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2554 LocationSummary* locations =
2555 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2556 locations->SetOut(Location::ConstantLocation(constant));
2557}
2558
2559void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2560 // Will be generated at use site.
2561}
2562
2563void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2564 exit->SetLocations(nullptr);
2565}
2566
2567void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2568}
2569
2570void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2571 LocationSummary* locations =
2572 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2573 locations->SetOut(Location::ConstantLocation(constant));
2574}
2575
2576void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2577 // Will be generated at use site.
2578}
2579
2580void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2581 got->SetLocations(nullptr);
2582}
2583
2584void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2585 DCHECK(!successor->IsExitBlock());
2586 HBasicBlock* block = got->GetBlock();
2587 HInstruction* previous = got->GetPrevious();
2588 HLoopInformation* info = block->GetLoopInformation();
2589
2590 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2591 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2592 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2593 return;
2594 }
2595 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2596 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2597 }
2598 if (!codegen_->GoesToNextBlock(block, successor)) {
2599 __ B(codegen_->GetLabelOf(successor));
2600 }
2601}
2602
2603void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2604 HandleGoto(got, got->GetSuccessor());
2605}
2606
2607void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2608 try_boundary->SetLocations(nullptr);
2609}
2610
2611void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2612 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2613 if (!successor->IsExitBlock()) {
2614 HandleGoto(try_boundary, successor);
2615 }
2616}
2617
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002618void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2619 LocationSummary* locations) {
2620 Register dst = locations->Out().AsRegister<Register>();
2621 Register lhs = locations->InAt(0).AsRegister<Register>();
2622 Location rhs_location = locations->InAt(1);
2623 Register rhs_reg = ZERO;
2624 int64_t rhs_imm = 0;
2625 bool use_imm = rhs_location.IsConstant();
2626 if (use_imm) {
2627 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2628 } else {
2629 rhs_reg = rhs_location.AsRegister<Register>();
2630 }
2631
2632 switch (cond) {
2633 case kCondEQ:
2634 case kCondNE:
2635 if (use_imm && IsUint<16>(rhs_imm)) {
2636 __ Xori(dst, lhs, rhs_imm);
2637 } else {
2638 if (use_imm) {
2639 rhs_reg = TMP;
2640 __ LoadConst32(rhs_reg, rhs_imm);
2641 }
2642 __ Xor(dst, lhs, rhs_reg);
2643 }
2644 if (cond == kCondEQ) {
2645 __ Sltiu(dst, dst, 1);
2646 } else {
2647 __ Sltu(dst, ZERO, dst);
2648 }
2649 break;
2650
2651 case kCondLT:
2652 case kCondGE:
2653 if (use_imm && IsInt<16>(rhs_imm)) {
2654 __ Slti(dst, lhs, rhs_imm);
2655 } else {
2656 if (use_imm) {
2657 rhs_reg = TMP;
2658 __ LoadConst32(rhs_reg, rhs_imm);
2659 }
2660 __ Slt(dst, lhs, rhs_reg);
2661 }
2662 if (cond == kCondGE) {
2663 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2664 // only the slt instruction but no sge.
2665 __ Xori(dst, dst, 1);
2666 }
2667 break;
2668
2669 case kCondLE:
2670 case kCondGT:
2671 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2672 // Simulate lhs <= rhs via lhs < rhs + 1.
2673 __ Slti(dst, lhs, rhs_imm + 1);
2674 if (cond == kCondGT) {
2675 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2676 // only the slti instruction but no sgti.
2677 __ Xori(dst, dst, 1);
2678 }
2679 } else {
2680 if (use_imm) {
2681 rhs_reg = TMP;
2682 __ LoadConst32(rhs_reg, rhs_imm);
2683 }
2684 __ Slt(dst, rhs_reg, lhs);
2685 if (cond == kCondLE) {
2686 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2687 // only the slt instruction but no sle.
2688 __ Xori(dst, dst, 1);
2689 }
2690 }
2691 break;
2692
2693 case kCondB:
2694 case kCondAE:
2695 if (use_imm && IsInt<16>(rhs_imm)) {
2696 // Sltiu sign-extends its 16-bit immediate operand before
2697 // the comparison and thus lets us compare directly with
2698 // unsigned values in the ranges [0, 0x7fff] and
2699 // [0xffff8000, 0xffffffff].
2700 __ Sltiu(dst, lhs, rhs_imm);
2701 } else {
2702 if (use_imm) {
2703 rhs_reg = TMP;
2704 __ LoadConst32(rhs_reg, rhs_imm);
2705 }
2706 __ Sltu(dst, lhs, rhs_reg);
2707 }
2708 if (cond == kCondAE) {
2709 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2710 // only the sltu instruction but no sgeu.
2711 __ Xori(dst, dst, 1);
2712 }
2713 break;
2714
2715 case kCondBE:
2716 case kCondA:
2717 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2718 // Simulate lhs <= rhs via lhs < rhs + 1.
2719 // Note that this only works if rhs + 1 does not overflow
2720 // to 0, hence the check above.
2721 // Sltiu sign-extends its 16-bit immediate operand before
2722 // the comparison and thus lets us compare directly with
2723 // unsigned values in the ranges [0, 0x7fff] and
2724 // [0xffff8000, 0xffffffff].
2725 __ Sltiu(dst, lhs, rhs_imm + 1);
2726 if (cond == kCondA) {
2727 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2728 // only the sltiu instruction but no sgtiu.
2729 __ Xori(dst, dst, 1);
2730 }
2731 } else {
2732 if (use_imm) {
2733 rhs_reg = TMP;
2734 __ LoadConst32(rhs_reg, rhs_imm);
2735 }
2736 __ Sltu(dst, rhs_reg, lhs);
2737 if (cond == kCondBE) {
2738 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2739 // only the sltu instruction but no sleu.
2740 __ Xori(dst, dst, 1);
2741 }
2742 }
2743 break;
2744 }
2745}
2746
2747void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2748 LocationSummary* locations,
2749 MipsLabel* label) {
2750 Register lhs = locations->InAt(0).AsRegister<Register>();
2751 Location rhs_location = locations->InAt(1);
2752 Register rhs_reg = ZERO;
2753 int32_t rhs_imm = 0;
2754 bool use_imm = rhs_location.IsConstant();
2755 if (use_imm) {
2756 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2757 } else {
2758 rhs_reg = rhs_location.AsRegister<Register>();
2759 }
2760
2761 if (use_imm && rhs_imm == 0) {
2762 switch (cond) {
2763 case kCondEQ:
2764 case kCondBE: // <= 0 if zero
2765 __ Beqz(lhs, label);
2766 break;
2767 case kCondNE:
2768 case kCondA: // > 0 if non-zero
2769 __ Bnez(lhs, label);
2770 break;
2771 case kCondLT:
2772 __ Bltz(lhs, label);
2773 break;
2774 case kCondGE:
2775 __ Bgez(lhs, label);
2776 break;
2777 case kCondLE:
2778 __ Blez(lhs, label);
2779 break;
2780 case kCondGT:
2781 __ Bgtz(lhs, label);
2782 break;
2783 case kCondB: // always false
2784 break;
2785 case kCondAE: // always true
2786 __ B(label);
2787 break;
2788 }
2789 } else {
2790 if (use_imm) {
2791 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2792 rhs_reg = TMP;
2793 __ LoadConst32(rhs_reg, rhs_imm);
2794 }
2795 switch (cond) {
2796 case kCondEQ:
2797 __ Beq(lhs, rhs_reg, label);
2798 break;
2799 case kCondNE:
2800 __ Bne(lhs, rhs_reg, label);
2801 break;
2802 case kCondLT:
2803 __ Blt(lhs, rhs_reg, label);
2804 break;
2805 case kCondGE:
2806 __ Bge(lhs, rhs_reg, label);
2807 break;
2808 case kCondLE:
2809 __ Bge(rhs_reg, lhs, label);
2810 break;
2811 case kCondGT:
2812 __ Blt(rhs_reg, lhs, label);
2813 break;
2814 case kCondB:
2815 __ Bltu(lhs, rhs_reg, label);
2816 break;
2817 case kCondAE:
2818 __ Bgeu(lhs, rhs_reg, label);
2819 break;
2820 case kCondBE:
2821 __ Bgeu(rhs_reg, lhs, label);
2822 break;
2823 case kCondA:
2824 __ Bltu(rhs_reg, lhs, label);
2825 break;
2826 }
2827 }
2828}
2829
2830void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2831 LocationSummary* locations,
2832 MipsLabel* label) {
2833 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2834 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2835 Location rhs_location = locations->InAt(1);
2836 Register rhs_high = ZERO;
2837 Register rhs_low = ZERO;
2838 int64_t imm = 0;
2839 uint32_t imm_high = 0;
2840 uint32_t imm_low = 0;
2841 bool use_imm = rhs_location.IsConstant();
2842 if (use_imm) {
2843 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2844 imm_high = High32Bits(imm);
2845 imm_low = Low32Bits(imm);
2846 } else {
2847 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2848 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2849 }
2850
2851 if (use_imm && imm == 0) {
2852 switch (cond) {
2853 case kCondEQ:
2854 case kCondBE: // <= 0 if zero
2855 __ Or(TMP, lhs_high, lhs_low);
2856 __ Beqz(TMP, label);
2857 break;
2858 case kCondNE:
2859 case kCondA: // > 0 if non-zero
2860 __ Or(TMP, lhs_high, lhs_low);
2861 __ Bnez(TMP, label);
2862 break;
2863 case kCondLT:
2864 __ Bltz(lhs_high, label);
2865 break;
2866 case kCondGE:
2867 __ Bgez(lhs_high, label);
2868 break;
2869 case kCondLE:
2870 __ Or(TMP, lhs_high, lhs_low);
2871 __ Sra(AT, lhs_high, 31);
2872 __ Bgeu(AT, TMP, label);
2873 break;
2874 case kCondGT:
2875 __ Or(TMP, lhs_high, lhs_low);
2876 __ Sra(AT, lhs_high, 31);
2877 __ Bltu(AT, TMP, label);
2878 break;
2879 case kCondB: // always false
2880 break;
2881 case kCondAE: // always true
2882 __ B(label);
2883 break;
2884 }
2885 } else if (use_imm) {
2886 // TODO: more efficient comparison with constants without loading them into TMP/AT.
2887 switch (cond) {
2888 case kCondEQ:
2889 __ LoadConst32(TMP, imm_high);
2890 __ Xor(TMP, TMP, lhs_high);
2891 __ LoadConst32(AT, imm_low);
2892 __ Xor(AT, AT, lhs_low);
2893 __ Or(TMP, TMP, AT);
2894 __ Beqz(TMP, label);
2895 break;
2896 case kCondNE:
2897 __ LoadConst32(TMP, imm_high);
2898 __ Xor(TMP, TMP, lhs_high);
2899 __ LoadConst32(AT, imm_low);
2900 __ Xor(AT, AT, lhs_low);
2901 __ Or(TMP, TMP, AT);
2902 __ Bnez(TMP, label);
2903 break;
2904 case kCondLT:
2905 __ LoadConst32(TMP, imm_high);
2906 __ Blt(lhs_high, TMP, label);
2907 __ Slt(TMP, TMP, lhs_high);
2908 __ LoadConst32(AT, imm_low);
2909 __ Sltu(AT, lhs_low, AT);
2910 __ Blt(TMP, AT, label);
2911 break;
2912 case kCondGE:
2913 __ LoadConst32(TMP, imm_high);
2914 __ Blt(TMP, lhs_high, label);
2915 __ Slt(TMP, lhs_high, TMP);
2916 __ LoadConst32(AT, imm_low);
2917 __ Sltu(AT, lhs_low, AT);
2918 __ Or(TMP, TMP, AT);
2919 __ Beqz(TMP, label);
2920 break;
2921 case kCondLE:
2922 __ LoadConst32(TMP, imm_high);
2923 __ Blt(lhs_high, TMP, label);
2924 __ Slt(TMP, TMP, lhs_high);
2925 __ LoadConst32(AT, imm_low);
2926 __ Sltu(AT, AT, lhs_low);
2927 __ Or(TMP, TMP, AT);
2928 __ Beqz(TMP, label);
2929 break;
2930 case kCondGT:
2931 __ LoadConst32(TMP, imm_high);
2932 __ Blt(TMP, lhs_high, label);
2933 __ Slt(TMP, lhs_high, TMP);
2934 __ LoadConst32(AT, imm_low);
2935 __ Sltu(AT, AT, lhs_low);
2936 __ Blt(TMP, AT, label);
2937 break;
2938 case kCondB:
2939 __ LoadConst32(TMP, imm_high);
2940 __ Bltu(lhs_high, TMP, label);
2941 __ Sltu(TMP, TMP, lhs_high);
2942 __ LoadConst32(AT, imm_low);
2943 __ Sltu(AT, lhs_low, AT);
2944 __ Blt(TMP, AT, label);
2945 break;
2946 case kCondAE:
2947 __ LoadConst32(TMP, imm_high);
2948 __ Bltu(TMP, lhs_high, label);
2949 __ Sltu(TMP, lhs_high, TMP);
2950 __ LoadConst32(AT, imm_low);
2951 __ Sltu(AT, lhs_low, AT);
2952 __ Or(TMP, TMP, AT);
2953 __ Beqz(TMP, label);
2954 break;
2955 case kCondBE:
2956 __ LoadConst32(TMP, imm_high);
2957 __ Bltu(lhs_high, TMP, label);
2958 __ Sltu(TMP, TMP, lhs_high);
2959 __ LoadConst32(AT, imm_low);
2960 __ Sltu(AT, AT, lhs_low);
2961 __ Or(TMP, TMP, AT);
2962 __ Beqz(TMP, label);
2963 break;
2964 case kCondA:
2965 __ LoadConst32(TMP, imm_high);
2966 __ Bltu(TMP, lhs_high, label);
2967 __ Sltu(TMP, lhs_high, TMP);
2968 __ LoadConst32(AT, imm_low);
2969 __ Sltu(AT, AT, lhs_low);
2970 __ Blt(TMP, AT, label);
2971 break;
2972 }
2973 } else {
2974 switch (cond) {
2975 case kCondEQ:
2976 __ Xor(TMP, lhs_high, rhs_high);
2977 __ Xor(AT, lhs_low, rhs_low);
2978 __ Or(TMP, TMP, AT);
2979 __ Beqz(TMP, label);
2980 break;
2981 case kCondNE:
2982 __ Xor(TMP, lhs_high, rhs_high);
2983 __ Xor(AT, lhs_low, rhs_low);
2984 __ Or(TMP, TMP, AT);
2985 __ Bnez(TMP, label);
2986 break;
2987 case kCondLT:
2988 __ Blt(lhs_high, rhs_high, label);
2989 __ Slt(TMP, rhs_high, lhs_high);
2990 __ Sltu(AT, lhs_low, rhs_low);
2991 __ Blt(TMP, AT, label);
2992 break;
2993 case kCondGE:
2994 __ Blt(rhs_high, lhs_high, label);
2995 __ Slt(TMP, lhs_high, rhs_high);
2996 __ Sltu(AT, lhs_low, rhs_low);
2997 __ Or(TMP, TMP, AT);
2998 __ Beqz(TMP, label);
2999 break;
3000 case kCondLE:
3001 __ Blt(lhs_high, rhs_high, label);
3002 __ Slt(TMP, rhs_high, lhs_high);
3003 __ Sltu(AT, rhs_low, lhs_low);
3004 __ Or(TMP, TMP, AT);
3005 __ Beqz(TMP, label);
3006 break;
3007 case kCondGT:
3008 __ Blt(rhs_high, lhs_high, label);
3009 __ Slt(TMP, lhs_high, rhs_high);
3010 __ Sltu(AT, rhs_low, lhs_low);
3011 __ Blt(TMP, AT, label);
3012 break;
3013 case kCondB:
3014 __ Bltu(lhs_high, rhs_high, label);
3015 __ Sltu(TMP, rhs_high, lhs_high);
3016 __ Sltu(AT, lhs_low, rhs_low);
3017 __ Blt(TMP, AT, label);
3018 break;
3019 case kCondAE:
3020 __ Bltu(rhs_high, lhs_high, label);
3021 __ Sltu(TMP, lhs_high, rhs_high);
3022 __ Sltu(AT, lhs_low, rhs_low);
3023 __ Or(TMP, TMP, AT);
3024 __ Beqz(TMP, label);
3025 break;
3026 case kCondBE:
3027 __ Bltu(lhs_high, rhs_high, label);
3028 __ Sltu(TMP, rhs_high, lhs_high);
3029 __ Sltu(AT, rhs_low, lhs_low);
3030 __ Or(TMP, TMP, AT);
3031 __ Beqz(TMP, label);
3032 break;
3033 case kCondA:
3034 __ Bltu(rhs_high, lhs_high, label);
3035 __ Sltu(TMP, lhs_high, rhs_high);
3036 __ Sltu(AT, rhs_low, lhs_low);
3037 __ Blt(TMP, AT, label);
3038 break;
3039 }
3040 }
3041}
3042
3043void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3044 bool gt_bias,
3045 Primitive::Type type,
3046 LocationSummary* locations,
3047 MipsLabel* label) {
3048 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3049 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3050 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3051 if (type == Primitive::kPrimFloat) {
3052 if (isR6) {
3053 switch (cond) {
3054 case kCondEQ:
3055 __ CmpEqS(FTMP, lhs, rhs);
3056 __ Bc1nez(FTMP, label);
3057 break;
3058 case kCondNE:
3059 __ CmpEqS(FTMP, lhs, rhs);
3060 __ Bc1eqz(FTMP, label);
3061 break;
3062 case kCondLT:
3063 if (gt_bias) {
3064 __ CmpLtS(FTMP, lhs, rhs);
3065 } else {
3066 __ CmpUltS(FTMP, lhs, rhs);
3067 }
3068 __ Bc1nez(FTMP, label);
3069 break;
3070 case kCondLE:
3071 if (gt_bias) {
3072 __ CmpLeS(FTMP, lhs, rhs);
3073 } else {
3074 __ CmpUleS(FTMP, lhs, rhs);
3075 }
3076 __ Bc1nez(FTMP, label);
3077 break;
3078 case kCondGT:
3079 if (gt_bias) {
3080 __ CmpUltS(FTMP, rhs, lhs);
3081 } else {
3082 __ CmpLtS(FTMP, rhs, lhs);
3083 }
3084 __ Bc1nez(FTMP, label);
3085 break;
3086 case kCondGE:
3087 if (gt_bias) {
3088 __ CmpUleS(FTMP, rhs, lhs);
3089 } else {
3090 __ CmpLeS(FTMP, rhs, lhs);
3091 }
3092 __ Bc1nez(FTMP, label);
3093 break;
3094 default:
3095 LOG(FATAL) << "Unexpected non-floating-point condition";
3096 }
3097 } else {
3098 switch (cond) {
3099 case kCondEQ:
3100 __ CeqS(0, lhs, rhs);
3101 __ Bc1t(0, label);
3102 break;
3103 case kCondNE:
3104 __ CeqS(0, lhs, rhs);
3105 __ Bc1f(0, label);
3106 break;
3107 case kCondLT:
3108 if (gt_bias) {
3109 __ ColtS(0, lhs, rhs);
3110 } else {
3111 __ CultS(0, lhs, rhs);
3112 }
3113 __ Bc1t(0, label);
3114 break;
3115 case kCondLE:
3116 if (gt_bias) {
3117 __ ColeS(0, lhs, rhs);
3118 } else {
3119 __ CuleS(0, lhs, rhs);
3120 }
3121 __ Bc1t(0, label);
3122 break;
3123 case kCondGT:
3124 if (gt_bias) {
3125 __ CultS(0, rhs, lhs);
3126 } else {
3127 __ ColtS(0, rhs, lhs);
3128 }
3129 __ Bc1t(0, label);
3130 break;
3131 case kCondGE:
3132 if (gt_bias) {
3133 __ CuleS(0, rhs, lhs);
3134 } else {
3135 __ ColeS(0, rhs, lhs);
3136 }
3137 __ Bc1t(0, label);
3138 break;
3139 default:
3140 LOG(FATAL) << "Unexpected non-floating-point condition";
3141 }
3142 }
3143 } else {
3144 DCHECK_EQ(type, Primitive::kPrimDouble);
3145 if (isR6) {
3146 switch (cond) {
3147 case kCondEQ:
3148 __ CmpEqD(FTMP, lhs, rhs);
3149 __ Bc1nez(FTMP, label);
3150 break;
3151 case kCondNE:
3152 __ CmpEqD(FTMP, lhs, rhs);
3153 __ Bc1eqz(FTMP, label);
3154 break;
3155 case kCondLT:
3156 if (gt_bias) {
3157 __ CmpLtD(FTMP, lhs, rhs);
3158 } else {
3159 __ CmpUltD(FTMP, lhs, rhs);
3160 }
3161 __ Bc1nez(FTMP, label);
3162 break;
3163 case kCondLE:
3164 if (gt_bias) {
3165 __ CmpLeD(FTMP, lhs, rhs);
3166 } else {
3167 __ CmpUleD(FTMP, lhs, rhs);
3168 }
3169 __ Bc1nez(FTMP, label);
3170 break;
3171 case kCondGT:
3172 if (gt_bias) {
3173 __ CmpUltD(FTMP, rhs, lhs);
3174 } else {
3175 __ CmpLtD(FTMP, rhs, lhs);
3176 }
3177 __ Bc1nez(FTMP, label);
3178 break;
3179 case kCondGE:
3180 if (gt_bias) {
3181 __ CmpUleD(FTMP, rhs, lhs);
3182 } else {
3183 __ CmpLeD(FTMP, rhs, lhs);
3184 }
3185 __ Bc1nez(FTMP, label);
3186 break;
3187 default:
3188 LOG(FATAL) << "Unexpected non-floating-point condition";
3189 }
3190 } else {
3191 switch (cond) {
3192 case kCondEQ:
3193 __ CeqD(0, lhs, rhs);
3194 __ Bc1t(0, label);
3195 break;
3196 case kCondNE:
3197 __ CeqD(0, lhs, rhs);
3198 __ Bc1f(0, label);
3199 break;
3200 case kCondLT:
3201 if (gt_bias) {
3202 __ ColtD(0, lhs, rhs);
3203 } else {
3204 __ CultD(0, lhs, rhs);
3205 }
3206 __ Bc1t(0, label);
3207 break;
3208 case kCondLE:
3209 if (gt_bias) {
3210 __ ColeD(0, lhs, rhs);
3211 } else {
3212 __ CuleD(0, lhs, rhs);
3213 }
3214 __ Bc1t(0, label);
3215 break;
3216 case kCondGT:
3217 if (gt_bias) {
3218 __ CultD(0, rhs, lhs);
3219 } else {
3220 __ ColtD(0, rhs, lhs);
3221 }
3222 __ Bc1t(0, label);
3223 break;
3224 case kCondGE:
3225 if (gt_bias) {
3226 __ CuleD(0, rhs, lhs);
3227 } else {
3228 __ ColeD(0, rhs, lhs);
3229 }
3230 __ Bc1t(0, label);
3231 break;
3232 default:
3233 LOG(FATAL) << "Unexpected non-floating-point condition";
3234 }
3235 }
3236 }
3237}
3238
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003239void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003240 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003241 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003242 MipsLabel* false_target) {
3243 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003244
David Brazdil0debae72015-11-12 18:37:00 +00003245 if (true_target == nullptr && false_target == nullptr) {
3246 // Nothing to do. The code always falls through.
3247 return;
3248 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003249 // Constant condition, statically compared against "true" (integer value 1).
3250 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003251 if (true_target != nullptr) {
3252 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003253 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003254 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003255 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003256 if (false_target != nullptr) {
3257 __ B(false_target);
3258 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003259 }
David Brazdil0debae72015-11-12 18:37:00 +00003260 return;
3261 }
3262
3263 // The following code generates these patterns:
3264 // (1) true_target == nullptr && false_target != nullptr
3265 // - opposite condition true => branch to false_target
3266 // (2) true_target != nullptr && false_target == nullptr
3267 // - condition true => branch to true_target
3268 // (3) true_target != nullptr && false_target != nullptr
3269 // - condition true => branch to true_target
3270 // - branch to false_target
3271 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003272 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003273 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003274 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003275 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003276 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3277 } else {
3278 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3279 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003280 } else {
3281 // The condition instruction has not been materialized, use its inputs as
3282 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003283 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003284 Primitive::Type type = condition->InputAt(0)->GetType();
3285 LocationSummary* locations = cond->GetLocations();
3286 IfCondition if_cond = condition->GetCondition();
3287 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003288
David Brazdil0debae72015-11-12 18:37:00 +00003289 if (true_target == nullptr) {
3290 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003291 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003292 }
3293
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003294 switch (type) {
3295 default:
3296 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3297 break;
3298 case Primitive::kPrimLong:
3299 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3300 break;
3301 case Primitive::kPrimFloat:
3302 case Primitive::kPrimDouble:
3303 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3304 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003305 }
3306 }
David Brazdil0debae72015-11-12 18:37:00 +00003307
3308 // If neither branch falls through (case 3), the conditional branch to `true_target`
3309 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3310 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003311 __ B(false_target);
3312 }
3313}
3314
3315void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003317 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003318 locations->SetInAt(0, Location::RequiresRegister());
3319 }
3320}
3321
3322void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003323 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3324 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3325 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3326 nullptr : codegen_->GetLabelOf(true_successor);
3327 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3328 nullptr : codegen_->GetLabelOf(false_successor);
3329 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003330}
3331
3332void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3333 LocationSummary* locations = new (GetGraph()->GetArena())
3334 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003335 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003336 locations->SetInAt(0, Location::RequiresRegister());
3337 }
3338}
3339
3340void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003341 SlowPathCodeMIPS* slow_path =
3342 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003343 GenerateTestAndBranch(deoptimize,
3344 /* condition_input_index */ 0,
3345 slow_path->GetEntryLabel(),
3346 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003347}
3348
David Brazdil74eb1b22015-12-14 11:44:01 +00003349void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3350 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3351 if (Primitive::IsFloatingPointType(select->GetType())) {
3352 locations->SetInAt(0, Location::RequiresFpuRegister());
3353 locations->SetInAt(1, Location::RequiresFpuRegister());
3354 } else {
3355 locations->SetInAt(0, Location::RequiresRegister());
3356 locations->SetInAt(1, Location::RequiresRegister());
3357 }
3358 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3359 locations->SetInAt(2, Location::RequiresRegister());
3360 }
3361 locations->SetOut(Location::SameAsFirstInput());
3362}
3363
3364void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3365 LocationSummary* locations = select->GetLocations();
3366 MipsLabel false_target;
3367 GenerateTestAndBranch(select,
3368 /* condition_input_index */ 2,
3369 /* true_target */ nullptr,
3370 &false_target);
3371 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3372 __ Bind(&false_target);
3373}
3374
David Srbecky0cf44932015-12-09 14:09:59 +00003375void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3376 new (GetGraph()->GetArena()) LocationSummary(info);
3377}
3378
David Srbeckyd28f4a02016-03-14 17:14:24 +00003379void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3380 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003381}
3382
3383void CodeGeneratorMIPS::GenerateNop() {
3384 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003385}
3386
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003387void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3388 Primitive::Type field_type = field_info.GetFieldType();
3389 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3390 bool generate_volatile = field_info.IsVolatile() && is_wide;
3391 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3392 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3393
3394 locations->SetInAt(0, Location::RequiresRegister());
3395 if (generate_volatile) {
3396 InvokeRuntimeCallingConvention calling_convention;
3397 // need A0 to hold base + offset
3398 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3399 if (field_type == Primitive::kPrimLong) {
3400 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3401 } else {
3402 locations->SetOut(Location::RequiresFpuRegister());
3403 // Need some temp core regs since FP results are returned in core registers
3404 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3405 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3406 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3407 }
3408 } else {
3409 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3410 locations->SetOut(Location::RequiresFpuRegister());
3411 } else {
3412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3413 }
3414 }
3415}
3416
3417void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3418 const FieldInfo& field_info,
3419 uint32_t dex_pc) {
3420 Primitive::Type type = field_info.GetFieldType();
3421 LocationSummary* locations = instruction->GetLocations();
3422 Register obj = locations->InAt(0).AsRegister<Register>();
3423 LoadOperandType load_type = kLoadUnsignedByte;
3424 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003425 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003426
3427 switch (type) {
3428 case Primitive::kPrimBoolean:
3429 load_type = kLoadUnsignedByte;
3430 break;
3431 case Primitive::kPrimByte:
3432 load_type = kLoadSignedByte;
3433 break;
3434 case Primitive::kPrimShort:
3435 load_type = kLoadSignedHalfword;
3436 break;
3437 case Primitive::kPrimChar:
3438 load_type = kLoadUnsignedHalfword;
3439 break;
3440 case Primitive::kPrimInt:
3441 case Primitive::kPrimFloat:
3442 case Primitive::kPrimNot:
3443 load_type = kLoadWord;
3444 break;
3445 case Primitive::kPrimLong:
3446 case Primitive::kPrimDouble:
3447 load_type = kLoadDoubleword;
3448 break;
3449 case Primitive::kPrimVoid:
3450 LOG(FATAL) << "Unreachable type " << type;
3451 UNREACHABLE();
3452 }
3453
3454 if (is_volatile && load_type == kLoadDoubleword) {
3455 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003456 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003457 // Do implicit Null check
3458 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3459 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3460 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3461 instruction,
3462 dex_pc,
3463 nullptr,
3464 IsDirectEntrypoint(kQuickA64Load));
3465 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3466 if (type == Primitive::kPrimDouble) {
3467 // Need to move to FP regs since FP results are returned in core registers.
3468 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
3469 locations->Out().AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003470 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3471 locations->Out().AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003472 }
3473 } else {
3474 if (!Primitive::IsFloatingPointType(type)) {
3475 Register dst;
3476 if (type == Primitive::kPrimLong) {
3477 DCHECK(locations->Out().IsRegisterPair());
3478 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003479 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3480 if (obj == dst) {
3481 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3482 codegen_->MaybeRecordImplicitNullCheck(instruction);
3483 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3484 } else {
3485 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3486 codegen_->MaybeRecordImplicitNullCheck(instruction);
3487 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3488 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003489 } else {
3490 DCHECK(locations->Out().IsRegister());
3491 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003492 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003493 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003494 } else {
3495 DCHECK(locations->Out().IsFpuRegister());
3496 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3497 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003498 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003499 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003500 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003501 }
3502 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003503 // Longs are handled earlier.
3504 if (type != Primitive::kPrimLong) {
3505 codegen_->MaybeRecordImplicitNullCheck(instruction);
3506 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003507 }
3508
3509 if (is_volatile) {
3510 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3511 }
3512}
3513
3514void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3515 Primitive::Type field_type = field_info.GetFieldType();
3516 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3517 bool generate_volatile = field_info.IsVolatile() && is_wide;
3518 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3519 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3520
3521 locations->SetInAt(0, Location::RequiresRegister());
3522 if (generate_volatile) {
3523 InvokeRuntimeCallingConvention calling_convention;
3524 // need A0 to hold base + offset
3525 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3526 if (field_type == Primitive::kPrimLong) {
3527 locations->SetInAt(1, Location::RegisterPairLocation(
3528 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3529 } else {
3530 locations->SetInAt(1, Location::RequiresFpuRegister());
3531 // Pass FP parameters in core registers.
3532 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3533 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3534 }
3535 } else {
3536 if (Primitive::IsFloatingPointType(field_type)) {
3537 locations->SetInAt(1, Location::RequiresFpuRegister());
3538 } else {
3539 locations->SetInAt(1, Location::RequiresRegister());
3540 }
3541 }
3542}
3543
3544void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3545 const FieldInfo& field_info,
3546 uint32_t dex_pc) {
3547 Primitive::Type type = field_info.GetFieldType();
3548 LocationSummary* locations = instruction->GetLocations();
3549 Register obj = locations->InAt(0).AsRegister<Register>();
3550 StoreOperandType store_type = kStoreByte;
3551 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003552 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003553
3554 switch (type) {
3555 case Primitive::kPrimBoolean:
3556 case Primitive::kPrimByte:
3557 store_type = kStoreByte;
3558 break;
3559 case Primitive::kPrimShort:
3560 case Primitive::kPrimChar:
3561 store_type = kStoreHalfword;
3562 break;
3563 case Primitive::kPrimInt:
3564 case Primitive::kPrimFloat:
3565 case Primitive::kPrimNot:
3566 store_type = kStoreWord;
3567 break;
3568 case Primitive::kPrimLong:
3569 case Primitive::kPrimDouble:
3570 store_type = kStoreDoubleword;
3571 break;
3572 case Primitive::kPrimVoid:
3573 LOG(FATAL) << "Unreachable type " << type;
3574 UNREACHABLE();
3575 }
3576
3577 if (is_volatile) {
3578 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3579 }
3580
3581 if (is_volatile && store_type == kStoreDoubleword) {
3582 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003583 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003584 // Do implicit Null check.
3585 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3586 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3587 if (type == Primitive::kPrimDouble) {
3588 // Pass FP parameters in core registers.
3589 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
3590 locations->InAt(1).AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003591 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3592 locations->InAt(1).AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003593 }
3594 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3595 instruction,
3596 dex_pc,
3597 nullptr,
3598 IsDirectEntrypoint(kQuickA64Store));
3599 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3600 } else {
3601 if (!Primitive::IsFloatingPointType(type)) {
3602 Register src;
3603 if (type == Primitive::kPrimLong) {
3604 DCHECK(locations->InAt(1).IsRegisterPair());
3605 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003606 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3607 __ StoreToOffset(kStoreWord, src, obj, offset);
3608 codegen_->MaybeRecordImplicitNullCheck(instruction);
3609 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003610 } else {
3611 DCHECK(locations->InAt(1).IsRegister());
3612 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003613 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003614 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003615 } else {
3616 DCHECK(locations->InAt(1).IsFpuRegister());
3617 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3618 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003619 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003620 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003621 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003622 }
3623 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003624 // Longs are handled earlier.
3625 if (type != Primitive::kPrimLong) {
3626 codegen_->MaybeRecordImplicitNullCheck(instruction);
3627 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003628 }
3629
3630 // TODO: memory barriers?
3631 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3632 DCHECK(locations->InAt(1).IsRegister());
3633 Register src = locations->InAt(1).AsRegister<Register>();
3634 codegen_->MarkGCCard(obj, src);
3635 }
3636
3637 if (is_volatile) {
3638 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3639 }
3640}
3641
3642void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3643 HandleFieldGet(instruction, instruction->GetFieldInfo());
3644}
3645
3646void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3647 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3648}
3649
3650void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3651 HandleFieldSet(instruction, instruction->GetFieldInfo());
3652}
3653
3654void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3655 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3656}
3657
3658void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3659 LocationSummary::CallKind call_kind =
3660 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3661 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3662 locations->SetInAt(0, Location::RequiresRegister());
3663 locations->SetInAt(1, Location::RequiresRegister());
3664 // The output does overlap inputs.
3665 // Note that TypeCheckSlowPathMIPS uses this register too.
3666 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3667}
3668
3669void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3670 LocationSummary* locations = instruction->GetLocations();
3671 Register obj = locations->InAt(0).AsRegister<Register>();
3672 Register cls = locations->InAt(1).AsRegister<Register>();
3673 Register out = locations->Out().AsRegister<Register>();
3674
3675 MipsLabel done;
3676
3677 // Return 0 if `obj` is null.
3678 // TODO: Avoid this check if we know `obj` is not null.
3679 __ Move(out, ZERO);
3680 __ Beqz(obj, &done);
3681
3682 // Compare the class of `obj` with `cls`.
3683 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3684 if (instruction->IsExactCheck()) {
3685 // Classes must be equal for the instanceof to succeed.
3686 __ Xor(out, out, cls);
3687 __ Sltiu(out, out, 1);
3688 } else {
3689 // If the classes are not equal, we go into a slow path.
3690 DCHECK(locations->OnlyCallsOnSlowPath());
3691 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3692 codegen_->AddSlowPath(slow_path);
3693 __ Bne(out, cls, slow_path->GetEntryLabel());
3694 __ LoadConst32(out, 1);
3695 __ Bind(slow_path->GetExitLabel());
3696 }
3697
3698 __ Bind(&done);
3699}
3700
3701void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3702 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3703 locations->SetOut(Location::ConstantLocation(constant));
3704}
3705
3706void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3707 // Will be generated at use site.
3708}
3709
3710void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3711 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3712 locations->SetOut(Location::ConstantLocation(constant));
3713}
3714
3715void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3716 // Will be generated at use site.
3717}
3718
3719void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3720 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3721 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3722}
3723
3724void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3725 HandleInvoke(invoke);
3726 // The register T0 is required to be used for the hidden argument in
3727 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3728 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3729}
3730
3731void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3732 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3733 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3734 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3735 invoke->GetImtIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
3736 Location receiver = invoke->GetLocations()->InAt(0);
3737 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3738 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3739
3740 // Set the hidden argument.
3741 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3742 invoke->GetDexMethodIndex());
3743
3744 // temp = object->GetClass();
3745 if (receiver.IsStackSlot()) {
3746 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3747 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3748 } else {
3749 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3750 }
3751 codegen_->MaybeRecordImplicitNullCheck(invoke);
3752 // temp = temp->GetImtEntryAt(method_offset);
3753 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3754 // T9 = temp->GetEntryPoint();
3755 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3756 // T9();
3757 __ Jalr(T9);
3758 __ Nop();
3759 DCHECK(!codegen_->IsLeafMethod());
3760 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3761}
3762
3763void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003764 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3765 if (intrinsic.TryDispatch(invoke)) {
3766 return;
3767 }
3768
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003769 HandleInvoke(invoke);
3770}
3771
3772void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003773 // Explicit clinit checks triggered by static invokes must have been pruned by
3774 // art::PrepareForRegisterAllocation.
3775 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003776
Chris Larsen701566a2015-10-27 15:29:13 -07003777 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3778 if (intrinsic.TryDispatch(invoke)) {
3779 return;
3780 }
3781
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003782 HandleInvoke(invoke);
3783}
3784
Chris Larsen701566a2015-10-27 15:29:13 -07003785static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003786 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003787 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3788 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003789 return true;
3790 }
3791 return false;
3792}
3793
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003794HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
3795 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
3796 // TODO: Implement other kinds.
3797 return HLoadString::LoadKind::kDexCacheViaMethod;
3798}
3799
Vladimir Markodc151b22015-10-15 18:02:30 +01003800HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3801 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3802 MethodReference target_method ATTRIBUTE_UNUSED) {
3803 switch (desired_dispatch_info.method_load_kind) {
3804 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3805 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3806 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3807 return HInvokeStaticOrDirect::DispatchInfo {
3808 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3809 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3810 0u,
3811 0u
3812 };
3813 default:
3814 break;
3815 }
3816 switch (desired_dispatch_info.code_ptr_location) {
3817 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3818 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3819 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3820 return HInvokeStaticOrDirect::DispatchInfo {
3821 desired_dispatch_info.method_load_kind,
3822 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3823 desired_dispatch_info.method_load_data,
3824 0u
3825 };
3826 default:
3827 return desired_dispatch_info;
3828 }
3829}
3830
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003831void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3832 // All registers are assumed to be correctly set up per the calling convention.
3833
3834 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3835 switch (invoke->GetMethodLoadKind()) {
3836 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3837 // temp = thread->string_init_entrypoint
3838 __ LoadFromOffset(kLoadWord,
3839 temp.AsRegister<Register>(),
3840 TR,
3841 invoke->GetStringInitOffset());
3842 break;
3843 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003844 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003845 break;
3846 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3847 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3848 break;
3849 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003850 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003851 // TODO: Implement these types.
3852 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3853 LOG(FATAL) << "Unsupported";
3854 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003855 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003856 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003857 Register reg = temp.AsRegister<Register>();
3858 Register method_reg;
3859 if (current_method.IsRegister()) {
3860 method_reg = current_method.AsRegister<Register>();
3861 } else {
3862 // TODO: use the appropriate DCHECK() here if possible.
3863 // DCHECK(invoke->GetLocations()->Intrinsified());
3864 DCHECK(!current_method.IsValid());
3865 method_reg = reg;
3866 __ Lw(reg, SP, kCurrentMethodStackOffset);
3867 }
3868
3869 // temp = temp->dex_cache_resolved_methods_;
3870 __ LoadFromOffset(kLoadWord,
3871 reg,
3872 method_reg,
3873 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003874 // temp = temp[index_in_cache];
3875 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3876 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003877 __ LoadFromOffset(kLoadWord,
3878 reg,
3879 reg,
3880 CodeGenerator::GetCachePointerOffset(index_in_cache));
3881 break;
3882 }
3883 }
3884
3885 switch (invoke->GetCodePtrLocation()) {
3886 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3887 __ Jalr(&frame_entry_label_, T9);
3888 break;
3889 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3890 // LR = invoke->GetDirectCodePtr();
3891 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3892 // LR()
3893 __ Jalr(T9);
3894 __ Nop();
3895 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003896 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003897 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3898 // TODO: Implement these types.
3899 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3900 LOG(FATAL) << "Unsupported";
3901 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003902 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3903 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01003904 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003905 T9,
3906 callee_method.AsRegister<Register>(),
3907 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3908 kMipsWordSize).Int32Value());
3909 // T9()
3910 __ Jalr(T9);
3911 __ Nop();
3912 break;
3913 }
3914 DCHECK(!IsLeafMethod());
3915}
3916
3917void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003918 // Explicit clinit checks triggered by static invokes must have been pruned by
3919 // art::PrepareForRegisterAllocation.
3920 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003921
3922 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3923 return;
3924 }
3925
3926 LocationSummary* locations = invoke->GetLocations();
3927 codegen_->GenerateStaticOrDirectCall(invoke,
3928 locations->HasTemps()
3929 ? locations->GetTemp(0)
3930 : Location::NoLocation());
3931 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3932}
3933
Chris Larsen3acee732015-11-18 13:31:08 -08003934void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003935 LocationSummary* locations = invoke->GetLocations();
3936 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08003937 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003938 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3939 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
3940 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3941 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3942
3943 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08003944 DCHECK(receiver.IsRegister());
3945 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3946 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003947 // temp = temp->GetMethodAt(method_offset);
3948 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3949 // T9 = temp->GetEntryPoint();
3950 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3951 // T9();
3952 __ Jalr(T9);
3953 __ Nop();
Chris Larsen3acee732015-11-18 13:31:08 -08003954}
3955
3956void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3957 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3958 return;
3959 }
3960
3961 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003962 DCHECK(!codegen_->IsLeafMethod());
3963 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3964}
3965
3966void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01003967 InvokeRuntimeCallingConvention calling_convention;
3968 CodeGenerator::CreateLoadClassLocationSummary(
3969 cls,
3970 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
3971 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003972}
3973
3974void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
3975 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01003976 if (cls->NeedsAccessCheck()) {
3977 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3978 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3979 cls,
3980 cls->GetDexPc(),
3981 nullptr,
3982 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00003983 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01003984 return;
3985 }
3986
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003987 Register out = locations->Out().AsRegister<Register>();
3988 Register current_method = locations->InAt(0).AsRegister<Register>();
3989 if (cls->IsReferrersClass()) {
3990 DCHECK(!cls->CanCallRuntime());
3991 DCHECK(!cls->MustGenerateClinitCheck());
3992 __ LoadFromOffset(kLoadWord, out, current_method,
3993 ArtMethod::DeclaringClassOffset().Int32Value());
3994 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003995 __ LoadFromOffset(kLoadWord, out, current_method,
3996 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
3997 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003998
3999 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
4000 DCHECK(cls->CanCallRuntime());
4001 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4002 cls,
4003 cls,
4004 cls->GetDexPc(),
4005 cls->MustGenerateClinitCheck());
4006 codegen_->AddSlowPath(slow_path);
4007 if (!cls->IsInDexCache()) {
4008 __ Beqz(out, slow_path->GetEntryLabel());
4009 }
4010 if (cls->MustGenerateClinitCheck()) {
4011 GenerateClassInitializationCheck(slow_path, out);
4012 } else {
4013 __ Bind(slow_path->GetExitLabel());
4014 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004015 }
4016 }
4017}
4018
4019static int32_t GetExceptionTlsOffset() {
4020 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
4021}
4022
4023void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4024 LocationSummary* locations =
4025 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4026 locations->SetOut(Location::RequiresRegister());
4027}
4028
4029void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4030 Register out = load->GetLocations()->Out().AsRegister<Register>();
4031 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4032}
4033
4034void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4035 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4036}
4037
4038void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4039 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4040}
4041
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004042void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004043 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
4044 ? LocationSummary::kCallOnSlowPath
4045 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004046 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004047 locations->SetInAt(0, Location::RequiresRegister());
4048 locations->SetOut(Location::RequiresRegister());
4049}
4050
4051void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004052 LocationSummary* locations = load->GetLocations();
4053 Register out = locations->Out().AsRegister<Register>();
4054 Register current_method = locations->InAt(0).AsRegister<Register>();
4055 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4056 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4057 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004058
4059 if (!load->IsInDexCache()) {
4060 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4061 codegen_->AddSlowPath(slow_path);
4062 __ Beqz(out, slow_path->GetEntryLabel());
4063 __ Bind(slow_path->GetExitLabel());
4064 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004065}
4066
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004067void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4068 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4069 locations->SetOut(Location::ConstantLocation(constant));
4070}
4071
4072void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4073 // Will be generated at use site.
4074}
4075
4076void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4077 LocationSummary* locations =
4078 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4079 InvokeRuntimeCallingConvention calling_convention;
4080 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4081}
4082
4083void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4084 if (instruction->IsEnter()) {
4085 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4086 instruction,
4087 instruction->GetDexPc(),
4088 nullptr,
4089 IsDirectEntrypoint(kQuickLockObject));
4090 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4091 } else {
4092 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4093 instruction,
4094 instruction->GetDexPc(),
4095 nullptr,
4096 IsDirectEntrypoint(kQuickUnlockObject));
4097 }
4098 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4099}
4100
4101void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4102 LocationSummary* locations =
4103 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4104 switch (mul->GetResultType()) {
4105 case Primitive::kPrimInt:
4106 case Primitive::kPrimLong:
4107 locations->SetInAt(0, Location::RequiresRegister());
4108 locations->SetInAt(1, Location::RequiresRegister());
4109 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4110 break;
4111
4112 case Primitive::kPrimFloat:
4113 case Primitive::kPrimDouble:
4114 locations->SetInAt(0, Location::RequiresFpuRegister());
4115 locations->SetInAt(1, Location::RequiresFpuRegister());
4116 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4117 break;
4118
4119 default:
4120 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4121 }
4122}
4123
4124void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4125 Primitive::Type type = instruction->GetType();
4126 LocationSummary* locations = instruction->GetLocations();
4127 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4128
4129 switch (type) {
4130 case Primitive::kPrimInt: {
4131 Register dst = locations->Out().AsRegister<Register>();
4132 Register lhs = locations->InAt(0).AsRegister<Register>();
4133 Register rhs = locations->InAt(1).AsRegister<Register>();
4134
4135 if (isR6) {
4136 __ MulR6(dst, lhs, rhs);
4137 } else {
4138 __ MulR2(dst, lhs, rhs);
4139 }
4140 break;
4141 }
4142 case Primitive::kPrimLong: {
4143 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4144 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4145 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4146 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4147 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4148 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4149
4150 // Extra checks to protect caused by the existance of A1_A2.
4151 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4152 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4153 DCHECK_NE(dst_high, lhs_low);
4154 DCHECK_NE(dst_high, rhs_low);
4155
4156 // A_B * C_D
4157 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4158 // dst_lo: [ low(B*D) ]
4159 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4160
4161 if (isR6) {
4162 __ MulR6(TMP, lhs_high, rhs_low);
4163 __ MulR6(dst_high, lhs_low, rhs_high);
4164 __ Addu(dst_high, dst_high, TMP);
4165 __ MuhuR6(TMP, lhs_low, rhs_low);
4166 __ Addu(dst_high, dst_high, TMP);
4167 __ MulR6(dst_low, lhs_low, rhs_low);
4168 } else {
4169 __ MulR2(TMP, lhs_high, rhs_low);
4170 __ MulR2(dst_high, lhs_low, rhs_high);
4171 __ Addu(dst_high, dst_high, TMP);
4172 __ MultuR2(lhs_low, rhs_low);
4173 __ Mfhi(TMP);
4174 __ Addu(dst_high, dst_high, TMP);
4175 __ Mflo(dst_low);
4176 }
4177 break;
4178 }
4179 case Primitive::kPrimFloat:
4180 case Primitive::kPrimDouble: {
4181 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4182 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4183 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4184 if (type == Primitive::kPrimFloat) {
4185 __ MulS(dst, lhs, rhs);
4186 } else {
4187 __ MulD(dst, lhs, rhs);
4188 }
4189 break;
4190 }
4191 default:
4192 LOG(FATAL) << "Unexpected mul type " << type;
4193 }
4194}
4195
4196void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4197 LocationSummary* locations =
4198 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4199 switch (neg->GetResultType()) {
4200 case Primitive::kPrimInt:
4201 case Primitive::kPrimLong:
4202 locations->SetInAt(0, Location::RequiresRegister());
4203 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4204 break;
4205
4206 case Primitive::kPrimFloat:
4207 case Primitive::kPrimDouble:
4208 locations->SetInAt(0, Location::RequiresFpuRegister());
4209 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4210 break;
4211
4212 default:
4213 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4214 }
4215}
4216
4217void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4218 Primitive::Type type = instruction->GetType();
4219 LocationSummary* locations = instruction->GetLocations();
4220
4221 switch (type) {
4222 case Primitive::kPrimInt: {
4223 Register dst = locations->Out().AsRegister<Register>();
4224 Register src = locations->InAt(0).AsRegister<Register>();
4225 __ Subu(dst, ZERO, src);
4226 break;
4227 }
4228 case Primitive::kPrimLong: {
4229 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4230 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4231 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4232 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4233 __ Subu(dst_low, ZERO, src_low);
4234 __ Sltu(TMP, ZERO, dst_low);
4235 __ Subu(dst_high, ZERO, src_high);
4236 __ Subu(dst_high, dst_high, TMP);
4237 break;
4238 }
4239 case Primitive::kPrimFloat:
4240 case Primitive::kPrimDouble: {
4241 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4242 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4243 if (type == Primitive::kPrimFloat) {
4244 __ NegS(dst, src);
4245 } else {
4246 __ NegD(dst, src);
4247 }
4248 break;
4249 }
4250 default:
4251 LOG(FATAL) << "Unexpected neg type " << type;
4252 }
4253}
4254
4255void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4256 LocationSummary* locations =
4257 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4258 InvokeRuntimeCallingConvention calling_convention;
4259 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4260 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4261 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4262 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4263}
4264
4265void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4266 InvokeRuntimeCallingConvention calling_convention;
4267 Register current_method_register = calling_convention.GetRegisterAt(2);
4268 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4269 // Move an uint16_t value to a register.
4270 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4271 codegen_->InvokeRuntime(
4272 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4273 instruction,
4274 instruction->GetDexPc(),
4275 nullptr,
4276 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4277 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4278 void*, uint32_t, int32_t, ArtMethod*>();
4279}
4280
4281void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4282 LocationSummary* locations =
4283 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4284 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004285 if (instruction->IsStringAlloc()) {
4286 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4287 } else {
4288 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4289 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4290 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004291 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4292}
4293
4294void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004295 if (instruction->IsStringAlloc()) {
4296 // String is allocated through StringFactory. Call NewEmptyString entry point.
4297 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4298 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4299 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4300 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4301 __ Jalr(T9);
4302 __ Nop();
4303 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4304 } else {
4305 codegen_->InvokeRuntime(
4306 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4307 instruction,
4308 instruction->GetDexPc(),
4309 nullptr,
4310 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4311 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4312 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004313}
4314
4315void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4317 locations->SetInAt(0, Location::RequiresRegister());
4318 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4319}
4320
4321void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4322 Primitive::Type type = instruction->GetType();
4323 LocationSummary* locations = instruction->GetLocations();
4324
4325 switch (type) {
4326 case Primitive::kPrimInt: {
4327 Register dst = locations->Out().AsRegister<Register>();
4328 Register src = locations->InAt(0).AsRegister<Register>();
4329 __ Nor(dst, src, ZERO);
4330 break;
4331 }
4332
4333 case Primitive::kPrimLong: {
4334 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4335 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4336 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4337 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4338 __ Nor(dst_high, src_high, ZERO);
4339 __ Nor(dst_low, src_low, ZERO);
4340 break;
4341 }
4342
4343 default:
4344 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4345 }
4346}
4347
4348void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4349 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4350 locations->SetInAt(0, Location::RequiresRegister());
4351 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4352}
4353
4354void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4355 LocationSummary* locations = instruction->GetLocations();
4356 __ Xori(locations->Out().AsRegister<Register>(),
4357 locations->InAt(0).AsRegister<Register>(),
4358 1);
4359}
4360
4361void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4362 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4363 ? LocationSummary::kCallOnSlowPath
4364 : LocationSummary::kNoCall;
4365 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4366 locations->SetInAt(0, Location::RequiresRegister());
4367 if (instruction->HasUses()) {
4368 locations->SetOut(Location::SameAsFirstInput());
4369 }
4370}
4371
Calin Juravle2ae48182016-03-16 14:05:09 +00004372void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4373 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004374 return;
4375 }
4376 Location obj = instruction->GetLocations()->InAt(0);
4377
4378 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004379 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004380}
4381
Calin Juravle2ae48182016-03-16 14:05:09 +00004382void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004383 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004384 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004385
4386 Location obj = instruction->GetLocations()->InAt(0);
4387
4388 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4389}
4390
4391void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004392 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004393}
4394
4395void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4396 HandleBinaryOp(instruction);
4397}
4398
4399void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4400 HandleBinaryOp(instruction);
4401}
4402
4403void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4404 LOG(FATAL) << "Unreachable";
4405}
4406
4407void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4408 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4409}
4410
4411void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4412 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4413 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4414 if (location.IsStackSlot()) {
4415 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4416 } else if (location.IsDoubleStackSlot()) {
4417 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4418 }
4419 locations->SetOut(location);
4420}
4421
4422void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4423 ATTRIBUTE_UNUSED) {
4424 // Nothing to do, the parameter is already at its location.
4425}
4426
4427void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4428 LocationSummary* locations =
4429 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4430 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4431}
4432
4433void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4434 ATTRIBUTE_UNUSED) {
4435 // Nothing to do, the method is already at its location.
4436}
4437
4438void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4439 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4440 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4441 locations->SetInAt(i, Location::Any());
4442 }
4443 locations->SetOut(Location::Any());
4444}
4445
4446void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4447 LOG(FATAL) << "Unreachable";
4448}
4449
4450void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4451 Primitive::Type type = rem->GetResultType();
4452 LocationSummary::CallKind call_kind =
4453 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCall;
4454 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4455
4456 switch (type) {
4457 case Primitive::kPrimInt:
4458 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004459 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4461 break;
4462
4463 case Primitive::kPrimLong: {
4464 InvokeRuntimeCallingConvention calling_convention;
4465 locations->SetInAt(0, Location::RegisterPairLocation(
4466 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4467 locations->SetInAt(1, Location::RegisterPairLocation(
4468 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4469 locations->SetOut(calling_convention.GetReturnLocation(type));
4470 break;
4471 }
4472
4473 case Primitive::kPrimFloat:
4474 case Primitive::kPrimDouble: {
4475 InvokeRuntimeCallingConvention calling_convention;
4476 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4477 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4478 locations->SetOut(calling_convention.GetReturnLocation(type));
4479 break;
4480 }
4481
4482 default:
4483 LOG(FATAL) << "Unexpected rem type " << type;
4484 }
4485}
4486
4487void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4488 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004489
4490 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004491 case Primitive::kPrimInt:
4492 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004493 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004494 case Primitive::kPrimLong: {
4495 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
4496 instruction,
4497 instruction->GetDexPc(),
4498 nullptr,
4499 IsDirectEntrypoint(kQuickLmod));
4500 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4501 break;
4502 }
4503 case Primitive::kPrimFloat: {
4504 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
4505 instruction, instruction->GetDexPc(),
4506 nullptr,
4507 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00004508 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004509 break;
4510 }
4511 case Primitive::kPrimDouble: {
4512 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
4513 instruction, instruction->GetDexPc(),
4514 nullptr,
4515 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00004516 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004517 break;
4518 }
4519 default:
4520 LOG(FATAL) << "Unexpected rem type " << type;
4521 }
4522}
4523
4524void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4525 memory_barrier->SetLocations(nullptr);
4526}
4527
4528void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4529 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4530}
4531
4532void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
4533 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4534 Primitive::Type return_type = ret->InputAt(0)->GetType();
4535 locations->SetInAt(0, MipsReturnLocation(return_type));
4536}
4537
4538void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4539 codegen_->GenerateFrameExit();
4540}
4541
4542void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
4543 ret->SetLocations(nullptr);
4544}
4545
4546void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4547 codegen_->GenerateFrameExit();
4548}
4549
Alexey Frunze92d90602015-12-18 18:16:36 -08004550void LocationsBuilderMIPS::VisitRor(HRor* ror) {
4551 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004552}
4553
Alexey Frunze92d90602015-12-18 18:16:36 -08004554void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
4555 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004556}
4557
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004558void LocationsBuilderMIPS::VisitShl(HShl* shl) {
4559 HandleShift(shl);
4560}
4561
4562void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
4563 HandleShift(shl);
4564}
4565
4566void LocationsBuilderMIPS::VisitShr(HShr* shr) {
4567 HandleShift(shr);
4568}
4569
4570void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
4571 HandleShift(shr);
4572}
4573
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004574void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
4575 HandleBinaryOp(instruction);
4576}
4577
4578void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
4579 HandleBinaryOp(instruction);
4580}
4581
4582void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4583 HandleFieldGet(instruction, instruction->GetFieldInfo());
4584}
4585
4586void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4587 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4588}
4589
4590void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4591 HandleFieldSet(instruction, instruction->GetFieldInfo());
4592}
4593
4594void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4595 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4596}
4597
4598void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
4599 HUnresolvedInstanceFieldGet* instruction) {
4600 FieldAccessCallingConventionMIPS calling_convention;
4601 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4602 instruction->GetFieldType(),
4603 calling_convention);
4604}
4605
4606void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
4607 HUnresolvedInstanceFieldGet* instruction) {
4608 FieldAccessCallingConventionMIPS calling_convention;
4609 codegen_->GenerateUnresolvedFieldAccess(instruction,
4610 instruction->GetFieldType(),
4611 instruction->GetFieldIndex(),
4612 instruction->GetDexPc(),
4613 calling_convention);
4614}
4615
4616void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4617 HUnresolvedInstanceFieldSet* instruction) {
4618 FieldAccessCallingConventionMIPS calling_convention;
4619 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4620 instruction->GetFieldType(),
4621 calling_convention);
4622}
4623
4624void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4625 HUnresolvedInstanceFieldSet* instruction) {
4626 FieldAccessCallingConventionMIPS calling_convention;
4627 codegen_->GenerateUnresolvedFieldAccess(instruction,
4628 instruction->GetFieldType(),
4629 instruction->GetFieldIndex(),
4630 instruction->GetDexPc(),
4631 calling_convention);
4632}
4633
4634void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4635 HUnresolvedStaticFieldGet* instruction) {
4636 FieldAccessCallingConventionMIPS calling_convention;
4637 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4638 instruction->GetFieldType(),
4639 calling_convention);
4640}
4641
4642void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4643 HUnresolvedStaticFieldGet* instruction) {
4644 FieldAccessCallingConventionMIPS calling_convention;
4645 codegen_->GenerateUnresolvedFieldAccess(instruction,
4646 instruction->GetFieldType(),
4647 instruction->GetFieldIndex(),
4648 instruction->GetDexPc(),
4649 calling_convention);
4650}
4651
4652void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4653 HUnresolvedStaticFieldSet* instruction) {
4654 FieldAccessCallingConventionMIPS calling_convention;
4655 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4656 instruction->GetFieldType(),
4657 calling_convention);
4658}
4659
4660void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4661 HUnresolvedStaticFieldSet* instruction) {
4662 FieldAccessCallingConventionMIPS calling_convention;
4663 codegen_->GenerateUnresolvedFieldAccess(instruction,
4664 instruction->GetFieldType(),
4665 instruction->GetFieldIndex(),
4666 instruction->GetDexPc(),
4667 calling_convention);
4668}
4669
4670void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4671 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4672}
4673
4674void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4675 HBasicBlock* block = instruction->GetBlock();
4676 if (block->GetLoopInformation() != nullptr) {
4677 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4678 // The back edge will generate the suspend check.
4679 return;
4680 }
4681 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4682 // The goto will generate the suspend check.
4683 return;
4684 }
4685 GenerateSuspendCheck(instruction, nullptr);
4686}
4687
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004688void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4689 LocationSummary* locations =
4690 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4691 InvokeRuntimeCallingConvention calling_convention;
4692 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4693}
4694
4695void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4696 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4697 instruction,
4698 instruction->GetDexPc(),
4699 nullptr,
4700 IsDirectEntrypoint(kQuickDeliverException));
4701 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4702}
4703
4704void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4705 Primitive::Type input_type = conversion->GetInputType();
4706 Primitive::Type result_type = conversion->GetResultType();
4707 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004708 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004709
4710 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4711 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4712 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4713 }
4714
4715 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004716 if (!isR6 &&
4717 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4718 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004719 call_kind = LocationSummary::kCall;
4720 }
4721
4722 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4723
4724 if (call_kind == LocationSummary::kNoCall) {
4725 if (Primitive::IsFloatingPointType(input_type)) {
4726 locations->SetInAt(0, Location::RequiresFpuRegister());
4727 } else {
4728 locations->SetInAt(0, Location::RequiresRegister());
4729 }
4730
4731 if (Primitive::IsFloatingPointType(result_type)) {
4732 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4733 } else {
4734 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4735 }
4736 } else {
4737 InvokeRuntimeCallingConvention calling_convention;
4738
4739 if (Primitive::IsFloatingPointType(input_type)) {
4740 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4741 } else {
4742 DCHECK_EQ(input_type, Primitive::kPrimLong);
4743 locations->SetInAt(0, Location::RegisterPairLocation(
4744 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4745 }
4746
4747 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4748 }
4749}
4750
4751void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4752 LocationSummary* locations = conversion->GetLocations();
4753 Primitive::Type result_type = conversion->GetResultType();
4754 Primitive::Type input_type = conversion->GetInputType();
4755 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004756 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4757 bool fpu_32bit = codegen_->GetInstructionSetFeatures().Is32BitFloatingPoint();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004758
4759 DCHECK_NE(input_type, result_type);
4760
4761 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4762 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4763 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4764 Register src = locations->InAt(0).AsRegister<Register>();
4765
4766 __ Move(dst_low, src);
4767 __ Sra(dst_high, src, 31);
4768 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4769 Register dst = locations->Out().AsRegister<Register>();
4770 Register src = (input_type == Primitive::kPrimLong)
4771 ? locations->InAt(0).AsRegisterPairLow<Register>()
4772 : locations->InAt(0).AsRegister<Register>();
4773
4774 switch (result_type) {
4775 case Primitive::kPrimChar:
4776 __ Andi(dst, src, 0xFFFF);
4777 break;
4778 case Primitive::kPrimByte:
4779 if (has_sign_extension) {
4780 __ Seb(dst, src);
4781 } else {
4782 __ Sll(dst, src, 24);
4783 __ Sra(dst, dst, 24);
4784 }
4785 break;
4786 case Primitive::kPrimShort:
4787 if (has_sign_extension) {
4788 __ Seh(dst, src);
4789 } else {
4790 __ Sll(dst, src, 16);
4791 __ Sra(dst, dst, 16);
4792 }
4793 break;
4794 case Primitive::kPrimInt:
4795 __ Move(dst, src);
4796 break;
4797
4798 default:
4799 LOG(FATAL) << "Unexpected type conversion from " << input_type
4800 << " to " << result_type;
4801 }
4802 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004803 if (input_type == Primitive::kPrimLong) {
4804 if (isR6) {
4805 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4806 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4807 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4808 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4809 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4810 __ Mtc1(src_low, FTMP);
4811 __ Mthc1(src_high, FTMP);
4812 if (result_type == Primitive::kPrimFloat) {
4813 __ Cvtsl(dst, FTMP);
4814 } else {
4815 __ Cvtdl(dst, FTMP);
4816 }
4817 } else {
4818 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4819 : QUICK_ENTRY_POINT(pL2d);
4820 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4821 : IsDirectEntrypoint(kQuickL2d);
4822 codegen_->InvokeRuntime(entry_offset,
4823 conversion,
4824 conversion->GetDexPc(),
4825 nullptr,
4826 direct);
4827 if (result_type == Primitive::kPrimFloat) {
4828 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4829 } else {
4830 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4831 }
4832 }
4833 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004834 Register src = locations->InAt(0).AsRegister<Register>();
4835 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4836 __ Mtc1(src, FTMP);
4837 if (result_type == Primitive::kPrimFloat) {
4838 __ Cvtsw(dst, FTMP);
4839 } else {
4840 __ Cvtdw(dst, FTMP);
4841 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004842 }
4843 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4844 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004845 if (result_type == Primitive::kPrimLong) {
4846 if (isR6) {
4847 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4848 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4849 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4850 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4851 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4852 MipsLabel truncate;
4853 MipsLabel done;
4854
4855 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4856 // value when the input is either a NaN or is outside of the range of the output type
4857 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4858 // the same result.
4859 //
4860 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4861 // value of the output type if the input is outside of the range after the truncation or
4862 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4863 // results. This matches the desired float/double-to-int/long conversion exactly.
4864 //
4865 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4866 //
4867 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4868 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4869 // even though it must be NAN2008=1 on R6.
4870 //
4871 // The code takes care of the different behaviors by first comparing the input to the
4872 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4873 // If the input is greater than or equal to the minimum, it procedes to the truncate
4874 // instruction, which will handle such an input the same way irrespective of NAN2008.
4875 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4876 // in order to return either zero or the minimum value.
4877 //
4878 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4879 // truncate instruction for MIPS64R6.
4880 if (input_type == Primitive::kPrimFloat) {
4881 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
4882 __ LoadConst32(TMP, min_val);
4883 __ Mtc1(TMP, FTMP);
4884 __ CmpLeS(FTMP, FTMP, src);
4885 } else {
4886 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
4887 __ LoadConst32(TMP, High32Bits(min_val));
4888 __ Mtc1(ZERO, FTMP);
4889 __ Mthc1(TMP, FTMP);
4890 __ CmpLeD(FTMP, FTMP, src);
4891 }
4892
4893 __ Bc1nez(FTMP, &truncate);
4894
4895 if (input_type == Primitive::kPrimFloat) {
4896 __ CmpEqS(FTMP, src, src);
4897 } else {
4898 __ CmpEqD(FTMP, src, src);
4899 }
4900 __ Move(dst_low, ZERO);
4901 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
4902 __ Mfc1(TMP, FTMP);
4903 __ And(dst_high, dst_high, TMP);
4904
4905 __ B(&done);
4906
4907 __ Bind(&truncate);
4908
4909 if (input_type == Primitive::kPrimFloat) {
4910 __ TruncLS(FTMP, src);
4911 } else {
4912 __ TruncLD(FTMP, src);
4913 }
4914 __ Mfc1(dst_low, FTMP);
4915 __ Mfhc1(dst_high, FTMP);
4916
4917 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004918 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004919 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
4920 : QUICK_ENTRY_POINT(pD2l);
4921 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
4922 : IsDirectEntrypoint(kQuickD2l);
4923 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
4924 if (input_type == Primitive::kPrimFloat) {
4925 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4926 } else {
4927 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4928 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004929 }
4930 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004931 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4932 Register dst = locations->Out().AsRegister<Register>();
4933 MipsLabel truncate;
4934 MipsLabel done;
4935
4936 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4937 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4938 // even though it must be NAN2008=1 on R6.
4939 //
4940 // For details see the large comment above for the truncation of float/double to long on R6.
4941 //
4942 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4943 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004944 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004945 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4946 __ LoadConst32(TMP, min_val);
4947 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004948 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004949 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4950 __ LoadConst32(TMP, High32Bits(min_val));
4951 __ Mtc1(ZERO, FTMP);
4952 if (fpu_32bit) {
4953 __ Mtc1(TMP, static_cast<FRegister>(FTMP + 1));
4954 } else {
4955 __ Mthc1(TMP, FTMP);
4956 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004957 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004958
4959 if (isR6) {
4960 if (input_type == Primitive::kPrimFloat) {
4961 __ CmpLeS(FTMP, FTMP, src);
4962 } else {
4963 __ CmpLeD(FTMP, FTMP, src);
4964 }
4965 __ Bc1nez(FTMP, &truncate);
4966
4967 if (input_type == Primitive::kPrimFloat) {
4968 __ CmpEqS(FTMP, src, src);
4969 } else {
4970 __ CmpEqD(FTMP, src, src);
4971 }
4972 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4973 __ Mfc1(TMP, FTMP);
4974 __ And(dst, dst, TMP);
4975 } else {
4976 if (input_type == Primitive::kPrimFloat) {
4977 __ ColeS(0, FTMP, src);
4978 } else {
4979 __ ColeD(0, FTMP, src);
4980 }
4981 __ Bc1t(0, &truncate);
4982
4983 if (input_type == Primitive::kPrimFloat) {
4984 __ CeqS(0, src, src);
4985 } else {
4986 __ CeqD(0, src, src);
4987 }
4988 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4989 __ Movf(dst, ZERO, 0);
4990 }
4991
4992 __ B(&done);
4993
4994 __ Bind(&truncate);
4995
4996 if (input_type == Primitive::kPrimFloat) {
4997 __ TruncWS(FTMP, src);
4998 } else {
4999 __ TruncWD(FTMP, src);
5000 }
5001 __ Mfc1(dst, FTMP);
5002
5003 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005004 }
5005 } else if (Primitive::IsFloatingPointType(result_type) &&
5006 Primitive::IsFloatingPointType(input_type)) {
5007 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5008 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5009 if (result_type == Primitive::kPrimFloat) {
5010 __ Cvtsd(dst, src);
5011 } else {
5012 __ Cvtds(dst, src);
5013 }
5014 } else {
5015 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5016 << " to " << result_type;
5017 }
5018}
5019
5020void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5021 HandleShift(ushr);
5022}
5023
5024void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5025 HandleShift(ushr);
5026}
5027
5028void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5029 HandleBinaryOp(instruction);
5030}
5031
5032void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5033 HandleBinaryOp(instruction);
5034}
5035
5036void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5037 // Nothing to do, this should be removed during prepare for register allocator.
5038 LOG(FATAL) << "Unreachable";
5039}
5040
5041void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5042 // Nothing to do, this should be removed during prepare for register allocator.
5043 LOG(FATAL) << "Unreachable";
5044}
5045
5046void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005047 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005048}
5049
5050void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005051 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005052}
5053
5054void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005055 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005056}
5057
5058void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005059 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005060}
5061
5062void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005063 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005064}
5065
5066void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005067 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005068}
5069
5070void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005071 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005072}
5073
5074void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005075 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005076}
5077
5078void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005079 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005080}
5081
5082void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005083 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005084}
5085
5086void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005087 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005088}
5089
5090void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005091 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005092}
5093
5094void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005095 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005096}
5097
5098void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005099 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005100}
5101
5102void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005103 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005104}
5105
5106void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005107 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005108}
5109
5110void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005111 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005112}
5113
5114void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005115 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005116}
5117
5118void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005119 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005120}
5121
5122void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005123 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005124}
5125
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005126void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5127 LocationSummary* locations =
5128 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5129 locations->SetInAt(0, Location::RequiresRegister());
5130}
5131
5132void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5133 int32_t lower_bound = switch_instr->GetStartValue();
5134 int32_t num_entries = switch_instr->GetNumEntries();
5135 LocationSummary* locations = switch_instr->GetLocations();
5136 Register value_reg = locations->InAt(0).AsRegister<Register>();
5137 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5138
5139 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005140 Register temp_reg = TMP;
5141 __ Addiu32(temp_reg, value_reg, -lower_bound);
5142 // Jump to default if index is negative
5143 // Note: We don't check the case that index is positive while value < lower_bound, because in
5144 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5145 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5146
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005147 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005148 // Jump to successors[0] if value == lower_bound.
5149 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5150 int32_t last_index = 0;
5151 for (; num_entries - last_index > 2; last_index += 2) {
5152 __ Addiu(temp_reg, temp_reg, -2);
5153 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5154 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5155 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5156 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5157 }
5158 if (num_entries - last_index == 2) {
5159 // The last missing case_value.
5160 __ Addiu(temp_reg, temp_reg, -1);
5161 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005162 }
5163
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005164 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005165 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5166 __ B(codegen_->GetLabelOf(default_block));
5167 }
5168}
5169
5170void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5171 // The trampoline uses the same calling convention as dex calling conventions,
5172 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5173 // the method_idx.
5174 HandleInvoke(invoke);
5175}
5176
5177void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5178 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5179}
5180
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005181void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5182 LocationSummary* locations =
5183 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5184 locations->SetInAt(0, Location::RequiresRegister());
5185 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005186}
5187
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005188void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5189 LocationSummary* locations = instruction->GetLocations();
5190 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005191 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005192 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5193 instruction->GetIndex(), kMipsPointerSize).SizeValue();
5194 } else {
5195 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
5196 instruction->GetIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
5197 }
5198 __ LoadFromOffset(kLoadWord,
5199 locations->Out().AsRegister<Register>(),
5200 locations->InAt(0).AsRegister<Register>(),
5201 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005202}
5203
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005204#undef __
5205#undef QUICK_ENTRY_POINT
5206
5207} // namespace mips
5208} // namespace art