blob: abc6b1309db0ab3ea82c1db3ce9f6f5e047d87d0 [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 Marko93205e32016-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
Alexey Frunze73296a72016-06-03 22:51:46 -0700666void CodeGeneratorMIPS::ComputeSpillMask() {
667 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
668 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
669 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
670 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
671 // registers, include the ZERO register to force alignment of FPU callee-saved registers
672 // within the stack frame.
673 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
674 core_spill_mask_ |= (1 << ZERO);
675 }
676}
677
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200678static dwarf::Reg DWARFReg(Register reg) {
679 return dwarf::Reg::MipsCore(static_cast<int>(reg));
680}
681
682// TODO: mapping of floating-point registers to DWARF.
683
684void CodeGeneratorMIPS::GenerateFrameEntry() {
685 __ Bind(&frame_entry_label_);
686
687 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
688
689 if (do_overflow_check) {
690 __ LoadFromOffset(kLoadWord,
691 ZERO,
692 SP,
693 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
694 RecordPcInfo(nullptr, 0);
695 }
696
697 if (HasEmptyFrame()) {
698 return;
699 }
700
701 // Make sure the frame size isn't unreasonably large.
702 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
703 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
704 }
705
706 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200707
Alexey Frunze73296a72016-06-03 22:51:46 -0700708 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200709 __ IncreaseFrameSize(ofs);
710
Alexey Frunze73296a72016-06-03 22:51:46 -0700711 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
712 Register reg = static_cast<Register>(MostSignificantBit(mask));
713 mask ^= 1u << reg;
714 ofs -= kMipsWordSize;
715 // The ZERO register is only included for alignment.
716 if (reg != ZERO) {
717 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200718 __ cfi().RelOffset(DWARFReg(reg), ofs);
719 }
720 }
721
Alexey Frunze73296a72016-06-03 22:51:46 -0700722 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
723 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
724 mask ^= 1u << reg;
725 ofs -= kMipsDoublewordSize;
726 __ StoreDToOffset(reg, SP, ofs);
727 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200728 }
729
Alexey Frunze73296a72016-06-03 22:51:46 -0700730 // Store the current method pointer.
731 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200732}
733
734void CodeGeneratorMIPS::GenerateFrameExit() {
735 __ cfi().RememberState();
736
737 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200738 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200739
Alexey Frunze73296a72016-06-03 22:51:46 -0700740 // For better instruction scheduling restore RA before other registers.
741 uint32_t ofs = GetFrameSize();
742 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
743 Register reg = static_cast<Register>(MostSignificantBit(mask));
744 mask ^= 1u << reg;
745 ofs -= kMipsWordSize;
746 // The ZERO register is only included for alignment.
747 if (reg != ZERO) {
748 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200749 __ cfi().Restore(DWARFReg(reg));
750 }
751 }
752
Alexey Frunze73296a72016-06-03 22:51:46 -0700753 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
754 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
755 mask ^= 1u << reg;
756 ofs -= kMipsDoublewordSize;
757 __ LoadDFromOffset(reg, SP, ofs);
758 // TODO: __ cfi().Restore(DWARFReg(reg));
759 }
760
761 __ DecreaseFrameSize(GetFrameSize());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200762 }
763
764 __ Jr(RA);
765 __ Nop();
766
767 __ cfi().RestoreState();
768 __ cfi().DefCFAOffset(GetFrameSize());
769}
770
771void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
772 __ Bind(GetLabelOf(block));
773}
774
775void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
776 if (src.Equals(dst)) {
777 return;
778 }
779
780 if (src.IsConstant()) {
781 MoveConstant(dst, src.GetConstant());
782 } else {
783 if (Primitive::Is64BitType(dst_type)) {
784 Move64(dst, src);
785 } else {
786 Move32(dst, src);
787 }
788 }
789}
790
791void CodeGeneratorMIPS::Move32(Location destination, Location source) {
792 if (source.Equals(destination)) {
793 return;
794 }
795
796 if (destination.IsRegister()) {
797 if (source.IsRegister()) {
798 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
799 } else if (source.IsFpuRegister()) {
800 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
801 } else {
802 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
803 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
804 }
805 } else if (destination.IsFpuRegister()) {
806 if (source.IsRegister()) {
807 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
808 } else if (source.IsFpuRegister()) {
809 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
810 } else {
811 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
812 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
813 }
814 } else {
815 DCHECK(destination.IsStackSlot()) << destination;
816 if (source.IsRegister()) {
817 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
818 } else if (source.IsFpuRegister()) {
819 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
820 } else {
821 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
822 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
823 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
824 }
825 }
826}
827
828void CodeGeneratorMIPS::Move64(Location destination, Location source) {
829 if (source.Equals(destination)) {
830 return;
831 }
832
833 if (destination.IsRegisterPair()) {
834 if (source.IsRegisterPair()) {
835 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
836 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
837 } else if (source.IsFpuRegister()) {
838 Register dst_high = destination.AsRegisterPairHigh<Register>();
839 Register dst_low = destination.AsRegisterPairLow<Register>();
840 FRegister src = source.AsFpuRegister<FRegister>();
841 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800842 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200843 } else {
844 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
845 int32_t off = source.GetStackIndex();
846 Register r = destination.AsRegisterPairLow<Register>();
847 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
848 }
849 } else if (destination.IsFpuRegister()) {
850 if (source.IsRegisterPair()) {
851 FRegister dst = destination.AsFpuRegister<FRegister>();
852 Register src_high = source.AsRegisterPairHigh<Register>();
853 Register src_low = source.AsRegisterPairLow<Register>();
854 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800855 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200856 } else if (source.IsFpuRegister()) {
857 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
858 } else {
859 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
860 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
861 }
862 } else {
863 DCHECK(destination.IsDoubleStackSlot()) << destination;
864 int32_t off = destination.GetStackIndex();
865 if (source.IsRegisterPair()) {
866 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
867 } else if (source.IsFpuRegister()) {
868 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
869 } else {
870 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
871 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
872 __ StoreToOffset(kStoreWord, TMP, SP, off);
873 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
874 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
875 }
876 }
877}
878
879void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
880 if (c->IsIntConstant() || c->IsNullConstant()) {
881 // Move 32 bit constant.
882 int32_t value = GetInt32ValueOf(c);
883 if (destination.IsRegister()) {
884 Register dst = destination.AsRegister<Register>();
885 __ LoadConst32(dst, value);
886 } else {
887 DCHECK(destination.IsStackSlot())
888 << "Cannot move " << c->DebugName() << " to " << destination;
889 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
890 }
891 } else if (c->IsLongConstant()) {
892 // Move 64 bit constant.
893 int64_t value = GetInt64ValueOf(c);
894 if (destination.IsRegisterPair()) {
895 Register r_h = destination.AsRegisterPairHigh<Register>();
896 Register r_l = destination.AsRegisterPairLow<Register>();
897 __ LoadConst64(r_h, r_l, value);
898 } else {
899 DCHECK(destination.IsDoubleStackSlot())
900 << "Cannot move " << c->DebugName() << " to " << destination;
901 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
902 }
903 } else if (c->IsFloatConstant()) {
904 // Move 32 bit float constant.
905 int32_t value = GetInt32ValueOf(c);
906 if (destination.IsFpuRegister()) {
907 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
908 } else {
909 DCHECK(destination.IsStackSlot())
910 << "Cannot move " << c->DebugName() << " to " << destination;
911 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
912 }
913 } else {
914 // Move 64 bit double constant.
915 DCHECK(c->IsDoubleConstant()) << c->DebugName();
916 int64_t value = GetInt64ValueOf(c);
917 if (destination.IsFpuRegister()) {
918 FRegister fd = destination.AsFpuRegister<FRegister>();
919 __ LoadDConst64(fd, value, TMP);
920 } else {
921 DCHECK(destination.IsDoubleStackSlot())
922 << "Cannot move " << c->DebugName() << " to " << destination;
923 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
924 }
925 }
926}
927
928void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
929 DCHECK(destination.IsRegister());
930 Register dst = destination.AsRegister<Register>();
931 __ LoadConst32(dst, value);
932}
933
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200934void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
935 if (location.IsRegister()) {
936 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700937 } else if (location.IsRegisterPair()) {
938 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
939 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200940 } else {
941 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
942 }
943}
944
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200945void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
946 MipsLabel done;
947 Register card = AT;
948 Register temp = TMP;
949 __ Beqz(value, &done);
950 __ LoadFromOffset(kLoadWord,
951 card,
952 TR,
953 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
954 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
955 __ Addu(temp, card, temp);
956 __ Sb(card, temp, 0);
957 __ Bind(&done);
958}
959
David Brazdil58282f42016-01-14 12:45:10 +0000960void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200961 // Don't allocate the dalvik style register pair passing.
962 blocked_register_pairs_[A1_A2] = true;
963
964 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
965 blocked_core_registers_[ZERO] = true;
966 blocked_core_registers_[K0] = true;
967 blocked_core_registers_[K1] = true;
968 blocked_core_registers_[GP] = true;
969 blocked_core_registers_[SP] = true;
970 blocked_core_registers_[RA] = true;
971
972 // AT and TMP(T8) are used as temporary/scratch registers
973 // (similar to how AT is used by MIPS assemblers).
974 blocked_core_registers_[AT] = true;
975 blocked_core_registers_[TMP] = true;
976 blocked_fpu_registers_[FTMP] = true;
977
978 // Reserve suspend and thread registers.
979 blocked_core_registers_[S0] = true;
980 blocked_core_registers_[TR] = true;
981
982 // Reserve T9 for function calls
983 blocked_core_registers_[T9] = true;
984
985 // Reserve odd-numbered FPU registers.
986 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
987 blocked_fpu_registers_[i] = true;
988 }
989
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200990 UpdateBlockedPairRegisters();
991}
992
993void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
994 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
995 MipsManagedRegister current =
996 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
997 if (blocked_core_registers_[current.AsRegisterPairLow()]
998 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
999 blocked_register_pairs_[i] = true;
1000 }
1001 }
1002}
1003
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001004size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1005 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1006 return kMipsWordSize;
1007}
1008
1009size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1010 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1011 return kMipsWordSize;
1012}
1013
1014size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1015 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1016 return kMipsDoublewordSize;
1017}
1018
1019size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1020 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1021 return kMipsDoublewordSize;
1022}
1023
1024void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001025 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001026}
1027
1028void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001029 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001030}
1031
1032void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1033 HInstruction* instruction,
1034 uint32_t dex_pc,
1035 SlowPathCode* slow_path) {
1036 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1037 instruction,
1038 dex_pc,
1039 slow_path,
1040 IsDirectEntrypoint(entrypoint));
1041}
1042
1043constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1044
1045void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1046 HInstruction* instruction,
1047 uint32_t dex_pc,
1048 SlowPathCode* slow_path,
1049 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001050 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1051 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001052 if (is_direct_entrypoint) {
1053 // Reserve argument space on stack (for $a0-$a3) for
1054 // entrypoints that directly reference native implementations.
1055 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001056 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001057 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001058 } else {
1059 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001060 }
1061 RecordPcInfo(instruction, dex_pc, slow_path);
1062}
1063
1064void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1065 Register class_reg) {
1066 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1067 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1068 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1069 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1070 __ Sync(0);
1071 __ Bind(slow_path->GetExitLabel());
1072}
1073
1074void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1075 __ Sync(0); // Only stype 0 is supported.
1076}
1077
1078void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1079 HBasicBlock* successor) {
1080 SuspendCheckSlowPathMIPS* slow_path =
1081 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1082 codegen_->AddSlowPath(slow_path);
1083
1084 __ LoadFromOffset(kLoadUnsignedHalfword,
1085 TMP,
1086 TR,
1087 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1088 if (successor == nullptr) {
1089 __ Bnez(TMP, slow_path->GetEntryLabel());
1090 __ Bind(slow_path->GetReturnLabel());
1091 } else {
1092 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1093 __ B(slow_path->GetEntryLabel());
1094 // slow_path will return to GetLabelOf(successor).
1095 }
1096}
1097
1098InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1099 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001100 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001101 assembler_(codegen->GetAssembler()),
1102 codegen_(codegen) {}
1103
1104void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1105 DCHECK_EQ(instruction->InputCount(), 2U);
1106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1107 Primitive::Type type = instruction->GetResultType();
1108 switch (type) {
1109 case Primitive::kPrimInt: {
1110 locations->SetInAt(0, Location::RequiresRegister());
1111 HInstruction* right = instruction->InputAt(1);
1112 bool can_use_imm = false;
1113 if (right->IsConstant()) {
1114 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1115 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1116 can_use_imm = IsUint<16>(imm);
1117 } else if (instruction->IsAdd()) {
1118 can_use_imm = IsInt<16>(imm);
1119 } else {
1120 DCHECK(instruction->IsSub());
1121 can_use_imm = IsInt<16>(-imm);
1122 }
1123 }
1124 if (can_use_imm)
1125 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1126 else
1127 locations->SetInAt(1, Location::RequiresRegister());
1128 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1129 break;
1130 }
1131
1132 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001133 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001134 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1135 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001136 break;
1137 }
1138
1139 case Primitive::kPrimFloat:
1140 case Primitive::kPrimDouble:
1141 DCHECK(instruction->IsAdd() || instruction->IsSub());
1142 locations->SetInAt(0, Location::RequiresFpuRegister());
1143 locations->SetInAt(1, Location::RequiresFpuRegister());
1144 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1145 break;
1146
1147 default:
1148 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1149 }
1150}
1151
1152void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1153 Primitive::Type type = instruction->GetType();
1154 LocationSummary* locations = instruction->GetLocations();
1155
1156 switch (type) {
1157 case Primitive::kPrimInt: {
1158 Register dst = locations->Out().AsRegister<Register>();
1159 Register lhs = locations->InAt(0).AsRegister<Register>();
1160 Location rhs_location = locations->InAt(1);
1161
1162 Register rhs_reg = ZERO;
1163 int32_t rhs_imm = 0;
1164 bool use_imm = rhs_location.IsConstant();
1165 if (use_imm) {
1166 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1167 } else {
1168 rhs_reg = rhs_location.AsRegister<Register>();
1169 }
1170
1171 if (instruction->IsAnd()) {
1172 if (use_imm)
1173 __ Andi(dst, lhs, rhs_imm);
1174 else
1175 __ And(dst, lhs, rhs_reg);
1176 } else if (instruction->IsOr()) {
1177 if (use_imm)
1178 __ Ori(dst, lhs, rhs_imm);
1179 else
1180 __ Or(dst, lhs, rhs_reg);
1181 } else if (instruction->IsXor()) {
1182 if (use_imm)
1183 __ Xori(dst, lhs, rhs_imm);
1184 else
1185 __ Xor(dst, lhs, rhs_reg);
1186 } else if (instruction->IsAdd()) {
1187 if (use_imm)
1188 __ Addiu(dst, lhs, rhs_imm);
1189 else
1190 __ Addu(dst, lhs, rhs_reg);
1191 } else {
1192 DCHECK(instruction->IsSub());
1193 if (use_imm)
1194 __ Addiu(dst, lhs, -rhs_imm);
1195 else
1196 __ Subu(dst, lhs, rhs_reg);
1197 }
1198 break;
1199 }
1200
1201 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001202 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1203 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1204 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1205 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001206 Location rhs_location = locations->InAt(1);
1207 bool use_imm = rhs_location.IsConstant();
1208 if (!use_imm) {
1209 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1210 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1211 if (instruction->IsAnd()) {
1212 __ And(dst_low, lhs_low, rhs_low);
1213 __ And(dst_high, lhs_high, rhs_high);
1214 } else if (instruction->IsOr()) {
1215 __ Or(dst_low, lhs_low, rhs_low);
1216 __ Or(dst_high, lhs_high, rhs_high);
1217 } else if (instruction->IsXor()) {
1218 __ Xor(dst_low, lhs_low, rhs_low);
1219 __ Xor(dst_high, lhs_high, rhs_high);
1220 } else if (instruction->IsAdd()) {
1221 if (lhs_low == rhs_low) {
1222 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1223 __ Slt(TMP, lhs_low, ZERO);
1224 __ Addu(dst_low, lhs_low, rhs_low);
1225 } else {
1226 __ Addu(dst_low, lhs_low, rhs_low);
1227 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1228 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1229 }
1230 __ Addu(dst_high, lhs_high, rhs_high);
1231 __ Addu(dst_high, dst_high, TMP);
1232 } else {
1233 DCHECK(instruction->IsSub());
1234 __ Sltu(TMP, lhs_low, rhs_low);
1235 __ Subu(dst_low, lhs_low, rhs_low);
1236 __ Subu(dst_high, lhs_high, rhs_high);
1237 __ Subu(dst_high, dst_high, TMP);
1238 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001239 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001240 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1241 if (instruction->IsOr()) {
1242 uint32_t low = Low32Bits(value);
1243 uint32_t high = High32Bits(value);
1244 if (IsUint<16>(low)) {
1245 if (dst_low != lhs_low || low != 0) {
1246 __ Ori(dst_low, lhs_low, low);
1247 }
1248 } else {
1249 __ LoadConst32(TMP, low);
1250 __ Or(dst_low, lhs_low, TMP);
1251 }
1252 if (IsUint<16>(high)) {
1253 if (dst_high != lhs_high || high != 0) {
1254 __ Ori(dst_high, lhs_high, high);
1255 }
1256 } else {
1257 if (high != low) {
1258 __ LoadConst32(TMP, high);
1259 }
1260 __ Or(dst_high, lhs_high, TMP);
1261 }
1262 } else if (instruction->IsXor()) {
1263 uint32_t low = Low32Bits(value);
1264 uint32_t high = High32Bits(value);
1265 if (IsUint<16>(low)) {
1266 if (dst_low != lhs_low || low != 0) {
1267 __ Xori(dst_low, lhs_low, low);
1268 }
1269 } else {
1270 __ LoadConst32(TMP, low);
1271 __ Xor(dst_low, lhs_low, TMP);
1272 }
1273 if (IsUint<16>(high)) {
1274 if (dst_high != lhs_high || high != 0) {
1275 __ Xori(dst_high, lhs_high, high);
1276 }
1277 } else {
1278 if (high != low) {
1279 __ LoadConst32(TMP, high);
1280 }
1281 __ Xor(dst_high, lhs_high, TMP);
1282 }
1283 } else if (instruction->IsAnd()) {
1284 uint32_t low = Low32Bits(value);
1285 uint32_t high = High32Bits(value);
1286 if (IsUint<16>(low)) {
1287 __ Andi(dst_low, lhs_low, low);
1288 } else if (low != 0xFFFFFFFF) {
1289 __ LoadConst32(TMP, low);
1290 __ And(dst_low, lhs_low, TMP);
1291 } else if (dst_low != lhs_low) {
1292 __ Move(dst_low, lhs_low);
1293 }
1294 if (IsUint<16>(high)) {
1295 __ Andi(dst_high, lhs_high, high);
1296 } else if (high != 0xFFFFFFFF) {
1297 if (high != low) {
1298 __ LoadConst32(TMP, high);
1299 }
1300 __ And(dst_high, lhs_high, TMP);
1301 } else if (dst_high != lhs_high) {
1302 __ Move(dst_high, lhs_high);
1303 }
1304 } else {
1305 if (instruction->IsSub()) {
1306 value = -value;
1307 } else {
1308 DCHECK(instruction->IsAdd());
1309 }
1310 int32_t low = Low32Bits(value);
1311 int32_t high = High32Bits(value);
1312 if (IsInt<16>(low)) {
1313 if (dst_low != lhs_low || low != 0) {
1314 __ Addiu(dst_low, lhs_low, low);
1315 }
1316 if (low != 0) {
1317 __ Sltiu(AT, dst_low, low);
1318 }
1319 } else {
1320 __ LoadConst32(TMP, low);
1321 __ Addu(dst_low, lhs_low, TMP);
1322 __ Sltu(AT, dst_low, TMP);
1323 }
1324 if (IsInt<16>(high)) {
1325 if (dst_high != lhs_high || high != 0) {
1326 __ Addiu(dst_high, lhs_high, high);
1327 }
1328 } else {
1329 if (high != low) {
1330 __ LoadConst32(TMP, high);
1331 }
1332 __ Addu(dst_high, lhs_high, TMP);
1333 }
1334 if (low != 0) {
1335 __ Addu(dst_high, dst_high, AT);
1336 }
1337 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001338 }
1339 break;
1340 }
1341
1342 case Primitive::kPrimFloat:
1343 case Primitive::kPrimDouble: {
1344 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1345 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1346 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1347 if (instruction->IsAdd()) {
1348 if (type == Primitive::kPrimFloat) {
1349 __ AddS(dst, lhs, rhs);
1350 } else {
1351 __ AddD(dst, lhs, rhs);
1352 }
1353 } else {
1354 DCHECK(instruction->IsSub());
1355 if (type == Primitive::kPrimFloat) {
1356 __ SubS(dst, lhs, rhs);
1357 } else {
1358 __ SubD(dst, lhs, rhs);
1359 }
1360 }
1361 break;
1362 }
1363
1364 default:
1365 LOG(FATAL) << "Unexpected binary operation type " << type;
1366 }
1367}
1368
1369void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001370 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001371
1372 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1373 Primitive::Type type = instr->GetResultType();
1374 switch (type) {
1375 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001376 locations->SetInAt(0, Location::RequiresRegister());
1377 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1378 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1379 break;
1380 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001381 locations->SetInAt(0, Location::RequiresRegister());
1382 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1383 locations->SetOut(Location::RequiresRegister());
1384 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001385 default:
1386 LOG(FATAL) << "Unexpected shift type " << type;
1387 }
1388}
1389
1390static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1391
1392void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001393 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001394 LocationSummary* locations = instr->GetLocations();
1395 Primitive::Type type = instr->GetType();
1396
1397 Location rhs_location = locations->InAt(1);
1398 bool use_imm = rhs_location.IsConstant();
1399 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1400 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001401 const uint32_t shift_mask =
1402 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001403 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001404 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1405 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001406
1407 switch (type) {
1408 case Primitive::kPrimInt: {
1409 Register dst = locations->Out().AsRegister<Register>();
1410 Register lhs = locations->InAt(0).AsRegister<Register>();
1411 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001412 if (shift_value == 0) {
1413 if (dst != lhs) {
1414 __ Move(dst, lhs);
1415 }
1416 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001417 __ Sll(dst, lhs, shift_value);
1418 } else if (instr->IsShr()) {
1419 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001420 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001421 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001422 } else {
1423 if (has_ins_rotr) {
1424 __ Rotr(dst, lhs, shift_value);
1425 } else {
1426 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1427 __ Srl(dst, lhs, shift_value);
1428 __ Or(dst, dst, TMP);
1429 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001430 }
1431 } else {
1432 if (instr->IsShl()) {
1433 __ Sllv(dst, lhs, rhs_reg);
1434 } else if (instr->IsShr()) {
1435 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001436 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001437 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001438 } else {
1439 if (has_ins_rotr) {
1440 __ Rotrv(dst, lhs, rhs_reg);
1441 } else {
1442 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001443 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1444 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1445 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1446 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1447 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001448 __ Sllv(TMP, lhs, TMP);
1449 __ Srlv(dst, lhs, rhs_reg);
1450 __ Or(dst, dst, TMP);
1451 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001452 }
1453 }
1454 break;
1455 }
1456
1457 case Primitive::kPrimLong: {
1458 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1459 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1460 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1461 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1462 if (use_imm) {
1463 if (shift_value == 0) {
1464 codegen_->Move64(locations->Out(), locations->InAt(0));
1465 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001466 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001467 if (instr->IsShl()) {
1468 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1469 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1470 __ Sll(dst_low, lhs_low, shift_value);
1471 } else if (instr->IsShr()) {
1472 __ Srl(dst_low, lhs_low, shift_value);
1473 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1474 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001475 } else if (instr->IsUShr()) {
1476 __ Srl(dst_low, lhs_low, shift_value);
1477 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1478 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001479 } else {
1480 __ Srl(dst_low, lhs_low, shift_value);
1481 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1482 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001483 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001484 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001485 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001486 if (instr->IsShl()) {
1487 __ Sll(dst_low, lhs_low, shift_value);
1488 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1489 __ Sll(dst_high, lhs_high, shift_value);
1490 __ Or(dst_high, dst_high, TMP);
1491 } else if (instr->IsShr()) {
1492 __ Sra(dst_high, lhs_high, shift_value);
1493 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1494 __ Srl(dst_low, lhs_low, shift_value);
1495 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001496 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001497 __ Srl(dst_high, lhs_high, shift_value);
1498 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1499 __ Srl(dst_low, lhs_low, shift_value);
1500 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001501 } else {
1502 __ Srl(TMP, lhs_low, shift_value);
1503 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1504 __ Or(dst_low, dst_low, TMP);
1505 __ Srl(TMP, lhs_high, shift_value);
1506 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1507 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001508 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001509 }
1510 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001511 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001512 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001513 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001514 __ Move(dst_low, ZERO);
1515 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001516 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001517 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001518 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001519 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001520 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001521 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001522 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001523 // 64-bit rotation by 32 is just a swap.
1524 __ Move(dst_low, lhs_high);
1525 __ Move(dst_high, lhs_low);
1526 } else {
1527 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001528 __ Srl(dst_low, lhs_high, shift_value_high);
1529 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1530 __ Srl(dst_high, lhs_low, shift_value_high);
1531 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001532 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001533 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1534 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001535 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001536 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1537 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001538 __ Or(dst_high, dst_high, TMP);
1539 }
1540 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001541 }
1542 }
1543 } else {
1544 MipsLabel done;
1545 if (instr->IsShl()) {
1546 __ Sllv(dst_low, lhs_low, rhs_reg);
1547 __ Nor(AT, ZERO, rhs_reg);
1548 __ Srl(TMP, lhs_low, 1);
1549 __ Srlv(TMP, TMP, AT);
1550 __ Sllv(dst_high, lhs_high, rhs_reg);
1551 __ Or(dst_high, dst_high, TMP);
1552 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1553 __ Beqz(TMP, &done);
1554 __ Move(dst_high, dst_low);
1555 __ Move(dst_low, ZERO);
1556 } else if (instr->IsShr()) {
1557 __ Srav(dst_high, lhs_high, rhs_reg);
1558 __ Nor(AT, ZERO, rhs_reg);
1559 __ Sll(TMP, lhs_high, 1);
1560 __ Sllv(TMP, TMP, AT);
1561 __ Srlv(dst_low, lhs_low, rhs_reg);
1562 __ Or(dst_low, dst_low, TMP);
1563 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1564 __ Beqz(TMP, &done);
1565 __ Move(dst_low, dst_high);
1566 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001567 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001568 __ Srlv(dst_high, lhs_high, rhs_reg);
1569 __ Nor(AT, ZERO, rhs_reg);
1570 __ Sll(TMP, lhs_high, 1);
1571 __ Sllv(TMP, TMP, AT);
1572 __ Srlv(dst_low, lhs_low, rhs_reg);
1573 __ Or(dst_low, dst_low, TMP);
1574 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1575 __ Beqz(TMP, &done);
1576 __ Move(dst_low, dst_high);
1577 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001578 } else {
1579 __ Nor(AT, ZERO, rhs_reg);
1580 __ Srlv(TMP, lhs_low, rhs_reg);
1581 __ Sll(dst_low, lhs_high, 1);
1582 __ Sllv(dst_low, dst_low, AT);
1583 __ Or(dst_low, dst_low, TMP);
1584 __ Srlv(TMP, lhs_high, rhs_reg);
1585 __ Sll(dst_high, lhs_low, 1);
1586 __ Sllv(dst_high, dst_high, AT);
1587 __ Or(dst_high, dst_high, TMP);
1588 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1589 __ Beqz(TMP, &done);
1590 __ Move(TMP, dst_high);
1591 __ Move(dst_high, dst_low);
1592 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001593 }
1594 __ Bind(&done);
1595 }
1596 break;
1597 }
1598
1599 default:
1600 LOG(FATAL) << "Unexpected shift operation type " << type;
1601 }
1602}
1603
1604void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1605 HandleBinaryOp(instruction);
1606}
1607
1608void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1609 HandleBinaryOp(instruction);
1610}
1611
1612void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1613 HandleBinaryOp(instruction);
1614}
1615
1616void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1617 HandleBinaryOp(instruction);
1618}
1619
1620void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1621 LocationSummary* locations =
1622 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1623 locations->SetInAt(0, Location::RequiresRegister());
1624 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1625 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1626 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1627 } else {
1628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1629 }
1630}
1631
1632void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1633 LocationSummary* locations = instruction->GetLocations();
1634 Register obj = locations->InAt(0).AsRegister<Register>();
1635 Location index = locations->InAt(1);
1636 Primitive::Type type = instruction->GetType();
1637
1638 switch (type) {
1639 case Primitive::kPrimBoolean: {
1640 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1641 Register out = locations->Out().AsRegister<Register>();
1642 if (index.IsConstant()) {
1643 size_t offset =
1644 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1645 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1646 } else {
1647 __ Addu(TMP, obj, index.AsRegister<Register>());
1648 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1649 }
1650 break;
1651 }
1652
1653 case Primitive::kPrimByte: {
1654 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1655 Register out = locations->Out().AsRegister<Register>();
1656 if (index.IsConstant()) {
1657 size_t offset =
1658 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1659 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1660 } else {
1661 __ Addu(TMP, obj, index.AsRegister<Register>());
1662 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1663 }
1664 break;
1665 }
1666
1667 case Primitive::kPrimShort: {
1668 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1669 Register out = locations->Out().AsRegister<Register>();
1670 if (index.IsConstant()) {
1671 size_t offset =
1672 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1673 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1674 } else {
1675 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1676 __ Addu(TMP, obj, TMP);
1677 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1678 }
1679 break;
1680 }
1681
1682 case Primitive::kPrimChar: {
1683 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1684 Register out = locations->Out().AsRegister<Register>();
1685 if (index.IsConstant()) {
1686 size_t offset =
1687 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1688 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1689 } else {
1690 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1691 __ Addu(TMP, obj, TMP);
1692 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1693 }
1694 break;
1695 }
1696
1697 case Primitive::kPrimInt:
1698 case Primitive::kPrimNot: {
1699 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1700 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1701 Register out = locations->Out().AsRegister<Register>();
1702 if (index.IsConstant()) {
1703 size_t offset =
1704 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1705 __ LoadFromOffset(kLoadWord, out, obj, offset);
1706 } else {
1707 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1708 __ Addu(TMP, obj, TMP);
1709 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1710 }
1711 break;
1712 }
1713
1714 case Primitive::kPrimLong: {
1715 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1716 Register out = locations->Out().AsRegisterPairLow<Register>();
1717 if (index.IsConstant()) {
1718 size_t offset =
1719 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1720 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1721 } else {
1722 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1723 __ Addu(TMP, obj, TMP);
1724 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1725 }
1726 break;
1727 }
1728
1729 case Primitive::kPrimFloat: {
1730 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1731 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1732 if (index.IsConstant()) {
1733 size_t offset =
1734 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1735 __ LoadSFromOffset(out, obj, offset);
1736 } else {
1737 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1738 __ Addu(TMP, obj, TMP);
1739 __ LoadSFromOffset(out, TMP, data_offset);
1740 }
1741 break;
1742 }
1743
1744 case Primitive::kPrimDouble: {
1745 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1746 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1747 if (index.IsConstant()) {
1748 size_t offset =
1749 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1750 __ LoadDFromOffset(out, obj, offset);
1751 } else {
1752 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1753 __ Addu(TMP, obj, TMP);
1754 __ LoadDFromOffset(out, TMP, data_offset);
1755 }
1756 break;
1757 }
1758
1759 case Primitive::kPrimVoid:
1760 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1761 UNREACHABLE();
1762 }
1763 codegen_->MaybeRecordImplicitNullCheck(instruction);
1764}
1765
1766void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1767 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1768 locations->SetInAt(0, Location::RequiresRegister());
1769 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1770}
1771
1772void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1773 LocationSummary* locations = instruction->GetLocations();
1774 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1775 Register obj = locations->InAt(0).AsRegister<Register>();
1776 Register out = locations->Out().AsRegister<Register>();
1777 __ LoadFromOffset(kLoadWord, out, obj, offset);
1778 codegen_->MaybeRecordImplicitNullCheck(instruction);
1779}
1780
1781void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001782 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001783 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1784 instruction,
Pavle Batuta934808f2015-11-03 13:23:54 +01001785 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1786 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001787 InvokeRuntimeCallingConvention calling_convention;
1788 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1789 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1790 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1791 } else {
1792 locations->SetInAt(0, Location::RequiresRegister());
1793 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1794 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1795 locations->SetInAt(2, Location::RequiresFpuRegister());
1796 } else {
1797 locations->SetInAt(2, Location::RequiresRegister());
1798 }
1799 }
1800}
1801
1802void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1803 LocationSummary* locations = instruction->GetLocations();
1804 Register obj = locations->InAt(0).AsRegister<Register>();
1805 Location index = locations->InAt(1);
1806 Primitive::Type value_type = instruction->GetComponentType();
1807 bool needs_runtime_call = locations->WillCall();
1808 bool needs_write_barrier =
1809 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1810
1811 switch (value_type) {
1812 case Primitive::kPrimBoolean:
1813 case Primitive::kPrimByte: {
1814 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1815 Register value = locations->InAt(2).AsRegister<Register>();
1816 if (index.IsConstant()) {
1817 size_t offset =
1818 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1819 __ StoreToOffset(kStoreByte, value, obj, offset);
1820 } else {
1821 __ Addu(TMP, obj, index.AsRegister<Register>());
1822 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1823 }
1824 break;
1825 }
1826
1827 case Primitive::kPrimShort:
1828 case Primitive::kPrimChar: {
1829 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1830 Register value = locations->InAt(2).AsRegister<Register>();
1831 if (index.IsConstant()) {
1832 size_t offset =
1833 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1834 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1835 } else {
1836 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1837 __ Addu(TMP, obj, TMP);
1838 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1839 }
1840 break;
1841 }
1842
1843 case Primitive::kPrimInt:
1844 case Primitive::kPrimNot: {
1845 if (!needs_runtime_call) {
1846 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1847 Register value = locations->InAt(2).AsRegister<Register>();
1848 if (index.IsConstant()) {
1849 size_t offset =
1850 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1851 __ StoreToOffset(kStoreWord, value, obj, offset);
1852 } else {
1853 DCHECK(index.IsRegister()) << index;
1854 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1855 __ Addu(TMP, obj, TMP);
1856 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1857 }
1858 codegen_->MaybeRecordImplicitNullCheck(instruction);
1859 if (needs_write_barrier) {
1860 DCHECK_EQ(value_type, Primitive::kPrimNot);
1861 codegen_->MarkGCCard(obj, value);
1862 }
1863 } else {
1864 DCHECK_EQ(value_type, Primitive::kPrimNot);
1865 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1866 instruction,
1867 instruction->GetDexPc(),
1868 nullptr,
1869 IsDirectEntrypoint(kQuickAputObject));
1870 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
1871 }
1872 break;
1873 }
1874
1875 case Primitive::kPrimLong: {
1876 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1877 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
1878 if (index.IsConstant()) {
1879 size_t offset =
1880 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1881 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1882 } else {
1883 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1884 __ Addu(TMP, obj, TMP);
1885 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1886 }
1887 break;
1888 }
1889
1890 case Primitive::kPrimFloat: {
1891 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1892 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1893 DCHECK(locations->InAt(2).IsFpuRegister());
1894 if (index.IsConstant()) {
1895 size_t offset =
1896 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1897 __ StoreSToOffset(value, obj, offset);
1898 } else {
1899 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1900 __ Addu(TMP, obj, TMP);
1901 __ StoreSToOffset(value, TMP, data_offset);
1902 }
1903 break;
1904 }
1905
1906 case Primitive::kPrimDouble: {
1907 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1908 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1909 DCHECK(locations->InAt(2).IsFpuRegister());
1910 if (index.IsConstant()) {
1911 size_t offset =
1912 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1913 __ StoreDToOffset(value, obj, offset);
1914 } else {
1915 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1916 __ Addu(TMP, obj, TMP);
1917 __ StoreDToOffset(value, TMP, data_offset);
1918 }
1919 break;
1920 }
1921
1922 case Primitive::kPrimVoid:
1923 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1924 UNREACHABLE();
1925 }
1926
1927 // Ints and objects are handled in the switch.
1928 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1929 codegen_->MaybeRecordImplicitNullCheck(instruction);
1930 }
1931}
1932
1933void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1934 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1935 ? LocationSummary::kCallOnSlowPath
1936 : LocationSummary::kNoCall;
1937 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1938 locations->SetInAt(0, Location::RequiresRegister());
1939 locations->SetInAt(1, Location::RequiresRegister());
1940 if (instruction->HasUses()) {
1941 locations->SetOut(Location::SameAsFirstInput());
1942 }
1943}
1944
1945void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1946 LocationSummary* locations = instruction->GetLocations();
1947 BoundsCheckSlowPathMIPS* slow_path =
1948 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
1949 codegen_->AddSlowPath(slow_path);
1950
1951 Register index = locations->InAt(0).AsRegister<Register>();
1952 Register length = locations->InAt(1).AsRegister<Register>();
1953
1954 // length is limited by the maximum positive signed 32-bit integer.
1955 // Unsigned comparison of length and index checks for index < 0
1956 // and for length <= index simultaneously.
1957 __ Bgeu(index, length, slow_path->GetEntryLabel());
1958}
1959
1960void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
1961 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1962 instruction,
1963 LocationSummary::kCallOnSlowPath);
1964 locations->SetInAt(0, Location::RequiresRegister());
1965 locations->SetInAt(1, Location::RequiresRegister());
1966 // Note that TypeCheckSlowPathMIPS uses this register too.
1967 locations->AddTemp(Location::RequiresRegister());
1968}
1969
1970void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
1971 LocationSummary* locations = instruction->GetLocations();
1972 Register obj = locations->InAt(0).AsRegister<Register>();
1973 Register cls = locations->InAt(1).AsRegister<Register>();
1974 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
1975
1976 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
1977 codegen_->AddSlowPath(slow_path);
1978
1979 // TODO: avoid this check if we know obj is not null.
1980 __ Beqz(obj, slow_path->GetExitLabel());
1981 // Compare the class of `obj` with `cls`.
1982 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1983 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
1984 __ Bind(slow_path->GetExitLabel());
1985}
1986
1987void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
1988 LocationSummary* locations =
1989 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1990 locations->SetInAt(0, Location::RequiresRegister());
1991 if (check->HasUses()) {
1992 locations->SetOut(Location::SameAsFirstInput());
1993 }
1994}
1995
1996void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
1997 // We assume the class is not null.
1998 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
1999 check->GetLoadClass(),
2000 check,
2001 check->GetDexPc(),
2002 true);
2003 codegen_->AddSlowPath(slow_path);
2004 GenerateClassInitializationCheck(slow_path,
2005 check->GetLocations()->InAt(0).AsRegister<Register>());
2006}
2007
2008void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2009 Primitive::Type in_type = compare->InputAt(0)->GetType();
2010
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002011 LocationSummary* locations =
2012 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002013
2014 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002015 case Primitive::kPrimBoolean:
2016 case Primitive::kPrimByte:
2017 case Primitive::kPrimShort:
2018 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002019 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002020 case Primitive::kPrimLong:
2021 locations->SetInAt(0, Location::RequiresRegister());
2022 locations->SetInAt(1, Location::RequiresRegister());
2023 // Output overlaps because it is written before doing the low comparison.
2024 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2025 break;
2026
2027 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002028 case Primitive::kPrimDouble:
2029 locations->SetInAt(0, Location::RequiresFpuRegister());
2030 locations->SetInAt(1, Location::RequiresFpuRegister());
2031 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002032 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002033
2034 default:
2035 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2036 }
2037}
2038
2039void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2040 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002041 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002042 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002043 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002044
2045 // 0 if: left == right
2046 // 1 if: left > right
2047 // -1 if: left < right
2048 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002049 case Primitive::kPrimBoolean:
2050 case Primitive::kPrimByte:
2051 case Primitive::kPrimShort:
2052 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002053 case Primitive::kPrimInt: {
2054 Register lhs = locations->InAt(0).AsRegister<Register>();
2055 Register rhs = locations->InAt(1).AsRegister<Register>();
2056 __ Slt(TMP, lhs, rhs);
2057 __ Slt(res, rhs, lhs);
2058 __ Subu(res, res, TMP);
2059 break;
2060 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002061 case Primitive::kPrimLong: {
2062 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002063 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2064 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2065 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2066 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2067 // TODO: more efficient (direct) comparison with a constant.
2068 __ Slt(TMP, lhs_high, rhs_high);
2069 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2070 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2071 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2072 __ Sltu(TMP, lhs_low, rhs_low);
2073 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2074 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2075 __ Bind(&done);
2076 break;
2077 }
2078
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002079 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002080 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002081 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2082 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2083 MipsLabel done;
2084 if (isR6) {
2085 __ CmpEqS(FTMP, lhs, rhs);
2086 __ LoadConst32(res, 0);
2087 __ Bc1nez(FTMP, &done);
2088 if (gt_bias) {
2089 __ CmpLtS(FTMP, lhs, rhs);
2090 __ LoadConst32(res, -1);
2091 __ Bc1nez(FTMP, &done);
2092 __ LoadConst32(res, 1);
2093 } else {
2094 __ CmpLtS(FTMP, rhs, lhs);
2095 __ LoadConst32(res, 1);
2096 __ Bc1nez(FTMP, &done);
2097 __ LoadConst32(res, -1);
2098 }
2099 } else {
2100 if (gt_bias) {
2101 __ ColtS(0, lhs, rhs);
2102 __ LoadConst32(res, -1);
2103 __ Bc1t(0, &done);
2104 __ CeqS(0, lhs, rhs);
2105 __ LoadConst32(res, 1);
2106 __ Movt(res, ZERO, 0);
2107 } else {
2108 __ ColtS(0, rhs, lhs);
2109 __ LoadConst32(res, 1);
2110 __ Bc1t(0, &done);
2111 __ CeqS(0, lhs, rhs);
2112 __ LoadConst32(res, -1);
2113 __ Movt(res, ZERO, 0);
2114 }
2115 }
2116 __ Bind(&done);
2117 break;
2118 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002119 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002120 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002121 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2122 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2123 MipsLabel done;
2124 if (isR6) {
2125 __ CmpEqD(FTMP, lhs, rhs);
2126 __ LoadConst32(res, 0);
2127 __ Bc1nez(FTMP, &done);
2128 if (gt_bias) {
2129 __ CmpLtD(FTMP, lhs, rhs);
2130 __ LoadConst32(res, -1);
2131 __ Bc1nez(FTMP, &done);
2132 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002133 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002134 __ CmpLtD(FTMP, rhs, lhs);
2135 __ LoadConst32(res, 1);
2136 __ Bc1nez(FTMP, &done);
2137 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002138 }
2139 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002140 if (gt_bias) {
2141 __ ColtD(0, lhs, rhs);
2142 __ LoadConst32(res, -1);
2143 __ Bc1t(0, &done);
2144 __ CeqD(0, lhs, rhs);
2145 __ LoadConst32(res, 1);
2146 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002147 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002148 __ ColtD(0, rhs, lhs);
2149 __ LoadConst32(res, 1);
2150 __ Bc1t(0, &done);
2151 __ CeqD(0, lhs, rhs);
2152 __ LoadConst32(res, -1);
2153 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002154 }
2155 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002156 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002157 break;
2158 }
2159
2160 default:
2161 LOG(FATAL) << "Unimplemented compare type " << in_type;
2162 }
2163}
2164
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002165void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002166 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002167 switch (instruction->InputAt(0)->GetType()) {
2168 default:
2169 case Primitive::kPrimLong:
2170 locations->SetInAt(0, Location::RequiresRegister());
2171 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2172 break;
2173
2174 case Primitive::kPrimFloat:
2175 case Primitive::kPrimDouble:
2176 locations->SetInAt(0, Location::RequiresFpuRegister());
2177 locations->SetInAt(1, Location::RequiresFpuRegister());
2178 break;
2179 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002180 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002181 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2182 }
2183}
2184
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002185void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002186 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002187 return;
2188 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002189
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002190 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002191 LocationSummary* locations = instruction->GetLocations();
2192 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002193 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002194
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002195 switch (type) {
2196 default:
2197 // Integer case.
2198 GenerateIntCompare(instruction->GetCondition(), locations);
2199 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002200
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002201 case Primitive::kPrimLong:
2202 // TODO: don't use branches.
2203 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002204 break;
2205
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002206 case Primitive::kPrimFloat:
2207 case Primitive::kPrimDouble:
2208 // TODO: don't use branches.
2209 GenerateFpCompareAndBranch(instruction->GetCondition(),
2210 instruction->IsGtBias(),
2211 type,
2212 locations,
2213 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002214 break;
2215 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002216
2217 // Convert the branches into the result.
2218 MipsLabel done;
2219
2220 // False case: result = 0.
2221 __ LoadConst32(dst, 0);
2222 __ B(&done);
2223
2224 // True case: result = 1.
2225 __ Bind(&true_label);
2226 __ LoadConst32(dst, 1);
2227 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002228}
2229
Alexey Frunze7e99e052015-11-24 19:28:01 -08002230void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2231 DCHECK(instruction->IsDiv() || instruction->IsRem());
2232 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2233
2234 LocationSummary* locations = instruction->GetLocations();
2235 Location second = locations->InAt(1);
2236 DCHECK(second.IsConstant());
2237
2238 Register out = locations->Out().AsRegister<Register>();
2239 Register dividend = locations->InAt(0).AsRegister<Register>();
2240 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2241 DCHECK(imm == 1 || imm == -1);
2242
2243 if (instruction->IsRem()) {
2244 __ Move(out, ZERO);
2245 } else {
2246 if (imm == -1) {
2247 __ Subu(out, ZERO, dividend);
2248 } else if (out != dividend) {
2249 __ Move(out, dividend);
2250 }
2251 }
2252}
2253
2254void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2255 DCHECK(instruction->IsDiv() || instruction->IsRem());
2256 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2257
2258 LocationSummary* locations = instruction->GetLocations();
2259 Location second = locations->InAt(1);
2260 DCHECK(second.IsConstant());
2261
2262 Register out = locations->Out().AsRegister<Register>();
2263 Register dividend = locations->InAt(0).AsRegister<Register>();
2264 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002265 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002266 int ctz_imm = CTZ(abs_imm);
2267
2268 if (instruction->IsDiv()) {
2269 if (ctz_imm == 1) {
2270 // Fast path for division by +/-2, which is very common.
2271 __ Srl(TMP, dividend, 31);
2272 } else {
2273 __ Sra(TMP, dividend, 31);
2274 __ Srl(TMP, TMP, 32 - ctz_imm);
2275 }
2276 __ Addu(out, dividend, TMP);
2277 __ Sra(out, out, ctz_imm);
2278 if (imm < 0) {
2279 __ Subu(out, ZERO, out);
2280 }
2281 } else {
2282 if (ctz_imm == 1) {
2283 // Fast path for modulo +/-2, which is very common.
2284 __ Sra(TMP, dividend, 31);
2285 __ Subu(out, dividend, TMP);
2286 __ Andi(out, out, 1);
2287 __ Addu(out, out, TMP);
2288 } else {
2289 __ Sra(TMP, dividend, 31);
2290 __ Srl(TMP, TMP, 32 - ctz_imm);
2291 __ Addu(out, dividend, TMP);
2292 if (IsUint<16>(abs_imm - 1)) {
2293 __ Andi(out, out, abs_imm - 1);
2294 } else {
2295 __ Sll(out, out, 32 - ctz_imm);
2296 __ Srl(out, out, 32 - ctz_imm);
2297 }
2298 __ Subu(out, out, TMP);
2299 }
2300 }
2301}
2302
2303void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2304 DCHECK(instruction->IsDiv() || instruction->IsRem());
2305 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2306
2307 LocationSummary* locations = instruction->GetLocations();
2308 Location second = locations->InAt(1);
2309 DCHECK(second.IsConstant());
2310
2311 Register out = locations->Out().AsRegister<Register>();
2312 Register dividend = locations->InAt(0).AsRegister<Register>();
2313 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2314
2315 int64_t magic;
2316 int shift;
2317 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2318
2319 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2320
2321 __ LoadConst32(TMP, magic);
2322 if (isR6) {
2323 __ MuhR6(TMP, dividend, TMP);
2324 } else {
2325 __ MultR2(dividend, TMP);
2326 __ Mfhi(TMP);
2327 }
2328 if (imm > 0 && magic < 0) {
2329 __ Addu(TMP, TMP, dividend);
2330 } else if (imm < 0 && magic > 0) {
2331 __ Subu(TMP, TMP, dividend);
2332 }
2333
2334 if (shift != 0) {
2335 __ Sra(TMP, TMP, shift);
2336 }
2337
2338 if (instruction->IsDiv()) {
2339 __ Sra(out, TMP, 31);
2340 __ Subu(out, TMP, out);
2341 } else {
2342 __ Sra(AT, TMP, 31);
2343 __ Subu(AT, TMP, AT);
2344 __ LoadConst32(TMP, imm);
2345 if (isR6) {
2346 __ MulR6(TMP, AT, TMP);
2347 } else {
2348 __ MulR2(TMP, AT, TMP);
2349 }
2350 __ Subu(out, dividend, TMP);
2351 }
2352}
2353
2354void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2355 DCHECK(instruction->IsDiv() || instruction->IsRem());
2356 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2357
2358 LocationSummary* locations = instruction->GetLocations();
2359 Register out = locations->Out().AsRegister<Register>();
2360 Location second = locations->InAt(1);
2361
2362 if (second.IsConstant()) {
2363 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2364 if (imm == 0) {
2365 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2366 } else if (imm == 1 || imm == -1) {
2367 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002368 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002369 DivRemByPowerOfTwo(instruction);
2370 } else {
2371 DCHECK(imm <= -2 || imm >= 2);
2372 GenerateDivRemWithAnyConstant(instruction);
2373 }
2374 } else {
2375 Register dividend = locations->InAt(0).AsRegister<Register>();
2376 Register divisor = second.AsRegister<Register>();
2377 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2378 if (instruction->IsDiv()) {
2379 if (isR6) {
2380 __ DivR6(out, dividend, divisor);
2381 } else {
2382 __ DivR2(out, dividend, divisor);
2383 }
2384 } else {
2385 if (isR6) {
2386 __ ModR6(out, dividend, divisor);
2387 } else {
2388 __ ModR2(out, dividend, divisor);
2389 }
2390 }
2391 }
2392}
2393
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002394void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2395 Primitive::Type type = div->GetResultType();
2396 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
2397 ? LocationSummary::kCall
2398 : LocationSummary::kNoCall;
2399
2400 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2401
2402 switch (type) {
2403 case Primitive::kPrimInt:
2404 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002405 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002406 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2407 break;
2408
2409 case Primitive::kPrimLong: {
2410 InvokeRuntimeCallingConvention calling_convention;
2411 locations->SetInAt(0, Location::RegisterPairLocation(
2412 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2413 locations->SetInAt(1, Location::RegisterPairLocation(
2414 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2415 locations->SetOut(calling_convention.GetReturnLocation(type));
2416 break;
2417 }
2418
2419 case Primitive::kPrimFloat:
2420 case Primitive::kPrimDouble:
2421 locations->SetInAt(0, Location::RequiresFpuRegister());
2422 locations->SetInAt(1, Location::RequiresFpuRegister());
2423 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2424 break;
2425
2426 default:
2427 LOG(FATAL) << "Unexpected div type " << type;
2428 }
2429}
2430
2431void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2432 Primitive::Type type = instruction->GetType();
2433 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002434
2435 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002436 case Primitive::kPrimInt:
2437 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002438 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002439 case Primitive::kPrimLong: {
2440 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2441 instruction,
2442 instruction->GetDexPc(),
2443 nullptr,
2444 IsDirectEntrypoint(kQuickLdiv));
2445 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2446 break;
2447 }
2448 case Primitive::kPrimFloat:
2449 case Primitive::kPrimDouble: {
2450 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2451 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2452 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2453 if (type == Primitive::kPrimFloat) {
2454 __ DivS(dst, lhs, rhs);
2455 } else {
2456 __ DivD(dst, lhs, rhs);
2457 }
2458 break;
2459 }
2460 default:
2461 LOG(FATAL) << "Unexpected div type " << type;
2462 }
2463}
2464
2465void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2466 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2467 ? LocationSummary::kCallOnSlowPath
2468 : LocationSummary::kNoCall;
2469 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2470 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2471 if (instruction->HasUses()) {
2472 locations->SetOut(Location::SameAsFirstInput());
2473 }
2474}
2475
2476void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2477 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2478 codegen_->AddSlowPath(slow_path);
2479 Location value = instruction->GetLocations()->InAt(0);
2480 Primitive::Type type = instruction->GetType();
2481
2482 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002483 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002484 case Primitive::kPrimByte:
2485 case Primitive::kPrimChar:
2486 case Primitive::kPrimShort:
2487 case Primitive::kPrimInt: {
2488 if (value.IsConstant()) {
2489 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2490 __ B(slow_path->GetEntryLabel());
2491 } else {
2492 // A division by a non-null constant is valid. We don't need to perform
2493 // any check, so simply fall through.
2494 }
2495 } else {
2496 DCHECK(value.IsRegister()) << value;
2497 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2498 }
2499 break;
2500 }
2501 case Primitive::kPrimLong: {
2502 if (value.IsConstant()) {
2503 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2504 __ B(slow_path->GetEntryLabel());
2505 } else {
2506 // A division by a non-null constant is valid. We don't need to perform
2507 // any check, so simply fall through.
2508 }
2509 } else {
2510 DCHECK(value.IsRegisterPair()) << value;
2511 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2512 __ Beqz(TMP, slow_path->GetEntryLabel());
2513 }
2514 break;
2515 }
2516 default:
2517 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2518 }
2519}
2520
2521void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2522 LocationSummary* locations =
2523 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2524 locations->SetOut(Location::ConstantLocation(constant));
2525}
2526
2527void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2528 // Will be generated at use site.
2529}
2530
2531void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2532 exit->SetLocations(nullptr);
2533}
2534
2535void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2536}
2537
2538void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2539 LocationSummary* locations =
2540 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2541 locations->SetOut(Location::ConstantLocation(constant));
2542}
2543
2544void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2545 // Will be generated at use site.
2546}
2547
2548void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2549 got->SetLocations(nullptr);
2550}
2551
2552void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2553 DCHECK(!successor->IsExitBlock());
2554 HBasicBlock* block = got->GetBlock();
2555 HInstruction* previous = got->GetPrevious();
2556 HLoopInformation* info = block->GetLoopInformation();
2557
2558 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2559 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2560 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2561 return;
2562 }
2563 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2564 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2565 }
2566 if (!codegen_->GoesToNextBlock(block, successor)) {
2567 __ B(codegen_->GetLabelOf(successor));
2568 }
2569}
2570
2571void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2572 HandleGoto(got, got->GetSuccessor());
2573}
2574
2575void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2576 try_boundary->SetLocations(nullptr);
2577}
2578
2579void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2580 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2581 if (!successor->IsExitBlock()) {
2582 HandleGoto(try_boundary, successor);
2583 }
2584}
2585
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002586void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2587 LocationSummary* locations) {
2588 Register dst = locations->Out().AsRegister<Register>();
2589 Register lhs = locations->InAt(0).AsRegister<Register>();
2590 Location rhs_location = locations->InAt(1);
2591 Register rhs_reg = ZERO;
2592 int64_t rhs_imm = 0;
2593 bool use_imm = rhs_location.IsConstant();
2594 if (use_imm) {
2595 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2596 } else {
2597 rhs_reg = rhs_location.AsRegister<Register>();
2598 }
2599
2600 switch (cond) {
2601 case kCondEQ:
2602 case kCondNE:
2603 if (use_imm && IsUint<16>(rhs_imm)) {
2604 __ Xori(dst, lhs, rhs_imm);
2605 } else {
2606 if (use_imm) {
2607 rhs_reg = TMP;
2608 __ LoadConst32(rhs_reg, rhs_imm);
2609 }
2610 __ Xor(dst, lhs, rhs_reg);
2611 }
2612 if (cond == kCondEQ) {
2613 __ Sltiu(dst, dst, 1);
2614 } else {
2615 __ Sltu(dst, ZERO, dst);
2616 }
2617 break;
2618
2619 case kCondLT:
2620 case kCondGE:
2621 if (use_imm && IsInt<16>(rhs_imm)) {
2622 __ Slti(dst, lhs, rhs_imm);
2623 } else {
2624 if (use_imm) {
2625 rhs_reg = TMP;
2626 __ LoadConst32(rhs_reg, rhs_imm);
2627 }
2628 __ Slt(dst, lhs, rhs_reg);
2629 }
2630 if (cond == kCondGE) {
2631 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2632 // only the slt instruction but no sge.
2633 __ Xori(dst, dst, 1);
2634 }
2635 break;
2636
2637 case kCondLE:
2638 case kCondGT:
2639 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2640 // Simulate lhs <= rhs via lhs < rhs + 1.
2641 __ Slti(dst, lhs, rhs_imm + 1);
2642 if (cond == kCondGT) {
2643 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2644 // only the slti instruction but no sgti.
2645 __ Xori(dst, dst, 1);
2646 }
2647 } else {
2648 if (use_imm) {
2649 rhs_reg = TMP;
2650 __ LoadConst32(rhs_reg, rhs_imm);
2651 }
2652 __ Slt(dst, rhs_reg, lhs);
2653 if (cond == kCondLE) {
2654 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2655 // only the slt instruction but no sle.
2656 __ Xori(dst, dst, 1);
2657 }
2658 }
2659 break;
2660
2661 case kCondB:
2662 case kCondAE:
2663 if (use_imm && IsInt<16>(rhs_imm)) {
2664 // Sltiu sign-extends its 16-bit immediate operand before
2665 // the comparison and thus lets us compare directly with
2666 // unsigned values in the ranges [0, 0x7fff] and
2667 // [0xffff8000, 0xffffffff].
2668 __ Sltiu(dst, lhs, rhs_imm);
2669 } else {
2670 if (use_imm) {
2671 rhs_reg = TMP;
2672 __ LoadConst32(rhs_reg, rhs_imm);
2673 }
2674 __ Sltu(dst, lhs, rhs_reg);
2675 }
2676 if (cond == kCondAE) {
2677 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2678 // only the sltu instruction but no sgeu.
2679 __ Xori(dst, dst, 1);
2680 }
2681 break;
2682
2683 case kCondBE:
2684 case kCondA:
2685 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2686 // Simulate lhs <= rhs via lhs < rhs + 1.
2687 // Note that this only works if rhs + 1 does not overflow
2688 // to 0, hence the check above.
2689 // Sltiu sign-extends its 16-bit immediate operand before
2690 // the comparison and thus lets us compare directly with
2691 // unsigned values in the ranges [0, 0x7fff] and
2692 // [0xffff8000, 0xffffffff].
2693 __ Sltiu(dst, lhs, rhs_imm + 1);
2694 if (cond == kCondA) {
2695 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2696 // only the sltiu instruction but no sgtiu.
2697 __ Xori(dst, dst, 1);
2698 }
2699 } else {
2700 if (use_imm) {
2701 rhs_reg = TMP;
2702 __ LoadConst32(rhs_reg, rhs_imm);
2703 }
2704 __ Sltu(dst, rhs_reg, lhs);
2705 if (cond == kCondBE) {
2706 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2707 // only the sltu instruction but no sleu.
2708 __ Xori(dst, dst, 1);
2709 }
2710 }
2711 break;
2712 }
2713}
2714
2715void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2716 LocationSummary* locations,
2717 MipsLabel* label) {
2718 Register lhs = locations->InAt(0).AsRegister<Register>();
2719 Location rhs_location = locations->InAt(1);
2720 Register rhs_reg = ZERO;
2721 int32_t rhs_imm = 0;
2722 bool use_imm = rhs_location.IsConstant();
2723 if (use_imm) {
2724 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2725 } else {
2726 rhs_reg = rhs_location.AsRegister<Register>();
2727 }
2728
2729 if (use_imm && rhs_imm == 0) {
2730 switch (cond) {
2731 case kCondEQ:
2732 case kCondBE: // <= 0 if zero
2733 __ Beqz(lhs, label);
2734 break;
2735 case kCondNE:
2736 case kCondA: // > 0 if non-zero
2737 __ Bnez(lhs, label);
2738 break;
2739 case kCondLT:
2740 __ Bltz(lhs, label);
2741 break;
2742 case kCondGE:
2743 __ Bgez(lhs, label);
2744 break;
2745 case kCondLE:
2746 __ Blez(lhs, label);
2747 break;
2748 case kCondGT:
2749 __ Bgtz(lhs, label);
2750 break;
2751 case kCondB: // always false
2752 break;
2753 case kCondAE: // always true
2754 __ B(label);
2755 break;
2756 }
2757 } else {
2758 if (use_imm) {
2759 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2760 rhs_reg = TMP;
2761 __ LoadConst32(rhs_reg, rhs_imm);
2762 }
2763 switch (cond) {
2764 case kCondEQ:
2765 __ Beq(lhs, rhs_reg, label);
2766 break;
2767 case kCondNE:
2768 __ Bne(lhs, rhs_reg, label);
2769 break;
2770 case kCondLT:
2771 __ Blt(lhs, rhs_reg, label);
2772 break;
2773 case kCondGE:
2774 __ Bge(lhs, rhs_reg, label);
2775 break;
2776 case kCondLE:
2777 __ Bge(rhs_reg, lhs, label);
2778 break;
2779 case kCondGT:
2780 __ Blt(rhs_reg, lhs, label);
2781 break;
2782 case kCondB:
2783 __ Bltu(lhs, rhs_reg, label);
2784 break;
2785 case kCondAE:
2786 __ Bgeu(lhs, rhs_reg, label);
2787 break;
2788 case kCondBE:
2789 __ Bgeu(rhs_reg, lhs, label);
2790 break;
2791 case kCondA:
2792 __ Bltu(rhs_reg, lhs, label);
2793 break;
2794 }
2795 }
2796}
2797
2798void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2799 LocationSummary* locations,
2800 MipsLabel* label) {
2801 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2802 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2803 Location rhs_location = locations->InAt(1);
2804 Register rhs_high = ZERO;
2805 Register rhs_low = ZERO;
2806 int64_t imm = 0;
2807 uint32_t imm_high = 0;
2808 uint32_t imm_low = 0;
2809 bool use_imm = rhs_location.IsConstant();
2810 if (use_imm) {
2811 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2812 imm_high = High32Bits(imm);
2813 imm_low = Low32Bits(imm);
2814 } else {
2815 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2816 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2817 }
2818
2819 if (use_imm && imm == 0) {
2820 switch (cond) {
2821 case kCondEQ:
2822 case kCondBE: // <= 0 if zero
2823 __ Or(TMP, lhs_high, lhs_low);
2824 __ Beqz(TMP, label);
2825 break;
2826 case kCondNE:
2827 case kCondA: // > 0 if non-zero
2828 __ Or(TMP, lhs_high, lhs_low);
2829 __ Bnez(TMP, label);
2830 break;
2831 case kCondLT:
2832 __ Bltz(lhs_high, label);
2833 break;
2834 case kCondGE:
2835 __ Bgez(lhs_high, label);
2836 break;
2837 case kCondLE:
2838 __ Or(TMP, lhs_high, lhs_low);
2839 __ Sra(AT, lhs_high, 31);
2840 __ Bgeu(AT, TMP, label);
2841 break;
2842 case kCondGT:
2843 __ Or(TMP, lhs_high, lhs_low);
2844 __ Sra(AT, lhs_high, 31);
2845 __ Bltu(AT, TMP, label);
2846 break;
2847 case kCondB: // always false
2848 break;
2849 case kCondAE: // always true
2850 __ B(label);
2851 break;
2852 }
2853 } else if (use_imm) {
2854 // TODO: more efficient comparison with constants without loading them into TMP/AT.
2855 switch (cond) {
2856 case kCondEQ:
2857 __ LoadConst32(TMP, imm_high);
2858 __ Xor(TMP, TMP, lhs_high);
2859 __ LoadConst32(AT, imm_low);
2860 __ Xor(AT, AT, lhs_low);
2861 __ Or(TMP, TMP, AT);
2862 __ Beqz(TMP, label);
2863 break;
2864 case kCondNE:
2865 __ LoadConst32(TMP, imm_high);
2866 __ Xor(TMP, TMP, lhs_high);
2867 __ LoadConst32(AT, imm_low);
2868 __ Xor(AT, AT, lhs_low);
2869 __ Or(TMP, TMP, AT);
2870 __ Bnez(TMP, label);
2871 break;
2872 case kCondLT:
2873 __ LoadConst32(TMP, imm_high);
2874 __ Blt(lhs_high, TMP, label);
2875 __ Slt(TMP, TMP, lhs_high);
2876 __ LoadConst32(AT, imm_low);
2877 __ Sltu(AT, lhs_low, AT);
2878 __ Blt(TMP, AT, label);
2879 break;
2880 case kCondGE:
2881 __ LoadConst32(TMP, imm_high);
2882 __ Blt(TMP, lhs_high, label);
2883 __ Slt(TMP, lhs_high, TMP);
2884 __ LoadConst32(AT, imm_low);
2885 __ Sltu(AT, lhs_low, AT);
2886 __ Or(TMP, TMP, AT);
2887 __ Beqz(TMP, label);
2888 break;
2889 case kCondLE:
2890 __ LoadConst32(TMP, imm_high);
2891 __ Blt(lhs_high, TMP, label);
2892 __ Slt(TMP, TMP, lhs_high);
2893 __ LoadConst32(AT, imm_low);
2894 __ Sltu(AT, AT, lhs_low);
2895 __ Or(TMP, TMP, AT);
2896 __ Beqz(TMP, label);
2897 break;
2898 case kCondGT:
2899 __ LoadConst32(TMP, imm_high);
2900 __ Blt(TMP, lhs_high, label);
2901 __ Slt(TMP, lhs_high, TMP);
2902 __ LoadConst32(AT, imm_low);
2903 __ Sltu(AT, AT, lhs_low);
2904 __ Blt(TMP, AT, label);
2905 break;
2906 case kCondB:
2907 __ LoadConst32(TMP, imm_high);
2908 __ Bltu(lhs_high, TMP, label);
2909 __ Sltu(TMP, TMP, lhs_high);
2910 __ LoadConst32(AT, imm_low);
2911 __ Sltu(AT, lhs_low, AT);
2912 __ Blt(TMP, AT, label);
2913 break;
2914 case kCondAE:
2915 __ LoadConst32(TMP, imm_high);
2916 __ Bltu(TMP, lhs_high, label);
2917 __ Sltu(TMP, lhs_high, TMP);
2918 __ LoadConst32(AT, imm_low);
2919 __ Sltu(AT, lhs_low, AT);
2920 __ Or(TMP, TMP, AT);
2921 __ Beqz(TMP, label);
2922 break;
2923 case kCondBE:
2924 __ LoadConst32(TMP, imm_high);
2925 __ Bltu(lhs_high, TMP, label);
2926 __ Sltu(TMP, TMP, lhs_high);
2927 __ LoadConst32(AT, imm_low);
2928 __ Sltu(AT, AT, lhs_low);
2929 __ Or(TMP, TMP, AT);
2930 __ Beqz(TMP, label);
2931 break;
2932 case kCondA:
2933 __ LoadConst32(TMP, imm_high);
2934 __ Bltu(TMP, lhs_high, label);
2935 __ Sltu(TMP, lhs_high, TMP);
2936 __ LoadConst32(AT, imm_low);
2937 __ Sltu(AT, AT, lhs_low);
2938 __ Blt(TMP, AT, label);
2939 break;
2940 }
2941 } else {
2942 switch (cond) {
2943 case kCondEQ:
2944 __ Xor(TMP, lhs_high, rhs_high);
2945 __ Xor(AT, lhs_low, rhs_low);
2946 __ Or(TMP, TMP, AT);
2947 __ Beqz(TMP, label);
2948 break;
2949 case kCondNE:
2950 __ Xor(TMP, lhs_high, rhs_high);
2951 __ Xor(AT, lhs_low, rhs_low);
2952 __ Or(TMP, TMP, AT);
2953 __ Bnez(TMP, label);
2954 break;
2955 case kCondLT:
2956 __ Blt(lhs_high, rhs_high, label);
2957 __ Slt(TMP, rhs_high, lhs_high);
2958 __ Sltu(AT, lhs_low, rhs_low);
2959 __ Blt(TMP, AT, label);
2960 break;
2961 case kCondGE:
2962 __ Blt(rhs_high, lhs_high, label);
2963 __ Slt(TMP, lhs_high, rhs_high);
2964 __ Sltu(AT, lhs_low, rhs_low);
2965 __ Or(TMP, TMP, AT);
2966 __ Beqz(TMP, label);
2967 break;
2968 case kCondLE:
2969 __ Blt(lhs_high, rhs_high, label);
2970 __ Slt(TMP, rhs_high, lhs_high);
2971 __ Sltu(AT, rhs_low, lhs_low);
2972 __ Or(TMP, TMP, AT);
2973 __ Beqz(TMP, label);
2974 break;
2975 case kCondGT:
2976 __ Blt(rhs_high, lhs_high, label);
2977 __ Slt(TMP, lhs_high, rhs_high);
2978 __ Sltu(AT, rhs_low, lhs_low);
2979 __ Blt(TMP, AT, label);
2980 break;
2981 case kCondB:
2982 __ Bltu(lhs_high, rhs_high, label);
2983 __ Sltu(TMP, rhs_high, lhs_high);
2984 __ Sltu(AT, lhs_low, rhs_low);
2985 __ Blt(TMP, AT, label);
2986 break;
2987 case kCondAE:
2988 __ Bltu(rhs_high, lhs_high, label);
2989 __ Sltu(TMP, lhs_high, rhs_high);
2990 __ Sltu(AT, lhs_low, rhs_low);
2991 __ Or(TMP, TMP, AT);
2992 __ Beqz(TMP, label);
2993 break;
2994 case kCondBE:
2995 __ Bltu(lhs_high, rhs_high, label);
2996 __ Sltu(TMP, rhs_high, lhs_high);
2997 __ Sltu(AT, rhs_low, lhs_low);
2998 __ Or(TMP, TMP, AT);
2999 __ Beqz(TMP, label);
3000 break;
3001 case kCondA:
3002 __ Bltu(rhs_high, lhs_high, label);
3003 __ Sltu(TMP, lhs_high, rhs_high);
3004 __ Sltu(AT, rhs_low, lhs_low);
3005 __ Blt(TMP, AT, label);
3006 break;
3007 }
3008 }
3009}
3010
3011void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3012 bool gt_bias,
3013 Primitive::Type type,
3014 LocationSummary* locations,
3015 MipsLabel* label) {
3016 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3017 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3018 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3019 if (type == Primitive::kPrimFloat) {
3020 if (isR6) {
3021 switch (cond) {
3022 case kCondEQ:
3023 __ CmpEqS(FTMP, lhs, rhs);
3024 __ Bc1nez(FTMP, label);
3025 break;
3026 case kCondNE:
3027 __ CmpEqS(FTMP, lhs, rhs);
3028 __ Bc1eqz(FTMP, label);
3029 break;
3030 case kCondLT:
3031 if (gt_bias) {
3032 __ CmpLtS(FTMP, lhs, rhs);
3033 } else {
3034 __ CmpUltS(FTMP, lhs, rhs);
3035 }
3036 __ Bc1nez(FTMP, label);
3037 break;
3038 case kCondLE:
3039 if (gt_bias) {
3040 __ CmpLeS(FTMP, lhs, rhs);
3041 } else {
3042 __ CmpUleS(FTMP, lhs, rhs);
3043 }
3044 __ Bc1nez(FTMP, label);
3045 break;
3046 case kCondGT:
3047 if (gt_bias) {
3048 __ CmpUltS(FTMP, rhs, lhs);
3049 } else {
3050 __ CmpLtS(FTMP, rhs, lhs);
3051 }
3052 __ Bc1nez(FTMP, label);
3053 break;
3054 case kCondGE:
3055 if (gt_bias) {
3056 __ CmpUleS(FTMP, rhs, lhs);
3057 } else {
3058 __ CmpLeS(FTMP, rhs, lhs);
3059 }
3060 __ Bc1nez(FTMP, label);
3061 break;
3062 default:
3063 LOG(FATAL) << "Unexpected non-floating-point condition";
3064 }
3065 } else {
3066 switch (cond) {
3067 case kCondEQ:
3068 __ CeqS(0, lhs, rhs);
3069 __ Bc1t(0, label);
3070 break;
3071 case kCondNE:
3072 __ CeqS(0, lhs, rhs);
3073 __ Bc1f(0, label);
3074 break;
3075 case kCondLT:
3076 if (gt_bias) {
3077 __ ColtS(0, lhs, rhs);
3078 } else {
3079 __ CultS(0, lhs, rhs);
3080 }
3081 __ Bc1t(0, label);
3082 break;
3083 case kCondLE:
3084 if (gt_bias) {
3085 __ ColeS(0, lhs, rhs);
3086 } else {
3087 __ CuleS(0, lhs, rhs);
3088 }
3089 __ Bc1t(0, label);
3090 break;
3091 case kCondGT:
3092 if (gt_bias) {
3093 __ CultS(0, rhs, lhs);
3094 } else {
3095 __ ColtS(0, rhs, lhs);
3096 }
3097 __ Bc1t(0, label);
3098 break;
3099 case kCondGE:
3100 if (gt_bias) {
3101 __ CuleS(0, rhs, lhs);
3102 } else {
3103 __ ColeS(0, rhs, lhs);
3104 }
3105 __ Bc1t(0, label);
3106 break;
3107 default:
3108 LOG(FATAL) << "Unexpected non-floating-point condition";
3109 }
3110 }
3111 } else {
3112 DCHECK_EQ(type, Primitive::kPrimDouble);
3113 if (isR6) {
3114 switch (cond) {
3115 case kCondEQ:
3116 __ CmpEqD(FTMP, lhs, rhs);
3117 __ Bc1nez(FTMP, label);
3118 break;
3119 case kCondNE:
3120 __ CmpEqD(FTMP, lhs, rhs);
3121 __ Bc1eqz(FTMP, label);
3122 break;
3123 case kCondLT:
3124 if (gt_bias) {
3125 __ CmpLtD(FTMP, lhs, rhs);
3126 } else {
3127 __ CmpUltD(FTMP, lhs, rhs);
3128 }
3129 __ Bc1nez(FTMP, label);
3130 break;
3131 case kCondLE:
3132 if (gt_bias) {
3133 __ CmpLeD(FTMP, lhs, rhs);
3134 } else {
3135 __ CmpUleD(FTMP, lhs, rhs);
3136 }
3137 __ Bc1nez(FTMP, label);
3138 break;
3139 case kCondGT:
3140 if (gt_bias) {
3141 __ CmpUltD(FTMP, rhs, lhs);
3142 } else {
3143 __ CmpLtD(FTMP, rhs, lhs);
3144 }
3145 __ Bc1nez(FTMP, label);
3146 break;
3147 case kCondGE:
3148 if (gt_bias) {
3149 __ CmpUleD(FTMP, rhs, lhs);
3150 } else {
3151 __ CmpLeD(FTMP, rhs, lhs);
3152 }
3153 __ Bc1nez(FTMP, label);
3154 break;
3155 default:
3156 LOG(FATAL) << "Unexpected non-floating-point condition";
3157 }
3158 } else {
3159 switch (cond) {
3160 case kCondEQ:
3161 __ CeqD(0, lhs, rhs);
3162 __ Bc1t(0, label);
3163 break;
3164 case kCondNE:
3165 __ CeqD(0, lhs, rhs);
3166 __ Bc1f(0, label);
3167 break;
3168 case kCondLT:
3169 if (gt_bias) {
3170 __ ColtD(0, lhs, rhs);
3171 } else {
3172 __ CultD(0, lhs, rhs);
3173 }
3174 __ Bc1t(0, label);
3175 break;
3176 case kCondLE:
3177 if (gt_bias) {
3178 __ ColeD(0, lhs, rhs);
3179 } else {
3180 __ CuleD(0, lhs, rhs);
3181 }
3182 __ Bc1t(0, label);
3183 break;
3184 case kCondGT:
3185 if (gt_bias) {
3186 __ CultD(0, rhs, lhs);
3187 } else {
3188 __ ColtD(0, rhs, lhs);
3189 }
3190 __ Bc1t(0, label);
3191 break;
3192 case kCondGE:
3193 if (gt_bias) {
3194 __ CuleD(0, rhs, lhs);
3195 } else {
3196 __ ColeD(0, rhs, lhs);
3197 }
3198 __ Bc1t(0, label);
3199 break;
3200 default:
3201 LOG(FATAL) << "Unexpected non-floating-point condition";
3202 }
3203 }
3204 }
3205}
3206
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003207void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003208 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003209 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003210 MipsLabel* false_target) {
3211 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003212
David Brazdil0debae72015-11-12 18:37:00 +00003213 if (true_target == nullptr && false_target == nullptr) {
3214 // Nothing to do. The code always falls through.
3215 return;
3216 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003217 // Constant condition, statically compared against "true" (integer value 1).
3218 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003219 if (true_target != nullptr) {
3220 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003221 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003222 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003223 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003224 if (false_target != nullptr) {
3225 __ B(false_target);
3226 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003227 }
David Brazdil0debae72015-11-12 18:37:00 +00003228 return;
3229 }
3230
3231 // The following code generates these patterns:
3232 // (1) true_target == nullptr && false_target != nullptr
3233 // - opposite condition true => branch to false_target
3234 // (2) true_target != nullptr && false_target == nullptr
3235 // - condition true => branch to true_target
3236 // (3) true_target != nullptr && false_target != nullptr
3237 // - condition true => branch to true_target
3238 // - branch to false_target
3239 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003240 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003241 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003242 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003243 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003244 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3245 } else {
3246 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3247 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003248 } else {
3249 // The condition instruction has not been materialized, use its inputs as
3250 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003251 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003252 Primitive::Type type = condition->InputAt(0)->GetType();
3253 LocationSummary* locations = cond->GetLocations();
3254 IfCondition if_cond = condition->GetCondition();
3255 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003256
David Brazdil0debae72015-11-12 18:37:00 +00003257 if (true_target == nullptr) {
3258 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003259 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003260 }
3261
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003262 switch (type) {
3263 default:
3264 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3265 break;
3266 case Primitive::kPrimLong:
3267 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3268 break;
3269 case Primitive::kPrimFloat:
3270 case Primitive::kPrimDouble:
3271 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3272 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003273 }
3274 }
David Brazdil0debae72015-11-12 18:37:00 +00003275
3276 // If neither branch falls through (case 3), the conditional branch to `true_target`
3277 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3278 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003279 __ B(false_target);
3280 }
3281}
3282
3283void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3284 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003285 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003286 locations->SetInAt(0, Location::RequiresRegister());
3287 }
3288}
3289
3290void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003291 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3292 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3293 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3294 nullptr : codegen_->GetLabelOf(true_successor);
3295 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3296 nullptr : codegen_->GetLabelOf(false_successor);
3297 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003298}
3299
3300void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3301 LocationSummary* locations = new (GetGraph()->GetArena())
3302 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003303 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003304 locations->SetInAt(0, Location::RequiresRegister());
3305 }
3306}
3307
3308void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003309 SlowPathCodeMIPS* slow_path =
3310 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003311 GenerateTestAndBranch(deoptimize,
3312 /* condition_input_index */ 0,
3313 slow_path->GetEntryLabel(),
3314 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003315}
3316
David Brazdil74eb1b22015-12-14 11:44:01 +00003317void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3318 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3319 if (Primitive::IsFloatingPointType(select->GetType())) {
3320 locations->SetInAt(0, Location::RequiresFpuRegister());
3321 locations->SetInAt(1, Location::RequiresFpuRegister());
3322 } else {
3323 locations->SetInAt(0, Location::RequiresRegister());
3324 locations->SetInAt(1, Location::RequiresRegister());
3325 }
3326 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3327 locations->SetInAt(2, Location::RequiresRegister());
3328 }
3329 locations->SetOut(Location::SameAsFirstInput());
3330}
3331
3332void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3333 LocationSummary* locations = select->GetLocations();
3334 MipsLabel false_target;
3335 GenerateTestAndBranch(select,
3336 /* condition_input_index */ 2,
3337 /* true_target */ nullptr,
3338 &false_target);
3339 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3340 __ Bind(&false_target);
3341}
3342
David Srbecky0cf44932015-12-09 14:09:59 +00003343void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3344 new (GetGraph()->GetArena()) LocationSummary(info);
3345}
3346
David Srbeckyd28f4a02016-03-14 17:14:24 +00003347void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3348 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003349}
3350
3351void CodeGeneratorMIPS::GenerateNop() {
3352 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003353}
3354
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003355void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3356 Primitive::Type field_type = field_info.GetFieldType();
3357 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3358 bool generate_volatile = field_info.IsVolatile() && is_wide;
3359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3360 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3361
3362 locations->SetInAt(0, Location::RequiresRegister());
3363 if (generate_volatile) {
3364 InvokeRuntimeCallingConvention calling_convention;
3365 // need A0 to hold base + offset
3366 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3367 if (field_type == Primitive::kPrimLong) {
3368 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3369 } else {
3370 locations->SetOut(Location::RequiresFpuRegister());
3371 // Need some temp core regs since FP results are returned in core registers
3372 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3373 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3374 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3375 }
3376 } else {
3377 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3378 locations->SetOut(Location::RequiresFpuRegister());
3379 } else {
3380 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3381 }
3382 }
3383}
3384
3385void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3386 const FieldInfo& field_info,
3387 uint32_t dex_pc) {
3388 Primitive::Type type = field_info.GetFieldType();
3389 LocationSummary* locations = instruction->GetLocations();
3390 Register obj = locations->InAt(0).AsRegister<Register>();
3391 LoadOperandType load_type = kLoadUnsignedByte;
3392 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003393 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003394
3395 switch (type) {
3396 case Primitive::kPrimBoolean:
3397 load_type = kLoadUnsignedByte;
3398 break;
3399 case Primitive::kPrimByte:
3400 load_type = kLoadSignedByte;
3401 break;
3402 case Primitive::kPrimShort:
3403 load_type = kLoadSignedHalfword;
3404 break;
3405 case Primitive::kPrimChar:
3406 load_type = kLoadUnsignedHalfword;
3407 break;
3408 case Primitive::kPrimInt:
3409 case Primitive::kPrimFloat:
3410 case Primitive::kPrimNot:
3411 load_type = kLoadWord;
3412 break;
3413 case Primitive::kPrimLong:
3414 case Primitive::kPrimDouble:
3415 load_type = kLoadDoubleword;
3416 break;
3417 case Primitive::kPrimVoid:
3418 LOG(FATAL) << "Unreachable type " << type;
3419 UNREACHABLE();
3420 }
3421
3422 if (is_volatile && load_type == kLoadDoubleword) {
3423 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003424 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003425 // Do implicit Null check
3426 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3427 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3428 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3429 instruction,
3430 dex_pc,
3431 nullptr,
3432 IsDirectEntrypoint(kQuickA64Load));
3433 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3434 if (type == Primitive::kPrimDouble) {
3435 // Need to move to FP regs since FP results are returned in core registers.
3436 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
3437 locations->Out().AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003438 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3439 locations->Out().AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003440 }
3441 } else {
3442 if (!Primitive::IsFloatingPointType(type)) {
3443 Register dst;
3444 if (type == Primitive::kPrimLong) {
3445 DCHECK(locations->Out().IsRegisterPair());
3446 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003447 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3448 if (obj == dst) {
3449 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3450 codegen_->MaybeRecordImplicitNullCheck(instruction);
3451 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3452 } else {
3453 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3454 codegen_->MaybeRecordImplicitNullCheck(instruction);
3455 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3456 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003457 } else {
3458 DCHECK(locations->Out().IsRegister());
3459 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003460 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003461 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003462 } else {
3463 DCHECK(locations->Out().IsFpuRegister());
3464 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3465 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003466 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003467 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003468 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003469 }
3470 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003471 // Longs are handled earlier.
3472 if (type != Primitive::kPrimLong) {
3473 codegen_->MaybeRecordImplicitNullCheck(instruction);
3474 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003475 }
3476
3477 if (is_volatile) {
3478 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3479 }
3480}
3481
3482void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3483 Primitive::Type field_type = field_info.GetFieldType();
3484 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3485 bool generate_volatile = field_info.IsVolatile() && is_wide;
3486 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3487 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3488
3489 locations->SetInAt(0, Location::RequiresRegister());
3490 if (generate_volatile) {
3491 InvokeRuntimeCallingConvention calling_convention;
3492 // need A0 to hold base + offset
3493 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3494 if (field_type == Primitive::kPrimLong) {
3495 locations->SetInAt(1, Location::RegisterPairLocation(
3496 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3497 } else {
3498 locations->SetInAt(1, Location::RequiresFpuRegister());
3499 // Pass FP parameters in core registers.
3500 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3501 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3502 }
3503 } else {
3504 if (Primitive::IsFloatingPointType(field_type)) {
3505 locations->SetInAt(1, Location::RequiresFpuRegister());
3506 } else {
3507 locations->SetInAt(1, Location::RequiresRegister());
3508 }
3509 }
3510}
3511
3512void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3513 const FieldInfo& field_info,
3514 uint32_t dex_pc) {
3515 Primitive::Type type = field_info.GetFieldType();
3516 LocationSummary* locations = instruction->GetLocations();
3517 Register obj = locations->InAt(0).AsRegister<Register>();
3518 StoreOperandType store_type = kStoreByte;
3519 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003520 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003521
3522 switch (type) {
3523 case Primitive::kPrimBoolean:
3524 case Primitive::kPrimByte:
3525 store_type = kStoreByte;
3526 break;
3527 case Primitive::kPrimShort:
3528 case Primitive::kPrimChar:
3529 store_type = kStoreHalfword;
3530 break;
3531 case Primitive::kPrimInt:
3532 case Primitive::kPrimFloat:
3533 case Primitive::kPrimNot:
3534 store_type = kStoreWord;
3535 break;
3536 case Primitive::kPrimLong:
3537 case Primitive::kPrimDouble:
3538 store_type = kStoreDoubleword;
3539 break;
3540 case Primitive::kPrimVoid:
3541 LOG(FATAL) << "Unreachable type " << type;
3542 UNREACHABLE();
3543 }
3544
3545 if (is_volatile) {
3546 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3547 }
3548
3549 if (is_volatile && store_type == kStoreDoubleword) {
3550 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003551 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003552 // Do implicit Null check.
3553 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3554 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3555 if (type == Primitive::kPrimDouble) {
3556 // Pass FP parameters in core registers.
3557 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
3558 locations->InAt(1).AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003559 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3560 locations->InAt(1).AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003561 }
3562 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3563 instruction,
3564 dex_pc,
3565 nullptr,
3566 IsDirectEntrypoint(kQuickA64Store));
3567 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3568 } else {
3569 if (!Primitive::IsFloatingPointType(type)) {
3570 Register src;
3571 if (type == Primitive::kPrimLong) {
3572 DCHECK(locations->InAt(1).IsRegisterPair());
3573 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003574 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3575 __ StoreToOffset(kStoreWord, src, obj, offset);
3576 codegen_->MaybeRecordImplicitNullCheck(instruction);
3577 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003578 } else {
3579 DCHECK(locations->InAt(1).IsRegister());
3580 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003581 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003582 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003583 } else {
3584 DCHECK(locations->InAt(1).IsFpuRegister());
3585 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3586 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003587 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003588 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003589 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003590 }
3591 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003592 // Longs are handled earlier.
3593 if (type != Primitive::kPrimLong) {
3594 codegen_->MaybeRecordImplicitNullCheck(instruction);
3595 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003596 }
3597
3598 // TODO: memory barriers?
3599 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3600 DCHECK(locations->InAt(1).IsRegister());
3601 Register src = locations->InAt(1).AsRegister<Register>();
3602 codegen_->MarkGCCard(obj, src);
3603 }
3604
3605 if (is_volatile) {
3606 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3607 }
3608}
3609
3610void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3611 HandleFieldGet(instruction, instruction->GetFieldInfo());
3612}
3613
3614void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3615 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3616}
3617
3618void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3619 HandleFieldSet(instruction, instruction->GetFieldInfo());
3620}
3621
3622void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3623 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3624}
3625
3626void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3627 LocationSummary::CallKind call_kind =
3628 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3629 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3630 locations->SetInAt(0, Location::RequiresRegister());
3631 locations->SetInAt(1, Location::RequiresRegister());
3632 // The output does overlap inputs.
3633 // Note that TypeCheckSlowPathMIPS uses this register too.
3634 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3635}
3636
3637void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3638 LocationSummary* locations = instruction->GetLocations();
3639 Register obj = locations->InAt(0).AsRegister<Register>();
3640 Register cls = locations->InAt(1).AsRegister<Register>();
3641 Register out = locations->Out().AsRegister<Register>();
3642
3643 MipsLabel done;
3644
3645 // Return 0 if `obj` is null.
3646 // TODO: Avoid this check if we know `obj` is not null.
3647 __ Move(out, ZERO);
3648 __ Beqz(obj, &done);
3649
3650 // Compare the class of `obj` with `cls`.
3651 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3652 if (instruction->IsExactCheck()) {
3653 // Classes must be equal for the instanceof to succeed.
3654 __ Xor(out, out, cls);
3655 __ Sltiu(out, out, 1);
3656 } else {
3657 // If the classes are not equal, we go into a slow path.
3658 DCHECK(locations->OnlyCallsOnSlowPath());
3659 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3660 codegen_->AddSlowPath(slow_path);
3661 __ Bne(out, cls, slow_path->GetEntryLabel());
3662 __ LoadConst32(out, 1);
3663 __ Bind(slow_path->GetExitLabel());
3664 }
3665
3666 __ Bind(&done);
3667}
3668
3669void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3670 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3671 locations->SetOut(Location::ConstantLocation(constant));
3672}
3673
3674void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3675 // Will be generated at use site.
3676}
3677
3678void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3680 locations->SetOut(Location::ConstantLocation(constant));
3681}
3682
3683void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3684 // Will be generated at use site.
3685}
3686
3687void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3688 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3689 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3690}
3691
3692void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3693 HandleInvoke(invoke);
3694 // The register T0 is required to be used for the hidden argument in
3695 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3696 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3697}
3698
3699void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3700 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3701 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3702 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3703 invoke->GetImtIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
3704 Location receiver = invoke->GetLocations()->InAt(0);
3705 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3706 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3707
3708 // Set the hidden argument.
3709 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3710 invoke->GetDexMethodIndex());
3711
3712 // temp = object->GetClass();
3713 if (receiver.IsStackSlot()) {
3714 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3715 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3716 } else {
3717 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3718 }
3719 codegen_->MaybeRecordImplicitNullCheck(invoke);
3720 // temp = temp->GetImtEntryAt(method_offset);
3721 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3722 // T9 = temp->GetEntryPoint();
3723 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3724 // T9();
3725 __ Jalr(T9);
3726 __ Nop();
3727 DCHECK(!codegen_->IsLeafMethod());
3728 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3729}
3730
3731void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003732 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3733 if (intrinsic.TryDispatch(invoke)) {
3734 return;
3735 }
3736
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003737 HandleInvoke(invoke);
3738}
3739
3740void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003741 // Explicit clinit checks triggered by static invokes must have been pruned by
3742 // art::PrepareForRegisterAllocation.
3743 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003744
Chris Larsen701566a2015-10-27 15:29:13 -07003745 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3746 if (intrinsic.TryDispatch(invoke)) {
3747 return;
3748 }
3749
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003750 HandleInvoke(invoke);
3751}
3752
Chris Larsen701566a2015-10-27 15:29:13 -07003753static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003754 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003755 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3756 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003757 return true;
3758 }
3759 return false;
3760}
3761
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003762HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
3763 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
3764 // TODO: Implement other kinds.
3765 return HLoadString::LoadKind::kDexCacheViaMethod;
3766}
3767
Vladimir Markodc151b22015-10-15 18:02:30 +01003768HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3769 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3770 MethodReference target_method ATTRIBUTE_UNUSED) {
3771 switch (desired_dispatch_info.method_load_kind) {
3772 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3773 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3774 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3775 return HInvokeStaticOrDirect::DispatchInfo {
3776 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3777 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3778 0u,
3779 0u
3780 };
3781 default:
3782 break;
3783 }
3784 switch (desired_dispatch_info.code_ptr_location) {
3785 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3786 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3787 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3788 return HInvokeStaticOrDirect::DispatchInfo {
3789 desired_dispatch_info.method_load_kind,
3790 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3791 desired_dispatch_info.method_load_data,
3792 0u
3793 };
3794 default:
3795 return desired_dispatch_info;
3796 }
3797}
3798
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003799void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3800 // All registers are assumed to be correctly set up per the calling convention.
3801
3802 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3803 switch (invoke->GetMethodLoadKind()) {
3804 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3805 // temp = thread->string_init_entrypoint
3806 __ LoadFromOffset(kLoadWord,
3807 temp.AsRegister<Register>(),
3808 TR,
3809 invoke->GetStringInitOffset());
3810 break;
3811 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003812 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003813 break;
3814 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3815 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3816 break;
3817 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003818 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003819 // TODO: Implement these types.
3820 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3821 LOG(FATAL) << "Unsupported";
3822 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003823 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003824 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003825 Register reg = temp.AsRegister<Register>();
3826 Register method_reg;
3827 if (current_method.IsRegister()) {
3828 method_reg = current_method.AsRegister<Register>();
3829 } else {
3830 // TODO: use the appropriate DCHECK() here if possible.
3831 // DCHECK(invoke->GetLocations()->Intrinsified());
3832 DCHECK(!current_method.IsValid());
3833 method_reg = reg;
3834 __ Lw(reg, SP, kCurrentMethodStackOffset);
3835 }
3836
3837 // temp = temp->dex_cache_resolved_methods_;
3838 __ LoadFromOffset(kLoadWord,
3839 reg,
3840 method_reg,
3841 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003842 // temp = temp[index_in_cache];
3843 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3844 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003845 __ LoadFromOffset(kLoadWord,
3846 reg,
3847 reg,
3848 CodeGenerator::GetCachePointerOffset(index_in_cache));
3849 break;
3850 }
3851 }
3852
3853 switch (invoke->GetCodePtrLocation()) {
3854 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3855 __ Jalr(&frame_entry_label_, T9);
3856 break;
3857 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3858 // LR = invoke->GetDirectCodePtr();
3859 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3860 // LR()
3861 __ Jalr(T9);
3862 __ Nop();
3863 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003864 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003865 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3866 // TODO: Implement these types.
3867 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3868 LOG(FATAL) << "Unsupported";
3869 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003870 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3871 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01003872 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003873 T9,
3874 callee_method.AsRegister<Register>(),
3875 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3876 kMipsWordSize).Int32Value());
3877 // T9()
3878 __ Jalr(T9);
3879 __ Nop();
3880 break;
3881 }
3882 DCHECK(!IsLeafMethod());
3883}
3884
3885void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003886 // Explicit clinit checks triggered by static invokes must have been pruned by
3887 // art::PrepareForRegisterAllocation.
3888 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003889
3890 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3891 return;
3892 }
3893
3894 LocationSummary* locations = invoke->GetLocations();
3895 codegen_->GenerateStaticOrDirectCall(invoke,
3896 locations->HasTemps()
3897 ? locations->GetTemp(0)
3898 : Location::NoLocation());
3899 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3900}
3901
Chris Larsen3acee732015-11-18 13:31:08 -08003902void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003903 LocationSummary* locations = invoke->GetLocations();
3904 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08003905 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003906 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3907 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
3908 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3909 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3910
3911 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08003912 DCHECK(receiver.IsRegister());
3913 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3914 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003915 // temp = temp->GetMethodAt(method_offset);
3916 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3917 // T9 = temp->GetEntryPoint();
3918 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3919 // T9();
3920 __ Jalr(T9);
3921 __ Nop();
Chris Larsen3acee732015-11-18 13:31:08 -08003922}
3923
3924void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3925 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3926 return;
3927 }
3928
3929 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003930 DCHECK(!codegen_->IsLeafMethod());
3931 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3932}
3933
3934void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01003935 InvokeRuntimeCallingConvention calling_convention;
3936 CodeGenerator::CreateLoadClassLocationSummary(
3937 cls,
3938 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
3939 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003940}
3941
3942void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
3943 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01003944 if (cls->NeedsAccessCheck()) {
3945 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3946 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3947 cls,
3948 cls->GetDexPc(),
3949 nullptr,
3950 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00003951 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01003952 return;
3953 }
3954
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003955 Register out = locations->Out().AsRegister<Register>();
3956 Register current_method = locations->InAt(0).AsRegister<Register>();
3957 if (cls->IsReferrersClass()) {
3958 DCHECK(!cls->CanCallRuntime());
3959 DCHECK(!cls->MustGenerateClinitCheck());
3960 __ LoadFromOffset(kLoadWord, out, current_method,
3961 ArtMethod::DeclaringClassOffset().Int32Value());
3962 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003963 __ LoadFromOffset(kLoadWord, out, current_method,
3964 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
3965 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003966
3967 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3968 DCHECK(cls->CanCallRuntime());
3969 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3970 cls,
3971 cls,
3972 cls->GetDexPc(),
3973 cls->MustGenerateClinitCheck());
3974 codegen_->AddSlowPath(slow_path);
3975 if (!cls->IsInDexCache()) {
3976 __ Beqz(out, slow_path->GetEntryLabel());
3977 }
3978 if (cls->MustGenerateClinitCheck()) {
3979 GenerateClassInitializationCheck(slow_path, out);
3980 } else {
3981 __ Bind(slow_path->GetExitLabel());
3982 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003983 }
3984 }
3985}
3986
3987static int32_t GetExceptionTlsOffset() {
3988 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
3989}
3990
3991void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
3992 LocationSummary* locations =
3993 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3994 locations->SetOut(Location::RequiresRegister());
3995}
3996
3997void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
3998 Register out = load->GetLocations()->Out().AsRegister<Register>();
3999 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4000}
4001
4002void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4003 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4004}
4005
4006void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4007 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4008}
4009
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004010void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004011 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
4012 ? LocationSummary::kCallOnSlowPath
4013 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004014 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004015 locations->SetInAt(0, Location::RequiresRegister());
4016 locations->SetOut(Location::RequiresRegister());
4017}
4018
4019void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004020 LocationSummary* locations = load->GetLocations();
4021 Register out = locations->Out().AsRegister<Register>();
4022 Register current_method = locations->InAt(0).AsRegister<Register>();
4023 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4024 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4025 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004026
4027 if (!load->IsInDexCache()) {
4028 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4029 codegen_->AddSlowPath(slow_path);
4030 __ Beqz(out, slow_path->GetEntryLabel());
4031 __ Bind(slow_path->GetExitLabel());
4032 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004033}
4034
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004035void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4036 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4037 locations->SetOut(Location::ConstantLocation(constant));
4038}
4039
4040void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4041 // Will be generated at use site.
4042}
4043
4044void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4045 LocationSummary* locations =
4046 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4047 InvokeRuntimeCallingConvention calling_convention;
4048 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4049}
4050
4051void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4052 if (instruction->IsEnter()) {
4053 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4054 instruction,
4055 instruction->GetDexPc(),
4056 nullptr,
4057 IsDirectEntrypoint(kQuickLockObject));
4058 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4059 } else {
4060 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4061 instruction,
4062 instruction->GetDexPc(),
4063 nullptr,
4064 IsDirectEntrypoint(kQuickUnlockObject));
4065 }
4066 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4067}
4068
4069void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4070 LocationSummary* locations =
4071 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4072 switch (mul->GetResultType()) {
4073 case Primitive::kPrimInt:
4074 case Primitive::kPrimLong:
4075 locations->SetInAt(0, Location::RequiresRegister());
4076 locations->SetInAt(1, Location::RequiresRegister());
4077 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4078 break;
4079
4080 case Primitive::kPrimFloat:
4081 case Primitive::kPrimDouble:
4082 locations->SetInAt(0, Location::RequiresFpuRegister());
4083 locations->SetInAt(1, Location::RequiresFpuRegister());
4084 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4085 break;
4086
4087 default:
4088 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4089 }
4090}
4091
4092void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4093 Primitive::Type type = instruction->GetType();
4094 LocationSummary* locations = instruction->GetLocations();
4095 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4096
4097 switch (type) {
4098 case Primitive::kPrimInt: {
4099 Register dst = locations->Out().AsRegister<Register>();
4100 Register lhs = locations->InAt(0).AsRegister<Register>();
4101 Register rhs = locations->InAt(1).AsRegister<Register>();
4102
4103 if (isR6) {
4104 __ MulR6(dst, lhs, rhs);
4105 } else {
4106 __ MulR2(dst, lhs, rhs);
4107 }
4108 break;
4109 }
4110 case Primitive::kPrimLong: {
4111 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4112 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4113 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4114 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4115 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4116 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4117
4118 // Extra checks to protect caused by the existance of A1_A2.
4119 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4120 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4121 DCHECK_NE(dst_high, lhs_low);
4122 DCHECK_NE(dst_high, rhs_low);
4123
4124 // A_B * C_D
4125 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4126 // dst_lo: [ low(B*D) ]
4127 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4128
4129 if (isR6) {
4130 __ MulR6(TMP, lhs_high, rhs_low);
4131 __ MulR6(dst_high, lhs_low, rhs_high);
4132 __ Addu(dst_high, dst_high, TMP);
4133 __ MuhuR6(TMP, lhs_low, rhs_low);
4134 __ Addu(dst_high, dst_high, TMP);
4135 __ MulR6(dst_low, lhs_low, rhs_low);
4136 } else {
4137 __ MulR2(TMP, lhs_high, rhs_low);
4138 __ MulR2(dst_high, lhs_low, rhs_high);
4139 __ Addu(dst_high, dst_high, TMP);
4140 __ MultuR2(lhs_low, rhs_low);
4141 __ Mfhi(TMP);
4142 __ Addu(dst_high, dst_high, TMP);
4143 __ Mflo(dst_low);
4144 }
4145 break;
4146 }
4147 case Primitive::kPrimFloat:
4148 case Primitive::kPrimDouble: {
4149 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4150 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4151 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4152 if (type == Primitive::kPrimFloat) {
4153 __ MulS(dst, lhs, rhs);
4154 } else {
4155 __ MulD(dst, lhs, rhs);
4156 }
4157 break;
4158 }
4159 default:
4160 LOG(FATAL) << "Unexpected mul type " << type;
4161 }
4162}
4163
4164void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4165 LocationSummary* locations =
4166 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4167 switch (neg->GetResultType()) {
4168 case Primitive::kPrimInt:
4169 case Primitive::kPrimLong:
4170 locations->SetInAt(0, Location::RequiresRegister());
4171 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4172 break;
4173
4174 case Primitive::kPrimFloat:
4175 case Primitive::kPrimDouble:
4176 locations->SetInAt(0, Location::RequiresFpuRegister());
4177 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4178 break;
4179
4180 default:
4181 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4182 }
4183}
4184
4185void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4186 Primitive::Type type = instruction->GetType();
4187 LocationSummary* locations = instruction->GetLocations();
4188
4189 switch (type) {
4190 case Primitive::kPrimInt: {
4191 Register dst = locations->Out().AsRegister<Register>();
4192 Register src = locations->InAt(0).AsRegister<Register>();
4193 __ Subu(dst, ZERO, src);
4194 break;
4195 }
4196 case Primitive::kPrimLong: {
4197 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4198 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4199 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4200 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4201 __ Subu(dst_low, ZERO, src_low);
4202 __ Sltu(TMP, ZERO, dst_low);
4203 __ Subu(dst_high, ZERO, src_high);
4204 __ Subu(dst_high, dst_high, TMP);
4205 break;
4206 }
4207 case Primitive::kPrimFloat:
4208 case Primitive::kPrimDouble: {
4209 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4210 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4211 if (type == Primitive::kPrimFloat) {
4212 __ NegS(dst, src);
4213 } else {
4214 __ NegD(dst, src);
4215 }
4216 break;
4217 }
4218 default:
4219 LOG(FATAL) << "Unexpected neg type " << type;
4220 }
4221}
4222
4223void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4224 LocationSummary* locations =
4225 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4226 InvokeRuntimeCallingConvention calling_convention;
4227 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4228 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4229 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4230 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4231}
4232
4233void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4234 InvokeRuntimeCallingConvention calling_convention;
4235 Register current_method_register = calling_convention.GetRegisterAt(2);
4236 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4237 // Move an uint16_t value to a register.
4238 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4239 codegen_->InvokeRuntime(
4240 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4241 instruction,
4242 instruction->GetDexPc(),
4243 nullptr,
4244 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4245 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4246 void*, uint32_t, int32_t, ArtMethod*>();
4247}
4248
4249void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4250 LocationSummary* locations =
4251 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4252 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004253 if (instruction->IsStringAlloc()) {
4254 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4255 } else {
4256 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4257 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4258 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004259 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4260}
4261
4262void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004263 if (instruction->IsStringAlloc()) {
4264 // String is allocated through StringFactory. Call NewEmptyString entry point.
4265 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4266 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4267 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4268 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4269 __ Jalr(T9);
4270 __ Nop();
4271 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4272 } else {
4273 codegen_->InvokeRuntime(
4274 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4275 instruction,
4276 instruction->GetDexPc(),
4277 nullptr,
4278 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4279 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4280 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004281}
4282
4283void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4284 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4285 locations->SetInAt(0, Location::RequiresRegister());
4286 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4287}
4288
4289void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4290 Primitive::Type type = instruction->GetType();
4291 LocationSummary* locations = instruction->GetLocations();
4292
4293 switch (type) {
4294 case Primitive::kPrimInt: {
4295 Register dst = locations->Out().AsRegister<Register>();
4296 Register src = locations->InAt(0).AsRegister<Register>();
4297 __ Nor(dst, src, ZERO);
4298 break;
4299 }
4300
4301 case Primitive::kPrimLong: {
4302 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4303 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4304 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4305 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4306 __ Nor(dst_high, src_high, ZERO);
4307 __ Nor(dst_low, src_low, ZERO);
4308 break;
4309 }
4310
4311 default:
4312 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4313 }
4314}
4315
4316void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4318 locations->SetInAt(0, Location::RequiresRegister());
4319 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4320}
4321
4322void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4323 LocationSummary* locations = instruction->GetLocations();
4324 __ Xori(locations->Out().AsRegister<Register>(),
4325 locations->InAt(0).AsRegister<Register>(),
4326 1);
4327}
4328
4329void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4330 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4331 ? LocationSummary::kCallOnSlowPath
4332 : LocationSummary::kNoCall;
4333 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4334 locations->SetInAt(0, Location::RequiresRegister());
4335 if (instruction->HasUses()) {
4336 locations->SetOut(Location::SameAsFirstInput());
4337 }
4338}
4339
Calin Juravle2ae48182016-03-16 14:05:09 +00004340void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4341 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004342 return;
4343 }
4344 Location obj = instruction->GetLocations()->InAt(0);
4345
4346 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004347 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004348}
4349
Calin Juravle2ae48182016-03-16 14:05:09 +00004350void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004351 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004352 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004353
4354 Location obj = instruction->GetLocations()->InAt(0);
4355
4356 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4357}
4358
4359void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004360 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004361}
4362
4363void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4364 HandleBinaryOp(instruction);
4365}
4366
4367void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4368 HandleBinaryOp(instruction);
4369}
4370
4371void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4372 LOG(FATAL) << "Unreachable";
4373}
4374
4375void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4376 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4377}
4378
4379void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4380 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4381 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4382 if (location.IsStackSlot()) {
4383 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4384 } else if (location.IsDoubleStackSlot()) {
4385 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4386 }
4387 locations->SetOut(location);
4388}
4389
4390void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4391 ATTRIBUTE_UNUSED) {
4392 // Nothing to do, the parameter is already at its location.
4393}
4394
4395void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4396 LocationSummary* locations =
4397 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4398 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4399}
4400
4401void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4402 ATTRIBUTE_UNUSED) {
4403 // Nothing to do, the method is already at its location.
4404}
4405
4406void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4407 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4408 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4409 locations->SetInAt(i, Location::Any());
4410 }
4411 locations->SetOut(Location::Any());
4412}
4413
4414void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4415 LOG(FATAL) << "Unreachable";
4416}
4417
4418void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4419 Primitive::Type type = rem->GetResultType();
4420 LocationSummary::CallKind call_kind =
4421 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCall;
4422 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4423
4424 switch (type) {
4425 case Primitive::kPrimInt:
4426 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004427 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004428 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4429 break;
4430
4431 case Primitive::kPrimLong: {
4432 InvokeRuntimeCallingConvention calling_convention;
4433 locations->SetInAt(0, Location::RegisterPairLocation(
4434 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4435 locations->SetInAt(1, Location::RegisterPairLocation(
4436 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4437 locations->SetOut(calling_convention.GetReturnLocation(type));
4438 break;
4439 }
4440
4441 case Primitive::kPrimFloat:
4442 case Primitive::kPrimDouble: {
4443 InvokeRuntimeCallingConvention calling_convention;
4444 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4445 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4446 locations->SetOut(calling_convention.GetReturnLocation(type));
4447 break;
4448 }
4449
4450 default:
4451 LOG(FATAL) << "Unexpected rem type " << type;
4452 }
4453}
4454
4455void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4456 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004457
4458 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004459 case Primitive::kPrimInt:
4460 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004461 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004462 case Primitive::kPrimLong: {
4463 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
4464 instruction,
4465 instruction->GetDexPc(),
4466 nullptr,
4467 IsDirectEntrypoint(kQuickLmod));
4468 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4469 break;
4470 }
4471 case Primitive::kPrimFloat: {
4472 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
4473 instruction, instruction->GetDexPc(),
4474 nullptr,
4475 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00004476 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004477 break;
4478 }
4479 case Primitive::kPrimDouble: {
4480 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
4481 instruction, instruction->GetDexPc(),
4482 nullptr,
4483 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00004484 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004485 break;
4486 }
4487 default:
4488 LOG(FATAL) << "Unexpected rem type " << type;
4489 }
4490}
4491
4492void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4493 memory_barrier->SetLocations(nullptr);
4494}
4495
4496void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4497 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4498}
4499
4500void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
4501 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4502 Primitive::Type return_type = ret->InputAt(0)->GetType();
4503 locations->SetInAt(0, MipsReturnLocation(return_type));
4504}
4505
4506void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4507 codegen_->GenerateFrameExit();
4508}
4509
4510void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
4511 ret->SetLocations(nullptr);
4512}
4513
4514void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4515 codegen_->GenerateFrameExit();
4516}
4517
Alexey Frunze92d90602015-12-18 18:16:36 -08004518void LocationsBuilderMIPS::VisitRor(HRor* ror) {
4519 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004520}
4521
Alexey Frunze92d90602015-12-18 18:16:36 -08004522void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
4523 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004524}
4525
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004526void LocationsBuilderMIPS::VisitShl(HShl* shl) {
4527 HandleShift(shl);
4528}
4529
4530void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
4531 HandleShift(shl);
4532}
4533
4534void LocationsBuilderMIPS::VisitShr(HShr* shr) {
4535 HandleShift(shr);
4536}
4537
4538void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
4539 HandleShift(shr);
4540}
4541
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004542void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
4543 HandleBinaryOp(instruction);
4544}
4545
4546void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
4547 HandleBinaryOp(instruction);
4548}
4549
4550void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4551 HandleFieldGet(instruction, instruction->GetFieldInfo());
4552}
4553
4554void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4555 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4556}
4557
4558void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4559 HandleFieldSet(instruction, instruction->GetFieldInfo());
4560}
4561
4562void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4563 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4564}
4565
4566void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
4567 HUnresolvedInstanceFieldGet* instruction) {
4568 FieldAccessCallingConventionMIPS calling_convention;
4569 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4570 instruction->GetFieldType(),
4571 calling_convention);
4572}
4573
4574void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
4575 HUnresolvedInstanceFieldGet* instruction) {
4576 FieldAccessCallingConventionMIPS calling_convention;
4577 codegen_->GenerateUnresolvedFieldAccess(instruction,
4578 instruction->GetFieldType(),
4579 instruction->GetFieldIndex(),
4580 instruction->GetDexPc(),
4581 calling_convention);
4582}
4583
4584void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4585 HUnresolvedInstanceFieldSet* instruction) {
4586 FieldAccessCallingConventionMIPS calling_convention;
4587 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4588 instruction->GetFieldType(),
4589 calling_convention);
4590}
4591
4592void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4593 HUnresolvedInstanceFieldSet* instruction) {
4594 FieldAccessCallingConventionMIPS calling_convention;
4595 codegen_->GenerateUnresolvedFieldAccess(instruction,
4596 instruction->GetFieldType(),
4597 instruction->GetFieldIndex(),
4598 instruction->GetDexPc(),
4599 calling_convention);
4600}
4601
4602void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4603 HUnresolvedStaticFieldGet* instruction) {
4604 FieldAccessCallingConventionMIPS calling_convention;
4605 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4606 instruction->GetFieldType(),
4607 calling_convention);
4608}
4609
4610void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4611 HUnresolvedStaticFieldGet* instruction) {
4612 FieldAccessCallingConventionMIPS calling_convention;
4613 codegen_->GenerateUnresolvedFieldAccess(instruction,
4614 instruction->GetFieldType(),
4615 instruction->GetFieldIndex(),
4616 instruction->GetDexPc(),
4617 calling_convention);
4618}
4619
4620void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4621 HUnresolvedStaticFieldSet* instruction) {
4622 FieldAccessCallingConventionMIPS calling_convention;
4623 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4624 instruction->GetFieldType(),
4625 calling_convention);
4626}
4627
4628void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4629 HUnresolvedStaticFieldSet* instruction) {
4630 FieldAccessCallingConventionMIPS calling_convention;
4631 codegen_->GenerateUnresolvedFieldAccess(instruction,
4632 instruction->GetFieldType(),
4633 instruction->GetFieldIndex(),
4634 instruction->GetDexPc(),
4635 calling_convention);
4636}
4637
4638void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4639 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4640}
4641
4642void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4643 HBasicBlock* block = instruction->GetBlock();
4644 if (block->GetLoopInformation() != nullptr) {
4645 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4646 // The back edge will generate the suspend check.
4647 return;
4648 }
4649 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4650 // The goto will generate the suspend check.
4651 return;
4652 }
4653 GenerateSuspendCheck(instruction, nullptr);
4654}
4655
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004656void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4657 LocationSummary* locations =
4658 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4659 InvokeRuntimeCallingConvention calling_convention;
4660 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4661}
4662
4663void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4664 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4665 instruction,
4666 instruction->GetDexPc(),
4667 nullptr,
4668 IsDirectEntrypoint(kQuickDeliverException));
4669 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4670}
4671
4672void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4673 Primitive::Type input_type = conversion->GetInputType();
4674 Primitive::Type result_type = conversion->GetResultType();
4675 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004676 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004677
4678 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4679 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4680 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4681 }
4682
4683 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004684 if (!isR6 &&
4685 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4686 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004687 call_kind = LocationSummary::kCall;
4688 }
4689
4690 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4691
4692 if (call_kind == LocationSummary::kNoCall) {
4693 if (Primitive::IsFloatingPointType(input_type)) {
4694 locations->SetInAt(0, Location::RequiresFpuRegister());
4695 } else {
4696 locations->SetInAt(0, Location::RequiresRegister());
4697 }
4698
4699 if (Primitive::IsFloatingPointType(result_type)) {
4700 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4701 } else {
4702 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4703 }
4704 } else {
4705 InvokeRuntimeCallingConvention calling_convention;
4706
4707 if (Primitive::IsFloatingPointType(input_type)) {
4708 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4709 } else {
4710 DCHECK_EQ(input_type, Primitive::kPrimLong);
4711 locations->SetInAt(0, Location::RegisterPairLocation(
4712 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4713 }
4714
4715 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4716 }
4717}
4718
4719void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4720 LocationSummary* locations = conversion->GetLocations();
4721 Primitive::Type result_type = conversion->GetResultType();
4722 Primitive::Type input_type = conversion->GetInputType();
4723 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004724 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4725 bool fpu_32bit = codegen_->GetInstructionSetFeatures().Is32BitFloatingPoint();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004726
4727 DCHECK_NE(input_type, result_type);
4728
4729 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4730 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4731 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4732 Register src = locations->InAt(0).AsRegister<Register>();
4733
4734 __ Move(dst_low, src);
4735 __ Sra(dst_high, src, 31);
4736 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4737 Register dst = locations->Out().AsRegister<Register>();
4738 Register src = (input_type == Primitive::kPrimLong)
4739 ? locations->InAt(0).AsRegisterPairLow<Register>()
4740 : locations->InAt(0).AsRegister<Register>();
4741
4742 switch (result_type) {
4743 case Primitive::kPrimChar:
4744 __ Andi(dst, src, 0xFFFF);
4745 break;
4746 case Primitive::kPrimByte:
4747 if (has_sign_extension) {
4748 __ Seb(dst, src);
4749 } else {
4750 __ Sll(dst, src, 24);
4751 __ Sra(dst, dst, 24);
4752 }
4753 break;
4754 case Primitive::kPrimShort:
4755 if (has_sign_extension) {
4756 __ Seh(dst, src);
4757 } else {
4758 __ Sll(dst, src, 16);
4759 __ Sra(dst, dst, 16);
4760 }
4761 break;
4762 case Primitive::kPrimInt:
4763 __ Move(dst, src);
4764 break;
4765
4766 default:
4767 LOG(FATAL) << "Unexpected type conversion from " << input_type
4768 << " to " << result_type;
4769 }
4770 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004771 if (input_type == Primitive::kPrimLong) {
4772 if (isR6) {
4773 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4774 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4775 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4776 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4777 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4778 __ Mtc1(src_low, FTMP);
4779 __ Mthc1(src_high, FTMP);
4780 if (result_type == Primitive::kPrimFloat) {
4781 __ Cvtsl(dst, FTMP);
4782 } else {
4783 __ Cvtdl(dst, FTMP);
4784 }
4785 } else {
4786 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4787 : QUICK_ENTRY_POINT(pL2d);
4788 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4789 : IsDirectEntrypoint(kQuickL2d);
4790 codegen_->InvokeRuntime(entry_offset,
4791 conversion,
4792 conversion->GetDexPc(),
4793 nullptr,
4794 direct);
4795 if (result_type == Primitive::kPrimFloat) {
4796 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4797 } else {
4798 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4799 }
4800 }
4801 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004802 Register src = locations->InAt(0).AsRegister<Register>();
4803 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4804 __ Mtc1(src, FTMP);
4805 if (result_type == Primitive::kPrimFloat) {
4806 __ Cvtsw(dst, FTMP);
4807 } else {
4808 __ Cvtdw(dst, FTMP);
4809 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004810 }
4811 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4812 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004813 if (result_type == Primitive::kPrimLong) {
4814 if (isR6) {
4815 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4816 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4817 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4818 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4819 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4820 MipsLabel truncate;
4821 MipsLabel done;
4822
4823 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4824 // value when the input is either a NaN or is outside of the range of the output type
4825 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4826 // the same result.
4827 //
4828 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4829 // value of the output type if the input is outside of the range after the truncation or
4830 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4831 // results. This matches the desired float/double-to-int/long conversion exactly.
4832 //
4833 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4834 //
4835 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4836 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4837 // even though it must be NAN2008=1 on R6.
4838 //
4839 // The code takes care of the different behaviors by first comparing the input to the
4840 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4841 // If the input is greater than or equal to the minimum, it procedes to the truncate
4842 // instruction, which will handle such an input the same way irrespective of NAN2008.
4843 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4844 // in order to return either zero or the minimum value.
4845 //
4846 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4847 // truncate instruction for MIPS64R6.
4848 if (input_type == Primitive::kPrimFloat) {
4849 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
4850 __ LoadConst32(TMP, min_val);
4851 __ Mtc1(TMP, FTMP);
4852 __ CmpLeS(FTMP, FTMP, src);
4853 } else {
4854 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
4855 __ LoadConst32(TMP, High32Bits(min_val));
4856 __ Mtc1(ZERO, FTMP);
4857 __ Mthc1(TMP, FTMP);
4858 __ CmpLeD(FTMP, FTMP, src);
4859 }
4860
4861 __ Bc1nez(FTMP, &truncate);
4862
4863 if (input_type == Primitive::kPrimFloat) {
4864 __ CmpEqS(FTMP, src, src);
4865 } else {
4866 __ CmpEqD(FTMP, src, src);
4867 }
4868 __ Move(dst_low, ZERO);
4869 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
4870 __ Mfc1(TMP, FTMP);
4871 __ And(dst_high, dst_high, TMP);
4872
4873 __ B(&done);
4874
4875 __ Bind(&truncate);
4876
4877 if (input_type == Primitive::kPrimFloat) {
4878 __ TruncLS(FTMP, src);
4879 } else {
4880 __ TruncLD(FTMP, src);
4881 }
4882 __ Mfc1(dst_low, FTMP);
4883 __ Mfhc1(dst_high, FTMP);
4884
4885 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004886 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004887 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
4888 : QUICK_ENTRY_POINT(pD2l);
4889 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
4890 : IsDirectEntrypoint(kQuickD2l);
4891 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
4892 if (input_type == Primitive::kPrimFloat) {
4893 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4894 } else {
4895 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4896 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004897 }
4898 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004899 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4900 Register dst = locations->Out().AsRegister<Register>();
4901 MipsLabel truncate;
4902 MipsLabel done;
4903
4904 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4905 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4906 // even though it must be NAN2008=1 on R6.
4907 //
4908 // For details see the large comment above for the truncation of float/double to long on R6.
4909 //
4910 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4911 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004912 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004913 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4914 __ LoadConst32(TMP, min_val);
4915 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004916 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004917 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4918 __ LoadConst32(TMP, High32Bits(min_val));
4919 __ Mtc1(ZERO, FTMP);
4920 if (fpu_32bit) {
4921 __ Mtc1(TMP, static_cast<FRegister>(FTMP + 1));
4922 } else {
4923 __ Mthc1(TMP, FTMP);
4924 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004925 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004926
4927 if (isR6) {
4928 if (input_type == Primitive::kPrimFloat) {
4929 __ CmpLeS(FTMP, FTMP, src);
4930 } else {
4931 __ CmpLeD(FTMP, FTMP, src);
4932 }
4933 __ Bc1nez(FTMP, &truncate);
4934
4935 if (input_type == Primitive::kPrimFloat) {
4936 __ CmpEqS(FTMP, src, src);
4937 } else {
4938 __ CmpEqD(FTMP, src, src);
4939 }
4940 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4941 __ Mfc1(TMP, FTMP);
4942 __ And(dst, dst, TMP);
4943 } else {
4944 if (input_type == Primitive::kPrimFloat) {
4945 __ ColeS(0, FTMP, src);
4946 } else {
4947 __ ColeD(0, FTMP, src);
4948 }
4949 __ Bc1t(0, &truncate);
4950
4951 if (input_type == Primitive::kPrimFloat) {
4952 __ CeqS(0, src, src);
4953 } else {
4954 __ CeqD(0, src, src);
4955 }
4956 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4957 __ Movf(dst, ZERO, 0);
4958 }
4959
4960 __ B(&done);
4961
4962 __ Bind(&truncate);
4963
4964 if (input_type == Primitive::kPrimFloat) {
4965 __ TruncWS(FTMP, src);
4966 } else {
4967 __ TruncWD(FTMP, src);
4968 }
4969 __ Mfc1(dst, FTMP);
4970
4971 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004972 }
4973 } else if (Primitive::IsFloatingPointType(result_type) &&
4974 Primitive::IsFloatingPointType(input_type)) {
4975 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4976 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4977 if (result_type == Primitive::kPrimFloat) {
4978 __ Cvtsd(dst, src);
4979 } else {
4980 __ Cvtds(dst, src);
4981 }
4982 } else {
4983 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4984 << " to " << result_type;
4985 }
4986}
4987
4988void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
4989 HandleShift(ushr);
4990}
4991
4992void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
4993 HandleShift(ushr);
4994}
4995
4996void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
4997 HandleBinaryOp(instruction);
4998}
4999
5000void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5001 HandleBinaryOp(instruction);
5002}
5003
5004void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5005 // Nothing to do, this should be removed during prepare for register allocator.
5006 LOG(FATAL) << "Unreachable";
5007}
5008
5009void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5010 // Nothing to do, this should be removed during prepare for register allocator.
5011 LOG(FATAL) << "Unreachable";
5012}
5013
5014void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005015 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005016}
5017
5018void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005019 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005020}
5021
5022void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005023 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005024}
5025
5026void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005027 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005028}
5029
5030void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005031 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005032}
5033
5034void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005035 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005036}
5037
5038void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005039 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005040}
5041
5042void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005043 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005044}
5045
5046void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005047 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005048}
5049
5050void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005051 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005052}
5053
5054void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005055 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005056}
5057
5058void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005059 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005060}
5061
5062void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005063 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005064}
5065
5066void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005067 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005068}
5069
5070void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005071 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005072}
5073
5074void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005075 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005076}
5077
5078void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005079 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005080}
5081
5082void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005083 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005084}
5085
5086void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005087 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005088}
5089
5090void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005091 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005092}
5093
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005094void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5095 LocationSummary* locations =
5096 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5097 locations->SetInAt(0, Location::RequiresRegister());
5098}
5099
5100void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5101 int32_t lower_bound = switch_instr->GetStartValue();
5102 int32_t num_entries = switch_instr->GetNumEntries();
5103 LocationSummary* locations = switch_instr->GetLocations();
5104 Register value_reg = locations->InAt(0).AsRegister<Register>();
5105 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5106
5107 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005108 Register temp_reg = TMP;
5109 __ Addiu32(temp_reg, value_reg, -lower_bound);
5110 // Jump to default if index is negative
5111 // Note: We don't check the case that index is positive while value < lower_bound, because in
5112 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5113 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5114
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005115 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005116 // Jump to successors[0] if value == lower_bound.
5117 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5118 int32_t last_index = 0;
5119 for (; num_entries - last_index > 2; last_index += 2) {
5120 __ Addiu(temp_reg, temp_reg, -2);
5121 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5122 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5123 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5124 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5125 }
5126 if (num_entries - last_index == 2) {
5127 // The last missing case_value.
5128 __ Addiu(temp_reg, temp_reg, -1);
5129 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005130 }
5131
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005132 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005133 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5134 __ B(codegen_->GetLabelOf(default_block));
5135 }
5136}
5137
5138void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5139 // The trampoline uses the same calling convention as dex calling conventions,
5140 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5141 // the method_idx.
5142 HandleInvoke(invoke);
5143}
5144
5145void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5146 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5147}
5148
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005149void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5150 LocationSummary* locations =
5151 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5152 locations->SetInAt(0, Location::RequiresRegister());
5153 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005154}
5155
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005156void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5157 LocationSummary* locations = instruction->GetLocations();
5158 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005159 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005160 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5161 instruction->GetIndex(), kMipsPointerSize).SizeValue();
5162 } else {
5163 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
5164 instruction->GetIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
5165 }
5166 __ LoadFromOffset(kLoadWord,
5167 locations->Out().AsRegister<Register>(),
5168 locations->InAt(0).AsRegister<Register>(),
5169 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005170}
5171
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005172#undef __
5173#undef QUICK_ENTRY_POINT
5174
5175} // namespace mips
5176} // namespace art