blob: 91b431b0b5ad23a72eae9386434c0fd98c30064e [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
Alexey Frunzee3fb2452016-05-10 16:08:05 -070042// We'll maximize the range of a single load instruction for dex cache array accesses
43// by aligning offset -32768 with the offset of the first used element.
44static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
45
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020046Location MipsReturnLocation(Primitive::Type return_type) {
47 switch (return_type) {
48 case Primitive::kPrimBoolean:
49 case Primitive::kPrimByte:
50 case Primitive::kPrimChar:
51 case Primitive::kPrimShort:
52 case Primitive::kPrimInt:
53 case Primitive::kPrimNot:
54 return Location::RegisterLocation(V0);
55
56 case Primitive::kPrimLong:
57 return Location::RegisterPairLocation(V0, V1);
58
59 case Primitive::kPrimFloat:
60 case Primitive::kPrimDouble:
61 return Location::FpuRegisterLocation(F0);
62
63 case Primitive::kPrimVoid:
64 return Location();
65 }
66 UNREACHABLE();
67}
68
69Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
70 return MipsReturnLocation(type);
71}
72
73Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
74 return Location::RegisterLocation(kMethodRegisterArgument);
75}
76
77Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
78 Location next_location;
79
80 switch (type) {
81 case Primitive::kPrimBoolean:
82 case Primitive::kPrimByte:
83 case Primitive::kPrimChar:
84 case Primitive::kPrimShort:
85 case Primitive::kPrimInt:
86 case Primitive::kPrimNot: {
87 uint32_t gp_index = gp_index_++;
88 if (gp_index < calling_convention.GetNumberOfRegisters()) {
89 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
90 } else {
91 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
92 next_location = Location::StackSlot(stack_offset);
93 }
94 break;
95 }
96
97 case Primitive::kPrimLong: {
98 uint32_t gp_index = gp_index_;
99 gp_index_ += 2;
100 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
101 if (calling_convention.GetRegisterAt(gp_index) == A1) {
102 gp_index_++; // Skip A1, and use A2_A3 instead.
103 gp_index++;
104 }
105 Register low_even = calling_convention.GetRegisterAt(gp_index);
106 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
107 DCHECK_EQ(low_even + 1, high_odd);
108 next_location = Location::RegisterPairLocation(low_even, high_odd);
109 } else {
110 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
111 next_location = Location::DoubleStackSlot(stack_offset);
112 }
113 break;
114 }
115
116 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
117 // will take up the even/odd pair, while floats are stored in even regs only.
118 // On 64 bit FPU, both double and float are stored in even registers only.
119 case Primitive::kPrimFloat:
120 case Primitive::kPrimDouble: {
121 uint32_t float_index = float_index_++;
122 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
123 next_location = Location::FpuRegisterLocation(
124 calling_convention.GetFpuRegisterAt(float_index));
125 } else {
126 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
127 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
128 : Location::StackSlot(stack_offset);
129 }
130 break;
131 }
132
133 case Primitive::kPrimVoid:
134 LOG(FATAL) << "Unexpected parameter type " << type;
135 break;
136 }
137
138 // Space on the stack is reserved for all arguments.
139 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
140
141 return next_location;
142}
143
144Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
145 return MipsReturnLocation(type);
146}
147
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700148// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
149#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200150#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
151
152class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
153 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000154 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155
156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
157 LocationSummary* locations = instruction_->GetLocations();
158 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
159 __ Bind(GetEntryLabel());
160 if (instruction_->CanThrowIntoCatchBlock()) {
161 // Live registers will be restored in the catch block if caught.
162 SaveLiveRegisters(codegen, instruction_->GetLocations());
163 }
164 // We're moving two locations to locations that could overlap, so we need a parallel
165 // move resolver.
166 InvokeRuntimeCallingConvention calling_convention;
167 codegen->EmitParallelMoves(locations->InAt(0),
168 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
169 Primitive::kPrimInt,
170 locations->InAt(1),
171 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
172 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100173 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
174 ? QUICK_ENTRY_POINT(pThrowStringBounds)
175 : QUICK_ENTRY_POINT(pThrowArrayBounds);
176 mips_codegen->InvokeRuntime(entry_point_offset,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200177 instruction_,
178 instruction_->GetDexPc(),
179 this,
180 IsDirectEntrypoint(kQuickThrowArrayBounds));
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100181 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200182 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
183 }
184
185 bool IsFatal() const OVERRIDE { return true; }
186
187 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
188
189 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200190 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
191};
192
193class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
194 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000195 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200196
197 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
198 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
199 __ Bind(GetEntryLabel());
200 if (instruction_->CanThrowIntoCatchBlock()) {
201 // Live registers will be restored in the catch block if caught.
202 SaveLiveRegisters(codegen, instruction_->GetLocations());
203 }
204 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
205 instruction_,
206 instruction_->GetDexPc(),
207 this,
208 IsDirectEntrypoint(kQuickThrowDivZero));
209 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
210 }
211
212 bool IsFatal() const OVERRIDE { return true; }
213
214 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
215
216 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200217 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
218};
219
220class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
221 public:
222 LoadClassSlowPathMIPS(HLoadClass* cls,
223 HInstruction* at,
224 uint32_t dex_pc,
225 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000226 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200227 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
228 }
229
230 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
231 LocationSummary* locations = at_->GetLocations();
232 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
233
234 __ Bind(GetEntryLabel());
235 SaveLiveRegisters(codegen, locations);
236
237 InvokeRuntimeCallingConvention calling_convention;
238 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
239
240 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
241 : QUICK_ENTRY_POINT(pInitializeType);
242 bool direct = do_clinit_ ? IsDirectEntrypoint(kQuickInitializeStaticStorage)
243 : IsDirectEntrypoint(kQuickInitializeType);
244
245 mips_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this, direct);
246 if (do_clinit_) {
247 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
248 } else {
249 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
250 }
251
252 // Move the class to the desired location.
253 Location out = locations->Out();
254 if (out.IsValid()) {
255 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
256 Primitive::Type type = at_->GetType();
257 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
258 }
259
260 RestoreLiveRegisters(codegen, locations);
261 __ B(GetExitLabel());
262 }
263
264 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
265
266 private:
267 // The class this slow path will load.
268 HLoadClass* const cls_;
269
270 // The instruction where this slow path is happening.
271 // (Might be the load class or an initialization check).
272 HInstruction* const at_;
273
274 // The dex PC of `at_`.
275 const uint32_t dex_pc_;
276
277 // Whether to initialize the class.
278 const bool do_clinit_;
279
280 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
281};
282
283class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
284 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000285 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286
287 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
288 LocationSummary* locations = instruction_->GetLocations();
289 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
290 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
291
292 __ Bind(GetEntryLabel());
293 SaveLiveRegisters(codegen, locations);
294
295 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000296 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
297 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200298 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
299 instruction_,
300 instruction_->GetDexPc(),
301 this,
302 IsDirectEntrypoint(kQuickResolveString));
303 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
304 Primitive::Type type = instruction_->GetType();
305 mips_codegen->MoveLocation(locations->Out(),
306 calling_convention.GetReturnLocation(type),
307 type);
308
309 RestoreLiveRegisters(codegen, locations);
310 __ B(GetExitLabel());
311 }
312
313 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
314
315 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
317};
318
319class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
320 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000321 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
325 __ Bind(GetEntryLabel());
326 if (instruction_->CanThrowIntoCatchBlock()) {
327 // Live registers will be restored in the catch block if caught.
328 SaveLiveRegisters(codegen, instruction_->GetLocations());
329 }
330 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
331 instruction_,
332 instruction_->GetDexPc(),
333 this,
334 IsDirectEntrypoint(kQuickThrowNullPointer));
335 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
336 }
337
338 bool IsFatal() const OVERRIDE { return true; }
339
340 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
341
342 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200343 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
344};
345
346class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
347 public:
348 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000349 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200350
351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
352 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
353 __ Bind(GetEntryLabel());
354 SaveLiveRegisters(codegen, instruction_->GetLocations());
355 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
356 instruction_,
357 instruction_->GetDexPc(),
358 this,
359 IsDirectEntrypoint(kQuickTestSuspend));
360 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
361 RestoreLiveRegisters(codegen, instruction_->GetLocations());
362 if (successor_ == nullptr) {
363 __ B(GetReturnLabel());
364 } else {
365 __ B(mips_codegen->GetLabelOf(successor_));
366 }
367 }
368
369 MipsLabel* GetReturnLabel() {
370 DCHECK(successor_ == nullptr);
371 return &return_label_;
372 }
373
374 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
375
376 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200377 // If not null, the block to branch to after the suspend check.
378 HBasicBlock* const successor_;
379
380 // If `successor_` is null, the label to branch to after the suspend check.
381 MipsLabel return_label_;
382
383 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
384};
385
386class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
387 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000388 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
391 LocationSummary* locations = instruction_->GetLocations();
392 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
393 uint32_t dex_pc = instruction_->GetDexPc();
394 DCHECK(instruction_->IsCheckCast()
395 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
396 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
397
398 __ Bind(GetEntryLabel());
399 SaveLiveRegisters(codegen, locations);
400
401 // We're moving two locations to locations that could overlap, so we need a parallel
402 // move resolver.
403 InvokeRuntimeCallingConvention calling_convention;
404 codegen->EmitParallelMoves(locations->InAt(1),
405 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
406 Primitive::kPrimNot,
407 object_class,
408 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
409 Primitive::kPrimNot);
410
411 if (instruction_->IsInstanceOf()) {
412 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
413 instruction_,
414 dex_pc,
415 this,
416 IsDirectEntrypoint(kQuickInstanceofNonTrivial));
Roland Levillain888d0672015-11-23 18:53:50 +0000417 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700418 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200419 Primitive::Type ret_type = instruction_->GetType();
420 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
421 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200422 } else {
423 DCHECK(instruction_->IsCheckCast());
424 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
425 instruction_,
426 dex_pc,
427 this,
428 IsDirectEntrypoint(kQuickCheckCast));
429 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
430 }
431
432 RestoreLiveRegisters(codegen, locations);
433 __ B(GetExitLabel());
434 }
435
436 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
437
438 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200439 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
440};
441
442class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
443 public:
Aart Bik42249c32016-01-07 15:33:50 -0800444 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000445 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200446
447 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800448 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200449 __ Bind(GetEntryLabel());
450 SaveLiveRegisters(codegen, instruction_->GetLocations());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200451 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
452 instruction_,
Aart Bik42249c32016-01-07 15:33:50 -0800453 instruction_->GetDexPc(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200454 this,
455 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000456 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200457 }
458
459 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
460
461 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200462 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
463};
464
465CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
466 const MipsInstructionSetFeatures& isa_features,
467 const CompilerOptions& compiler_options,
468 OptimizingCompilerStats* stats)
469 : CodeGenerator(graph,
470 kNumberOfCoreRegisters,
471 kNumberOfFRegisters,
472 kNumberOfRegisterPairs,
473 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
474 arraysize(kCoreCalleeSaves)),
475 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
476 arraysize(kFpuCalleeSaves)),
477 compiler_options,
478 stats),
479 block_labels_(nullptr),
480 location_builder_(graph, this),
481 instruction_visitor_(graph, this),
482 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100483 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700484 isa_features_(isa_features),
485 method_patches_(MethodReferenceComparator(),
486 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
487 call_patches_(MethodReferenceComparator(),
488 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
489 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200490 // Save RA (containing the return address) to mimic Quick.
491 AddAllocatedRegister(Location::RegisterLocation(RA));
492}
493
494#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700495// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
496#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200497#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
498
499void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
500 // Ensure that we fix up branches.
501 __ FinalizeCode();
502
503 // Adjust native pc offsets in stack maps.
504 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
505 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
506 uint32_t new_position = __ GetAdjustedPosition(old_position);
507 DCHECK_GE(new_position, old_position);
508 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
509 }
510
511 // Adjust pc offsets for the disassembly information.
512 if (disasm_info_ != nullptr) {
513 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
514 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
515 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
516 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
517 it.second.start = __ GetAdjustedPosition(it.second.start);
518 it.second.end = __ GetAdjustedPosition(it.second.end);
519 }
520 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
521 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
522 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
523 }
524 }
525
526 CodeGenerator::Finalize(allocator);
527}
528
529MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
530 return codegen_->GetAssembler();
531}
532
533void ParallelMoveResolverMIPS::EmitMove(size_t index) {
534 DCHECK_LT(index, moves_.size());
535 MoveOperands* move = moves_[index];
536 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
537}
538
539void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
540 DCHECK_LT(index, moves_.size());
541 MoveOperands* move = moves_[index];
542 Primitive::Type type = move->GetType();
543 Location loc1 = move->GetDestination();
544 Location loc2 = move->GetSource();
545
546 DCHECK(!loc1.IsConstant());
547 DCHECK(!loc2.IsConstant());
548
549 if (loc1.Equals(loc2)) {
550 return;
551 }
552
553 if (loc1.IsRegister() && loc2.IsRegister()) {
554 // Swap 2 GPRs.
555 Register r1 = loc1.AsRegister<Register>();
556 Register r2 = loc2.AsRegister<Register>();
557 __ Move(TMP, r2);
558 __ Move(r2, r1);
559 __ Move(r1, TMP);
560 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
561 FRegister f1 = loc1.AsFpuRegister<FRegister>();
562 FRegister f2 = loc2.AsFpuRegister<FRegister>();
563 if (type == Primitive::kPrimFloat) {
564 __ MovS(FTMP, f2);
565 __ MovS(f2, f1);
566 __ MovS(f1, FTMP);
567 } else {
568 DCHECK_EQ(type, Primitive::kPrimDouble);
569 __ MovD(FTMP, f2);
570 __ MovD(f2, f1);
571 __ MovD(f1, FTMP);
572 }
573 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
574 (loc1.IsFpuRegister() && loc2.IsRegister())) {
575 // Swap FPR and GPR.
576 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
577 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
578 : loc2.AsFpuRegister<FRegister>();
579 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
580 : loc2.AsRegister<Register>();
581 __ Move(TMP, r2);
582 __ Mfc1(r2, f1);
583 __ Mtc1(TMP, f1);
584 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
585 // Swap 2 GPR register pairs.
586 Register r1 = loc1.AsRegisterPairLow<Register>();
587 Register r2 = loc2.AsRegisterPairLow<Register>();
588 __ Move(TMP, r2);
589 __ Move(r2, r1);
590 __ Move(r1, TMP);
591 r1 = loc1.AsRegisterPairHigh<Register>();
592 r2 = loc2.AsRegisterPairHigh<Register>();
593 __ Move(TMP, r2);
594 __ Move(r2, r1);
595 __ Move(r1, TMP);
596 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
597 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
598 // Swap FPR and GPR register pair.
599 DCHECK_EQ(type, Primitive::kPrimDouble);
600 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
601 : loc2.AsFpuRegister<FRegister>();
602 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
603 : loc2.AsRegisterPairLow<Register>();
604 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
605 : loc2.AsRegisterPairHigh<Register>();
606 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
607 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
608 // unpredictable and the following mfch1 will fail.
609 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800610 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200611 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800612 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200613 __ Move(r2_l, TMP);
614 __ Move(r2_h, AT);
615 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
616 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
617 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
618 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000619 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
620 (loc1.IsStackSlot() && loc2.IsRegister())) {
621 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
622 : loc2.AsRegister<Register>();
623 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
624 : loc2.GetStackIndex();
625 __ Move(TMP, reg);
626 __ LoadFromOffset(kLoadWord, reg, SP, offset);
627 __ StoreToOffset(kStoreWord, TMP, SP, offset);
628 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
629 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
630 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
631 : loc2.AsRegisterPairLow<Register>();
632 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
633 : loc2.AsRegisterPairHigh<Register>();
634 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
635 : loc2.GetStackIndex();
636 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
637 : loc2.GetHighStackIndex(kMipsWordSize);
638 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000639 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000640 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000641 __ Move(TMP, reg_h);
642 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
643 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200644 } else {
645 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
646 }
647}
648
649void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
650 __ Pop(static_cast<Register>(reg));
651}
652
653void ParallelMoveResolverMIPS::SpillScratch(int reg) {
654 __ Push(static_cast<Register>(reg));
655}
656
657void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
658 // Allocate a scratch register other than TMP, if available.
659 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
660 // automatically unspilled when the scratch scope object is destroyed).
661 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
662 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
663 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
664 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
665 __ LoadFromOffset(kLoadWord,
666 Register(ensure_scratch.GetRegister()),
667 SP,
668 index1 + stack_offset);
669 __ LoadFromOffset(kLoadWord,
670 TMP,
671 SP,
672 index2 + stack_offset);
673 __ StoreToOffset(kStoreWord,
674 Register(ensure_scratch.GetRegister()),
675 SP,
676 index2 + stack_offset);
677 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
678 }
679}
680
Alexey Frunze73296a72016-06-03 22:51:46 -0700681void CodeGeneratorMIPS::ComputeSpillMask() {
682 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
683 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
684 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
685 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
686 // registers, include the ZERO register to force alignment of FPU callee-saved registers
687 // within the stack frame.
688 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
689 core_spill_mask_ |= (1 << ZERO);
690 }
691}
692
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200693static dwarf::Reg DWARFReg(Register reg) {
694 return dwarf::Reg::MipsCore(static_cast<int>(reg));
695}
696
697// TODO: mapping of floating-point registers to DWARF.
698
699void CodeGeneratorMIPS::GenerateFrameEntry() {
700 __ Bind(&frame_entry_label_);
701
702 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
703
704 if (do_overflow_check) {
705 __ LoadFromOffset(kLoadWord,
706 ZERO,
707 SP,
708 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
709 RecordPcInfo(nullptr, 0);
710 }
711
712 if (HasEmptyFrame()) {
713 return;
714 }
715
716 // Make sure the frame size isn't unreasonably large.
717 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
718 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
719 }
720
721 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200722
Alexey Frunze73296a72016-06-03 22:51:46 -0700723 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200724 __ IncreaseFrameSize(ofs);
725
Alexey Frunze73296a72016-06-03 22:51:46 -0700726 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
727 Register reg = static_cast<Register>(MostSignificantBit(mask));
728 mask ^= 1u << reg;
729 ofs -= kMipsWordSize;
730 // The ZERO register is only included for alignment.
731 if (reg != ZERO) {
732 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200733 __ cfi().RelOffset(DWARFReg(reg), ofs);
734 }
735 }
736
Alexey Frunze73296a72016-06-03 22:51:46 -0700737 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
738 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
739 mask ^= 1u << reg;
740 ofs -= kMipsDoublewordSize;
741 __ StoreDToOffset(reg, SP, ofs);
742 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200743 }
744
Alexey Frunze73296a72016-06-03 22:51:46 -0700745 // Store the current method pointer.
746 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200747}
748
749void CodeGeneratorMIPS::GenerateFrameExit() {
750 __ cfi().RememberState();
751
752 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200753 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200754
Alexey Frunze73296a72016-06-03 22:51:46 -0700755 // For better instruction scheduling restore RA before other registers.
756 uint32_t ofs = GetFrameSize();
757 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
758 Register reg = static_cast<Register>(MostSignificantBit(mask));
759 mask ^= 1u << reg;
760 ofs -= kMipsWordSize;
761 // The ZERO register is only included for alignment.
762 if (reg != ZERO) {
763 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200764 __ cfi().Restore(DWARFReg(reg));
765 }
766 }
767
Alexey Frunze73296a72016-06-03 22:51:46 -0700768 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
769 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
770 mask ^= 1u << reg;
771 ofs -= kMipsDoublewordSize;
772 __ LoadDFromOffset(reg, SP, ofs);
773 // TODO: __ cfi().Restore(DWARFReg(reg));
774 }
775
776 __ DecreaseFrameSize(GetFrameSize());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200777 }
778
779 __ Jr(RA);
780 __ Nop();
781
782 __ cfi().RestoreState();
783 __ cfi().DefCFAOffset(GetFrameSize());
784}
785
786void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
787 __ Bind(GetLabelOf(block));
788}
789
790void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
791 if (src.Equals(dst)) {
792 return;
793 }
794
795 if (src.IsConstant()) {
796 MoveConstant(dst, src.GetConstant());
797 } else {
798 if (Primitive::Is64BitType(dst_type)) {
799 Move64(dst, src);
800 } else {
801 Move32(dst, src);
802 }
803 }
804}
805
806void CodeGeneratorMIPS::Move32(Location destination, Location source) {
807 if (source.Equals(destination)) {
808 return;
809 }
810
811 if (destination.IsRegister()) {
812 if (source.IsRegister()) {
813 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
814 } else if (source.IsFpuRegister()) {
815 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
816 } else {
817 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
818 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
819 }
820 } else if (destination.IsFpuRegister()) {
821 if (source.IsRegister()) {
822 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
823 } else if (source.IsFpuRegister()) {
824 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
825 } else {
826 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
827 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
828 }
829 } else {
830 DCHECK(destination.IsStackSlot()) << destination;
831 if (source.IsRegister()) {
832 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
833 } else if (source.IsFpuRegister()) {
834 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
835 } else {
836 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
837 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
838 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
839 }
840 }
841}
842
843void CodeGeneratorMIPS::Move64(Location destination, Location source) {
844 if (source.Equals(destination)) {
845 return;
846 }
847
848 if (destination.IsRegisterPair()) {
849 if (source.IsRegisterPair()) {
850 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
851 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
852 } else if (source.IsFpuRegister()) {
853 Register dst_high = destination.AsRegisterPairHigh<Register>();
854 Register dst_low = destination.AsRegisterPairLow<Register>();
855 FRegister src = source.AsFpuRegister<FRegister>();
856 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800857 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200858 } else {
859 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
860 int32_t off = source.GetStackIndex();
861 Register r = destination.AsRegisterPairLow<Register>();
862 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
863 }
864 } else if (destination.IsFpuRegister()) {
865 if (source.IsRegisterPair()) {
866 FRegister dst = destination.AsFpuRegister<FRegister>();
867 Register src_high = source.AsRegisterPairHigh<Register>();
868 Register src_low = source.AsRegisterPairLow<Register>();
869 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800870 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200871 } else if (source.IsFpuRegister()) {
872 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
873 } else {
874 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
875 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
876 }
877 } else {
878 DCHECK(destination.IsDoubleStackSlot()) << destination;
879 int32_t off = destination.GetStackIndex();
880 if (source.IsRegisterPair()) {
881 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
882 } else if (source.IsFpuRegister()) {
883 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
884 } else {
885 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
886 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
887 __ StoreToOffset(kStoreWord, TMP, SP, off);
888 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
889 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
890 }
891 }
892}
893
894void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
895 if (c->IsIntConstant() || c->IsNullConstant()) {
896 // Move 32 bit constant.
897 int32_t value = GetInt32ValueOf(c);
898 if (destination.IsRegister()) {
899 Register dst = destination.AsRegister<Register>();
900 __ LoadConst32(dst, value);
901 } else {
902 DCHECK(destination.IsStackSlot())
903 << "Cannot move " << c->DebugName() << " to " << destination;
904 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
905 }
906 } else if (c->IsLongConstant()) {
907 // Move 64 bit constant.
908 int64_t value = GetInt64ValueOf(c);
909 if (destination.IsRegisterPair()) {
910 Register r_h = destination.AsRegisterPairHigh<Register>();
911 Register r_l = destination.AsRegisterPairLow<Register>();
912 __ LoadConst64(r_h, r_l, value);
913 } else {
914 DCHECK(destination.IsDoubleStackSlot())
915 << "Cannot move " << c->DebugName() << " to " << destination;
916 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
917 }
918 } else if (c->IsFloatConstant()) {
919 // Move 32 bit float constant.
920 int32_t value = GetInt32ValueOf(c);
921 if (destination.IsFpuRegister()) {
922 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
923 } else {
924 DCHECK(destination.IsStackSlot())
925 << "Cannot move " << c->DebugName() << " to " << destination;
926 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
927 }
928 } else {
929 // Move 64 bit double constant.
930 DCHECK(c->IsDoubleConstant()) << c->DebugName();
931 int64_t value = GetInt64ValueOf(c);
932 if (destination.IsFpuRegister()) {
933 FRegister fd = destination.AsFpuRegister<FRegister>();
934 __ LoadDConst64(fd, value, TMP);
935 } else {
936 DCHECK(destination.IsDoubleStackSlot())
937 << "Cannot move " << c->DebugName() << " to " << destination;
938 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
939 }
940 }
941}
942
943void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
944 DCHECK(destination.IsRegister());
945 Register dst = destination.AsRegister<Register>();
946 __ LoadConst32(dst, value);
947}
948
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200949void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
950 if (location.IsRegister()) {
951 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700952 } else if (location.IsRegisterPair()) {
953 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
954 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200955 } else {
956 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
957 }
958}
959
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700960void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
961 DCHECK(linker_patches->empty());
962 size_t size =
963 method_patches_.size() +
964 call_patches_.size() +
965 pc_relative_dex_cache_patches_.size();
966 linker_patches->reserve(size);
967 for (const auto& entry : method_patches_) {
968 const MethodReference& target_method = entry.first;
969 Literal* literal = entry.second;
970 DCHECK(literal->GetLabel()->IsBound());
971 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
972 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
973 target_method.dex_file,
974 target_method.dex_method_index));
975 }
976 for (const auto& entry : call_patches_) {
977 const MethodReference& target_method = entry.first;
978 Literal* literal = entry.second;
979 DCHECK(literal->GetLabel()->IsBound());
980 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
981 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
982 target_method.dex_file,
983 target_method.dex_method_index));
984 }
985 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
986 const DexFile& dex_file = info.target_dex_file;
987 size_t base_element_offset = info.offset_or_index;
988 DCHECK(info.high_label.IsBound());
989 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
990 DCHECK(info.pc_rel_label.IsBound());
991 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
992 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(high_offset,
993 &dex_file,
994 pc_rel_offset,
995 base_element_offset));
996 }
997}
998
999CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1000 const DexFile& dex_file, uint32_t element_offset) {
1001 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1002}
1003
1004CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1005 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1006 patches->emplace_back(dex_file, offset_or_index);
1007 return &patches->back();
1008}
1009
1010Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1011 MethodToLiteralMap* map) {
1012 return map->GetOrCreate(
1013 target_method,
1014 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1015}
1016
1017Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1018 return DeduplicateMethodLiteral(target_method, &method_patches_);
1019}
1020
1021Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1022 return DeduplicateMethodLiteral(target_method, &call_patches_);
1023}
1024
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001025void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1026 MipsLabel done;
1027 Register card = AT;
1028 Register temp = TMP;
1029 __ Beqz(value, &done);
1030 __ LoadFromOffset(kLoadWord,
1031 card,
1032 TR,
1033 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
1034 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1035 __ Addu(temp, card, temp);
1036 __ Sb(card, temp, 0);
1037 __ Bind(&done);
1038}
1039
David Brazdil58282f42016-01-14 12:45:10 +00001040void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001041 // Don't allocate the dalvik style register pair passing.
1042 blocked_register_pairs_[A1_A2] = true;
1043
1044 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1045 blocked_core_registers_[ZERO] = true;
1046 blocked_core_registers_[K0] = true;
1047 blocked_core_registers_[K1] = true;
1048 blocked_core_registers_[GP] = true;
1049 blocked_core_registers_[SP] = true;
1050 blocked_core_registers_[RA] = true;
1051
1052 // AT and TMP(T8) are used as temporary/scratch registers
1053 // (similar to how AT is used by MIPS assemblers).
1054 blocked_core_registers_[AT] = true;
1055 blocked_core_registers_[TMP] = true;
1056 blocked_fpu_registers_[FTMP] = true;
1057
1058 // Reserve suspend and thread registers.
1059 blocked_core_registers_[S0] = true;
1060 blocked_core_registers_[TR] = true;
1061
1062 // Reserve T9 for function calls
1063 blocked_core_registers_[T9] = true;
1064
1065 // Reserve odd-numbered FPU registers.
1066 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1067 blocked_fpu_registers_[i] = true;
1068 }
1069
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001070 if (GetGraph()->IsDebuggable()) {
1071 // Stubs do not save callee-save floating point registers. If the graph
1072 // is debuggable, we need to deal with these registers differently. For
1073 // now, just block them.
1074 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1075 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1076 }
1077 }
1078
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001079 UpdateBlockedPairRegisters();
1080}
1081
1082void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1083 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1084 MipsManagedRegister current =
1085 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1086 if (blocked_core_registers_[current.AsRegisterPairLow()]
1087 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1088 blocked_register_pairs_[i] = true;
1089 }
1090 }
1091}
1092
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001093size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1094 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1095 return kMipsWordSize;
1096}
1097
1098size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1099 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1100 return kMipsWordSize;
1101}
1102
1103size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1104 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1105 return kMipsDoublewordSize;
1106}
1107
1108size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1109 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1110 return kMipsDoublewordSize;
1111}
1112
1113void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001114 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001115}
1116
1117void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001118 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001119}
1120
1121void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1122 HInstruction* instruction,
1123 uint32_t dex_pc,
1124 SlowPathCode* slow_path) {
1125 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1126 instruction,
1127 dex_pc,
1128 slow_path,
1129 IsDirectEntrypoint(entrypoint));
1130}
1131
1132constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1133
1134void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1135 HInstruction* instruction,
1136 uint32_t dex_pc,
1137 SlowPathCode* slow_path,
1138 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001139 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1140 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001141 if (is_direct_entrypoint) {
1142 // Reserve argument space on stack (for $a0-$a3) for
1143 // entrypoints that directly reference native implementations.
1144 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001145 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001146 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001147 } else {
1148 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001149 }
1150 RecordPcInfo(instruction, dex_pc, slow_path);
1151}
1152
1153void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1154 Register class_reg) {
1155 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1156 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1157 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1158 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1159 __ Sync(0);
1160 __ Bind(slow_path->GetExitLabel());
1161}
1162
1163void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1164 __ Sync(0); // Only stype 0 is supported.
1165}
1166
1167void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1168 HBasicBlock* successor) {
1169 SuspendCheckSlowPathMIPS* slow_path =
1170 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1171 codegen_->AddSlowPath(slow_path);
1172
1173 __ LoadFromOffset(kLoadUnsignedHalfword,
1174 TMP,
1175 TR,
1176 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1177 if (successor == nullptr) {
1178 __ Bnez(TMP, slow_path->GetEntryLabel());
1179 __ Bind(slow_path->GetReturnLabel());
1180 } else {
1181 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1182 __ B(slow_path->GetEntryLabel());
1183 // slow_path will return to GetLabelOf(successor).
1184 }
1185}
1186
1187InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1188 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001189 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001190 assembler_(codegen->GetAssembler()),
1191 codegen_(codegen) {}
1192
1193void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1194 DCHECK_EQ(instruction->InputCount(), 2U);
1195 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1196 Primitive::Type type = instruction->GetResultType();
1197 switch (type) {
1198 case Primitive::kPrimInt: {
1199 locations->SetInAt(0, Location::RequiresRegister());
1200 HInstruction* right = instruction->InputAt(1);
1201 bool can_use_imm = false;
1202 if (right->IsConstant()) {
1203 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1204 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1205 can_use_imm = IsUint<16>(imm);
1206 } else if (instruction->IsAdd()) {
1207 can_use_imm = IsInt<16>(imm);
1208 } else {
1209 DCHECK(instruction->IsSub());
1210 can_use_imm = IsInt<16>(-imm);
1211 }
1212 }
1213 if (can_use_imm)
1214 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1215 else
1216 locations->SetInAt(1, Location::RequiresRegister());
1217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1218 break;
1219 }
1220
1221 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001222 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001223 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1224 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001225 break;
1226 }
1227
1228 case Primitive::kPrimFloat:
1229 case Primitive::kPrimDouble:
1230 DCHECK(instruction->IsAdd() || instruction->IsSub());
1231 locations->SetInAt(0, Location::RequiresFpuRegister());
1232 locations->SetInAt(1, Location::RequiresFpuRegister());
1233 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1234 break;
1235
1236 default:
1237 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1238 }
1239}
1240
1241void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1242 Primitive::Type type = instruction->GetType();
1243 LocationSummary* locations = instruction->GetLocations();
1244
1245 switch (type) {
1246 case Primitive::kPrimInt: {
1247 Register dst = locations->Out().AsRegister<Register>();
1248 Register lhs = locations->InAt(0).AsRegister<Register>();
1249 Location rhs_location = locations->InAt(1);
1250
1251 Register rhs_reg = ZERO;
1252 int32_t rhs_imm = 0;
1253 bool use_imm = rhs_location.IsConstant();
1254 if (use_imm) {
1255 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1256 } else {
1257 rhs_reg = rhs_location.AsRegister<Register>();
1258 }
1259
1260 if (instruction->IsAnd()) {
1261 if (use_imm)
1262 __ Andi(dst, lhs, rhs_imm);
1263 else
1264 __ And(dst, lhs, rhs_reg);
1265 } else if (instruction->IsOr()) {
1266 if (use_imm)
1267 __ Ori(dst, lhs, rhs_imm);
1268 else
1269 __ Or(dst, lhs, rhs_reg);
1270 } else if (instruction->IsXor()) {
1271 if (use_imm)
1272 __ Xori(dst, lhs, rhs_imm);
1273 else
1274 __ Xor(dst, lhs, rhs_reg);
1275 } else if (instruction->IsAdd()) {
1276 if (use_imm)
1277 __ Addiu(dst, lhs, rhs_imm);
1278 else
1279 __ Addu(dst, lhs, rhs_reg);
1280 } else {
1281 DCHECK(instruction->IsSub());
1282 if (use_imm)
1283 __ Addiu(dst, lhs, -rhs_imm);
1284 else
1285 __ Subu(dst, lhs, rhs_reg);
1286 }
1287 break;
1288 }
1289
1290 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001291 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1292 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1293 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1294 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001295 Location rhs_location = locations->InAt(1);
1296 bool use_imm = rhs_location.IsConstant();
1297 if (!use_imm) {
1298 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1299 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1300 if (instruction->IsAnd()) {
1301 __ And(dst_low, lhs_low, rhs_low);
1302 __ And(dst_high, lhs_high, rhs_high);
1303 } else if (instruction->IsOr()) {
1304 __ Or(dst_low, lhs_low, rhs_low);
1305 __ Or(dst_high, lhs_high, rhs_high);
1306 } else if (instruction->IsXor()) {
1307 __ Xor(dst_low, lhs_low, rhs_low);
1308 __ Xor(dst_high, lhs_high, rhs_high);
1309 } else if (instruction->IsAdd()) {
1310 if (lhs_low == rhs_low) {
1311 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1312 __ Slt(TMP, lhs_low, ZERO);
1313 __ Addu(dst_low, lhs_low, rhs_low);
1314 } else {
1315 __ Addu(dst_low, lhs_low, rhs_low);
1316 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1317 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1318 }
1319 __ Addu(dst_high, lhs_high, rhs_high);
1320 __ Addu(dst_high, dst_high, TMP);
1321 } else {
1322 DCHECK(instruction->IsSub());
1323 __ Sltu(TMP, lhs_low, rhs_low);
1324 __ Subu(dst_low, lhs_low, rhs_low);
1325 __ Subu(dst_high, lhs_high, rhs_high);
1326 __ Subu(dst_high, dst_high, TMP);
1327 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001328 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001329 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1330 if (instruction->IsOr()) {
1331 uint32_t low = Low32Bits(value);
1332 uint32_t high = High32Bits(value);
1333 if (IsUint<16>(low)) {
1334 if (dst_low != lhs_low || low != 0) {
1335 __ Ori(dst_low, lhs_low, low);
1336 }
1337 } else {
1338 __ LoadConst32(TMP, low);
1339 __ Or(dst_low, lhs_low, TMP);
1340 }
1341 if (IsUint<16>(high)) {
1342 if (dst_high != lhs_high || high != 0) {
1343 __ Ori(dst_high, lhs_high, high);
1344 }
1345 } else {
1346 if (high != low) {
1347 __ LoadConst32(TMP, high);
1348 }
1349 __ Or(dst_high, lhs_high, TMP);
1350 }
1351 } else if (instruction->IsXor()) {
1352 uint32_t low = Low32Bits(value);
1353 uint32_t high = High32Bits(value);
1354 if (IsUint<16>(low)) {
1355 if (dst_low != lhs_low || low != 0) {
1356 __ Xori(dst_low, lhs_low, low);
1357 }
1358 } else {
1359 __ LoadConst32(TMP, low);
1360 __ Xor(dst_low, lhs_low, TMP);
1361 }
1362 if (IsUint<16>(high)) {
1363 if (dst_high != lhs_high || high != 0) {
1364 __ Xori(dst_high, lhs_high, high);
1365 }
1366 } else {
1367 if (high != low) {
1368 __ LoadConst32(TMP, high);
1369 }
1370 __ Xor(dst_high, lhs_high, TMP);
1371 }
1372 } else if (instruction->IsAnd()) {
1373 uint32_t low = Low32Bits(value);
1374 uint32_t high = High32Bits(value);
1375 if (IsUint<16>(low)) {
1376 __ Andi(dst_low, lhs_low, low);
1377 } else if (low != 0xFFFFFFFF) {
1378 __ LoadConst32(TMP, low);
1379 __ And(dst_low, lhs_low, TMP);
1380 } else if (dst_low != lhs_low) {
1381 __ Move(dst_low, lhs_low);
1382 }
1383 if (IsUint<16>(high)) {
1384 __ Andi(dst_high, lhs_high, high);
1385 } else if (high != 0xFFFFFFFF) {
1386 if (high != low) {
1387 __ LoadConst32(TMP, high);
1388 }
1389 __ And(dst_high, lhs_high, TMP);
1390 } else if (dst_high != lhs_high) {
1391 __ Move(dst_high, lhs_high);
1392 }
1393 } else {
1394 if (instruction->IsSub()) {
1395 value = -value;
1396 } else {
1397 DCHECK(instruction->IsAdd());
1398 }
1399 int32_t low = Low32Bits(value);
1400 int32_t high = High32Bits(value);
1401 if (IsInt<16>(low)) {
1402 if (dst_low != lhs_low || low != 0) {
1403 __ Addiu(dst_low, lhs_low, low);
1404 }
1405 if (low != 0) {
1406 __ Sltiu(AT, dst_low, low);
1407 }
1408 } else {
1409 __ LoadConst32(TMP, low);
1410 __ Addu(dst_low, lhs_low, TMP);
1411 __ Sltu(AT, dst_low, TMP);
1412 }
1413 if (IsInt<16>(high)) {
1414 if (dst_high != lhs_high || high != 0) {
1415 __ Addiu(dst_high, lhs_high, high);
1416 }
1417 } else {
1418 if (high != low) {
1419 __ LoadConst32(TMP, high);
1420 }
1421 __ Addu(dst_high, lhs_high, TMP);
1422 }
1423 if (low != 0) {
1424 __ Addu(dst_high, dst_high, AT);
1425 }
1426 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001427 }
1428 break;
1429 }
1430
1431 case Primitive::kPrimFloat:
1432 case Primitive::kPrimDouble: {
1433 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1434 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1435 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1436 if (instruction->IsAdd()) {
1437 if (type == Primitive::kPrimFloat) {
1438 __ AddS(dst, lhs, rhs);
1439 } else {
1440 __ AddD(dst, lhs, rhs);
1441 }
1442 } else {
1443 DCHECK(instruction->IsSub());
1444 if (type == Primitive::kPrimFloat) {
1445 __ SubS(dst, lhs, rhs);
1446 } else {
1447 __ SubD(dst, lhs, rhs);
1448 }
1449 }
1450 break;
1451 }
1452
1453 default:
1454 LOG(FATAL) << "Unexpected binary operation type " << type;
1455 }
1456}
1457
1458void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001459 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001460
1461 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1462 Primitive::Type type = instr->GetResultType();
1463 switch (type) {
1464 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001465 locations->SetInAt(0, Location::RequiresRegister());
1466 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1467 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1468 break;
1469 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001470 locations->SetInAt(0, Location::RequiresRegister());
1471 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1472 locations->SetOut(Location::RequiresRegister());
1473 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001474 default:
1475 LOG(FATAL) << "Unexpected shift type " << type;
1476 }
1477}
1478
1479static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1480
1481void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001482 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001483 LocationSummary* locations = instr->GetLocations();
1484 Primitive::Type type = instr->GetType();
1485
1486 Location rhs_location = locations->InAt(1);
1487 bool use_imm = rhs_location.IsConstant();
1488 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1489 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001490 const uint32_t shift_mask =
1491 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001492 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001493 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1494 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001495
1496 switch (type) {
1497 case Primitive::kPrimInt: {
1498 Register dst = locations->Out().AsRegister<Register>();
1499 Register lhs = locations->InAt(0).AsRegister<Register>();
1500 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001501 if (shift_value == 0) {
1502 if (dst != lhs) {
1503 __ Move(dst, lhs);
1504 }
1505 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001506 __ Sll(dst, lhs, shift_value);
1507 } else if (instr->IsShr()) {
1508 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001509 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001510 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001511 } else {
1512 if (has_ins_rotr) {
1513 __ Rotr(dst, lhs, shift_value);
1514 } else {
1515 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1516 __ Srl(dst, lhs, shift_value);
1517 __ Or(dst, dst, TMP);
1518 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001519 }
1520 } else {
1521 if (instr->IsShl()) {
1522 __ Sllv(dst, lhs, rhs_reg);
1523 } else if (instr->IsShr()) {
1524 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001525 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001526 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001527 } else {
1528 if (has_ins_rotr) {
1529 __ Rotrv(dst, lhs, rhs_reg);
1530 } else {
1531 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001532 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1533 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1534 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1535 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1536 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001537 __ Sllv(TMP, lhs, TMP);
1538 __ Srlv(dst, lhs, rhs_reg);
1539 __ Or(dst, dst, TMP);
1540 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001541 }
1542 }
1543 break;
1544 }
1545
1546 case Primitive::kPrimLong: {
1547 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1548 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1549 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1550 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1551 if (use_imm) {
1552 if (shift_value == 0) {
1553 codegen_->Move64(locations->Out(), locations->InAt(0));
1554 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001555 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001556 if (instr->IsShl()) {
1557 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1558 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1559 __ Sll(dst_low, lhs_low, shift_value);
1560 } else if (instr->IsShr()) {
1561 __ Srl(dst_low, lhs_low, shift_value);
1562 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1563 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001564 } else if (instr->IsUShr()) {
1565 __ Srl(dst_low, lhs_low, shift_value);
1566 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1567 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001568 } else {
1569 __ Srl(dst_low, lhs_low, shift_value);
1570 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1571 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001572 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001573 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001574 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001575 if (instr->IsShl()) {
1576 __ Sll(dst_low, lhs_low, shift_value);
1577 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1578 __ Sll(dst_high, lhs_high, shift_value);
1579 __ Or(dst_high, dst_high, TMP);
1580 } else if (instr->IsShr()) {
1581 __ Sra(dst_high, lhs_high, shift_value);
1582 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1583 __ Srl(dst_low, lhs_low, shift_value);
1584 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001585 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001586 __ Srl(dst_high, lhs_high, shift_value);
1587 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1588 __ Srl(dst_low, lhs_low, shift_value);
1589 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001590 } else {
1591 __ Srl(TMP, lhs_low, shift_value);
1592 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1593 __ Or(dst_low, dst_low, TMP);
1594 __ Srl(TMP, lhs_high, shift_value);
1595 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1596 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001597 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001598 }
1599 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001600 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001601 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001602 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001603 __ Move(dst_low, ZERO);
1604 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001605 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001606 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001607 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001608 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001609 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001610 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001611 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001612 // 64-bit rotation by 32 is just a swap.
1613 __ Move(dst_low, lhs_high);
1614 __ Move(dst_high, lhs_low);
1615 } else {
1616 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001617 __ Srl(dst_low, lhs_high, shift_value_high);
1618 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1619 __ Srl(dst_high, lhs_low, shift_value_high);
1620 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001621 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001622 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1623 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001624 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001625 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1626 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001627 __ Or(dst_high, dst_high, TMP);
1628 }
1629 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001630 }
1631 }
1632 } else {
1633 MipsLabel done;
1634 if (instr->IsShl()) {
1635 __ Sllv(dst_low, lhs_low, rhs_reg);
1636 __ Nor(AT, ZERO, rhs_reg);
1637 __ Srl(TMP, lhs_low, 1);
1638 __ Srlv(TMP, TMP, AT);
1639 __ Sllv(dst_high, lhs_high, rhs_reg);
1640 __ Or(dst_high, dst_high, TMP);
1641 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1642 __ Beqz(TMP, &done);
1643 __ Move(dst_high, dst_low);
1644 __ Move(dst_low, ZERO);
1645 } else if (instr->IsShr()) {
1646 __ Srav(dst_high, lhs_high, rhs_reg);
1647 __ Nor(AT, ZERO, rhs_reg);
1648 __ Sll(TMP, lhs_high, 1);
1649 __ Sllv(TMP, TMP, AT);
1650 __ Srlv(dst_low, lhs_low, rhs_reg);
1651 __ Or(dst_low, dst_low, TMP);
1652 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1653 __ Beqz(TMP, &done);
1654 __ Move(dst_low, dst_high);
1655 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001656 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001657 __ Srlv(dst_high, lhs_high, rhs_reg);
1658 __ Nor(AT, ZERO, rhs_reg);
1659 __ Sll(TMP, lhs_high, 1);
1660 __ Sllv(TMP, TMP, AT);
1661 __ Srlv(dst_low, lhs_low, rhs_reg);
1662 __ Or(dst_low, dst_low, TMP);
1663 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1664 __ Beqz(TMP, &done);
1665 __ Move(dst_low, dst_high);
1666 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001667 } else {
1668 __ Nor(AT, ZERO, rhs_reg);
1669 __ Srlv(TMP, lhs_low, rhs_reg);
1670 __ Sll(dst_low, lhs_high, 1);
1671 __ Sllv(dst_low, dst_low, AT);
1672 __ Or(dst_low, dst_low, TMP);
1673 __ Srlv(TMP, lhs_high, rhs_reg);
1674 __ Sll(dst_high, lhs_low, 1);
1675 __ Sllv(dst_high, dst_high, AT);
1676 __ Or(dst_high, dst_high, TMP);
1677 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1678 __ Beqz(TMP, &done);
1679 __ Move(TMP, dst_high);
1680 __ Move(dst_high, dst_low);
1681 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001682 }
1683 __ Bind(&done);
1684 }
1685 break;
1686 }
1687
1688 default:
1689 LOG(FATAL) << "Unexpected shift operation type " << type;
1690 }
1691}
1692
1693void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1694 HandleBinaryOp(instruction);
1695}
1696
1697void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1698 HandleBinaryOp(instruction);
1699}
1700
1701void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1702 HandleBinaryOp(instruction);
1703}
1704
1705void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1706 HandleBinaryOp(instruction);
1707}
1708
1709void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1710 LocationSummary* locations =
1711 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1712 locations->SetInAt(0, Location::RequiresRegister());
1713 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1714 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1715 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1716 } else {
1717 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1718 }
1719}
1720
1721void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1722 LocationSummary* locations = instruction->GetLocations();
1723 Register obj = locations->InAt(0).AsRegister<Register>();
1724 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001725 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001726
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001727 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001728 switch (type) {
1729 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001730 Register out = locations->Out().AsRegister<Register>();
1731 if (index.IsConstant()) {
1732 size_t offset =
1733 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1734 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1735 } else {
1736 __ Addu(TMP, obj, index.AsRegister<Register>());
1737 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1738 }
1739 break;
1740 }
1741
1742 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001743 Register out = locations->Out().AsRegister<Register>();
1744 if (index.IsConstant()) {
1745 size_t offset =
1746 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1747 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1748 } else {
1749 __ Addu(TMP, obj, index.AsRegister<Register>());
1750 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1751 }
1752 break;
1753 }
1754
1755 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001756 Register out = locations->Out().AsRegister<Register>();
1757 if (index.IsConstant()) {
1758 size_t offset =
1759 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1760 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1761 } else {
1762 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1763 __ Addu(TMP, obj, TMP);
1764 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1765 }
1766 break;
1767 }
1768
1769 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001770 Register out = locations->Out().AsRegister<Register>();
1771 if (index.IsConstant()) {
1772 size_t offset =
1773 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1774 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1775 } else {
1776 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1777 __ Addu(TMP, obj, TMP);
1778 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1779 }
1780 break;
1781 }
1782
1783 case Primitive::kPrimInt:
1784 case Primitive::kPrimNot: {
1785 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001786 Register out = locations->Out().AsRegister<Register>();
1787 if (index.IsConstant()) {
1788 size_t offset =
1789 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1790 __ LoadFromOffset(kLoadWord, out, obj, offset);
1791 } else {
1792 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1793 __ Addu(TMP, obj, TMP);
1794 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1795 }
1796 break;
1797 }
1798
1799 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001800 Register out = locations->Out().AsRegisterPairLow<Register>();
1801 if (index.IsConstant()) {
1802 size_t offset =
1803 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1804 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1805 } else {
1806 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1807 __ Addu(TMP, obj, TMP);
1808 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1809 }
1810 break;
1811 }
1812
1813 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001814 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1815 if (index.IsConstant()) {
1816 size_t offset =
1817 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1818 __ LoadSFromOffset(out, obj, offset);
1819 } else {
1820 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1821 __ Addu(TMP, obj, TMP);
1822 __ LoadSFromOffset(out, TMP, data_offset);
1823 }
1824 break;
1825 }
1826
1827 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001828 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1829 if (index.IsConstant()) {
1830 size_t offset =
1831 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1832 __ LoadDFromOffset(out, obj, offset);
1833 } else {
1834 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1835 __ Addu(TMP, obj, TMP);
1836 __ LoadDFromOffset(out, TMP, data_offset);
1837 }
1838 break;
1839 }
1840
1841 case Primitive::kPrimVoid:
1842 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1843 UNREACHABLE();
1844 }
1845 codegen_->MaybeRecordImplicitNullCheck(instruction);
1846}
1847
1848void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1849 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1850 locations->SetInAt(0, Location::RequiresRegister());
1851 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1852}
1853
1854void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1855 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001856 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857 Register obj = locations->InAt(0).AsRegister<Register>();
1858 Register out = locations->Out().AsRegister<Register>();
1859 __ LoadFromOffset(kLoadWord, out, obj, offset);
1860 codegen_->MaybeRecordImplicitNullCheck(instruction);
1861}
1862
1863void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001864 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001865 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1866 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001867 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01001868 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001869 InvokeRuntimeCallingConvention calling_convention;
1870 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1871 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1872 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1873 } else {
1874 locations->SetInAt(0, Location::RequiresRegister());
1875 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1876 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1877 locations->SetInAt(2, Location::RequiresFpuRegister());
1878 } else {
1879 locations->SetInAt(2, Location::RequiresRegister());
1880 }
1881 }
1882}
1883
1884void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1885 LocationSummary* locations = instruction->GetLocations();
1886 Register obj = locations->InAt(0).AsRegister<Register>();
1887 Location index = locations->InAt(1);
1888 Primitive::Type value_type = instruction->GetComponentType();
1889 bool needs_runtime_call = locations->WillCall();
1890 bool needs_write_barrier =
1891 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1892
1893 switch (value_type) {
1894 case Primitive::kPrimBoolean:
1895 case Primitive::kPrimByte: {
1896 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1897 Register value = locations->InAt(2).AsRegister<Register>();
1898 if (index.IsConstant()) {
1899 size_t offset =
1900 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1901 __ StoreToOffset(kStoreByte, value, obj, offset);
1902 } else {
1903 __ Addu(TMP, obj, index.AsRegister<Register>());
1904 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1905 }
1906 break;
1907 }
1908
1909 case Primitive::kPrimShort:
1910 case Primitive::kPrimChar: {
1911 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1912 Register value = locations->InAt(2).AsRegister<Register>();
1913 if (index.IsConstant()) {
1914 size_t offset =
1915 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1916 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1917 } else {
1918 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1919 __ Addu(TMP, obj, TMP);
1920 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1921 }
1922 break;
1923 }
1924
1925 case Primitive::kPrimInt:
1926 case Primitive::kPrimNot: {
1927 if (!needs_runtime_call) {
1928 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1929 Register value = locations->InAt(2).AsRegister<Register>();
1930 if (index.IsConstant()) {
1931 size_t offset =
1932 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1933 __ StoreToOffset(kStoreWord, value, obj, offset);
1934 } else {
1935 DCHECK(index.IsRegister()) << index;
1936 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1937 __ Addu(TMP, obj, TMP);
1938 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1939 }
1940 codegen_->MaybeRecordImplicitNullCheck(instruction);
1941 if (needs_write_barrier) {
1942 DCHECK_EQ(value_type, Primitive::kPrimNot);
1943 codegen_->MarkGCCard(obj, value);
1944 }
1945 } else {
1946 DCHECK_EQ(value_type, Primitive::kPrimNot);
1947 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1948 instruction,
1949 instruction->GetDexPc(),
1950 nullptr,
1951 IsDirectEntrypoint(kQuickAputObject));
1952 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
1953 }
1954 break;
1955 }
1956
1957 case Primitive::kPrimLong: {
1958 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1959 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
1960 if (index.IsConstant()) {
1961 size_t offset =
1962 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1963 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1964 } else {
1965 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1966 __ Addu(TMP, obj, TMP);
1967 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1968 }
1969 break;
1970 }
1971
1972 case Primitive::kPrimFloat: {
1973 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1974 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1975 DCHECK(locations->InAt(2).IsFpuRegister());
1976 if (index.IsConstant()) {
1977 size_t offset =
1978 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1979 __ StoreSToOffset(value, obj, offset);
1980 } else {
1981 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1982 __ Addu(TMP, obj, TMP);
1983 __ StoreSToOffset(value, TMP, data_offset);
1984 }
1985 break;
1986 }
1987
1988 case Primitive::kPrimDouble: {
1989 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1990 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1991 DCHECK(locations->InAt(2).IsFpuRegister());
1992 if (index.IsConstant()) {
1993 size_t offset =
1994 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1995 __ StoreDToOffset(value, obj, offset);
1996 } else {
1997 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1998 __ Addu(TMP, obj, TMP);
1999 __ StoreDToOffset(value, TMP, data_offset);
2000 }
2001 break;
2002 }
2003
2004 case Primitive::kPrimVoid:
2005 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2006 UNREACHABLE();
2007 }
2008
2009 // Ints and objects are handled in the switch.
2010 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
2011 codegen_->MaybeRecordImplicitNullCheck(instruction);
2012 }
2013}
2014
2015void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2016 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2017 ? LocationSummary::kCallOnSlowPath
2018 : LocationSummary::kNoCall;
2019 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2020 locations->SetInAt(0, Location::RequiresRegister());
2021 locations->SetInAt(1, Location::RequiresRegister());
2022 if (instruction->HasUses()) {
2023 locations->SetOut(Location::SameAsFirstInput());
2024 }
2025}
2026
2027void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2028 LocationSummary* locations = instruction->GetLocations();
2029 BoundsCheckSlowPathMIPS* slow_path =
2030 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2031 codegen_->AddSlowPath(slow_path);
2032
2033 Register index = locations->InAt(0).AsRegister<Register>();
2034 Register length = locations->InAt(1).AsRegister<Register>();
2035
2036 // length is limited by the maximum positive signed 32-bit integer.
2037 // Unsigned comparison of length and index checks for index < 0
2038 // and for length <= index simultaneously.
2039 __ Bgeu(index, length, slow_path->GetEntryLabel());
2040}
2041
2042void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2043 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2044 instruction,
2045 LocationSummary::kCallOnSlowPath);
2046 locations->SetInAt(0, Location::RequiresRegister());
2047 locations->SetInAt(1, Location::RequiresRegister());
2048 // Note that TypeCheckSlowPathMIPS uses this register too.
2049 locations->AddTemp(Location::RequiresRegister());
2050}
2051
2052void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2053 LocationSummary* locations = instruction->GetLocations();
2054 Register obj = locations->InAt(0).AsRegister<Register>();
2055 Register cls = locations->InAt(1).AsRegister<Register>();
2056 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2057
2058 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2059 codegen_->AddSlowPath(slow_path);
2060
2061 // TODO: avoid this check if we know obj is not null.
2062 __ Beqz(obj, slow_path->GetExitLabel());
2063 // Compare the class of `obj` with `cls`.
2064 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2065 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2066 __ Bind(slow_path->GetExitLabel());
2067}
2068
2069void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2070 LocationSummary* locations =
2071 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2072 locations->SetInAt(0, Location::RequiresRegister());
2073 if (check->HasUses()) {
2074 locations->SetOut(Location::SameAsFirstInput());
2075 }
2076}
2077
2078void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2079 // We assume the class is not null.
2080 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2081 check->GetLoadClass(),
2082 check,
2083 check->GetDexPc(),
2084 true);
2085 codegen_->AddSlowPath(slow_path);
2086 GenerateClassInitializationCheck(slow_path,
2087 check->GetLocations()->InAt(0).AsRegister<Register>());
2088}
2089
2090void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2091 Primitive::Type in_type = compare->InputAt(0)->GetType();
2092
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002093 LocationSummary* locations =
2094 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002095
2096 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002097 case Primitive::kPrimBoolean:
2098 case Primitive::kPrimByte:
2099 case Primitive::kPrimShort:
2100 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002101 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002102 case Primitive::kPrimLong:
2103 locations->SetInAt(0, Location::RequiresRegister());
2104 locations->SetInAt(1, Location::RequiresRegister());
2105 // Output overlaps because it is written before doing the low comparison.
2106 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2107 break;
2108
2109 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002110 case Primitive::kPrimDouble:
2111 locations->SetInAt(0, Location::RequiresFpuRegister());
2112 locations->SetInAt(1, Location::RequiresFpuRegister());
2113 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002114 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002115
2116 default:
2117 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2118 }
2119}
2120
2121void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2122 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002123 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002124 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002125 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002126
2127 // 0 if: left == right
2128 // 1 if: left > right
2129 // -1 if: left < right
2130 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002131 case Primitive::kPrimBoolean:
2132 case Primitive::kPrimByte:
2133 case Primitive::kPrimShort:
2134 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002135 case Primitive::kPrimInt: {
2136 Register lhs = locations->InAt(0).AsRegister<Register>();
2137 Register rhs = locations->InAt(1).AsRegister<Register>();
2138 __ Slt(TMP, lhs, rhs);
2139 __ Slt(res, rhs, lhs);
2140 __ Subu(res, res, TMP);
2141 break;
2142 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002143 case Primitive::kPrimLong: {
2144 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002145 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2146 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2147 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2148 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2149 // TODO: more efficient (direct) comparison with a constant.
2150 __ Slt(TMP, lhs_high, rhs_high);
2151 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2152 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2153 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2154 __ Sltu(TMP, lhs_low, rhs_low);
2155 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2156 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2157 __ Bind(&done);
2158 break;
2159 }
2160
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002161 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002162 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002163 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2164 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2165 MipsLabel done;
2166 if (isR6) {
2167 __ CmpEqS(FTMP, lhs, rhs);
2168 __ LoadConst32(res, 0);
2169 __ Bc1nez(FTMP, &done);
2170 if (gt_bias) {
2171 __ CmpLtS(FTMP, lhs, rhs);
2172 __ LoadConst32(res, -1);
2173 __ Bc1nez(FTMP, &done);
2174 __ LoadConst32(res, 1);
2175 } else {
2176 __ CmpLtS(FTMP, rhs, lhs);
2177 __ LoadConst32(res, 1);
2178 __ Bc1nez(FTMP, &done);
2179 __ LoadConst32(res, -1);
2180 }
2181 } else {
2182 if (gt_bias) {
2183 __ ColtS(0, lhs, rhs);
2184 __ LoadConst32(res, -1);
2185 __ Bc1t(0, &done);
2186 __ CeqS(0, lhs, rhs);
2187 __ LoadConst32(res, 1);
2188 __ Movt(res, ZERO, 0);
2189 } else {
2190 __ ColtS(0, rhs, lhs);
2191 __ LoadConst32(res, 1);
2192 __ Bc1t(0, &done);
2193 __ CeqS(0, lhs, rhs);
2194 __ LoadConst32(res, -1);
2195 __ Movt(res, ZERO, 0);
2196 }
2197 }
2198 __ Bind(&done);
2199 break;
2200 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002201 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002202 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002203 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2204 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2205 MipsLabel done;
2206 if (isR6) {
2207 __ CmpEqD(FTMP, lhs, rhs);
2208 __ LoadConst32(res, 0);
2209 __ Bc1nez(FTMP, &done);
2210 if (gt_bias) {
2211 __ CmpLtD(FTMP, lhs, rhs);
2212 __ LoadConst32(res, -1);
2213 __ Bc1nez(FTMP, &done);
2214 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002215 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002216 __ CmpLtD(FTMP, rhs, lhs);
2217 __ LoadConst32(res, 1);
2218 __ Bc1nez(FTMP, &done);
2219 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002220 }
2221 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002222 if (gt_bias) {
2223 __ ColtD(0, lhs, rhs);
2224 __ LoadConst32(res, -1);
2225 __ Bc1t(0, &done);
2226 __ CeqD(0, lhs, rhs);
2227 __ LoadConst32(res, 1);
2228 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002229 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002230 __ ColtD(0, rhs, lhs);
2231 __ LoadConst32(res, 1);
2232 __ Bc1t(0, &done);
2233 __ CeqD(0, lhs, rhs);
2234 __ LoadConst32(res, -1);
2235 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002236 }
2237 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002238 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002239 break;
2240 }
2241
2242 default:
2243 LOG(FATAL) << "Unimplemented compare type " << in_type;
2244 }
2245}
2246
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002247void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002248 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002249 switch (instruction->InputAt(0)->GetType()) {
2250 default:
2251 case Primitive::kPrimLong:
2252 locations->SetInAt(0, Location::RequiresRegister());
2253 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2254 break;
2255
2256 case Primitive::kPrimFloat:
2257 case Primitive::kPrimDouble:
2258 locations->SetInAt(0, Location::RequiresFpuRegister());
2259 locations->SetInAt(1, Location::RequiresFpuRegister());
2260 break;
2261 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002262 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002263 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2264 }
2265}
2266
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002267void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002268 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002269 return;
2270 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002271
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002272 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002273 LocationSummary* locations = instruction->GetLocations();
2274 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002275 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002276
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002277 switch (type) {
2278 default:
2279 // Integer case.
2280 GenerateIntCompare(instruction->GetCondition(), locations);
2281 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002282
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002283 case Primitive::kPrimLong:
2284 // TODO: don't use branches.
2285 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002286 break;
2287
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002288 case Primitive::kPrimFloat:
2289 case Primitive::kPrimDouble:
2290 // TODO: don't use branches.
2291 GenerateFpCompareAndBranch(instruction->GetCondition(),
2292 instruction->IsGtBias(),
2293 type,
2294 locations,
2295 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002296 break;
2297 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002298
2299 // Convert the branches into the result.
2300 MipsLabel done;
2301
2302 // False case: result = 0.
2303 __ LoadConst32(dst, 0);
2304 __ B(&done);
2305
2306 // True case: result = 1.
2307 __ Bind(&true_label);
2308 __ LoadConst32(dst, 1);
2309 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002310}
2311
Alexey Frunze7e99e052015-11-24 19:28:01 -08002312void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2313 DCHECK(instruction->IsDiv() || instruction->IsRem());
2314 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2315
2316 LocationSummary* locations = instruction->GetLocations();
2317 Location second = locations->InAt(1);
2318 DCHECK(second.IsConstant());
2319
2320 Register out = locations->Out().AsRegister<Register>();
2321 Register dividend = locations->InAt(0).AsRegister<Register>();
2322 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2323 DCHECK(imm == 1 || imm == -1);
2324
2325 if (instruction->IsRem()) {
2326 __ Move(out, ZERO);
2327 } else {
2328 if (imm == -1) {
2329 __ Subu(out, ZERO, dividend);
2330 } else if (out != dividend) {
2331 __ Move(out, dividend);
2332 }
2333 }
2334}
2335
2336void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2337 DCHECK(instruction->IsDiv() || instruction->IsRem());
2338 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2339
2340 LocationSummary* locations = instruction->GetLocations();
2341 Location second = locations->InAt(1);
2342 DCHECK(second.IsConstant());
2343
2344 Register out = locations->Out().AsRegister<Register>();
2345 Register dividend = locations->InAt(0).AsRegister<Register>();
2346 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002347 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002348 int ctz_imm = CTZ(abs_imm);
2349
2350 if (instruction->IsDiv()) {
2351 if (ctz_imm == 1) {
2352 // Fast path for division by +/-2, which is very common.
2353 __ Srl(TMP, dividend, 31);
2354 } else {
2355 __ Sra(TMP, dividend, 31);
2356 __ Srl(TMP, TMP, 32 - ctz_imm);
2357 }
2358 __ Addu(out, dividend, TMP);
2359 __ Sra(out, out, ctz_imm);
2360 if (imm < 0) {
2361 __ Subu(out, ZERO, out);
2362 }
2363 } else {
2364 if (ctz_imm == 1) {
2365 // Fast path for modulo +/-2, which is very common.
2366 __ Sra(TMP, dividend, 31);
2367 __ Subu(out, dividend, TMP);
2368 __ Andi(out, out, 1);
2369 __ Addu(out, out, TMP);
2370 } else {
2371 __ Sra(TMP, dividend, 31);
2372 __ Srl(TMP, TMP, 32 - ctz_imm);
2373 __ Addu(out, dividend, TMP);
2374 if (IsUint<16>(abs_imm - 1)) {
2375 __ Andi(out, out, abs_imm - 1);
2376 } else {
2377 __ Sll(out, out, 32 - ctz_imm);
2378 __ Srl(out, out, 32 - ctz_imm);
2379 }
2380 __ Subu(out, out, TMP);
2381 }
2382 }
2383}
2384
2385void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2386 DCHECK(instruction->IsDiv() || instruction->IsRem());
2387 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2388
2389 LocationSummary* locations = instruction->GetLocations();
2390 Location second = locations->InAt(1);
2391 DCHECK(second.IsConstant());
2392
2393 Register out = locations->Out().AsRegister<Register>();
2394 Register dividend = locations->InAt(0).AsRegister<Register>();
2395 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2396
2397 int64_t magic;
2398 int shift;
2399 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2400
2401 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2402
2403 __ LoadConst32(TMP, magic);
2404 if (isR6) {
2405 __ MuhR6(TMP, dividend, TMP);
2406 } else {
2407 __ MultR2(dividend, TMP);
2408 __ Mfhi(TMP);
2409 }
2410 if (imm > 0 && magic < 0) {
2411 __ Addu(TMP, TMP, dividend);
2412 } else if (imm < 0 && magic > 0) {
2413 __ Subu(TMP, TMP, dividend);
2414 }
2415
2416 if (shift != 0) {
2417 __ Sra(TMP, TMP, shift);
2418 }
2419
2420 if (instruction->IsDiv()) {
2421 __ Sra(out, TMP, 31);
2422 __ Subu(out, TMP, out);
2423 } else {
2424 __ Sra(AT, TMP, 31);
2425 __ Subu(AT, TMP, AT);
2426 __ LoadConst32(TMP, imm);
2427 if (isR6) {
2428 __ MulR6(TMP, AT, TMP);
2429 } else {
2430 __ MulR2(TMP, AT, TMP);
2431 }
2432 __ Subu(out, dividend, TMP);
2433 }
2434}
2435
2436void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2437 DCHECK(instruction->IsDiv() || instruction->IsRem());
2438 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2439
2440 LocationSummary* locations = instruction->GetLocations();
2441 Register out = locations->Out().AsRegister<Register>();
2442 Location second = locations->InAt(1);
2443
2444 if (second.IsConstant()) {
2445 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2446 if (imm == 0) {
2447 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2448 } else if (imm == 1 || imm == -1) {
2449 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002450 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002451 DivRemByPowerOfTwo(instruction);
2452 } else {
2453 DCHECK(imm <= -2 || imm >= 2);
2454 GenerateDivRemWithAnyConstant(instruction);
2455 }
2456 } else {
2457 Register dividend = locations->InAt(0).AsRegister<Register>();
2458 Register divisor = second.AsRegister<Register>();
2459 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2460 if (instruction->IsDiv()) {
2461 if (isR6) {
2462 __ DivR6(out, dividend, divisor);
2463 } else {
2464 __ DivR2(out, dividend, divisor);
2465 }
2466 } else {
2467 if (isR6) {
2468 __ ModR6(out, dividend, divisor);
2469 } else {
2470 __ ModR2(out, dividend, divisor);
2471 }
2472 }
2473 }
2474}
2475
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002476void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2477 Primitive::Type type = div->GetResultType();
2478 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002479 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002480 : LocationSummary::kNoCall;
2481
2482 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2483
2484 switch (type) {
2485 case Primitive::kPrimInt:
2486 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002487 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002488 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2489 break;
2490
2491 case Primitive::kPrimLong: {
2492 InvokeRuntimeCallingConvention calling_convention;
2493 locations->SetInAt(0, Location::RegisterPairLocation(
2494 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2495 locations->SetInAt(1, Location::RegisterPairLocation(
2496 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2497 locations->SetOut(calling_convention.GetReturnLocation(type));
2498 break;
2499 }
2500
2501 case Primitive::kPrimFloat:
2502 case Primitive::kPrimDouble:
2503 locations->SetInAt(0, Location::RequiresFpuRegister());
2504 locations->SetInAt(1, Location::RequiresFpuRegister());
2505 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2506 break;
2507
2508 default:
2509 LOG(FATAL) << "Unexpected div type " << type;
2510 }
2511}
2512
2513void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2514 Primitive::Type type = instruction->GetType();
2515 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002516
2517 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002518 case Primitive::kPrimInt:
2519 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002520 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002521 case Primitive::kPrimLong: {
2522 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2523 instruction,
2524 instruction->GetDexPc(),
2525 nullptr,
2526 IsDirectEntrypoint(kQuickLdiv));
2527 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2528 break;
2529 }
2530 case Primitive::kPrimFloat:
2531 case Primitive::kPrimDouble: {
2532 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2533 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2534 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2535 if (type == Primitive::kPrimFloat) {
2536 __ DivS(dst, lhs, rhs);
2537 } else {
2538 __ DivD(dst, lhs, rhs);
2539 }
2540 break;
2541 }
2542 default:
2543 LOG(FATAL) << "Unexpected div type " << type;
2544 }
2545}
2546
2547void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2548 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2549 ? LocationSummary::kCallOnSlowPath
2550 : LocationSummary::kNoCall;
2551 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2552 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2553 if (instruction->HasUses()) {
2554 locations->SetOut(Location::SameAsFirstInput());
2555 }
2556}
2557
2558void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2559 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2560 codegen_->AddSlowPath(slow_path);
2561 Location value = instruction->GetLocations()->InAt(0);
2562 Primitive::Type type = instruction->GetType();
2563
2564 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002565 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002566 case Primitive::kPrimByte:
2567 case Primitive::kPrimChar:
2568 case Primitive::kPrimShort:
2569 case Primitive::kPrimInt: {
2570 if (value.IsConstant()) {
2571 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2572 __ B(slow_path->GetEntryLabel());
2573 } else {
2574 // A division by a non-null constant is valid. We don't need to perform
2575 // any check, so simply fall through.
2576 }
2577 } else {
2578 DCHECK(value.IsRegister()) << value;
2579 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2580 }
2581 break;
2582 }
2583 case Primitive::kPrimLong: {
2584 if (value.IsConstant()) {
2585 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2586 __ B(slow_path->GetEntryLabel());
2587 } else {
2588 // A division by a non-null constant is valid. We don't need to perform
2589 // any check, so simply fall through.
2590 }
2591 } else {
2592 DCHECK(value.IsRegisterPair()) << value;
2593 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2594 __ Beqz(TMP, slow_path->GetEntryLabel());
2595 }
2596 break;
2597 }
2598 default:
2599 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2600 }
2601}
2602
2603void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2604 LocationSummary* locations =
2605 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2606 locations->SetOut(Location::ConstantLocation(constant));
2607}
2608
2609void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2610 // Will be generated at use site.
2611}
2612
2613void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2614 exit->SetLocations(nullptr);
2615}
2616
2617void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2618}
2619
2620void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2621 LocationSummary* locations =
2622 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2623 locations->SetOut(Location::ConstantLocation(constant));
2624}
2625
2626void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2627 // Will be generated at use site.
2628}
2629
2630void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2631 got->SetLocations(nullptr);
2632}
2633
2634void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2635 DCHECK(!successor->IsExitBlock());
2636 HBasicBlock* block = got->GetBlock();
2637 HInstruction* previous = got->GetPrevious();
2638 HLoopInformation* info = block->GetLoopInformation();
2639
2640 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2641 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2642 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2643 return;
2644 }
2645 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2646 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2647 }
2648 if (!codegen_->GoesToNextBlock(block, successor)) {
2649 __ B(codegen_->GetLabelOf(successor));
2650 }
2651}
2652
2653void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2654 HandleGoto(got, got->GetSuccessor());
2655}
2656
2657void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2658 try_boundary->SetLocations(nullptr);
2659}
2660
2661void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2662 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2663 if (!successor->IsExitBlock()) {
2664 HandleGoto(try_boundary, successor);
2665 }
2666}
2667
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002668void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2669 LocationSummary* locations) {
2670 Register dst = locations->Out().AsRegister<Register>();
2671 Register lhs = locations->InAt(0).AsRegister<Register>();
2672 Location rhs_location = locations->InAt(1);
2673 Register rhs_reg = ZERO;
2674 int64_t rhs_imm = 0;
2675 bool use_imm = rhs_location.IsConstant();
2676 if (use_imm) {
2677 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2678 } else {
2679 rhs_reg = rhs_location.AsRegister<Register>();
2680 }
2681
2682 switch (cond) {
2683 case kCondEQ:
2684 case kCondNE:
2685 if (use_imm && IsUint<16>(rhs_imm)) {
2686 __ Xori(dst, lhs, rhs_imm);
2687 } else {
2688 if (use_imm) {
2689 rhs_reg = TMP;
2690 __ LoadConst32(rhs_reg, rhs_imm);
2691 }
2692 __ Xor(dst, lhs, rhs_reg);
2693 }
2694 if (cond == kCondEQ) {
2695 __ Sltiu(dst, dst, 1);
2696 } else {
2697 __ Sltu(dst, ZERO, dst);
2698 }
2699 break;
2700
2701 case kCondLT:
2702 case kCondGE:
2703 if (use_imm && IsInt<16>(rhs_imm)) {
2704 __ Slti(dst, lhs, rhs_imm);
2705 } else {
2706 if (use_imm) {
2707 rhs_reg = TMP;
2708 __ LoadConst32(rhs_reg, rhs_imm);
2709 }
2710 __ Slt(dst, lhs, rhs_reg);
2711 }
2712 if (cond == kCondGE) {
2713 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2714 // only the slt instruction but no sge.
2715 __ Xori(dst, dst, 1);
2716 }
2717 break;
2718
2719 case kCondLE:
2720 case kCondGT:
2721 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2722 // Simulate lhs <= rhs via lhs < rhs + 1.
2723 __ Slti(dst, lhs, rhs_imm + 1);
2724 if (cond == kCondGT) {
2725 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2726 // only the slti instruction but no sgti.
2727 __ Xori(dst, dst, 1);
2728 }
2729 } else {
2730 if (use_imm) {
2731 rhs_reg = TMP;
2732 __ LoadConst32(rhs_reg, rhs_imm);
2733 }
2734 __ Slt(dst, rhs_reg, lhs);
2735 if (cond == kCondLE) {
2736 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2737 // only the slt instruction but no sle.
2738 __ Xori(dst, dst, 1);
2739 }
2740 }
2741 break;
2742
2743 case kCondB:
2744 case kCondAE:
2745 if (use_imm && IsInt<16>(rhs_imm)) {
2746 // Sltiu sign-extends its 16-bit immediate operand before
2747 // the comparison and thus lets us compare directly with
2748 // unsigned values in the ranges [0, 0x7fff] and
2749 // [0xffff8000, 0xffffffff].
2750 __ Sltiu(dst, lhs, rhs_imm);
2751 } else {
2752 if (use_imm) {
2753 rhs_reg = TMP;
2754 __ LoadConst32(rhs_reg, rhs_imm);
2755 }
2756 __ Sltu(dst, lhs, rhs_reg);
2757 }
2758 if (cond == kCondAE) {
2759 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2760 // only the sltu instruction but no sgeu.
2761 __ Xori(dst, dst, 1);
2762 }
2763 break;
2764
2765 case kCondBE:
2766 case kCondA:
2767 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2768 // Simulate lhs <= rhs via lhs < rhs + 1.
2769 // Note that this only works if rhs + 1 does not overflow
2770 // to 0, hence the check above.
2771 // Sltiu sign-extends its 16-bit immediate operand before
2772 // the comparison and thus lets us compare directly with
2773 // unsigned values in the ranges [0, 0x7fff] and
2774 // [0xffff8000, 0xffffffff].
2775 __ Sltiu(dst, lhs, rhs_imm + 1);
2776 if (cond == kCondA) {
2777 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2778 // only the sltiu instruction but no sgtiu.
2779 __ Xori(dst, dst, 1);
2780 }
2781 } else {
2782 if (use_imm) {
2783 rhs_reg = TMP;
2784 __ LoadConst32(rhs_reg, rhs_imm);
2785 }
2786 __ Sltu(dst, rhs_reg, lhs);
2787 if (cond == kCondBE) {
2788 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2789 // only the sltu instruction but no sleu.
2790 __ Xori(dst, dst, 1);
2791 }
2792 }
2793 break;
2794 }
2795}
2796
2797void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2798 LocationSummary* locations,
2799 MipsLabel* label) {
2800 Register lhs = locations->InAt(0).AsRegister<Register>();
2801 Location rhs_location = locations->InAt(1);
2802 Register rhs_reg = ZERO;
2803 int32_t rhs_imm = 0;
2804 bool use_imm = rhs_location.IsConstant();
2805 if (use_imm) {
2806 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2807 } else {
2808 rhs_reg = rhs_location.AsRegister<Register>();
2809 }
2810
2811 if (use_imm && rhs_imm == 0) {
2812 switch (cond) {
2813 case kCondEQ:
2814 case kCondBE: // <= 0 if zero
2815 __ Beqz(lhs, label);
2816 break;
2817 case kCondNE:
2818 case kCondA: // > 0 if non-zero
2819 __ Bnez(lhs, label);
2820 break;
2821 case kCondLT:
2822 __ Bltz(lhs, label);
2823 break;
2824 case kCondGE:
2825 __ Bgez(lhs, label);
2826 break;
2827 case kCondLE:
2828 __ Blez(lhs, label);
2829 break;
2830 case kCondGT:
2831 __ Bgtz(lhs, label);
2832 break;
2833 case kCondB: // always false
2834 break;
2835 case kCondAE: // always true
2836 __ B(label);
2837 break;
2838 }
2839 } else {
2840 if (use_imm) {
2841 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2842 rhs_reg = TMP;
2843 __ LoadConst32(rhs_reg, rhs_imm);
2844 }
2845 switch (cond) {
2846 case kCondEQ:
2847 __ Beq(lhs, rhs_reg, label);
2848 break;
2849 case kCondNE:
2850 __ Bne(lhs, rhs_reg, label);
2851 break;
2852 case kCondLT:
2853 __ Blt(lhs, rhs_reg, label);
2854 break;
2855 case kCondGE:
2856 __ Bge(lhs, rhs_reg, label);
2857 break;
2858 case kCondLE:
2859 __ Bge(rhs_reg, lhs, label);
2860 break;
2861 case kCondGT:
2862 __ Blt(rhs_reg, lhs, label);
2863 break;
2864 case kCondB:
2865 __ Bltu(lhs, rhs_reg, label);
2866 break;
2867 case kCondAE:
2868 __ Bgeu(lhs, rhs_reg, label);
2869 break;
2870 case kCondBE:
2871 __ Bgeu(rhs_reg, lhs, label);
2872 break;
2873 case kCondA:
2874 __ Bltu(rhs_reg, lhs, label);
2875 break;
2876 }
2877 }
2878}
2879
2880void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2881 LocationSummary* locations,
2882 MipsLabel* label) {
2883 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2884 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2885 Location rhs_location = locations->InAt(1);
2886 Register rhs_high = ZERO;
2887 Register rhs_low = ZERO;
2888 int64_t imm = 0;
2889 uint32_t imm_high = 0;
2890 uint32_t imm_low = 0;
2891 bool use_imm = rhs_location.IsConstant();
2892 if (use_imm) {
2893 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2894 imm_high = High32Bits(imm);
2895 imm_low = Low32Bits(imm);
2896 } else {
2897 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2898 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2899 }
2900
2901 if (use_imm && imm == 0) {
2902 switch (cond) {
2903 case kCondEQ:
2904 case kCondBE: // <= 0 if zero
2905 __ Or(TMP, lhs_high, lhs_low);
2906 __ Beqz(TMP, label);
2907 break;
2908 case kCondNE:
2909 case kCondA: // > 0 if non-zero
2910 __ Or(TMP, lhs_high, lhs_low);
2911 __ Bnez(TMP, label);
2912 break;
2913 case kCondLT:
2914 __ Bltz(lhs_high, label);
2915 break;
2916 case kCondGE:
2917 __ Bgez(lhs_high, label);
2918 break;
2919 case kCondLE:
2920 __ Or(TMP, lhs_high, lhs_low);
2921 __ Sra(AT, lhs_high, 31);
2922 __ Bgeu(AT, TMP, label);
2923 break;
2924 case kCondGT:
2925 __ Or(TMP, lhs_high, lhs_low);
2926 __ Sra(AT, lhs_high, 31);
2927 __ Bltu(AT, TMP, label);
2928 break;
2929 case kCondB: // always false
2930 break;
2931 case kCondAE: // always true
2932 __ B(label);
2933 break;
2934 }
2935 } else if (use_imm) {
2936 // TODO: more efficient comparison with constants without loading them into TMP/AT.
2937 switch (cond) {
2938 case kCondEQ:
2939 __ LoadConst32(TMP, imm_high);
2940 __ Xor(TMP, TMP, lhs_high);
2941 __ LoadConst32(AT, imm_low);
2942 __ Xor(AT, AT, lhs_low);
2943 __ Or(TMP, TMP, AT);
2944 __ Beqz(TMP, label);
2945 break;
2946 case kCondNE:
2947 __ LoadConst32(TMP, imm_high);
2948 __ Xor(TMP, TMP, lhs_high);
2949 __ LoadConst32(AT, imm_low);
2950 __ Xor(AT, AT, lhs_low);
2951 __ Or(TMP, TMP, AT);
2952 __ Bnez(TMP, label);
2953 break;
2954 case kCondLT:
2955 __ LoadConst32(TMP, imm_high);
2956 __ Blt(lhs_high, TMP, label);
2957 __ Slt(TMP, TMP, lhs_high);
2958 __ LoadConst32(AT, imm_low);
2959 __ Sltu(AT, lhs_low, AT);
2960 __ Blt(TMP, AT, label);
2961 break;
2962 case kCondGE:
2963 __ LoadConst32(TMP, imm_high);
2964 __ Blt(TMP, lhs_high, label);
2965 __ Slt(TMP, lhs_high, TMP);
2966 __ LoadConst32(AT, imm_low);
2967 __ Sltu(AT, lhs_low, AT);
2968 __ Or(TMP, TMP, AT);
2969 __ Beqz(TMP, label);
2970 break;
2971 case kCondLE:
2972 __ LoadConst32(TMP, imm_high);
2973 __ Blt(lhs_high, TMP, label);
2974 __ Slt(TMP, TMP, lhs_high);
2975 __ LoadConst32(AT, imm_low);
2976 __ Sltu(AT, AT, lhs_low);
2977 __ Or(TMP, TMP, AT);
2978 __ Beqz(TMP, label);
2979 break;
2980 case kCondGT:
2981 __ LoadConst32(TMP, imm_high);
2982 __ Blt(TMP, lhs_high, label);
2983 __ Slt(TMP, lhs_high, TMP);
2984 __ LoadConst32(AT, imm_low);
2985 __ Sltu(AT, AT, lhs_low);
2986 __ Blt(TMP, AT, label);
2987 break;
2988 case kCondB:
2989 __ LoadConst32(TMP, imm_high);
2990 __ Bltu(lhs_high, TMP, label);
2991 __ Sltu(TMP, TMP, lhs_high);
2992 __ LoadConst32(AT, imm_low);
2993 __ Sltu(AT, lhs_low, AT);
2994 __ Blt(TMP, AT, label);
2995 break;
2996 case kCondAE:
2997 __ LoadConst32(TMP, imm_high);
2998 __ Bltu(TMP, lhs_high, label);
2999 __ Sltu(TMP, lhs_high, TMP);
3000 __ LoadConst32(AT, imm_low);
3001 __ Sltu(AT, lhs_low, AT);
3002 __ Or(TMP, TMP, AT);
3003 __ Beqz(TMP, label);
3004 break;
3005 case kCondBE:
3006 __ LoadConst32(TMP, imm_high);
3007 __ Bltu(lhs_high, TMP, label);
3008 __ Sltu(TMP, TMP, lhs_high);
3009 __ LoadConst32(AT, imm_low);
3010 __ Sltu(AT, AT, lhs_low);
3011 __ Or(TMP, TMP, AT);
3012 __ Beqz(TMP, label);
3013 break;
3014 case kCondA:
3015 __ LoadConst32(TMP, imm_high);
3016 __ Bltu(TMP, lhs_high, label);
3017 __ Sltu(TMP, lhs_high, TMP);
3018 __ LoadConst32(AT, imm_low);
3019 __ Sltu(AT, AT, lhs_low);
3020 __ Blt(TMP, AT, label);
3021 break;
3022 }
3023 } else {
3024 switch (cond) {
3025 case kCondEQ:
3026 __ Xor(TMP, lhs_high, rhs_high);
3027 __ Xor(AT, lhs_low, rhs_low);
3028 __ Or(TMP, TMP, AT);
3029 __ Beqz(TMP, label);
3030 break;
3031 case kCondNE:
3032 __ Xor(TMP, lhs_high, rhs_high);
3033 __ Xor(AT, lhs_low, rhs_low);
3034 __ Or(TMP, TMP, AT);
3035 __ Bnez(TMP, label);
3036 break;
3037 case kCondLT:
3038 __ Blt(lhs_high, rhs_high, label);
3039 __ Slt(TMP, rhs_high, lhs_high);
3040 __ Sltu(AT, lhs_low, rhs_low);
3041 __ Blt(TMP, AT, label);
3042 break;
3043 case kCondGE:
3044 __ Blt(rhs_high, lhs_high, label);
3045 __ Slt(TMP, lhs_high, rhs_high);
3046 __ Sltu(AT, lhs_low, rhs_low);
3047 __ Or(TMP, TMP, AT);
3048 __ Beqz(TMP, label);
3049 break;
3050 case kCondLE:
3051 __ Blt(lhs_high, rhs_high, label);
3052 __ Slt(TMP, rhs_high, lhs_high);
3053 __ Sltu(AT, rhs_low, lhs_low);
3054 __ Or(TMP, TMP, AT);
3055 __ Beqz(TMP, label);
3056 break;
3057 case kCondGT:
3058 __ Blt(rhs_high, lhs_high, label);
3059 __ Slt(TMP, lhs_high, rhs_high);
3060 __ Sltu(AT, rhs_low, lhs_low);
3061 __ Blt(TMP, AT, label);
3062 break;
3063 case kCondB:
3064 __ Bltu(lhs_high, rhs_high, label);
3065 __ Sltu(TMP, rhs_high, lhs_high);
3066 __ Sltu(AT, lhs_low, rhs_low);
3067 __ Blt(TMP, AT, label);
3068 break;
3069 case kCondAE:
3070 __ Bltu(rhs_high, lhs_high, label);
3071 __ Sltu(TMP, lhs_high, rhs_high);
3072 __ Sltu(AT, lhs_low, rhs_low);
3073 __ Or(TMP, TMP, AT);
3074 __ Beqz(TMP, label);
3075 break;
3076 case kCondBE:
3077 __ Bltu(lhs_high, rhs_high, label);
3078 __ Sltu(TMP, rhs_high, lhs_high);
3079 __ Sltu(AT, rhs_low, lhs_low);
3080 __ Or(TMP, TMP, AT);
3081 __ Beqz(TMP, label);
3082 break;
3083 case kCondA:
3084 __ Bltu(rhs_high, lhs_high, label);
3085 __ Sltu(TMP, lhs_high, rhs_high);
3086 __ Sltu(AT, rhs_low, lhs_low);
3087 __ Blt(TMP, AT, label);
3088 break;
3089 }
3090 }
3091}
3092
3093void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3094 bool gt_bias,
3095 Primitive::Type type,
3096 LocationSummary* locations,
3097 MipsLabel* label) {
3098 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3099 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3100 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3101 if (type == Primitive::kPrimFloat) {
3102 if (isR6) {
3103 switch (cond) {
3104 case kCondEQ:
3105 __ CmpEqS(FTMP, lhs, rhs);
3106 __ Bc1nez(FTMP, label);
3107 break;
3108 case kCondNE:
3109 __ CmpEqS(FTMP, lhs, rhs);
3110 __ Bc1eqz(FTMP, label);
3111 break;
3112 case kCondLT:
3113 if (gt_bias) {
3114 __ CmpLtS(FTMP, lhs, rhs);
3115 } else {
3116 __ CmpUltS(FTMP, lhs, rhs);
3117 }
3118 __ Bc1nez(FTMP, label);
3119 break;
3120 case kCondLE:
3121 if (gt_bias) {
3122 __ CmpLeS(FTMP, lhs, rhs);
3123 } else {
3124 __ CmpUleS(FTMP, lhs, rhs);
3125 }
3126 __ Bc1nez(FTMP, label);
3127 break;
3128 case kCondGT:
3129 if (gt_bias) {
3130 __ CmpUltS(FTMP, rhs, lhs);
3131 } else {
3132 __ CmpLtS(FTMP, rhs, lhs);
3133 }
3134 __ Bc1nez(FTMP, label);
3135 break;
3136 case kCondGE:
3137 if (gt_bias) {
3138 __ CmpUleS(FTMP, rhs, lhs);
3139 } else {
3140 __ CmpLeS(FTMP, rhs, lhs);
3141 }
3142 __ Bc1nez(FTMP, label);
3143 break;
3144 default:
3145 LOG(FATAL) << "Unexpected non-floating-point condition";
3146 }
3147 } else {
3148 switch (cond) {
3149 case kCondEQ:
3150 __ CeqS(0, lhs, rhs);
3151 __ Bc1t(0, label);
3152 break;
3153 case kCondNE:
3154 __ CeqS(0, lhs, rhs);
3155 __ Bc1f(0, label);
3156 break;
3157 case kCondLT:
3158 if (gt_bias) {
3159 __ ColtS(0, lhs, rhs);
3160 } else {
3161 __ CultS(0, lhs, rhs);
3162 }
3163 __ Bc1t(0, label);
3164 break;
3165 case kCondLE:
3166 if (gt_bias) {
3167 __ ColeS(0, lhs, rhs);
3168 } else {
3169 __ CuleS(0, lhs, rhs);
3170 }
3171 __ Bc1t(0, label);
3172 break;
3173 case kCondGT:
3174 if (gt_bias) {
3175 __ CultS(0, rhs, lhs);
3176 } else {
3177 __ ColtS(0, rhs, lhs);
3178 }
3179 __ Bc1t(0, label);
3180 break;
3181 case kCondGE:
3182 if (gt_bias) {
3183 __ CuleS(0, rhs, lhs);
3184 } else {
3185 __ ColeS(0, rhs, lhs);
3186 }
3187 __ Bc1t(0, label);
3188 break;
3189 default:
3190 LOG(FATAL) << "Unexpected non-floating-point condition";
3191 }
3192 }
3193 } else {
3194 DCHECK_EQ(type, Primitive::kPrimDouble);
3195 if (isR6) {
3196 switch (cond) {
3197 case kCondEQ:
3198 __ CmpEqD(FTMP, lhs, rhs);
3199 __ Bc1nez(FTMP, label);
3200 break;
3201 case kCondNE:
3202 __ CmpEqD(FTMP, lhs, rhs);
3203 __ Bc1eqz(FTMP, label);
3204 break;
3205 case kCondLT:
3206 if (gt_bias) {
3207 __ CmpLtD(FTMP, lhs, rhs);
3208 } else {
3209 __ CmpUltD(FTMP, lhs, rhs);
3210 }
3211 __ Bc1nez(FTMP, label);
3212 break;
3213 case kCondLE:
3214 if (gt_bias) {
3215 __ CmpLeD(FTMP, lhs, rhs);
3216 } else {
3217 __ CmpUleD(FTMP, lhs, rhs);
3218 }
3219 __ Bc1nez(FTMP, label);
3220 break;
3221 case kCondGT:
3222 if (gt_bias) {
3223 __ CmpUltD(FTMP, rhs, lhs);
3224 } else {
3225 __ CmpLtD(FTMP, rhs, lhs);
3226 }
3227 __ Bc1nez(FTMP, label);
3228 break;
3229 case kCondGE:
3230 if (gt_bias) {
3231 __ CmpUleD(FTMP, rhs, lhs);
3232 } else {
3233 __ CmpLeD(FTMP, rhs, lhs);
3234 }
3235 __ Bc1nez(FTMP, label);
3236 break;
3237 default:
3238 LOG(FATAL) << "Unexpected non-floating-point condition";
3239 }
3240 } else {
3241 switch (cond) {
3242 case kCondEQ:
3243 __ CeqD(0, lhs, rhs);
3244 __ Bc1t(0, label);
3245 break;
3246 case kCondNE:
3247 __ CeqD(0, lhs, rhs);
3248 __ Bc1f(0, label);
3249 break;
3250 case kCondLT:
3251 if (gt_bias) {
3252 __ ColtD(0, lhs, rhs);
3253 } else {
3254 __ CultD(0, lhs, rhs);
3255 }
3256 __ Bc1t(0, label);
3257 break;
3258 case kCondLE:
3259 if (gt_bias) {
3260 __ ColeD(0, lhs, rhs);
3261 } else {
3262 __ CuleD(0, lhs, rhs);
3263 }
3264 __ Bc1t(0, label);
3265 break;
3266 case kCondGT:
3267 if (gt_bias) {
3268 __ CultD(0, rhs, lhs);
3269 } else {
3270 __ ColtD(0, rhs, lhs);
3271 }
3272 __ Bc1t(0, label);
3273 break;
3274 case kCondGE:
3275 if (gt_bias) {
3276 __ CuleD(0, rhs, lhs);
3277 } else {
3278 __ ColeD(0, rhs, lhs);
3279 }
3280 __ Bc1t(0, label);
3281 break;
3282 default:
3283 LOG(FATAL) << "Unexpected non-floating-point condition";
3284 }
3285 }
3286 }
3287}
3288
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003289void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003290 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003291 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003292 MipsLabel* false_target) {
3293 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003294
David Brazdil0debae72015-11-12 18:37:00 +00003295 if (true_target == nullptr && false_target == nullptr) {
3296 // Nothing to do. The code always falls through.
3297 return;
3298 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003299 // Constant condition, statically compared against "true" (integer value 1).
3300 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003301 if (true_target != nullptr) {
3302 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003303 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003304 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003305 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003306 if (false_target != nullptr) {
3307 __ B(false_target);
3308 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003309 }
David Brazdil0debae72015-11-12 18:37:00 +00003310 return;
3311 }
3312
3313 // The following code generates these patterns:
3314 // (1) true_target == nullptr && false_target != nullptr
3315 // - opposite condition true => branch to false_target
3316 // (2) true_target != nullptr && false_target == nullptr
3317 // - condition true => branch to true_target
3318 // (3) true_target != nullptr && false_target != nullptr
3319 // - condition true => branch to true_target
3320 // - branch to false_target
3321 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003322 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003323 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003324 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003325 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003326 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3327 } else {
3328 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3329 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003330 } else {
3331 // The condition instruction has not been materialized, use its inputs as
3332 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003333 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003334 Primitive::Type type = condition->InputAt(0)->GetType();
3335 LocationSummary* locations = cond->GetLocations();
3336 IfCondition if_cond = condition->GetCondition();
3337 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003338
David Brazdil0debae72015-11-12 18:37:00 +00003339 if (true_target == nullptr) {
3340 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003341 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003342 }
3343
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003344 switch (type) {
3345 default:
3346 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3347 break;
3348 case Primitive::kPrimLong:
3349 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3350 break;
3351 case Primitive::kPrimFloat:
3352 case Primitive::kPrimDouble:
3353 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3354 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003355 }
3356 }
David Brazdil0debae72015-11-12 18:37:00 +00003357
3358 // If neither branch falls through (case 3), the conditional branch to `true_target`
3359 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3360 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003361 __ B(false_target);
3362 }
3363}
3364
3365void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3366 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003367 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003368 locations->SetInAt(0, Location::RequiresRegister());
3369 }
3370}
3371
3372void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003373 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3374 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3375 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3376 nullptr : codegen_->GetLabelOf(true_successor);
3377 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3378 nullptr : codegen_->GetLabelOf(false_successor);
3379 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003380}
3381
3382void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3383 LocationSummary* locations = new (GetGraph()->GetArena())
3384 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003385 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003386 locations->SetInAt(0, Location::RequiresRegister());
3387 }
3388}
3389
3390void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003391 SlowPathCodeMIPS* slow_path =
3392 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003393 GenerateTestAndBranch(deoptimize,
3394 /* condition_input_index */ 0,
3395 slow_path->GetEntryLabel(),
3396 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003397}
3398
David Brazdil74eb1b22015-12-14 11:44:01 +00003399void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3400 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3401 if (Primitive::IsFloatingPointType(select->GetType())) {
3402 locations->SetInAt(0, Location::RequiresFpuRegister());
3403 locations->SetInAt(1, Location::RequiresFpuRegister());
3404 } else {
3405 locations->SetInAt(0, Location::RequiresRegister());
3406 locations->SetInAt(1, Location::RequiresRegister());
3407 }
3408 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3409 locations->SetInAt(2, Location::RequiresRegister());
3410 }
3411 locations->SetOut(Location::SameAsFirstInput());
3412}
3413
3414void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3415 LocationSummary* locations = select->GetLocations();
3416 MipsLabel false_target;
3417 GenerateTestAndBranch(select,
3418 /* condition_input_index */ 2,
3419 /* true_target */ nullptr,
3420 &false_target);
3421 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3422 __ Bind(&false_target);
3423}
3424
David Srbecky0cf44932015-12-09 14:09:59 +00003425void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3426 new (GetGraph()->GetArena()) LocationSummary(info);
3427}
3428
David Srbeckyd28f4a02016-03-14 17:14:24 +00003429void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3430 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003431}
3432
3433void CodeGeneratorMIPS::GenerateNop() {
3434 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003435}
3436
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003437void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3438 Primitive::Type field_type = field_info.GetFieldType();
3439 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3440 bool generate_volatile = field_info.IsVolatile() && is_wide;
3441 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003442 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003443
3444 locations->SetInAt(0, Location::RequiresRegister());
3445 if (generate_volatile) {
3446 InvokeRuntimeCallingConvention calling_convention;
3447 // need A0 to hold base + offset
3448 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3449 if (field_type == Primitive::kPrimLong) {
3450 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3451 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003452 // Use Location::Any() to prevent situations when running out of available fp registers.
3453 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003454 // Need some temp core regs since FP results are returned in core registers
3455 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3456 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3457 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3458 }
3459 } else {
3460 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3461 locations->SetOut(Location::RequiresFpuRegister());
3462 } else {
3463 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3464 }
3465 }
3466}
3467
3468void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3469 const FieldInfo& field_info,
3470 uint32_t dex_pc) {
3471 Primitive::Type type = field_info.GetFieldType();
3472 LocationSummary* locations = instruction->GetLocations();
3473 Register obj = locations->InAt(0).AsRegister<Register>();
3474 LoadOperandType load_type = kLoadUnsignedByte;
3475 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003476 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003477
3478 switch (type) {
3479 case Primitive::kPrimBoolean:
3480 load_type = kLoadUnsignedByte;
3481 break;
3482 case Primitive::kPrimByte:
3483 load_type = kLoadSignedByte;
3484 break;
3485 case Primitive::kPrimShort:
3486 load_type = kLoadSignedHalfword;
3487 break;
3488 case Primitive::kPrimChar:
3489 load_type = kLoadUnsignedHalfword;
3490 break;
3491 case Primitive::kPrimInt:
3492 case Primitive::kPrimFloat:
3493 case Primitive::kPrimNot:
3494 load_type = kLoadWord;
3495 break;
3496 case Primitive::kPrimLong:
3497 case Primitive::kPrimDouble:
3498 load_type = kLoadDoubleword;
3499 break;
3500 case Primitive::kPrimVoid:
3501 LOG(FATAL) << "Unreachable type " << type;
3502 UNREACHABLE();
3503 }
3504
3505 if (is_volatile && load_type == kLoadDoubleword) {
3506 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003507 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003508 // Do implicit Null check
3509 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3510 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3511 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3512 instruction,
3513 dex_pc,
3514 nullptr,
3515 IsDirectEntrypoint(kQuickA64Load));
3516 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3517 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003518 // FP results are returned in core registers. Need to move them.
3519 Location out = locations->Out();
3520 if (out.IsFpuRegister()) {
3521 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
3522 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3523 out.AsFpuRegister<FRegister>());
3524 } else {
3525 DCHECK(out.IsDoubleStackSlot());
3526 __ StoreToOffset(kStoreWord,
3527 locations->GetTemp(1).AsRegister<Register>(),
3528 SP,
3529 out.GetStackIndex());
3530 __ StoreToOffset(kStoreWord,
3531 locations->GetTemp(2).AsRegister<Register>(),
3532 SP,
3533 out.GetStackIndex() + 4);
3534 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003535 }
3536 } else {
3537 if (!Primitive::IsFloatingPointType(type)) {
3538 Register dst;
3539 if (type == Primitive::kPrimLong) {
3540 DCHECK(locations->Out().IsRegisterPair());
3541 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003542 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3543 if (obj == dst) {
3544 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3545 codegen_->MaybeRecordImplicitNullCheck(instruction);
3546 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3547 } else {
3548 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3549 codegen_->MaybeRecordImplicitNullCheck(instruction);
3550 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3551 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003552 } else {
3553 DCHECK(locations->Out().IsRegister());
3554 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003555 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003556 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003557 } else {
3558 DCHECK(locations->Out().IsFpuRegister());
3559 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3560 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003561 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003562 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003563 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003564 }
3565 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003566 // Longs are handled earlier.
3567 if (type != Primitive::kPrimLong) {
3568 codegen_->MaybeRecordImplicitNullCheck(instruction);
3569 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003570 }
3571
3572 if (is_volatile) {
3573 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3574 }
3575}
3576
3577void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3578 Primitive::Type field_type = field_info.GetFieldType();
3579 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3580 bool generate_volatile = field_info.IsVolatile() && is_wide;
3581 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003582 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003583
3584 locations->SetInAt(0, Location::RequiresRegister());
3585 if (generate_volatile) {
3586 InvokeRuntimeCallingConvention calling_convention;
3587 // need A0 to hold base + offset
3588 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3589 if (field_type == Primitive::kPrimLong) {
3590 locations->SetInAt(1, Location::RegisterPairLocation(
3591 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3592 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003593 // Use Location::Any() to prevent situations when running out of available fp registers.
3594 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003595 // Pass FP parameters in core registers.
3596 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3597 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3598 }
3599 } else {
3600 if (Primitive::IsFloatingPointType(field_type)) {
3601 locations->SetInAt(1, Location::RequiresFpuRegister());
3602 } else {
3603 locations->SetInAt(1, Location::RequiresRegister());
3604 }
3605 }
3606}
3607
3608void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3609 const FieldInfo& field_info,
3610 uint32_t dex_pc) {
3611 Primitive::Type type = field_info.GetFieldType();
3612 LocationSummary* locations = instruction->GetLocations();
3613 Register obj = locations->InAt(0).AsRegister<Register>();
3614 StoreOperandType store_type = kStoreByte;
3615 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003616 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003617
3618 switch (type) {
3619 case Primitive::kPrimBoolean:
3620 case Primitive::kPrimByte:
3621 store_type = kStoreByte;
3622 break;
3623 case Primitive::kPrimShort:
3624 case Primitive::kPrimChar:
3625 store_type = kStoreHalfword;
3626 break;
3627 case Primitive::kPrimInt:
3628 case Primitive::kPrimFloat:
3629 case Primitive::kPrimNot:
3630 store_type = kStoreWord;
3631 break;
3632 case Primitive::kPrimLong:
3633 case Primitive::kPrimDouble:
3634 store_type = kStoreDoubleword;
3635 break;
3636 case Primitive::kPrimVoid:
3637 LOG(FATAL) << "Unreachable type " << type;
3638 UNREACHABLE();
3639 }
3640
3641 if (is_volatile) {
3642 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3643 }
3644
3645 if (is_volatile && store_type == kStoreDoubleword) {
3646 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003647 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003648 // Do implicit Null check.
3649 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3650 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3651 if (type == Primitive::kPrimDouble) {
3652 // Pass FP parameters in core registers.
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003653 Location in = locations->InAt(1);
3654 if (in.IsFpuRegister()) {
3655 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(), in.AsFpuRegister<FRegister>());
3656 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3657 in.AsFpuRegister<FRegister>());
3658 } else if (in.IsDoubleStackSlot()) {
3659 __ LoadFromOffset(kLoadWord,
3660 locations->GetTemp(1).AsRegister<Register>(),
3661 SP,
3662 in.GetStackIndex());
3663 __ LoadFromOffset(kLoadWord,
3664 locations->GetTemp(2).AsRegister<Register>(),
3665 SP,
3666 in.GetStackIndex() + 4);
3667 } else {
3668 DCHECK(in.IsConstant());
3669 DCHECK(in.GetConstant()->IsDoubleConstant());
3670 int64_t value = bit_cast<int64_t, double>(in.GetConstant()->AsDoubleConstant()->GetValue());
3671 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
3672 locations->GetTemp(1).AsRegister<Register>(),
3673 value);
3674 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003675 }
3676 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3677 instruction,
3678 dex_pc,
3679 nullptr,
3680 IsDirectEntrypoint(kQuickA64Store));
3681 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3682 } else {
3683 if (!Primitive::IsFloatingPointType(type)) {
3684 Register src;
3685 if (type == Primitive::kPrimLong) {
3686 DCHECK(locations->InAt(1).IsRegisterPair());
3687 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003688 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3689 __ StoreToOffset(kStoreWord, src, obj, offset);
3690 codegen_->MaybeRecordImplicitNullCheck(instruction);
3691 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003692 } else {
3693 DCHECK(locations->InAt(1).IsRegister());
3694 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003695 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003696 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003697 } else {
3698 DCHECK(locations->InAt(1).IsFpuRegister());
3699 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3700 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003701 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003702 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003703 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003704 }
3705 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003706 // Longs are handled earlier.
3707 if (type != Primitive::kPrimLong) {
3708 codegen_->MaybeRecordImplicitNullCheck(instruction);
3709 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003710 }
3711
3712 // TODO: memory barriers?
3713 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3714 DCHECK(locations->InAt(1).IsRegister());
3715 Register src = locations->InAt(1).AsRegister<Register>();
3716 codegen_->MarkGCCard(obj, src);
3717 }
3718
3719 if (is_volatile) {
3720 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3721 }
3722}
3723
3724void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3725 HandleFieldGet(instruction, instruction->GetFieldInfo());
3726}
3727
3728void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3729 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3730}
3731
3732void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3733 HandleFieldSet(instruction, instruction->GetFieldInfo());
3734}
3735
3736void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3737 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3738}
3739
3740void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3741 LocationSummary::CallKind call_kind =
3742 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3743 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3744 locations->SetInAt(0, Location::RequiresRegister());
3745 locations->SetInAt(1, Location::RequiresRegister());
3746 // The output does overlap inputs.
3747 // Note that TypeCheckSlowPathMIPS uses this register too.
3748 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3749}
3750
3751void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3752 LocationSummary* locations = instruction->GetLocations();
3753 Register obj = locations->InAt(0).AsRegister<Register>();
3754 Register cls = locations->InAt(1).AsRegister<Register>();
3755 Register out = locations->Out().AsRegister<Register>();
3756
3757 MipsLabel done;
3758
3759 // Return 0 if `obj` is null.
3760 // TODO: Avoid this check if we know `obj` is not null.
3761 __ Move(out, ZERO);
3762 __ Beqz(obj, &done);
3763
3764 // Compare the class of `obj` with `cls`.
3765 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3766 if (instruction->IsExactCheck()) {
3767 // Classes must be equal for the instanceof to succeed.
3768 __ Xor(out, out, cls);
3769 __ Sltiu(out, out, 1);
3770 } else {
3771 // If the classes are not equal, we go into a slow path.
3772 DCHECK(locations->OnlyCallsOnSlowPath());
3773 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3774 codegen_->AddSlowPath(slow_path);
3775 __ Bne(out, cls, slow_path->GetEntryLabel());
3776 __ LoadConst32(out, 1);
3777 __ Bind(slow_path->GetExitLabel());
3778 }
3779
3780 __ Bind(&done);
3781}
3782
3783void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3784 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3785 locations->SetOut(Location::ConstantLocation(constant));
3786}
3787
3788void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3789 // Will be generated at use site.
3790}
3791
3792void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3793 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3794 locations->SetOut(Location::ConstantLocation(constant));
3795}
3796
3797void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3798 // Will be generated at use site.
3799}
3800
3801void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3802 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3803 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3804}
3805
3806void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3807 HandleInvoke(invoke);
3808 // The register T0 is required to be used for the hidden argument in
3809 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3810 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3811}
3812
3813void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3814 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3815 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003816 Location receiver = invoke->GetLocations()->InAt(0);
3817 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3818 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3819
3820 // Set the hidden argument.
3821 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3822 invoke->GetDexMethodIndex());
3823
3824 // temp = object->GetClass();
3825 if (receiver.IsStackSlot()) {
3826 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3827 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3828 } else {
3829 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3830 }
3831 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003832 __ LoadFromOffset(kLoadWord, temp, temp,
3833 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
3834 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003835 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003836 // temp = temp->GetImtEntryAt(method_offset);
3837 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3838 // T9 = temp->GetEntryPoint();
3839 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3840 // T9();
3841 __ Jalr(T9);
3842 __ Nop();
3843 DCHECK(!codegen_->IsLeafMethod());
3844 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3845}
3846
3847void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003848 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3849 if (intrinsic.TryDispatch(invoke)) {
3850 return;
3851 }
3852
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003853 HandleInvoke(invoke);
3854}
3855
3856void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003857 // Explicit clinit checks triggered by static invokes must have been pruned by
3858 // art::PrepareForRegisterAllocation.
3859 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003860
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003861 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3862 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3863 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3864
3865 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
3866 // R6 has PC-relative addressing.
3867 bool has_extra_input = !isR6 &&
3868 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
3869 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
3870
3871 if (invoke->HasPcRelativeDexCache()) {
3872 // kDexCachePcRelative is mutually exclusive with
3873 // kDirectAddressWithFixup/kCallDirectWithFixup.
3874 CHECK(!has_extra_input);
3875 has_extra_input = true;
3876 }
3877
Chris Larsen701566a2015-10-27 15:29:13 -07003878 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3879 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003880 if (invoke->GetLocations()->CanCall() && has_extra_input) {
3881 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3882 }
Chris Larsen701566a2015-10-27 15:29:13 -07003883 return;
3884 }
3885
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003886 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003887
3888 // Add the extra input register if either the dex cache array base register
3889 // or the PC-relative base register for accessing literals is needed.
3890 if (has_extra_input) {
3891 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3892 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003893}
3894
Chris Larsen701566a2015-10-27 15:29:13 -07003895static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003896 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003897 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3898 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003899 return true;
3900 }
3901 return false;
3902}
3903
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003904HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
3905 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
3906 // TODO: Implement other kinds.
3907 return HLoadString::LoadKind::kDexCacheViaMethod;
3908}
3909
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003910HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
3911 HLoadClass::LoadKind desired_class_load_kind) {
3912 DCHECK_NE(desired_class_load_kind, HLoadClass::LoadKind::kReferrersClass);
3913 // TODO: Implement other kinds.
3914 return HLoadClass::LoadKind::kDexCacheViaMethod;
3915}
3916
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003917Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
3918 Register temp) {
3919 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
3920 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
3921 if (!invoke->GetLocations()->Intrinsified()) {
3922 return location.AsRegister<Register>();
3923 }
3924 // For intrinsics we allow any location, so it may be on the stack.
3925 if (!location.IsRegister()) {
3926 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
3927 return temp;
3928 }
3929 // For register locations, check if the register was saved. If so, get it from the stack.
3930 // Note: There is a chance that the register was saved but not overwritten, so we could
3931 // save one load. However, since this is just an intrinsic slow path we prefer this
3932 // simple and more robust approach rather that trying to determine if that's the case.
3933 SlowPathCode* slow_path = GetCurrentSlowPath();
3934 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
3935 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
3936 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
3937 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
3938 return temp;
3939 }
3940 return location.AsRegister<Register>();
3941}
3942
Vladimir Markodc151b22015-10-15 18:02:30 +01003943HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3944 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3945 MethodReference target_method ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003946 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
3947 // We disable PC-relative load when there is an irreducible loop, as the optimization
3948 // is incompatible with it.
3949 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
3950 bool fallback_load = true;
3951 bool fallback_call = true;
3952 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01003953 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3954 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003955 fallback_load = has_irreducible_loops;
3956 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01003957 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003958 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01003959 break;
3960 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003961 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01003962 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003963 fallback_call = has_irreducible_loops;
3964 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01003965 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003966 // TODO: Implement this type.
3967 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01003968 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003969 fallback_call = false;
3970 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01003971 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003972 if (fallback_load) {
3973 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
3974 dispatch_info.method_load_data = 0;
3975 }
3976 if (fallback_call) {
3977 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
3978 dispatch_info.direct_code_ptr = 0;
3979 }
3980 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01003981}
3982
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003983void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3984 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003985 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003986 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3987 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3988 bool isR6 = isa_features_.IsR6();
3989 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
3990 // R6 has PC-relative addressing.
3991 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
3992 (!isR6 &&
3993 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
3994 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
3995 Register base_reg = has_extra_input
3996 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
3997 : ZERO;
3998
3999 // For better instruction scheduling we load the direct code pointer before the method pointer.
4000 switch (code_ptr_location) {
4001 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4002 // T9 = invoke->GetDirectCodePtr();
4003 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4004 break;
4005 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4006 // T9 = code address from literal pool with link-time patch.
4007 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4008 break;
4009 default:
4010 break;
4011 }
4012
4013 switch (method_load_kind) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004014 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4015 // temp = thread->string_init_entrypoint
4016 __ LoadFromOffset(kLoadWord,
4017 temp.AsRegister<Register>(),
4018 TR,
4019 invoke->GetStringInitOffset());
4020 break;
4021 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004022 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004023 break;
4024 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4025 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4026 break;
4027 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004028 __ LoadLiteral(temp.AsRegister<Register>(),
4029 base_reg,
4030 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4031 break;
4032 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4033 HMipsDexCacheArraysBase* base =
4034 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4035 int32_t offset =
4036 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4037 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4038 break;
4039 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004040 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004041 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004042 Register reg = temp.AsRegister<Register>();
4043 Register method_reg;
4044 if (current_method.IsRegister()) {
4045 method_reg = current_method.AsRegister<Register>();
4046 } else {
4047 // TODO: use the appropriate DCHECK() here if possible.
4048 // DCHECK(invoke->GetLocations()->Intrinsified());
4049 DCHECK(!current_method.IsValid());
4050 method_reg = reg;
4051 __ Lw(reg, SP, kCurrentMethodStackOffset);
4052 }
4053
4054 // temp = temp->dex_cache_resolved_methods_;
4055 __ LoadFromOffset(kLoadWord,
4056 reg,
4057 method_reg,
4058 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004059 // temp = temp[index_in_cache];
4060 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4061 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004062 __ LoadFromOffset(kLoadWord,
4063 reg,
4064 reg,
4065 CodeGenerator::GetCachePointerOffset(index_in_cache));
4066 break;
4067 }
4068 }
4069
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004070 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004071 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004072 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004073 break;
4074 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004075 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4076 // T9 prepared above for better instruction scheduling.
4077 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004078 __ Jalr(T9);
4079 __ Nop();
4080 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004081 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004082 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004083 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4084 LOG(FATAL) << "Unsupported";
4085 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004086 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4087 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004088 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004089 T9,
4090 callee_method.AsRegister<Register>(),
4091 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4092 kMipsWordSize).Int32Value());
4093 // T9()
4094 __ Jalr(T9);
4095 __ Nop();
4096 break;
4097 }
4098 DCHECK(!IsLeafMethod());
4099}
4100
4101void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004102 // Explicit clinit checks triggered by static invokes must have been pruned by
4103 // art::PrepareForRegisterAllocation.
4104 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004105
4106 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4107 return;
4108 }
4109
4110 LocationSummary* locations = invoke->GetLocations();
4111 codegen_->GenerateStaticOrDirectCall(invoke,
4112 locations->HasTemps()
4113 ? locations->GetTemp(0)
4114 : Location::NoLocation());
4115 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4116}
4117
Chris Larsen3acee732015-11-18 13:31:08 -08004118void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004119 LocationSummary* locations = invoke->GetLocations();
4120 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08004121 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004122 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4123 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4124 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4125 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4126
4127 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08004128 DCHECK(receiver.IsRegister());
4129 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4130 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004131 // temp = temp->GetMethodAt(method_offset);
4132 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4133 // T9 = temp->GetEntryPoint();
4134 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4135 // T9();
4136 __ Jalr(T9);
4137 __ Nop();
Chris Larsen3acee732015-11-18 13:31:08 -08004138}
4139
4140void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4141 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4142 return;
4143 }
4144
4145 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004146 DCHECK(!codegen_->IsLeafMethod());
4147 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4148}
4149
4150void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01004151 InvokeRuntimeCallingConvention calling_convention;
4152 CodeGenerator::CreateLoadClassLocationSummary(
4153 cls,
4154 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4155 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004156}
4157
4158void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4159 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004160 if (cls->NeedsAccessCheck()) {
4161 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4162 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4163 cls,
4164 cls->GetDexPc(),
4165 nullptr,
4166 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00004167 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004168 return;
4169 }
4170
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004171 Register out = locations->Out().AsRegister<Register>();
4172 Register current_method = locations->InAt(0).AsRegister<Register>();
4173 if (cls->IsReferrersClass()) {
4174 DCHECK(!cls->CanCallRuntime());
4175 DCHECK(!cls->MustGenerateClinitCheck());
4176 __ LoadFromOffset(kLoadWord, out, current_method,
4177 ArtMethod::DeclaringClassOffset().Int32Value());
4178 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004179 __ LoadFromOffset(kLoadWord, out, current_method,
4180 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
4181 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004182
4183 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
4184 DCHECK(cls->CanCallRuntime());
4185 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4186 cls,
4187 cls,
4188 cls->GetDexPc(),
4189 cls->MustGenerateClinitCheck());
4190 codegen_->AddSlowPath(slow_path);
4191 if (!cls->IsInDexCache()) {
4192 __ Beqz(out, slow_path->GetEntryLabel());
4193 }
4194 if (cls->MustGenerateClinitCheck()) {
4195 GenerateClassInitializationCheck(slow_path, out);
4196 } else {
4197 __ Bind(slow_path->GetExitLabel());
4198 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004199 }
4200 }
4201}
4202
4203static int32_t GetExceptionTlsOffset() {
4204 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
4205}
4206
4207void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4208 LocationSummary* locations =
4209 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4210 locations->SetOut(Location::RequiresRegister());
4211}
4212
4213void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4214 Register out = load->GetLocations()->Out().AsRegister<Register>();
4215 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4216}
4217
4218void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4219 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4220}
4221
4222void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4223 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4224}
4225
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004226void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004227 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
4228 ? LocationSummary::kCallOnSlowPath
4229 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004230 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004231 locations->SetInAt(0, Location::RequiresRegister());
4232 locations->SetOut(Location::RequiresRegister());
4233}
4234
4235void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004236 LocationSummary* locations = load->GetLocations();
4237 Register out = locations->Out().AsRegister<Register>();
4238 Register current_method = locations->InAt(0).AsRegister<Register>();
4239 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4240 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4241 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004242
4243 if (!load->IsInDexCache()) {
4244 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4245 codegen_->AddSlowPath(slow_path);
4246 __ Beqz(out, slow_path->GetEntryLabel());
4247 __ Bind(slow_path->GetExitLabel());
4248 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004249}
4250
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004251void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4252 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4253 locations->SetOut(Location::ConstantLocation(constant));
4254}
4255
4256void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4257 // Will be generated at use site.
4258}
4259
4260void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4261 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004262 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004263 InvokeRuntimeCallingConvention calling_convention;
4264 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4265}
4266
4267void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4268 if (instruction->IsEnter()) {
4269 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4270 instruction,
4271 instruction->GetDexPc(),
4272 nullptr,
4273 IsDirectEntrypoint(kQuickLockObject));
4274 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4275 } else {
4276 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4277 instruction,
4278 instruction->GetDexPc(),
4279 nullptr,
4280 IsDirectEntrypoint(kQuickUnlockObject));
4281 }
4282 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4283}
4284
4285void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4286 LocationSummary* locations =
4287 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4288 switch (mul->GetResultType()) {
4289 case Primitive::kPrimInt:
4290 case Primitive::kPrimLong:
4291 locations->SetInAt(0, Location::RequiresRegister());
4292 locations->SetInAt(1, Location::RequiresRegister());
4293 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4294 break;
4295
4296 case Primitive::kPrimFloat:
4297 case Primitive::kPrimDouble:
4298 locations->SetInAt(0, Location::RequiresFpuRegister());
4299 locations->SetInAt(1, Location::RequiresFpuRegister());
4300 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4301 break;
4302
4303 default:
4304 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4305 }
4306}
4307
4308void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4309 Primitive::Type type = instruction->GetType();
4310 LocationSummary* locations = instruction->GetLocations();
4311 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4312
4313 switch (type) {
4314 case Primitive::kPrimInt: {
4315 Register dst = locations->Out().AsRegister<Register>();
4316 Register lhs = locations->InAt(0).AsRegister<Register>();
4317 Register rhs = locations->InAt(1).AsRegister<Register>();
4318
4319 if (isR6) {
4320 __ MulR6(dst, lhs, rhs);
4321 } else {
4322 __ MulR2(dst, lhs, rhs);
4323 }
4324 break;
4325 }
4326 case Primitive::kPrimLong: {
4327 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4328 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4329 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4330 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4331 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4332 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4333
4334 // Extra checks to protect caused by the existance of A1_A2.
4335 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4336 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4337 DCHECK_NE(dst_high, lhs_low);
4338 DCHECK_NE(dst_high, rhs_low);
4339
4340 // A_B * C_D
4341 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4342 // dst_lo: [ low(B*D) ]
4343 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4344
4345 if (isR6) {
4346 __ MulR6(TMP, lhs_high, rhs_low);
4347 __ MulR6(dst_high, lhs_low, rhs_high);
4348 __ Addu(dst_high, dst_high, TMP);
4349 __ MuhuR6(TMP, lhs_low, rhs_low);
4350 __ Addu(dst_high, dst_high, TMP);
4351 __ MulR6(dst_low, lhs_low, rhs_low);
4352 } else {
4353 __ MulR2(TMP, lhs_high, rhs_low);
4354 __ MulR2(dst_high, lhs_low, rhs_high);
4355 __ Addu(dst_high, dst_high, TMP);
4356 __ MultuR2(lhs_low, rhs_low);
4357 __ Mfhi(TMP);
4358 __ Addu(dst_high, dst_high, TMP);
4359 __ Mflo(dst_low);
4360 }
4361 break;
4362 }
4363 case Primitive::kPrimFloat:
4364 case Primitive::kPrimDouble: {
4365 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4366 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4367 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4368 if (type == Primitive::kPrimFloat) {
4369 __ MulS(dst, lhs, rhs);
4370 } else {
4371 __ MulD(dst, lhs, rhs);
4372 }
4373 break;
4374 }
4375 default:
4376 LOG(FATAL) << "Unexpected mul type " << type;
4377 }
4378}
4379
4380void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4381 LocationSummary* locations =
4382 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4383 switch (neg->GetResultType()) {
4384 case Primitive::kPrimInt:
4385 case Primitive::kPrimLong:
4386 locations->SetInAt(0, Location::RequiresRegister());
4387 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4388 break;
4389
4390 case Primitive::kPrimFloat:
4391 case Primitive::kPrimDouble:
4392 locations->SetInAt(0, Location::RequiresFpuRegister());
4393 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4394 break;
4395
4396 default:
4397 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4398 }
4399}
4400
4401void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4402 Primitive::Type type = instruction->GetType();
4403 LocationSummary* locations = instruction->GetLocations();
4404
4405 switch (type) {
4406 case Primitive::kPrimInt: {
4407 Register dst = locations->Out().AsRegister<Register>();
4408 Register src = locations->InAt(0).AsRegister<Register>();
4409 __ Subu(dst, ZERO, src);
4410 break;
4411 }
4412 case Primitive::kPrimLong: {
4413 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4414 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4415 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4416 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4417 __ Subu(dst_low, ZERO, src_low);
4418 __ Sltu(TMP, ZERO, dst_low);
4419 __ Subu(dst_high, ZERO, src_high);
4420 __ Subu(dst_high, dst_high, TMP);
4421 break;
4422 }
4423 case Primitive::kPrimFloat:
4424 case Primitive::kPrimDouble: {
4425 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4426 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4427 if (type == Primitive::kPrimFloat) {
4428 __ NegS(dst, src);
4429 } else {
4430 __ NegD(dst, src);
4431 }
4432 break;
4433 }
4434 default:
4435 LOG(FATAL) << "Unexpected neg type " << type;
4436 }
4437}
4438
4439void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4440 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004441 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004442 InvokeRuntimeCallingConvention calling_convention;
4443 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4444 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4445 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4446 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4447}
4448
4449void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4450 InvokeRuntimeCallingConvention calling_convention;
4451 Register current_method_register = calling_convention.GetRegisterAt(2);
4452 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4453 // Move an uint16_t value to a register.
4454 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4455 codegen_->InvokeRuntime(
4456 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4457 instruction,
4458 instruction->GetDexPc(),
4459 nullptr,
4460 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4461 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4462 void*, uint32_t, int32_t, ArtMethod*>();
4463}
4464
4465void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4466 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004467 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004468 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004469 if (instruction->IsStringAlloc()) {
4470 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4471 } else {
4472 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4473 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4474 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004475 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4476}
4477
4478void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004479 if (instruction->IsStringAlloc()) {
4480 // String is allocated through StringFactory. Call NewEmptyString entry point.
4481 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4482 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4483 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4484 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4485 __ Jalr(T9);
4486 __ Nop();
4487 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4488 } else {
4489 codegen_->InvokeRuntime(
4490 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4491 instruction,
4492 instruction->GetDexPc(),
4493 nullptr,
4494 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4495 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4496 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004497}
4498
4499void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4500 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4501 locations->SetInAt(0, Location::RequiresRegister());
4502 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4503}
4504
4505void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4506 Primitive::Type type = instruction->GetType();
4507 LocationSummary* locations = instruction->GetLocations();
4508
4509 switch (type) {
4510 case Primitive::kPrimInt: {
4511 Register dst = locations->Out().AsRegister<Register>();
4512 Register src = locations->InAt(0).AsRegister<Register>();
4513 __ Nor(dst, src, ZERO);
4514 break;
4515 }
4516
4517 case Primitive::kPrimLong: {
4518 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4519 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4520 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4521 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4522 __ Nor(dst_high, src_high, ZERO);
4523 __ Nor(dst_low, src_low, ZERO);
4524 break;
4525 }
4526
4527 default:
4528 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4529 }
4530}
4531
4532void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4533 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4534 locations->SetInAt(0, Location::RequiresRegister());
4535 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4536}
4537
4538void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4539 LocationSummary* locations = instruction->GetLocations();
4540 __ Xori(locations->Out().AsRegister<Register>(),
4541 locations->InAt(0).AsRegister<Register>(),
4542 1);
4543}
4544
4545void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4546 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4547 ? LocationSummary::kCallOnSlowPath
4548 : LocationSummary::kNoCall;
4549 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4550 locations->SetInAt(0, Location::RequiresRegister());
4551 if (instruction->HasUses()) {
4552 locations->SetOut(Location::SameAsFirstInput());
4553 }
4554}
4555
Calin Juravle2ae48182016-03-16 14:05:09 +00004556void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4557 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004558 return;
4559 }
4560 Location obj = instruction->GetLocations()->InAt(0);
4561
4562 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004563 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004564}
4565
Calin Juravle2ae48182016-03-16 14:05:09 +00004566void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004567 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004568 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004569
4570 Location obj = instruction->GetLocations()->InAt(0);
4571
4572 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4573}
4574
4575void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004576 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004577}
4578
4579void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4580 HandleBinaryOp(instruction);
4581}
4582
4583void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4584 HandleBinaryOp(instruction);
4585}
4586
4587void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4588 LOG(FATAL) << "Unreachable";
4589}
4590
4591void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4592 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4593}
4594
4595void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4596 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4597 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4598 if (location.IsStackSlot()) {
4599 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4600 } else if (location.IsDoubleStackSlot()) {
4601 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4602 }
4603 locations->SetOut(location);
4604}
4605
4606void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4607 ATTRIBUTE_UNUSED) {
4608 // Nothing to do, the parameter is already at its location.
4609}
4610
4611void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4612 LocationSummary* locations =
4613 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4614 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4615}
4616
4617void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4618 ATTRIBUTE_UNUSED) {
4619 // Nothing to do, the method is already at its location.
4620}
4621
4622void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4623 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004624 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004625 locations->SetInAt(i, Location::Any());
4626 }
4627 locations->SetOut(Location::Any());
4628}
4629
4630void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4631 LOG(FATAL) << "Unreachable";
4632}
4633
4634void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4635 Primitive::Type type = rem->GetResultType();
4636 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004637 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004638 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4639
4640 switch (type) {
4641 case Primitive::kPrimInt:
4642 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004643 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004644 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4645 break;
4646
4647 case Primitive::kPrimLong: {
4648 InvokeRuntimeCallingConvention calling_convention;
4649 locations->SetInAt(0, Location::RegisterPairLocation(
4650 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4651 locations->SetInAt(1, Location::RegisterPairLocation(
4652 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4653 locations->SetOut(calling_convention.GetReturnLocation(type));
4654 break;
4655 }
4656
4657 case Primitive::kPrimFloat:
4658 case Primitive::kPrimDouble: {
4659 InvokeRuntimeCallingConvention calling_convention;
4660 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4661 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4662 locations->SetOut(calling_convention.GetReturnLocation(type));
4663 break;
4664 }
4665
4666 default:
4667 LOG(FATAL) << "Unexpected rem type " << type;
4668 }
4669}
4670
4671void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4672 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004673
4674 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004675 case Primitive::kPrimInt:
4676 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004677 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004678 case Primitive::kPrimLong: {
4679 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
4680 instruction,
4681 instruction->GetDexPc(),
4682 nullptr,
4683 IsDirectEntrypoint(kQuickLmod));
4684 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4685 break;
4686 }
4687 case Primitive::kPrimFloat: {
4688 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
4689 instruction, instruction->GetDexPc(),
4690 nullptr,
4691 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00004692 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004693 break;
4694 }
4695 case Primitive::kPrimDouble: {
4696 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
4697 instruction, instruction->GetDexPc(),
4698 nullptr,
4699 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00004700 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004701 break;
4702 }
4703 default:
4704 LOG(FATAL) << "Unexpected rem type " << type;
4705 }
4706}
4707
4708void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4709 memory_barrier->SetLocations(nullptr);
4710}
4711
4712void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4713 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4714}
4715
4716void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
4717 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4718 Primitive::Type return_type = ret->InputAt(0)->GetType();
4719 locations->SetInAt(0, MipsReturnLocation(return_type));
4720}
4721
4722void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4723 codegen_->GenerateFrameExit();
4724}
4725
4726void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
4727 ret->SetLocations(nullptr);
4728}
4729
4730void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4731 codegen_->GenerateFrameExit();
4732}
4733
Alexey Frunze92d90602015-12-18 18:16:36 -08004734void LocationsBuilderMIPS::VisitRor(HRor* ror) {
4735 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004736}
4737
Alexey Frunze92d90602015-12-18 18:16:36 -08004738void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
4739 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004740}
4741
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004742void LocationsBuilderMIPS::VisitShl(HShl* shl) {
4743 HandleShift(shl);
4744}
4745
4746void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
4747 HandleShift(shl);
4748}
4749
4750void LocationsBuilderMIPS::VisitShr(HShr* shr) {
4751 HandleShift(shr);
4752}
4753
4754void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
4755 HandleShift(shr);
4756}
4757
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004758void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
4759 HandleBinaryOp(instruction);
4760}
4761
4762void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
4763 HandleBinaryOp(instruction);
4764}
4765
4766void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4767 HandleFieldGet(instruction, instruction->GetFieldInfo());
4768}
4769
4770void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4771 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4772}
4773
4774void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4775 HandleFieldSet(instruction, instruction->GetFieldInfo());
4776}
4777
4778void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4779 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4780}
4781
4782void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
4783 HUnresolvedInstanceFieldGet* instruction) {
4784 FieldAccessCallingConventionMIPS calling_convention;
4785 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4786 instruction->GetFieldType(),
4787 calling_convention);
4788}
4789
4790void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
4791 HUnresolvedInstanceFieldGet* instruction) {
4792 FieldAccessCallingConventionMIPS calling_convention;
4793 codegen_->GenerateUnresolvedFieldAccess(instruction,
4794 instruction->GetFieldType(),
4795 instruction->GetFieldIndex(),
4796 instruction->GetDexPc(),
4797 calling_convention);
4798}
4799
4800void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4801 HUnresolvedInstanceFieldSet* instruction) {
4802 FieldAccessCallingConventionMIPS calling_convention;
4803 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4804 instruction->GetFieldType(),
4805 calling_convention);
4806}
4807
4808void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4809 HUnresolvedInstanceFieldSet* instruction) {
4810 FieldAccessCallingConventionMIPS calling_convention;
4811 codegen_->GenerateUnresolvedFieldAccess(instruction,
4812 instruction->GetFieldType(),
4813 instruction->GetFieldIndex(),
4814 instruction->GetDexPc(),
4815 calling_convention);
4816}
4817
4818void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4819 HUnresolvedStaticFieldGet* instruction) {
4820 FieldAccessCallingConventionMIPS calling_convention;
4821 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4822 instruction->GetFieldType(),
4823 calling_convention);
4824}
4825
4826void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4827 HUnresolvedStaticFieldGet* instruction) {
4828 FieldAccessCallingConventionMIPS calling_convention;
4829 codegen_->GenerateUnresolvedFieldAccess(instruction,
4830 instruction->GetFieldType(),
4831 instruction->GetFieldIndex(),
4832 instruction->GetDexPc(),
4833 calling_convention);
4834}
4835
4836void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4837 HUnresolvedStaticFieldSet* instruction) {
4838 FieldAccessCallingConventionMIPS calling_convention;
4839 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4840 instruction->GetFieldType(),
4841 calling_convention);
4842}
4843
4844void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4845 HUnresolvedStaticFieldSet* instruction) {
4846 FieldAccessCallingConventionMIPS calling_convention;
4847 codegen_->GenerateUnresolvedFieldAccess(instruction,
4848 instruction->GetFieldType(),
4849 instruction->GetFieldIndex(),
4850 instruction->GetDexPc(),
4851 calling_convention);
4852}
4853
4854void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4855 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4856}
4857
4858void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4859 HBasicBlock* block = instruction->GetBlock();
4860 if (block->GetLoopInformation() != nullptr) {
4861 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4862 // The back edge will generate the suspend check.
4863 return;
4864 }
4865 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4866 // The goto will generate the suspend check.
4867 return;
4868 }
4869 GenerateSuspendCheck(instruction, nullptr);
4870}
4871
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004872void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4873 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004874 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004875 InvokeRuntimeCallingConvention calling_convention;
4876 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4877}
4878
4879void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4880 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4881 instruction,
4882 instruction->GetDexPc(),
4883 nullptr,
4884 IsDirectEntrypoint(kQuickDeliverException));
4885 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4886}
4887
4888void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4889 Primitive::Type input_type = conversion->GetInputType();
4890 Primitive::Type result_type = conversion->GetResultType();
4891 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004892 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004893
4894 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4895 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4896 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4897 }
4898
4899 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004900 if (!isR6 &&
4901 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4902 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004903 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004904 }
4905
4906 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4907
4908 if (call_kind == LocationSummary::kNoCall) {
4909 if (Primitive::IsFloatingPointType(input_type)) {
4910 locations->SetInAt(0, Location::RequiresFpuRegister());
4911 } else {
4912 locations->SetInAt(0, Location::RequiresRegister());
4913 }
4914
4915 if (Primitive::IsFloatingPointType(result_type)) {
4916 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4917 } else {
4918 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4919 }
4920 } else {
4921 InvokeRuntimeCallingConvention calling_convention;
4922
4923 if (Primitive::IsFloatingPointType(input_type)) {
4924 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4925 } else {
4926 DCHECK_EQ(input_type, Primitive::kPrimLong);
4927 locations->SetInAt(0, Location::RegisterPairLocation(
4928 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4929 }
4930
4931 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4932 }
4933}
4934
4935void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4936 LocationSummary* locations = conversion->GetLocations();
4937 Primitive::Type result_type = conversion->GetResultType();
4938 Primitive::Type input_type = conversion->GetInputType();
4939 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004940 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004941
4942 DCHECK_NE(input_type, result_type);
4943
4944 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4945 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4946 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4947 Register src = locations->InAt(0).AsRegister<Register>();
4948
Alexey Frunzea871ef12016-06-27 15:20:11 -07004949 if (dst_low != src) {
4950 __ Move(dst_low, src);
4951 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004952 __ Sra(dst_high, src, 31);
4953 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4954 Register dst = locations->Out().AsRegister<Register>();
4955 Register src = (input_type == Primitive::kPrimLong)
4956 ? locations->InAt(0).AsRegisterPairLow<Register>()
4957 : locations->InAt(0).AsRegister<Register>();
4958
4959 switch (result_type) {
4960 case Primitive::kPrimChar:
4961 __ Andi(dst, src, 0xFFFF);
4962 break;
4963 case Primitive::kPrimByte:
4964 if (has_sign_extension) {
4965 __ Seb(dst, src);
4966 } else {
4967 __ Sll(dst, src, 24);
4968 __ Sra(dst, dst, 24);
4969 }
4970 break;
4971 case Primitive::kPrimShort:
4972 if (has_sign_extension) {
4973 __ Seh(dst, src);
4974 } else {
4975 __ Sll(dst, src, 16);
4976 __ Sra(dst, dst, 16);
4977 }
4978 break;
4979 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07004980 if (dst != src) {
4981 __ Move(dst, src);
4982 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004983 break;
4984
4985 default:
4986 LOG(FATAL) << "Unexpected type conversion from " << input_type
4987 << " to " << result_type;
4988 }
4989 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004990 if (input_type == Primitive::kPrimLong) {
4991 if (isR6) {
4992 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4993 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4994 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4995 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4996 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4997 __ Mtc1(src_low, FTMP);
4998 __ Mthc1(src_high, FTMP);
4999 if (result_type == Primitive::kPrimFloat) {
5000 __ Cvtsl(dst, FTMP);
5001 } else {
5002 __ Cvtdl(dst, FTMP);
5003 }
5004 } else {
5005 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
5006 : QUICK_ENTRY_POINT(pL2d);
5007 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
5008 : IsDirectEntrypoint(kQuickL2d);
5009 codegen_->InvokeRuntime(entry_offset,
5010 conversion,
5011 conversion->GetDexPc(),
5012 nullptr,
5013 direct);
5014 if (result_type == Primitive::kPrimFloat) {
5015 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5016 } else {
5017 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5018 }
5019 }
5020 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005021 Register src = locations->InAt(0).AsRegister<Register>();
5022 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5023 __ Mtc1(src, FTMP);
5024 if (result_type == Primitive::kPrimFloat) {
5025 __ Cvtsw(dst, FTMP);
5026 } else {
5027 __ Cvtdw(dst, FTMP);
5028 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005029 }
5030 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5031 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005032 if (result_type == Primitive::kPrimLong) {
5033 if (isR6) {
5034 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5035 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5036 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5037 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5038 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5039 MipsLabel truncate;
5040 MipsLabel done;
5041
5042 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5043 // value when the input is either a NaN or is outside of the range of the output type
5044 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5045 // the same result.
5046 //
5047 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5048 // value of the output type if the input is outside of the range after the truncation or
5049 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5050 // results. This matches the desired float/double-to-int/long conversion exactly.
5051 //
5052 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5053 //
5054 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5055 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5056 // even though it must be NAN2008=1 on R6.
5057 //
5058 // The code takes care of the different behaviors by first comparing the input to the
5059 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5060 // If the input is greater than or equal to the minimum, it procedes to the truncate
5061 // instruction, which will handle such an input the same way irrespective of NAN2008.
5062 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5063 // in order to return either zero or the minimum value.
5064 //
5065 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5066 // truncate instruction for MIPS64R6.
5067 if (input_type == Primitive::kPrimFloat) {
5068 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5069 __ LoadConst32(TMP, min_val);
5070 __ Mtc1(TMP, FTMP);
5071 __ CmpLeS(FTMP, FTMP, src);
5072 } else {
5073 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5074 __ LoadConst32(TMP, High32Bits(min_val));
5075 __ Mtc1(ZERO, FTMP);
5076 __ Mthc1(TMP, FTMP);
5077 __ CmpLeD(FTMP, FTMP, src);
5078 }
5079
5080 __ Bc1nez(FTMP, &truncate);
5081
5082 if (input_type == Primitive::kPrimFloat) {
5083 __ CmpEqS(FTMP, src, src);
5084 } else {
5085 __ CmpEqD(FTMP, src, src);
5086 }
5087 __ Move(dst_low, ZERO);
5088 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5089 __ Mfc1(TMP, FTMP);
5090 __ And(dst_high, dst_high, TMP);
5091
5092 __ B(&done);
5093
5094 __ Bind(&truncate);
5095
5096 if (input_type == Primitive::kPrimFloat) {
5097 __ TruncLS(FTMP, src);
5098 } else {
5099 __ TruncLD(FTMP, src);
5100 }
5101 __ Mfc1(dst_low, FTMP);
5102 __ Mfhc1(dst_high, FTMP);
5103
5104 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005105 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005106 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
5107 : QUICK_ENTRY_POINT(pD2l);
5108 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
5109 : IsDirectEntrypoint(kQuickD2l);
5110 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
5111 if (input_type == Primitive::kPrimFloat) {
5112 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5113 } else {
5114 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5115 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005116 }
5117 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005118 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5119 Register dst = locations->Out().AsRegister<Register>();
5120 MipsLabel truncate;
5121 MipsLabel done;
5122
5123 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5124 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5125 // even though it must be NAN2008=1 on R6.
5126 //
5127 // For details see the large comment above for the truncation of float/double to long on R6.
5128 //
5129 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5130 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005131 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005132 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5133 __ LoadConst32(TMP, min_val);
5134 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005135 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005136 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5137 __ LoadConst32(TMP, High32Bits(min_val));
5138 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005139 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005140 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005141
5142 if (isR6) {
5143 if (input_type == Primitive::kPrimFloat) {
5144 __ CmpLeS(FTMP, FTMP, src);
5145 } else {
5146 __ CmpLeD(FTMP, FTMP, src);
5147 }
5148 __ Bc1nez(FTMP, &truncate);
5149
5150 if (input_type == Primitive::kPrimFloat) {
5151 __ CmpEqS(FTMP, src, src);
5152 } else {
5153 __ CmpEqD(FTMP, src, src);
5154 }
5155 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5156 __ Mfc1(TMP, FTMP);
5157 __ And(dst, dst, TMP);
5158 } else {
5159 if (input_type == Primitive::kPrimFloat) {
5160 __ ColeS(0, FTMP, src);
5161 } else {
5162 __ ColeD(0, FTMP, src);
5163 }
5164 __ Bc1t(0, &truncate);
5165
5166 if (input_type == Primitive::kPrimFloat) {
5167 __ CeqS(0, src, src);
5168 } else {
5169 __ CeqD(0, src, src);
5170 }
5171 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5172 __ Movf(dst, ZERO, 0);
5173 }
5174
5175 __ B(&done);
5176
5177 __ Bind(&truncate);
5178
5179 if (input_type == Primitive::kPrimFloat) {
5180 __ TruncWS(FTMP, src);
5181 } else {
5182 __ TruncWD(FTMP, src);
5183 }
5184 __ Mfc1(dst, FTMP);
5185
5186 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005187 }
5188 } else if (Primitive::IsFloatingPointType(result_type) &&
5189 Primitive::IsFloatingPointType(input_type)) {
5190 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5191 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5192 if (result_type == Primitive::kPrimFloat) {
5193 __ Cvtsd(dst, src);
5194 } else {
5195 __ Cvtds(dst, src);
5196 }
5197 } else {
5198 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5199 << " to " << result_type;
5200 }
5201}
5202
5203void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5204 HandleShift(ushr);
5205}
5206
5207void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5208 HandleShift(ushr);
5209}
5210
5211void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5212 HandleBinaryOp(instruction);
5213}
5214
5215void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5216 HandleBinaryOp(instruction);
5217}
5218
5219void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5220 // Nothing to do, this should be removed during prepare for register allocator.
5221 LOG(FATAL) << "Unreachable";
5222}
5223
5224void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5225 // Nothing to do, this should be removed during prepare for register allocator.
5226 LOG(FATAL) << "Unreachable";
5227}
5228
5229void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005230 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005231}
5232
5233void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005234 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005235}
5236
5237void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005238 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005239}
5240
5241void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005242 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005243}
5244
5245void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005246 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005247}
5248
5249void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005250 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005251}
5252
5253void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005254 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005255}
5256
5257void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005258 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005259}
5260
5261void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005262 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005263}
5264
5265void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005266 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005267}
5268
5269void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005270 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005271}
5272
5273void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005274 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005275}
5276
5277void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005278 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005279}
5280
5281void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005282 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005283}
5284
5285void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005286 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005287}
5288
5289void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005290 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005291}
5292
5293void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005294 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005295}
5296
5297void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005298 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005299}
5300
5301void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005302 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005303}
5304
5305void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005306 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005307}
5308
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005309void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5310 LocationSummary* locations =
5311 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5312 locations->SetInAt(0, Location::RequiresRegister());
5313}
5314
5315void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5316 int32_t lower_bound = switch_instr->GetStartValue();
5317 int32_t num_entries = switch_instr->GetNumEntries();
5318 LocationSummary* locations = switch_instr->GetLocations();
5319 Register value_reg = locations->InAt(0).AsRegister<Register>();
5320 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5321
5322 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005323 Register temp_reg = TMP;
5324 __ Addiu32(temp_reg, value_reg, -lower_bound);
5325 // Jump to default if index is negative
5326 // Note: We don't check the case that index is positive while value < lower_bound, because in
5327 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5328 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5329
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005330 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005331 // Jump to successors[0] if value == lower_bound.
5332 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5333 int32_t last_index = 0;
5334 for (; num_entries - last_index > 2; last_index += 2) {
5335 __ Addiu(temp_reg, temp_reg, -2);
5336 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5337 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5338 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5339 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5340 }
5341 if (num_entries - last_index == 2) {
5342 // The last missing case_value.
5343 __ Addiu(temp_reg, temp_reg, -1);
5344 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005345 }
5346
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005347 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005348 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5349 __ B(codegen_->GetLabelOf(default_block));
5350 }
5351}
5352
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005353void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
5354 HMipsComputeBaseMethodAddress* insn) {
5355 LocationSummary* locations =
5356 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5357 locations->SetOut(Location::RequiresRegister());
5358}
5359
5360void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
5361 HMipsComputeBaseMethodAddress* insn) {
5362 LocationSummary* locations = insn->GetLocations();
5363 Register reg = locations->Out().AsRegister<Register>();
5364
5365 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5366
5367 // Generate a dummy PC-relative call to obtain PC.
5368 __ Nal();
5369 // Grab the return address off RA.
5370 __ Move(reg, RA);
5371
5372 // Remember this offset (the obtained PC value) for later use with constant area.
5373 __ BindPcRelBaseLabel();
5374}
5375
5376void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5377 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
5378 locations->SetOut(Location::RequiresRegister());
5379}
5380
5381void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5382 Register reg = base->GetLocations()->Out().AsRegister<Register>();
5383 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5384 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
5385
5386 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5387 __ Bind(&info->high_label);
5388 __ Bind(&info->pc_rel_label);
5389 // Add a 32-bit offset to PC.
5390 __ Auipc(reg, /* placeholder */ 0x1234);
5391 __ Addiu(reg, reg, /* placeholder */ 0x5678);
5392 } else {
5393 // Generate a dummy PC-relative call to obtain PC.
5394 __ Nal();
5395 __ Bind(&info->high_label);
5396 __ Lui(reg, /* placeholder */ 0x1234);
5397 __ Bind(&info->pc_rel_label);
5398 __ Ori(reg, reg, /* placeholder */ 0x5678);
5399 // Add a 32-bit offset to PC.
5400 __ Addu(reg, reg, RA);
5401 }
5402}
5403
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005404void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5405 // The trampoline uses the same calling convention as dex calling conventions,
5406 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5407 // the method_idx.
5408 HandleInvoke(invoke);
5409}
5410
5411void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5412 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5413}
5414
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005415void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5416 LocationSummary* locations =
5417 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5418 locations->SetInAt(0, Location::RequiresRegister());
5419 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005420}
5421
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005422void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5423 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005424 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005425 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005426 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005427 __ LoadFromOffset(kLoadWord,
5428 locations->Out().AsRegister<Register>(),
5429 locations->InAt(0).AsRegister<Register>(),
5430 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005431 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005432 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005433 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005434 __ LoadFromOffset(kLoadWord,
5435 locations->Out().AsRegister<Register>(),
5436 locations->InAt(0).AsRegister<Register>(),
5437 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005438 __ LoadFromOffset(kLoadWord,
5439 locations->Out().AsRegister<Register>(),
5440 locations->Out().AsRegister<Register>(),
5441 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005442 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005443}
5444
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005445#undef __
5446#undef QUICK_ENTRY_POINT
5447
5448} // namespace mips
5449} // namespace art