blob: 56def3194cbe5eff4c94f3a78d610f6b7311114f [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>();
571 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
572 : loc2.AsRegister<Register>();
573 __ Move(TMP, r2);
574 __ Mfc1(r2, f1);
575 __ Mtc1(TMP, f1);
576 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
577 // Swap 2 GPR register pairs.
578 Register r1 = loc1.AsRegisterPairLow<Register>();
579 Register r2 = loc2.AsRegisterPairLow<Register>();
580 __ Move(TMP, r2);
581 __ Move(r2, r1);
582 __ Move(r1, TMP);
583 r1 = loc1.AsRegisterPairHigh<Register>();
584 r2 = loc2.AsRegisterPairHigh<Register>();
585 __ Move(TMP, r2);
586 __ Move(r2, r1);
587 __ Move(r1, TMP);
588 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
589 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
590 // Swap FPR and GPR register pair.
591 DCHECK_EQ(type, Primitive::kPrimDouble);
592 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
593 : loc2.AsFpuRegister<FRegister>();
594 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
595 : loc2.AsRegisterPairLow<Register>();
596 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
597 : loc2.AsRegisterPairHigh<Register>();
598 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
599 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
600 // unpredictable and the following mfch1 will fail.
601 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800602 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200603 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800604 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200605 __ Move(r2_l, TMP);
606 __ Move(r2_h, AT);
607 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
608 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
609 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
610 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000611 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
612 (loc1.IsStackSlot() && loc2.IsRegister())) {
613 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
614 : loc2.AsRegister<Register>();
615 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
616 : loc2.GetStackIndex();
617 __ Move(TMP, reg);
618 __ LoadFromOffset(kLoadWord, reg, SP, offset);
619 __ StoreToOffset(kStoreWord, TMP, SP, offset);
620 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
621 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
622 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
623 : loc2.AsRegisterPairLow<Register>();
624 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
625 : loc2.AsRegisterPairHigh<Register>();
626 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
627 : loc2.GetStackIndex();
628 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
629 : loc2.GetHighStackIndex(kMipsWordSize);
630 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000631 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000632 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000633 __ Move(TMP, reg_h);
634 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
635 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200636 } else {
637 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
638 }
639}
640
641void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
642 __ Pop(static_cast<Register>(reg));
643}
644
645void ParallelMoveResolverMIPS::SpillScratch(int reg) {
646 __ Push(static_cast<Register>(reg));
647}
648
649void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
650 // Allocate a scratch register other than TMP, if available.
651 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
652 // automatically unspilled when the scratch scope object is destroyed).
653 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
654 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
655 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
656 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
657 __ LoadFromOffset(kLoadWord,
658 Register(ensure_scratch.GetRegister()),
659 SP,
660 index1 + stack_offset);
661 __ LoadFromOffset(kLoadWord,
662 TMP,
663 SP,
664 index2 + stack_offset);
665 __ StoreToOffset(kStoreWord,
666 Register(ensure_scratch.GetRegister()),
667 SP,
668 index2 + stack_offset);
669 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
670 }
671}
672
Alexey Frunze73296a72016-06-03 22:51:46 -0700673void CodeGeneratorMIPS::ComputeSpillMask() {
674 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
675 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
676 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
677 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
678 // registers, include the ZERO register to force alignment of FPU callee-saved registers
679 // within the stack frame.
680 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
681 core_spill_mask_ |= (1 << ZERO);
682 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700683}
684
685bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700686 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700687 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
688 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
689 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700690 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
691 // saved in an unused temporary register) and saving of RA and the current method pointer
692 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700693 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700694}
695
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200696static dwarf::Reg DWARFReg(Register reg) {
697 return dwarf::Reg::MipsCore(static_cast<int>(reg));
698}
699
700// TODO: mapping of floating-point registers to DWARF.
701
702void CodeGeneratorMIPS::GenerateFrameEntry() {
703 __ Bind(&frame_entry_label_);
704
705 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
706
707 if (do_overflow_check) {
708 __ LoadFromOffset(kLoadWord,
709 ZERO,
710 SP,
711 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
712 RecordPcInfo(nullptr, 0);
713 }
714
715 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700716 CHECK_EQ(fpu_spill_mask_, 0u);
717 CHECK_EQ(core_spill_mask_, 1u << RA);
718 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200719 return;
720 }
721
722 // Make sure the frame size isn't unreasonably large.
723 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
724 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
725 }
726
727 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200728
Alexey Frunze73296a72016-06-03 22:51:46 -0700729 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200730 __ IncreaseFrameSize(ofs);
731
Alexey Frunze73296a72016-06-03 22:51:46 -0700732 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
733 Register reg = static_cast<Register>(MostSignificantBit(mask));
734 mask ^= 1u << reg;
735 ofs -= kMipsWordSize;
736 // The ZERO register is only included for alignment.
737 if (reg != ZERO) {
738 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200739 __ cfi().RelOffset(DWARFReg(reg), ofs);
740 }
741 }
742
Alexey Frunze73296a72016-06-03 22:51:46 -0700743 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
744 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
745 mask ^= 1u << reg;
746 ofs -= kMipsDoublewordSize;
747 __ StoreDToOffset(reg, SP, ofs);
748 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200749 }
750
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100751 // Save the current method if we need it. Note that we do not
752 // do this in HCurrentMethod, as the instruction might have been removed
753 // in the SSA graph.
754 if (RequiresCurrentMethod()) {
755 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
756 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200757}
758
759void CodeGeneratorMIPS::GenerateFrameExit() {
760 __ cfi().RememberState();
761
762 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200763 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200764
Alexey Frunze73296a72016-06-03 22:51:46 -0700765 // For better instruction scheduling restore RA before other registers.
766 uint32_t ofs = GetFrameSize();
767 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
768 Register reg = static_cast<Register>(MostSignificantBit(mask));
769 mask ^= 1u << reg;
770 ofs -= kMipsWordSize;
771 // The ZERO register is only included for alignment.
772 if (reg != ZERO) {
773 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200774 __ cfi().Restore(DWARFReg(reg));
775 }
776 }
777
Alexey Frunze73296a72016-06-03 22:51:46 -0700778 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
779 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
780 mask ^= 1u << reg;
781 ofs -= kMipsDoublewordSize;
782 __ LoadDFromOffset(reg, SP, ofs);
783 // TODO: __ cfi().Restore(DWARFReg(reg));
784 }
785
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700786 size_t frame_size = GetFrameSize();
787 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
788 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
789 bool reordering = __ SetReorder(false);
790 if (exchange) {
791 __ Jr(RA);
792 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
793 } else {
794 __ DecreaseFrameSize(frame_size);
795 __ Jr(RA);
796 __ Nop(); // In delay slot.
797 }
798 __ SetReorder(reordering);
799 } else {
800 __ Jr(RA);
801 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200802 }
803
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200804 __ cfi().RestoreState();
805 __ cfi().DefCFAOffset(GetFrameSize());
806}
807
808void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
809 __ Bind(GetLabelOf(block));
810}
811
812void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
813 if (src.Equals(dst)) {
814 return;
815 }
816
817 if (src.IsConstant()) {
818 MoveConstant(dst, src.GetConstant());
819 } else {
820 if (Primitive::Is64BitType(dst_type)) {
821 Move64(dst, src);
822 } else {
823 Move32(dst, src);
824 }
825 }
826}
827
828void CodeGeneratorMIPS::Move32(Location destination, Location source) {
829 if (source.Equals(destination)) {
830 return;
831 }
832
833 if (destination.IsRegister()) {
834 if (source.IsRegister()) {
835 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
836 } else if (source.IsFpuRegister()) {
837 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
838 } else {
839 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
840 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
841 }
842 } else if (destination.IsFpuRegister()) {
843 if (source.IsRegister()) {
844 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
845 } else if (source.IsFpuRegister()) {
846 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
847 } else {
848 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
849 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
850 }
851 } else {
852 DCHECK(destination.IsStackSlot()) << destination;
853 if (source.IsRegister()) {
854 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
855 } else if (source.IsFpuRegister()) {
856 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
857 } else {
858 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
859 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
860 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
861 }
862 }
863}
864
865void CodeGeneratorMIPS::Move64(Location destination, Location source) {
866 if (source.Equals(destination)) {
867 return;
868 }
869
870 if (destination.IsRegisterPair()) {
871 if (source.IsRegisterPair()) {
872 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
873 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
874 } else if (source.IsFpuRegister()) {
875 Register dst_high = destination.AsRegisterPairHigh<Register>();
876 Register dst_low = destination.AsRegisterPairLow<Register>();
877 FRegister src = source.AsFpuRegister<FRegister>();
878 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800879 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200880 } else {
881 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
882 int32_t off = source.GetStackIndex();
883 Register r = destination.AsRegisterPairLow<Register>();
884 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
885 }
886 } else if (destination.IsFpuRegister()) {
887 if (source.IsRegisterPair()) {
888 FRegister dst = destination.AsFpuRegister<FRegister>();
889 Register src_high = source.AsRegisterPairHigh<Register>();
890 Register src_low = source.AsRegisterPairLow<Register>();
891 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800892 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200893 } else if (source.IsFpuRegister()) {
894 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
895 } else {
896 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
897 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
898 }
899 } else {
900 DCHECK(destination.IsDoubleStackSlot()) << destination;
901 int32_t off = destination.GetStackIndex();
902 if (source.IsRegisterPair()) {
903 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
904 } else if (source.IsFpuRegister()) {
905 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
906 } else {
907 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
908 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
909 __ StoreToOffset(kStoreWord, TMP, SP, off);
910 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
911 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
912 }
913 }
914}
915
916void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
917 if (c->IsIntConstant() || c->IsNullConstant()) {
918 // Move 32 bit constant.
919 int32_t value = GetInt32ValueOf(c);
920 if (destination.IsRegister()) {
921 Register dst = destination.AsRegister<Register>();
922 __ LoadConst32(dst, value);
923 } else {
924 DCHECK(destination.IsStackSlot())
925 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700926 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200927 }
928 } else if (c->IsLongConstant()) {
929 // Move 64 bit constant.
930 int64_t value = GetInt64ValueOf(c);
931 if (destination.IsRegisterPair()) {
932 Register r_h = destination.AsRegisterPairHigh<Register>();
933 Register r_l = destination.AsRegisterPairLow<Register>();
934 __ LoadConst64(r_h, r_l, value);
935 } else {
936 DCHECK(destination.IsDoubleStackSlot())
937 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700938 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200939 }
940 } else if (c->IsFloatConstant()) {
941 // Move 32 bit float constant.
942 int32_t value = GetInt32ValueOf(c);
943 if (destination.IsFpuRegister()) {
944 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
945 } else {
946 DCHECK(destination.IsStackSlot())
947 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700948 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200949 }
950 } else {
951 // Move 64 bit double constant.
952 DCHECK(c->IsDoubleConstant()) << c->DebugName();
953 int64_t value = GetInt64ValueOf(c);
954 if (destination.IsFpuRegister()) {
955 FRegister fd = destination.AsFpuRegister<FRegister>();
956 __ LoadDConst64(fd, value, TMP);
957 } else {
958 DCHECK(destination.IsDoubleStackSlot())
959 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700960 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200961 }
962 }
963}
964
965void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
966 DCHECK(destination.IsRegister());
967 Register dst = destination.AsRegister<Register>();
968 __ LoadConst32(dst, value);
969}
970
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200971void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
972 if (location.IsRegister()) {
973 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700974 } else if (location.IsRegisterPair()) {
975 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
976 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200977 } else {
978 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
979 }
980}
981
Vladimir Markoaad75c62016-10-03 08:46:48 +0000982template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
983inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
984 const ArenaDeque<PcRelativePatchInfo>& infos,
985 ArenaVector<LinkerPatch>* linker_patches) {
986 for (const PcRelativePatchInfo& info : infos) {
987 const DexFile& dex_file = info.target_dex_file;
988 size_t offset_or_index = info.offset_or_index;
989 DCHECK(info.high_label.IsBound());
990 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
991 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
992 // the assembler's base label used for PC-relative addressing.
993 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
994 ? __ GetLabelLocation(&info.pc_rel_label)
995 : __ GetPcRelBaseLabelLocation();
996 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
997 }
998}
999
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001000void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1001 DCHECK(linker_patches->empty());
1002 size_t size =
1003 method_patches_.size() +
1004 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001005 pc_relative_dex_cache_patches_.size() +
1006 pc_relative_string_patches_.size() +
1007 pc_relative_type_patches_.size() +
1008 boot_image_string_patches_.size() +
1009 boot_image_type_patches_.size() +
1010 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001011 linker_patches->reserve(size);
1012 for (const auto& entry : method_patches_) {
1013 const MethodReference& target_method = entry.first;
1014 Literal* literal = entry.second;
1015 DCHECK(literal->GetLabel()->IsBound());
1016 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1017 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
1018 target_method.dex_file,
1019 target_method.dex_method_index));
1020 }
1021 for (const auto& entry : call_patches_) {
1022 const MethodReference& target_method = entry.first;
1023 Literal* literal = entry.second;
1024 DCHECK(literal->GetLabel()->IsBound());
1025 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1026 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1027 target_method.dex_file,
1028 target_method.dex_method_index));
1029 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001030 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1031 linker_patches);
1032 if (!GetCompilerOptions().IsBootImage()) {
1033 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1034 linker_patches);
1035 } else {
1036 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1037 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001038 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001039 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1040 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001041 for (const auto& entry : boot_image_string_patches_) {
1042 const StringReference& target_string = entry.first;
1043 Literal* literal = entry.second;
1044 DCHECK(literal->GetLabel()->IsBound());
1045 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1046 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1047 target_string.dex_file,
1048 target_string.string_index));
1049 }
1050 for (const auto& entry : boot_image_type_patches_) {
1051 const TypeReference& target_type = entry.first;
1052 Literal* literal = entry.second;
1053 DCHECK(literal->GetLabel()->IsBound());
1054 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1055 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1056 target_type.dex_file,
1057 target_type.type_index));
1058 }
1059 for (const auto& entry : boot_image_address_patches_) {
1060 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1061 Literal* literal = entry.second;
1062 DCHECK(literal->GetLabel()->IsBound());
1063 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1064 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1065 }
1066}
1067
1068CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1069 const DexFile& dex_file, uint32_t string_index) {
1070 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1071}
1072
1073CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1074 const DexFile& dex_file, uint32_t type_index) {
1075 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001076}
1077
1078CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1079 const DexFile& dex_file, uint32_t element_offset) {
1080 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1081}
1082
1083CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1084 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1085 patches->emplace_back(dex_file, offset_or_index);
1086 return &patches->back();
1087}
1088
Alexey Frunze06a46c42016-07-19 15:00:40 -07001089Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1090 return map->GetOrCreate(
1091 value,
1092 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1093}
1094
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001095Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1096 MethodToLiteralMap* map) {
1097 return map->GetOrCreate(
1098 target_method,
1099 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1100}
1101
1102Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1103 return DeduplicateMethodLiteral(target_method, &method_patches_);
1104}
1105
1106Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1107 return DeduplicateMethodLiteral(target_method, &call_patches_);
1108}
1109
Alexey Frunze06a46c42016-07-19 15:00:40 -07001110Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1111 uint32_t string_index) {
1112 return boot_image_string_patches_.GetOrCreate(
1113 StringReference(&dex_file, string_index),
1114 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1115}
1116
1117Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1118 uint32_t type_index) {
1119 return boot_image_type_patches_.GetOrCreate(
1120 TypeReference(&dex_file, type_index),
1121 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1122}
1123
1124Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1125 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1126 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1127 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1128}
1129
Vladimir Markoaad75c62016-10-03 08:46:48 +00001130void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholder(
1131 PcRelativePatchInfo* info, Register out, Register base) {
1132 bool reordering = __ SetReorder(false);
1133 if (GetInstructionSetFeatures().IsR6()) {
1134 DCHECK_EQ(base, ZERO);
1135 __ Bind(&info->high_label);
1136 __ Bind(&info->pc_rel_label);
1137 // Add a 32-bit offset to PC.
1138 __ Auipc(out, /* placeholder */ 0x1234);
1139 __ Addiu(out, out, /* placeholder */ 0x5678);
1140 } else {
1141 // If base is ZERO, emit NAL to obtain the actual base.
1142 if (base == ZERO) {
1143 // Generate a dummy PC-relative call to obtain PC.
1144 __ Nal();
1145 }
1146 __ Bind(&info->high_label);
1147 __ Lui(out, /* placeholder */ 0x1234);
1148 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1149 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1150 if (base == ZERO) {
1151 __ Bind(&info->pc_rel_label);
1152 }
1153 __ Ori(out, out, /* placeholder */ 0x5678);
1154 // Add a 32-bit offset to PC.
1155 __ Addu(out, out, (base == ZERO) ? RA : base);
1156 }
1157 __ SetReorder(reordering);
1158}
1159
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001160void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1161 MipsLabel done;
1162 Register card = AT;
1163 Register temp = TMP;
1164 __ Beqz(value, &done);
1165 __ LoadFromOffset(kLoadWord,
1166 card,
1167 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001168 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001169 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1170 __ Addu(temp, card, temp);
1171 __ Sb(card, temp, 0);
1172 __ Bind(&done);
1173}
1174
David Brazdil58282f42016-01-14 12:45:10 +00001175void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001176 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1177 blocked_core_registers_[ZERO] = true;
1178 blocked_core_registers_[K0] = true;
1179 blocked_core_registers_[K1] = true;
1180 blocked_core_registers_[GP] = true;
1181 blocked_core_registers_[SP] = true;
1182 blocked_core_registers_[RA] = true;
1183
1184 // AT and TMP(T8) are used as temporary/scratch registers
1185 // (similar to how AT is used by MIPS assemblers).
1186 blocked_core_registers_[AT] = true;
1187 blocked_core_registers_[TMP] = true;
1188 blocked_fpu_registers_[FTMP] = true;
1189
1190 // Reserve suspend and thread registers.
1191 blocked_core_registers_[S0] = true;
1192 blocked_core_registers_[TR] = true;
1193
1194 // Reserve T9 for function calls
1195 blocked_core_registers_[T9] = true;
1196
1197 // Reserve odd-numbered FPU registers.
1198 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1199 blocked_fpu_registers_[i] = true;
1200 }
1201
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001202 if (GetGraph()->IsDebuggable()) {
1203 // Stubs do not save callee-save floating point registers. If the graph
1204 // is debuggable, we need to deal with these registers differently. For
1205 // now, just block them.
1206 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1207 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1208 }
1209 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001210}
1211
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001212size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1213 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1214 return kMipsWordSize;
1215}
1216
1217size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1218 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1219 return kMipsWordSize;
1220}
1221
1222size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1223 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1224 return kMipsDoublewordSize;
1225}
1226
1227size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1228 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1229 return kMipsDoublewordSize;
1230}
1231
1232void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001233 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001234}
1235
1236void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001237 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001238}
1239
Serban Constantinescufca16662016-07-14 09:21:59 +01001240constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1241
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001242void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1243 HInstruction* instruction,
1244 uint32_t dex_pc,
1245 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001246 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001247 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001248 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001249 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001250 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001251 // Reserve argument space on stack (for $a0-$a3) for
1252 // entrypoints that directly reference native implementations.
1253 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001254 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001255 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001256 } else {
1257 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001258 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001259 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001260 if (EntrypointRequiresStackMap(entrypoint)) {
1261 RecordPcInfo(instruction, dex_pc, slow_path);
1262 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001263}
1264
1265void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1266 Register class_reg) {
1267 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1268 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1269 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1270 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1271 __ Sync(0);
1272 __ Bind(slow_path->GetExitLabel());
1273}
1274
1275void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1276 __ Sync(0); // Only stype 0 is supported.
1277}
1278
1279void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1280 HBasicBlock* successor) {
1281 SuspendCheckSlowPathMIPS* slow_path =
1282 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1283 codegen_->AddSlowPath(slow_path);
1284
1285 __ LoadFromOffset(kLoadUnsignedHalfword,
1286 TMP,
1287 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001288 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001289 if (successor == nullptr) {
1290 __ Bnez(TMP, slow_path->GetEntryLabel());
1291 __ Bind(slow_path->GetReturnLabel());
1292 } else {
1293 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1294 __ B(slow_path->GetEntryLabel());
1295 // slow_path will return to GetLabelOf(successor).
1296 }
1297}
1298
1299InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1300 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001301 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001302 assembler_(codegen->GetAssembler()),
1303 codegen_(codegen) {}
1304
1305void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1306 DCHECK_EQ(instruction->InputCount(), 2U);
1307 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1308 Primitive::Type type = instruction->GetResultType();
1309 switch (type) {
1310 case Primitive::kPrimInt: {
1311 locations->SetInAt(0, Location::RequiresRegister());
1312 HInstruction* right = instruction->InputAt(1);
1313 bool can_use_imm = false;
1314 if (right->IsConstant()) {
1315 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1316 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1317 can_use_imm = IsUint<16>(imm);
1318 } else if (instruction->IsAdd()) {
1319 can_use_imm = IsInt<16>(imm);
1320 } else {
1321 DCHECK(instruction->IsSub());
1322 can_use_imm = IsInt<16>(-imm);
1323 }
1324 }
1325 if (can_use_imm)
1326 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1327 else
1328 locations->SetInAt(1, Location::RequiresRegister());
1329 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1330 break;
1331 }
1332
1333 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001334 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001335 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1336 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001337 break;
1338 }
1339
1340 case Primitive::kPrimFloat:
1341 case Primitive::kPrimDouble:
1342 DCHECK(instruction->IsAdd() || instruction->IsSub());
1343 locations->SetInAt(0, Location::RequiresFpuRegister());
1344 locations->SetInAt(1, Location::RequiresFpuRegister());
1345 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1346 break;
1347
1348 default:
1349 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1350 }
1351}
1352
1353void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1354 Primitive::Type type = instruction->GetType();
1355 LocationSummary* locations = instruction->GetLocations();
1356
1357 switch (type) {
1358 case Primitive::kPrimInt: {
1359 Register dst = locations->Out().AsRegister<Register>();
1360 Register lhs = locations->InAt(0).AsRegister<Register>();
1361 Location rhs_location = locations->InAt(1);
1362
1363 Register rhs_reg = ZERO;
1364 int32_t rhs_imm = 0;
1365 bool use_imm = rhs_location.IsConstant();
1366 if (use_imm) {
1367 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1368 } else {
1369 rhs_reg = rhs_location.AsRegister<Register>();
1370 }
1371
1372 if (instruction->IsAnd()) {
1373 if (use_imm)
1374 __ Andi(dst, lhs, rhs_imm);
1375 else
1376 __ And(dst, lhs, rhs_reg);
1377 } else if (instruction->IsOr()) {
1378 if (use_imm)
1379 __ Ori(dst, lhs, rhs_imm);
1380 else
1381 __ Or(dst, lhs, rhs_reg);
1382 } else if (instruction->IsXor()) {
1383 if (use_imm)
1384 __ Xori(dst, lhs, rhs_imm);
1385 else
1386 __ Xor(dst, lhs, rhs_reg);
1387 } else if (instruction->IsAdd()) {
1388 if (use_imm)
1389 __ Addiu(dst, lhs, rhs_imm);
1390 else
1391 __ Addu(dst, lhs, rhs_reg);
1392 } else {
1393 DCHECK(instruction->IsSub());
1394 if (use_imm)
1395 __ Addiu(dst, lhs, -rhs_imm);
1396 else
1397 __ Subu(dst, lhs, rhs_reg);
1398 }
1399 break;
1400 }
1401
1402 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001403 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1404 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1405 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1406 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001407 Location rhs_location = locations->InAt(1);
1408 bool use_imm = rhs_location.IsConstant();
1409 if (!use_imm) {
1410 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1411 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1412 if (instruction->IsAnd()) {
1413 __ And(dst_low, lhs_low, rhs_low);
1414 __ And(dst_high, lhs_high, rhs_high);
1415 } else if (instruction->IsOr()) {
1416 __ Or(dst_low, lhs_low, rhs_low);
1417 __ Or(dst_high, lhs_high, rhs_high);
1418 } else if (instruction->IsXor()) {
1419 __ Xor(dst_low, lhs_low, rhs_low);
1420 __ Xor(dst_high, lhs_high, rhs_high);
1421 } else if (instruction->IsAdd()) {
1422 if (lhs_low == rhs_low) {
1423 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1424 __ Slt(TMP, lhs_low, ZERO);
1425 __ Addu(dst_low, lhs_low, rhs_low);
1426 } else {
1427 __ Addu(dst_low, lhs_low, rhs_low);
1428 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1429 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1430 }
1431 __ Addu(dst_high, lhs_high, rhs_high);
1432 __ Addu(dst_high, dst_high, TMP);
1433 } else {
1434 DCHECK(instruction->IsSub());
1435 __ Sltu(TMP, lhs_low, rhs_low);
1436 __ Subu(dst_low, lhs_low, rhs_low);
1437 __ Subu(dst_high, lhs_high, rhs_high);
1438 __ Subu(dst_high, dst_high, TMP);
1439 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001440 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001441 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1442 if (instruction->IsOr()) {
1443 uint32_t low = Low32Bits(value);
1444 uint32_t high = High32Bits(value);
1445 if (IsUint<16>(low)) {
1446 if (dst_low != lhs_low || low != 0) {
1447 __ Ori(dst_low, lhs_low, low);
1448 }
1449 } else {
1450 __ LoadConst32(TMP, low);
1451 __ Or(dst_low, lhs_low, TMP);
1452 }
1453 if (IsUint<16>(high)) {
1454 if (dst_high != lhs_high || high != 0) {
1455 __ Ori(dst_high, lhs_high, high);
1456 }
1457 } else {
1458 if (high != low) {
1459 __ LoadConst32(TMP, high);
1460 }
1461 __ Or(dst_high, lhs_high, TMP);
1462 }
1463 } else if (instruction->IsXor()) {
1464 uint32_t low = Low32Bits(value);
1465 uint32_t high = High32Bits(value);
1466 if (IsUint<16>(low)) {
1467 if (dst_low != lhs_low || low != 0) {
1468 __ Xori(dst_low, lhs_low, low);
1469 }
1470 } else {
1471 __ LoadConst32(TMP, low);
1472 __ Xor(dst_low, lhs_low, TMP);
1473 }
1474 if (IsUint<16>(high)) {
1475 if (dst_high != lhs_high || high != 0) {
1476 __ Xori(dst_high, lhs_high, high);
1477 }
1478 } else {
1479 if (high != low) {
1480 __ LoadConst32(TMP, high);
1481 }
1482 __ Xor(dst_high, lhs_high, TMP);
1483 }
1484 } else if (instruction->IsAnd()) {
1485 uint32_t low = Low32Bits(value);
1486 uint32_t high = High32Bits(value);
1487 if (IsUint<16>(low)) {
1488 __ Andi(dst_low, lhs_low, low);
1489 } else if (low != 0xFFFFFFFF) {
1490 __ LoadConst32(TMP, low);
1491 __ And(dst_low, lhs_low, TMP);
1492 } else if (dst_low != lhs_low) {
1493 __ Move(dst_low, lhs_low);
1494 }
1495 if (IsUint<16>(high)) {
1496 __ Andi(dst_high, lhs_high, high);
1497 } else if (high != 0xFFFFFFFF) {
1498 if (high != low) {
1499 __ LoadConst32(TMP, high);
1500 }
1501 __ And(dst_high, lhs_high, TMP);
1502 } else if (dst_high != lhs_high) {
1503 __ Move(dst_high, lhs_high);
1504 }
1505 } else {
1506 if (instruction->IsSub()) {
1507 value = -value;
1508 } else {
1509 DCHECK(instruction->IsAdd());
1510 }
1511 int32_t low = Low32Bits(value);
1512 int32_t high = High32Bits(value);
1513 if (IsInt<16>(low)) {
1514 if (dst_low != lhs_low || low != 0) {
1515 __ Addiu(dst_low, lhs_low, low);
1516 }
1517 if (low != 0) {
1518 __ Sltiu(AT, dst_low, low);
1519 }
1520 } else {
1521 __ LoadConst32(TMP, low);
1522 __ Addu(dst_low, lhs_low, TMP);
1523 __ Sltu(AT, dst_low, TMP);
1524 }
1525 if (IsInt<16>(high)) {
1526 if (dst_high != lhs_high || high != 0) {
1527 __ Addiu(dst_high, lhs_high, high);
1528 }
1529 } else {
1530 if (high != low) {
1531 __ LoadConst32(TMP, high);
1532 }
1533 __ Addu(dst_high, lhs_high, TMP);
1534 }
1535 if (low != 0) {
1536 __ Addu(dst_high, dst_high, AT);
1537 }
1538 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001539 }
1540 break;
1541 }
1542
1543 case Primitive::kPrimFloat:
1544 case Primitive::kPrimDouble: {
1545 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1546 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1547 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1548 if (instruction->IsAdd()) {
1549 if (type == Primitive::kPrimFloat) {
1550 __ AddS(dst, lhs, rhs);
1551 } else {
1552 __ AddD(dst, lhs, rhs);
1553 }
1554 } else {
1555 DCHECK(instruction->IsSub());
1556 if (type == Primitive::kPrimFloat) {
1557 __ SubS(dst, lhs, rhs);
1558 } else {
1559 __ SubD(dst, lhs, rhs);
1560 }
1561 }
1562 break;
1563 }
1564
1565 default:
1566 LOG(FATAL) << "Unexpected binary operation type " << type;
1567 }
1568}
1569
1570void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001571 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001572
1573 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1574 Primitive::Type type = instr->GetResultType();
1575 switch (type) {
1576 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001577 locations->SetInAt(0, Location::RequiresRegister());
1578 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1579 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1580 break;
1581 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001582 locations->SetInAt(0, Location::RequiresRegister());
1583 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1584 locations->SetOut(Location::RequiresRegister());
1585 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001586 default:
1587 LOG(FATAL) << "Unexpected shift type " << type;
1588 }
1589}
1590
1591static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1592
1593void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001594 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001595 LocationSummary* locations = instr->GetLocations();
1596 Primitive::Type type = instr->GetType();
1597
1598 Location rhs_location = locations->InAt(1);
1599 bool use_imm = rhs_location.IsConstant();
1600 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1601 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001602 const uint32_t shift_mask =
1603 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001604 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001605 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1606 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001607
1608 switch (type) {
1609 case Primitive::kPrimInt: {
1610 Register dst = locations->Out().AsRegister<Register>();
1611 Register lhs = locations->InAt(0).AsRegister<Register>();
1612 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001613 if (shift_value == 0) {
1614 if (dst != lhs) {
1615 __ Move(dst, lhs);
1616 }
1617 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001618 __ Sll(dst, lhs, shift_value);
1619 } else if (instr->IsShr()) {
1620 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001621 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001622 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001623 } else {
1624 if (has_ins_rotr) {
1625 __ Rotr(dst, lhs, shift_value);
1626 } else {
1627 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1628 __ Srl(dst, lhs, shift_value);
1629 __ Or(dst, dst, TMP);
1630 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001631 }
1632 } else {
1633 if (instr->IsShl()) {
1634 __ Sllv(dst, lhs, rhs_reg);
1635 } else if (instr->IsShr()) {
1636 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001637 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001638 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001639 } else {
1640 if (has_ins_rotr) {
1641 __ Rotrv(dst, lhs, rhs_reg);
1642 } else {
1643 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001644 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1645 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1646 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1647 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1648 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001649 __ Sllv(TMP, lhs, TMP);
1650 __ Srlv(dst, lhs, rhs_reg);
1651 __ Or(dst, dst, TMP);
1652 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001653 }
1654 }
1655 break;
1656 }
1657
1658 case Primitive::kPrimLong: {
1659 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1660 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1661 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1662 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1663 if (use_imm) {
1664 if (shift_value == 0) {
1665 codegen_->Move64(locations->Out(), locations->InAt(0));
1666 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001667 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001668 if (instr->IsShl()) {
1669 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1670 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1671 __ Sll(dst_low, lhs_low, shift_value);
1672 } else if (instr->IsShr()) {
1673 __ Srl(dst_low, lhs_low, shift_value);
1674 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1675 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001676 } else if (instr->IsUShr()) {
1677 __ Srl(dst_low, lhs_low, shift_value);
1678 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1679 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001680 } else {
1681 __ Srl(dst_low, lhs_low, shift_value);
1682 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1683 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001684 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001685 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001686 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001687 if (instr->IsShl()) {
1688 __ Sll(dst_low, lhs_low, shift_value);
1689 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1690 __ Sll(dst_high, lhs_high, shift_value);
1691 __ Or(dst_high, dst_high, TMP);
1692 } else if (instr->IsShr()) {
1693 __ Sra(dst_high, lhs_high, shift_value);
1694 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1695 __ Srl(dst_low, lhs_low, shift_value);
1696 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001697 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001698 __ Srl(dst_high, lhs_high, shift_value);
1699 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1700 __ Srl(dst_low, lhs_low, shift_value);
1701 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001702 } else {
1703 __ Srl(TMP, lhs_low, shift_value);
1704 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1705 __ Or(dst_low, dst_low, TMP);
1706 __ Srl(TMP, lhs_high, shift_value);
1707 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1708 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001709 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001710 }
1711 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001712 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001713 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001714 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001715 __ Move(dst_low, ZERO);
1716 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001717 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001718 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001719 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001720 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001721 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001722 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001723 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001724 // 64-bit rotation by 32 is just a swap.
1725 __ Move(dst_low, lhs_high);
1726 __ Move(dst_high, lhs_low);
1727 } else {
1728 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001729 __ Srl(dst_low, lhs_high, shift_value_high);
1730 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1731 __ Srl(dst_high, lhs_low, shift_value_high);
1732 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001733 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001734 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1735 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001736 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001737 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1738 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001739 __ Or(dst_high, dst_high, TMP);
1740 }
1741 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001742 }
1743 }
1744 } else {
1745 MipsLabel done;
1746 if (instr->IsShl()) {
1747 __ Sllv(dst_low, lhs_low, rhs_reg);
1748 __ Nor(AT, ZERO, rhs_reg);
1749 __ Srl(TMP, lhs_low, 1);
1750 __ Srlv(TMP, TMP, AT);
1751 __ Sllv(dst_high, lhs_high, rhs_reg);
1752 __ Or(dst_high, dst_high, TMP);
1753 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1754 __ Beqz(TMP, &done);
1755 __ Move(dst_high, dst_low);
1756 __ Move(dst_low, ZERO);
1757 } else if (instr->IsShr()) {
1758 __ Srav(dst_high, lhs_high, rhs_reg);
1759 __ Nor(AT, ZERO, rhs_reg);
1760 __ Sll(TMP, lhs_high, 1);
1761 __ Sllv(TMP, TMP, AT);
1762 __ Srlv(dst_low, lhs_low, rhs_reg);
1763 __ Or(dst_low, dst_low, TMP);
1764 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1765 __ Beqz(TMP, &done);
1766 __ Move(dst_low, dst_high);
1767 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001768 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001769 __ Srlv(dst_high, lhs_high, rhs_reg);
1770 __ Nor(AT, ZERO, rhs_reg);
1771 __ Sll(TMP, lhs_high, 1);
1772 __ Sllv(TMP, TMP, AT);
1773 __ Srlv(dst_low, lhs_low, rhs_reg);
1774 __ Or(dst_low, dst_low, TMP);
1775 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1776 __ Beqz(TMP, &done);
1777 __ Move(dst_low, dst_high);
1778 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001779 } else {
1780 __ Nor(AT, ZERO, rhs_reg);
1781 __ Srlv(TMP, lhs_low, rhs_reg);
1782 __ Sll(dst_low, lhs_high, 1);
1783 __ Sllv(dst_low, dst_low, AT);
1784 __ Or(dst_low, dst_low, TMP);
1785 __ Srlv(TMP, lhs_high, rhs_reg);
1786 __ Sll(dst_high, lhs_low, 1);
1787 __ Sllv(dst_high, dst_high, AT);
1788 __ Or(dst_high, dst_high, TMP);
1789 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1790 __ Beqz(TMP, &done);
1791 __ Move(TMP, dst_high);
1792 __ Move(dst_high, dst_low);
1793 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001794 }
1795 __ Bind(&done);
1796 }
1797 break;
1798 }
1799
1800 default:
1801 LOG(FATAL) << "Unexpected shift operation type " << type;
1802 }
1803}
1804
1805void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1806 HandleBinaryOp(instruction);
1807}
1808
1809void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1810 HandleBinaryOp(instruction);
1811}
1812
1813void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1814 HandleBinaryOp(instruction);
1815}
1816
1817void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1818 HandleBinaryOp(instruction);
1819}
1820
1821void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1822 LocationSummary* locations =
1823 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1824 locations->SetInAt(0, Location::RequiresRegister());
1825 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1826 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1827 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1828 } else {
1829 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1830 }
1831}
1832
Alexey Frunze2923db72016-08-20 01:55:47 -07001833auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1834 auto null_checker = [this, instruction]() {
1835 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1836 };
1837 return null_checker;
1838}
1839
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001840void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1841 LocationSummary* locations = instruction->GetLocations();
1842 Register obj = locations->InAt(0).AsRegister<Register>();
1843 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001844 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001845 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001846
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001847 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001848 switch (type) {
1849 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001850 Register out = locations->Out().AsRegister<Register>();
1851 if (index.IsConstant()) {
1852 size_t offset =
1853 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001854 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001855 } else {
1856 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001857 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001858 }
1859 break;
1860 }
1861
1862 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 Register out = locations->Out().AsRegister<Register>();
1864 if (index.IsConstant()) {
1865 size_t offset =
1866 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001867 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001868 } else {
1869 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001870 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 }
1872 break;
1873 }
1874
1875 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 Register out = locations->Out().AsRegister<Register>();
1877 if (index.IsConstant()) {
1878 size_t offset =
1879 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001880 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001881 } else {
1882 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1883 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001884 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001885 }
1886 break;
1887 }
1888
1889 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001890 Register out = locations->Out().AsRegister<Register>();
1891 if (index.IsConstant()) {
1892 size_t offset =
1893 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001894 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001895 } else {
1896 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1897 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001898 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001899 }
1900 break;
1901 }
1902
1903 case Primitive::kPrimInt:
1904 case Primitive::kPrimNot: {
1905 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001906 Register out = locations->Out().AsRegister<Register>();
1907 if (index.IsConstant()) {
1908 size_t offset =
1909 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001910 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001911 } else {
1912 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1913 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001914 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915 }
1916 break;
1917 }
1918
1919 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001920 Register out = locations->Out().AsRegisterPairLow<Register>();
1921 if (index.IsConstant()) {
1922 size_t offset =
1923 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001924 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001925 } else {
1926 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1927 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001928 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929 }
1930 break;
1931 }
1932
1933 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001934 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1935 if (index.IsConstant()) {
1936 size_t offset =
1937 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001938 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001939 } else {
1940 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1941 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001942 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001943 }
1944 break;
1945 }
1946
1947 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001948 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1949 if (index.IsConstant()) {
1950 size_t offset =
1951 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001952 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001953 } else {
1954 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1955 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001956 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001957 }
1958 break;
1959 }
1960
1961 case Primitive::kPrimVoid:
1962 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1963 UNREACHABLE();
1964 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001965}
1966
1967void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1968 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1969 locations->SetInAt(0, Location::RequiresRegister());
1970 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1971}
1972
1973void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1974 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001975 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001976 Register obj = locations->InAt(0).AsRegister<Register>();
1977 Register out = locations->Out().AsRegister<Register>();
1978 __ LoadFromOffset(kLoadWord, out, obj, offset);
1979 codegen_->MaybeRecordImplicitNullCheck(instruction);
1980}
1981
Alexey Frunzef58b2482016-09-02 22:14:06 -07001982Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1983 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1984 ? Location::ConstantLocation(instruction->AsConstant())
1985 : Location::RequiresRegister();
1986}
1987
1988Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1989 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1990 // We can store a non-zero float or double constant without first loading it into the FPU,
1991 // but we should only prefer this if the constant has a single use.
1992 if (instruction->IsConstant() &&
1993 (instruction->AsConstant()->IsZeroBitPattern() ||
1994 instruction->GetUses().HasExactlyOneElement())) {
1995 return Location::ConstantLocation(instruction->AsConstant());
1996 // Otherwise fall through and require an FPU register for the constant.
1997 }
1998 return Location::RequiresFpuRegister();
1999}
2000
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002001void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002002 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002003 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2004 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002005 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002006 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002007 InvokeRuntimeCallingConvention calling_convention;
2008 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2009 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2010 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2011 } else {
2012 locations->SetInAt(0, Location::RequiresRegister());
2013 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2014 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002015 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002016 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002017 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002018 }
2019 }
2020}
2021
2022void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2023 LocationSummary* locations = instruction->GetLocations();
2024 Register obj = locations->InAt(0).AsRegister<Register>();
2025 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002026 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002027 Primitive::Type value_type = instruction->GetComponentType();
2028 bool needs_runtime_call = locations->WillCall();
2029 bool needs_write_barrier =
2030 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002031 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002032 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002033
2034 switch (value_type) {
2035 case Primitive::kPrimBoolean:
2036 case Primitive::kPrimByte: {
2037 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002038 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002039 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002040 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002041 __ Addu(base_reg, obj, index.AsRegister<Register>());
2042 }
2043 if (value_location.IsConstant()) {
2044 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2045 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2046 } else {
2047 Register value = value_location.AsRegister<Register>();
2048 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002049 }
2050 break;
2051 }
2052
2053 case Primitive::kPrimShort:
2054 case Primitive::kPrimChar: {
2055 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002056 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002057 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002058 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002059 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2060 __ Addu(base_reg, obj, base_reg);
2061 }
2062 if (value_location.IsConstant()) {
2063 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2064 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2065 } else {
2066 Register value = value_location.AsRegister<Register>();
2067 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002068 }
2069 break;
2070 }
2071
2072 case Primitive::kPrimInt:
2073 case Primitive::kPrimNot: {
2074 if (!needs_runtime_call) {
2075 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002076 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002077 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002078 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002079 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2080 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002081 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002082 if (value_location.IsConstant()) {
2083 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2084 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2085 DCHECK(!needs_write_barrier);
2086 } else {
2087 Register value = value_location.AsRegister<Register>();
2088 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2089 if (needs_write_barrier) {
2090 DCHECK_EQ(value_type, Primitive::kPrimNot);
2091 codegen_->MarkGCCard(obj, value);
2092 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002093 }
2094 } else {
2095 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002096 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002097 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2098 }
2099 break;
2100 }
2101
2102 case Primitive::kPrimLong: {
2103 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002104 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002105 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002106 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002107 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2108 __ Addu(base_reg, obj, base_reg);
2109 }
2110 if (value_location.IsConstant()) {
2111 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2112 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2113 } else {
2114 Register value = value_location.AsRegisterPairLow<Register>();
2115 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002116 }
2117 break;
2118 }
2119
2120 case Primitive::kPrimFloat: {
2121 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002122 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002123 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002124 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002125 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2126 __ Addu(base_reg, obj, base_reg);
2127 }
2128 if (value_location.IsConstant()) {
2129 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2130 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2131 } else {
2132 FRegister value = value_location.AsFpuRegister<FRegister>();
2133 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002134 }
2135 break;
2136 }
2137
2138 case Primitive::kPrimDouble: {
2139 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002140 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002141 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002142 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002143 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2144 __ Addu(base_reg, obj, base_reg);
2145 }
2146 if (value_location.IsConstant()) {
2147 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2148 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2149 } else {
2150 FRegister value = value_location.AsFpuRegister<FRegister>();
2151 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002152 }
2153 break;
2154 }
2155
2156 case Primitive::kPrimVoid:
2157 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2158 UNREACHABLE();
2159 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002160}
2161
2162void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002163 RegisterSet caller_saves = RegisterSet::Empty();
2164 InvokeRuntimeCallingConvention calling_convention;
2165 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2166 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2167 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002168 locations->SetInAt(0, Location::RequiresRegister());
2169 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002170}
2171
2172void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2173 LocationSummary* locations = instruction->GetLocations();
2174 BoundsCheckSlowPathMIPS* slow_path =
2175 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2176 codegen_->AddSlowPath(slow_path);
2177
2178 Register index = locations->InAt(0).AsRegister<Register>();
2179 Register length = locations->InAt(1).AsRegister<Register>();
2180
2181 // length is limited by the maximum positive signed 32-bit integer.
2182 // Unsigned comparison of length and index checks for index < 0
2183 // and for length <= index simultaneously.
2184 __ Bgeu(index, length, slow_path->GetEntryLabel());
2185}
2186
2187void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2188 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2189 instruction,
2190 LocationSummary::kCallOnSlowPath);
2191 locations->SetInAt(0, Location::RequiresRegister());
2192 locations->SetInAt(1, Location::RequiresRegister());
2193 // Note that TypeCheckSlowPathMIPS uses this register too.
2194 locations->AddTemp(Location::RequiresRegister());
2195}
2196
2197void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2198 LocationSummary* locations = instruction->GetLocations();
2199 Register obj = locations->InAt(0).AsRegister<Register>();
2200 Register cls = locations->InAt(1).AsRegister<Register>();
2201 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2202
2203 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2204 codegen_->AddSlowPath(slow_path);
2205
2206 // TODO: avoid this check if we know obj is not null.
2207 __ Beqz(obj, slow_path->GetExitLabel());
2208 // Compare the class of `obj` with `cls`.
2209 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2210 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2211 __ Bind(slow_path->GetExitLabel());
2212}
2213
2214void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2215 LocationSummary* locations =
2216 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2217 locations->SetInAt(0, Location::RequiresRegister());
2218 if (check->HasUses()) {
2219 locations->SetOut(Location::SameAsFirstInput());
2220 }
2221}
2222
2223void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2224 // We assume the class is not null.
2225 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2226 check->GetLoadClass(),
2227 check,
2228 check->GetDexPc(),
2229 true);
2230 codegen_->AddSlowPath(slow_path);
2231 GenerateClassInitializationCheck(slow_path,
2232 check->GetLocations()->InAt(0).AsRegister<Register>());
2233}
2234
2235void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2236 Primitive::Type in_type = compare->InputAt(0)->GetType();
2237
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002238 LocationSummary* locations =
2239 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002240
2241 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002242 case Primitive::kPrimBoolean:
2243 case Primitive::kPrimByte:
2244 case Primitive::kPrimShort:
2245 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002246 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002247 locations->SetInAt(0, Location::RequiresRegister());
2248 locations->SetInAt(1, Location::RequiresRegister());
2249 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2250 break;
2251
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002252 case Primitive::kPrimLong:
2253 locations->SetInAt(0, Location::RequiresRegister());
2254 locations->SetInAt(1, Location::RequiresRegister());
2255 // Output overlaps because it is written before doing the low comparison.
2256 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2257 break;
2258
2259 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002260 case Primitive::kPrimDouble:
2261 locations->SetInAt(0, Location::RequiresFpuRegister());
2262 locations->SetInAt(1, Location::RequiresFpuRegister());
2263 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002264 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002265
2266 default:
2267 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2268 }
2269}
2270
2271void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2272 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002273 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002274 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002275 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002276
2277 // 0 if: left == right
2278 // 1 if: left > right
2279 // -1 if: left < right
2280 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002281 case Primitive::kPrimBoolean:
2282 case Primitive::kPrimByte:
2283 case Primitive::kPrimShort:
2284 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002285 case Primitive::kPrimInt: {
2286 Register lhs = locations->InAt(0).AsRegister<Register>();
2287 Register rhs = locations->InAt(1).AsRegister<Register>();
2288 __ Slt(TMP, lhs, rhs);
2289 __ Slt(res, rhs, lhs);
2290 __ Subu(res, res, TMP);
2291 break;
2292 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002293 case Primitive::kPrimLong: {
2294 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002295 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2296 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2297 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2298 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2299 // TODO: more efficient (direct) comparison with a constant.
2300 __ Slt(TMP, lhs_high, rhs_high);
2301 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2302 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2303 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2304 __ Sltu(TMP, lhs_low, rhs_low);
2305 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2306 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2307 __ Bind(&done);
2308 break;
2309 }
2310
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002311 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002312 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002313 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2314 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2315 MipsLabel done;
2316 if (isR6) {
2317 __ CmpEqS(FTMP, lhs, rhs);
2318 __ LoadConst32(res, 0);
2319 __ Bc1nez(FTMP, &done);
2320 if (gt_bias) {
2321 __ CmpLtS(FTMP, lhs, rhs);
2322 __ LoadConst32(res, -1);
2323 __ Bc1nez(FTMP, &done);
2324 __ LoadConst32(res, 1);
2325 } else {
2326 __ CmpLtS(FTMP, rhs, lhs);
2327 __ LoadConst32(res, 1);
2328 __ Bc1nez(FTMP, &done);
2329 __ LoadConst32(res, -1);
2330 }
2331 } else {
2332 if (gt_bias) {
2333 __ ColtS(0, lhs, rhs);
2334 __ LoadConst32(res, -1);
2335 __ Bc1t(0, &done);
2336 __ CeqS(0, lhs, rhs);
2337 __ LoadConst32(res, 1);
2338 __ Movt(res, ZERO, 0);
2339 } else {
2340 __ ColtS(0, rhs, lhs);
2341 __ LoadConst32(res, 1);
2342 __ Bc1t(0, &done);
2343 __ CeqS(0, lhs, rhs);
2344 __ LoadConst32(res, -1);
2345 __ Movt(res, ZERO, 0);
2346 }
2347 }
2348 __ Bind(&done);
2349 break;
2350 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002351 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002352 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002353 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2354 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2355 MipsLabel done;
2356 if (isR6) {
2357 __ CmpEqD(FTMP, lhs, rhs);
2358 __ LoadConst32(res, 0);
2359 __ Bc1nez(FTMP, &done);
2360 if (gt_bias) {
2361 __ CmpLtD(FTMP, lhs, rhs);
2362 __ LoadConst32(res, -1);
2363 __ Bc1nez(FTMP, &done);
2364 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002365 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002366 __ CmpLtD(FTMP, rhs, lhs);
2367 __ LoadConst32(res, 1);
2368 __ Bc1nez(FTMP, &done);
2369 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002370 }
2371 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002372 if (gt_bias) {
2373 __ ColtD(0, lhs, rhs);
2374 __ LoadConst32(res, -1);
2375 __ Bc1t(0, &done);
2376 __ CeqD(0, lhs, rhs);
2377 __ LoadConst32(res, 1);
2378 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002379 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002380 __ ColtD(0, rhs, lhs);
2381 __ LoadConst32(res, 1);
2382 __ Bc1t(0, &done);
2383 __ CeqD(0, lhs, rhs);
2384 __ LoadConst32(res, -1);
2385 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002386 }
2387 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002388 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002389 break;
2390 }
2391
2392 default:
2393 LOG(FATAL) << "Unimplemented compare type " << in_type;
2394 }
2395}
2396
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002397void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002399 switch (instruction->InputAt(0)->GetType()) {
2400 default:
2401 case Primitive::kPrimLong:
2402 locations->SetInAt(0, Location::RequiresRegister());
2403 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2404 break;
2405
2406 case Primitive::kPrimFloat:
2407 case Primitive::kPrimDouble:
2408 locations->SetInAt(0, Location::RequiresFpuRegister());
2409 locations->SetInAt(1, Location::RequiresFpuRegister());
2410 break;
2411 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002412 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002413 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2414 }
2415}
2416
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002417void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002418 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002419 return;
2420 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002421
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002422 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002423 LocationSummary* locations = instruction->GetLocations();
2424 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002425 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002426
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002427 switch (type) {
2428 default:
2429 // Integer case.
2430 GenerateIntCompare(instruction->GetCondition(), locations);
2431 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002433 case Primitive::kPrimLong:
2434 // TODO: don't use branches.
2435 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002436 break;
2437
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002438 case Primitive::kPrimFloat:
2439 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002440 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2441 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002442 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002443
2444 // Convert the branches into the result.
2445 MipsLabel done;
2446
2447 // False case: result = 0.
2448 __ LoadConst32(dst, 0);
2449 __ B(&done);
2450
2451 // True case: result = 1.
2452 __ Bind(&true_label);
2453 __ LoadConst32(dst, 1);
2454 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002455}
2456
Alexey Frunze7e99e052015-11-24 19:28:01 -08002457void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2458 DCHECK(instruction->IsDiv() || instruction->IsRem());
2459 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2460
2461 LocationSummary* locations = instruction->GetLocations();
2462 Location second = locations->InAt(1);
2463 DCHECK(second.IsConstant());
2464
2465 Register out = locations->Out().AsRegister<Register>();
2466 Register dividend = locations->InAt(0).AsRegister<Register>();
2467 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2468 DCHECK(imm == 1 || imm == -1);
2469
2470 if (instruction->IsRem()) {
2471 __ Move(out, ZERO);
2472 } else {
2473 if (imm == -1) {
2474 __ Subu(out, ZERO, dividend);
2475 } else if (out != dividend) {
2476 __ Move(out, dividend);
2477 }
2478 }
2479}
2480
2481void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2482 DCHECK(instruction->IsDiv() || instruction->IsRem());
2483 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2484
2485 LocationSummary* locations = instruction->GetLocations();
2486 Location second = locations->InAt(1);
2487 DCHECK(second.IsConstant());
2488
2489 Register out = locations->Out().AsRegister<Register>();
2490 Register dividend = locations->InAt(0).AsRegister<Register>();
2491 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002492 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002493 int ctz_imm = CTZ(abs_imm);
2494
2495 if (instruction->IsDiv()) {
2496 if (ctz_imm == 1) {
2497 // Fast path for division by +/-2, which is very common.
2498 __ Srl(TMP, dividend, 31);
2499 } else {
2500 __ Sra(TMP, dividend, 31);
2501 __ Srl(TMP, TMP, 32 - ctz_imm);
2502 }
2503 __ Addu(out, dividend, TMP);
2504 __ Sra(out, out, ctz_imm);
2505 if (imm < 0) {
2506 __ Subu(out, ZERO, out);
2507 }
2508 } else {
2509 if (ctz_imm == 1) {
2510 // Fast path for modulo +/-2, which is very common.
2511 __ Sra(TMP, dividend, 31);
2512 __ Subu(out, dividend, TMP);
2513 __ Andi(out, out, 1);
2514 __ Addu(out, out, TMP);
2515 } else {
2516 __ Sra(TMP, dividend, 31);
2517 __ Srl(TMP, TMP, 32 - ctz_imm);
2518 __ Addu(out, dividend, TMP);
2519 if (IsUint<16>(abs_imm - 1)) {
2520 __ Andi(out, out, abs_imm - 1);
2521 } else {
2522 __ Sll(out, out, 32 - ctz_imm);
2523 __ Srl(out, out, 32 - ctz_imm);
2524 }
2525 __ Subu(out, out, TMP);
2526 }
2527 }
2528}
2529
2530void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2531 DCHECK(instruction->IsDiv() || instruction->IsRem());
2532 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2533
2534 LocationSummary* locations = instruction->GetLocations();
2535 Location second = locations->InAt(1);
2536 DCHECK(second.IsConstant());
2537
2538 Register out = locations->Out().AsRegister<Register>();
2539 Register dividend = locations->InAt(0).AsRegister<Register>();
2540 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2541
2542 int64_t magic;
2543 int shift;
2544 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2545
2546 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2547
2548 __ LoadConst32(TMP, magic);
2549 if (isR6) {
2550 __ MuhR6(TMP, dividend, TMP);
2551 } else {
2552 __ MultR2(dividend, TMP);
2553 __ Mfhi(TMP);
2554 }
2555 if (imm > 0 && magic < 0) {
2556 __ Addu(TMP, TMP, dividend);
2557 } else if (imm < 0 && magic > 0) {
2558 __ Subu(TMP, TMP, dividend);
2559 }
2560
2561 if (shift != 0) {
2562 __ Sra(TMP, TMP, shift);
2563 }
2564
2565 if (instruction->IsDiv()) {
2566 __ Sra(out, TMP, 31);
2567 __ Subu(out, TMP, out);
2568 } else {
2569 __ Sra(AT, TMP, 31);
2570 __ Subu(AT, TMP, AT);
2571 __ LoadConst32(TMP, imm);
2572 if (isR6) {
2573 __ MulR6(TMP, AT, TMP);
2574 } else {
2575 __ MulR2(TMP, AT, TMP);
2576 }
2577 __ Subu(out, dividend, TMP);
2578 }
2579}
2580
2581void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2582 DCHECK(instruction->IsDiv() || instruction->IsRem());
2583 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2584
2585 LocationSummary* locations = instruction->GetLocations();
2586 Register out = locations->Out().AsRegister<Register>();
2587 Location second = locations->InAt(1);
2588
2589 if (second.IsConstant()) {
2590 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2591 if (imm == 0) {
2592 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2593 } else if (imm == 1 || imm == -1) {
2594 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002595 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002596 DivRemByPowerOfTwo(instruction);
2597 } else {
2598 DCHECK(imm <= -2 || imm >= 2);
2599 GenerateDivRemWithAnyConstant(instruction);
2600 }
2601 } else {
2602 Register dividend = locations->InAt(0).AsRegister<Register>();
2603 Register divisor = second.AsRegister<Register>();
2604 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2605 if (instruction->IsDiv()) {
2606 if (isR6) {
2607 __ DivR6(out, dividend, divisor);
2608 } else {
2609 __ DivR2(out, dividend, divisor);
2610 }
2611 } else {
2612 if (isR6) {
2613 __ ModR6(out, dividend, divisor);
2614 } else {
2615 __ ModR2(out, dividend, divisor);
2616 }
2617 }
2618 }
2619}
2620
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002621void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2622 Primitive::Type type = div->GetResultType();
2623 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002624 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002625 : LocationSummary::kNoCall;
2626
2627 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2628
2629 switch (type) {
2630 case Primitive::kPrimInt:
2631 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002632 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2634 break;
2635
2636 case Primitive::kPrimLong: {
2637 InvokeRuntimeCallingConvention calling_convention;
2638 locations->SetInAt(0, Location::RegisterPairLocation(
2639 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2640 locations->SetInAt(1, Location::RegisterPairLocation(
2641 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2642 locations->SetOut(calling_convention.GetReturnLocation(type));
2643 break;
2644 }
2645
2646 case Primitive::kPrimFloat:
2647 case Primitive::kPrimDouble:
2648 locations->SetInAt(0, Location::RequiresFpuRegister());
2649 locations->SetInAt(1, Location::RequiresFpuRegister());
2650 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2651 break;
2652
2653 default:
2654 LOG(FATAL) << "Unexpected div type " << type;
2655 }
2656}
2657
2658void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2659 Primitive::Type type = instruction->GetType();
2660 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002661
2662 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002663 case Primitive::kPrimInt:
2664 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002665 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002666 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002667 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002668 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2669 break;
2670 }
2671 case Primitive::kPrimFloat:
2672 case Primitive::kPrimDouble: {
2673 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2674 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2675 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2676 if (type == Primitive::kPrimFloat) {
2677 __ DivS(dst, lhs, rhs);
2678 } else {
2679 __ DivD(dst, lhs, rhs);
2680 }
2681 break;
2682 }
2683 default:
2684 LOG(FATAL) << "Unexpected div type " << type;
2685 }
2686}
2687
2688void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002689 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002690 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002691}
2692
2693void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2694 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2695 codegen_->AddSlowPath(slow_path);
2696 Location value = instruction->GetLocations()->InAt(0);
2697 Primitive::Type type = instruction->GetType();
2698
2699 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002700 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002701 case Primitive::kPrimByte:
2702 case Primitive::kPrimChar:
2703 case Primitive::kPrimShort:
2704 case Primitive::kPrimInt: {
2705 if (value.IsConstant()) {
2706 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2707 __ B(slow_path->GetEntryLabel());
2708 } else {
2709 // A division by a non-null constant is valid. We don't need to perform
2710 // any check, so simply fall through.
2711 }
2712 } else {
2713 DCHECK(value.IsRegister()) << value;
2714 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2715 }
2716 break;
2717 }
2718 case Primitive::kPrimLong: {
2719 if (value.IsConstant()) {
2720 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2721 __ B(slow_path->GetEntryLabel());
2722 } else {
2723 // A division by a non-null constant is valid. We don't need to perform
2724 // any check, so simply fall through.
2725 }
2726 } else {
2727 DCHECK(value.IsRegisterPair()) << value;
2728 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2729 __ Beqz(TMP, slow_path->GetEntryLabel());
2730 }
2731 break;
2732 }
2733 default:
2734 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2735 }
2736}
2737
2738void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2739 LocationSummary* locations =
2740 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2741 locations->SetOut(Location::ConstantLocation(constant));
2742}
2743
2744void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2745 // Will be generated at use site.
2746}
2747
2748void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2749 exit->SetLocations(nullptr);
2750}
2751
2752void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2753}
2754
2755void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2756 LocationSummary* locations =
2757 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2758 locations->SetOut(Location::ConstantLocation(constant));
2759}
2760
2761void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2762 // Will be generated at use site.
2763}
2764
2765void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2766 got->SetLocations(nullptr);
2767}
2768
2769void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2770 DCHECK(!successor->IsExitBlock());
2771 HBasicBlock* block = got->GetBlock();
2772 HInstruction* previous = got->GetPrevious();
2773 HLoopInformation* info = block->GetLoopInformation();
2774
2775 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2776 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2777 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2778 return;
2779 }
2780 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2781 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2782 }
2783 if (!codegen_->GoesToNextBlock(block, successor)) {
2784 __ B(codegen_->GetLabelOf(successor));
2785 }
2786}
2787
2788void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2789 HandleGoto(got, got->GetSuccessor());
2790}
2791
2792void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2793 try_boundary->SetLocations(nullptr);
2794}
2795
2796void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2797 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2798 if (!successor->IsExitBlock()) {
2799 HandleGoto(try_boundary, successor);
2800 }
2801}
2802
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002803void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2804 LocationSummary* locations) {
2805 Register dst = locations->Out().AsRegister<Register>();
2806 Register lhs = locations->InAt(0).AsRegister<Register>();
2807 Location rhs_location = locations->InAt(1);
2808 Register rhs_reg = ZERO;
2809 int64_t rhs_imm = 0;
2810 bool use_imm = rhs_location.IsConstant();
2811 if (use_imm) {
2812 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2813 } else {
2814 rhs_reg = rhs_location.AsRegister<Register>();
2815 }
2816
2817 switch (cond) {
2818 case kCondEQ:
2819 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002820 if (use_imm && IsInt<16>(-rhs_imm)) {
2821 if (rhs_imm == 0) {
2822 if (cond == kCondEQ) {
2823 __ Sltiu(dst, lhs, 1);
2824 } else {
2825 __ Sltu(dst, ZERO, lhs);
2826 }
2827 } else {
2828 __ Addiu(dst, lhs, -rhs_imm);
2829 if (cond == kCondEQ) {
2830 __ Sltiu(dst, dst, 1);
2831 } else {
2832 __ Sltu(dst, ZERO, dst);
2833 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002834 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002835 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002836 if (use_imm && IsUint<16>(rhs_imm)) {
2837 __ Xori(dst, lhs, rhs_imm);
2838 } else {
2839 if (use_imm) {
2840 rhs_reg = TMP;
2841 __ LoadConst32(rhs_reg, rhs_imm);
2842 }
2843 __ Xor(dst, lhs, rhs_reg);
2844 }
2845 if (cond == kCondEQ) {
2846 __ Sltiu(dst, dst, 1);
2847 } else {
2848 __ Sltu(dst, ZERO, dst);
2849 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002850 }
2851 break;
2852
2853 case kCondLT:
2854 case kCondGE:
2855 if (use_imm && IsInt<16>(rhs_imm)) {
2856 __ Slti(dst, lhs, rhs_imm);
2857 } else {
2858 if (use_imm) {
2859 rhs_reg = TMP;
2860 __ LoadConst32(rhs_reg, rhs_imm);
2861 }
2862 __ Slt(dst, lhs, rhs_reg);
2863 }
2864 if (cond == kCondGE) {
2865 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2866 // only the slt instruction but no sge.
2867 __ Xori(dst, dst, 1);
2868 }
2869 break;
2870
2871 case kCondLE:
2872 case kCondGT:
2873 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2874 // Simulate lhs <= rhs via lhs < rhs + 1.
2875 __ Slti(dst, lhs, rhs_imm + 1);
2876 if (cond == kCondGT) {
2877 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2878 // only the slti instruction but no sgti.
2879 __ Xori(dst, dst, 1);
2880 }
2881 } else {
2882 if (use_imm) {
2883 rhs_reg = TMP;
2884 __ LoadConst32(rhs_reg, rhs_imm);
2885 }
2886 __ Slt(dst, rhs_reg, lhs);
2887 if (cond == kCondLE) {
2888 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2889 // only the slt instruction but no sle.
2890 __ Xori(dst, dst, 1);
2891 }
2892 }
2893 break;
2894
2895 case kCondB:
2896 case kCondAE:
2897 if (use_imm && IsInt<16>(rhs_imm)) {
2898 // Sltiu sign-extends its 16-bit immediate operand before
2899 // the comparison and thus lets us compare directly with
2900 // unsigned values in the ranges [0, 0x7fff] and
2901 // [0xffff8000, 0xffffffff].
2902 __ Sltiu(dst, lhs, rhs_imm);
2903 } else {
2904 if (use_imm) {
2905 rhs_reg = TMP;
2906 __ LoadConst32(rhs_reg, rhs_imm);
2907 }
2908 __ Sltu(dst, lhs, rhs_reg);
2909 }
2910 if (cond == kCondAE) {
2911 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2912 // only the sltu instruction but no sgeu.
2913 __ Xori(dst, dst, 1);
2914 }
2915 break;
2916
2917 case kCondBE:
2918 case kCondA:
2919 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2920 // Simulate lhs <= rhs via lhs < rhs + 1.
2921 // Note that this only works if rhs + 1 does not overflow
2922 // to 0, hence the check above.
2923 // Sltiu sign-extends its 16-bit immediate operand before
2924 // the comparison and thus lets us compare directly with
2925 // unsigned values in the ranges [0, 0x7fff] and
2926 // [0xffff8000, 0xffffffff].
2927 __ Sltiu(dst, lhs, rhs_imm + 1);
2928 if (cond == kCondA) {
2929 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2930 // only the sltiu instruction but no sgtiu.
2931 __ Xori(dst, dst, 1);
2932 }
2933 } else {
2934 if (use_imm) {
2935 rhs_reg = TMP;
2936 __ LoadConst32(rhs_reg, rhs_imm);
2937 }
2938 __ Sltu(dst, rhs_reg, lhs);
2939 if (cond == kCondBE) {
2940 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2941 // only the sltu instruction but no sleu.
2942 __ Xori(dst, dst, 1);
2943 }
2944 }
2945 break;
2946 }
2947}
2948
Alexey Frunze674b9ee2016-09-20 14:54:15 -07002949bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
2950 LocationSummary* input_locations,
2951 Register dst) {
2952 Register lhs = input_locations->InAt(0).AsRegister<Register>();
2953 Location rhs_location = input_locations->InAt(1);
2954 Register rhs_reg = ZERO;
2955 int64_t rhs_imm = 0;
2956 bool use_imm = rhs_location.IsConstant();
2957 if (use_imm) {
2958 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2959 } else {
2960 rhs_reg = rhs_location.AsRegister<Register>();
2961 }
2962
2963 switch (cond) {
2964 case kCondEQ:
2965 case kCondNE:
2966 if (use_imm && IsInt<16>(-rhs_imm)) {
2967 __ Addiu(dst, lhs, -rhs_imm);
2968 } else if (use_imm && IsUint<16>(rhs_imm)) {
2969 __ Xori(dst, lhs, rhs_imm);
2970 } else {
2971 if (use_imm) {
2972 rhs_reg = TMP;
2973 __ LoadConst32(rhs_reg, rhs_imm);
2974 }
2975 __ Xor(dst, lhs, rhs_reg);
2976 }
2977 return (cond == kCondEQ);
2978
2979 case kCondLT:
2980 case kCondGE:
2981 if (use_imm && IsInt<16>(rhs_imm)) {
2982 __ Slti(dst, lhs, rhs_imm);
2983 } else {
2984 if (use_imm) {
2985 rhs_reg = TMP;
2986 __ LoadConst32(rhs_reg, rhs_imm);
2987 }
2988 __ Slt(dst, lhs, rhs_reg);
2989 }
2990 return (cond == kCondGE);
2991
2992 case kCondLE:
2993 case kCondGT:
2994 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2995 // Simulate lhs <= rhs via lhs < rhs + 1.
2996 __ Slti(dst, lhs, rhs_imm + 1);
2997 return (cond == kCondGT);
2998 } else {
2999 if (use_imm) {
3000 rhs_reg = TMP;
3001 __ LoadConst32(rhs_reg, rhs_imm);
3002 }
3003 __ Slt(dst, rhs_reg, lhs);
3004 return (cond == kCondLE);
3005 }
3006
3007 case kCondB:
3008 case kCondAE:
3009 if (use_imm && IsInt<16>(rhs_imm)) {
3010 // Sltiu sign-extends its 16-bit immediate operand before
3011 // the comparison and thus lets us compare directly with
3012 // unsigned values in the ranges [0, 0x7fff] and
3013 // [0xffff8000, 0xffffffff].
3014 __ Sltiu(dst, lhs, rhs_imm);
3015 } else {
3016 if (use_imm) {
3017 rhs_reg = TMP;
3018 __ LoadConst32(rhs_reg, rhs_imm);
3019 }
3020 __ Sltu(dst, lhs, rhs_reg);
3021 }
3022 return (cond == kCondAE);
3023
3024 case kCondBE:
3025 case kCondA:
3026 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3027 // Simulate lhs <= rhs via lhs < rhs + 1.
3028 // Note that this only works if rhs + 1 does not overflow
3029 // to 0, hence the check above.
3030 // Sltiu sign-extends its 16-bit immediate operand before
3031 // the comparison and thus lets us compare directly with
3032 // unsigned values in the ranges [0, 0x7fff] and
3033 // [0xffff8000, 0xffffffff].
3034 __ Sltiu(dst, lhs, rhs_imm + 1);
3035 return (cond == kCondA);
3036 } else {
3037 if (use_imm) {
3038 rhs_reg = TMP;
3039 __ LoadConst32(rhs_reg, rhs_imm);
3040 }
3041 __ Sltu(dst, rhs_reg, lhs);
3042 return (cond == kCondBE);
3043 }
3044 }
3045}
3046
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003047void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3048 LocationSummary* locations,
3049 MipsLabel* label) {
3050 Register lhs = locations->InAt(0).AsRegister<Register>();
3051 Location rhs_location = locations->InAt(1);
3052 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003053 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003054 bool use_imm = rhs_location.IsConstant();
3055 if (use_imm) {
3056 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3057 } else {
3058 rhs_reg = rhs_location.AsRegister<Register>();
3059 }
3060
3061 if (use_imm && rhs_imm == 0) {
3062 switch (cond) {
3063 case kCondEQ:
3064 case kCondBE: // <= 0 if zero
3065 __ Beqz(lhs, label);
3066 break;
3067 case kCondNE:
3068 case kCondA: // > 0 if non-zero
3069 __ Bnez(lhs, label);
3070 break;
3071 case kCondLT:
3072 __ Bltz(lhs, label);
3073 break;
3074 case kCondGE:
3075 __ Bgez(lhs, label);
3076 break;
3077 case kCondLE:
3078 __ Blez(lhs, label);
3079 break;
3080 case kCondGT:
3081 __ Bgtz(lhs, label);
3082 break;
3083 case kCondB: // always false
3084 break;
3085 case kCondAE: // always true
3086 __ B(label);
3087 break;
3088 }
3089 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003090 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3091 if (isR6 || !use_imm) {
3092 if (use_imm) {
3093 rhs_reg = TMP;
3094 __ LoadConst32(rhs_reg, rhs_imm);
3095 }
3096 switch (cond) {
3097 case kCondEQ:
3098 __ Beq(lhs, rhs_reg, label);
3099 break;
3100 case kCondNE:
3101 __ Bne(lhs, rhs_reg, label);
3102 break;
3103 case kCondLT:
3104 __ Blt(lhs, rhs_reg, label);
3105 break;
3106 case kCondGE:
3107 __ Bge(lhs, rhs_reg, label);
3108 break;
3109 case kCondLE:
3110 __ Bge(rhs_reg, lhs, label);
3111 break;
3112 case kCondGT:
3113 __ Blt(rhs_reg, lhs, label);
3114 break;
3115 case kCondB:
3116 __ Bltu(lhs, rhs_reg, label);
3117 break;
3118 case kCondAE:
3119 __ Bgeu(lhs, rhs_reg, label);
3120 break;
3121 case kCondBE:
3122 __ Bgeu(rhs_reg, lhs, label);
3123 break;
3124 case kCondA:
3125 __ Bltu(rhs_reg, lhs, label);
3126 break;
3127 }
3128 } else {
3129 // Special cases for more efficient comparison with constants on R2.
3130 switch (cond) {
3131 case kCondEQ:
3132 __ LoadConst32(TMP, rhs_imm);
3133 __ Beq(lhs, TMP, label);
3134 break;
3135 case kCondNE:
3136 __ LoadConst32(TMP, rhs_imm);
3137 __ Bne(lhs, TMP, label);
3138 break;
3139 case kCondLT:
3140 if (IsInt<16>(rhs_imm)) {
3141 __ Slti(TMP, lhs, rhs_imm);
3142 __ Bnez(TMP, label);
3143 } else {
3144 __ LoadConst32(TMP, rhs_imm);
3145 __ Blt(lhs, TMP, label);
3146 }
3147 break;
3148 case kCondGE:
3149 if (IsInt<16>(rhs_imm)) {
3150 __ Slti(TMP, lhs, rhs_imm);
3151 __ Beqz(TMP, label);
3152 } else {
3153 __ LoadConst32(TMP, rhs_imm);
3154 __ Bge(lhs, TMP, label);
3155 }
3156 break;
3157 case kCondLE:
3158 if (IsInt<16>(rhs_imm + 1)) {
3159 // Simulate lhs <= rhs via lhs < rhs + 1.
3160 __ Slti(TMP, lhs, rhs_imm + 1);
3161 __ Bnez(TMP, label);
3162 } else {
3163 __ LoadConst32(TMP, rhs_imm);
3164 __ Bge(TMP, lhs, label);
3165 }
3166 break;
3167 case kCondGT:
3168 if (IsInt<16>(rhs_imm + 1)) {
3169 // Simulate lhs > rhs via !(lhs < rhs + 1).
3170 __ Slti(TMP, lhs, rhs_imm + 1);
3171 __ Beqz(TMP, label);
3172 } else {
3173 __ LoadConst32(TMP, rhs_imm);
3174 __ Blt(TMP, lhs, label);
3175 }
3176 break;
3177 case kCondB:
3178 if (IsInt<16>(rhs_imm)) {
3179 __ Sltiu(TMP, lhs, rhs_imm);
3180 __ Bnez(TMP, label);
3181 } else {
3182 __ LoadConst32(TMP, rhs_imm);
3183 __ Bltu(lhs, TMP, label);
3184 }
3185 break;
3186 case kCondAE:
3187 if (IsInt<16>(rhs_imm)) {
3188 __ Sltiu(TMP, lhs, rhs_imm);
3189 __ Beqz(TMP, label);
3190 } else {
3191 __ LoadConst32(TMP, rhs_imm);
3192 __ Bgeu(lhs, TMP, label);
3193 }
3194 break;
3195 case kCondBE:
3196 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3197 // Simulate lhs <= rhs via lhs < rhs + 1.
3198 // Note that this only works if rhs + 1 does not overflow
3199 // to 0, hence the check above.
3200 __ Sltiu(TMP, lhs, rhs_imm + 1);
3201 __ Bnez(TMP, label);
3202 } else {
3203 __ LoadConst32(TMP, rhs_imm);
3204 __ Bgeu(TMP, lhs, label);
3205 }
3206 break;
3207 case kCondA:
3208 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3209 // Simulate lhs > rhs via !(lhs < rhs + 1).
3210 // Note that this only works if rhs + 1 does not overflow
3211 // to 0, hence the check above.
3212 __ Sltiu(TMP, lhs, rhs_imm + 1);
3213 __ Beqz(TMP, label);
3214 } else {
3215 __ LoadConst32(TMP, rhs_imm);
3216 __ Bltu(TMP, lhs, label);
3217 }
3218 break;
3219 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003220 }
3221 }
3222}
3223
3224void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3225 LocationSummary* locations,
3226 MipsLabel* label) {
3227 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3228 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3229 Location rhs_location = locations->InAt(1);
3230 Register rhs_high = ZERO;
3231 Register rhs_low = ZERO;
3232 int64_t imm = 0;
3233 uint32_t imm_high = 0;
3234 uint32_t imm_low = 0;
3235 bool use_imm = rhs_location.IsConstant();
3236 if (use_imm) {
3237 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3238 imm_high = High32Bits(imm);
3239 imm_low = Low32Bits(imm);
3240 } else {
3241 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3242 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3243 }
3244
3245 if (use_imm && imm == 0) {
3246 switch (cond) {
3247 case kCondEQ:
3248 case kCondBE: // <= 0 if zero
3249 __ Or(TMP, lhs_high, lhs_low);
3250 __ Beqz(TMP, label);
3251 break;
3252 case kCondNE:
3253 case kCondA: // > 0 if non-zero
3254 __ Or(TMP, lhs_high, lhs_low);
3255 __ Bnez(TMP, label);
3256 break;
3257 case kCondLT:
3258 __ Bltz(lhs_high, label);
3259 break;
3260 case kCondGE:
3261 __ Bgez(lhs_high, label);
3262 break;
3263 case kCondLE:
3264 __ Or(TMP, lhs_high, lhs_low);
3265 __ Sra(AT, lhs_high, 31);
3266 __ Bgeu(AT, TMP, label);
3267 break;
3268 case kCondGT:
3269 __ Or(TMP, lhs_high, lhs_low);
3270 __ Sra(AT, lhs_high, 31);
3271 __ Bltu(AT, TMP, label);
3272 break;
3273 case kCondB: // always false
3274 break;
3275 case kCondAE: // always true
3276 __ B(label);
3277 break;
3278 }
3279 } else if (use_imm) {
3280 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3281 switch (cond) {
3282 case kCondEQ:
3283 __ LoadConst32(TMP, imm_high);
3284 __ Xor(TMP, TMP, lhs_high);
3285 __ LoadConst32(AT, imm_low);
3286 __ Xor(AT, AT, lhs_low);
3287 __ Or(TMP, TMP, AT);
3288 __ Beqz(TMP, label);
3289 break;
3290 case kCondNE:
3291 __ LoadConst32(TMP, imm_high);
3292 __ Xor(TMP, TMP, lhs_high);
3293 __ LoadConst32(AT, imm_low);
3294 __ Xor(AT, AT, lhs_low);
3295 __ Or(TMP, TMP, AT);
3296 __ Bnez(TMP, label);
3297 break;
3298 case kCondLT:
3299 __ LoadConst32(TMP, imm_high);
3300 __ Blt(lhs_high, TMP, label);
3301 __ Slt(TMP, TMP, lhs_high);
3302 __ LoadConst32(AT, imm_low);
3303 __ Sltu(AT, lhs_low, AT);
3304 __ Blt(TMP, AT, label);
3305 break;
3306 case kCondGE:
3307 __ LoadConst32(TMP, imm_high);
3308 __ Blt(TMP, lhs_high, label);
3309 __ Slt(TMP, lhs_high, TMP);
3310 __ LoadConst32(AT, imm_low);
3311 __ Sltu(AT, lhs_low, AT);
3312 __ Or(TMP, TMP, AT);
3313 __ Beqz(TMP, label);
3314 break;
3315 case kCondLE:
3316 __ LoadConst32(TMP, imm_high);
3317 __ Blt(lhs_high, TMP, label);
3318 __ Slt(TMP, TMP, lhs_high);
3319 __ LoadConst32(AT, imm_low);
3320 __ Sltu(AT, AT, lhs_low);
3321 __ Or(TMP, TMP, AT);
3322 __ Beqz(TMP, label);
3323 break;
3324 case kCondGT:
3325 __ LoadConst32(TMP, imm_high);
3326 __ Blt(TMP, lhs_high, label);
3327 __ Slt(TMP, lhs_high, TMP);
3328 __ LoadConst32(AT, imm_low);
3329 __ Sltu(AT, AT, lhs_low);
3330 __ Blt(TMP, AT, label);
3331 break;
3332 case kCondB:
3333 __ LoadConst32(TMP, imm_high);
3334 __ Bltu(lhs_high, TMP, label);
3335 __ Sltu(TMP, TMP, lhs_high);
3336 __ LoadConst32(AT, imm_low);
3337 __ Sltu(AT, lhs_low, AT);
3338 __ Blt(TMP, AT, label);
3339 break;
3340 case kCondAE:
3341 __ LoadConst32(TMP, imm_high);
3342 __ Bltu(TMP, lhs_high, label);
3343 __ Sltu(TMP, lhs_high, TMP);
3344 __ LoadConst32(AT, imm_low);
3345 __ Sltu(AT, lhs_low, AT);
3346 __ Or(TMP, TMP, AT);
3347 __ Beqz(TMP, label);
3348 break;
3349 case kCondBE:
3350 __ LoadConst32(TMP, imm_high);
3351 __ Bltu(lhs_high, TMP, label);
3352 __ Sltu(TMP, TMP, lhs_high);
3353 __ LoadConst32(AT, imm_low);
3354 __ Sltu(AT, AT, lhs_low);
3355 __ Or(TMP, TMP, AT);
3356 __ Beqz(TMP, label);
3357 break;
3358 case kCondA:
3359 __ LoadConst32(TMP, imm_high);
3360 __ Bltu(TMP, lhs_high, label);
3361 __ Sltu(TMP, lhs_high, TMP);
3362 __ LoadConst32(AT, imm_low);
3363 __ Sltu(AT, AT, lhs_low);
3364 __ Blt(TMP, AT, label);
3365 break;
3366 }
3367 } else {
3368 switch (cond) {
3369 case kCondEQ:
3370 __ Xor(TMP, lhs_high, rhs_high);
3371 __ Xor(AT, lhs_low, rhs_low);
3372 __ Or(TMP, TMP, AT);
3373 __ Beqz(TMP, label);
3374 break;
3375 case kCondNE:
3376 __ Xor(TMP, lhs_high, rhs_high);
3377 __ Xor(AT, lhs_low, rhs_low);
3378 __ Or(TMP, TMP, AT);
3379 __ Bnez(TMP, label);
3380 break;
3381 case kCondLT:
3382 __ Blt(lhs_high, rhs_high, label);
3383 __ Slt(TMP, rhs_high, lhs_high);
3384 __ Sltu(AT, lhs_low, rhs_low);
3385 __ Blt(TMP, AT, label);
3386 break;
3387 case kCondGE:
3388 __ Blt(rhs_high, lhs_high, label);
3389 __ Slt(TMP, lhs_high, rhs_high);
3390 __ Sltu(AT, lhs_low, rhs_low);
3391 __ Or(TMP, TMP, AT);
3392 __ Beqz(TMP, label);
3393 break;
3394 case kCondLE:
3395 __ Blt(lhs_high, rhs_high, label);
3396 __ Slt(TMP, rhs_high, lhs_high);
3397 __ Sltu(AT, rhs_low, lhs_low);
3398 __ Or(TMP, TMP, AT);
3399 __ Beqz(TMP, label);
3400 break;
3401 case kCondGT:
3402 __ Blt(rhs_high, lhs_high, label);
3403 __ Slt(TMP, lhs_high, rhs_high);
3404 __ Sltu(AT, rhs_low, lhs_low);
3405 __ Blt(TMP, AT, label);
3406 break;
3407 case kCondB:
3408 __ Bltu(lhs_high, rhs_high, label);
3409 __ Sltu(TMP, rhs_high, lhs_high);
3410 __ Sltu(AT, lhs_low, rhs_low);
3411 __ Blt(TMP, AT, label);
3412 break;
3413 case kCondAE:
3414 __ Bltu(rhs_high, lhs_high, label);
3415 __ Sltu(TMP, lhs_high, rhs_high);
3416 __ Sltu(AT, lhs_low, rhs_low);
3417 __ Or(TMP, TMP, AT);
3418 __ Beqz(TMP, label);
3419 break;
3420 case kCondBE:
3421 __ Bltu(lhs_high, rhs_high, label);
3422 __ Sltu(TMP, rhs_high, lhs_high);
3423 __ Sltu(AT, rhs_low, lhs_low);
3424 __ Or(TMP, TMP, AT);
3425 __ Beqz(TMP, label);
3426 break;
3427 case kCondA:
3428 __ Bltu(rhs_high, lhs_high, label);
3429 __ Sltu(TMP, lhs_high, rhs_high);
3430 __ Sltu(AT, rhs_low, lhs_low);
3431 __ Blt(TMP, AT, label);
3432 break;
3433 }
3434 }
3435}
3436
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003437void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3438 bool gt_bias,
3439 Primitive::Type type,
3440 LocationSummary* locations) {
3441 Register dst = locations->Out().AsRegister<Register>();
3442 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3443 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3444 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3445 if (type == Primitive::kPrimFloat) {
3446 if (isR6) {
3447 switch (cond) {
3448 case kCondEQ:
3449 __ CmpEqS(FTMP, lhs, rhs);
3450 __ Mfc1(dst, FTMP);
3451 __ Andi(dst, dst, 1);
3452 break;
3453 case kCondNE:
3454 __ CmpEqS(FTMP, lhs, rhs);
3455 __ Mfc1(dst, FTMP);
3456 __ Addiu(dst, dst, 1);
3457 break;
3458 case kCondLT:
3459 if (gt_bias) {
3460 __ CmpLtS(FTMP, lhs, rhs);
3461 } else {
3462 __ CmpUltS(FTMP, lhs, rhs);
3463 }
3464 __ Mfc1(dst, FTMP);
3465 __ Andi(dst, dst, 1);
3466 break;
3467 case kCondLE:
3468 if (gt_bias) {
3469 __ CmpLeS(FTMP, lhs, rhs);
3470 } else {
3471 __ CmpUleS(FTMP, lhs, rhs);
3472 }
3473 __ Mfc1(dst, FTMP);
3474 __ Andi(dst, dst, 1);
3475 break;
3476 case kCondGT:
3477 if (gt_bias) {
3478 __ CmpUltS(FTMP, rhs, lhs);
3479 } else {
3480 __ CmpLtS(FTMP, rhs, lhs);
3481 }
3482 __ Mfc1(dst, FTMP);
3483 __ Andi(dst, dst, 1);
3484 break;
3485 case kCondGE:
3486 if (gt_bias) {
3487 __ CmpUleS(FTMP, rhs, lhs);
3488 } else {
3489 __ CmpLeS(FTMP, rhs, lhs);
3490 }
3491 __ Mfc1(dst, FTMP);
3492 __ Andi(dst, dst, 1);
3493 break;
3494 default:
3495 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3496 UNREACHABLE();
3497 }
3498 } else {
3499 switch (cond) {
3500 case kCondEQ:
3501 __ CeqS(0, lhs, rhs);
3502 __ LoadConst32(dst, 1);
3503 __ Movf(dst, ZERO, 0);
3504 break;
3505 case kCondNE:
3506 __ CeqS(0, lhs, rhs);
3507 __ LoadConst32(dst, 1);
3508 __ Movt(dst, ZERO, 0);
3509 break;
3510 case kCondLT:
3511 if (gt_bias) {
3512 __ ColtS(0, lhs, rhs);
3513 } else {
3514 __ CultS(0, lhs, rhs);
3515 }
3516 __ LoadConst32(dst, 1);
3517 __ Movf(dst, ZERO, 0);
3518 break;
3519 case kCondLE:
3520 if (gt_bias) {
3521 __ ColeS(0, lhs, rhs);
3522 } else {
3523 __ CuleS(0, lhs, rhs);
3524 }
3525 __ LoadConst32(dst, 1);
3526 __ Movf(dst, ZERO, 0);
3527 break;
3528 case kCondGT:
3529 if (gt_bias) {
3530 __ CultS(0, rhs, lhs);
3531 } else {
3532 __ ColtS(0, rhs, lhs);
3533 }
3534 __ LoadConst32(dst, 1);
3535 __ Movf(dst, ZERO, 0);
3536 break;
3537 case kCondGE:
3538 if (gt_bias) {
3539 __ CuleS(0, rhs, lhs);
3540 } else {
3541 __ ColeS(0, rhs, lhs);
3542 }
3543 __ LoadConst32(dst, 1);
3544 __ Movf(dst, ZERO, 0);
3545 break;
3546 default:
3547 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3548 UNREACHABLE();
3549 }
3550 }
3551 } else {
3552 DCHECK_EQ(type, Primitive::kPrimDouble);
3553 if (isR6) {
3554 switch (cond) {
3555 case kCondEQ:
3556 __ CmpEqD(FTMP, lhs, rhs);
3557 __ Mfc1(dst, FTMP);
3558 __ Andi(dst, dst, 1);
3559 break;
3560 case kCondNE:
3561 __ CmpEqD(FTMP, lhs, rhs);
3562 __ Mfc1(dst, FTMP);
3563 __ Addiu(dst, dst, 1);
3564 break;
3565 case kCondLT:
3566 if (gt_bias) {
3567 __ CmpLtD(FTMP, lhs, rhs);
3568 } else {
3569 __ CmpUltD(FTMP, lhs, rhs);
3570 }
3571 __ Mfc1(dst, FTMP);
3572 __ Andi(dst, dst, 1);
3573 break;
3574 case kCondLE:
3575 if (gt_bias) {
3576 __ CmpLeD(FTMP, lhs, rhs);
3577 } else {
3578 __ CmpUleD(FTMP, lhs, rhs);
3579 }
3580 __ Mfc1(dst, FTMP);
3581 __ Andi(dst, dst, 1);
3582 break;
3583 case kCondGT:
3584 if (gt_bias) {
3585 __ CmpUltD(FTMP, rhs, lhs);
3586 } else {
3587 __ CmpLtD(FTMP, rhs, lhs);
3588 }
3589 __ Mfc1(dst, FTMP);
3590 __ Andi(dst, dst, 1);
3591 break;
3592 case kCondGE:
3593 if (gt_bias) {
3594 __ CmpUleD(FTMP, rhs, lhs);
3595 } else {
3596 __ CmpLeD(FTMP, rhs, lhs);
3597 }
3598 __ Mfc1(dst, FTMP);
3599 __ Andi(dst, dst, 1);
3600 break;
3601 default:
3602 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3603 UNREACHABLE();
3604 }
3605 } else {
3606 switch (cond) {
3607 case kCondEQ:
3608 __ CeqD(0, lhs, rhs);
3609 __ LoadConst32(dst, 1);
3610 __ Movf(dst, ZERO, 0);
3611 break;
3612 case kCondNE:
3613 __ CeqD(0, lhs, rhs);
3614 __ LoadConst32(dst, 1);
3615 __ Movt(dst, ZERO, 0);
3616 break;
3617 case kCondLT:
3618 if (gt_bias) {
3619 __ ColtD(0, lhs, rhs);
3620 } else {
3621 __ CultD(0, lhs, rhs);
3622 }
3623 __ LoadConst32(dst, 1);
3624 __ Movf(dst, ZERO, 0);
3625 break;
3626 case kCondLE:
3627 if (gt_bias) {
3628 __ ColeD(0, lhs, rhs);
3629 } else {
3630 __ CuleD(0, lhs, rhs);
3631 }
3632 __ LoadConst32(dst, 1);
3633 __ Movf(dst, ZERO, 0);
3634 break;
3635 case kCondGT:
3636 if (gt_bias) {
3637 __ CultD(0, rhs, lhs);
3638 } else {
3639 __ ColtD(0, rhs, lhs);
3640 }
3641 __ LoadConst32(dst, 1);
3642 __ Movf(dst, ZERO, 0);
3643 break;
3644 case kCondGE:
3645 if (gt_bias) {
3646 __ CuleD(0, rhs, lhs);
3647 } else {
3648 __ ColeD(0, rhs, lhs);
3649 }
3650 __ LoadConst32(dst, 1);
3651 __ Movf(dst, ZERO, 0);
3652 break;
3653 default:
3654 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3655 UNREACHABLE();
3656 }
3657 }
3658 }
3659}
3660
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003661bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3662 bool gt_bias,
3663 Primitive::Type type,
3664 LocationSummary* input_locations,
3665 int cc) {
3666 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3667 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3668 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3669 if (type == Primitive::kPrimFloat) {
3670 switch (cond) {
3671 case kCondEQ:
3672 __ CeqS(cc, lhs, rhs);
3673 return false;
3674 case kCondNE:
3675 __ CeqS(cc, lhs, rhs);
3676 return true;
3677 case kCondLT:
3678 if (gt_bias) {
3679 __ ColtS(cc, lhs, rhs);
3680 } else {
3681 __ CultS(cc, lhs, rhs);
3682 }
3683 return false;
3684 case kCondLE:
3685 if (gt_bias) {
3686 __ ColeS(cc, lhs, rhs);
3687 } else {
3688 __ CuleS(cc, lhs, rhs);
3689 }
3690 return false;
3691 case kCondGT:
3692 if (gt_bias) {
3693 __ CultS(cc, rhs, lhs);
3694 } else {
3695 __ ColtS(cc, rhs, lhs);
3696 }
3697 return false;
3698 case kCondGE:
3699 if (gt_bias) {
3700 __ CuleS(cc, rhs, lhs);
3701 } else {
3702 __ ColeS(cc, rhs, lhs);
3703 }
3704 return false;
3705 default:
3706 LOG(FATAL) << "Unexpected non-floating-point condition";
3707 UNREACHABLE();
3708 }
3709 } else {
3710 DCHECK_EQ(type, Primitive::kPrimDouble);
3711 switch (cond) {
3712 case kCondEQ:
3713 __ CeqD(cc, lhs, rhs);
3714 return false;
3715 case kCondNE:
3716 __ CeqD(cc, lhs, rhs);
3717 return true;
3718 case kCondLT:
3719 if (gt_bias) {
3720 __ ColtD(cc, lhs, rhs);
3721 } else {
3722 __ CultD(cc, lhs, rhs);
3723 }
3724 return false;
3725 case kCondLE:
3726 if (gt_bias) {
3727 __ ColeD(cc, lhs, rhs);
3728 } else {
3729 __ CuleD(cc, lhs, rhs);
3730 }
3731 return false;
3732 case kCondGT:
3733 if (gt_bias) {
3734 __ CultD(cc, rhs, lhs);
3735 } else {
3736 __ ColtD(cc, rhs, lhs);
3737 }
3738 return false;
3739 case kCondGE:
3740 if (gt_bias) {
3741 __ CuleD(cc, rhs, lhs);
3742 } else {
3743 __ ColeD(cc, rhs, lhs);
3744 }
3745 return false;
3746 default:
3747 LOG(FATAL) << "Unexpected non-floating-point condition";
3748 UNREACHABLE();
3749 }
3750 }
3751}
3752
3753bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
3754 bool gt_bias,
3755 Primitive::Type type,
3756 LocationSummary* input_locations,
3757 FRegister dst) {
3758 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3759 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3760 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
3761 if (type == Primitive::kPrimFloat) {
3762 switch (cond) {
3763 case kCondEQ:
3764 __ CmpEqS(dst, lhs, rhs);
3765 return false;
3766 case kCondNE:
3767 __ CmpEqS(dst, lhs, rhs);
3768 return true;
3769 case kCondLT:
3770 if (gt_bias) {
3771 __ CmpLtS(dst, lhs, rhs);
3772 } else {
3773 __ CmpUltS(dst, lhs, rhs);
3774 }
3775 return false;
3776 case kCondLE:
3777 if (gt_bias) {
3778 __ CmpLeS(dst, lhs, rhs);
3779 } else {
3780 __ CmpUleS(dst, lhs, rhs);
3781 }
3782 return false;
3783 case kCondGT:
3784 if (gt_bias) {
3785 __ CmpUltS(dst, rhs, lhs);
3786 } else {
3787 __ CmpLtS(dst, rhs, lhs);
3788 }
3789 return false;
3790 case kCondGE:
3791 if (gt_bias) {
3792 __ CmpUleS(dst, rhs, lhs);
3793 } else {
3794 __ CmpLeS(dst, rhs, lhs);
3795 }
3796 return false;
3797 default:
3798 LOG(FATAL) << "Unexpected non-floating-point condition";
3799 UNREACHABLE();
3800 }
3801 } else {
3802 DCHECK_EQ(type, Primitive::kPrimDouble);
3803 switch (cond) {
3804 case kCondEQ:
3805 __ CmpEqD(dst, lhs, rhs);
3806 return false;
3807 case kCondNE:
3808 __ CmpEqD(dst, lhs, rhs);
3809 return true;
3810 case kCondLT:
3811 if (gt_bias) {
3812 __ CmpLtD(dst, lhs, rhs);
3813 } else {
3814 __ CmpUltD(dst, lhs, rhs);
3815 }
3816 return false;
3817 case kCondLE:
3818 if (gt_bias) {
3819 __ CmpLeD(dst, lhs, rhs);
3820 } else {
3821 __ CmpUleD(dst, lhs, rhs);
3822 }
3823 return false;
3824 case kCondGT:
3825 if (gt_bias) {
3826 __ CmpUltD(dst, rhs, lhs);
3827 } else {
3828 __ CmpLtD(dst, rhs, lhs);
3829 }
3830 return false;
3831 case kCondGE:
3832 if (gt_bias) {
3833 __ CmpUleD(dst, rhs, lhs);
3834 } else {
3835 __ CmpLeD(dst, rhs, lhs);
3836 }
3837 return false;
3838 default:
3839 LOG(FATAL) << "Unexpected non-floating-point condition";
3840 UNREACHABLE();
3841 }
3842 }
3843}
3844
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003845void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3846 bool gt_bias,
3847 Primitive::Type type,
3848 LocationSummary* locations,
3849 MipsLabel* label) {
3850 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3851 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3852 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3853 if (type == Primitive::kPrimFloat) {
3854 if (isR6) {
3855 switch (cond) {
3856 case kCondEQ:
3857 __ CmpEqS(FTMP, lhs, rhs);
3858 __ Bc1nez(FTMP, label);
3859 break;
3860 case kCondNE:
3861 __ CmpEqS(FTMP, lhs, rhs);
3862 __ Bc1eqz(FTMP, label);
3863 break;
3864 case kCondLT:
3865 if (gt_bias) {
3866 __ CmpLtS(FTMP, lhs, rhs);
3867 } else {
3868 __ CmpUltS(FTMP, lhs, rhs);
3869 }
3870 __ Bc1nez(FTMP, label);
3871 break;
3872 case kCondLE:
3873 if (gt_bias) {
3874 __ CmpLeS(FTMP, lhs, rhs);
3875 } else {
3876 __ CmpUleS(FTMP, lhs, rhs);
3877 }
3878 __ Bc1nez(FTMP, label);
3879 break;
3880 case kCondGT:
3881 if (gt_bias) {
3882 __ CmpUltS(FTMP, rhs, lhs);
3883 } else {
3884 __ CmpLtS(FTMP, rhs, lhs);
3885 }
3886 __ Bc1nez(FTMP, label);
3887 break;
3888 case kCondGE:
3889 if (gt_bias) {
3890 __ CmpUleS(FTMP, rhs, lhs);
3891 } else {
3892 __ CmpLeS(FTMP, rhs, lhs);
3893 }
3894 __ Bc1nez(FTMP, label);
3895 break;
3896 default:
3897 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003898 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003899 }
3900 } else {
3901 switch (cond) {
3902 case kCondEQ:
3903 __ CeqS(0, lhs, rhs);
3904 __ Bc1t(0, label);
3905 break;
3906 case kCondNE:
3907 __ CeqS(0, lhs, rhs);
3908 __ Bc1f(0, label);
3909 break;
3910 case kCondLT:
3911 if (gt_bias) {
3912 __ ColtS(0, lhs, rhs);
3913 } else {
3914 __ CultS(0, lhs, rhs);
3915 }
3916 __ Bc1t(0, label);
3917 break;
3918 case kCondLE:
3919 if (gt_bias) {
3920 __ ColeS(0, lhs, rhs);
3921 } else {
3922 __ CuleS(0, lhs, rhs);
3923 }
3924 __ Bc1t(0, label);
3925 break;
3926 case kCondGT:
3927 if (gt_bias) {
3928 __ CultS(0, rhs, lhs);
3929 } else {
3930 __ ColtS(0, rhs, lhs);
3931 }
3932 __ Bc1t(0, label);
3933 break;
3934 case kCondGE:
3935 if (gt_bias) {
3936 __ CuleS(0, rhs, lhs);
3937 } else {
3938 __ ColeS(0, rhs, lhs);
3939 }
3940 __ Bc1t(0, label);
3941 break;
3942 default:
3943 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003944 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003945 }
3946 }
3947 } else {
3948 DCHECK_EQ(type, Primitive::kPrimDouble);
3949 if (isR6) {
3950 switch (cond) {
3951 case kCondEQ:
3952 __ CmpEqD(FTMP, lhs, rhs);
3953 __ Bc1nez(FTMP, label);
3954 break;
3955 case kCondNE:
3956 __ CmpEqD(FTMP, lhs, rhs);
3957 __ Bc1eqz(FTMP, label);
3958 break;
3959 case kCondLT:
3960 if (gt_bias) {
3961 __ CmpLtD(FTMP, lhs, rhs);
3962 } else {
3963 __ CmpUltD(FTMP, lhs, rhs);
3964 }
3965 __ Bc1nez(FTMP, label);
3966 break;
3967 case kCondLE:
3968 if (gt_bias) {
3969 __ CmpLeD(FTMP, lhs, rhs);
3970 } else {
3971 __ CmpUleD(FTMP, lhs, rhs);
3972 }
3973 __ Bc1nez(FTMP, label);
3974 break;
3975 case kCondGT:
3976 if (gt_bias) {
3977 __ CmpUltD(FTMP, rhs, lhs);
3978 } else {
3979 __ CmpLtD(FTMP, rhs, lhs);
3980 }
3981 __ Bc1nez(FTMP, label);
3982 break;
3983 case kCondGE:
3984 if (gt_bias) {
3985 __ CmpUleD(FTMP, rhs, lhs);
3986 } else {
3987 __ CmpLeD(FTMP, rhs, lhs);
3988 }
3989 __ Bc1nez(FTMP, label);
3990 break;
3991 default:
3992 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003993 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003994 }
3995 } else {
3996 switch (cond) {
3997 case kCondEQ:
3998 __ CeqD(0, lhs, rhs);
3999 __ Bc1t(0, label);
4000 break;
4001 case kCondNE:
4002 __ CeqD(0, lhs, rhs);
4003 __ Bc1f(0, label);
4004 break;
4005 case kCondLT:
4006 if (gt_bias) {
4007 __ ColtD(0, lhs, rhs);
4008 } else {
4009 __ CultD(0, lhs, rhs);
4010 }
4011 __ Bc1t(0, label);
4012 break;
4013 case kCondLE:
4014 if (gt_bias) {
4015 __ ColeD(0, lhs, rhs);
4016 } else {
4017 __ CuleD(0, lhs, rhs);
4018 }
4019 __ Bc1t(0, label);
4020 break;
4021 case kCondGT:
4022 if (gt_bias) {
4023 __ CultD(0, rhs, lhs);
4024 } else {
4025 __ ColtD(0, rhs, lhs);
4026 }
4027 __ Bc1t(0, label);
4028 break;
4029 case kCondGE:
4030 if (gt_bias) {
4031 __ CuleD(0, rhs, lhs);
4032 } else {
4033 __ ColeD(0, rhs, lhs);
4034 }
4035 __ Bc1t(0, label);
4036 break;
4037 default:
4038 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004039 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004040 }
4041 }
4042 }
4043}
4044
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004045void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004046 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004047 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004048 MipsLabel* false_target) {
4049 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004050
David Brazdil0debae72015-11-12 18:37:00 +00004051 if (true_target == nullptr && false_target == nullptr) {
4052 // Nothing to do. The code always falls through.
4053 return;
4054 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004055 // Constant condition, statically compared against "true" (integer value 1).
4056 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004057 if (true_target != nullptr) {
4058 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004059 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004060 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004061 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004062 if (false_target != nullptr) {
4063 __ B(false_target);
4064 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004065 }
David Brazdil0debae72015-11-12 18:37:00 +00004066 return;
4067 }
4068
4069 // The following code generates these patterns:
4070 // (1) true_target == nullptr && false_target != nullptr
4071 // - opposite condition true => branch to false_target
4072 // (2) true_target != nullptr && false_target == nullptr
4073 // - condition true => branch to true_target
4074 // (3) true_target != nullptr && false_target != nullptr
4075 // - condition true => branch to true_target
4076 // - branch to false_target
4077 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004078 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004079 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004080 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004081 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004082 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4083 } else {
4084 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4085 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004086 } else {
4087 // The condition instruction has not been materialized, use its inputs as
4088 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004089 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004090 Primitive::Type type = condition->InputAt(0)->GetType();
4091 LocationSummary* locations = cond->GetLocations();
4092 IfCondition if_cond = condition->GetCondition();
4093 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004094
David Brazdil0debae72015-11-12 18:37:00 +00004095 if (true_target == nullptr) {
4096 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004097 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004098 }
4099
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004100 switch (type) {
4101 default:
4102 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4103 break;
4104 case Primitive::kPrimLong:
4105 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4106 break;
4107 case Primitive::kPrimFloat:
4108 case Primitive::kPrimDouble:
4109 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4110 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004111 }
4112 }
David Brazdil0debae72015-11-12 18:37:00 +00004113
4114 // If neither branch falls through (case 3), the conditional branch to `true_target`
4115 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4116 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004117 __ B(false_target);
4118 }
4119}
4120
4121void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4122 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004123 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004124 locations->SetInAt(0, Location::RequiresRegister());
4125 }
4126}
4127
4128void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004129 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4130 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4131 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4132 nullptr : codegen_->GetLabelOf(true_successor);
4133 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4134 nullptr : codegen_->GetLabelOf(false_successor);
4135 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004136}
4137
4138void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4139 LocationSummary* locations = new (GetGraph()->GetArena())
4140 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004141 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004142 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004143 locations->SetInAt(0, Location::RequiresRegister());
4144 }
4145}
4146
4147void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004148 SlowPathCodeMIPS* slow_path =
4149 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004150 GenerateTestAndBranch(deoptimize,
4151 /* condition_input_index */ 0,
4152 slow_path->GetEntryLabel(),
4153 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004154}
4155
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004156// This function returns true if a conditional move can be generated for HSelect.
4157// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4158// branches and regular moves.
4159//
4160// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4161//
4162// While determining feasibility of a conditional move and setting inputs/outputs
4163// are two distinct tasks, this function does both because they share quite a bit
4164// of common logic.
4165static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4166 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4167 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4168 HCondition* condition = cond->AsCondition();
4169
4170 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4171 Primitive::Type dst_type = select->GetType();
4172
4173 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4174 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4175 bool is_true_value_zero_constant =
4176 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4177 bool is_false_value_zero_constant =
4178 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4179
4180 bool can_move_conditionally = false;
4181 bool use_const_for_false_in = false;
4182 bool use_const_for_true_in = false;
4183
4184 if (!cond->IsConstant()) {
4185 switch (cond_type) {
4186 default:
4187 switch (dst_type) {
4188 default:
4189 // Moving int on int condition.
4190 if (is_r6) {
4191 if (is_true_value_zero_constant) {
4192 // seleqz out_reg, false_reg, cond_reg
4193 can_move_conditionally = true;
4194 use_const_for_true_in = true;
4195 } else if (is_false_value_zero_constant) {
4196 // selnez out_reg, true_reg, cond_reg
4197 can_move_conditionally = true;
4198 use_const_for_false_in = true;
4199 } else if (materialized) {
4200 // Not materializing unmaterialized int conditions
4201 // to keep the instruction count low.
4202 // selnez AT, true_reg, cond_reg
4203 // seleqz TMP, false_reg, cond_reg
4204 // or out_reg, AT, TMP
4205 can_move_conditionally = true;
4206 }
4207 } else {
4208 // movn out_reg, true_reg/ZERO, cond_reg
4209 can_move_conditionally = true;
4210 use_const_for_true_in = is_true_value_zero_constant;
4211 }
4212 break;
4213 case Primitive::kPrimLong:
4214 // Moving long on int condition.
4215 if (is_r6) {
4216 if (is_true_value_zero_constant) {
4217 // seleqz out_reg_lo, false_reg_lo, cond_reg
4218 // seleqz out_reg_hi, false_reg_hi, cond_reg
4219 can_move_conditionally = true;
4220 use_const_for_true_in = true;
4221 } else if (is_false_value_zero_constant) {
4222 // selnez out_reg_lo, true_reg_lo, cond_reg
4223 // selnez out_reg_hi, true_reg_hi, cond_reg
4224 can_move_conditionally = true;
4225 use_const_for_false_in = true;
4226 }
4227 // Other long conditional moves would generate 6+ instructions,
4228 // which is too many.
4229 } else {
4230 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4231 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4232 can_move_conditionally = true;
4233 use_const_for_true_in = is_true_value_zero_constant;
4234 }
4235 break;
4236 case Primitive::kPrimFloat:
4237 case Primitive::kPrimDouble:
4238 // Moving float/double on int condition.
4239 if (is_r6) {
4240 if (materialized) {
4241 // Not materializing unmaterialized int conditions
4242 // to keep the instruction count low.
4243 can_move_conditionally = true;
4244 if (is_true_value_zero_constant) {
4245 // sltu TMP, ZERO, cond_reg
4246 // mtc1 TMP, temp_cond_reg
4247 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4248 use_const_for_true_in = true;
4249 } else if (is_false_value_zero_constant) {
4250 // sltu TMP, ZERO, cond_reg
4251 // mtc1 TMP, temp_cond_reg
4252 // selnez.fmt out_reg, true_reg, temp_cond_reg
4253 use_const_for_false_in = true;
4254 } else {
4255 // sltu TMP, ZERO, cond_reg
4256 // mtc1 TMP, temp_cond_reg
4257 // sel.fmt temp_cond_reg, false_reg, true_reg
4258 // mov.fmt out_reg, temp_cond_reg
4259 }
4260 }
4261 } else {
4262 // movn.fmt out_reg, true_reg, cond_reg
4263 can_move_conditionally = true;
4264 }
4265 break;
4266 }
4267 break;
4268 case Primitive::kPrimLong:
4269 // We don't materialize long comparison now
4270 // and use conditional branches instead.
4271 break;
4272 case Primitive::kPrimFloat:
4273 case Primitive::kPrimDouble:
4274 switch (dst_type) {
4275 default:
4276 // Moving int on float/double condition.
4277 if (is_r6) {
4278 if (is_true_value_zero_constant) {
4279 // mfc1 TMP, temp_cond_reg
4280 // seleqz out_reg, false_reg, TMP
4281 can_move_conditionally = true;
4282 use_const_for_true_in = true;
4283 } else if (is_false_value_zero_constant) {
4284 // mfc1 TMP, temp_cond_reg
4285 // selnez out_reg, true_reg, TMP
4286 can_move_conditionally = true;
4287 use_const_for_false_in = true;
4288 } else {
4289 // mfc1 TMP, temp_cond_reg
4290 // selnez AT, true_reg, TMP
4291 // seleqz TMP, false_reg, TMP
4292 // or out_reg, AT, TMP
4293 can_move_conditionally = true;
4294 }
4295 } else {
4296 // movt out_reg, true_reg/ZERO, cc
4297 can_move_conditionally = true;
4298 use_const_for_true_in = is_true_value_zero_constant;
4299 }
4300 break;
4301 case Primitive::kPrimLong:
4302 // Moving long on float/double condition.
4303 if (is_r6) {
4304 if (is_true_value_zero_constant) {
4305 // mfc1 TMP, temp_cond_reg
4306 // seleqz out_reg_lo, false_reg_lo, TMP
4307 // seleqz out_reg_hi, false_reg_hi, TMP
4308 can_move_conditionally = true;
4309 use_const_for_true_in = true;
4310 } else if (is_false_value_zero_constant) {
4311 // mfc1 TMP, temp_cond_reg
4312 // selnez out_reg_lo, true_reg_lo, TMP
4313 // selnez out_reg_hi, true_reg_hi, TMP
4314 can_move_conditionally = true;
4315 use_const_for_false_in = true;
4316 }
4317 // Other long conditional moves would generate 6+ instructions,
4318 // which is too many.
4319 } else {
4320 // movt out_reg_lo, true_reg_lo/ZERO, cc
4321 // movt out_reg_hi, true_reg_hi/ZERO, cc
4322 can_move_conditionally = true;
4323 use_const_for_true_in = is_true_value_zero_constant;
4324 }
4325 break;
4326 case Primitive::kPrimFloat:
4327 case Primitive::kPrimDouble:
4328 // Moving float/double on float/double condition.
4329 if (is_r6) {
4330 can_move_conditionally = true;
4331 if (is_true_value_zero_constant) {
4332 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4333 use_const_for_true_in = true;
4334 } else if (is_false_value_zero_constant) {
4335 // selnez.fmt out_reg, true_reg, temp_cond_reg
4336 use_const_for_false_in = true;
4337 } else {
4338 // sel.fmt temp_cond_reg, false_reg, true_reg
4339 // mov.fmt out_reg, temp_cond_reg
4340 }
4341 } else {
4342 // movt.fmt out_reg, true_reg, cc
4343 can_move_conditionally = true;
4344 }
4345 break;
4346 }
4347 break;
4348 }
4349 }
4350
4351 if (can_move_conditionally) {
4352 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4353 } else {
4354 DCHECK(!use_const_for_false_in);
4355 DCHECK(!use_const_for_true_in);
4356 }
4357
4358 if (locations_to_set != nullptr) {
4359 if (use_const_for_false_in) {
4360 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4361 } else {
4362 locations_to_set->SetInAt(0,
4363 Primitive::IsFloatingPointType(dst_type)
4364 ? Location::RequiresFpuRegister()
4365 : Location::RequiresRegister());
4366 }
4367 if (use_const_for_true_in) {
4368 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4369 } else {
4370 locations_to_set->SetInAt(1,
4371 Primitive::IsFloatingPointType(dst_type)
4372 ? Location::RequiresFpuRegister()
4373 : Location::RequiresRegister());
4374 }
4375 if (materialized) {
4376 locations_to_set->SetInAt(2, Location::RequiresRegister());
4377 }
4378 // On R6 we don't require the output to be the same as the
4379 // first input for conditional moves unlike on R2.
4380 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4381 if (is_out_same_as_first_in) {
4382 locations_to_set->SetOut(Location::SameAsFirstInput());
4383 } else {
4384 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4385 ? Location::RequiresFpuRegister()
4386 : Location::RequiresRegister());
4387 }
4388 }
4389
4390 return can_move_conditionally;
4391}
4392
4393void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4394 LocationSummary* locations = select->GetLocations();
4395 Location dst = locations->Out();
4396 Location src = locations->InAt(1);
4397 Register src_reg = ZERO;
4398 Register src_reg_high = ZERO;
4399 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4400 Register cond_reg = TMP;
4401 int cond_cc = 0;
4402 Primitive::Type cond_type = Primitive::kPrimInt;
4403 bool cond_inverted = false;
4404 Primitive::Type dst_type = select->GetType();
4405
4406 if (IsBooleanValueOrMaterializedCondition(cond)) {
4407 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4408 } else {
4409 HCondition* condition = cond->AsCondition();
4410 LocationSummary* cond_locations = cond->GetLocations();
4411 IfCondition if_cond = condition->GetCondition();
4412 cond_type = condition->InputAt(0)->GetType();
4413 switch (cond_type) {
4414 default:
4415 DCHECK_NE(cond_type, Primitive::kPrimLong);
4416 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4417 break;
4418 case Primitive::kPrimFloat:
4419 case Primitive::kPrimDouble:
4420 cond_inverted = MaterializeFpCompareR2(if_cond,
4421 condition->IsGtBias(),
4422 cond_type,
4423 cond_locations,
4424 cond_cc);
4425 break;
4426 }
4427 }
4428
4429 DCHECK(dst.Equals(locations->InAt(0)));
4430 if (src.IsRegister()) {
4431 src_reg = src.AsRegister<Register>();
4432 } else if (src.IsRegisterPair()) {
4433 src_reg = src.AsRegisterPairLow<Register>();
4434 src_reg_high = src.AsRegisterPairHigh<Register>();
4435 } else if (src.IsConstant()) {
4436 DCHECK(src.GetConstant()->IsZeroBitPattern());
4437 }
4438
4439 switch (cond_type) {
4440 default:
4441 switch (dst_type) {
4442 default:
4443 if (cond_inverted) {
4444 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4445 } else {
4446 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4447 }
4448 break;
4449 case Primitive::kPrimLong:
4450 if (cond_inverted) {
4451 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4452 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4453 } else {
4454 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4455 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4456 }
4457 break;
4458 case Primitive::kPrimFloat:
4459 if (cond_inverted) {
4460 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4461 } else {
4462 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4463 }
4464 break;
4465 case Primitive::kPrimDouble:
4466 if (cond_inverted) {
4467 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4468 } else {
4469 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4470 }
4471 break;
4472 }
4473 break;
4474 case Primitive::kPrimLong:
4475 LOG(FATAL) << "Unreachable";
4476 UNREACHABLE();
4477 case Primitive::kPrimFloat:
4478 case Primitive::kPrimDouble:
4479 switch (dst_type) {
4480 default:
4481 if (cond_inverted) {
4482 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4483 } else {
4484 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4485 }
4486 break;
4487 case Primitive::kPrimLong:
4488 if (cond_inverted) {
4489 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4490 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4491 } else {
4492 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4493 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4494 }
4495 break;
4496 case Primitive::kPrimFloat:
4497 if (cond_inverted) {
4498 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4499 } else {
4500 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4501 }
4502 break;
4503 case Primitive::kPrimDouble:
4504 if (cond_inverted) {
4505 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4506 } else {
4507 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4508 }
4509 break;
4510 }
4511 break;
4512 }
4513}
4514
4515void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4516 LocationSummary* locations = select->GetLocations();
4517 Location dst = locations->Out();
4518 Location false_src = locations->InAt(0);
4519 Location true_src = locations->InAt(1);
4520 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4521 Register cond_reg = TMP;
4522 FRegister fcond_reg = FTMP;
4523 Primitive::Type cond_type = Primitive::kPrimInt;
4524 bool cond_inverted = false;
4525 Primitive::Type dst_type = select->GetType();
4526
4527 if (IsBooleanValueOrMaterializedCondition(cond)) {
4528 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4529 } else {
4530 HCondition* condition = cond->AsCondition();
4531 LocationSummary* cond_locations = cond->GetLocations();
4532 IfCondition if_cond = condition->GetCondition();
4533 cond_type = condition->InputAt(0)->GetType();
4534 switch (cond_type) {
4535 default:
4536 DCHECK_NE(cond_type, Primitive::kPrimLong);
4537 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4538 break;
4539 case Primitive::kPrimFloat:
4540 case Primitive::kPrimDouble:
4541 cond_inverted = MaterializeFpCompareR6(if_cond,
4542 condition->IsGtBias(),
4543 cond_type,
4544 cond_locations,
4545 fcond_reg);
4546 break;
4547 }
4548 }
4549
4550 if (true_src.IsConstant()) {
4551 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4552 }
4553 if (false_src.IsConstant()) {
4554 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4555 }
4556
4557 switch (dst_type) {
4558 default:
4559 if (Primitive::IsFloatingPointType(cond_type)) {
4560 __ Mfc1(cond_reg, fcond_reg);
4561 }
4562 if (true_src.IsConstant()) {
4563 if (cond_inverted) {
4564 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4565 } else {
4566 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4567 }
4568 } else if (false_src.IsConstant()) {
4569 if (cond_inverted) {
4570 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4571 } else {
4572 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4573 }
4574 } else {
4575 DCHECK_NE(cond_reg, AT);
4576 if (cond_inverted) {
4577 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4578 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4579 } else {
4580 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4581 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4582 }
4583 __ Or(dst.AsRegister<Register>(), AT, TMP);
4584 }
4585 break;
4586 case Primitive::kPrimLong: {
4587 if (Primitive::IsFloatingPointType(cond_type)) {
4588 __ Mfc1(cond_reg, fcond_reg);
4589 }
4590 Register dst_lo = dst.AsRegisterPairLow<Register>();
4591 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4592 if (true_src.IsConstant()) {
4593 Register src_lo = false_src.AsRegisterPairLow<Register>();
4594 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4595 if (cond_inverted) {
4596 __ Selnez(dst_lo, src_lo, cond_reg);
4597 __ Selnez(dst_hi, src_hi, cond_reg);
4598 } else {
4599 __ Seleqz(dst_lo, src_lo, cond_reg);
4600 __ Seleqz(dst_hi, src_hi, cond_reg);
4601 }
4602 } else {
4603 DCHECK(false_src.IsConstant());
4604 Register src_lo = true_src.AsRegisterPairLow<Register>();
4605 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4606 if (cond_inverted) {
4607 __ Seleqz(dst_lo, src_lo, cond_reg);
4608 __ Seleqz(dst_hi, src_hi, cond_reg);
4609 } else {
4610 __ Selnez(dst_lo, src_lo, cond_reg);
4611 __ Selnez(dst_hi, src_hi, cond_reg);
4612 }
4613 }
4614 break;
4615 }
4616 case Primitive::kPrimFloat: {
4617 if (!Primitive::IsFloatingPointType(cond_type)) {
4618 // sel*.fmt tests bit 0 of the condition register, account for that.
4619 __ Sltu(TMP, ZERO, cond_reg);
4620 __ Mtc1(TMP, fcond_reg);
4621 }
4622 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4623 if (true_src.IsConstant()) {
4624 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4625 if (cond_inverted) {
4626 __ SelnezS(dst_reg, src_reg, fcond_reg);
4627 } else {
4628 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4629 }
4630 } else if (false_src.IsConstant()) {
4631 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4632 if (cond_inverted) {
4633 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4634 } else {
4635 __ SelnezS(dst_reg, src_reg, fcond_reg);
4636 }
4637 } else {
4638 if (cond_inverted) {
4639 __ SelS(fcond_reg,
4640 true_src.AsFpuRegister<FRegister>(),
4641 false_src.AsFpuRegister<FRegister>());
4642 } else {
4643 __ SelS(fcond_reg,
4644 false_src.AsFpuRegister<FRegister>(),
4645 true_src.AsFpuRegister<FRegister>());
4646 }
4647 __ MovS(dst_reg, fcond_reg);
4648 }
4649 break;
4650 }
4651 case Primitive::kPrimDouble: {
4652 if (!Primitive::IsFloatingPointType(cond_type)) {
4653 // sel*.fmt tests bit 0 of the condition register, account for that.
4654 __ Sltu(TMP, ZERO, cond_reg);
4655 __ Mtc1(TMP, fcond_reg);
4656 }
4657 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4658 if (true_src.IsConstant()) {
4659 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4660 if (cond_inverted) {
4661 __ SelnezD(dst_reg, src_reg, fcond_reg);
4662 } else {
4663 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4664 }
4665 } else if (false_src.IsConstant()) {
4666 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4667 if (cond_inverted) {
4668 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4669 } else {
4670 __ SelnezD(dst_reg, src_reg, fcond_reg);
4671 }
4672 } else {
4673 if (cond_inverted) {
4674 __ SelD(fcond_reg,
4675 true_src.AsFpuRegister<FRegister>(),
4676 false_src.AsFpuRegister<FRegister>());
4677 } else {
4678 __ SelD(fcond_reg,
4679 false_src.AsFpuRegister<FRegister>(),
4680 true_src.AsFpuRegister<FRegister>());
4681 }
4682 __ MovD(dst_reg, fcond_reg);
4683 }
4684 break;
4685 }
4686 }
4687}
4688
David Brazdil74eb1b22015-12-14 11:44:01 +00004689void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4690 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004691 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004692}
4693
4694void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004695 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
4696 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
4697 if (is_r6) {
4698 GenConditionalMoveR6(select);
4699 } else {
4700 GenConditionalMoveR2(select);
4701 }
4702 } else {
4703 LocationSummary* locations = select->GetLocations();
4704 MipsLabel false_target;
4705 GenerateTestAndBranch(select,
4706 /* condition_input_index */ 2,
4707 /* true_target */ nullptr,
4708 &false_target);
4709 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4710 __ Bind(&false_target);
4711 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004712}
4713
David Srbecky0cf44932015-12-09 14:09:59 +00004714void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4715 new (GetGraph()->GetArena()) LocationSummary(info);
4716}
4717
David Srbeckyd28f4a02016-03-14 17:14:24 +00004718void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
4719 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004720}
4721
4722void CodeGeneratorMIPS::GenerateNop() {
4723 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004724}
4725
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004726void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4727 Primitive::Type field_type = field_info.GetFieldType();
4728 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4729 bool generate_volatile = field_info.IsVolatile() && is_wide;
4730 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004731 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004732
4733 locations->SetInAt(0, Location::RequiresRegister());
4734 if (generate_volatile) {
4735 InvokeRuntimeCallingConvention calling_convention;
4736 // need A0 to hold base + offset
4737 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4738 if (field_type == Primitive::kPrimLong) {
4739 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
4740 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004741 // Use Location::Any() to prevent situations when running out of available fp registers.
4742 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004743 // Need some temp core regs since FP results are returned in core registers
4744 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
4745 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
4746 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
4747 }
4748 } else {
4749 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4750 locations->SetOut(Location::RequiresFpuRegister());
4751 } else {
4752 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4753 }
4754 }
4755}
4756
4757void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
4758 const FieldInfo& field_info,
4759 uint32_t dex_pc) {
4760 Primitive::Type type = field_info.GetFieldType();
4761 LocationSummary* locations = instruction->GetLocations();
4762 Register obj = locations->InAt(0).AsRegister<Register>();
4763 LoadOperandType load_type = kLoadUnsignedByte;
4764 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004765 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004766 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004767
4768 switch (type) {
4769 case Primitive::kPrimBoolean:
4770 load_type = kLoadUnsignedByte;
4771 break;
4772 case Primitive::kPrimByte:
4773 load_type = kLoadSignedByte;
4774 break;
4775 case Primitive::kPrimShort:
4776 load_type = kLoadSignedHalfword;
4777 break;
4778 case Primitive::kPrimChar:
4779 load_type = kLoadUnsignedHalfword;
4780 break;
4781 case Primitive::kPrimInt:
4782 case Primitive::kPrimFloat:
4783 case Primitive::kPrimNot:
4784 load_type = kLoadWord;
4785 break;
4786 case Primitive::kPrimLong:
4787 case Primitive::kPrimDouble:
4788 load_type = kLoadDoubleword;
4789 break;
4790 case Primitive::kPrimVoid:
4791 LOG(FATAL) << "Unreachable type " << type;
4792 UNREACHABLE();
4793 }
4794
4795 if (is_volatile && load_type == kLoadDoubleword) {
4796 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004797 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004798 // Do implicit Null check
4799 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4800 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01004801 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004802 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
4803 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004804 // FP results are returned in core registers. Need to move them.
4805 Location out = locations->Out();
4806 if (out.IsFpuRegister()) {
4807 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4808 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4809 out.AsFpuRegister<FRegister>());
4810 } else {
4811 DCHECK(out.IsDoubleStackSlot());
4812 __ StoreToOffset(kStoreWord,
4813 locations->GetTemp(1).AsRegister<Register>(),
4814 SP,
4815 out.GetStackIndex());
4816 __ StoreToOffset(kStoreWord,
4817 locations->GetTemp(2).AsRegister<Register>(),
4818 SP,
4819 out.GetStackIndex() + 4);
4820 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004821 }
4822 } else {
4823 if (!Primitive::IsFloatingPointType(type)) {
4824 Register dst;
4825 if (type == Primitive::kPrimLong) {
4826 DCHECK(locations->Out().IsRegisterPair());
4827 dst = locations->Out().AsRegisterPairLow<Register>();
4828 } else {
4829 DCHECK(locations->Out().IsRegister());
4830 dst = locations->Out().AsRegister<Register>();
4831 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004832 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004833 } else {
4834 DCHECK(locations->Out().IsFpuRegister());
4835 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4836 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004837 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004838 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004839 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004840 }
4841 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004842 }
4843
4844 if (is_volatile) {
4845 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4846 }
4847}
4848
4849void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4850 Primitive::Type field_type = field_info.GetFieldType();
4851 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4852 bool generate_volatile = field_info.IsVolatile() && is_wide;
4853 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004854 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004855
4856 locations->SetInAt(0, Location::RequiresRegister());
4857 if (generate_volatile) {
4858 InvokeRuntimeCallingConvention calling_convention;
4859 // need A0 to hold base + offset
4860 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4861 if (field_type == Primitive::kPrimLong) {
4862 locations->SetInAt(1, Location::RegisterPairLocation(
4863 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4864 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004865 // Use Location::Any() to prevent situations when running out of available fp registers.
4866 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004867 // Pass FP parameters in core registers.
4868 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4869 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4870 }
4871 } else {
4872 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004873 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004874 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004875 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004876 }
4877 }
4878}
4879
4880void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
4881 const FieldInfo& field_info,
4882 uint32_t dex_pc) {
4883 Primitive::Type type = field_info.GetFieldType();
4884 LocationSummary* locations = instruction->GetLocations();
4885 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07004886 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004887 StoreOperandType store_type = kStoreByte;
4888 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004889 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004890 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004891
4892 switch (type) {
4893 case Primitive::kPrimBoolean:
4894 case Primitive::kPrimByte:
4895 store_type = kStoreByte;
4896 break;
4897 case Primitive::kPrimShort:
4898 case Primitive::kPrimChar:
4899 store_type = kStoreHalfword;
4900 break;
4901 case Primitive::kPrimInt:
4902 case Primitive::kPrimFloat:
4903 case Primitive::kPrimNot:
4904 store_type = kStoreWord;
4905 break;
4906 case Primitive::kPrimLong:
4907 case Primitive::kPrimDouble:
4908 store_type = kStoreDoubleword;
4909 break;
4910 case Primitive::kPrimVoid:
4911 LOG(FATAL) << "Unreachable type " << type;
4912 UNREACHABLE();
4913 }
4914
4915 if (is_volatile) {
4916 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4917 }
4918
4919 if (is_volatile && store_type == kStoreDoubleword) {
4920 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004921 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004922 // Do implicit Null check.
4923 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4924 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4925 if (type == Primitive::kPrimDouble) {
4926 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004927 if (value_location.IsFpuRegister()) {
4928 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4929 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004930 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004931 value_location.AsFpuRegister<FRegister>());
4932 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004933 __ LoadFromOffset(kLoadWord,
4934 locations->GetTemp(1).AsRegister<Register>(),
4935 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004936 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004937 __ LoadFromOffset(kLoadWord,
4938 locations->GetTemp(2).AsRegister<Register>(),
4939 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004940 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004941 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004942 DCHECK(value_location.IsConstant());
4943 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4944 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004945 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4946 locations->GetTemp(1).AsRegister<Register>(),
4947 value);
4948 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004949 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004950 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004951 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4952 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004953 if (value_location.IsConstant()) {
4954 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4955 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4956 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004957 Register src;
4958 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004959 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004960 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004961 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004962 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004963 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004964 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004965 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004966 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004967 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004968 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004969 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004970 }
4971 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004972 }
4973
4974 // TODO: memory barriers?
4975 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004976 Register src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004977 codegen_->MarkGCCard(obj, src);
4978 }
4979
4980 if (is_volatile) {
4981 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4982 }
4983}
4984
4985void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4986 HandleFieldGet(instruction, instruction->GetFieldInfo());
4987}
4988
4989void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4990 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4991}
4992
4993void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4994 HandleFieldSet(instruction, instruction->GetFieldInfo());
4995}
4996
4997void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4998 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4999}
5000
Alexey Frunze06a46c42016-07-19 15:00:40 -07005001void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
5002 HInstruction* instruction ATTRIBUTE_UNUSED,
5003 Location root,
5004 Register obj,
5005 uint32_t offset) {
5006 Register root_reg = root.AsRegister<Register>();
5007 if (kEmitCompilerReadBarrier) {
5008 UNIMPLEMENTED(FATAL) << "for read barrier";
5009 } else {
5010 // Plain GC root load with no read barrier.
5011 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5012 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5013 // Note that GC roots are not affected by heap poisoning, thus we
5014 // do not have to unpoison `root_reg` here.
5015 }
5016}
5017
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005018void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5019 LocationSummary::CallKind call_kind =
5020 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5021 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5022 locations->SetInAt(0, Location::RequiresRegister());
5023 locations->SetInAt(1, Location::RequiresRegister());
5024 // The output does overlap inputs.
5025 // Note that TypeCheckSlowPathMIPS uses this register too.
5026 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5027}
5028
5029void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5030 LocationSummary* locations = instruction->GetLocations();
5031 Register obj = locations->InAt(0).AsRegister<Register>();
5032 Register cls = locations->InAt(1).AsRegister<Register>();
5033 Register out = locations->Out().AsRegister<Register>();
5034
5035 MipsLabel done;
5036
5037 // Return 0 if `obj` is null.
5038 // TODO: Avoid this check if we know `obj` is not null.
5039 __ Move(out, ZERO);
5040 __ Beqz(obj, &done);
5041
5042 // Compare the class of `obj` with `cls`.
5043 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
5044 if (instruction->IsExactCheck()) {
5045 // Classes must be equal for the instanceof to succeed.
5046 __ Xor(out, out, cls);
5047 __ Sltiu(out, out, 1);
5048 } else {
5049 // If the classes are not equal, we go into a slow path.
5050 DCHECK(locations->OnlyCallsOnSlowPath());
5051 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5052 codegen_->AddSlowPath(slow_path);
5053 __ Bne(out, cls, slow_path->GetEntryLabel());
5054 __ LoadConst32(out, 1);
5055 __ Bind(slow_path->GetExitLabel());
5056 }
5057
5058 __ Bind(&done);
5059}
5060
5061void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5062 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5063 locations->SetOut(Location::ConstantLocation(constant));
5064}
5065
5066void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5067 // Will be generated at use site.
5068}
5069
5070void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5071 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5072 locations->SetOut(Location::ConstantLocation(constant));
5073}
5074
5075void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5076 // Will be generated at use site.
5077}
5078
5079void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5080 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5081 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5082}
5083
5084void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5085 HandleInvoke(invoke);
5086 // The register T0 is required to be used for the hidden argument in
5087 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5088 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5089}
5090
5091void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5092 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5093 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005094 Location receiver = invoke->GetLocations()->InAt(0);
5095 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005096 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005097
5098 // Set the hidden argument.
5099 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5100 invoke->GetDexMethodIndex());
5101
5102 // temp = object->GetClass();
5103 if (receiver.IsStackSlot()) {
5104 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5105 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5106 } else {
5107 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5108 }
5109 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005110 __ LoadFromOffset(kLoadWord, temp, temp,
5111 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5112 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005113 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005114 // temp = temp->GetImtEntryAt(method_offset);
5115 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5116 // T9 = temp->GetEntryPoint();
5117 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5118 // T9();
5119 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005120 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005121 DCHECK(!codegen_->IsLeafMethod());
5122 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5123}
5124
5125void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005126 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5127 if (intrinsic.TryDispatch(invoke)) {
5128 return;
5129 }
5130
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005131 HandleInvoke(invoke);
5132}
5133
5134void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005135 // Explicit clinit checks triggered by static invokes must have been pruned by
5136 // art::PrepareForRegisterAllocation.
5137 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005138
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005139 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5140 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5141 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5142
5143 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
5144 // R6 has PC-relative addressing.
5145 bool has_extra_input = !isR6 &&
5146 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
5147 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
5148
5149 if (invoke->HasPcRelativeDexCache()) {
5150 // kDexCachePcRelative is mutually exclusive with
5151 // kDirectAddressWithFixup/kCallDirectWithFixup.
5152 CHECK(!has_extra_input);
5153 has_extra_input = true;
5154 }
5155
Chris Larsen701566a2015-10-27 15:29:13 -07005156 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5157 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005158 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5159 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5160 }
Chris Larsen701566a2015-10-27 15:29:13 -07005161 return;
5162 }
5163
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005164 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005165
5166 // Add the extra input register if either the dex cache array base register
5167 // or the PC-relative base register for accessing literals is needed.
5168 if (has_extra_input) {
5169 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5170 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005171}
5172
Chris Larsen701566a2015-10-27 15:29:13 -07005173static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005174 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005175 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5176 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005177 return true;
5178 }
5179 return false;
5180}
5181
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005182HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005183 HLoadString::LoadKind desired_string_load_kind) {
5184 if (kEmitCompilerReadBarrier) {
5185 UNIMPLEMENTED(FATAL) << "for read barrier";
5186 }
5187 // We disable PC-relative load when there is an irreducible loop, as the optimization
5188 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005189 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5190 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005191 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5192 bool fallback_load = has_irreducible_loops;
5193 switch (desired_string_load_kind) {
5194 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5195 DCHECK(!GetCompilerOptions().GetCompilePic());
5196 break;
5197 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5198 DCHECK(GetCompilerOptions().GetCompilePic());
5199 break;
5200 case HLoadString::LoadKind::kBootImageAddress:
5201 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005202 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005203 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005204 break;
5205 case HLoadString::LoadKind::kDexCacheViaMethod:
5206 fallback_load = false;
5207 break;
Nicolas Geoffrayac3ebc32016-10-05 13:13:50 +01005208 case HLoadString::LoadKind::kJitTableAddress:
5209 DCHECK(Runtime::Current()->UseJitCompilation());
5210 // TODO: implement.
5211 fallback_load = true;
5212 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005213 }
5214 if (fallback_load) {
5215 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5216 }
5217 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005218}
5219
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005220HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5221 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005222 if (kEmitCompilerReadBarrier) {
5223 UNIMPLEMENTED(FATAL) << "for read barrier";
5224 }
5225 // We disable pc-relative load when there is an irreducible loop, as the optimization
5226 // is incompatible with it.
5227 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5228 bool fallback_load = has_irreducible_loops;
5229 switch (desired_class_load_kind) {
5230 case HLoadClass::LoadKind::kReferrersClass:
5231 fallback_load = false;
5232 break;
5233 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5234 DCHECK(!GetCompilerOptions().GetCompilePic());
5235 break;
5236 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5237 DCHECK(GetCompilerOptions().GetCompilePic());
5238 break;
5239 case HLoadClass::LoadKind::kBootImageAddress:
5240 break;
5241 case HLoadClass::LoadKind::kDexCacheAddress:
5242 DCHECK(Runtime::Current()->UseJitCompilation());
5243 fallback_load = false;
5244 break;
5245 case HLoadClass::LoadKind::kDexCachePcRelative:
5246 DCHECK(!Runtime::Current()->UseJitCompilation());
5247 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5248 // with irreducible loops.
5249 break;
5250 case HLoadClass::LoadKind::kDexCacheViaMethod:
5251 fallback_load = false;
5252 break;
5253 }
5254 if (fallback_load) {
5255 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5256 }
5257 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005258}
5259
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005260Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5261 Register temp) {
5262 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5263 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5264 if (!invoke->GetLocations()->Intrinsified()) {
5265 return location.AsRegister<Register>();
5266 }
5267 // For intrinsics we allow any location, so it may be on the stack.
5268 if (!location.IsRegister()) {
5269 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5270 return temp;
5271 }
5272 // For register locations, check if the register was saved. If so, get it from the stack.
5273 // Note: There is a chance that the register was saved but not overwritten, so we could
5274 // save one load. However, since this is just an intrinsic slow path we prefer this
5275 // simple and more robust approach rather that trying to determine if that's the case.
5276 SlowPathCode* slow_path = GetCurrentSlowPath();
5277 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5278 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5279 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5280 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5281 return temp;
5282 }
5283 return location.AsRegister<Register>();
5284}
5285
Vladimir Markodc151b22015-10-15 18:02:30 +01005286HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5287 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005288 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005289 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
5290 // We disable PC-relative load when there is an irreducible loop, as the optimization
5291 // is incompatible with it.
5292 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5293 bool fallback_load = true;
5294 bool fallback_call = true;
5295 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005296 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5297 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005298 fallback_load = has_irreducible_loops;
5299 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005300 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005301 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005302 break;
5303 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005304 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005305 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005306 fallback_call = has_irreducible_loops;
5307 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005308 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005309 // TODO: Implement this type.
5310 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005311 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005312 fallback_call = false;
5313 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005314 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005315 if (fallback_load) {
5316 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5317 dispatch_info.method_load_data = 0;
5318 }
5319 if (fallback_call) {
5320 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
5321 dispatch_info.direct_code_ptr = 0;
5322 }
5323 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005324}
5325
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005326void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5327 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005328 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005329 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5330 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5331 bool isR6 = isa_features_.IsR6();
5332 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
5333 // R6 has PC-relative addressing.
5334 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
5335 (!isR6 &&
5336 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
5337 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
5338 Register base_reg = has_extra_input
5339 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5340 : ZERO;
5341
5342 // For better instruction scheduling we load the direct code pointer before the method pointer.
5343 switch (code_ptr_location) {
5344 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5345 // T9 = invoke->GetDirectCodePtr();
5346 __ LoadConst32(T9, invoke->GetDirectCodePtr());
5347 break;
5348 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5349 // T9 = code address from literal pool with link-time patch.
5350 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
5351 break;
5352 default:
5353 break;
5354 }
5355
5356 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005357 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005358 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005359 uint32_t offset =
5360 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005361 __ LoadFromOffset(kLoadWord,
5362 temp.AsRegister<Register>(),
5363 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005364 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005365 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005366 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005367 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005368 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005369 break;
5370 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5371 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5372 break;
5373 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005374 __ LoadLiteral(temp.AsRegister<Register>(),
5375 base_reg,
5376 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5377 break;
5378 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
5379 HMipsDexCacheArraysBase* base =
5380 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5381 int32_t offset =
5382 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5383 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5384 break;
5385 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005386 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005387 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005388 Register reg = temp.AsRegister<Register>();
5389 Register method_reg;
5390 if (current_method.IsRegister()) {
5391 method_reg = current_method.AsRegister<Register>();
5392 } else {
5393 // TODO: use the appropriate DCHECK() here if possible.
5394 // DCHECK(invoke->GetLocations()->Intrinsified());
5395 DCHECK(!current_method.IsValid());
5396 method_reg = reg;
5397 __ Lw(reg, SP, kCurrentMethodStackOffset);
5398 }
5399
5400 // temp = temp->dex_cache_resolved_methods_;
5401 __ LoadFromOffset(kLoadWord,
5402 reg,
5403 method_reg,
5404 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005405 // temp = temp[index_in_cache];
5406 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5407 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005408 __ LoadFromOffset(kLoadWord,
5409 reg,
5410 reg,
5411 CodeGenerator::GetCachePointerOffset(index_in_cache));
5412 break;
5413 }
5414 }
5415
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005416 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005417 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005418 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005419 break;
5420 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005421 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5422 // T9 prepared above for better instruction scheduling.
5423 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005424 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005425 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005426 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005427 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005428 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01005429 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
5430 LOG(FATAL) << "Unsupported";
5431 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005432 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5433 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005434 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005435 T9,
5436 callee_method.AsRegister<Register>(),
5437 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005438 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005439 // T9()
5440 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005441 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005442 break;
5443 }
5444 DCHECK(!IsLeafMethod());
5445}
5446
5447void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005448 // Explicit clinit checks triggered by static invokes must have been pruned by
5449 // art::PrepareForRegisterAllocation.
5450 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005451
5452 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5453 return;
5454 }
5455
5456 LocationSummary* locations = invoke->GetLocations();
5457 codegen_->GenerateStaticOrDirectCall(invoke,
5458 locations->HasTemps()
5459 ? locations->GetTemp(0)
5460 : Location::NoLocation());
5461 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5462}
5463
Chris Larsen3acee732015-11-18 13:31:08 -08005464void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005465 // Use the calling convention instead of the location of the receiver, as
5466 // intrinsics may have put the receiver in a different register. In the intrinsics
5467 // slow path, the arguments have been moved to the right place, so here we are
5468 // guaranteed that the receiver is the first register of the calling convention.
5469 InvokeDexCallingConvention calling_convention;
5470 Register receiver = calling_convention.GetRegisterAt(0);
5471
Chris Larsen3acee732015-11-18 13:31:08 -08005472 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005473 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5474 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5475 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005476 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005477
5478 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005479 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005480 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005481 // temp = temp->GetMethodAt(method_offset);
5482 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5483 // T9 = temp->GetEntryPoint();
5484 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5485 // T9();
5486 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005487 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005488}
5489
5490void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5491 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5492 return;
5493 }
5494
5495 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005496 DCHECK(!codegen_->IsLeafMethod());
5497 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5498}
5499
5500void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005501 if (cls->NeedsAccessCheck()) {
5502 InvokeRuntimeCallingConvention calling_convention;
5503 CodeGenerator::CreateLoadClassLocationSummary(
5504 cls,
5505 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5506 Location::RegisterLocation(V0),
5507 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
5508 return;
5509 }
5510
5511 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5512 ? LocationSummary::kCallOnSlowPath
5513 : LocationSummary::kNoCall;
5514 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5515 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5516 switch (load_kind) {
5517 // We need an extra register for PC-relative literals on R2.
5518 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5519 case HLoadClass::LoadKind::kBootImageAddress:
5520 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5521 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5522 break;
5523 }
5524 FALLTHROUGH_INTENDED;
5525 // We need an extra register for PC-relative dex cache accesses.
5526 case HLoadClass::LoadKind::kDexCachePcRelative:
5527 case HLoadClass::LoadKind::kReferrersClass:
5528 case HLoadClass::LoadKind::kDexCacheViaMethod:
5529 locations->SetInAt(0, Location::RequiresRegister());
5530 break;
5531 default:
5532 break;
5533 }
5534 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005535}
5536
5537void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
5538 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01005539 if (cls->NeedsAccessCheck()) {
5540 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005541 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005542 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01005543 return;
5544 }
5545
Alexey Frunze06a46c42016-07-19 15:00:40 -07005546 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5547 Location out_loc = locations->Out();
5548 Register out = out_loc.AsRegister<Register>();
5549 Register base_or_current_method_reg;
5550 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5551 switch (load_kind) {
5552 // We need an extra register for PC-relative literals on R2.
5553 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5554 case HLoadClass::LoadKind::kBootImageAddress:
5555 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5556 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5557 break;
5558 // We need an extra register for PC-relative dex cache accesses.
5559 case HLoadClass::LoadKind::kDexCachePcRelative:
5560 case HLoadClass::LoadKind::kReferrersClass:
5561 case HLoadClass::LoadKind::kDexCacheViaMethod:
5562 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5563 break;
5564 default:
5565 base_or_current_method_reg = ZERO;
5566 break;
5567 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005568
Alexey Frunze06a46c42016-07-19 15:00:40 -07005569 bool generate_null_check = false;
5570 switch (load_kind) {
5571 case HLoadClass::LoadKind::kReferrersClass: {
5572 DCHECK(!cls->CanCallRuntime());
5573 DCHECK(!cls->MustGenerateClinitCheck());
5574 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5575 GenerateGcRootFieldLoad(cls,
5576 out_loc,
5577 base_or_current_method_reg,
5578 ArtMethod::DeclaringClassOffset().Int32Value());
5579 break;
5580 }
5581 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5582 DCHECK(!kEmitCompilerReadBarrier);
5583 __ LoadLiteral(out,
5584 base_or_current_method_reg,
5585 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5586 cls->GetTypeIndex()));
5587 break;
5588 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5589 DCHECK(!kEmitCompilerReadBarrier);
5590 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5591 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005592 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005593 break;
5594 }
5595 case HLoadClass::LoadKind::kBootImageAddress: {
5596 DCHECK(!kEmitCompilerReadBarrier);
5597 DCHECK_NE(cls->GetAddress(), 0u);
5598 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5599 __ LoadLiteral(out,
5600 base_or_current_method_reg,
5601 codegen_->DeduplicateBootImageAddressLiteral(address));
5602 break;
5603 }
5604 case HLoadClass::LoadKind::kDexCacheAddress: {
5605 DCHECK_NE(cls->GetAddress(), 0u);
5606 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5607 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5608 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5609 int16_t offset = Low16Bits(address);
5610 uint32_t base_address = address - offset; // This accounts for offset sign extension.
5611 __ Lui(out, High16Bits(base_address));
5612 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5613 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5614 generate_null_check = !cls->IsInDexCache();
5615 break;
5616 }
5617 case HLoadClass::LoadKind::kDexCachePcRelative: {
5618 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
5619 int32_t offset =
5620 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5621 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5622 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
5623 generate_null_check = !cls->IsInDexCache();
5624 break;
5625 }
5626 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5627 // /* GcRoot<mirror::Class>[] */ out =
5628 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5629 __ LoadFromOffset(kLoadWord,
5630 out,
5631 base_or_current_method_reg,
5632 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5633 // /* GcRoot<mirror::Class> */ out = out[type_index]
5634 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5635 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5636 generate_null_check = !cls->IsInDexCache();
5637 }
5638 }
5639
5640 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5641 DCHECK(cls->CanCallRuntime());
5642 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5643 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5644 codegen_->AddSlowPath(slow_path);
5645 if (generate_null_check) {
5646 __ Beqz(out, slow_path->GetEntryLabel());
5647 }
5648 if (cls->MustGenerateClinitCheck()) {
5649 GenerateClassInitializationCheck(slow_path, out);
5650 } else {
5651 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005652 }
5653 }
5654}
5655
5656static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005657 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005658}
5659
5660void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5661 LocationSummary* locations =
5662 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5663 locations->SetOut(Location::RequiresRegister());
5664}
5665
5666void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5667 Register out = load->GetLocations()->Out().AsRegister<Register>();
5668 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5669}
5670
5671void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5672 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5673}
5674
5675void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5676 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5677}
5678
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005679void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005680 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00005681 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5682 ? LocationSummary::kCallOnMainOnly
5683 : LocationSummary::kCallOnSlowPath)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005684 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005686 HLoadString::LoadKind load_kind = load->GetLoadKind();
5687 switch (load_kind) {
5688 // We need an extra register for PC-relative literals on R2.
5689 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5690 case HLoadString::LoadKind::kBootImageAddress:
5691 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005692 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005693 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5694 break;
5695 }
5696 FALLTHROUGH_INTENDED;
5697 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005698 case HLoadString::LoadKind::kDexCacheViaMethod:
5699 locations->SetInAt(0, Location::RequiresRegister());
5700 break;
5701 default:
5702 break;
5703 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005704 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5705 InvokeRuntimeCallingConvention calling_convention;
5706 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5707 } else {
5708 locations->SetOut(Location::RequiresRegister());
5709 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005710}
5711
5712void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005713 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005714 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005715 Location out_loc = locations->Out();
5716 Register out = out_loc.AsRegister<Register>();
5717 Register base_or_current_method_reg;
5718 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5719 switch (load_kind) {
5720 // We need an extra register for PC-relative literals on R2.
5721 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5722 case HLoadString::LoadKind::kBootImageAddress:
5723 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005724 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005725 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5726 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005727 default:
5728 base_or_current_method_reg = ZERO;
5729 break;
5730 }
5731
5732 switch (load_kind) {
5733 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5734 DCHECK(!kEmitCompilerReadBarrier);
5735 __ LoadLiteral(out,
5736 base_or_current_method_reg,
5737 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5738 load->GetStringIndex()));
5739 return; // No dex cache slow path.
5740 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5741 DCHECK(!kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005742 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005743 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5744 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005745 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005746 return; // No dex cache slow path.
5747 }
5748 case HLoadString::LoadKind::kBootImageAddress: {
5749 DCHECK(!kEmitCompilerReadBarrier);
5750 DCHECK_NE(load->GetAddress(), 0u);
5751 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5752 __ LoadLiteral(out,
5753 base_or_current_method_reg,
5754 codegen_->DeduplicateBootImageAddressLiteral(address));
5755 return; // No dex cache slow path.
5756 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005757 case HLoadString::LoadKind::kBssEntry: {
5758 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5759 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5760 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5761 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
5762 __ LoadFromOffset(kLoadWord, out, out, 0);
5763 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5764 codegen_->AddSlowPath(slow_path);
5765 __ Beqz(out, slow_path->GetEntryLabel());
5766 __ Bind(slow_path->GetExitLabel());
5767 return;
5768 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005769 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005770 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005771 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005772
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005773 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005774 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5775 InvokeRuntimeCallingConvention calling_convention;
5776 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5777 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5778 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005779}
5780
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005781void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5782 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5783 locations->SetOut(Location::ConstantLocation(constant));
5784}
5785
5786void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5787 // Will be generated at use site.
5788}
5789
5790void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5791 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005792 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005793 InvokeRuntimeCallingConvention calling_convention;
5794 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5795}
5796
5797void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5798 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005799 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005800 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5801 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005802 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005803 }
5804 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5805}
5806
5807void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5808 LocationSummary* locations =
5809 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5810 switch (mul->GetResultType()) {
5811 case Primitive::kPrimInt:
5812 case Primitive::kPrimLong:
5813 locations->SetInAt(0, Location::RequiresRegister());
5814 locations->SetInAt(1, Location::RequiresRegister());
5815 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5816 break;
5817
5818 case Primitive::kPrimFloat:
5819 case Primitive::kPrimDouble:
5820 locations->SetInAt(0, Location::RequiresFpuRegister());
5821 locations->SetInAt(1, Location::RequiresFpuRegister());
5822 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5823 break;
5824
5825 default:
5826 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5827 }
5828}
5829
5830void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5831 Primitive::Type type = instruction->GetType();
5832 LocationSummary* locations = instruction->GetLocations();
5833 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5834
5835 switch (type) {
5836 case Primitive::kPrimInt: {
5837 Register dst = locations->Out().AsRegister<Register>();
5838 Register lhs = locations->InAt(0).AsRegister<Register>();
5839 Register rhs = locations->InAt(1).AsRegister<Register>();
5840
5841 if (isR6) {
5842 __ MulR6(dst, lhs, rhs);
5843 } else {
5844 __ MulR2(dst, lhs, rhs);
5845 }
5846 break;
5847 }
5848 case Primitive::kPrimLong: {
5849 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5850 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5851 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5852 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5853 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5854 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5855
5856 // Extra checks to protect caused by the existance of A1_A2.
5857 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5858 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5859 DCHECK_NE(dst_high, lhs_low);
5860 DCHECK_NE(dst_high, rhs_low);
5861
5862 // A_B * C_D
5863 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5864 // dst_lo: [ low(B*D) ]
5865 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5866
5867 if (isR6) {
5868 __ MulR6(TMP, lhs_high, rhs_low);
5869 __ MulR6(dst_high, lhs_low, rhs_high);
5870 __ Addu(dst_high, dst_high, TMP);
5871 __ MuhuR6(TMP, lhs_low, rhs_low);
5872 __ Addu(dst_high, dst_high, TMP);
5873 __ MulR6(dst_low, lhs_low, rhs_low);
5874 } else {
5875 __ MulR2(TMP, lhs_high, rhs_low);
5876 __ MulR2(dst_high, lhs_low, rhs_high);
5877 __ Addu(dst_high, dst_high, TMP);
5878 __ MultuR2(lhs_low, rhs_low);
5879 __ Mfhi(TMP);
5880 __ Addu(dst_high, dst_high, TMP);
5881 __ Mflo(dst_low);
5882 }
5883 break;
5884 }
5885 case Primitive::kPrimFloat:
5886 case Primitive::kPrimDouble: {
5887 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5888 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5889 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5890 if (type == Primitive::kPrimFloat) {
5891 __ MulS(dst, lhs, rhs);
5892 } else {
5893 __ MulD(dst, lhs, rhs);
5894 }
5895 break;
5896 }
5897 default:
5898 LOG(FATAL) << "Unexpected mul type " << type;
5899 }
5900}
5901
5902void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5903 LocationSummary* locations =
5904 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5905 switch (neg->GetResultType()) {
5906 case Primitive::kPrimInt:
5907 case Primitive::kPrimLong:
5908 locations->SetInAt(0, Location::RequiresRegister());
5909 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5910 break;
5911
5912 case Primitive::kPrimFloat:
5913 case Primitive::kPrimDouble:
5914 locations->SetInAt(0, Location::RequiresFpuRegister());
5915 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5916 break;
5917
5918 default:
5919 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5920 }
5921}
5922
5923void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5924 Primitive::Type type = instruction->GetType();
5925 LocationSummary* locations = instruction->GetLocations();
5926
5927 switch (type) {
5928 case Primitive::kPrimInt: {
5929 Register dst = locations->Out().AsRegister<Register>();
5930 Register src = locations->InAt(0).AsRegister<Register>();
5931 __ Subu(dst, ZERO, src);
5932 break;
5933 }
5934 case Primitive::kPrimLong: {
5935 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5936 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5937 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5938 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5939 __ Subu(dst_low, ZERO, src_low);
5940 __ Sltu(TMP, ZERO, dst_low);
5941 __ Subu(dst_high, ZERO, src_high);
5942 __ Subu(dst_high, dst_high, TMP);
5943 break;
5944 }
5945 case Primitive::kPrimFloat:
5946 case Primitive::kPrimDouble: {
5947 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5948 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5949 if (type == Primitive::kPrimFloat) {
5950 __ NegS(dst, src);
5951 } else {
5952 __ NegD(dst, src);
5953 }
5954 break;
5955 }
5956 default:
5957 LOG(FATAL) << "Unexpected neg type " << type;
5958 }
5959}
5960
5961void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5962 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005963 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005964 InvokeRuntimeCallingConvention calling_convention;
5965 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5966 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5967 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5968 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5969}
5970
5971void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5972 InvokeRuntimeCallingConvention calling_convention;
5973 Register current_method_register = calling_convention.GetRegisterAt(2);
5974 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5975 // Move an uint16_t value to a register.
5976 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005977 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005978 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5979 void*, uint32_t, int32_t, ArtMethod*>();
5980}
5981
5982void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5983 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005985 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005986 if (instruction->IsStringAlloc()) {
5987 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5988 } else {
5989 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5990 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5991 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005992 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5993}
5994
5995void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005996 if (instruction->IsStringAlloc()) {
5997 // String is allocated through StringFactory. Call NewEmptyString entry point.
5998 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005999 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006000 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6001 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
6002 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006003 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00006004 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6005 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006006 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00006007 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
6008 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006009}
6010
6011void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
6012 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6013 locations->SetInAt(0, Location::RequiresRegister());
6014 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6015}
6016
6017void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
6018 Primitive::Type type = instruction->GetType();
6019 LocationSummary* locations = instruction->GetLocations();
6020
6021 switch (type) {
6022 case Primitive::kPrimInt: {
6023 Register dst = locations->Out().AsRegister<Register>();
6024 Register src = locations->InAt(0).AsRegister<Register>();
6025 __ Nor(dst, src, ZERO);
6026 break;
6027 }
6028
6029 case Primitive::kPrimLong: {
6030 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6031 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6032 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6033 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6034 __ Nor(dst_high, src_high, ZERO);
6035 __ Nor(dst_low, src_low, ZERO);
6036 break;
6037 }
6038
6039 default:
6040 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6041 }
6042}
6043
6044void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6045 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6046 locations->SetInAt(0, Location::RequiresRegister());
6047 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6048}
6049
6050void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6051 LocationSummary* locations = instruction->GetLocations();
6052 __ Xori(locations->Out().AsRegister<Register>(),
6053 locations->InAt(0).AsRegister<Register>(),
6054 1);
6055}
6056
6057void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006058 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6059 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006060}
6061
Calin Juravle2ae48182016-03-16 14:05:09 +00006062void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
6063 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006064 return;
6065 }
6066 Location obj = instruction->GetLocations()->InAt(0);
6067
6068 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006069 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006070}
6071
Calin Juravle2ae48182016-03-16 14:05:09 +00006072void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006073 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006074 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006075
6076 Location obj = instruction->GetLocations()->InAt(0);
6077
6078 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6079}
6080
6081void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006082 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006083}
6084
6085void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6086 HandleBinaryOp(instruction);
6087}
6088
6089void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6090 HandleBinaryOp(instruction);
6091}
6092
6093void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6094 LOG(FATAL) << "Unreachable";
6095}
6096
6097void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6098 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6099}
6100
6101void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6102 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6103 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6104 if (location.IsStackSlot()) {
6105 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6106 } else if (location.IsDoubleStackSlot()) {
6107 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6108 }
6109 locations->SetOut(location);
6110}
6111
6112void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6113 ATTRIBUTE_UNUSED) {
6114 // Nothing to do, the parameter is already at its location.
6115}
6116
6117void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6118 LocationSummary* locations =
6119 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6120 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6121}
6122
6123void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6124 ATTRIBUTE_UNUSED) {
6125 // Nothing to do, the method is already at its location.
6126}
6127
6128void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6129 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006130 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006131 locations->SetInAt(i, Location::Any());
6132 }
6133 locations->SetOut(Location::Any());
6134}
6135
6136void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6137 LOG(FATAL) << "Unreachable";
6138}
6139
6140void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6141 Primitive::Type type = rem->GetResultType();
6142 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006143 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006144 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6145
6146 switch (type) {
6147 case Primitive::kPrimInt:
6148 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006149 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006150 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6151 break;
6152
6153 case Primitive::kPrimLong: {
6154 InvokeRuntimeCallingConvention calling_convention;
6155 locations->SetInAt(0, Location::RegisterPairLocation(
6156 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6157 locations->SetInAt(1, Location::RegisterPairLocation(
6158 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6159 locations->SetOut(calling_convention.GetReturnLocation(type));
6160 break;
6161 }
6162
6163 case Primitive::kPrimFloat:
6164 case Primitive::kPrimDouble: {
6165 InvokeRuntimeCallingConvention calling_convention;
6166 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6167 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6168 locations->SetOut(calling_convention.GetReturnLocation(type));
6169 break;
6170 }
6171
6172 default:
6173 LOG(FATAL) << "Unexpected rem type " << type;
6174 }
6175}
6176
6177void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6178 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006179
6180 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006181 case Primitive::kPrimInt:
6182 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006183 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006184 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006185 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006186 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6187 break;
6188 }
6189 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006190 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006191 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006192 break;
6193 }
6194 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006195 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006196 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006197 break;
6198 }
6199 default:
6200 LOG(FATAL) << "Unexpected rem type " << type;
6201 }
6202}
6203
6204void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6205 memory_barrier->SetLocations(nullptr);
6206}
6207
6208void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6209 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6210}
6211
6212void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6213 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6214 Primitive::Type return_type = ret->InputAt(0)->GetType();
6215 locations->SetInAt(0, MipsReturnLocation(return_type));
6216}
6217
6218void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6219 codegen_->GenerateFrameExit();
6220}
6221
6222void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6223 ret->SetLocations(nullptr);
6224}
6225
6226void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6227 codegen_->GenerateFrameExit();
6228}
6229
Alexey Frunze92d90602015-12-18 18:16:36 -08006230void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6231 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006232}
6233
Alexey Frunze92d90602015-12-18 18:16:36 -08006234void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6235 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006236}
6237
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006238void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6239 HandleShift(shl);
6240}
6241
6242void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6243 HandleShift(shl);
6244}
6245
6246void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6247 HandleShift(shr);
6248}
6249
6250void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6251 HandleShift(shr);
6252}
6253
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006254void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6255 HandleBinaryOp(instruction);
6256}
6257
6258void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6259 HandleBinaryOp(instruction);
6260}
6261
6262void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6263 HandleFieldGet(instruction, instruction->GetFieldInfo());
6264}
6265
6266void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6267 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6268}
6269
6270void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6271 HandleFieldSet(instruction, instruction->GetFieldInfo());
6272}
6273
6274void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6275 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6276}
6277
6278void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6279 HUnresolvedInstanceFieldGet* instruction) {
6280 FieldAccessCallingConventionMIPS calling_convention;
6281 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6282 instruction->GetFieldType(),
6283 calling_convention);
6284}
6285
6286void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6287 HUnresolvedInstanceFieldGet* instruction) {
6288 FieldAccessCallingConventionMIPS calling_convention;
6289 codegen_->GenerateUnresolvedFieldAccess(instruction,
6290 instruction->GetFieldType(),
6291 instruction->GetFieldIndex(),
6292 instruction->GetDexPc(),
6293 calling_convention);
6294}
6295
6296void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6297 HUnresolvedInstanceFieldSet* instruction) {
6298 FieldAccessCallingConventionMIPS calling_convention;
6299 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6300 instruction->GetFieldType(),
6301 calling_convention);
6302}
6303
6304void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6305 HUnresolvedInstanceFieldSet* instruction) {
6306 FieldAccessCallingConventionMIPS calling_convention;
6307 codegen_->GenerateUnresolvedFieldAccess(instruction,
6308 instruction->GetFieldType(),
6309 instruction->GetFieldIndex(),
6310 instruction->GetDexPc(),
6311 calling_convention);
6312}
6313
6314void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6315 HUnresolvedStaticFieldGet* instruction) {
6316 FieldAccessCallingConventionMIPS calling_convention;
6317 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6318 instruction->GetFieldType(),
6319 calling_convention);
6320}
6321
6322void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6323 HUnresolvedStaticFieldGet* instruction) {
6324 FieldAccessCallingConventionMIPS calling_convention;
6325 codegen_->GenerateUnresolvedFieldAccess(instruction,
6326 instruction->GetFieldType(),
6327 instruction->GetFieldIndex(),
6328 instruction->GetDexPc(),
6329 calling_convention);
6330}
6331
6332void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6333 HUnresolvedStaticFieldSet* instruction) {
6334 FieldAccessCallingConventionMIPS calling_convention;
6335 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6336 instruction->GetFieldType(),
6337 calling_convention);
6338}
6339
6340void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6341 HUnresolvedStaticFieldSet* instruction) {
6342 FieldAccessCallingConventionMIPS calling_convention;
6343 codegen_->GenerateUnresolvedFieldAccess(instruction,
6344 instruction->GetFieldType(),
6345 instruction->GetFieldIndex(),
6346 instruction->GetDexPc(),
6347 calling_convention);
6348}
6349
6350void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006351 LocationSummary* locations =
6352 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006353 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006354}
6355
6356void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6357 HBasicBlock* block = instruction->GetBlock();
6358 if (block->GetLoopInformation() != nullptr) {
6359 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6360 // The back edge will generate the suspend check.
6361 return;
6362 }
6363 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6364 // The goto will generate the suspend check.
6365 return;
6366 }
6367 GenerateSuspendCheck(instruction, nullptr);
6368}
6369
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006370void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6371 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006372 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006373 InvokeRuntimeCallingConvention calling_convention;
6374 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6375}
6376
6377void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006378 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006379 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6380}
6381
6382void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6383 Primitive::Type input_type = conversion->GetInputType();
6384 Primitive::Type result_type = conversion->GetResultType();
6385 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006386 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006387
6388 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6389 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6390 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6391 }
6392
6393 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006394 if (!isR6 &&
6395 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6396 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006397 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006398 }
6399
6400 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6401
6402 if (call_kind == LocationSummary::kNoCall) {
6403 if (Primitive::IsFloatingPointType(input_type)) {
6404 locations->SetInAt(0, Location::RequiresFpuRegister());
6405 } else {
6406 locations->SetInAt(0, Location::RequiresRegister());
6407 }
6408
6409 if (Primitive::IsFloatingPointType(result_type)) {
6410 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6411 } else {
6412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6413 }
6414 } else {
6415 InvokeRuntimeCallingConvention calling_convention;
6416
6417 if (Primitive::IsFloatingPointType(input_type)) {
6418 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6419 } else {
6420 DCHECK_EQ(input_type, Primitive::kPrimLong);
6421 locations->SetInAt(0, Location::RegisterPairLocation(
6422 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6423 }
6424
6425 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6426 }
6427}
6428
6429void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6430 LocationSummary* locations = conversion->GetLocations();
6431 Primitive::Type result_type = conversion->GetResultType();
6432 Primitive::Type input_type = conversion->GetInputType();
6433 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006434 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006435
6436 DCHECK_NE(input_type, result_type);
6437
6438 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6439 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6440 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6441 Register src = locations->InAt(0).AsRegister<Register>();
6442
Alexey Frunzea871ef12016-06-27 15:20:11 -07006443 if (dst_low != src) {
6444 __ Move(dst_low, src);
6445 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006446 __ Sra(dst_high, src, 31);
6447 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6448 Register dst = locations->Out().AsRegister<Register>();
6449 Register src = (input_type == Primitive::kPrimLong)
6450 ? locations->InAt(0).AsRegisterPairLow<Register>()
6451 : locations->InAt(0).AsRegister<Register>();
6452
6453 switch (result_type) {
6454 case Primitive::kPrimChar:
6455 __ Andi(dst, src, 0xFFFF);
6456 break;
6457 case Primitive::kPrimByte:
6458 if (has_sign_extension) {
6459 __ Seb(dst, src);
6460 } else {
6461 __ Sll(dst, src, 24);
6462 __ Sra(dst, dst, 24);
6463 }
6464 break;
6465 case Primitive::kPrimShort:
6466 if (has_sign_extension) {
6467 __ Seh(dst, src);
6468 } else {
6469 __ Sll(dst, src, 16);
6470 __ Sra(dst, dst, 16);
6471 }
6472 break;
6473 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006474 if (dst != src) {
6475 __ Move(dst, src);
6476 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006477 break;
6478
6479 default:
6480 LOG(FATAL) << "Unexpected type conversion from " << input_type
6481 << " to " << result_type;
6482 }
6483 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006484 if (input_type == Primitive::kPrimLong) {
6485 if (isR6) {
6486 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6487 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6488 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6489 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6490 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6491 __ Mtc1(src_low, FTMP);
6492 __ Mthc1(src_high, FTMP);
6493 if (result_type == Primitive::kPrimFloat) {
6494 __ Cvtsl(dst, FTMP);
6495 } else {
6496 __ Cvtdl(dst, FTMP);
6497 }
6498 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006499 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6500 : kQuickL2d;
6501 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006502 if (result_type == Primitive::kPrimFloat) {
6503 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6504 } else {
6505 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6506 }
6507 }
6508 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006509 Register src = locations->InAt(0).AsRegister<Register>();
6510 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6511 __ Mtc1(src, FTMP);
6512 if (result_type == Primitive::kPrimFloat) {
6513 __ Cvtsw(dst, FTMP);
6514 } else {
6515 __ Cvtdw(dst, FTMP);
6516 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006517 }
6518 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6519 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006520 if (result_type == Primitive::kPrimLong) {
6521 if (isR6) {
6522 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6523 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6524 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6525 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6526 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6527 MipsLabel truncate;
6528 MipsLabel done;
6529
6530 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6531 // value when the input is either a NaN or is outside of the range of the output type
6532 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6533 // the same result.
6534 //
6535 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6536 // value of the output type if the input is outside of the range after the truncation or
6537 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6538 // results. This matches the desired float/double-to-int/long conversion exactly.
6539 //
6540 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6541 //
6542 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6543 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6544 // even though it must be NAN2008=1 on R6.
6545 //
6546 // The code takes care of the different behaviors by first comparing the input to the
6547 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6548 // If the input is greater than or equal to the minimum, it procedes to the truncate
6549 // instruction, which will handle such an input the same way irrespective of NAN2008.
6550 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6551 // in order to return either zero or the minimum value.
6552 //
6553 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6554 // truncate instruction for MIPS64R6.
6555 if (input_type == Primitive::kPrimFloat) {
6556 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6557 __ LoadConst32(TMP, min_val);
6558 __ Mtc1(TMP, FTMP);
6559 __ CmpLeS(FTMP, FTMP, src);
6560 } else {
6561 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6562 __ LoadConst32(TMP, High32Bits(min_val));
6563 __ Mtc1(ZERO, FTMP);
6564 __ Mthc1(TMP, FTMP);
6565 __ CmpLeD(FTMP, FTMP, src);
6566 }
6567
6568 __ Bc1nez(FTMP, &truncate);
6569
6570 if (input_type == Primitive::kPrimFloat) {
6571 __ CmpEqS(FTMP, src, src);
6572 } else {
6573 __ CmpEqD(FTMP, src, src);
6574 }
6575 __ Move(dst_low, ZERO);
6576 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6577 __ Mfc1(TMP, FTMP);
6578 __ And(dst_high, dst_high, TMP);
6579
6580 __ B(&done);
6581
6582 __ Bind(&truncate);
6583
6584 if (input_type == Primitive::kPrimFloat) {
6585 __ TruncLS(FTMP, src);
6586 } else {
6587 __ TruncLD(FTMP, src);
6588 }
6589 __ Mfc1(dst_low, FTMP);
6590 __ Mfhc1(dst_high, FTMP);
6591
6592 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006593 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006594 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6595 : kQuickD2l;
6596 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006597 if (input_type == Primitive::kPrimFloat) {
6598 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6599 } else {
6600 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6601 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006602 }
6603 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006604 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6605 Register dst = locations->Out().AsRegister<Register>();
6606 MipsLabel truncate;
6607 MipsLabel done;
6608
6609 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6610 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6611 // even though it must be NAN2008=1 on R6.
6612 //
6613 // For details see the large comment above for the truncation of float/double to long on R6.
6614 //
6615 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6616 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006617 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006618 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6619 __ LoadConst32(TMP, min_val);
6620 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006621 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006622 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6623 __ LoadConst32(TMP, High32Bits(min_val));
6624 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006625 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006626 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006627
6628 if (isR6) {
6629 if (input_type == Primitive::kPrimFloat) {
6630 __ CmpLeS(FTMP, FTMP, src);
6631 } else {
6632 __ CmpLeD(FTMP, FTMP, src);
6633 }
6634 __ Bc1nez(FTMP, &truncate);
6635
6636 if (input_type == Primitive::kPrimFloat) {
6637 __ CmpEqS(FTMP, src, src);
6638 } else {
6639 __ CmpEqD(FTMP, src, src);
6640 }
6641 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6642 __ Mfc1(TMP, FTMP);
6643 __ And(dst, dst, TMP);
6644 } else {
6645 if (input_type == Primitive::kPrimFloat) {
6646 __ ColeS(0, FTMP, src);
6647 } else {
6648 __ ColeD(0, FTMP, src);
6649 }
6650 __ Bc1t(0, &truncate);
6651
6652 if (input_type == Primitive::kPrimFloat) {
6653 __ CeqS(0, src, src);
6654 } else {
6655 __ CeqD(0, src, src);
6656 }
6657 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6658 __ Movf(dst, ZERO, 0);
6659 }
6660
6661 __ B(&done);
6662
6663 __ Bind(&truncate);
6664
6665 if (input_type == Primitive::kPrimFloat) {
6666 __ TruncWS(FTMP, src);
6667 } else {
6668 __ TruncWD(FTMP, src);
6669 }
6670 __ Mfc1(dst, FTMP);
6671
6672 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006673 }
6674 } else if (Primitive::IsFloatingPointType(result_type) &&
6675 Primitive::IsFloatingPointType(input_type)) {
6676 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6677 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6678 if (result_type == Primitive::kPrimFloat) {
6679 __ Cvtsd(dst, src);
6680 } else {
6681 __ Cvtds(dst, src);
6682 }
6683 } else {
6684 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6685 << " to " << result_type;
6686 }
6687}
6688
6689void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6690 HandleShift(ushr);
6691}
6692
6693void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6694 HandleShift(ushr);
6695}
6696
6697void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6698 HandleBinaryOp(instruction);
6699}
6700
6701void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6702 HandleBinaryOp(instruction);
6703}
6704
6705void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6706 // Nothing to do, this should be removed during prepare for register allocator.
6707 LOG(FATAL) << "Unreachable";
6708}
6709
6710void InstructionCodeGeneratorMIPS::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 LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006716 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006717}
6718
6719void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006720 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006721}
6722
6723void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006724 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006725}
6726
6727void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006728 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006729}
6730
6731void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006732 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006733}
6734
6735void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006736 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006737}
6738
6739void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006740 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006741}
6742
6743void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006744 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006745}
6746
6747void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006748 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006749}
6750
6751void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006752 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006753}
6754
6755void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006756 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006757}
6758
6759void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006760 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006761}
6762
6763void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006764 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006765}
6766
6767void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006768 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006769}
6770
6771void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006772 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006773}
6774
6775void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006776 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006777}
6778
6779void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006780 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006781}
6782
6783void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006784 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006785}
6786
6787void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006788 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006789}
6790
6791void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006792 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006793}
6794
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006795void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6796 LocationSummary* locations =
6797 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6798 locations->SetInAt(0, Location::RequiresRegister());
6799}
6800
Alexey Frunze96b66822016-09-10 02:32:44 -07006801void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6802 int32_t lower_bound,
6803 uint32_t num_entries,
6804 HBasicBlock* switch_block,
6805 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006806 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006807 Register temp_reg = TMP;
6808 __ Addiu32(temp_reg, value_reg, -lower_bound);
6809 // Jump to default if index is negative
6810 // Note: We don't check the case that index is positive while value < lower_bound, because in
6811 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6812 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6813
Alexey Frunze96b66822016-09-10 02:32:44 -07006814 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006815 // Jump to successors[0] if value == lower_bound.
6816 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6817 int32_t last_index = 0;
6818 for (; num_entries - last_index > 2; last_index += 2) {
6819 __ Addiu(temp_reg, temp_reg, -2);
6820 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6821 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6822 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6823 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6824 }
6825 if (num_entries - last_index == 2) {
6826 // The last missing case_value.
6827 __ Addiu(temp_reg, temp_reg, -1);
6828 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006829 }
6830
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006831 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006832 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006833 __ B(codegen_->GetLabelOf(default_block));
6834 }
6835}
6836
Alexey Frunze96b66822016-09-10 02:32:44 -07006837void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6838 Register constant_area,
6839 int32_t lower_bound,
6840 uint32_t num_entries,
6841 HBasicBlock* switch_block,
6842 HBasicBlock* default_block) {
6843 // Create a jump table.
6844 std::vector<MipsLabel*> labels(num_entries);
6845 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6846 for (uint32_t i = 0; i < num_entries; i++) {
6847 labels[i] = codegen_->GetLabelOf(successors[i]);
6848 }
6849 JumpTable* table = __ CreateJumpTable(std::move(labels));
6850
6851 // Is the value in range?
6852 __ Addiu32(TMP, value_reg, -lower_bound);
6853 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6854 __ Sltiu(AT, TMP, num_entries);
6855 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6856 } else {
6857 __ LoadConst32(AT, num_entries);
6858 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6859 }
6860
6861 // We are in the range of the table.
6862 // Load the target address from the jump table, indexing by the value.
6863 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6864 __ Sll(TMP, TMP, 2);
6865 __ Addu(TMP, TMP, AT);
6866 __ Lw(TMP, TMP, 0);
6867 // Compute the absolute target address by adding the table start address
6868 // (the table contains offsets to targets relative to its start).
6869 __ Addu(TMP, TMP, AT);
6870 // And jump.
6871 __ Jr(TMP);
6872 __ NopIfNoReordering();
6873}
6874
6875void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6876 int32_t lower_bound = switch_instr->GetStartValue();
6877 uint32_t num_entries = switch_instr->GetNumEntries();
6878 LocationSummary* locations = switch_instr->GetLocations();
6879 Register value_reg = locations->InAt(0).AsRegister<Register>();
6880 HBasicBlock* switch_block = switch_instr->GetBlock();
6881 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6882
6883 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6884 num_entries > kPackedSwitchJumpTableThreshold) {
6885 // R6 uses PC-relative addressing to access the jump table.
6886 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6887 // the jump table and it is implemented by changing HPackedSwitch to
6888 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6889 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6890 GenTableBasedPackedSwitch(value_reg,
6891 ZERO,
6892 lower_bound,
6893 num_entries,
6894 switch_block,
6895 default_block);
6896 } else {
6897 GenPackedSwitchWithCompares(value_reg,
6898 lower_bound,
6899 num_entries,
6900 switch_block,
6901 default_block);
6902 }
6903}
6904
6905void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6906 LocationSummary* locations =
6907 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6908 locations->SetInAt(0, Location::RequiresRegister());
6909 // Constant area pointer (HMipsComputeBaseMethodAddress).
6910 locations->SetInAt(1, Location::RequiresRegister());
6911}
6912
6913void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6914 int32_t lower_bound = switch_instr->GetStartValue();
6915 uint32_t num_entries = switch_instr->GetNumEntries();
6916 LocationSummary* locations = switch_instr->GetLocations();
6917 Register value_reg = locations->InAt(0).AsRegister<Register>();
6918 Register constant_area = locations->InAt(1).AsRegister<Register>();
6919 HBasicBlock* switch_block = switch_instr->GetBlock();
6920 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6921
6922 // This is an R2-only path. HPackedSwitch has been changed to
6923 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6924 // required to address the jump table relative to PC.
6925 GenTableBasedPackedSwitch(value_reg,
6926 constant_area,
6927 lower_bound,
6928 num_entries,
6929 switch_block,
6930 default_block);
6931}
6932
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006933void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6934 HMipsComputeBaseMethodAddress* insn) {
6935 LocationSummary* locations =
6936 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6937 locations->SetOut(Location::RequiresRegister());
6938}
6939
6940void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6941 HMipsComputeBaseMethodAddress* insn) {
6942 LocationSummary* locations = insn->GetLocations();
6943 Register reg = locations->Out().AsRegister<Register>();
6944
6945 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6946
6947 // Generate a dummy PC-relative call to obtain PC.
6948 __ Nal();
6949 // Grab the return address off RA.
6950 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006951 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006952
6953 // Remember this offset (the obtained PC value) for later use with constant area.
6954 __ BindPcRelBaseLabel();
6955}
6956
6957void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6958 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6959 locations->SetOut(Location::RequiresRegister());
6960}
6961
6962void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6963 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6964 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6965 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006966 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6967 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006968}
6969
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006970void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6971 // The trampoline uses the same calling convention as dex calling conventions,
6972 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6973 // the method_idx.
6974 HandleInvoke(invoke);
6975}
6976
6977void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6978 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6979}
6980
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006981void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6982 LocationSummary* locations =
6983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6984 locations->SetInAt(0, Location::RequiresRegister());
6985 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006986}
6987
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006988void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6989 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006990 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006991 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006992 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006993 __ LoadFromOffset(kLoadWord,
6994 locations->Out().AsRegister<Register>(),
6995 locations->InAt(0).AsRegister<Register>(),
6996 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006997 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006998 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006999 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007000 __ LoadFromOffset(kLoadWord,
7001 locations->Out().AsRegister<Register>(),
7002 locations->InAt(0).AsRegister<Register>(),
7003 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007004 __ LoadFromOffset(kLoadWord,
7005 locations->Out().AsRegister<Register>(),
7006 locations->Out().AsRegister<Register>(),
7007 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007008 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007009}
7010
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007011#undef __
7012#undef QUICK_ENTRY_POINT
7013
7014} // namespace mips
7015} // namespace art