blob: f169eb00f3346917eb08af1426111a6ce6c550e7 [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"
Vladimir Marko3a21e382016-09-02 12:38:38 +010023#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
27#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070028#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020029#include "mirror/array-inl.h"
30#include "mirror/class-inl.h"
31#include "offsets.h"
32#include "thread.h"
33#include "utils/assembler.h"
34#include "utils/mips/assembler_mips.h"
35#include "utils/stack_checks.h"
36
37namespace art {
38namespace mips {
39
40static constexpr int kCurrentMethodStackOffset = 0;
41static constexpr Register kMethodRegisterArgument = A0;
42
Alexey Frunzee3fb2452016-05-10 16:08:05 -070043// We'll maximize the range of a single load instruction for dex cache array accesses
44// by aligning offset -32768 with the offset of the first used element.
45static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
46
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020047Location MipsReturnLocation(Primitive::Type return_type) {
48 switch (return_type) {
49 case Primitive::kPrimBoolean:
50 case Primitive::kPrimByte:
51 case Primitive::kPrimChar:
52 case Primitive::kPrimShort:
53 case Primitive::kPrimInt:
54 case Primitive::kPrimNot:
55 return Location::RegisterLocation(V0);
56
57 case Primitive::kPrimLong:
58 return Location::RegisterPairLocation(V0, V1);
59
60 case Primitive::kPrimFloat:
61 case Primitive::kPrimDouble:
62 return Location::FpuRegisterLocation(F0);
63
64 case Primitive::kPrimVoid:
65 return Location();
66 }
67 UNREACHABLE();
68}
69
70Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
71 return MipsReturnLocation(type);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
75 return Location::RegisterLocation(kMethodRegisterArgument);
76}
77
78Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
79 Location next_location;
80
81 switch (type) {
82 case Primitive::kPrimBoolean:
83 case Primitive::kPrimByte:
84 case Primitive::kPrimChar:
85 case Primitive::kPrimShort:
86 case Primitive::kPrimInt:
87 case Primitive::kPrimNot: {
88 uint32_t gp_index = gp_index_++;
89 if (gp_index < calling_convention.GetNumberOfRegisters()) {
90 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
91 } else {
92 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
93 next_location = Location::StackSlot(stack_offset);
94 }
95 break;
96 }
97
98 case Primitive::kPrimLong: {
99 uint32_t gp_index = gp_index_;
100 gp_index_ += 2;
101 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
102 if (calling_convention.GetRegisterAt(gp_index) == A1) {
103 gp_index_++; // Skip A1, and use A2_A3 instead.
104 gp_index++;
105 }
106 Register low_even = calling_convention.GetRegisterAt(gp_index);
107 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
108 DCHECK_EQ(low_even + 1, high_odd);
109 next_location = Location::RegisterPairLocation(low_even, high_odd);
110 } else {
111 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
112 next_location = Location::DoubleStackSlot(stack_offset);
113 }
114 break;
115 }
116
117 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
118 // will take up the even/odd pair, while floats are stored in even regs only.
119 // On 64 bit FPU, both double and float are stored in even registers only.
120 case Primitive::kPrimFloat:
121 case Primitive::kPrimDouble: {
122 uint32_t float_index = float_index_++;
123 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
124 next_location = Location::FpuRegisterLocation(
125 calling_convention.GetFpuRegisterAt(float_index));
126 } else {
127 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
128 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
129 : Location::StackSlot(stack_offset);
130 }
131 break;
132 }
133
134 case Primitive::kPrimVoid:
135 LOG(FATAL) << "Unexpected parameter type " << type;
136 break;
137 }
138
139 // Space on the stack is reserved for all arguments.
140 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
141
142 return next_location;
143}
144
145Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
146 return MipsReturnLocation(type);
147}
148
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100149// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
150#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700151#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200152
153class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
154 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000155 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200156
157 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
158 LocationSummary* locations = instruction_->GetLocations();
159 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
160 __ Bind(GetEntryLabel());
161 if (instruction_->CanThrowIntoCatchBlock()) {
162 // Live registers will be restored in the catch block if caught.
163 SaveLiveRegisters(codegen, instruction_->GetLocations());
164 }
165 // We're moving two locations to locations that could overlap, so we need a parallel
166 // move resolver.
167 InvokeRuntimeCallingConvention calling_convention;
168 codegen->EmitParallelMoves(locations->InAt(0),
169 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
170 Primitive::kPrimInt,
171 locations->InAt(1),
172 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
173 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100174 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
175 ? kQuickThrowStringBounds
176 : kQuickThrowArrayBounds;
177 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100178 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200179 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
180 }
181
182 bool IsFatal() const OVERRIDE { return true; }
183
184 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
185
186 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200187 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
188};
189
190class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
191 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000192 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200193
194 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
195 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
196 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100197 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200198 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
199 }
200
201 bool IsFatal() const OVERRIDE { return true; }
202
203 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
204
205 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200206 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
207};
208
209class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
210 public:
211 LoadClassSlowPathMIPS(HLoadClass* cls,
212 HInstruction* at,
213 uint32_t dex_pc,
214 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000215 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200216 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
217 }
218
219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
220 LocationSummary* locations = at_->GetLocations();
221 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
222
223 __ Bind(GetEntryLabel());
224 SaveLiveRegisters(codegen, locations);
225
226 InvokeRuntimeCallingConvention calling_convention;
227 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
228
Serban Constantinescufca16662016-07-14 09:21:59 +0100229 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
230 : kQuickInitializeType;
231 mips_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200232 if (do_clinit_) {
233 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
234 } else {
235 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
236 }
237
238 // Move the class to the desired location.
239 Location out = locations->Out();
240 if (out.IsValid()) {
241 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
242 Primitive::Type type = at_->GetType();
243 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
244 }
245
246 RestoreLiveRegisters(codegen, locations);
247 __ B(GetExitLabel());
248 }
249
250 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
251
252 private:
253 // The class this slow path will load.
254 HLoadClass* const cls_;
255
256 // The instruction where this slow path is happening.
257 // (Might be the load class or an initialization check).
258 HInstruction* const at_;
259
260 // The dex PC of `at_`.
261 const uint32_t dex_pc_;
262
263 // Whether to initialize the class.
264 const bool do_clinit_;
265
266 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
267};
268
269class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
270 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000271 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200272
273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
274 LocationSummary* locations = instruction_->GetLocations();
275 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
276 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
277
278 __ Bind(GetEntryLabel());
279 SaveLiveRegisters(codegen, locations);
280
281 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000282 HLoadString* load = instruction_->AsLoadString();
283 const uint32_t string_index = load->GetStringIndex();
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Serban Constantinescufca16662016-07-14 09:21:59 +0100285 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
287 Primitive::Type type = instruction_->GetType();
288 mips_codegen->MoveLocation(locations->Out(),
289 calling_convention.GetReturnLocation(type),
290 type);
291
292 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000293
294 // Store the resolved String to the BSS entry.
295 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary for the
296 // .bss entry address in the fast path, so that we can avoid another calculation here.
297 bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
298 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
299 Register out = locations->Out().AsRegister<Register>();
300 DCHECK_NE(out, AT);
301 CodeGeneratorMIPS::PcRelativePatchInfo* info =
302 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
303 mips_codegen->EmitPcRelativeAddressPlaceholder(info, TMP, base);
304 __ StoreToOffset(kStoreWord, out, TMP, 0);
305
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200306 __ B(GetExitLabel());
307 }
308
309 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
310
311 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200312 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
313};
314
315class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
316 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000317 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200318
319 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
320 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
321 __ Bind(GetEntryLabel());
322 if (instruction_->CanThrowIntoCatchBlock()) {
323 // Live registers will be restored in the catch block if caught.
324 SaveLiveRegisters(codegen, instruction_->GetLocations());
325 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100326 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200327 instruction_,
328 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100329 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200330 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
331 }
332
333 bool IsFatal() const OVERRIDE { return true; }
334
335 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
336
337 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200338 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
339};
340
341class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
342 public:
343 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000344 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200345
346 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
347 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
348 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100349 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200350 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 if (successor_ == nullptr) {
352 __ B(GetReturnLabel());
353 } else {
354 __ B(mips_codegen->GetLabelOf(successor_));
355 }
356 }
357
358 MipsLabel* GetReturnLabel() {
359 DCHECK(successor_ == nullptr);
360 return &return_label_;
361 }
362
363 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
364
365 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200366 // If not null, the block to branch to after the suspend check.
367 HBasicBlock* const successor_;
368
369 // If `successor_` is null, the label to branch to after the suspend check.
370 MipsLabel return_label_;
371
372 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
373};
374
375class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
376 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000377 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200378
379 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
380 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800381 Location arg0, arg1;
382 if (instruction_->IsInstanceOf()) {
383 arg0 = locations->InAt(1);
384 arg1 = locations->Out();
385 } else {
386 arg0 = locations->InAt(0);
387 arg1 = locations->InAt(1);
388 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200389 uint32_t dex_pc = instruction_->GetDexPc();
390 DCHECK(instruction_->IsCheckCast()
391 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
392 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
393
394 __ Bind(GetEntryLabel());
395 SaveLiveRegisters(codegen, locations);
396
397 // We're moving two locations to locations that could overlap, so we need a parallel
398 // move resolver.
399 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800400 codegen->EmitParallelMoves(arg0,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200401 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
402 Primitive::kPrimNot,
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800403 arg1,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200404 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
405 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200406 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100407 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800408 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Class*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200409 Primitive::Type ret_type = instruction_->GetType();
410 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
411 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200412 } else {
413 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800414 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
415 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200416 }
417
418 RestoreLiveRegisters(codegen, locations);
419 __ B(GetExitLabel());
420 }
421
422 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
423
424 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200425 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
426};
427
428class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
429 public:
Aart Bik42249c32016-01-07 15:33:50 -0800430 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000431 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200432
433 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800434 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200435 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100436 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000437 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200438 }
439
440 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
441
442 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200443 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
444};
445
446CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
447 const MipsInstructionSetFeatures& isa_features,
448 const CompilerOptions& compiler_options,
449 OptimizingCompilerStats* stats)
450 : CodeGenerator(graph,
451 kNumberOfCoreRegisters,
452 kNumberOfFRegisters,
453 kNumberOfRegisterPairs,
454 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
455 arraysize(kCoreCalleeSaves)),
456 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
457 arraysize(kFpuCalleeSaves)),
458 compiler_options,
459 stats),
460 block_labels_(nullptr),
461 location_builder_(graph, this),
462 instruction_visitor_(graph, this),
463 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100464 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700465 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700466 uint32_literals_(std::less<uint32_t>(),
467 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700468 method_patches_(MethodReferenceComparator(),
469 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
470 call_patches_(MethodReferenceComparator(),
471 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700472 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
473 boot_image_string_patches_(StringReferenceValueComparator(),
474 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
475 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
476 boot_image_type_patches_(TypeReferenceValueComparator(),
477 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
478 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
479 boot_image_address_patches_(std::less<uint32_t>(),
480 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
481 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200482 // Save RA (containing the return address) to mimic Quick.
483 AddAllocatedRegister(Location::RegisterLocation(RA));
484}
485
486#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100487// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
488#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700489#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200490
491void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
492 // Ensure that we fix up branches.
493 __ FinalizeCode();
494
495 // Adjust native pc offsets in stack maps.
496 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
497 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
498 uint32_t new_position = __ GetAdjustedPosition(old_position);
499 DCHECK_GE(new_position, old_position);
500 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
501 }
502
503 // Adjust pc offsets for the disassembly information.
504 if (disasm_info_ != nullptr) {
505 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
506 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
507 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
508 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
509 it.second.start = __ GetAdjustedPosition(it.second.start);
510 it.second.end = __ GetAdjustedPosition(it.second.end);
511 }
512 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
513 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
514 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
515 }
516 }
517
518 CodeGenerator::Finalize(allocator);
519}
520
521MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
522 return codegen_->GetAssembler();
523}
524
525void ParallelMoveResolverMIPS::EmitMove(size_t index) {
526 DCHECK_LT(index, moves_.size());
527 MoveOperands* move = moves_[index];
528 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
529}
530
531void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
532 DCHECK_LT(index, moves_.size());
533 MoveOperands* move = moves_[index];
534 Primitive::Type type = move->GetType();
535 Location loc1 = move->GetDestination();
536 Location loc2 = move->GetSource();
537
538 DCHECK(!loc1.IsConstant());
539 DCHECK(!loc2.IsConstant());
540
541 if (loc1.Equals(loc2)) {
542 return;
543 }
544
545 if (loc1.IsRegister() && loc2.IsRegister()) {
546 // Swap 2 GPRs.
547 Register r1 = loc1.AsRegister<Register>();
548 Register r2 = loc2.AsRegister<Register>();
549 __ Move(TMP, r2);
550 __ Move(r2, r1);
551 __ Move(r1, TMP);
552 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
553 FRegister f1 = loc1.AsFpuRegister<FRegister>();
554 FRegister f2 = loc2.AsFpuRegister<FRegister>();
555 if (type == Primitive::kPrimFloat) {
556 __ MovS(FTMP, f2);
557 __ MovS(f2, f1);
558 __ MovS(f1, FTMP);
559 } else {
560 DCHECK_EQ(type, Primitive::kPrimDouble);
561 __ MovD(FTMP, f2);
562 __ MovD(f2, f1);
563 __ MovD(f1, FTMP);
564 }
565 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
566 (loc1.IsFpuRegister() && loc2.IsRegister())) {
567 // Swap FPR and GPR.
568 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
569 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
570 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200571 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200572 __ Move(TMP, r2);
573 __ Mfc1(r2, f1);
574 __ Mtc1(TMP, f1);
575 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
576 // Swap 2 GPR register pairs.
577 Register r1 = loc1.AsRegisterPairLow<Register>();
578 Register r2 = loc2.AsRegisterPairLow<Register>();
579 __ Move(TMP, r2);
580 __ Move(r2, r1);
581 __ Move(r1, TMP);
582 r1 = loc1.AsRegisterPairHigh<Register>();
583 r2 = loc2.AsRegisterPairHigh<Register>();
584 __ Move(TMP, r2);
585 __ Move(r2, r1);
586 __ Move(r1, TMP);
587 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
588 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
589 // Swap FPR and GPR register pair.
590 DCHECK_EQ(type, Primitive::kPrimDouble);
591 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
592 : loc2.AsFpuRegister<FRegister>();
593 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
594 : loc2.AsRegisterPairLow<Register>();
595 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
596 : loc2.AsRegisterPairHigh<Register>();
597 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
598 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
599 // unpredictable and the following mfch1 will fail.
600 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800601 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200602 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800603 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200604 __ Move(r2_l, TMP);
605 __ Move(r2_h, AT);
606 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
607 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
608 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
609 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000610 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
611 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200612 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
613 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000614 __ Move(TMP, reg);
615 __ LoadFromOffset(kLoadWord, reg, SP, offset);
616 __ StoreToOffset(kStoreWord, TMP, SP, offset);
617 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
618 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
619 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
620 : loc2.AsRegisterPairLow<Register>();
621 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
622 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200623 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000624 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
625 : loc2.GetHighStackIndex(kMipsWordSize);
626 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000627 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000628 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000629 __ Move(TMP, reg_h);
630 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
631 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200632 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
633 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
634 : loc2.AsFpuRegister<FRegister>();
635 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
636 if (type == Primitive::kPrimFloat) {
637 __ MovS(FTMP, reg);
638 __ LoadSFromOffset(reg, SP, offset);
639 __ StoreSToOffset(FTMP, SP, offset);
640 } else {
641 DCHECK_EQ(type, Primitive::kPrimDouble);
642 __ MovD(FTMP, reg);
643 __ LoadDFromOffset(reg, SP, offset);
644 __ StoreDToOffset(FTMP, SP, offset);
645 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200646 } else {
647 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
648 }
649}
650
651void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
652 __ Pop(static_cast<Register>(reg));
653}
654
655void ParallelMoveResolverMIPS::SpillScratch(int reg) {
656 __ Push(static_cast<Register>(reg));
657}
658
659void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
660 // Allocate a scratch register other than TMP, if available.
661 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
662 // automatically unspilled when the scratch scope object is destroyed).
663 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
664 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
665 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
666 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
667 __ LoadFromOffset(kLoadWord,
668 Register(ensure_scratch.GetRegister()),
669 SP,
670 index1 + stack_offset);
671 __ LoadFromOffset(kLoadWord,
672 TMP,
673 SP,
674 index2 + stack_offset);
675 __ StoreToOffset(kStoreWord,
676 Register(ensure_scratch.GetRegister()),
677 SP,
678 index2 + stack_offset);
679 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
680 }
681}
682
Alexey Frunze73296a72016-06-03 22:51:46 -0700683void CodeGeneratorMIPS::ComputeSpillMask() {
684 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
685 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
686 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
687 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
688 // registers, include the ZERO register to force alignment of FPU callee-saved registers
689 // within the stack frame.
690 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
691 core_spill_mask_ |= (1 << ZERO);
692 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700693}
694
695bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700696 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700697 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
698 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
699 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700700 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
701 // saved in an unused temporary register) and saving of RA and the current method pointer
702 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700703 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700704}
705
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200706static dwarf::Reg DWARFReg(Register reg) {
707 return dwarf::Reg::MipsCore(static_cast<int>(reg));
708}
709
710// TODO: mapping of floating-point registers to DWARF.
711
712void CodeGeneratorMIPS::GenerateFrameEntry() {
713 __ Bind(&frame_entry_label_);
714
715 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
716
717 if (do_overflow_check) {
718 __ LoadFromOffset(kLoadWord,
719 ZERO,
720 SP,
721 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
722 RecordPcInfo(nullptr, 0);
723 }
724
725 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700726 CHECK_EQ(fpu_spill_mask_, 0u);
727 CHECK_EQ(core_spill_mask_, 1u << RA);
728 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200729 return;
730 }
731
732 // Make sure the frame size isn't unreasonably large.
733 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
734 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
735 }
736
737 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200738
Alexey Frunze73296a72016-06-03 22:51:46 -0700739 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200740 __ IncreaseFrameSize(ofs);
741
Alexey Frunze73296a72016-06-03 22:51:46 -0700742 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
743 Register reg = static_cast<Register>(MostSignificantBit(mask));
744 mask ^= 1u << reg;
745 ofs -= kMipsWordSize;
746 // The ZERO register is only included for alignment.
747 if (reg != ZERO) {
748 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200749 __ cfi().RelOffset(DWARFReg(reg), ofs);
750 }
751 }
752
Alexey Frunze73296a72016-06-03 22:51:46 -0700753 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
754 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
755 mask ^= 1u << reg;
756 ofs -= kMipsDoublewordSize;
757 __ StoreDToOffset(reg, SP, ofs);
758 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200759 }
760
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100761 // Save the current method if we need it. Note that we do not
762 // do this in HCurrentMethod, as the instruction might have been removed
763 // in the SSA graph.
764 if (RequiresCurrentMethod()) {
765 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
766 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200767}
768
769void CodeGeneratorMIPS::GenerateFrameExit() {
770 __ cfi().RememberState();
771
772 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200773 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200774
Alexey Frunze73296a72016-06-03 22:51:46 -0700775 // For better instruction scheduling restore RA before other registers.
776 uint32_t ofs = GetFrameSize();
777 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
778 Register reg = static_cast<Register>(MostSignificantBit(mask));
779 mask ^= 1u << reg;
780 ofs -= kMipsWordSize;
781 // The ZERO register is only included for alignment.
782 if (reg != ZERO) {
783 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200784 __ cfi().Restore(DWARFReg(reg));
785 }
786 }
787
Alexey Frunze73296a72016-06-03 22:51:46 -0700788 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
789 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
790 mask ^= 1u << reg;
791 ofs -= kMipsDoublewordSize;
792 __ LoadDFromOffset(reg, SP, ofs);
793 // TODO: __ cfi().Restore(DWARFReg(reg));
794 }
795
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700796 size_t frame_size = GetFrameSize();
797 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
798 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
799 bool reordering = __ SetReorder(false);
800 if (exchange) {
801 __ Jr(RA);
802 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
803 } else {
804 __ DecreaseFrameSize(frame_size);
805 __ Jr(RA);
806 __ Nop(); // In delay slot.
807 }
808 __ SetReorder(reordering);
809 } else {
810 __ Jr(RA);
811 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200812 }
813
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200814 __ cfi().RestoreState();
815 __ cfi().DefCFAOffset(GetFrameSize());
816}
817
818void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
819 __ Bind(GetLabelOf(block));
820}
821
822void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
823 if (src.Equals(dst)) {
824 return;
825 }
826
827 if (src.IsConstant()) {
828 MoveConstant(dst, src.GetConstant());
829 } else {
830 if (Primitive::Is64BitType(dst_type)) {
831 Move64(dst, src);
832 } else {
833 Move32(dst, src);
834 }
835 }
836}
837
838void CodeGeneratorMIPS::Move32(Location destination, Location source) {
839 if (source.Equals(destination)) {
840 return;
841 }
842
843 if (destination.IsRegister()) {
844 if (source.IsRegister()) {
845 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
846 } else if (source.IsFpuRegister()) {
847 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
848 } else {
849 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
850 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
851 }
852 } else if (destination.IsFpuRegister()) {
853 if (source.IsRegister()) {
854 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
855 } else if (source.IsFpuRegister()) {
856 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
857 } else {
858 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
859 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
860 }
861 } else {
862 DCHECK(destination.IsStackSlot()) << destination;
863 if (source.IsRegister()) {
864 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
865 } else if (source.IsFpuRegister()) {
866 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
867 } else {
868 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
869 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
870 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
871 }
872 }
873}
874
875void CodeGeneratorMIPS::Move64(Location destination, Location source) {
876 if (source.Equals(destination)) {
877 return;
878 }
879
880 if (destination.IsRegisterPair()) {
881 if (source.IsRegisterPair()) {
882 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
883 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
884 } else if (source.IsFpuRegister()) {
885 Register dst_high = destination.AsRegisterPairHigh<Register>();
886 Register dst_low = destination.AsRegisterPairLow<Register>();
887 FRegister src = source.AsFpuRegister<FRegister>();
888 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800889 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200890 } else {
891 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
892 int32_t off = source.GetStackIndex();
893 Register r = destination.AsRegisterPairLow<Register>();
894 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
895 }
896 } else if (destination.IsFpuRegister()) {
897 if (source.IsRegisterPair()) {
898 FRegister dst = destination.AsFpuRegister<FRegister>();
899 Register src_high = source.AsRegisterPairHigh<Register>();
900 Register src_low = source.AsRegisterPairLow<Register>();
901 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800902 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200903 } else if (source.IsFpuRegister()) {
904 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
905 } else {
906 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
907 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
908 }
909 } else {
910 DCHECK(destination.IsDoubleStackSlot()) << destination;
911 int32_t off = destination.GetStackIndex();
912 if (source.IsRegisterPair()) {
913 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
914 } else if (source.IsFpuRegister()) {
915 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
916 } else {
917 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
918 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
919 __ StoreToOffset(kStoreWord, TMP, SP, off);
920 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
921 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
922 }
923 }
924}
925
926void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
927 if (c->IsIntConstant() || c->IsNullConstant()) {
928 // Move 32 bit constant.
929 int32_t value = GetInt32ValueOf(c);
930 if (destination.IsRegister()) {
931 Register dst = destination.AsRegister<Register>();
932 __ LoadConst32(dst, value);
933 } else {
934 DCHECK(destination.IsStackSlot())
935 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700936 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200937 }
938 } else if (c->IsLongConstant()) {
939 // Move 64 bit constant.
940 int64_t value = GetInt64ValueOf(c);
941 if (destination.IsRegisterPair()) {
942 Register r_h = destination.AsRegisterPairHigh<Register>();
943 Register r_l = destination.AsRegisterPairLow<Register>();
944 __ LoadConst64(r_h, r_l, value);
945 } else {
946 DCHECK(destination.IsDoubleStackSlot())
947 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700948 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200949 }
950 } else if (c->IsFloatConstant()) {
951 // Move 32 bit float constant.
952 int32_t value = GetInt32ValueOf(c);
953 if (destination.IsFpuRegister()) {
954 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
955 } else {
956 DCHECK(destination.IsStackSlot())
957 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700958 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200959 }
960 } else {
961 // Move 64 bit double constant.
962 DCHECK(c->IsDoubleConstant()) << c->DebugName();
963 int64_t value = GetInt64ValueOf(c);
964 if (destination.IsFpuRegister()) {
965 FRegister fd = destination.AsFpuRegister<FRegister>();
966 __ LoadDConst64(fd, value, TMP);
967 } else {
968 DCHECK(destination.IsDoubleStackSlot())
969 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700970 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200971 }
972 }
973}
974
975void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
976 DCHECK(destination.IsRegister());
977 Register dst = destination.AsRegister<Register>();
978 __ LoadConst32(dst, value);
979}
980
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200981void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
982 if (location.IsRegister()) {
983 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700984 } else if (location.IsRegisterPair()) {
985 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
986 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200987 } else {
988 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
989 }
990}
991
Vladimir Markoaad75c62016-10-03 08:46:48 +0000992template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
993inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
994 const ArenaDeque<PcRelativePatchInfo>& infos,
995 ArenaVector<LinkerPatch>* linker_patches) {
996 for (const PcRelativePatchInfo& info : infos) {
997 const DexFile& dex_file = info.target_dex_file;
998 size_t offset_or_index = info.offset_or_index;
999 DCHECK(info.high_label.IsBound());
1000 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1001 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1002 // the assembler's base label used for PC-relative addressing.
1003 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1004 ? __ GetLabelLocation(&info.pc_rel_label)
1005 : __ GetPcRelBaseLabelLocation();
1006 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1007 }
1008}
1009
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001010void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1011 DCHECK(linker_patches->empty());
1012 size_t size =
1013 method_patches_.size() +
1014 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001015 pc_relative_dex_cache_patches_.size() +
1016 pc_relative_string_patches_.size() +
1017 pc_relative_type_patches_.size() +
1018 boot_image_string_patches_.size() +
1019 boot_image_type_patches_.size() +
1020 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001021 linker_patches->reserve(size);
1022 for (const auto& entry : method_patches_) {
1023 const MethodReference& target_method = entry.first;
1024 Literal* literal = entry.second;
1025 DCHECK(literal->GetLabel()->IsBound());
1026 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1027 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
1028 target_method.dex_file,
1029 target_method.dex_method_index));
1030 }
1031 for (const auto& entry : call_patches_) {
1032 const MethodReference& target_method = entry.first;
1033 Literal* literal = entry.second;
1034 DCHECK(literal->GetLabel()->IsBound());
1035 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1036 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1037 target_method.dex_file,
1038 target_method.dex_method_index));
1039 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001040 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1041 linker_patches);
1042 if (!GetCompilerOptions().IsBootImage()) {
1043 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1044 linker_patches);
1045 } else {
1046 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1047 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001048 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001049 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1050 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001051 for (const auto& entry : boot_image_string_patches_) {
1052 const StringReference& target_string = entry.first;
1053 Literal* literal = entry.second;
1054 DCHECK(literal->GetLabel()->IsBound());
1055 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1056 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1057 target_string.dex_file,
1058 target_string.string_index));
1059 }
1060 for (const auto& entry : boot_image_type_patches_) {
1061 const TypeReference& target_type = entry.first;
1062 Literal* literal = entry.second;
1063 DCHECK(literal->GetLabel()->IsBound());
1064 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1065 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1066 target_type.dex_file,
1067 target_type.type_index));
1068 }
1069 for (const auto& entry : boot_image_address_patches_) {
1070 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1071 Literal* literal = entry.second;
1072 DCHECK(literal->GetLabel()->IsBound());
1073 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1074 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1075 }
1076}
1077
1078CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1079 const DexFile& dex_file, uint32_t string_index) {
1080 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1081}
1082
1083CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1084 const DexFile& dex_file, uint32_t type_index) {
1085 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001086}
1087
1088CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1089 const DexFile& dex_file, uint32_t element_offset) {
1090 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1091}
1092
1093CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1094 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1095 patches->emplace_back(dex_file, offset_or_index);
1096 return &patches->back();
1097}
1098
Alexey Frunze06a46c42016-07-19 15:00:40 -07001099Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1100 return map->GetOrCreate(
1101 value,
1102 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1103}
1104
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001105Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1106 MethodToLiteralMap* map) {
1107 return map->GetOrCreate(
1108 target_method,
1109 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1110}
1111
1112Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1113 return DeduplicateMethodLiteral(target_method, &method_patches_);
1114}
1115
1116Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1117 return DeduplicateMethodLiteral(target_method, &call_patches_);
1118}
1119
Alexey Frunze06a46c42016-07-19 15:00:40 -07001120Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1121 uint32_t string_index) {
1122 return boot_image_string_patches_.GetOrCreate(
1123 StringReference(&dex_file, string_index),
1124 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1125}
1126
1127Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1128 uint32_t type_index) {
1129 return boot_image_type_patches_.GetOrCreate(
1130 TypeReference(&dex_file, type_index),
1131 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1132}
1133
1134Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1135 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1136 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1137 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1138}
1139
Vladimir Markoaad75c62016-10-03 08:46:48 +00001140void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholder(
1141 PcRelativePatchInfo* info, Register out, Register base) {
1142 bool reordering = __ SetReorder(false);
1143 if (GetInstructionSetFeatures().IsR6()) {
1144 DCHECK_EQ(base, ZERO);
1145 __ Bind(&info->high_label);
1146 __ Bind(&info->pc_rel_label);
1147 // Add a 32-bit offset to PC.
1148 __ Auipc(out, /* placeholder */ 0x1234);
1149 __ Addiu(out, out, /* placeholder */ 0x5678);
1150 } else {
1151 // If base is ZERO, emit NAL to obtain the actual base.
1152 if (base == ZERO) {
1153 // Generate a dummy PC-relative call to obtain PC.
1154 __ Nal();
1155 }
1156 __ Bind(&info->high_label);
1157 __ Lui(out, /* placeholder */ 0x1234);
1158 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1159 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1160 if (base == ZERO) {
1161 __ Bind(&info->pc_rel_label);
1162 }
1163 __ Ori(out, out, /* placeholder */ 0x5678);
1164 // Add a 32-bit offset to PC.
1165 __ Addu(out, out, (base == ZERO) ? RA : base);
1166 }
1167 __ SetReorder(reordering);
1168}
1169
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001170void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1171 MipsLabel done;
1172 Register card = AT;
1173 Register temp = TMP;
1174 __ Beqz(value, &done);
1175 __ LoadFromOffset(kLoadWord,
1176 card,
1177 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001178 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001179 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1180 __ Addu(temp, card, temp);
1181 __ Sb(card, temp, 0);
1182 __ Bind(&done);
1183}
1184
David Brazdil58282f42016-01-14 12:45:10 +00001185void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001186 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1187 blocked_core_registers_[ZERO] = true;
1188 blocked_core_registers_[K0] = true;
1189 blocked_core_registers_[K1] = true;
1190 blocked_core_registers_[GP] = true;
1191 blocked_core_registers_[SP] = true;
1192 blocked_core_registers_[RA] = true;
1193
1194 // AT and TMP(T8) are used as temporary/scratch registers
1195 // (similar to how AT is used by MIPS assemblers).
1196 blocked_core_registers_[AT] = true;
1197 blocked_core_registers_[TMP] = true;
1198 blocked_fpu_registers_[FTMP] = true;
1199
1200 // Reserve suspend and thread registers.
1201 blocked_core_registers_[S0] = true;
1202 blocked_core_registers_[TR] = true;
1203
1204 // Reserve T9 for function calls
1205 blocked_core_registers_[T9] = true;
1206
1207 // Reserve odd-numbered FPU registers.
1208 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1209 blocked_fpu_registers_[i] = true;
1210 }
1211
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001212 if (GetGraph()->IsDebuggable()) {
1213 // Stubs do not save callee-save floating point registers. If the graph
1214 // is debuggable, we need to deal with these registers differently. For
1215 // now, just block them.
1216 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1217 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1218 }
1219 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001220}
1221
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001222size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1223 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1224 return kMipsWordSize;
1225}
1226
1227size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1228 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1229 return kMipsWordSize;
1230}
1231
1232size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1233 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1234 return kMipsDoublewordSize;
1235}
1236
1237size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1238 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1239 return kMipsDoublewordSize;
1240}
1241
1242void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001243 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001244}
1245
1246void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001247 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001248}
1249
Serban Constantinescufca16662016-07-14 09:21:59 +01001250constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1251
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001252void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1253 HInstruction* instruction,
1254 uint32_t dex_pc,
1255 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001256 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001257 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001258 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001259 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001260 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001261 // Reserve argument space on stack (for $a0-$a3) for
1262 // entrypoints that directly reference native implementations.
1263 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001264 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001265 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001266 } else {
1267 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001268 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001269 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001270 if (EntrypointRequiresStackMap(entrypoint)) {
1271 RecordPcInfo(instruction, dex_pc, slow_path);
1272 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001273}
1274
1275void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1276 Register class_reg) {
1277 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1278 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1279 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1280 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1281 __ Sync(0);
1282 __ Bind(slow_path->GetExitLabel());
1283}
1284
1285void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1286 __ Sync(0); // Only stype 0 is supported.
1287}
1288
1289void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1290 HBasicBlock* successor) {
1291 SuspendCheckSlowPathMIPS* slow_path =
1292 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1293 codegen_->AddSlowPath(slow_path);
1294
1295 __ LoadFromOffset(kLoadUnsignedHalfword,
1296 TMP,
1297 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001298 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001299 if (successor == nullptr) {
1300 __ Bnez(TMP, slow_path->GetEntryLabel());
1301 __ Bind(slow_path->GetReturnLabel());
1302 } else {
1303 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1304 __ B(slow_path->GetEntryLabel());
1305 // slow_path will return to GetLabelOf(successor).
1306 }
1307}
1308
1309InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1310 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001311 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001312 assembler_(codegen->GetAssembler()),
1313 codegen_(codegen) {}
1314
1315void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1316 DCHECK_EQ(instruction->InputCount(), 2U);
1317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1318 Primitive::Type type = instruction->GetResultType();
1319 switch (type) {
1320 case Primitive::kPrimInt: {
1321 locations->SetInAt(0, Location::RequiresRegister());
1322 HInstruction* right = instruction->InputAt(1);
1323 bool can_use_imm = false;
1324 if (right->IsConstant()) {
1325 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1326 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1327 can_use_imm = IsUint<16>(imm);
1328 } else if (instruction->IsAdd()) {
1329 can_use_imm = IsInt<16>(imm);
1330 } else {
1331 DCHECK(instruction->IsSub());
1332 can_use_imm = IsInt<16>(-imm);
1333 }
1334 }
1335 if (can_use_imm)
1336 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1337 else
1338 locations->SetInAt(1, Location::RequiresRegister());
1339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1340 break;
1341 }
1342
1343 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001344 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001345 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1346 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001347 break;
1348 }
1349
1350 case Primitive::kPrimFloat:
1351 case Primitive::kPrimDouble:
1352 DCHECK(instruction->IsAdd() || instruction->IsSub());
1353 locations->SetInAt(0, Location::RequiresFpuRegister());
1354 locations->SetInAt(1, Location::RequiresFpuRegister());
1355 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1356 break;
1357
1358 default:
1359 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1360 }
1361}
1362
1363void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1364 Primitive::Type type = instruction->GetType();
1365 LocationSummary* locations = instruction->GetLocations();
1366
1367 switch (type) {
1368 case Primitive::kPrimInt: {
1369 Register dst = locations->Out().AsRegister<Register>();
1370 Register lhs = locations->InAt(0).AsRegister<Register>();
1371 Location rhs_location = locations->InAt(1);
1372
1373 Register rhs_reg = ZERO;
1374 int32_t rhs_imm = 0;
1375 bool use_imm = rhs_location.IsConstant();
1376 if (use_imm) {
1377 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1378 } else {
1379 rhs_reg = rhs_location.AsRegister<Register>();
1380 }
1381
1382 if (instruction->IsAnd()) {
1383 if (use_imm)
1384 __ Andi(dst, lhs, rhs_imm);
1385 else
1386 __ And(dst, lhs, rhs_reg);
1387 } else if (instruction->IsOr()) {
1388 if (use_imm)
1389 __ Ori(dst, lhs, rhs_imm);
1390 else
1391 __ Or(dst, lhs, rhs_reg);
1392 } else if (instruction->IsXor()) {
1393 if (use_imm)
1394 __ Xori(dst, lhs, rhs_imm);
1395 else
1396 __ Xor(dst, lhs, rhs_reg);
1397 } else if (instruction->IsAdd()) {
1398 if (use_imm)
1399 __ Addiu(dst, lhs, rhs_imm);
1400 else
1401 __ Addu(dst, lhs, rhs_reg);
1402 } else {
1403 DCHECK(instruction->IsSub());
1404 if (use_imm)
1405 __ Addiu(dst, lhs, -rhs_imm);
1406 else
1407 __ Subu(dst, lhs, rhs_reg);
1408 }
1409 break;
1410 }
1411
1412 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001413 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1414 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1415 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1416 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001417 Location rhs_location = locations->InAt(1);
1418 bool use_imm = rhs_location.IsConstant();
1419 if (!use_imm) {
1420 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1421 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1422 if (instruction->IsAnd()) {
1423 __ And(dst_low, lhs_low, rhs_low);
1424 __ And(dst_high, lhs_high, rhs_high);
1425 } else if (instruction->IsOr()) {
1426 __ Or(dst_low, lhs_low, rhs_low);
1427 __ Or(dst_high, lhs_high, rhs_high);
1428 } else if (instruction->IsXor()) {
1429 __ Xor(dst_low, lhs_low, rhs_low);
1430 __ Xor(dst_high, lhs_high, rhs_high);
1431 } else if (instruction->IsAdd()) {
1432 if (lhs_low == rhs_low) {
1433 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1434 __ Slt(TMP, lhs_low, ZERO);
1435 __ Addu(dst_low, lhs_low, rhs_low);
1436 } else {
1437 __ Addu(dst_low, lhs_low, rhs_low);
1438 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1439 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1440 }
1441 __ Addu(dst_high, lhs_high, rhs_high);
1442 __ Addu(dst_high, dst_high, TMP);
1443 } else {
1444 DCHECK(instruction->IsSub());
1445 __ Sltu(TMP, lhs_low, rhs_low);
1446 __ Subu(dst_low, lhs_low, rhs_low);
1447 __ Subu(dst_high, lhs_high, rhs_high);
1448 __ Subu(dst_high, dst_high, TMP);
1449 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001450 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001451 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1452 if (instruction->IsOr()) {
1453 uint32_t low = Low32Bits(value);
1454 uint32_t high = High32Bits(value);
1455 if (IsUint<16>(low)) {
1456 if (dst_low != lhs_low || low != 0) {
1457 __ Ori(dst_low, lhs_low, low);
1458 }
1459 } else {
1460 __ LoadConst32(TMP, low);
1461 __ Or(dst_low, lhs_low, TMP);
1462 }
1463 if (IsUint<16>(high)) {
1464 if (dst_high != lhs_high || high != 0) {
1465 __ Ori(dst_high, lhs_high, high);
1466 }
1467 } else {
1468 if (high != low) {
1469 __ LoadConst32(TMP, high);
1470 }
1471 __ Or(dst_high, lhs_high, TMP);
1472 }
1473 } else if (instruction->IsXor()) {
1474 uint32_t low = Low32Bits(value);
1475 uint32_t high = High32Bits(value);
1476 if (IsUint<16>(low)) {
1477 if (dst_low != lhs_low || low != 0) {
1478 __ Xori(dst_low, lhs_low, low);
1479 }
1480 } else {
1481 __ LoadConst32(TMP, low);
1482 __ Xor(dst_low, lhs_low, TMP);
1483 }
1484 if (IsUint<16>(high)) {
1485 if (dst_high != lhs_high || high != 0) {
1486 __ Xori(dst_high, lhs_high, high);
1487 }
1488 } else {
1489 if (high != low) {
1490 __ LoadConst32(TMP, high);
1491 }
1492 __ Xor(dst_high, lhs_high, TMP);
1493 }
1494 } else if (instruction->IsAnd()) {
1495 uint32_t low = Low32Bits(value);
1496 uint32_t high = High32Bits(value);
1497 if (IsUint<16>(low)) {
1498 __ Andi(dst_low, lhs_low, low);
1499 } else if (low != 0xFFFFFFFF) {
1500 __ LoadConst32(TMP, low);
1501 __ And(dst_low, lhs_low, TMP);
1502 } else if (dst_low != lhs_low) {
1503 __ Move(dst_low, lhs_low);
1504 }
1505 if (IsUint<16>(high)) {
1506 __ Andi(dst_high, lhs_high, high);
1507 } else if (high != 0xFFFFFFFF) {
1508 if (high != low) {
1509 __ LoadConst32(TMP, high);
1510 }
1511 __ And(dst_high, lhs_high, TMP);
1512 } else if (dst_high != lhs_high) {
1513 __ Move(dst_high, lhs_high);
1514 }
1515 } else {
1516 if (instruction->IsSub()) {
1517 value = -value;
1518 } else {
1519 DCHECK(instruction->IsAdd());
1520 }
1521 int32_t low = Low32Bits(value);
1522 int32_t high = High32Bits(value);
1523 if (IsInt<16>(low)) {
1524 if (dst_low != lhs_low || low != 0) {
1525 __ Addiu(dst_low, lhs_low, low);
1526 }
1527 if (low != 0) {
1528 __ Sltiu(AT, dst_low, low);
1529 }
1530 } else {
1531 __ LoadConst32(TMP, low);
1532 __ Addu(dst_low, lhs_low, TMP);
1533 __ Sltu(AT, dst_low, TMP);
1534 }
1535 if (IsInt<16>(high)) {
1536 if (dst_high != lhs_high || high != 0) {
1537 __ Addiu(dst_high, lhs_high, high);
1538 }
1539 } else {
1540 if (high != low) {
1541 __ LoadConst32(TMP, high);
1542 }
1543 __ Addu(dst_high, lhs_high, TMP);
1544 }
1545 if (low != 0) {
1546 __ Addu(dst_high, dst_high, AT);
1547 }
1548 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001549 }
1550 break;
1551 }
1552
1553 case Primitive::kPrimFloat:
1554 case Primitive::kPrimDouble: {
1555 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1556 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1557 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1558 if (instruction->IsAdd()) {
1559 if (type == Primitive::kPrimFloat) {
1560 __ AddS(dst, lhs, rhs);
1561 } else {
1562 __ AddD(dst, lhs, rhs);
1563 }
1564 } else {
1565 DCHECK(instruction->IsSub());
1566 if (type == Primitive::kPrimFloat) {
1567 __ SubS(dst, lhs, rhs);
1568 } else {
1569 __ SubD(dst, lhs, rhs);
1570 }
1571 }
1572 break;
1573 }
1574
1575 default:
1576 LOG(FATAL) << "Unexpected binary operation type " << type;
1577 }
1578}
1579
1580void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001581 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001582
1583 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1584 Primitive::Type type = instr->GetResultType();
1585 switch (type) {
1586 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001587 locations->SetInAt(0, Location::RequiresRegister());
1588 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1590 break;
1591 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001592 locations->SetInAt(0, Location::RequiresRegister());
1593 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1594 locations->SetOut(Location::RequiresRegister());
1595 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001596 default:
1597 LOG(FATAL) << "Unexpected shift type " << type;
1598 }
1599}
1600
1601static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1602
1603void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001604 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001605 LocationSummary* locations = instr->GetLocations();
1606 Primitive::Type type = instr->GetType();
1607
1608 Location rhs_location = locations->InAt(1);
1609 bool use_imm = rhs_location.IsConstant();
1610 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1611 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001612 const uint32_t shift_mask =
1613 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001614 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001615 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1616 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001617
1618 switch (type) {
1619 case Primitive::kPrimInt: {
1620 Register dst = locations->Out().AsRegister<Register>();
1621 Register lhs = locations->InAt(0).AsRegister<Register>();
1622 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001623 if (shift_value == 0) {
1624 if (dst != lhs) {
1625 __ Move(dst, lhs);
1626 }
1627 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001628 __ Sll(dst, lhs, shift_value);
1629 } else if (instr->IsShr()) {
1630 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001631 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001632 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001633 } else {
1634 if (has_ins_rotr) {
1635 __ Rotr(dst, lhs, shift_value);
1636 } else {
1637 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1638 __ Srl(dst, lhs, shift_value);
1639 __ Or(dst, dst, TMP);
1640 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001641 }
1642 } else {
1643 if (instr->IsShl()) {
1644 __ Sllv(dst, lhs, rhs_reg);
1645 } else if (instr->IsShr()) {
1646 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001647 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001648 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001649 } else {
1650 if (has_ins_rotr) {
1651 __ Rotrv(dst, lhs, rhs_reg);
1652 } else {
1653 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001654 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1655 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1656 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1657 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1658 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001659 __ Sllv(TMP, lhs, TMP);
1660 __ Srlv(dst, lhs, rhs_reg);
1661 __ Or(dst, dst, TMP);
1662 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001663 }
1664 }
1665 break;
1666 }
1667
1668 case Primitive::kPrimLong: {
1669 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1670 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1671 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1672 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1673 if (use_imm) {
1674 if (shift_value == 0) {
1675 codegen_->Move64(locations->Out(), locations->InAt(0));
1676 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001677 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001678 if (instr->IsShl()) {
1679 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1680 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1681 __ Sll(dst_low, lhs_low, shift_value);
1682 } else if (instr->IsShr()) {
1683 __ Srl(dst_low, lhs_low, shift_value);
1684 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1685 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001686 } else if (instr->IsUShr()) {
1687 __ Srl(dst_low, lhs_low, shift_value);
1688 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1689 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001690 } else {
1691 __ Srl(dst_low, lhs_low, shift_value);
1692 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1693 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001694 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001695 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001696 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001697 if (instr->IsShl()) {
1698 __ Sll(dst_low, lhs_low, shift_value);
1699 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1700 __ Sll(dst_high, lhs_high, shift_value);
1701 __ Or(dst_high, dst_high, TMP);
1702 } else if (instr->IsShr()) {
1703 __ Sra(dst_high, lhs_high, shift_value);
1704 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1705 __ Srl(dst_low, lhs_low, shift_value);
1706 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001707 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001708 __ Srl(dst_high, lhs_high, shift_value);
1709 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1710 __ Srl(dst_low, lhs_low, shift_value);
1711 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001712 } else {
1713 __ Srl(TMP, lhs_low, shift_value);
1714 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1715 __ Or(dst_low, dst_low, TMP);
1716 __ Srl(TMP, lhs_high, shift_value);
1717 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1718 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001719 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001720 }
1721 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001722 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001723 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001724 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001725 __ Move(dst_low, ZERO);
1726 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001727 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001728 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001729 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001730 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001731 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001732 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001733 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001734 // 64-bit rotation by 32 is just a swap.
1735 __ Move(dst_low, lhs_high);
1736 __ Move(dst_high, lhs_low);
1737 } else {
1738 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001739 __ Srl(dst_low, lhs_high, shift_value_high);
1740 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1741 __ Srl(dst_high, lhs_low, shift_value_high);
1742 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001743 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001744 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1745 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001746 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001747 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1748 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001749 __ Or(dst_high, dst_high, TMP);
1750 }
1751 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001752 }
1753 }
1754 } else {
1755 MipsLabel done;
1756 if (instr->IsShl()) {
1757 __ Sllv(dst_low, lhs_low, rhs_reg);
1758 __ Nor(AT, ZERO, rhs_reg);
1759 __ Srl(TMP, lhs_low, 1);
1760 __ Srlv(TMP, TMP, AT);
1761 __ Sllv(dst_high, lhs_high, rhs_reg);
1762 __ Or(dst_high, dst_high, TMP);
1763 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1764 __ Beqz(TMP, &done);
1765 __ Move(dst_high, dst_low);
1766 __ Move(dst_low, ZERO);
1767 } else if (instr->IsShr()) {
1768 __ Srav(dst_high, lhs_high, rhs_reg);
1769 __ Nor(AT, ZERO, rhs_reg);
1770 __ Sll(TMP, lhs_high, 1);
1771 __ Sllv(TMP, TMP, AT);
1772 __ Srlv(dst_low, lhs_low, rhs_reg);
1773 __ Or(dst_low, dst_low, TMP);
1774 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1775 __ Beqz(TMP, &done);
1776 __ Move(dst_low, dst_high);
1777 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001778 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001779 __ Srlv(dst_high, lhs_high, rhs_reg);
1780 __ Nor(AT, ZERO, rhs_reg);
1781 __ Sll(TMP, lhs_high, 1);
1782 __ Sllv(TMP, TMP, AT);
1783 __ Srlv(dst_low, lhs_low, rhs_reg);
1784 __ Or(dst_low, dst_low, TMP);
1785 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1786 __ Beqz(TMP, &done);
1787 __ Move(dst_low, dst_high);
1788 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001789 } else {
1790 __ Nor(AT, ZERO, rhs_reg);
1791 __ Srlv(TMP, lhs_low, rhs_reg);
1792 __ Sll(dst_low, lhs_high, 1);
1793 __ Sllv(dst_low, dst_low, AT);
1794 __ Or(dst_low, dst_low, TMP);
1795 __ Srlv(TMP, lhs_high, rhs_reg);
1796 __ Sll(dst_high, lhs_low, 1);
1797 __ Sllv(dst_high, dst_high, AT);
1798 __ Or(dst_high, dst_high, TMP);
1799 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1800 __ Beqz(TMP, &done);
1801 __ Move(TMP, dst_high);
1802 __ Move(dst_high, dst_low);
1803 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001804 }
1805 __ Bind(&done);
1806 }
1807 break;
1808 }
1809
1810 default:
1811 LOG(FATAL) << "Unexpected shift operation type " << type;
1812 }
1813}
1814
1815void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1816 HandleBinaryOp(instruction);
1817}
1818
1819void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1820 HandleBinaryOp(instruction);
1821}
1822
1823void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1824 HandleBinaryOp(instruction);
1825}
1826
1827void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1828 HandleBinaryOp(instruction);
1829}
1830
1831void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1832 LocationSummary* locations =
1833 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1834 locations->SetInAt(0, Location::RequiresRegister());
1835 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1836 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1837 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1838 } else {
1839 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1840 }
1841}
1842
Alexey Frunze2923db72016-08-20 01:55:47 -07001843auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1844 auto null_checker = [this, instruction]() {
1845 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1846 };
1847 return null_checker;
1848}
1849
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001850void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1851 LocationSummary* locations = instruction->GetLocations();
1852 Register obj = locations->InAt(0).AsRegister<Register>();
1853 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001854 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001855 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001856
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001857 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001858 switch (type) {
1859 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001860 Register out = locations->Out().AsRegister<Register>();
1861 if (index.IsConstant()) {
1862 size_t offset =
1863 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001864 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001865 } else {
1866 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001867 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001868 }
1869 break;
1870 }
1871
1872 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001873 Register out = locations->Out().AsRegister<Register>();
1874 if (index.IsConstant()) {
1875 size_t offset =
1876 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001877 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001878 } else {
1879 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001880 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001881 }
1882 break;
1883 }
1884
1885 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001886 Register out = locations->Out().AsRegister<Register>();
1887 if (index.IsConstant()) {
1888 size_t offset =
1889 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001890 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001891 } else {
1892 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1893 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001894 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001895 }
1896 break;
1897 }
1898
1899 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001900 Register out = locations->Out().AsRegister<Register>();
1901 if (index.IsConstant()) {
1902 size_t offset =
1903 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001904 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001905 } else {
1906 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1907 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001908 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001909 }
1910 break;
1911 }
1912
1913 case Primitive::kPrimInt:
1914 case Primitive::kPrimNot: {
1915 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001916 Register out = locations->Out().AsRegister<Register>();
1917 if (index.IsConstant()) {
1918 size_t offset =
1919 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001920 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001921 } else {
1922 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1923 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001924 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001925 }
1926 break;
1927 }
1928
1929 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001930 Register out = locations->Out().AsRegisterPairLow<Register>();
1931 if (index.IsConstant()) {
1932 size_t offset =
1933 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001934 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001935 } else {
1936 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1937 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001938 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001939 }
1940 break;
1941 }
1942
1943 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001944 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1945 if (index.IsConstant()) {
1946 size_t offset =
1947 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001948 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001949 } else {
1950 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1951 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001952 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001953 }
1954 break;
1955 }
1956
1957 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001958 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1959 if (index.IsConstant()) {
1960 size_t offset =
1961 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001962 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001963 } else {
1964 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1965 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001966 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001967 }
1968 break;
1969 }
1970
1971 case Primitive::kPrimVoid:
1972 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1973 UNREACHABLE();
1974 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001975}
1976
1977void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1978 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1979 locations->SetInAt(0, Location::RequiresRegister());
1980 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1981}
1982
1983void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1984 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001985 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001986 Register obj = locations->InAt(0).AsRegister<Register>();
1987 Register out = locations->Out().AsRegister<Register>();
1988 __ LoadFromOffset(kLoadWord, out, obj, offset);
1989 codegen_->MaybeRecordImplicitNullCheck(instruction);
1990}
1991
Alexey Frunzef58b2482016-09-02 22:14:06 -07001992Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1993 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1994 ? Location::ConstantLocation(instruction->AsConstant())
1995 : Location::RequiresRegister();
1996}
1997
1998Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1999 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2000 // We can store a non-zero float or double constant without first loading it into the FPU,
2001 // but we should only prefer this if the constant has a single use.
2002 if (instruction->IsConstant() &&
2003 (instruction->AsConstant()->IsZeroBitPattern() ||
2004 instruction->GetUses().HasExactlyOneElement())) {
2005 return Location::ConstantLocation(instruction->AsConstant());
2006 // Otherwise fall through and require an FPU register for the constant.
2007 }
2008 return Location::RequiresFpuRegister();
2009}
2010
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002011void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002012 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002013 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2014 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002015 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002016 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002017 InvokeRuntimeCallingConvention calling_convention;
2018 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2019 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2020 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2021 } else {
2022 locations->SetInAt(0, Location::RequiresRegister());
2023 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2024 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002025 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002026 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002027 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002028 }
2029 }
2030}
2031
2032void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2033 LocationSummary* locations = instruction->GetLocations();
2034 Register obj = locations->InAt(0).AsRegister<Register>();
2035 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002036 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002037 Primitive::Type value_type = instruction->GetComponentType();
2038 bool needs_runtime_call = locations->WillCall();
2039 bool needs_write_barrier =
2040 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002041 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002042 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002043
2044 switch (value_type) {
2045 case Primitive::kPrimBoolean:
2046 case Primitive::kPrimByte: {
2047 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002048 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002049 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002050 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002051 __ Addu(base_reg, obj, index.AsRegister<Register>());
2052 }
2053 if (value_location.IsConstant()) {
2054 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2055 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2056 } else {
2057 Register value = value_location.AsRegister<Register>();
2058 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002059 }
2060 break;
2061 }
2062
2063 case Primitive::kPrimShort:
2064 case Primitive::kPrimChar: {
2065 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002066 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002067 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002068 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002069 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2070 __ Addu(base_reg, obj, base_reg);
2071 }
2072 if (value_location.IsConstant()) {
2073 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2074 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2075 } else {
2076 Register value = value_location.AsRegister<Register>();
2077 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002078 }
2079 break;
2080 }
2081
2082 case Primitive::kPrimInt:
2083 case Primitive::kPrimNot: {
2084 if (!needs_runtime_call) {
2085 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002086 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002087 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002088 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002089 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2090 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002091 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002092 if (value_location.IsConstant()) {
2093 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2094 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2095 DCHECK(!needs_write_barrier);
2096 } else {
2097 Register value = value_location.AsRegister<Register>();
2098 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2099 if (needs_write_barrier) {
2100 DCHECK_EQ(value_type, Primitive::kPrimNot);
2101 codegen_->MarkGCCard(obj, value);
2102 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002103 }
2104 } else {
2105 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002106 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002107 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2108 }
2109 break;
2110 }
2111
2112 case Primitive::kPrimLong: {
2113 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002114 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002115 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002116 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002117 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2118 __ Addu(base_reg, obj, base_reg);
2119 }
2120 if (value_location.IsConstant()) {
2121 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2122 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2123 } else {
2124 Register value = value_location.AsRegisterPairLow<Register>();
2125 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002126 }
2127 break;
2128 }
2129
2130 case Primitive::kPrimFloat: {
2131 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002132 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002133 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002134 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002135 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2136 __ Addu(base_reg, obj, base_reg);
2137 }
2138 if (value_location.IsConstant()) {
2139 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2140 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2141 } else {
2142 FRegister value = value_location.AsFpuRegister<FRegister>();
2143 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002144 }
2145 break;
2146 }
2147
2148 case Primitive::kPrimDouble: {
2149 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002150 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002151 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002152 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002153 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2154 __ Addu(base_reg, obj, base_reg);
2155 }
2156 if (value_location.IsConstant()) {
2157 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2158 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2159 } else {
2160 FRegister value = value_location.AsFpuRegister<FRegister>();
2161 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002162 }
2163 break;
2164 }
2165
2166 case Primitive::kPrimVoid:
2167 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2168 UNREACHABLE();
2169 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002170}
2171
2172void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002173 RegisterSet caller_saves = RegisterSet::Empty();
2174 InvokeRuntimeCallingConvention calling_convention;
2175 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2176 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2177 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002178 locations->SetInAt(0, Location::RequiresRegister());
2179 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002180}
2181
2182void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2183 LocationSummary* locations = instruction->GetLocations();
2184 BoundsCheckSlowPathMIPS* slow_path =
2185 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2186 codegen_->AddSlowPath(slow_path);
2187
2188 Register index = locations->InAt(0).AsRegister<Register>();
2189 Register length = locations->InAt(1).AsRegister<Register>();
2190
2191 // length is limited by the maximum positive signed 32-bit integer.
2192 // Unsigned comparison of length and index checks for index < 0
2193 // and for length <= index simultaneously.
2194 __ Bgeu(index, length, slow_path->GetEntryLabel());
2195}
2196
2197void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2198 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2199 instruction,
2200 LocationSummary::kCallOnSlowPath);
2201 locations->SetInAt(0, Location::RequiresRegister());
2202 locations->SetInAt(1, Location::RequiresRegister());
2203 // Note that TypeCheckSlowPathMIPS uses this register too.
2204 locations->AddTemp(Location::RequiresRegister());
2205}
2206
2207void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2208 LocationSummary* locations = instruction->GetLocations();
2209 Register obj = locations->InAt(0).AsRegister<Register>();
2210 Register cls = locations->InAt(1).AsRegister<Register>();
2211 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2212
2213 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2214 codegen_->AddSlowPath(slow_path);
2215
2216 // TODO: avoid this check if we know obj is not null.
2217 __ Beqz(obj, slow_path->GetExitLabel());
2218 // Compare the class of `obj` with `cls`.
2219 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2220 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2221 __ Bind(slow_path->GetExitLabel());
2222}
2223
2224void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2225 LocationSummary* locations =
2226 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2227 locations->SetInAt(0, Location::RequiresRegister());
2228 if (check->HasUses()) {
2229 locations->SetOut(Location::SameAsFirstInput());
2230 }
2231}
2232
2233void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2234 // We assume the class is not null.
2235 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2236 check->GetLoadClass(),
2237 check,
2238 check->GetDexPc(),
2239 true);
2240 codegen_->AddSlowPath(slow_path);
2241 GenerateClassInitializationCheck(slow_path,
2242 check->GetLocations()->InAt(0).AsRegister<Register>());
2243}
2244
2245void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2246 Primitive::Type in_type = compare->InputAt(0)->GetType();
2247
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002248 LocationSummary* locations =
2249 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002250
2251 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002252 case Primitive::kPrimBoolean:
2253 case Primitive::kPrimByte:
2254 case Primitive::kPrimShort:
2255 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002256 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002257 locations->SetInAt(0, Location::RequiresRegister());
2258 locations->SetInAt(1, Location::RequiresRegister());
2259 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2260 break;
2261
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262 case Primitive::kPrimLong:
2263 locations->SetInAt(0, Location::RequiresRegister());
2264 locations->SetInAt(1, Location::RequiresRegister());
2265 // Output overlaps because it is written before doing the low comparison.
2266 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2267 break;
2268
2269 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002270 case Primitive::kPrimDouble:
2271 locations->SetInAt(0, Location::RequiresFpuRegister());
2272 locations->SetInAt(1, Location::RequiresFpuRegister());
2273 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002274 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002275
2276 default:
2277 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2278 }
2279}
2280
2281void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2282 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002283 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002284 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002285 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002286
2287 // 0 if: left == right
2288 // 1 if: left > right
2289 // -1 if: left < right
2290 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002291 case Primitive::kPrimBoolean:
2292 case Primitive::kPrimByte:
2293 case Primitive::kPrimShort:
2294 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002295 case Primitive::kPrimInt: {
2296 Register lhs = locations->InAt(0).AsRegister<Register>();
2297 Register rhs = locations->InAt(1).AsRegister<Register>();
2298 __ Slt(TMP, lhs, rhs);
2299 __ Slt(res, rhs, lhs);
2300 __ Subu(res, res, TMP);
2301 break;
2302 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002303 case Primitive::kPrimLong: {
2304 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002305 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2306 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2307 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2308 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2309 // TODO: more efficient (direct) comparison with a constant.
2310 __ Slt(TMP, lhs_high, rhs_high);
2311 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2312 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2313 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2314 __ Sltu(TMP, lhs_low, rhs_low);
2315 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2316 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2317 __ Bind(&done);
2318 break;
2319 }
2320
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002321 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002322 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002323 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2324 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2325 MipsLabel done;
2326 if (isR6) {
2327 __ CmpEqS(FTMP, lhs, rhs);
2328 __ LoadConst32(res, 0);
2329 __ Bc1nez(FTMP, &done);
2330 if (gt_bias) {
2331 __ CmpLtS(FTMP, lhs, rhs);
2332 __ LoadConst32(res, -1);
2333 __ Bc1nez(FTMP, &done);
2334 __ LoadConst32(res, 1);
2335 } else {
2336 __ CmpLtS(FTMP, rhs, lhs);
2337 __ LoadConst32(res, 1);
2338 __ Bc1nez(FTMP, &done);
2339 __ LoadConst32(res, -1);
2340 }
2341 } else {
2342 if (gt_bias) {
2343 __ ColtS(0, lhs, rhs);
2344 __ LoadConst32(res, -1);
2345 __ Bc1t(0, &done);
2346 __ CeqS(0, lhs, rhs);
2347 __ LoadConst32(res, 1);
2348 __ Movt(res, ZERO, 0);
2349 } else {
2350 __ ColtS(0, rhs, lhs);
2351 __ LoadConst32(res, 1);
2352 __ Bc1t(0, &done);
2353 __ CeqS(0, lhs, rhs);
2354 __ LoadConst32(res, -1);
2355 __ Movt(res, ZERO, 0);
2356 }
2357 }
2358 __ Bind(&done);
2359 break;
2360 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002361 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002362 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002363 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2364 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2365 MipsLabel done;
2366 if (isR6) {
2367 __ CmpEqD(FTMP, lhs, rhs);
2368 __ LoadConst32(res, 0);
2369 __ Bc1nez(FTMP, &done);
2370 if (gt_bias) {
2371 __ CmpLtD(FTMP, lhs, rhs);
2372 __ LoadConst32(res, -1);
2373 __ Bc1nez(FTMP, &done);
2374 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002375 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002376 __ CmpLtD(FTMP, rhs, lhs);
2377 __ LoadConst32(res, 1);
2378 __ Bc1nez(FTMP, &done);
2379 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002380 }
2381 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002382 if (gt_bias) {
2383 __ ColtD(0, lhs, rhs);
2384 __ LoadConst32(res, -1);
2385 __ Bc1t(0, &done);
2386 __ CeqD(0, lhs, rhs);
2387 __ LoadConst32(res, 1);
2388 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002389 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002390 __ ColtD(0, rhs, lhs);
2391 __ LoadConst32(res, 1);
2392 __ Bc1t(0, &done);
2393 __ CeqD(0, lhs, rhs);
2394 __ LoadConst32(res, -1);
2395 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002396 }
2397 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002398 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002399 break;
2400 }
2401
2402 default:
2403 LOG(FATAL) << "Unimplemented compare type " << in_type;
2404 }
2405}
2406
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002407void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002408 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002409 switch (instruction->InputAt(0)->GetType()) {
2410 default:
2411 case Primitive::kPrimLong:
2412 locations->SetInAt(0, Location::RequiresRegister());
2413 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2414 break;
2415
2416 case Primitive::kPrimFloat:
2417 case Primitive::kPrimDouble:
2418 locations->SetInAt(0, Location::RequiresFpuRegister());
2419 locations->SetInAt(1, Location::RequiresFpuRegister());
2420 break;
2421 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002422 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002423 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2424 }
2425}
2426
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002427void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002428 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002429 return;
2430 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002431
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002432 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002433 LocationSummary* locations = instruction->GetLocations();
2434 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002435 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002436
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002437 switch (type) {
2438 default:
2439 // Integer case.
2440 GenerateIntCompare(instruction->GetCondition(), locations);
2441 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002442
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002443 case Primitive::kPrimLong:
2444 // TODO: don't use branches.
2445 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002446 break;
2447
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002448 case Primitive::kPrimFloat:
2449 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002450 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2451 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002452 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002453
2454 // Convert the branches into the result.
2455 MipsLabel done;
2456
2457 // False case: result = 0.
2458 __ LoadConst32(dst, 0);
2459 __ B(&done);
2460
2461 // True case: result = 1.
2462 __ Bind(&true_label);
2463 __ LoadConst32(dst, 1);
2464 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002465}
2466
Alexey Frunze7e99e052015-11-24 19:28:01 -08002467void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2468 DCHECK(instruction->IsDiv() || instruction->IsRem());
2469 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2470
2471 LocationSummary* locations = instruction->GetLocations();
2472 Location second = locations->InAt(1);
2473 DCHECK(second.IsConstant());
2474
2475 Register out = locations->Out().AsRegister<Register>();
2476 Register dividend = locations->InAt(0).AsRegister<Register>();
2477 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2478 DCHECK(imm == 1 || imm == -1);
2479
2480 if (instruction->IsRem()) {
2481 __ Move(out, ZERO);
2482 } else {
2483 if (imm == -1) {
2484 __ Subu(out, ZERO, dividend);
2485 } else if (out != dividend) {
2486 __ Move(out, dividend);
2487 }
2488 }
2489}
2490
2491void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2492 DCHECK(instruction->IsDiv() || instruction->IsRem());
2493 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2494
2495 LocationSummary* locations = instruction->GetLocations();
2496 Location second = locations->InAt(1);
2497 DCHECK(second.IsConstant());
2498
2499 Register out = locations->Out().AsRegister<Register>();
2500 Register dividend = locations->InAt(0).AsRegister<Register>();
2501 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002502 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002503 int ctz_imm = CTZ(abs_imm);
2504
2505 if (instruction->IsDiv()) {
2506 if (ctz_imm == 1) {
2507 // Fast path for division by +/-2, which is very common.
2508 __ Srl(TMP, dividend, 31);
2509 } else {
2510 __ Sra(TMP, dividend, 31);
2511 __ Srl(TMP, TMP, 32 - ctz_imm);
2512 }
2513 __ Addu(out, dividend, TMP);
2514 __ Sra(out, out, ctz_imm);
2515 if (imm < 0) {
2516 __ Subu(out, ZERO, out);
2517 }
2518 } else {
2519 if (ctz_imm == 1) {
2520 // Fast path for modulo +/-2, which is very common.
2521 __ Sra(TMP, dividend, 31);
2522 __ Subu(out, dividend, TMP);
2523 __ Andi(out, out, 1);
2524 __ Addu(out, out, TMP);
2525 } else {
2526 __ Sra(TMP, dividend, 31);
2527 __ Srl(TMP, TMP, 32 - ctz_imm);
2528 __ Addu(out, dividend, TMP);
2529 if (IsUint<16>(abs_imm - 1)) {
2530 __ Andi(out, out, abs_imm - 1);
2531 } else {
2532 __ Sll(out, out, 32 - ctz_imm);
2533 __ Srl(out, out, 32 - ctz_imm);
2534 }
2535 __ Subu(out, out, TMP);
2536 }
2537 }
2538}
2539
2540void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2541 DCHECK(instruction->IsDiv() || instruction->IsRem());
2542 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2543
2544 LocationSummary* locations = instruction->GetLocations();
2545 Location second = locations->InAt(1);
2546 DCHECK(second.IsConstant());
2547
2548 Register out = locations->Out().AsRegister<Register>();
2549 Register dividend = locations->InAt(0).AsRegister<Register>();
2550 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2551
2552 int64_t magic;
2553 int shift;
2554 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2555
2556 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2557
2558 __ LoadConst32(TMP, magic);
2559 if (isR6) {
2560 __ MuhR6(TMP, dividend, TMP);
2561 } else {
2562 __ MultR2(dividend, TMP);
2563 __ Mfhi(TMP);
2564 }
2565 if (imm > 0 && magic < 0) {
2566 __ Addu(TMP, TMP, dividend);
2567 } else if (imm < 0 && magic > 0) {
2568 __ Subu(TMP, TMP, dividend);
2569 }
2570
2571 if (shift != 0) {
2572 __ Sra(TMP, TMP, shift);
2573 }
2574
2575 if (instruction->IsDiv()) {
2576 __ Sra(out, TMP, 31);
2577 __ Subu(out, TMP, out);
2578 } else {
2579 __ Sra(AT, TMP, 31);
2580 __ Subu(AT, TMP, AT);
2581 __ LoadConst32(TMP, imm);
2582 if (isR6) {
2583 __ MulR6(TMP, AT, TMP);
2584 } else {
2585 __ MulR2(TMP, AT, TMP);
2586 }
2587 __ Subu(out, dividend, TMP);
2588 }
2589}
2590
2591void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2592 DCHECK(instruction->IsDiv() || instruction->IsRem());
2593 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2594
2595 LocationSummary* locations = instruction->GetLocations();
2596 Register out = locations->Out().AsRegister<Register>();
2597 Location second = locations->InAt(1);
2598
2599 if (second.IsConstant()) {
2600 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2601 if (imm == 0) {
2602 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2603 } else if (imm == 1 || imm == -1) {
2604 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002605 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002606 DivRemByPowerOfTwo(instruction);
2607 } else {
2608 DCHECK(imm <= -2 || imm >= 2);
2609 GenerateDivRemWithAnyConstant(instruction);
2610 }
2611 } else {
2612 Register dividend = locations->InAt(0).AsRegister<Register>();
2613 Register divisor = second.AsRegister<Register>();
2614 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2615 if (instruction->IsDiv()) {
2616 if (isR6) {
2617 __ DivR6(out, dividend, divisor);
2618 } else {
2619 __ DivR2(out, dividend, divisor);
2620 }
2621 } else {
2622 if (isR6) {
2623 __ ModR6(out, dividend, divisor);
2624 } else {
2625 __ ModR2(out, dividend, divisor);
2626 }
2627 }
2628 }
2629}
2630
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002631void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2632 Primitive::Type type = div->GetResultType();
2633 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002634 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002635 : LocationSummary::kNoCall;
2636
2637 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2638
2639 switch (type) {
2640 case Primitive::kPrimInt:
2641 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002642 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002643 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2644 break;
2645
2646 case Primitive::kPrimLong: {
2647 InvokeRuntimeCallingConvention calling_convention;
2648 locations->SetInAt(0, Location::RegisterPairLocation(
2649 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2650 locations->SetInAt(1, Location::RegisterPairLocation(
2651 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2652 locations->SetOut(calling_convention.GetReturnLocation(type));
2653 break;
2654 }
2655
2656 case Primitive::kPrimFloat:
2657 case Primitive::kPrimDouble:
2658 locations->SetInAt(0, Location::RequiresFpuRegister());
2659 locations->SetInAt(1, Location::RequiresFpuRegister());
2660 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2661 break;
2662
2663 default:
2664 LOG(FATAL) << "Unexpected div type " << type;
2665 }
2666}
2667
2668void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2669 Primitive::Type type = instruction->GetType();
2670 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002671
2672 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002673 case Primitive::kPrimInt:
2674 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002675 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002676 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002677 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002678 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2679 break;
2680 }
2681 case Primitive::kPrimFloat:
2682 case Primitive::kPrimDouble: {
2683 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2684 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2685 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2686 if (type == Primitive::kPrimFloat) {
2687 __ DivS(dst, lhs, rhs);
2688 } else {
2689 __ DivD(dst, lhs, rhs);
2690 }
2691 break;
2692 }
2693 default:
2694 LOG(FATAL) << "Unexpected div type " << type;
2695 }
2696}
2697
2698void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002699 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002700 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002701}
2702
2703void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2704 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2705 codegen_->AddSlowPath(slow_path);
2706 Location value = instruction->GetLocations()->InAt(0);
2707 Primitive::Type type = instruction->GetType();
2708
2709 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002710 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002711 case Primitive::kPrimByte:
2712 case Primitive::kPrimChar:
2713 case Primitive::kPrimShort:
2714 case Primitive::kPrimInt: {
2715 if (value.IsConstant()) {
2716 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2717 __ B(slow_path->GetEntryLabel());
2718 } else {
2719 // A division by a non-null constant is valid. We don't need to perform
2720 // any check, so simply fall through.
2721 }
2722 } else {
2723 DCHECK(value.IsRegister()) << value;
2724 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2725 }
2726 break;
2727 }
2728 case Primitive::kPrimLong: {
2729 if (value.IsConstant()) {
2730 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2731 __ B(slow_path->GetEntryLabel());
2732 } else {
2733 // A division by a non-null constant is valid. We don't need to perform
2734 // any check, so simply fall through.
2735 }
2736 } else {
2737 DCHECK(value.IsRegisterPair()) << value;
2738 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2739 __ Beqz(TMP, slow_path->GetEntryLabel());
2740 }
2741 break;
2742 }
2743 default:
2744 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2745 }
2746}
2747
2748void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2749 LocationSummary* locations =
2750 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2751 locations->SetOut(Location::ConstantLocation(constant));
2752}
2753
2754void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2755 // Will be generated at use site.
2756}
2757
2758void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2759 exit->SetLocations(nullptr);
2760}
2761
2762void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2763}
2764
2765void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2766 LocationSummary* locations =
2767 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2768 locations->SetOut(Location::ConstantLocation(constant));
2769}
2770
2771void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2772 // Will be generated at use site.
2773}
2774
2775void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2776 got->SetLocations(nullptr);
2777}
2778
2779void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2780 DCHECK(!successor->IsExitBlock());
2781 HBasicBlock* block = got->GetBlock();
2782 HInstruction* previous = got->GetPrevious();
2783 HLoopInformation* info = block->GetLoopInformation();
2784
2785 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2786 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2787 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2788 return;
2789 }
2790 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2791 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2792 }
2793 if (!codegen_->GoesToNextBlock(block, successor)) {
2794 __ B(codegen_->GetLabelOf(successor));
2795 }
2796}
2797
2798void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2799 HandleGoto(got, got->GetSuccessor());
2800}
2801
2802void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2803 try_boundary->SetLocations(nullptr);
2804}
2805
2806void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2807 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2808 if (!successor->IsExitBlock()) {
2809 HandleGoto(try_boundary, successor);
2810 }
2811}
2812
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002813void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2814 LocationSummary* locations) {
2815 Register dst = locations->Out().AsRegister<Register>();
2816 Register lhs = locations->InAt(0).AsRegister<Register>();
2817 Location rhs_location = locations->InAt(1);
2818 Register rhs_reg = ZERO;
2819 int64_t rhs_imm = 0;
2820 bool use_imm = rhs_location.IsConstant();
2821 if (use_imm) {
2822 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2823 } else {
2824 rhs_reg = rhs_location.AsRegister<Register>();
2825 }
2826
2827 switch (cond) {
2828 case kCondEQ:
2829 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002830 if (use_imm && IsInt<16>(-rhs_imm)) {
2831 if (rhs_imm == 0) {
2832 if (cond == kCondEQ) {
2833 __ Sltiu(dst, lhs, 1);
2834 } else {
2835 __ Sltu(dst, ZERO, lhs);
2836 }
2837 } else {
2838 __ Addiu(dst, lhs, -rhs_imm);
2839 if (cond == kCondEQ) {
2840 __ Sltiu(dst, dst, 1);
2841 } else {
2842 __ Sltu(dst, ZERO, dst);
2843 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002844 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002845 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002846 if (use_imm && IsUint<16>(rhs_imm)) {
2847 __ Xori(dst, lhs, rhs_imm);
2848 } else {
2849 if (use_imm) {
2850 rhs_reg = TMP;
2851 __ LoadConst32(rhs_reg, rhs_imm);
2852 }
2853 __ Xor(dst, lhs, rhs_reg);
2854 }
2855 if (cond == kCondEQ) {
2856 __ Sltiu(dst, dst, 1);
2857 } else {
2858 __ Sltu(dst, ZERO, dst);
2859 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002860 }
2861 break;
2862
2863 case kCondLT:
2864 case kCondGE:
2865 if (use_imm && IsInt<16>(rhs_imm)) {
2866 __ Slti(dst, lhs, rhs_imm);
2867 } else {
2868 if (use_imm) {
2869 rhs_reg = TMP;
2870 __ LoadConst32(rhs_reg, rhs_imm);
2871 }
2872 __ Slt(dst, lhs, rhs_reg);
2873 }
2874 if (cond == kCondGE) {
2875 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2876 // only the slt instruction but no sge.
2877 __ Xori(dst, dst, 1);
2878 }
2879 break;
2880
2881 case kCondLE:
2882 case kCondGT:
2883 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2884 // Simulate lhs <= rhs via lhs < rhs + 1.
2885 __ Slti(dst, lhs, rhs_imm + 1);
2886 if (cond == kCondGT) {
2887 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2888 // only the slti instruction but no sgti.
2889 __ Xori(dst, dst, 1);
2890 }
2891 } else {
2892 if (use_imm) {
2893 rhs_reg = TMP;
2894 __ LoadConst32(rhs_reg, rhs_imm);
2895 }
2896 __ Slt(dst, rhs_reg, lhs);
2897 if (cond == kCondLE) {
2898 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2899 // only the slt instruction but no sle.
2900 __ Xori(dst, dst, 1);
2901 }
2902 }
2903 break;
2904
2905 case kCondB:
2906 case kCondAE:
2907 if (use_imm && IsInt<16>(rhs_imm)) {
2908 // Sltiu sign-extends its 16-bit immediate operand before
2909 // the comparison and thus lets us compare directly with
2910 // unsigned values in the ranges [0, 0x7fff] and
2911 // [0xffff8000, 0xffffffff].
2912 __ Sltiu(dst, lhs, rhs_imm);
2913 } else {
2914 if (use_imm) {
2915 rhs_reg = TMP;
2916 __ LoadConst32(rhs_reg, rhs_imm);
2917 }
2918 __ Sltu(dst, lhs, rhs_reg);
2919 }
2920 if (cond == kCondAE) {
2921 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2922 // only the sltu instruction but no sgeu.
2923 __ Xori(dst, dst, 1);
2924 }
2925 break;
2926
2927 case kCondBE:
2928 case kCondA:
2929 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2930 // Simulate lhs <= rhs via lhs < rhs + 1.
2931 // Note that this only works if rhs + 1 does not overflow
2932 // to 0, hence the check above.
2933 // Sltiu sign-extends its 16-bit immediate operand before
2934 // the comparison and thus lets us compare directly with
2935 // unsigned values in the ranges [0, 0x7fff] and
2936 // [0xffff8000, 0xffffffff].
2937 __ Sltiu(dst, lhs, rhs_imm + 1);
2938 if (cond == kCondA) {
2939 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2940 // only the sltiu instruction but no sgtiu.
2941 __ Xori(dst, dst, 1);
2942 }
2943 } else {
2944 if (use_imm) {
2945 rhs_reg = TMP;
2946 __ LoadConst32(rhs_reg, rhs_imm);
2947 }
2948 __ Sltu(dst, rhs_reg, lhs);
2949 if (cond == kCondBE) {
2950 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2951 // only the sltu instruction but no sleu.
2952 __ Xori(dst, dst, 1);
2953 }
2954 }
2955 break;
2956 }
2957}
2958
Alexey Frunze674b9ee2016-09-20 14:54:15 -07002959bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
2960 LocationSummary* input_locations,
2961 Register dst) {
2962 Register lhs = input_locations->InAt(0).AsRegister<Register>();
2963 Location rhs_location = input_locations->InAt(1);
2964 Register rhs_reg = ZERO;
2965 int64_t rhs_imm = 0;
2966 bool use_imm = rhs_location.IsConstant();
2967 if (use_imm) {
2968 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2969 } else {
2970 rhs_reg = rhs_location.AsRegister<Register>();
2971 }
2972
2973 switch (cond) {
2974 case kCondEQ:
2975 case kCondNE:
2976 if (use_imm && IsInt<16>(-rhs_imm)) {
2977 __ Addiu(dst, lhs, -rhs_imm);
2978 } else if (use_imm && IsUint<16>(rhs_imm)) {
2979 __ Xori(dst, lhs, rhs_imm);
2980 } else {
2981 if (use_imm) {
2982 rhs_reg = TMP;
2983 __ LoadConst32(rhs_reg, rhs_imm);
2984 }
2985 __ Xor(dst, lhs, rhs_reg);
2986 }
2987 return (cond == kCondEQ);
2988
2989 case kCondLT:
2990 case kCondGE:
2991 if (use_imm && IsInt<16>(rhs_imm)) {
2992 __ Slti(dst, lhs, rhs_imm);
2993 } else {
2994 if (use_imm) {
2995 rhs_reg = TMP;
2996 __ LoadConst32(rhs_reg, rhs_imm);
2997 }
2998 __ Slt(dst, lhs, rhs_reg);
2999 }
3000 return (cond == kCondGE);
3001
3002 case kCondLE:
3003 case kCondGT:
3004 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3005 // Simulate lhs <= rhs via lhs < rhs + 1.
3006 __ Slti(dst, lhs, rhs_imm + 1);
3007 return (cond == kCondGT);
3008 } else {
3009 if (use_imm) {
3010 rhs_reg = TMP;
3011 __ LoadConst32(rhs_reg, rhs_imm);
3012 }
3013 __ Slt(dst, rhs_reg, lhs);
3014 return (cond == kCondLE);
3015 }
3016
3017 case kCondB:
3018 case kCondAE:
3019 if (use_imm && IsInt<16>(rhs_imm)) {
3020 // Sltiu sign-extends its 16-bit immediate operand before
3021 // the comparison and thus lets us compare directly with
3022 // unsigned values in the ranges [0, 0x7fff] and
3023 // [0xffff8000, 0xffffffff].
3024 __ Sltiu(dst, lhs, rhs_imm);
3025 } else {
3026 if (use_imm) {
3027 rhs_reg = TMP;
3028 __ LoadConst32(rhs_reg, rhs_imm);
3029 }
3030 __ Sltu(dst, lhs, rhs_reg);
3031 }
3032 return (cond == kCondAE);
3033
3034 case kCondBE:
3035 case kCondA:
3036 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3037 // Simulate lhs <= rhs via lhs < rhs + 1.
3038 // Note that this only works if rhs + 1 does not overflow
3039 // to 0, hence the check above.
3040 // Sltiu sign-extends its 16-bit immediate operand before
3041 // the comparison and thus lets us compare directly with
3042 // unsigned values in the ranges [0, 0x7fff] and
3043 // [0xffff8000, 0xffffffff].
3044 __ Sltiu(dst, lhs, rhs_imm + 1);
3045 return (cond == kCondA);
3046 } else {
3047 if (use_imm) {
3048 rhs_reg = TMP;
3049 __ LoadConst32(rhs_reg, rhs_imm);
3050 }
3051 __ Sltu(dst, rhs_reg, lhs);
3052 return (cond == kCondBE);
3053 }
3054 }
3055}
3056
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003057void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3058 LocationSummary* locations,
3059 MipsLabel* label) {
3060 Register lhs = locations->InAt(0).AsRegister<Register>();
3061 Location rhs_location = locations->InAt(1);
3062 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003063 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003064 bool use_imm = rhs_location.IsConstant();
3065 if (use_imm) {
3066 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3067 } else {
3068 rhs_reg = rhs_location.AsRegister<Register>();
3069 }
3070
3071 if (use_imm && rhs_imm == 0) {
3072 switch (cond) {
3073 case kCondEQ:
3074 case kCondBE: // <= 0 if zero
3075 __ Beqz(lhs, label);
3076 break;
3077 case kCondNE:
3078 case kCondA: // > 0 if non-zero
3079 __ Bnez(lhs, label);
3080 break;
3081 case kCondLT:
3082 __ Bltz(lhs, label);
3083 break;
3084 case kCondGE:
3085 __ Bgez(lhs, label);
3086 break;
3087 case kCondLE:
3088 __ Blez(lhs, label);
3089 break;
3090 case kCondGT:
3091 __ Bgtz(lhs, label);
3092 break;
3093 case kCondB: // always false
3094 break;
3095 case kCondAE: // always true
3096 __ B(label);
3097 break;
3098 }
3099 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003100 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3101 if (isR6 || !use_imm) {
3102 if (use_imm) {
3103 rhs_reg = TMP;
3104 __ LoadConst32(rhs_reg, rhs_imm);
3105 }
3106 switch (cond) {
3107 case kCondEQ:
3108 __ Beq(lhs, rhs_reg, label);
3109 break;
3110 case kCondNE:
3111 __ Bne(lhs, rhs_reg, label);
3112 break;
3113 case kCondLT:
3114 __ Blt(lhs, rhs_reg, label);
3115 break;
3116 case kCondGE:
3117 __ Bge(lhs, rhs_reg, label);
3118 break;
3119 case kCondLE:
3120 __ Bge(rhs_reg, lhs, label);
3121 break;
3122 case kCondGT:
3123 __ Blt(rhs_reg, lhs, label);
3124 break;
3125 case kCondB:
3126 __ Bltu(lhs, rhs_reg, label);
3127 break;
3128 case kCondAE:
3129 __ Bgeu(lhs, rhs_reg, label);
3130 break;
3131 case kCondBE:
3132 __ Bgeu(rhs_reg, lhs, label);
3133 break;
3134 case kCondA:
3135 __ Bltu(rhs_reg, lhs, label);
3136 break;
3137 }
3138 } else {
3139 // Special cases for more efficient comparison with constants on R2.
3140 switch (cond) {
3141 case kCondEQ:
3142 __ LoadConst32(TMP, rhs_imm);
3143 __ Beq(lhs, TMP, label);
3144 break;
3145 case kCondNE:
3146 __ LoadConst32(TMP, rhs_imm);
3147 __ Bne(lhs, TMP, label);
3148 break;
3149 case kCondLT:
3150 if (IsInt<16>(rhs_imm)) {
3151 __ Slti(TMP, lhs, rhs_imm);
3152 __ Bnez(TMP, label);
3153 } else {
3154 __ LoadConst32(TMP, rhs_imm);
3155 __ Blt(lhs, TMP, label);
3156 }
3157 break;
3158 case kCondGE:
3159 if (IsInt<16>(rhs_imm)) {
3160 __ Slti(TMP, lhs, rhs_imm);
3161 __ Beqz(TMP, label);
3162 } else {
3163 __ LoadConst32(TMP, rhs_imm);
3164 __ Bge(lhs, TMP, label);
3165 }
3166 break;
3167 case kCondLE:
3168 if (IsInt<16>(rhs_imm + 1)) {
3169 // Simulate lhs <= rhs via lhs < rhs + 1.
3170 __ Slti(TMP, lhs, rhs_imm + 1);
3171 __ Bnez(TMP, label);
3172 } else {
3173 __ LoadConst32(TMP, rhs_imm);
3174 __ Bge(TMP, lhs, label);
3175 }
3176 break;
3177 case kCondGT:
3178 if (IsInt<16>(rhs_imm + 1)) {
3179 // Simulate lhs > rhs via !(lhs < rhs + 1).
3180 __ Slti(TMP, lhs, rhs_imm + 1);
3181 __ Beqz(TMP, label);
3182 } else {
3183 __ LoadConst32(TMP, rhs_imm);
3184 __ Blt(TMP, lhs, label);
3185 }
3186 break;
3187 case kCondB:
3188 if (IsInt<16>(rhs_imm)) {
3189 __ Sltiu(TMP, lhs, rhs_imm);
3190 __ Bnez(TMP, label);
3191 } else {
3192 __ LoadConst32(TMP, rhs_imm);
3193 __ Bltu(lhs, TMP, label);
3194 }
3195 break;
3196 case kCondAE:
3197 if (IsInt<16>(rhs_imm)) {
3198 __ Sltiu(TMP, lhs, rhs_imm);
3199 __ Beqz(TMP, label);
3200 } else {
3201 __ LoadConst32(TMP, rhs_imm);
3202 __ Bgeu(lhs, TMP, label);
3203 }
3204 break;
3205 case kCondBE:
3206 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3207 // Simulate lhs <= rhs via lhs < rhs + 1.
3208 // Note that this only works if rhs + 1 does not overflow
3209 // to 0, hence the check above.
3210 __ Sltiu(TMP, lhs, rhs_imm + 1);
3211 __ Bnez(TMP, label);
3212 } else {
3213 __ LoadConst32(TMP, rhs_imm);
3214 __ Bgeu(TMP, lhs, label);
3215 }
3216 break;
3217 case kCondA:
3218 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3219 // Simulate lhs > rhs via !(lhs < rhs + 1).
3220 // Note that this only works if rhs + 1 does not overflow
3221 // to 0, hence the check above.
3222 __ Sltiu(TMP, lhs, rhs_imm + 1);
3223 __ Beqz(TMP, label);
3224 } else {
3225 __ LoadConst32(TMP, rhs_imm);
3226 __ Bltu(TMP, lhs, label);
3227 }
3228 break;
3229 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003230 }
3231 }
3232}
3233
3234void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3235 LocationSummary* locations,
3236 MipsLabel* label) {
3237 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3238 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3239 Location rhs_location = locations->InAt(1);
3240 Register rhs_high = ZERO;
3241 Register rhs_low = ZERO;
3242 int64_t imm = 0;
3243 uint32_t imm_high = 0;
3244 uint32_t imm_low = 0;
3245 bool use_imm = rhs_location.IsConstant();
3246 if (use_imm) {
3247 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3248 imm_high = High32Bits(imm);
3249 imm_low = Low32Bits(imm);
3250 } else {
3251 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3252 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3253 }
3254
3255 if (use_imm && imm == 0) {
3256 switch (cond) {
3257 case kCondEQ:
3258 case kCondBE: // <= 0 if zero
3259 __ Or(TMP, lhs_high, lhs_low);
3260 __ Beqz(TMP, label);
3261 break;
3262 case kCondNE:
3263 case kCondA: // > 0 if non-zero
3264 __ Or(TMP, lhs_high, lhs_low);
3265 __ Bnez(TMP, label);
3266 break;
3267 case kCondLT:
3268 __ Bltz(lhs_high, label);
3269 break;
3270 case kCondGE:
3271 __ Bgez(lhs_high, label);
3272 break;
3273 case kCondLE:
3274 __ Or(TMP, lhs_high, lhs_low);
3275 __ Sra(AT, lhs_high, 31);
3276 __ Bgeu(AT, TMP, label);
3277 break;
3278 case kCondGT:
3279 __ Or(TMP, lhs_high, lhs_low);
3280 __ Sra(AT, lhs_high, 31);
3281 __ Bltu(AT, TMP, label);
3282 break;
3283 case kCondB: // always false
3284 break;
3285 case kCondAE: // always true
3286 __ B(label);
3287 break;
3288 }
3289 } else if (use_imm) {
3290 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3291 switch (cond) {
3292 case kCondEQ:
3293 __ LoadConst32(TMP, imm_high);
3294 __ Xor(TMP, TMP, lhs_high);
3295 __ LoadConst32(AT, imm_low);
3296 __ Xor(AT, AT, lhs_low);
3297 __ Or(TMP, TMP, AT);
3298 __ Beqz(TMP, label);
3299 break;
3300 case kCondNE:
3301 __ LoadConst32(TMP, imm_high);
3302 __ Xor(TMP, TMP, lhs_high);
3303 __ LoadConst32(AT, imm_low);
3304 __ Xor(AT, AT, lhs_low);
3305 __ Or(TMP, TMP, AT);
3306 __ Bnez(TMP, label);
3307 break;
3308 case kCondLT:
3309 __ LoadConst32(TMP, imm_high);
3310 __ Blt(lhs_high, TMP, label);
3311 __ Slt(TMP, TMP, lhs_high);
3312 __ LoadConst32(AT, imm_low);
3313 __ Sltu(AT, lhs_low, AT);
3314 __ Blt(TMP, AT, label);
3315 break;
3316 case kCondGE:
3317 __ LoadConst32(TMP, imm_high);
3318 __ Blt(TMP, lhs_high, label);
3319 __ Slt(TMP, lhs_high, TMP);
3320 __ LoadConst32(AT, imm_low);
3321 __ Sltu(AT, lhs_low, AT);
3322 __ Or(TMP, TMP, AT);
3323 __ Beqz(TMP, label);
3324 break;
3325 case kCondLE:
3326 __ LoadConst32(TMP, imm_high);
3327 __ Blt(lhs_high, TMP, label);
3328 __ Slt(TMP, TMP, lhs_high);
3329 __ LoadConst32(AT, imm_low);
3330 __ Sltu(AT, AT, lhs_low);
3331 __ Or(TMP, TMP, AT);
3332 __ Beqz(TMP, label);
3333 break;
3334 case kCondGT:
3335 __ LoadConst32(TMP, imm_high);
3336 __ Blt(TMP, lhs_high, label);
3337 __ Slt(TMP, lhs_high, TMP);
3338 __ LoadConst32(AT, imm_low);
3339 __ Sltu(AT, AT, lhs_low);
3340 __ Blt(TMP, AT, label);
3341 break;
3342 case kCondB:
3343 __ LoadConst32(TMP, imm_high);
3344 __ Bltu(lhs_high, TMP, label);
3345 __ Sltu(TMP, TMP, lhs_high);
3346 __ LoadConst32(AT, imm_low);
3347 __ Sltu(AT, lhs_low, AT);
3348 __ Blt(TMP, AT, label);
3349 break;
3350 case kCondAE:
3351 __ LoadConst32(TMP, imm_high);
3352 __ Bltu(TMP, lhs_high, label);
3353 __ Sltu(TMP, lhs_high, TMP);
3354 __ LoadConst32(AT, imm_low);
3355 __ Sltu(AT, lhs_low, AT);
3356 __ Or(TMP, TMP, AT);
3357 __ Beqz(TMP, label);
3358 break;
3359 case kCondBE:
3360 __ LoadConst32(TMP, imm_high);
3361 __ Bltu(lhs_high, TMP, label);
3362 __ Sltu(TMP, TMP, lhs_high);
3363 __ LoadConst32(AT, imm_low);
3364 __ Sltu(AT, AT, lhs_low);
3365 __ Or(TMP, TMP, AT);
3366 __ Beqz(TMP, label);
3367 break;
3368 case kCondA:
3369 __ LoadConst32(TMP, imm_high);
3370 __ Bltu(TMP, lhs_high, label);
3371 __ Sltu(TMP, lhs_high, TMP);
3372 __ LoadConst32(AT, imm_low);
3373 __ Sltu(AT, AT, lhs_low);
3374 __ Blt(TMP, AT, label);
3375 break;
3376 }
3377 } else {
3378 switch (cond) {
3379 case kCondEQ:
3380 __ Xor(TMP, lhs_high, rhs_high);
3381 __ Xor(AT, lhs_low, rhs_low);
3382 __ Or(TMP, TMP, AT);
3383 __ Beqz(TMP, label);
3384 break;
3385 case kCondNE:
3386 __ Xor(TMP, lhs_high, rhs_high);
3387 __ Xor(AT, lhs_low, rhs_low);
3388 __ Or(TMP, TMP, AT);
3389 __ Bnez(TMP, label);
3390 break;
3391 case kCondLT:
3392 __ Blt(lhs_high, rhs_high, label);
3393 __ Slt(TMP, rhs_high, lhs_high);
3394 __ Sltu(AT, lhs_low, rhs_low);
3395 __ Blt(TMP, AT, label);
3396 break;
3397 case kCondGE:
3398 __ Blt(rhs_high, lhs_high, label);
3399 __ Slt(TMP, lhs_high, rhs_high);
3400 __ Sltu(AT, lhs_low, rhs_low);
3401 __ Or(TMP, TMP, AT);
3402 __ Beqz(TMP, label);
3403 break;
3404 case kCondLE:
3405 __ Blt(lhs_high, rhs_high, label);
3406 __ Slt(TMP, rhs_high, lhs_high);
3407 __ Sltu(AT, rhs_low, lhs_low);
3408 __ Or(TMP, TMP, AT);
3409 __ Beqz(TMP, label);
3410 break;
3411 case kCondGT:
3412 __ Blt(rhs_high, lhs_high, label);
3413 __ Slt(TMP, lhs_high, rhs_high);
3414 __ Sltu(AT, rhs_low, lhs_low);
3415 __ Blt(TMP, AT, label);
3416 break;
3417 case kCondB:
3418 __ Bltu(lhs_high, rhs_high, label);
3419 __ Sltu(TMP, rhs_high, lhs_high);
3420 __ Sltu(AT, lhs_low, rhs_low);
3421 __ Blt(TMP, AT, label);
3422 break;
3423 case kCondAE:
3424 __ Bltu(rhs_high, lhs_high, label);
3425 __ Sltu(TMP, lhs_high, rhs_high);
3426 __ Sltu(AT, lhs_low, rhs_low);
3427 __ Or(TMP, TMP, AT);
3428 __ Beqz(TMP, label);
3429 break;
3430 case kCondBE:
3431 __ Bltu(lhs_high, rhs_high, label);
3432 __ Sltu(TMP, rhs_high, lhs_high);
3433 __ Sltu(AT, rhs_low, lhs_low);
3434 __ Or(TMP, TMP, AT);
3435 __ Beqz(TMP, label);
3436 break;
3437 case kCondA:
3438 __ Bltu(rhs_high, lhs_high, label);
3439 __ Sltu(TMP, lhs_high, rhs_high);
3440 __ Sltu(AT, rhs_low, lhs_low);
3441 __ Blt(TMP, AT, label);
3442 break;
3443 }
3444 }
3445}
3446
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003447void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3448 bool gt_bias,
3449 Primitive::Type type,
3450 LocationSummary* locations) {
3451 Register dst = locations->Out().AsRegister<Register>();
3452 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3453 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3454 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3455 if (type == Primitive::kPrimFloat) {
3456 if (isR6) {
3457 switch (cond) {
3458 case kCondEQ:
3459 __ CmpEqS(FTMP, lhs, rhs);
3460 __ Mfc1(dst, FTMP);
3461 __ Andi(dst, dst, 1);
3462 break;
3463 case kCondNE:
3464 __ CmpEqS(FTMP, lhs, rhs);
3465 __ Mfc1(dst, FTMP);
3466 __ Addiu(dst, dst, 1);
3467 break;
3468 case kCondLT:
3469 if (gt_bias) {
3470 __ CmpLtS(FTMP, lhs, rhs);
3471 } else {
3472 __ CmpUltS(FTMP, lhs, rhs);
3473 }
3474 __ Mfc1(dst, FTMP);
3475 __ Andi(dst, dst, 1);
3476 break;
3477 case kCondLE:
3478 if (gt_bias) {
3479 __ CmpLeS(FTMP, lhs, rhs);
3480 } else {
3481 __ CmpUleS(FTMP, lhs, rhs);
3482 }
3483 __ Mfc1(dst, FTMP);
3484 __ Andi(dst, dst, 1);
3485 break;
3486 case kCondGT:
3487 if (gt_bias) {
3488 __ CmpUltS(FTMP, rhs, lhs);
3489 } else {
3490 __ CmpLtS(FTMP, rhs, lhs);
3491 }
3492 __ Mfc1(dst, FTMP);
3493 __ Andi(dst, dst, 1);
3494 break;
3495 case kCondGE:
3496 if (gt_bias) {
3497 __ CmpUleS(FTMP, rhs, lhs);
3498 } else {
3499 __ CmpLeS(FTMP, rhs, lhs);
3500 }
3501 __ Mfc1(dst, FTMP);
3502 __ Andi(dst, dst, 1);
3503 break;
3504 default:
3505 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3506 UNREACHABLE();
3507 }
3508 } else {
3509 switch (cond) {
3510 case kCondEQ:
3511 __ CeqS(0, lhs, rhs);
3512 __ LoadConst32(dst, 1);
3513 __ Movf(dst, ZERO, 0);
3514 break;
3515 case kCondNE:
3516 __ CeqS(0, lhs, rhs);
3517 __ LoadConst32(dst, 1);
3518 __ Movt(dst, ZERO, 0);
3519 break;
3520 case kCondLT:
3521 if (gt_bias) {
3522 __ ColtS(0, lhs, rhs);
3523 } else {
3524 __ CultS(0, lhs, rhs);
3525 }
3526 __ LoadConst32(dst, 1);
3527 __ Movf(dst, ZERO, 0);
3528 break;
3529 case kCondLE:
3530 if (gt_bias) {
3531 __ ColeS(0, lhs, rhs);
3532 } else {
3533 __ CuleS(0, lhs, rhs);
3534 }
3535 __ LoadConst32(dst, 1);
3536 __ Movf(dst, ZERO, 0);
3537 break;
3538 case kCondGT:
3539 if (gt_bias) {
3540 __ CultS(0, rhs, lhs);
3541 } else {
3542 __ ColtS(0, rhs, lhs);
3543 }
3544 __ LoadConst32(dst, 1);
3545 __ Movf(dst, ZERO, 0);
3546 break;
3547 case kCondGE:
3548 if (gt_bias) {
3549 __ CuleS(0, rhs, lhs);
3550 } else {
3551 __ ColeS(0, rhs, lhs);
3552 }
3553 __ LoadConst32(dst, 1);
3554 __ Movf(dst, ZERO, 0);
3555 break;
3556 default:
3557 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3558 UNREACHABLE();
3559 }
3560 }
3561 } else {
3562 DCHECK_EQ(type, Primitive::kPrimDouble);
3563 if (isR6) {
3564 switch (cond) {
3565 case kCondEQ:
3566 __ CmpEqD(FTMP, lhs, rhs);
3567 __ Mfc1(dst, FTMP);
3568 __ Andi(dst, dst, 1);
3569 break;
3570 case kCondNE:
3571 __ CmpEqD(FTMP, lhs, rhs);
3572 __ Mfc1(dst, FTMP);
3573 __ Addiu(dst, dst, 1);
3574 break;
3575 case kCondLT:
3576 if (gt_bias) {
3577 __ CmpLtD(FTMP, lhs, rhs);
3578 } else {
3579 __ CmpUltD(FTMP, lhs, rhs);
3580 }
3581 __ Mfc1(dst, FTMP);
3582 __ Andi(dst, dst, 1);
3583 break;
3584 case kCondLE:
3585 if (gt_bias) {
3586 __ CmpLeD(FTMP, lhs, rhs);
3587 } else {
3588 __ CmpUleD(FTMP, lhs, rhs);
3589 }
3590 __ Mfc1(dst, FTMP);
3591 __ Andi(dst, dst, 1);
3592 break;
3593 case kCondGT:
3594 if (gt_bias) {
3595 __ CmpUltD(FTMP, rhs, lhs);
3596 } else {
3597 __ CmpLtD(FTMP, rhs, lhs);
3598 }
3599 __ Mfc1(dst, FTMP);
3600 __ Andi(dst, dst, 1);
3601 break;
3602 case kCondGE:
3603 if (gt_bias) {
3604 __ CmpUleD(FTMP, rhs, lhs);
3605 } else {
3606 __ CmpLeD(FTMP, rhs, lhs);
3607 }
3608 __ Mfc1(dst, FTMP);
3609 __ Andi(dst, dst, 1);
3610 break;
3611 default:
3612 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3613 UNREACHABLE();
3614 }
3615 } else {
3616 switch (cond) {
3617 case kCondEQ:
3618 __ CeqD(0, lhs, rhs);
3619 __ LoadConst32(dst, 1);
3620 __ Movf(dst, ZERO, 0);
3621 break;
3622 case kCondNE:
3623 __ CeqD(0, lhs, rhs);
3624 __ LoadConst32(dst, 1);
3625 __ Movt(dst, ZERO, 0);
3626 break;
3627 case kCondLT:
3628 if (gt_bias) {
3629 __ ColtD(0, lhs, rhs);
3630 } else {
3631 __ CultD(0, lhs, rhs);
3632 }
3633 __ LoadConst32(dst, 1);
3634 __ Movf(dst, ZERO, 0);
3635 break;
3636 case kCondLE:
3637 if (gt_bias) {
3638 __ ColeD(0, lhs, rhs);
3639 } else {
3640 __ CuleD(0, lhs, rhs);
3641 }
3642 __ LoadConst32(dst, 1);
3643 __ Movf(dst, ZERO, 0);
3644 break;
3645 case kCondGT:
3646 if (gt_bias) {
3647 __ CultD(0, rhs, lhs);
3648 } else {
3649 __ ColtD(0, rhs, lhs);
3650 }
3651 __ LoadConst32(dst, 1);
3652 __ Movf(dst, ZERO, 0);
3653 break;
3654 case kCondGE:
3655 if (gt_bias) {
3656 __ CuleD(0, rhs, lhs);
3657 } else {
3658 __ ColeD(0, rhs, lhs);
3659 }
3660 __ LoadConst32(dst, 1);
3661 __ Movf(dst, ZERO, 0);
3662 break;
3663 default:
3664 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3665 UNREACHABLE();
3666 }
3667 }
3668 }
3669}
3670
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003671bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3672 bool gt_bias,
3673 Primitive::Type type,
3674 LocationSummary* input_locations,
3675 int cc) {
3676 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3677 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3678 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3679 if (type == Primitive::kPrimFloat) {
3680 switch (cond) {
3681 case kCondEQ:
3682 __ CeqS(cc, lhs, rhs);
3683 return false;
3684 case kCondNE:
3685 __ CeqS(cc, lhs, rhs);
3686 return true;
3687 case kCondLT:
3688 if (gt_bias) {
3689 __ ColtS(cc, lhs, rhs);
3690 } else {
3691 __ CultS(cc, lhs, rhs);
3692 }
3693 return false;
3694 case kCondLE:
3695 if (gt_bias) {
3696 __ ColeS(cc, lhs, rhs);
3697 } else {
3698 __ CuleS(cc, lhs, rhs);
3699 }
3700 return false;
3701 case kCondGT:
3702 if (gt_bias) {
3703 __ CultS(cc, rhs, lhs);
3704 } else {
3705 __ ColtS(cc, rhs, lhs);
3706 }
3707 return false;
3708 case kCondGE:
3709 if (gt_bias) {
3710 __ CuleS(cc, rhs, lhs);
3711 } else {
3712 __ ColeS(cc, rhs, lhs);
3713 }
3714 return false;
3715 default:
3716 LOG(FATAL) << "Unexpected non-floating-point condition";
3717 UNREACHABLE();
3718 }
3719 } else {
3720 DCHECK_EQ(type, Primitive::kPrimDouble);
3721 switch (cond) {
3722 case kCondEQ:
3723 __ CeqD(cc, lhs, rhs);
3724 return false;
3725 case kCondNE:
3726 __ CeqD(cc, lhs, rhs);
3727 return true;
3728 case kCondLT:
3729 if (gt_bias) {
3730 __ ColtD(cc, lhs, rhs);
3731 } else {
3732 __ CultD(cc, lhs, rhs);
3733 }
3734 return false;
3735 case kCondLE:
3736 if (gt_bias) {
3737 __ ColeD(cc, lhs, rhs);
3738 } else {
3739 __ CuleD(cc, lhs, rhs);
3740 }
3741 return false;
3742 case kCondGT:
3743 if (gt_bias) {
3744 __ CultD(cc, rhs, lhs);
3745 } else {
3746 __ ColtD(cc, rhs, lhs);
3747 }
3748 return false;
3749 case kCondGE:
3750 if (gt_bias) {
3751 __ CuleD(cc, rhs, lhs);
3752 } else {
3753 __ ColeD(cc, rhs, lhs);
3754 }
3755 return false;
3756 default:
3757 LOG(FATAL) << "Unexpected non-floating-point condition";
3758 UNREACHABLE();
3759 }
3760 }
3761}
3762
3763bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
3764 bool gt_bias,
3765 Primitive::Type type,
3766 LocationSummary* input_locations,
3767 FRegister dst) {
3768 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3769 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3770 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
3771 if (type == Primitive::kPrimFloat) {
3772 switch (cond) {
3773 case kCondEQ:
3774 __ CmpEqS(dst, lhs, rhs);
3775 return false;
3776 case kCondNE:
3777 __ CmpEqS(dst, lhs, rhs);
3778 return true;
3779 case kCondLT:
3780 if (gt_bias) {
3781 __ CmpLtS(dst, lhs, rhs);
3782 } else {
3783 __ CmpUltS(dst, lhs, rhs);
3784 }
3785 return false;
3786 case kCondLE:
3787 if (gt_bias) {
3788 __ CmpLeS(dst, lhs, rhs);
3789 } else {
3790 __ CmpUleS(dst, lhs, rhs);
3791 }
3792 return false;
3793 case kCondGT:
3794 if (gt_bias) {
3795 __ CmpUltS(dst, rhs, lhs);
3796 } else {
3797 __ CmpLtS(dst, rhs, lhs);
3798 }
3799 return false;
3800 case kCondGE:
3801 if (gt_bias) {
3802 __ CmpUleS(dst, rhs, lhs);
3803 } else {
3804 __ CmpLeS(dst, rhs, lhs);
3805 }
3806 return false;
3807 default:
3808 LOG(FATAL) << "Unexpected non-floating-point condition";
3809 UNREACHABLE();
3810 }
3811 } else {
3812 DCHECK_EQ(type, Primitive::kPrimDouble);
3813 switch (cond) {
3814 case kCondEQ:
3815 __ CmpEqD(dst, lhs, rhs);
3816 return false;
3817 case kCondNE:
3818 __ CmpEqD(dst, lhs, rhs);
3819 return true;
3820 case kCondLT:
3821 if (gt_bias) {
3822 __ CmpLtD(dst, lhs, rhs);
3823 } else {
3824 __ CmpUltD(dst, lhs, rhs);
3825 }
3826 return false;
3827 case kCondLE:
3828 if (gt_bias) {
3829 __ CmpLeD(dst, lhs, rhs);
3830 } else {
3831 __ CmpUleD(dst, lhs, rhs);
3832 }
3833 return false;
3834 case kCondGT:
3835 if (gt_bias) {
3836 __ CmpUltD(dst, rhs, lhs);
3837 } else {
3838 __ CmpLtD(dst, rhs, lhs);
3839 }
3840 return false;
3841 case kCondGE:
3842 if (gt_bias) {
3843 __ CmpUleD(dst, rhs, lhs);
3844 } else {
3845 __ CmpLeD(dst, rhs, lhs);
3846 }
3847 return false;
3848 default:
3849 LOG(FATAL) << "Unexpected non-floating-point condition";
3850 UNREACHABLE();
3851 }
3852 }
3853}
3854
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003855void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3856 bool gt_bias,
3857 Primitive::Type type,
3858 LocationSummary* locations,
3859 MipsLabel* label) {
3860 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3861 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3862 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3863 if (type == Primitive::kPrimFloat) {
3864 if (isR6) {
3865 switch (cond) {
3866 case kCondEQ:
3867 __ CmpEqS(FTMP, lhs, rhs);
3868 __ Bc1nez(FTMP, label);
3869 break;
3870 case kCondNE:
3871 __ CmpEqS(FTMP, lhs, rhs);
3872 __ Bc1eqz(FTMP, label);
3873 break;
3874 case kCondLT:
3875 if (gt_bias) {
3876 __ CmpLtS(FTMP, lhs, rhs);
3877 } else {
3878 __ CmpUltS(FTMP, lhs, rhs);
3879 }
3880 __ Bc1nez(FTMP, label);
3881 break;
3882 case kCondLE:
3883 if (gt_bias) {
3884 __ CmpLeS(FTMP, lhs, rhs);
3885 } else {
3886 __ CmpUleS(FTMP, lhs, rhs);
3887 }
3888 __ Bc1nez(FTMP, label);
3889 break;
3890 case kCondGT:
3891 if (gt_bias) {
3892 __ CmpUltS(FTMP, rhs, lhs);
3893 } else {
3894 __ CmpLtS(FTMP, rhs, lhs);
3895 }
3896 __ Bc1nez(FTMP, label);
3897 break;
3898 case kCondGE:
3899 if (gt_bias) {
3900 __ CmpUleS(FTMP, rhs, lhs);
3901 } else {
3902 __ CmpLeS(FTMP, rhs, lhs);
3903 }
3904 __ Bc1nez(FTMP, label);
3905 break;
3906 default:
3907 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003908 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003909 }
3910 } else {
3911 switch (cond) {
3912 case kCondEQ:
3913 __ CeqS(0, lhs, rhs);
3914 __ Bc1t(0, label);
3915 break;
3916 case kCondNE:
3917 __ CeqS(0, lhs, rhs);
3918 __ Bc1f(0, label);
3919 break;
3920 case kCondLT:
3921 if (gt_bias) {
3922 __ ColtS(0, lhs, rhs);
3923 } else {
3924 __ CultS(0, lhs, rhs);
3925 }
3926 __ Bc1t(0, label);
3927 break;
3928 case kCondLE:
3929 if (gt_bias) {
3930 __ ColeS(0, lhs, rhs);
3931 } else {
3932 __ CuleS(0, lhs, rhs);
3933 }
3934 __ Bc1t(0, label);
3935 break;
3936 case kCondGT:
3937 if (gt_bias) {
3938 __ CultS(0, rhs, lhs);
3939 } else {
3940 __ ColtS(0, rhs, lhs);
3941 }
3942 __ Bc1t(0, label);
3943 break;
3944 case kCondGE:
3945 if (gt_bias) {
3946 __ CuleS(0, rhs, lhs);
3947 } else {
3948 __ ColeS(0, rhs, lhs);
3949 }
3950 __ Bc1t(0, label);
3951 break;
3952 default:
3953 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003954 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003955 }
3956 }
3957 } else {
3958 DCHECK_EQ(type, Primitive::kPrimDouble);
3959 if (isR6) {
3960 switch (cond) {
3961 case kCondEQ:
3962 __ CmpEqD(FTMP, lhs, rhs);
3963 __ Bc1nez(FTMP, label);
3964 break;
3965 case kCondNE:
3966 __ CmpEqD(FTMP, lhs, rhs);
3967 __ Bc1eqz(FTMP, label);
3968 break;
3969 case kCondLT:
3970 if (gt_bias) {
3971 __ CmpLtD(FTMP, lhs, rhs);
3972 } else {
3973 __ CmpUltD(FTMP, lhs, rhs);
3974 }
3975 __ Bc1nez(FTMP, label);
3976 break;
3977 case kCondLE:
3978 if (gt_bias) {
3979 __ CmpLeD(FTMP, lhs, rhs);
3980 } else {
3981 __ CmpUleD(FTMP, lhs, rhs);
3982 }
3983 __ Bc1nez(FTMP, label);
3984 break;
3985 case kCondGT:
3986 if (gt_bias) {
3987 __ CmpUltD(FTMP, rhs, lhs);
3988 } else {
3989 __ CmpLtD(FTMP, rhs, lhs);
3990 }
3991 __ Bc1nez(FTMP, label);
3992 break;
3993 case kCondGE:
3994 if (gt_bias) {
3995 __ CmpUleD(FTMP, rhs, lhs);
3996 } else {
3997 __ CmpLeD(FTMP, rhs, lhs);
3998 }
3999 __ Bc1nez(FTMP, label);
4000 break;
4001 default:
4002 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004003 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004004 }
4005 } else {
4006 switch (cond) {
4007 case kCondEQ:
4008 __ CeqD(0, lhs, rhs);
4009 __ Bc1t(0, label);
4010 break;
4011 case kCondNE:
4012 __ CeqD(0, lhs, rhs);
4013 __ Bc1f(0, label);
4014 break;
4015 case kCondLT:
4016 if (gt_bias) {
4017 __ ColtD(0, lhs, rhs);
4018 } else {
4019 __ CultD(0, lhs, rhs);
4020 }
4021 __ Bc1t(0, label);
4022 break;
4023 case kCondLE:
4024 if (gt_bias) {
4025 __ ColeD(0, lhs, rhs);
4026 } else {
4027 __ CuleD(0, lhs, rhs);
4028 }
4029 __ Bc1t(0, label);
4030 break;
4031 case kCondGT:
4032 if (gt_bias) {
4033 __ CultD(0, rhs, lhs);
4034 } else {
4035 __ ColtD(0, rhs, lhs);
4036 }
4037 __ Bc1t(0, label);
4038 break;
4039 case kCondGE:
4040 if (gt_bias) {
4041 __ CuleD(0, rhs, lhs);
4042 } else {
4043 __ ColeD(0, rhs, lhs);
4044 }
4045 __ Bc1t(0, label);
4046 break;
4047 default:
4048 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004049 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004050 }
4051 }
4052 }
4053}
4054
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004055void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004056 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004057 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004058 MipsLabel* false_target) {
4059 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004060
David Brazdil0debae72015-11-12 18:37:00 +00004061 if (true_target == nullptr && false_target == nullptr) {
4062 // Nothing to do. The code always falls through.
4063 return;
4064 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004065 // Constant condition, statically compared against "true" (integer value 1).
4066 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004067 if (true_target != nullptr) {
4068 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004069 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004070 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004071 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004072 if (false_target != nullptr) {
4073 __ B(false_target);
4074 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004075 }
David Brazdil0debae72015-11-12 18:37:00 +00004076 return;
4077 }
4078
4079 // The following code generates these patterns:
4080 // (1) true_target == nullptr && false_target != nullptr
4081 // - opposite condition true => branch to false_target
4082 // (2) true_target != nullptr && false_target == nullptr
4083 // - condition true => branch to true_target
4084 // (3) true_target != nullptr && false_target != nullptr
4085 // - condition true => branch to true_target
4086 // - branch to false_target
4087 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004088 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004089 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004090 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004091 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004092 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4093 } else {
4094 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4095 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004096 } else {
4097 // The condition instruction has not been materialized, use its inputs as
4098 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004099 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004100 Primitive::Type type = condition->InputAt(0)->GetType();
4101 LocationSummary* locations = cond->GetLocations();
4102 IfCondition if_cond = condition->GetCondition();
4103 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004104
David Brazdil0debae72015-11-12 18:37:00 +00004105 if (true_target == nullptr) {
4106 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004107 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004108 }
4109
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004110 switch (type) {
4111 default:
4112 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4113 break;
4114 case Primitive::kPrimLong:
4115 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4116 break;
4117 case Primitive::kPrimFloat:
4118 case Primitive::kPrimDouble:
4119 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4120 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004121 }
4122 }
David Brazdil0debae72015-11-12 18:37:00 +00004123
4124 // If neither branch falls through (case 3), the conditional branch to `true_target`
4125 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4126 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004127 __ B(false_target);
4128 }
4129}
4130
4131void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4132 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004133 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004134 locations->SetInAt(0, Location::RequiresRegister());
4135 }
4136}
4137
4138void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004139 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4140 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4141 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4142 nullptr : codegen_->GetLabelOf(true_successor);
4143 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4144 nullptr : codegen_->GetLabelOf(false_successor);
4145 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004146}
4147
4148void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4149 LocationSummary* locations = new (GetGraph()->GetArena())
4150 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004151 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004152 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004153 locations->SetInAt(0, Location::RequiresRegister());
4154 }
4155}
4156
4157void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004158 SlowPathCodeMIPS* slow_path =
4159 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004160 GenerateTestAndBranch(deoptimize,
4161 /* condition_input_index */ 0,
4162 slow_path->GetEntryLabel(),
4163 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004164}
4165
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004166// This function returns true if a conditional move can be generated for HSelect.
4167// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4168// branches and regular moves.
4169//
4170// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4171//
4172// While determining feasibility of a conditional move and setting inputs/outputs
4173// are two distinct tasks, this function does both because they share quite a bit
4174// of common logic.
4175static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4176 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4177 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4178 HCondition* condition = cond->AsCondition();
4179
4180 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4181 Primitive::Type dst_type = select->GetType();
4182
4183 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4184 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4185 bool is_true_value_zero_constant =
4186 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4187 bool is_false_value_zero_constant =
4188 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4189
4190 bool can_move_conditionally = false;
4191 bool use_const_for_false_in = false;
4192 bool use_const_for_true_in = false;
4193
4194 if (!cond->IsConstant()) {
4195 switch (cond_type) {
4196 default:
4197 switch (dst_type) {
4198 default:
4199 // Moving int on int condition.
4200 if (is_r6) {
4201 if (is_true_value_zero_constant) {
4202 // seleqz out_reg, false_reg, cond_reg
4203 can_move_conditionally = true;
4204 use_const_for_true_in = true;
4205 } else if (is_false_value_zero_constant) {
4206 // selnez out_reg, true_reg, cond_reg
4207 can_move_conditionally = true;
4208 use_const_for_false_in = true;
4209 } else if (materialized) {
4210 // Not materializing unmaterialized int conditions
4211 // to keep the instruction count low.
4212 // selnez AT, true_reg, cond_reg
4213 // seleqz TMP, false_reg, cond_reg
4214 // or out_reg, AT, TMP
4215 can_move_conditionally = true;
4216 }
4217 } else {
4218 // movn out_reg, true_reg/ZERO, cond_reg
4219 can_move_conditionally = true;
4220 use_const_for_true_in = is_true_value_zero_constant;
4221 }
4222 break;
4223 case Primitive::kPrimLong:
4224 // Moving long on int condition.
4225 if (is_r6) {
4226 if (is_true_value_zero_constant) {
4227 // seleqz out_reg_lo, false_reg_lo, cond_reg
4228 // seleqz out_reg_hi, false_reg_hi, cond_reg
4229 can_move_conditionally = true;
4230 use_const_for_true_in = true;
4231 } else if (is_false_value_zero_constant) {
4232 // selnez out_reg_lo, true_reg_lo, cond_reg
4233 // selnez out_reg_hi, true_reg_hi, cond_reg
4234 can_move_conditionally = true;
4235 use_const_for_false_in = true;
4236 }
4237 // Other long conditional moves would generate 6+ instructions,
4238 // which is too many.
4239 } else {
4240 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4241 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4242 can_move_conditionally = true;
4243 use_const_for_true_in = is_true_value_zero_constant;
4244 }
4245 break;
4246 case Primitive::kPrimFloat:
4247 case Primitive::kPrimDouble:
4248 // Moving float/double on int condition.
4249 if (is_r6) {
4250 if (materialized) {
4251 // Not materializing unmaterialized int conditions
4252 // to keep the instruction count low.
4253 can_move_conditionally = true;
4254 if (is_true_value_zero_constant) {
4255 // sltu TMP, ZERO, cond_reg
4256 // mtc1 TMP, temp_cond_reg
4257 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4258 use_const_for_true_in = true;
4259 } else if (is_false_value_zero_constant) {
4260 // sltu TMP, ZERO, cond_reg
4261 // mtc1 TMP, temp_cond_reg
4262 // selnez.fmt out_reg, true_reg, temp_cond_reg
4263 use_const_for_false_in = true;
4264 } else {
4265 // sltu TMP, ZERO, cond_reg
4266 // mtc1 TMP, temp_cond_reg
4267 // sel.fmt temp_cond_reg, false_reg, true_reg
4268 // mov.fmt out_reg, temp_cond_reg
4269 }
4270 }
4271 } else {
4272 // movn.fmt out_reg, true_reg, cond_reg
4273 can_move_conditionally = true;
4274 }
4275 break;
4276 }
4277 break;
4278 case Primitive::kPrimLong:
4279 // We don't materialize long comparison now
4280 // and use conditional branches instead.
4281 break;
4282 case Primitive::kPrimFloat:
4283 case Primitive::kPrimDouble:
4284 switch (dst_type) {
4285 default:
4286 // Moving int on float/double condition.
4287 if (is_r6) {
4288 if (is_true_value_zero_constant) {
4289 // mfc1 TMP, temp_cond_reg
4290 // seleqz out_reg, false_reg, TMP
4291 can_move_conditionally = true;
4292 use_const_for_true_in = true;
4293 } else if (is_false_value_zero_constant) {
4294 // mfc1 TMP, temp_cond_reg
4295 // selnez out_reg, true_reg, TMP
4296 can_move_conditionally = true;
4297 use_const_for_false_in = true;
4298 } else {
4299 // mfc1 TMP, temp_cond_reg
4300 // selnez AT, true_reg, TMP
4301 // seleqz TMP, false_reg, TMP
4302 // or out_reg, AT, TMP
4303 can_move_conditionally = true;
4304 }
4305 } else {
4306 // movt out_reg, true_reg/ZERO, cc
4307 can_move_conditionally = true;
4308 use_const_for_true_in = is_true_value_zero_constant;
4309 }
4310 break;
4311 case Primitive::kPrimLong:
4312 // Moving long on float/double condition.
4313 if (is_r6) {
4314 if (is_true_value_zero_constant) {
4315 // mfc1 TMP, temp_cond_reg
4316 // seleqz out_reg_lo, false_reg_lo, TMP
4317 // seleqz out_reg_hi, false_reg_hi, TMP
4318 can_move_conditionally = true;
4319 use_const_for_true_in = true;
4320 } else if (is_false_value_zero_constant) {
4321 // mfc1 TMP, temp_cond_reg
4322 // selnez out_reg_lo, true_reg_lo, TMP
4323 // selnez out_reg_hi, true_reg_hi, TMP
4324 can_move_conditionally = true;
4325 use_const_for_false_in = true;
4326 }
4327 // Other long conditional moves would generate 6+ instructions,
4328 // which is too many.
4329 } else {
4330 // movt out_reg_lo, true_reg_lo/ZERO, cc
4331 // movt out_reg_hi, true_reg_hi/ZERO, cc
4332 can_move_conditionally = true;
4333 use_const_for_true_in = is_true_value_zero_constant;
4334 }
4335 break;
4336 case Primitive::kPrimFloat:
4337 case Primitive::kPrimDouble:
4338 // Moving float/double on float/double condition.
4339 if (is_r6) {
4340 can_move_conditionally = true;
4341 if (is_true_value_zero_constant) {
4342 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4343 use_const_for_true_in = true;
4344 } else if (is_false_value_zero_constant) {
4345 // selnez.fmt out_reg, true_reg, temp_cond_reg
4346 use_const_for_false_in = true;
4347 } else {
4348 // sel.fmt temp_cond_reg, false_reg, true_reg
4349 // mov.fmt out_reg, temp_cond_reg
4350 }
4351 } else {
4352 // movt.fmt out_reg, true_reg, cc
4353 can_move_conditionally = true;
4354 }
4355 break;
4356 }
4357 break;
4358 }
4359 }
4360
4361 if (can_move_conditionally) {
4362 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4363 } else {
4364 DCHECK(!use_const_for_false_in);
4365 DCHECK(!use_const_for_true_in);
4366 }
4367
4368 if (locations_to_set != nullptr) {
4369 if (use_const_for_false_in) {
4370 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4371 } else {
4372 locations_to_set->SetInAt(0,
4373 Primitive::IsFloatingPointType(dst_type)
4374 ? Location::RequiresFpuRegister()
4375 : Location::RequiresRegister());
4376 }
4377 if (use_const_for_true_in) {
4378 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4379 } else {
4380 locations_to_set->SetInAt(1,
4381 Primitive::IsFloatingPointType(dst_type)
4382 ? Location::RequiresFpuRegister()
4383 : Location::RequiresRegister());
4384 }
4385 if (materialized) {
4386 locations_to_set->SetInAt(2, Location::RequiresRegister());
4387 }
4388 // On R6 we don't require the output to be the same as the
4389 // first input for conditional moves unlike on R2.
4390 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4391 if (is_out_same_as_first_in) {
4392 locations_to_set->SetOut(Location::SameAsFirstInput());
4393 } else {
4394 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4395 ? Location::RequiresFpuRegister()
4396 : Location::RequiresRegister());
4397 }
4398 }
4399
4400 return can_move_conditionally;
4401}
4402
4403void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4404 LocationSummary* locations = select->GetLocations();
4405 Location dst = locations->Out();
4406 Location src = locations->InAt(1);
4407 Register src_reg = ZERO;
4408 Register src_reg_high = ZERO;
4409 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4410 Register cond_reg = TMP;
4411 int cond_cc = 0;
4412 Primitive::Type cond_type = Primitive::kPrimInt;
4413 bool cond_inverted = false;
4414 Primitive::Type dst_type = select->GetType();
4415
4416 if (IsBooleanValueOrMaterializedCondition(cond)) {
4417 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4418 } else {
4419 HCondition* condition = cond->AsCondition();
4420 LocationSummary* cond_locations = cond->GetLocations();
4421 IfCondition if_cond = condition->GetCondition();
4422 cond_type = condition->InputAt(0)->GetType();
4423 switch (cond_type) {
4424 default:
4425 DCHECK_NE(cond_type, Primitive::kPrimLong);
4426 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4427 break;
4428 case Primitive::kPrimFloat:
4429 case Primitive::kPrimDouble:
4430 cond_inverted = MaterializeFpCompareR2(if_cond,
4431 condition->IsGtBias(),
4432 cond_type,
4433 cond_locations,
4434 cond_cc);
4435 break;
4436 }
4437 }
4438
4439 DCHECK(dst.Equals(locations->InAt(0)));
4440 if (src.IsRegister()) {
4441 src_reg = src.AsRegister<Register>();
4442 } else if (src.IsRegisterPair()) {
4443 src_reg = src.AsRegisterPairLow<Register>();
4444 src_reg_high = src.AsRegisterPairHigh<Register>();
4445 } else if (src.IsConstant()) {
4446 DCHECK(src.GetConstant()->IsZeroBitPattern());
4447 }
4448
4449 switch (cond_type) {
4450 default:
4451 switch (dst_type) {
4452 default:
4453 if (cond_inverted) {
4454 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4455 } else {
4456 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4457 }
4458 break;
4459 case Primitive::kPrimLong:
4460 if (cond_inverted) {
4461 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4462 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4463 } else {
4464 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4465 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4466 }
4467 break;
4468 case Primitive::kPrimFloat:
4469 if (cond_inverted) {
4470 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4471 } else {
4472 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4473 }
4474 break;
4475 case Primitive::kPrimDouble:
4476 if (cond_inverted) {
4477 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4478 } else {
4479 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4480 }
4481 break;
4482 }
4483 break;
4484 case Primitive::kPrimLong:
4485 LOG(FATAL) << "Unreachable";
4486 UNREACHABLE();
4487 case Primitive::kPrimFloat:
4488 case Primitive::kPrimDouble:
4489 switch (dst_type) {
4490 default:
4491 if (cond_inverted) {
4492 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4493 } else {
4494 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4495 }
4496 break;
4497 case Primitive::kPrimLong:
4498 if (cond_inverted) {
4499 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4500 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4501 } else {
4502 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4503 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4504 }
4505 break;
4506 case Primitive::kPrimFloat:
4507 if (cond_inverted) {
4508 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4509 } else {
4510 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4511 }
4512 break;
4513 case Primitive::kPrimDouble:
4514 if (cond_inverted) {
4515 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4516 } else {
4517 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4518 }
4519 break;
4520 }
4521 break;
4522 }
4523}
4524
4525void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4526 LocationSummary* locations = select->GetLocations();
4527 Location dst = locations->Out();
4528 Location false_src = locations->InAt(0);
4529 Location true_src = locations->InAt(1);
4530 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4531 Register cond_reg = TMP;
4532 FRegister fcond_reg = FTMP;
4533 Primitive::Type cond_type = Primitive::kPrimInt;
4534 bool cond_inverted = false;
4535 Primitive::Type dst_type = select->GetType();
4536
4537 if (IsBooleanValueOrMaterializedCondition(cond)) {
4538 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4539 } else {
4540 HCondition* condition = cond->AsCondition();
4541 LocationSummary* cond_locations = cond->GetLocations();
4542 IfCondition if_cond = condition->GetCondition();
4543 cond_type = condition->InputAt(0)->GetType();
4544 switch (cond_type) {
4545 default:
4546 DCHECK_NE(cond_type, Primitive::kPrimLong);
4547 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4548 break;
4549 case Primitive::kPrimFloat:
4550 case Primitive::kPrimDouble:
4551 cond_inverted = MaterializeFpCompareR6(if_cond,
4552 condition->IsGtBias(),
4553 cond_type,
4554 cond_locations,
4555 fcond_reg);
4556 break;
4557 }
4558 }
4559
4560 if (true_src.IsConstant()) {
4561 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4562 }
4563 if (false_src.IsConstant()) {
4564 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4565 }
4566
4567 switch (dst_type) {
4568 default:
4569 if (Primitive::IsFloatingPointType(cond_type)) {
4570 __ Mfc1(cond_reg, fcond_reg);
4571 }
4572 if (true_src.IsConstant()) {
4573 if (cond_inverted) {
4574 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4575 } else {
4576 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4577 }
4578 } else if (false_src.IsConstant()) {
4579 if (cond_inverted) {
4580 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4581 } else {
4582 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4583 }
4584 } else {
4585 DCHECK_NE(cond_reg, AT);
4586 if (cond_inverted) {
4587 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4588 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4589 } else {
4590 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4591 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4592 }
4593 __ Or(dst.AsRegister<Register>(), AT, TMP);
4594 }
4595 break;
4596 case Primitive::kPrimLong: {
4597 if (Primitive::IsFloatingPointType(cond_type)) {
4598 __ Mfc1(cond_reg, fcond_reg);
4599 }
4600 Register dst_lo = dst.AsRegisterPairLow<Register>();
4601 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4602 if (true_src.IsConstant()) {
4603 Register src_lo = false_src.AsRegisterPairLow<Register>();
4604 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4605 if (cond_inverted) {
4606 __ Selnez(dst_lo, src_lo, cond_reg);
4607 __ Selnez(dst_hi, src_hi, cond_reg);
4608 } else {
4609 __ Seleqz(dst_lo, src_lo, cond_reg);
4610 __ Seleqz(dst_hi, src_hi, cond_reg);
4611 }
4612 } else {
4613 DCHECK(false_src.IsConstant());
4614 Register src_lo = true_src.AsRegisterPairLow<Register>();
4615 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4616 if (cond_inverted) {
4617 __ Seleqz(dst_lo, src_lo, cond_reg);
4618 __ Seleqz(dst_hi, src_hi, cond_reg);
4619 } else {
4620 __ Selnez(dst_lo, src_lo, cond_reg);
4621 __ Selnez(dst_hi, src_hi, cond_reg);
4622 }
4623 }
4624 break;
4625 }
4626 case Primitive::kPrimFloat: {
4627 if (!Primitive::IsFloatingPointType(cond_type)) {
4628 // sel*.fmt tests bit 0 of the condition register, account for that.
4629 __ Sltu(TMP, ZERO, cond_reg);
4630 __ Mtc1(TMP, fcond_reg);
4631 }
4632 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4633 if (true_src.IsConstant()) {
4634 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4635 if (cond_inverted) {
4636 __ SelnezS(dst_reg, src_reg, fcond_reg);
4637 } else {
4638 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4639 }
4640 } else if (false_src.IsConstant()) {
4641 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4642 if (cond_inverted) {
4643 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4644 } else {
4645 __ SelnezS(dst_reg, src_reg, fcond_reg);
4646 }
4647 } else {
4648 if (cond_inverted) {
4649 __ SelS(fcond_reg,
4650 true_src.AsFpuRegister<FRegister>(),
4651 false_src.AsFpuRegister<FRegister>());
4652 } else {
4653 __ SelS(fcond_reg,
4654 false_src.AsFpuRegister<FRegister>(),
4655 true_src.AsFpuRegister<FRegister>());
4656 }
4657 __ MovS(dst_reg, fcond_reg);
4658 }
4659 break;
4660 }
4661 case Primitive::kPrimDouble: {
4662 if (!Primitive::IsFloatingPointType(cond_type)) {
4663 // sel*.fmt tests bit 0 of the condition register, account for that.
4664 __ Sltu(TMP, ZERO, cond_reg);
4665 __ Mtc1(TMP, fcond_reg);
4666 }
4667 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4668 if (true_src.IsConstant()) {
4669 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4670 if (cond_inverted) {
4671 __ SelnezD(dst_reg, src_reg, fcond_reg);
4672 } else {
4673 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4674 }
4675 } else if (false_src.IsConstant()) {
4676 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4677 if (cond_inverted) {
4678 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4679 } else {
4680 __ SelnezD(dst_reg, src_reg, fcond_reg);
4681 }
4682 } else {
4683 if (cond_inverted) {
4684 __ SelD(fcond_reg,
4685 true_src.AsFpuRegister<FRegister>(),
4686 false_src.AsFpuRegister<FRegister>());
4687 } else {
4688 __ SelD(fcond_reg,
4689 false_src.AsFpuRegister<FRegister>(),
4690 true_src.AsFpuRegister<FRegister>());
4691 }
4692 __ MovD(dst_reg, fcond_reg);
4693 }
4694 break;
4695 }
4696 }
4697}
4698
David Brazdil74eb1b22015-12-14 11:44:01 +00004699void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4700 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004701 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004702}
4703
4704void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004705 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
4706 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
4707 if (is_r6) {
4708 GenConditionalMoveR6(select);
4709 } else {
4710 GenConditionalMoveR2(select);
4711 }
4712 } else {
4713 LocationSummary* locations = select->GetLocations();
4714 MipsLabel false_target;
4715 GenerateTestAndBranch(select,
4716 /* condition_input_index */ 2,
4717 /* true_target */ nullptr,
4718 &false_target);
4719 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4720 __ Bind(&false_target);
4721 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004722}
4723
David Srbecky0cf44932015-12-09 14:09:59 +00004724void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4725 new (GetGraph()->GetArena()) LocationSummary(info);
4726}
4727
David Srbeckyd28f4a02016-03-14 17:14:24 +00004728void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
4729 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004730}
4731
4732void CodeGeneratorMIPS::GenerateNop() {
4733 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004734}
4735
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004736void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4737 Primitive::Type field_type = field_info.GetFieldType();
4738 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4739 bool generate_volatile = field_info.IsVolatile() && is_wide;
4740 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004741 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004742
4743 locations->SetInAt(0, Location::RequiresRegister());
4744 if (generate_volatile) {
4745 InvokeRuntimeCallingConvention calling_convention;
4746 // need A0 to hold base + offset
4747 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4748 if (field_type == Primitive::kPrimLong) {
4749 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
4750 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004751 // Use Location::Any() to prevent situations when running out of available fp registers.
4752 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004753 // Need some temp core regs since FP results are returned in core registers
4754 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
4755 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
4756 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
4757 }
4758 } else {
4759 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4760 locations->SetOut(Location::RequiresFpuRegister());
4761 } else {
4762 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4763 }
4764 }
4765}
4766
4767void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
4768 const FieldInfo& field_info,
4769 uint32_t dex_pc) {
4770 Primitive::Type type = field_info.GetFieldType();
4771 LocationSummary* locations = instruction->GetLocations();
4772 Register obj = locations->InAt(0).AsRegister<Register>();
4773 LoadOperandType load_type = kLoadUnsignedByte;
4774 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004775 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004776 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004777
4778 switch (type) {
4779 case Primitive::kPrimBoolean:
4780 load_type = kLoadUnsignedByte;
4781 break;
4782 case Primitive::kPrimByte:
4783 load_type = kLoadSignedByte;
4784 break;
4785 case Primitive::kPrimShort:
4786 load_type = kLoadSignedHalfword;
4787 break;
4788 case Primitive::kPrimChar:
4789 load_type = kLoadUnsignedHalfword;
4790 break;
4791 case Primitive::kPrimInt:
4792 case Primitive::kPrimFloat:
4793 case Primitive::kPrimNot:
4794 load_type = kLoadWord;
4795 break;
4796 case Primitive::kPrimLong:
4797 case Primitive::kPrimDouble:
4798 load_type = kLoadDoubleword;
4799 break;
4800 case Primitive::kPrimVoid:
4801 LOG(FATAL) << "Unreachable type " << type;
4802 UNREACHABLE();
4803 }
4804
4805 if (is_volatile && load_type == kLoadDoubleword) {
4806 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004807 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004808 // Do implicit Null check
4809 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4810 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01004811 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004812 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
4813 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004814 // FP results are returned in core registers. Need to move them.
4815 Location out = locations->Out();
4816 if (out.IsFpuRegister()) {
4817 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4818 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4819 out.AsFpuRegister<FRegister>());
4820 } else {
4821 DCHECK(out.IsDoubleStackSlot());
4822 __ StoreToOffset(kStoreWord,
4823 locations->GetTemp(1).AsRegister<Register>(),
4824 SP,
4825 out.GetStackIndex());
4826 __ StoreToOffset(kStoreWord,
4827 locations->GetTemp(2).AsRegister<Register>(),
4828 SP,
4829 out.GetStackIndex() + 4);
4830 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004831 }
4832 } else {
4833 if (!Primitive::IsFloatingPointType(type)) {
4834 Register dst;
4835 if (type == Primitive::kPrimLong) {
4836 DCHECK(locations->Out().IsRegisterPair());
4837 dst = locations->Out().AsRegisterPairLow<Register>();
4838 } else {
4839 DCHECK(locations->Out().IsRegister());
4840 dst = locations->Out().AsRegister<Register>();
4841 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004842 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004843 } else {
4844 DCHECK(locations->Out().IsFpuRegister());
4845 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4846 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004847 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004848 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004849 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004850 }
4851 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004852 }
4853
4854 if (is_volatile) {
4855 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4856 }
4857}
4858
4859void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4860 Primitive::Type field_type = field_info.GetFieldType();
4861 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4862 bool generate_volatile = field_info.IsVolatile() && is_wide;
4863 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004864 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004865
4866 locations->SetInAt(0, Location::RequiresRegister());
4867 if (generate_volatile) {
4868 InvokeRuntimeCallingConvention calling_convention;
4869 // need A0 to hold base + offset
4870 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4871 if (field_type == Primitive::kPrimLong) {
4872 locations->SetInAt(1, Location::RegisterPairLocation(
4873 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4874 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004875 // Use Location::Any() to prevent situations when running out of available fp registers.
4876 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004877 // Pass FP parameters in core registers.
4878 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4879 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4880 }
4881 } else {
4882 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004883 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004884 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004885 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004886 }
4887 }
4888}
4889
4890void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
4891 const FieldInfo& field_info,
4892 uint32_t dex_pc) {
4893 Primitive::Type type = field_info.GetFieldType();
4894 LocationSummary* locations = instruction->GetLocations();
4895 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07004896 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004897 StoreOperandType store_type = kStoreByte;
4898 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004899 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004900 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004901
4902 switch (type) {
4903 case Primitive::kPrimBoolean:
4904 case Primitive::kPrimByte:
4905 store_type = kStoreByte;
4906 break;
4907 case Primitive::kPrimShort:
4908 case Primitive::kPrimChar:
4909 store_type = kStoreHalfword;
4910 break;
4911 case Primitive::kPrimInt:
4912 case Primitive::kPrimFloat:
4913 case Primitive::kPrimNot:
4914 store_type = kStoreWord;
4915 break;
4916 case Primitive::kPrimLong:
4917 case Primitive::kPrimDouble:
4918 store_type = kStoreDoubleword;
4919 break;
4920 case Primitive::kPrimVoid:
4921 LOG(FATAL) << "Unreachable type " << type;
4922 UNREACHABLE();
4923 }
4924
4925 if (is_volatile) {
4926 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4927 }
4928
4929 if (is_volatile && store_type == kStoreDoubleword) {
4930 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004931 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004932 // Do implicit Null check.
4933 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4934 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4935 if (type == Primitive::kPrimDouble) {
4936 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004937 if (value_location.IsFpuRegister()) {
4938 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4939 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004940 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004941 value_location.AsFpuRegister<FRegister>());
4942 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004943 __ LoadFromOffset(kLoadWord,
4944 locations->GetTemp(1).AsRegister<Register>(),
4945 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004946 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004947 __ LoadFromOffset(kLoadWord,
4948 locations->GetTemp(2).AsRegister<Register>(),
4949 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004950 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004951 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004952 DCHECK(value_location.IsConstant());
4953 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4954 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004955 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4956 locations->GetTemp(1).AsRegister<Register>(),
4957 value);
4958 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004959 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004960 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004961 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4962 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004963 if (value_location.IsConstant()) {
4964 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4965 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4966 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004967 Register src;
4968 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004969 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004970 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004971 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004972 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004973 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004974 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004975 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004976 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004977 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004978 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004979 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004980 }
4981 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004982 }
4983
4984 // TODO: memory barriers?
4985 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004986 Register src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004987 codegen_->MarkGCCard(obj, src);
4988 }
4989
4990 if (is_volatile) {
4991 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4992 }
4993}
4994
4995void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4996 HandleFieldGet(instruction, instruction->GetFieldInfo());
4997}
4998
4999void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5000 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5001}
5002
5003void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5004 HandleFieldSet(instruction, instruction->GetFieldInfo());
5005}
5006
5007void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5008 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5009}
5010
Alexey Frunze06a46c42016-07-19 15:00:40 -07005011void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
5012 HInstruction* instruction ATTRIBUTE_UNUSED,
5013 Location root,
5014 Register obj,
5015 uint32_t offset) {
5016 Register root_reg = root.AsRegister<Register>();
5017 if (kEmitCompilerReadBarrier) {
5018 UNIMPLEMENTED(FATAL) << "for read barrier";
5019 } else {
5020 // Plain GC root load with no read barrier.
5021 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5022 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5023 // Note that GC roots are not affected by heap poisoning, thus we
5024 // do not have to unpoison `root_reg` here.
5025 }
5026}
5027
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005028void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5029 LocationSummary::CallKind call_kind =
5030 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5031 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5032 locations->SetInAt(0, Location::RequiresRegister());
5033 locations->SetInAt(1, Location::RequiresRegister());
5034 // The output does overlap inputs.
5035 // Note that TypeCheckSlowPathMIPS uses this register too.
5036 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5037}
5038
5039void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5040 LocationSummary* locations = instruction->GetLocations();
5041 Register obj = locations->InAt(0).AsRegister<Register>();
5042 Register cls = locations->InAt(1).AsRegister<Register>();
5043 Register out = locations->Out().AsRegister<Register>();
5044
5045 MipsLabel done;
5046
5047 // Return 0 if `obj` is null.
5048 // TODO: Avoid this check if we know `obj` is not null.
5049 __ Move(out, ZERO);
5050 __ Beqz(obj, &done);
5051
5052 // Compare the class of `obj` with `cls`.
5053 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
5054 if (instruction->IsExactCheck()) {
5055 // Classes must be equal for the instanceof to succeed.
5056 __ Xor(out, out, cls);
5057 __ Sltiu(out, out, 1);
5058 } else {
5059 // If the classes are not equal, we go into a slow path.
5060 DCHECK(locations->OnlyCallsOnSlowPath());
5061 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5062 codegen_->AddSlowPath(slow_path);
5063 __ Bne(out, cls, slow_path->GetEntryLabel());
5064 __ LoadConst32(out, 1);
5065 __ Bind(slow_path->GetExitLabel());
5066 }
5067
5068 __ Bind(&done);
5069}
5070
5071void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5072 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5073 locations->SetOut(Location::ConstantLocation(constant));
5074}
5075
5076void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5077 // Will be generated at use site.
5078}
5079
5080void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5081 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5082 locations->SetOut(Location::ConstantLocation(constant));
5083}
5084
5085void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5086 // Will be generated at use site.
5087}
5088
5089void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5090 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5091 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5092}
5093
5094void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5095 HandleInvoke(invoke);
5096 // The register T0 is required to be used for the hidden argument in
5097 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5098 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5099}
5100
5101void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5102 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5103 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005104 Location receiver = invoke->GetLocations()->InAt(0);
5105 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005106 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005107
5108 // Set the hidden argument.
5109 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5110 invoke->GetDexMethodIndex());
5111
5112 // temp = object->GetClass();
5113 if (receiver.IsStackSlot()) {
5114 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5115 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5116 } else {
5117 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5118 }
5119 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005120 __ LoadFromOffset(kLoadWord, temp, temp,
5121 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5122 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005123 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005124 // temp = temp->GetImtEntryAt(method_offset);
5125 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5126 // T9 = temp->GetEntryPoint();
5127 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5128 // T9();
5129 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005130 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005131 DCHECK(!codegen_->IsLeafMethod());
5132 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5133}
5134
5135void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005136 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5137 if (intrinsic.TryDispatch(invoke)) {
5138 return;
5139 }
5140
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005141 HandleInvoke(invoke);
5142}
5143
5144void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005145 // Explicit clinit checks triggered by static invokes must have been pruned by
5146 // art::PrepareForRegisterAllocation.
5147 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005148
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005149 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5150 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5151 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5152
5153 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
5154 // R6 has PC-relative addressing.
5155 bool has_extra_input = !isR6 &&
5156 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
5157 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
5158
5159 if (invoke->HasPcRelativeDexCache()) {
5160 // kDexCachePcRelative is mutually exclusive with
5161 // kDirectAddressWithFixup/kCallDirectWithFixup.
5162 CHECK(!has_extra_input);
5163 has_extra_input = true;
5164 }
5165
Chris Larsen701566a2015-10-27 15:29:13 -07005166 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5167 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005168 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5169 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5170 }
Chris Larsen701566a2015-10-27 15:29:13 -07005171 return;
5172 }
5173
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005174 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005175
5176 // Add the extra input register if either the dex cache array base register
5177 // or the PC-relative base register for accessing literals is needed.
5178 if (has_extra_input) {
5179 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5180 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005181}
5182
Chris Larsen701566a2015-10-27 15:29:13 -07005183static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005184 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005185 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5186 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005187 return true;
5188 }
5189 return false;
5190}
5191
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005192HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005193 HLoadString::LoadKind desired_string_load_kind) {
5194 if (kEmitCompilerReadBarrier) {
5195 UNIMPLEMENTED(FATAL) << "for read barrier";
5196 }
5197 // We disable PC-relative load when there is an irreducible loop, as the optimization
5198 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005199 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5200 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005201 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5202 bool fallback_load = has_irreducible_loops;
5203 switch (desired_string_load_kind) {
5204 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5205 DCHECK(!GetCompilerOptions().GetCompilePic());
5206 break;
5207 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5208 DCHECK(GetCompilerOptions().GetCompilePic());
5209 break;
5210 case HLoadString::LoadKind::kBootImageAddress:
5211 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005212 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005213 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005214 break;
5215 case HLoadString::LoadKind::kDexCacheViaMethod:
5216 fallback_load = false;
5217 break;
5218 }
5219 if (fallback_load) {
5220 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5221 }
5222 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005223}
5224
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005225HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5226 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005227 if (kEmitCompilerReadBarrier) {
5228 UNIMPLEMENTED(FATAL) << "for read barrier";
5229 }
5230 // We disable pc-relative load when there is an irreducible loop, as the optimization
5231 // is incompatible with it.
5232 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5233 bool fallback_load = has_irreducible_loops;
5234 switch (desired_class_load_kind) {
5235 case HLoadClass::LoadKind::kReferrersClass:
5236 fallback_load = false;
5237 break;
5238 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5239 DCHECK(!GetCompilerOptions().GetCompilePic());
5240 break;
5241 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5242 DCHECK(GetCompilerOptions().GetCompilePic());
5243 break;
5244 case HLoadClass::LoadKind::kBootImageAddress:
5245 break;
5246 case HLoadClass::LoadKind::kDexCacheAddress:
5247 DCHECK(Runtime::Current()->UseJitCompilation());
5248 fallback_load = false;
5249 break;
5250 case HLoadClass::LoadKind::kDexCachePcRelative:
5251 DCHECK(!Runtime::Current()->UseJitCompilation());
5252 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5253 // with irreducible loops.
5254 break;
5255 case HLoadClass::LoadKind::kDexCacheViaMethod:
5256 fallback_load = false;
5257 break;
5258 }
5259 if (fallback_load) {
5260 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5261 }
5262 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005263}
5264
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005265Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5266 Register temp) {
5267 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5268 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5269 if (!invoke->GetLocations()->Intrinsified()) {
5270 return location.AsRegister<Register>();
5271 }
5272 // For intrinsics we allow any location, so it may be on the stack.
5273 if (!location.IsRegister()) {
5274 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5275 return temp;
5276 }
5277 // For register locations, check if the register was saved. If so, get it from the stack.
5278 // Note: There is a chance that the register was saved but not overwritten, so we could
5279 // save one load. However, since this is just an intrinsic slow path we prefer this
5280 // simple and more robust approach rather that trying to determine if that's the case.
5281 SlowPathCode* slow_path = GetCurrentSlowPath();
5282 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5283 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5284 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5285 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5286 return temp;
5287 }
5288 return location.AsRegister<Register>();
5289}
5290
Vladimir Markodc151b22015-10-15 18:02:30 +01005291HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5292 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005293 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005294 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
5295 // We disable PC-relative load when there is an irreducible loop, as the optimization
5296 // is incompatible with it.
5297 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5298 bool fallback_load = true;
5299 bool fallback_call = true;
5300 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005301 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5302 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005303 fallback_load = has_irreducible_loops;
5304 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005305 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005306 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005307 break;
5308 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005309 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005310 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005311 fallback_call = has_irreducible_loops;
5312 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005313 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005314 // TODO: Implement this type.
5315 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005316 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005317 fallback_call = false;
5318 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005319 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005320 if (fallback_load) {
5321 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5322 dispatch_info.method_load_data = 0;
5323 }
5324 if (fallback_call) {
5325 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
5326 dispatch_info.direct_code_ptr = 0;
5327 }
5328 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005329}
5330
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005331void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5332 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005333 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005334 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5335 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5336 bool isR6 = isa_features_.IsR6();
5337 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
5338 // R6 has PC-relative addressing.
5339 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
5340 (!isR6 &&
5341 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
5342 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
5343 Register base_reg = has_extra_input
5344 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5345 : ZERO;
5346
5347 // For better instruction scheduling we load the direct code pointer before the method pointer.
5348 switch (code_ptr_location) {
5349 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5350 // T9 = invoke->GetDirectCodePtr();
5351 __ LoadConst32(T9, invoke->GetDirectCodePtr());
5352 break;
5353 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5354 // T9 = code address from literal pool with link-time patch.
5355 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
5356 break;
5357 default:
5358 break;
5359 }
5360
5361 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005362 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005363 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005364 uint32_t offset =
5365 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005366 __ LoadFromOffset(kLoadWord,
5367 temp.AsRegister<Register>(),
5368 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005369 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005370 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005371 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005372 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005373 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005374 break;
5375 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5376 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5377 break;
5378 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005379 __ LoadLiteral(temp.AsRegister<Register>(),
5380 base_reg,
5381 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5382 break;
5383 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
5384 HMipsDexCacheArraysBase* base =
5385 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5386 int32_t offset =
5387 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5388 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5389 break;
5390 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005391 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005392 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005393 Register reg = temp.AsRegister<Register>();
5394 Register method_reg;
5395 if (current_method.IsRegister()) {
5396 method_reg = current_method.AsRegister<Register>();
5397 } else {
5398 // TODO: use the appropriate DCHECK() here if possible.
5399 // DCHECK(invoke->GetLocations()->Intrinsified());
5400 DCHECK(!current_method.IsValid());
5401 method_reg = reg;
5402 __ Lw(reg, SP, kCurrentMethodStackOffset);
5403 }
5404
5405 // temp = temp->dex_cache_resolved_methods_;
5406 __ LoadFromOffset(kLoadWord,
5407 reg,
5408 method_reg,
5409 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005410 // temp = temp[index_in_cache];
5411 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5412 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005413 __ LoadFromOffset(kLoadWord,
5414 reg,
5415 reg,
5416 CodeGenerator::GetCachePointerOffset(index_in_cache));
5417 break;
5418 }
5419 }
5420
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005421 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005422 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005423 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005424 break;
5425 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005426 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5427 // T9 prepared above for better instruction scheduling.
5428 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005429 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005430 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005431 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005432 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005433 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01005434 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
5435 LOG(FATAL) << "Unsupported";
5436 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005437 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5438 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005439 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005440 T9,
5441 callee_method.AsRegister<Register>(),
5442 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005443 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005444 // T9()
5445 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005446 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005447 break;
5448 }
5449 DCHECK(!IsLeafMethod());
5450}
5451
5452void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005453 // Explicit clinit checks triggered by static invokes must have been pruned by
5454 // art::PrepareForRegisterAllocation.
5455 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005456
5457 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5458 return;
5459 }
5460
5461 LocationSummary* locations = invoke->GetLocations();
5462 codegen_->GenerateStaticOrDirectCall(invoke,
5463 locations->HasTemps()
5464 ? locations->GetTemp(0)
5465 : Location::NoLocation());
5466 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5467}
5468
Chris Larsen3acee732015-11-18 13:31:08 -08005469void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005470 // Use the calling convention instead of the location of the receiver, as
5471 // intrinsics may have put the receiver in a different register. In the intrinsics
5472 // slow path, the arguments have been moved to the right place, so here we are
5473 // guaranteed that the receiver is the first register of the calling convention.
5474 InvokeDexCallingConvention calling_convention;
5475 Register receiver = calling_convention.GetRegisterAt(0);
5476
Chris Larsen3acee732015-11-18 13:31:08 -08005477 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005478 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5479 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5480 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005481 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005482
5483 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005484 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005485 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005486 // temp = temp->GetMethodAt(method_offset);
5487 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5488 // T9 = temp->GetEntryPoint();
5489 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5490 // T9();
5491 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005492 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005493}
5494
5495void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5496 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5497 return;
5498 }
5499
5500 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005501 DCHECK(!codegen_->IsLeafMethod());
5502 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5503}
5504
5505void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005506 if (cls->NeedsAccessCheck()) {
5507 InvokeRuntimeCallingConvention calling_convention;
5508 CodeGenerator::CreateLoadClassLocationSummary(
5509 cls,
5510 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5511 Location::RegisterLocation(V0),
5512 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
5513 return;
5514 }
5515
5516 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5517 ? LocationSummary::kCallOnSlowPath
5518 : LocationSummary::kNoCall;
5519 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5520 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5521 switch (load_kind) {
5522 // We need an extra register for PC-relative literals on R2.
5523 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5524 case HLoadClass::LoadKind::kBootImageAddress:
5525 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5526 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5527 break;
5528 }
5529 FALLTHROUGH_INTENDED;
5530 // We need an extra register for PC-relative dex cache accesses.
5531 case HLoadClass::LoadKind::kDexCachePcRelative:
5532 case HLoadClass::LoadKind::kReferrersClass:
5533 case HLoadClass::LoadKind::kDexCacheViaMethod:
5534 locations->SetInAt(0, Location::RequiresRegister());
5535 break;
5536 default:
5537 break;
5538 }
5539 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005540}
5541
5542void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
5543 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01005544 if (cls->NeedsAccessCheck()) {
5545 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005546 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005547 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01005548 return;
5549 }
5550
Alexey Frunze06a46c42016-07-19 15:00:40 -07005551 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5552 Location out_loc = locations->Out();
5553 Register out = out_loc.AsRegister<Register>();
5554 Register base_or_current_method_reg;
5555 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5556 switch (load_kind) {
5557 // We need an extra register for PC-relative literals on R2.
5558 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5559 case HLoadClass::LoadKind::kBootImageAddress:
5560 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5561 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5562 break;
5563 // We need an extra register for PC-relative dex cache accesses.
5564 case HLoadClass::LoadKind::kDexCachePcRelative:
5565 case HLoadClass::LoadKind::kReferrersClass:
5566 case HLoadClass::LoadKind::kDexCacheViaMethod:
5567 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5568 break;
5569 default:
5570 base_or_current_method_reg = ZERO;
5571 break;
5572 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005573
Alexey Frunze06a46c42016-07-19 15:00:40 -07005574 bool generate_null_check = false;
5575 switch (load_kind) {
5576 case HLoadClass::LoadKind::kReferrersClass: {
5577 DCHECK(!cls->CanCallRuntime());
5578 DCHECK(!cls->MustGenerateClinitCheck());
5579 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5580 GenerateGcRootFieldLoad(cls,
5581 out_loc,
5582 base_or_current_method_reg,
5583 ArtMethod::DeclaringClassOffset().Int32Value());
5584 break;
5585 }
5586 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5587 DCHECK(!kEmitCompilerReadBarrier);
5588 __ LoadLiteral(out,
5589 base_or_current_method_reg,
5590 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5591 cls->GetTypeIndex()));
5592 break;
5593 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5594 DCHECK(!kEmitCompilerReadBarrier);
5595 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5596 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005597 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005598 break;
5599 }
5600 case HLoadClass::LoadKind::kBootImageAddress: {
5601 DCHECK(!kEmitCompilerReadBarrier);
5602 DCHECK_NE(cls->GetAddress(), 0u);
5603 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5604 __ LoadLiteral(out,
5605 base_or_current_method_reg,
5606 codegen_->DeduplicateBootImageAddressLiteral(address));
5607 break;
5608 }
5609 case HLoadClass::LoadKind::kDexCacheAddress: {
5610 DCHECK_NE(cls->GetAddress(), 0u);
5611 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5612 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5613 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5614 int16_t offset = Low16Bits(address);
5615 uint32_t base_address = address - offset; // This accounts for offset sign extension.
5616 __ Lui(out, High16Bits(base_address));
5617 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5618 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5619 generate_null_check = !cls->IsInDexCache();
5620 break;
5621 }
5622 case HLoadClass::LoadKind::kDexCachePcRelative: {
5623 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
5624 int32_t offset =
5625 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5626 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5627 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
5628 generate_null_check = !cls->IsInDexCache();
5629 break;
5630 }
5631 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5632 // /* GcRoot<mirror::Class>[] */ out =
5633 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5634 __ LoadFromOffset(kLoadWord,
5635 out,
5636 base_or_current_method_reg,
5637 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5638 // /* GcRoot<mirror::Class> */ out = out[type_index]
5639 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5640 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5641 generate_null_check = !cls->IsInDexCache();
5642 }
5643 }
5644
5645 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5646 DCHECK(cls->CanCallRuntime());
5647 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5648 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5649 codegen_->AddSlowPath(slow_path);
5650 if (generate_null_check) {
5651 __ Beqz(out, slow_path->GetEntryLabel());
5652 }
5653 if (cls->MustGenerateClinitCheck()) {
5654 GenerateClassInitializationCheck(slow_path, out);
5655 } else {
5656 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005657 }
5658 }
5659}
5660
5661static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005662 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005663}
5664
5665void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5666 LocationSummary* locations =
5667 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5668 locations->SetOut(Location::RequiresRegister());
5669}
5670
5671void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5672 Register out = load->GetLocations()->Out().AsRegister<Register>();
5673 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5674}
5675
5676void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5677 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5678}
5679
5680void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5681 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5682}
5683
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005684void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005685 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00005686 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5687 ? LocationSummary::kCallOnMainOnly
5688 : LocationSummary::kCallOnSlowPath)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005689 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005690 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005691 HLoadString::LoadKind load_kind = load->GetLoadKind();
5692 switch (load_kind) {
5693 // We need an extra register for PC-relative literals on R2.
5694 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5695 case HLoadString::LoadKind::kBootImageAddress:
5696 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005697 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005698 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5699 break;
5700 }
5701 FALLTHROUGH_INTENDED;
5702 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005703 case HLoadString::LoadKind::kDexCacheViaMethod:
5704 locations->SetInAt(0, Location::RequiresRegister());
5705 break;
5706 default:
5707 break;
5708 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005709 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5710 InvokeRuntimeCallingConvention calling_convention;
5711 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5712 } else {
5713 locations->SetOut(Location::RequiresRegister());
5714 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005715}
5716
5717void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005718 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005719 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005720 Location out_loc = locations->Out();
5721 Register out = out_loc.AsRegister<Register>();
5722 Register base_or_current_method_reg;
5723 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5724 switch (load_kind) {
5725 // We need an extra register for PC-relative literals on R2.
5726 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5727 case HLoadString::LoadKind::kBootImageAddress:
5728 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005729 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005730 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5731 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005732 default:
5733 base_or_current_method_reg = ZERO;
5734 break;
5735 }
5736
5737 switch (load_kind) {
5738 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5739 DCHECK(!kEmitCompilerReadBarrier);
5740 __ LoadLiteral(out,
5741 base_or_current_method_reg,
5742 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5743 load->GetStringIndex()));
5744 return; // No dex cache slow path.
5745 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5746 DCHECK(!kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005747 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005748 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5749 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005750 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005751 return; // No dex cache slow path.
5752 }
5753 case HLoadString::LoadKind::kBootImageAddress: {
5754 DCHECK(!kEmitCompilerReadBarrier);
5755 DCHECK_NE(load->GetAddress(), 0u);
5756 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5757 __ LoadLiteral(out,
5758 base_or_current_method_reg,
5759 codegen_->DeduplicateBootImageAddressLiteral(address));
5760 return; // No dex cache slow path.
5761 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005762 case HLoadString::LoadKind::kBssEntry: {
5763 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5764 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5765 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5766 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
5767 __ LoadFromOffset(kLoadWord, out, out, 0);
5768 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5769 codegen_->AddSlowPath(slow_path);
5770 __ Beqz(out, slow_path->GetEntryLabel());
5771 __ Bind(slow_path->GetExitLabel());
5772 return;
5773 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005774 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005775 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005776 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005777
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005778 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005779 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5780 InvokeRuntimeCallingConvention calling_convention;
5781 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5782 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5783 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005784}
5785
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005786void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5787 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5788 locations->SetOut(Location::ConstantLocation(constant));
5789}
5790
5791void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5792 // Will be generated at use site.
5793}
5794
5795void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5796 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005797 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005798 InvokeRuntimeCallingConvention calling_convention;
5799 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5800}
5801
5802void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5803 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005804 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005805 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5806 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005807 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005808 }
5809 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5810}
5811
5812void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5813 LocationSummary* locations =
5814 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5815 switch (mul->GetResultType()) {
5816 case Primitive::kPrimInt:
5817 case Primitive::kPrimLong:
5818 locations->SetInAt(0, Location::RequiresRegister());
5819 locations->SetInAt(1, Location::RequiresRegister());
5820 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5821 break;
5822
5823 case Primitive::kPrimFloat:
5824 case Primitive::kPrimDouble:
5825 locations->SetInAt(0, Location::RequiresFpuRegister());
5826 locations->SetInAt(1, Location::RequiresFpuRegister());
5827 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5828 break;
5829
5830 default:
5831 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5832 }
5833}
5834
5835void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5836 Primitive::Type type = instruction->GetType();
5837 LocationSummary* locations = instruction->GetLocations();
5838 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5839
5840 switch (type) {
5841 case Primitive::kPrimInt: {
5842 Register dst = locations->Out().AsRegister<Register>();
5843 Register lhs = locations->InAt(0).AsRegister<Register>();
5844 Register rhs = locations->InAt(1).AsRegister<Register>();
5845
5846 if (isR6) {
5847 __ MulR6(dst, lhs, rhs);
5848 } else {
5849 __ MulR2(dst, lhs, rhs);
5850 }
5851 break;
5852 }
5853 case Primitive::kPrimLong: {
5854 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5855 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5856 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5857 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5858 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5859 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5860
5861 // Extra checks to protect caused by the existance of A1_A2.
5862 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5863 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5864 DCHECK_NE(dst_high, lhs_low);
5865 DCHECK_NE(dst_high, rhs_low);
5866
5867 // A_B * C_D
5868 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5869 // dst_lo: [ low(B*D) ]
5870 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5871
5872 if (isR6) {
5873 __ MulR6(TMP, lhs_high, rhs_low);
5874 __ MulR6(dst_high, lhs_low, rhs_high);
5875 __ Addu(dst_high, dst_high, TMP);
5876 __ MuhuR6(TMP, lhs_low, rhs_low);
5877 __ Addu(dst_high, dst_high, TMP);
5878 __ MulR6(dst_low, lhs_low, rhs_low);
5879 } else {
5880 __ MulR2(TMP, lhs_high, rhs_low);
5881 __ MulR2(dst_high, lhs_low, rhs_high);
5882 __ Addu(dst_high, dst_high, TMP);
5883 __ MultuR2(lhs_low, rhs_low);
5884 __ Mfhi(TMP);
5885 __ Addu(dst_high, dst_high, TMP);
5886 __ Mflo(dst_low);
5887 }
5888 break;
5889 }
5890 case Primitive::kPrimFloat:
5891 case Primitive::kPrimDouble: {
5892 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5893 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5894 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5895 if (type == Primitive::kPrimFloat) {
5896 __ MulS(dst, lhs, rhs);
5897 } else {
5898 __ MulD(dst, lhs, rhs);
5899 }
5900 break;
5901 }
5902 default:
5903 LOG(FATAL) << "Unexpected mul type " << type;
5904 }
5905}
5906
5907void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5908 LocationSummary* locations =
5909 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5910 switch (neg->GetResultType()) {
5911 case Primitive::kPrimInt:
5912 case Primitive::kPrimLong:
5913 locations->SetInAt(0, Location::RequiresRegister());
5914 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5915 break;
5916
5917 case Primitive::kPrimFloat:
5918 case Primitive::kPrimDouble:
5919 locations->SetInAt(0, Location::RequiresFpuRegister());
5920 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5921 break;
5922
5923 default:
5924 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5925 }
5926}
5927
5928void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5929 Primitive::Type type = instruction->GetType();
5930 LocationSummary* locations = instruction->GetLocations();
5931
5932 switch (type) {
5933 case Primitive::kPrimInt: {
5934 Register dst = locations->Out().AsRegister<Register>();
5935 Register src = locations->InAt(0).AsRegister<Register>();
5936 __ Subu(dst, ZERO, src);
5937 break;
5938 }
5939 case Primitive::kPrimLong: {
5940 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5941 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5942 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5943 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5944 __ Subu(dst_low, ZERO, src_low);
5945 __ Sltu(TMP, ZERO, dst_low);
5946 __ Subu(dst_high, ZERO, src_high);
5947 __ Subu(dst_high, dst_high, TMP);
5948 break;
5949 }
5950 case Primitive::kPrimFloat:
5951 case Primitive::kPrimDouble: {
5952 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5953 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5954 if (type == Primitive::kPrimFloat) {
5955 __ NegS(dst, src);
5956 } else {
5957 __ NegD(dst, src);
5958 }
5959 break;
5960 }
5961 default:
5962 LOG(FATAL) << "Unexpected neg type " << type;
5963 }
5964}
5965
5966void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5967 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005968 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005969 InvokeRuntimeCallingConvention calling_convention;
5970 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5971 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5972 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5973 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5974}
5975
5976void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5977 InvokeRuntimeCallingConvention calling_convention;
5978 Register current_method_register = calling_convention.GetRegisterAt(2);
5979 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5980 // Move an uint16_t value to a register.
5981 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005982 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005983 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5984 void*, uint32_t, int32_t, ArtMethod*>();
5985}
5986
5987void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5988 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005989 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005990 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005991 if (instruction->IsStringAlloc()) {
5992 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5993 } else {
5994 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5995 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5996 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005997 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5998}
5999
6000void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00006001 if (instruction->IsStringAlloc()) {
6002 // String is allocated through StringFactory. Call NewEmptyString entry point.
6003 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07006004 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006005 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6006 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
6007 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006008 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00006009 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6010 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006011 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00006012 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
6013 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006014}
6015
6016void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
6017 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6018 locations->SetInAt(0, Location::RequiresRegister());
6019 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6020}
6021
6022void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
6023 Primitive::Type type = instruction->GetType();
6024 LocationSummary* locations = instruction->GetLocations();
6025
6026 switch (type) {
6027 case Primitive::kPrimInt: {
6028 Register dst = locations->Out().AsRegister<Register>();
6029 Register src = locations->InAt(0).AsRegister<Register>();
6030 __ Nor(dst, src, ZERO);
6031 break;
6032 }
6033
6034 case Primitive::kPrimLong: {
6035 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6036 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6037 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6038 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6039 __ Nor(dst_high, src_high, ZERO);
6040 __ Nor(dst_low, src_low, ZERO);
6041 break;
6042 }
6043
6044 default:
6045 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6046 }
6047}
6048
6049void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6050 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6051 locations->SetInAt(0, Location::RequiresRegister());
6052 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6053}
6054
6055void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6056 LocationSummary* locations = instruction->GetLocations();
6057 __ Xori(locations->Out().AsRegister<Register>(),
6058 locations->InAt(0).AsRegister<Register>(),
6059 1);
6060}
6061
6062void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006063 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6064 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006065}
6066
Calin Juravle2ae48182016-03-16 14:05:09 +00006067void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
6068 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006069 return;
6070 }
6071 Location obj = instruction->GetLocations()->InAt(0);
6072
6073 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006074 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006075}
6076
Calin Juravle2ae48182016-03-16 14:05:09 +00006077void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006078 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006079 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006080
6081 Location obj = instruction->GetLocations()->InAt(0);
6082
6083 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6084}
6085
6086void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006087 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006088}
6089
6090void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6091 HandleBinaryOp(instruction);
6092}
6093
6094void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6095 HandleBinaryOp(instruction);
6096}
6097
6098void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6099 LOG(FATAL) << "Unreachable";
6100}
6101
6102void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6103 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6104}
6105
6106void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6107 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6108 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6109 if (location.IsStackSlot()) {
6110 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6111 } else if (location.IsDoubleStackSlot()) {
6112 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6113 }
6114 locations->SetOut(location);
6115}
6116
6117void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6118 ATTRIBUTE_UNUSED) {
6119 // Nothing to do, the parameter is already at its location.
6120}
6121
6122void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6123 LocationSummary* locations =
6124 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6125 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6126}
6127
6128void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6129 ATTRIBUTE_UNUSED) {
6130 // Nothing to do, the method is already at its location.
6131}
6132
6133void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6134 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006135 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006136 locations->SetInAt(i, Location::Any());
6137 }
6138 locations->SetOut(Location::Any());
6139}
6140
6141void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6142 LOG(FATAL) << "Unreachable";
6143}
6144
6145void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6146 Primitive::Type type = rem->GetResultType();
6147 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006148 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006149 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6150
6151 switch (type) {
6152 case Primitive::kPrimInt:
6153 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006154 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006155 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6156 break;
6157
6158 case Primitive::kPrimLong: {
6159 InvokeRuntimeCallingConvention calling_convention;
6160 locations->SetInAt(0, Location::RegisterPairLocation(
6161 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6162 locations->SetInAt(1, Location::RegisterPairLocation(
6163 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6164 locations->SetOut(calling_convention.GetReturnLocation(type));
6165 break;
6166 }
6167
6168 case Primitive::kPrimFloat:
6169 case Primitive::kPrimDouble: {
6170 InvokeRuntimeCallingConvention calling_convention;
6171 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6172 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6173 locations->SetOut(calling_convention.GetReturnLocation(type));
6174 break;
6175 }
6176
6177 default:
6178 LOG(FATAL) << "Unexpected rem type " << type;
6179 }
6180}
6181
6182void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6183 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006184
6185 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006186 case Primitive::kPrimInt:
6187 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006188 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006189 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006190 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006191 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6192 break;
6193 }
6194 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006195 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006196 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006197 break;
6198 }
6199 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006200 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006201 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006202 break;
6203 }
6204 default:
6205 LOG(FATAL) << "Unexpected rem type " << type;
6206 }
6207}
6208
6209void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6210 memory_barrier->SetLocations(nullptr);
6211}
6212
6213void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6214 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6215}
6216
6217void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6218 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6219 Primitive::Type return_type = ret->InputAt(0)->GetType();
6220 locations->SetInAt(0, MipsReturnLocation(return_type));
6221}
6222
6223void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6224 codegen_->GenerateFrameExit();
6225}
6226
6227void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6228 ret->SetLocations(nullptr);
6229}
6230
6231void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6232 codegen_->GenerateFrameExit();
6233}
6234
Alexey Frunze92d90602015-12-18 18:16:36 -08006235void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6236 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006237}
6238
Alexey Frunze92d90602015-12-18 18:16:36 -08006239void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6240 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006241}
6242
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006243void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6244 HandleShift(shl);
6245}
6246
6247void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6248 HandleShift(shl);
6249}
6250
6251void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6252 HandleShift(shr);
6253}
6254
6255void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6256 HandleShift(shr);
6257}
6258
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006259void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6260 HandleBinaryOp(instruction);
6261}
6262
6263void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6264 HandleBinaryOp(instruction);
6265}
6266
6267void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6268 HandleFieldGet(instruction, instruction->GetFieldInfo());
6269}
6270
6271void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6272 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6273}
6274
6275void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6276 HandleFieldSet(instruction, instruction->GetFieldInfo());
6277}
6278
6279void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6280 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6281}
6282
6283void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6284 HUnresolvedInstanceFieldGet* instruction) {
6285 FieldAccessCallingConventionMIPS calling_convention;
6286 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6287 instruction->GetFieldType(),
6288 calling_convention);
6289}
6290
6291void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6292 HUnresolvedInstanceFieldGet* instruction) {
6293 FieldAccessCallingConventionMIPS calling_convention;
6294 codegen_->GenerateUnresolvedFieldAccess(instruction,
6295 instruction->GetFieldType(),
6296 instruction->GetFieldIndex(),
6297 instruction->GetDexPc(),
6298 calling_convention);
6299}
6300
6301void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6302 HUnresolvedInstanceFieldSet* instruction) {
6303 FieldAccessCallingConventionMIPS calling_convention;
6304 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6305 instruction->GetFieldType(),
6306 calling_convention);
6307}
6308
6309void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6310 HUnresolvedInstanceFieldSet* instruction) {
6311 FieldAccessCallingConventionMIPS calling_convention;
6312 codegen_->GenerateUnresolvedFieldAccess(instruction,
6313 instruction->GetFieldType(),
6314 instruction->GetFieldIndex(),
6315 instruction->GetDexPc(),
6316 calling_convention);
6317}
6318
6319void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6320 HUnresolvedStaticFieldGet* instruction) {
6321 FieldAccessCallingConventionMIPS calling_convention;
6322 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6323 instruction->GetFieldType(),
6324 calling_convention);
6325}
6326
6327void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6328 HUnresolvedStaticFieldGet* instruction) {
6329 FieldAccessCallingConventionMIPS calling_convention;
6330 codegen_->GenerateUnresolvedFieldAccess(instruction,
6331 instruction->GetFieldType(),
6332 instruction->GetFieldIndex(),
6333 instruction->GetDexPc(),
6334 calling_convention);
6335}
6336
6337void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6338 HUnresolvedStaticFieldSet* instruction) {
6339 FieldAccessCallingConventionMIPS calling_convention;
6340 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6341 instruction->GetFieldType(),
6342 calling_convention);
6343}
6344
6345void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6346 HUnresolvedStaticFieldSet* instruction) {
6347 FieldAccessCallingConventionMIPS calling_convention;
6348 codegen_->GenerateUnresolvedFieldAccess(instruction,
6349 instruction->GetFieldType(),
6350 instruction->GetFieldIndex(),
6351 instruction->GetDexPc(),
6352 calling_convention);
6353}
6354
6355void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006356 LocationSummary* locations =
6357 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006358 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006359}
6360
6361void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6362 HBasicBlock* block = instruction->GetBlock();
6363 if (block->GetLoopInformation() != nullptr) {
6364 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6365 // The back edge will generate the suspend check.
6366 return;
6367 }
6368 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6369 // The goto will generate the suspend check.
6370 return;
6371 }
6372 GenerateSuspendCheck(instruction, nullptr);
6373}
6374
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006375void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6376 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006377 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006378 InvokeRuntimeCallingConvention calling_convention;
6379 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6380}
6381
6382void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006383 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006384 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6385}
6386
6387void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6388 Primitive::Type input_type = conversion->GetInputType();
6389 Primitive::Type result_type = conversion->GetResultType();
6390 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006391 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006392
6393 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6394 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6395 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6396 }
6397
6398 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006399 if (!isR6 &&
6400 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6401 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006402 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006403 }
6404
6405 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6406
6407 if (call_kind == LocationSummary::kNoCall) {
6408 if (Primitive::IsFloatingPointType(input_type)) {
6409 locations->SetInAt(0, Location::RequiresFpuRegister());
6410 } else {
6411 locations->SetInAt(0, Location::RequiresRegister());
6412 }
6413
6414 if (Primitive::IsFloatingPointType(result_type)) {
6415 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6416 } else {
6417 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6418 }
6419 } else {
6420 InvokeRuntimeCallingConvention calling_convention;
6421
6422 if (Primitive::IsFloatingPointType(input_type)) {
6423 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6424 } else {
6425 DCHECK_EQ(input_type, Primitive::kPrimLong);
6426 locations->SetInAt(0, Location::RegisterPairLocation(
6427 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6428 }
6429
6430 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6431 }
6432}
6433
6434void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6435 LocationSummary* locations = conversion->GetLocations();
6436 Primitive::Type result_type = conversion->GetResultType();
6437 Primitive::Type input_type = conversion->GetInputType();
6438 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006439 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006440
6441 DCHECK_NE(input_type, result_type);
6442
6443 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6444 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6445 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6446 Register src = locations->InAt(0).AsRegister<Register>();
6447
Alexey Frunzea871ef12016-06-27 15:20:11 -07006448 if (dst_low != src) {
6449 __ Move(dst_low, src);
6450 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006451 __ Sra(dst_high, src, 31);
6452 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6453 Register dst = locations->Out().AsRegister<Register>();
6454 Register src = (input_type == Primitive::kPrimLong)
6455 ? locations->InAt(0).AsRegisterPairLow<Register>()
6456 : locations->InAt(0).AsRegister<Register>();
6457
6458 switch (result_type) {
6459 case Primitive::kPrimChar:
6460 __ Andi(dst, src, 0xFFFF);
6461 break;
6462 case Primitive::kPrimByte:
6463 if (has_sign_extension) {
6464 __ Seb(dst, src);
6465 } else {
6466 __ Sll(dst, src, 24);
6467 __ Sra(dst, dst, 24);
6468 }
6469 break;
6470 case Primitive::kPrimShort:
6471 if (has_sign_extension) {
6472 __ Seh(dst, src);
6473 } else {
6474 __ Sll(dst, src, 16);
6475 __ Sra(dst, dst, 16);
6476 }
6477 break;
6478 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006479 if (dst != src) {
6480 __ Move(dst, src);
6481 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006482 break;
6483
6484 default:
6485 LOG(FATAL) << "Unexpected type conversion from " << input_type
6486 << " to " << result_type;
6487 }
6488 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006489 if (input_type == Primitive::kPrimLong) {
6490 if (isR6) {
6491 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6492 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6493 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6494 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6495 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6496 __ Mtc1(src_low, FTMP);
6497 __ Mthc1(src_high, FTMP);
6498 if (result_type == Primitive::kPrimFloat) {
6499 __ Cvtsl(dst, FTMP);
6500 } else {
6501 __ Cvtdl(dst, FTMP);
6502 }
6503 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006504 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6505 : kQuickL2d;
6506 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006507 if (result_type == Primitive::kPrimFloat) {
6508 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6509 } else {
6510 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6511 }
6512 }
6513 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006514 Register src = locations->InAt(0).AsRegister<Register>();
6515 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6516 __ Mtc1(src, FTMP);
6517 if (result_type == Primitive::kPrimFloat) {
6518 __ Cvtsw(dst, FTMP);
6519 } else {
6520 __ Cvtdw(dst, FTMP);
6521 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006522 }
6523 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6524 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006525 if (result_type == Primitive::kPrimLong) {
6526 if (isR6) {
6527 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6528 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6529 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6530 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6531 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6532 MipsLabel truncate;
6533 MipsLabel done;
6534
6535 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6536 // value when the input is either a NaN or is outside of the range of the output type
6537 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6538 // the same result.
6539 //
6540 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6541 // value of the output type if the input is outside of the range after the truncation or
6542 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6543 // results. This matches the desired float/double-to-int/long conversion exactly.
6544 //
6545 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6546 //
6547 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6548 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6549 // even though it must be NAN2008=1 on R6.
6550 //
6551 // The code takes care of the different behaviors by first comparing the input to the
6552 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6553 // If the input is greater than or equal to the minimum, it procedes to the truncate
6554 // instruction, which will handle such an input the same way irrespective of NAN2008.
6555 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6556 // in order to return either zero or the minimum value.
6557 //
6558 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6559 // truncate instruction for MIPS64R6.
6560 if (input_type == Primitive::kPrimFloat) {
6561 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6562 __ LoadConst32(TMP, min_val);
6563 __ Mtc1(TMP, FTMP);
6564 __ CmpLeS(FTMP, FTMP, src);
6565 } else {
6566 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6567 __ LoadConst32(TMP, High32Bits(min_val));
6568 __ Mtc1(ZERO, FTMP);
6569 __ Mthc1(TMP, FTMP);
6570 __ CmpLeD(FTMP, FTMP, src);
6571 }
6572
6573 __ Bc1nez(FTMP, &truncate);
6574
6575 if (input_type == Primitive::kPrimFloat) {
6576 __ CmpEqS(FTMP, src, src);
6577 } else {
6578 __ CmpEqD(FTMP, src, src);
6579 }
6580 __ Move(dst_low, ZERO);
6581 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6582 __ Mfc1(TMP, FTMP);
6583 __ And(dst_high, dst_high, TMP);
6584
6585 __ B(&done);
6586
6587 __ Bind(&truncate);
6588
6589 if (input_type == Primitive::kPrimFloat) {
6590 __ TruncLS(FTMP, src);
6591 } else {
6592 __ TruncLD(FTMP, src);
6593 }
6594 __ Mfc1(dst_low, FTMP);
6595 __ Mfhc1(dst_high, FTMP);
6596
6597 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006598 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006599 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6600 : kQuickD2l;
6601 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006602 if (input_type == Primitive::kPrimFloat) {
6603 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6604 } else {
6605 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6606 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006607 }
6608 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006609 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6610 Register dst = locations->Out().AsRegister<Register>();
6611 MipsLabel truncate;
6612 MipsLabel done;
6613
6614 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6615 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6616 // even though it must be NAN2008=1 on R6.
6617 //
6618 // For details see the large comment above for the truncation of float/double to long on R6.
6619 //
6620 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6621 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006622 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006623 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6624 __ LoadConst32(TMP, min_val);
6625 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006626 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006627 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6628 __ LoadConst32(TMP, High32Bits(min_val));
6629 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006630 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006631 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006632
6633 if (isR6) {
6634 if (input_type == Primitive::kPrimFloat) {
6635 __ CmpLeS(FTMP, FTMP, src);
6636 } else {
6637 __ CmpLeD(FTMP, FTMP, src);
6638 }
6639 __ Bc1nez(FTMP, &truncate);
6640
6641 if (input_type == Primitive::kPrimFloat) {
6642 __ CmpEqS(FTMP, src, src);
6643 } else {
6644 __ CmpEqD(FTMP, src, src);
6645 }
6646 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6647 __ Mfc1(TMP, FTMP);
6648 __ And(dst, dst, TMP);
6649 } else {
6650 if (input_type == Primitive::kPrimFloat) {
6651 __ ColeS(0, FTMP, src);
6652 } else {
6653 __ ColeD(0, FTMP, src);
6654 }
6655 __ Bc1t(0, &truncate);
6656
6657 if (input_type == Primitive::kPrimFloat) {
6658 __ CeqS(0, src, src);
6659 } else {
6660 __ CeqD(0, src, src);
6661 }
6662 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6663 __ Movf(dst, ZERO, 0);
6664 }
6665
6666 __ B(&done);
6667
6668 __ Bind(&truncate);
6669
6670 if (input_type == Primitive::kPrimFloat) {
6671 __ TruncWS(FTMP, src);
6672 } else {
6673 __ TruncWD(FTMP, src);
6674 }
6675 __ Mfc1(dst, FTMP);
6676
6677 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006678 }
6679 } else if (Primitive::IsFloatingPointType(result_type) &&
6680 Primitive::IsFloatingPointType(input_type)) {
6681 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6682 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6683 if (result_type == Primitive::kPrimFloat) {
6684 __ Cvtsd(dst, src);
6685 } else {
6686 __ Cvtds(dst, src);
6687 }
6688 } else {
6689 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6690 << " to " << result_type;
6691 }
6692}
6693
6694void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6695 HandleShift(ushr);
6696}
6697
6698void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6699 HandleShift(ushr);
6700}
6701
6702void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6703 HandleBinaryOp(instruction);
6704}
6705
6706void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6707 HandleBinaryOp(instruction);
6708}
6709
6710void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6711 // Nothing to do, this should be removed during prepare for register allocator.
6712 LOG(FATAL) << "Unreachable";
6713}
6714
6715void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6716 // Nothing to do, this should be removed during prepare for register allocator.
6717 LOG(FATAL) << "Unreachable";
6718}
6719
6720void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006721 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006722}
6723
6724void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006725 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006726}
6727
6728void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006729 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006730}
6731
6732void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006733 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006734}
6735
6736void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006737 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006738}
6739
6740void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006741 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006742}
6743
6744void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006745 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006746}
6747
6748void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006749 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006750}
6751
6752void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006753 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006754}
6755
6756void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006757 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006758}
6759
6760void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006761 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006762}
6763
6764void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006765 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006766}
6767
6768void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006769 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006770}
6771
6772void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006773 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006774}
6775
6776void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006777 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006778}
6779
6780void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006781 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006782}
6783
6784void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006785 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006786}
6787
6788void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006789 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006790}
6791
6792void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006793 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006794}
6795
6796void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006797 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006798}
6799
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006800void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6801 LocationSummary* locations =
6802 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6803 locations->SetInAt(0, Location::RequiresRegister());
6804}
6805
Alexey Frunze96b66822016-09-10 02:32:44 -07006806void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6807 int32_t lower_bound,
6808 uint32_t num_entries,
6809 HBasicBlock* switch_block,
6810 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006811 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006812 Register temp_reg = TMP;
6813 __ Addiu32(temp_reg, value_reg, -lower_bound);
6814 // Jump to default if index is negative
6815 // Note: We don't check the case that index is positive while value < lower_bound, because in
6816 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6817 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6818
Alexey Frunze96b66822016-09-10 02:32:44 -07006819 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006820 // Jump to successors[0] if value == lower_bound.
6821 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6822 int32_t last_index = 0;
6823 for (; num_entries - last_index > 2; last_index += 2) {
6824 __ Addiu(temp_reg, temp_reg, -2);
6825 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6826 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6827 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6828 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6829 }
6830 if (num_entries - last_index == 2) {
6831 // The last missing case_value.
6832 __ Addiu(temp_reg, temp_reg, -1);
6833 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006834 }
6835
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006836 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006837 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006838 __ B(codegen_->GetLabelOf(default_block));
6839 }
6840}
6841
Alexey Frunze96b66822016-09-10 02:32:44 -07006842void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6843 Register constant_area,
6844 int32_t lower_bound,
6845 uint32_t num_entries,
6846 HBasicBlock* switch_block,
6847 HBasicBlock* default_block) {
6848 // Create a jump table.
6849 std::vector<MipsLabel*> labels(num_entries);
6850 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6851 for (uint32_t i = 0; i < num_entries; i++) {
6852 labels[i] = codegen_->GetLabelOf(successors[i]);
6853 }
6854 JumpTable* table = __ CreateJumpTable(std::move(labels));
6855
6856 // Is the value in range?
6857 __ Addiu32(TMP, value_reg, -lower_bound);
6858 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6859 __ Sltiu(AT, TMP, num_entries);
6860 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6861 } else {
6862 __ LoadConst32(AT, num_entries);
6863 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6864 }
6865
6866 // We are in the range of the table.
6867 // Load the target address from the jump table, indexing by the value.
6868 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6869 __ Sll(TMP, TMP, 2);
6870 __ Addu(TMP, TMP, AT);
6871 __ Lw(TMP, TMP, 0);
6872 // Compute the absolute target address by adding the table start address
6873 // (the table contains offsets to targets relative to its start).
6874 __ Addu(TMP, TMP, AT);
6875 // And jump.
6876 __ Jr(TMP);
6877 __ NopIfNoReordering();
6878}
6879
6880void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6881 int32_t lower_bound = switch_instr->GetStartValue();
6882 uint32_t num_entries = switch_instr->GetNumEntries();
6883 LocationSummary* locations = switch_instr->GetLocations();
6884 Register value_reg = locations->InAt(0).AsRegister<Register>();
6885 HBasicBlock* switch_block = switch_instr->GetBlock();
6886 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6887
6888 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6889 num_entries > kPackedSwitchJumpTableThreshold) {
6890 // R6 uses PC-relative addressing to access the jump table.
6891 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6892 // the jump table and it is implemented by changing HPackedSwitch to
6893 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6894 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6895 GenTableBasedPackedSwitch(value_reg,
6896 ZERO,
6897 lower_bound,
6898 num_entries,
6899 switch_block,
6900 default_block);
6901 } else {
6902 GenPackedSwitchWithCompares(value_reg,
6903 lower_bound,
6904 num_entries,
6905 switch_block,
6906 default_block);
6907 }
6908}
6909
6910void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6911 LocationSummary* locations =
6912 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6913 locations->SetInAt(0, Location::RequiresRegister());
6914 // Constant area pointer (HMipsComputeBaseMethodAddress).
6915 locations->SetInAt(1, Location::RequiresRegister());
6916}
6917
6918void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6919 int32_t lower_bound = switch_instr->GetStartValue();
6920 uint32_t num_entries = switch_instr->GetNumEntries();
6921 LocationSummary* locations = switch_instr->GetLocations();
6922 Register value_reg = locations->InAt(0).AsRegister<Register>();
6923 Register constant_area = locations->InAt(1).AsRegister<Register>();
6924 HBasicBlock* switch_block = switch_instr->GetBlock();
6925 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6926
6927 // This is an R2-only path. HPackedSwitch has been changed to
6928 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6929 // required to address the jump table relative to PC.
6930 GenTableBasedPackedSwitch(value_reg,
6931 constant_area,
6932 lower_bound,
6933 num_entries,
6934 switch_block,
6935 default_block);
6936}
6937
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006938void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6939 HMipsComputeBaseMethodAddress* insn) {
6940 LocationSummary* locations =
6941 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6942 locations->SetOut(Location::RequiresRegister());
6943}
6944
6945void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6946 HMipsComputeBaseMethodAddress* insn) {
6947 LocationSummary* locations = insn->GetLocations();
6948 Register reg = locations->Out().AsRegister<Register>();
6949
6950 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6951
6952 // Generate a dummy PC-relative call to obtain PC.
6953 __ Nal();
6954 // Grab the return address off RA.
6955 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006956 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006957
6958 // Remember this offset (the obtained PC value) for later use with constant area.
6959 __ BindPcRelBaseLabel();
6960}
6961
6962void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6963 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6964 locations->SetOut(Location::RequiresRegister());
6965}
6966
6967void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6968 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6969 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6970 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006971 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6972 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006973}
6974
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006975void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6976 // The trampoline uses the same calling convention as dex calling conventions,
6977 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6978 // the method_idx.
6979 HandleInvoke(invoke);
6980}
6981
6982void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6983 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6984}
6985
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006986void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6987 LocationSummary* locations =
6988 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6989 locations->SetInAt(0, Location::RequiresRegister());
6990 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006991}
6992
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006993void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6994 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006995 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006996 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006997 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006998 __ LoadFromOffset(kLoadWord,
6999 locations->Out().AsRegister<Register>(),
7000 locations->InAt(0).AsRegister<Register>(),
7001 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007002 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007003 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007004 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007005 __ LoadFromOffset(kLoadWord,
7006 locations->Out().AsRegister<Register>(),
7007 locations->InAt(0).AsRegister<Register>(),
7008 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007009 __ LoadFromOffset(kLoadWord,
7010 locations->Out().AsRegister<Register>(),
7011 locations->Out().AsRegister<Register>(),
7012 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007013 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007014}
7015
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007016#undef __
7017#undef QUICK_ENTRY_POINT
7018
7019} // namespace mips
7020} // namespace art