blob: 76be74e921b506c3a6679c5aaa3159edac4f219b [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()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800102 Register reg = calling_convention.GetRegisterAt(gp_index);
103 if (reg == A1 || reg == A3) {
104 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200105 gp_index++;
106 }
107 Register low_even = calling_convention.GetRegisterAt(gp_index);
108 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
109 DCHECK_EQ(low_even + 1, high_odd);
110 next_location = Location::RegisterPairLocation(low_even, high_odd);
111 } else {
112 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
113 next_location = Location::DoubleStackSlot(stack_offset);
114 }
115 break;
116 }
117
118 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
119 // will take up the even/odd pair, while floats are stored in even regs only.
120 // On 64 bit FPU, both double and float are stored in even registers only.
121 case Primitive::kPrimFloat:
122 case Primitive::kPrimDouble: {
123 uint32_t float_index = float_index_++;
124 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
125 next_location = Location::FpuRegisterLocation(
126 calling_convention.GetFpuRegisterAt(float_index));
127 } else {
128 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
129 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
130 : Location::StackSlot(stack_offset);
131 }
132 break;
133 }
134
135 case Primitive::kPrimVoid:
136 LOG(FATAL) << "Unexpected parameter type " << type;
137 break;
138 }
139
140 // Space on the stack is reserved for all arguments.
141 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
142
143 return next_location;
144}
145
146Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
147 return MipsReturnLocation(type);
148}
149
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100150// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
151#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700152#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200153
154class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
155 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000156 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200157
158 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
159 LocationSummary* locations = instruction_->GetLocations();
160 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
161 __ Bind(GetEntryLabel());
162 if (instruction_->CanThrowIntoCatchBlock()) {
163 // Live registers will be restored in the catch block if caught.
164 SaveLiveRegisters(codegen, instruction_->GetLocations());
165 }
166 // We're moving two locations to locations that could overlap, so we need a parallel
167 // move resolver.
168 InvokeRuntimeCallingConvention calling_convention;
169 codegen->EmitParallelMoves(locations->InAt(0),
170 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
171 Primitive::kPrimInt,
172 locations->InAt(1),
173 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
174 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100175 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
176 ? kQuickThrowStringBounds
177 : kQuickThrowArrayBounds;
178 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100179 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200180 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
181 }
182
183 bool IsFatal() const OVERRIDE { return true; }
184
185 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
186
187 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200188 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
189};
190
191class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
192 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000193 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200194
195 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
196 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
197 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100198 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200199 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
200 }
201
202 bool IsFatal() const OVERRIDE { return true; }
203
204 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
205
206 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200207 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
208};
209
210class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
211 public:
212 LoadClassSlowPathMIPS(HLoadClass* cls,
213 HInstruction* at,
214 uint32_t dex_pc,
215 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000216 : SlowPathCodeMIPS(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200217 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
218 }
219
220 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000221 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200222 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
223
224 __ Bind(GetEntryLabel());
225 SaveLiveRegisters(codegen, locations);
226
227 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000228 dex::TypeIndex type_index = cls_->GetTypeIndex();
229 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200230
Serban Constantinescufca16662016-07-14 09:21:59 +0100231 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
232 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000233 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200234 if (do_clinit_) {
235 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
236 } else {
237 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
238 }
239
240 // Move the class to the desired location.
241 Location out = locations->Out();
242 if (out.IsValid()) {
243 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 Primitive::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200245 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
246 }
247
248 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000249 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
250 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
251 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
252 DCHECK(out.IsValid());
253 // TODO: Change art_quick_initialize_type/art_quick_initialize_static_storage to
254 // kSaveEverything and use a temporary for the .bss entry address in the fast path,
255 // so that we can avoid another calculation here.
256 bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
257 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
258 DCHECK_NE(out.AsRegister<Register>(), AT);
259 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000260 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000261 mips_codegen->EmitPcRelativeAddressPlaceholder(info, TMP, base);
262 __ StoreToOffset(kStoreWord, out.AsRegister<Register>(), TMP, 0);
263 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200264 __ B(GetExitLabel());
265 }
266
267 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
268
269 private:
270 // The class this slow path will load.
271 HLoadClass* const cls_;
272
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200273 // The dex PC of `at_`.
274 const uint32_t dex_pc_;
275
276 // Whether to initialize the class.
277 const bool do_clinit_;
278
279 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
280};
281
282class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
283 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200285
286 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
287 LocationSummary* locations = instruction_->GetLocations();
288 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
289 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
290
291 __ Bind(GetEntryLabel());
292 SaveLiveRegisters(codegen, locations);
293
294 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000295 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000296 const dex::StringIndex string_index = load->GetStringIndex();
297 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100298 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200299 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
300 Primitive::Type type = instruction_->GetType();
301 mips_codegen->MoveLocation(locations->Out(),
302 calling_convention.GetReturnLocation(type),
303 type);
304
305 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000306
307 // Store the resolved String to the BSS entry.
308 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary for the
309 // .bss entry address in the fast path, so that we can avoid another calculation here.
310 bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
311 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
312 Register out = locations->Out().AsRegister<Register>();
313 DCHECK_NE(out, AT);
314 CodeGeneratorMIPS::PcRelativePatchInfo* info =
315 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
316 mips_codegen->EmitPcRelativeAddressPlaceholder(info, TMP, base);
317 __ StoreToOffset(kStoreWord, out, TMP, 0);
318
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200319 __ B(GetExitLabel());
320 }
321
322 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
323
324 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200325 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
326};
327
328class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
329 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000330 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200331
332 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
333 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
334 __ Bind(GetEntryLabel());
335 if (instruction_->CanThrowIntoCatchBlock()) {
336 // Live registers will be restored in the catch block if caught.
337 SaveLiveRegisters(codegen, instruction_->GetLocations());
338 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100339 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200340 instruction_,
341 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100342 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200343 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
344 }
345
346 bool IsFatal() const OVERRIDE { return true; }
347
348 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
349
350 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
352};
353
354class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
355 public:
356 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000357 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200358
359 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
360 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
361 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100362 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200363 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200364 if (successor_ == nullptr) {
365 __ B(GetReturnLabel());
366 } else {
367 __ B(mips_codegen->GetLabelOf(successor_));
368 }
369 }
370
371 MipsLabel* GetReturnLabel() {
372 DCHECK(successor_ == nullptr);
373 return &return_label_;
374 }
375
376 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
377
378 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200379 // If not null, the block to branch to after the suspend check.
380 HBasicBlock* const successor_;
381
382 // If `successor_` is null, the label to branch to after the suspend check.
383 MipsLabel return_label_;
384
385 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
386};
387
388class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
389 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000390 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200391
392 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
393 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200394 uint32_t dex_pc = instruction_->GetDexPc();
395 DCHECK(instruction_->IsCheckCast()
396 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
397 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
398
399 __ Bind(GetEntryLabel());
400 SaveLiveRegisters(codegen, locations);
401
402 // We're moving two locations to locations that could overlap, so we need a parallel
403 // move resolver.
404 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800405 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200406 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
407 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800408 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200409 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
410 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200411 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100412 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800413 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200414 Primitive::Type ret_type = instruction_->GetType();
415 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
416 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200417 } else {
418 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800419 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
420 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200421 }
422
423 RestoreLiveRegisters(codegen, locations);
424 __ B(GetExitLabel());
425 }
426
427 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
428
429 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200430 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
431};
432
433class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
434 public:
Aart Bik42249c32016-01-07 15:33:50 -0800435 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000436 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200437
438 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800439 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200440 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100441 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000442 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200443 }
444
445 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
446
447 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200448 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
449};
450
451CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
452 const MipsInstructionSetFeatures& isa_features,
453 const CompilerOptions& compiler_options,
454 OptimizingCompilerStats* stats)
455 : CodeGenerator(graph,
456 kNumberOfCoreRegisters,
457 kNumberOfFRegisters,
458 kNumberOfRegisterPairs,
459 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
460 arraysize(kCoreCalleeSaves)),
461 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
462 arraysize(kFpuCalleeSaves)),
463 compiler_options,
464 stats),
465 block_labels_(nullptr),
466 location_builder_(graph, this),
467 instruction_visitor_(graph, this),
468 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100469 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700470 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700471 uint32_literals_(std::less<uint32_t>(),
472 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700473 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
474 boot_image_string_patches_(StringReferenceValueComparator(),
475 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
476 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
477 boot_image_type_patches_(TypeReferenceValueComparator(),
478 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
479 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +0000480 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700481 boot_image_address_patches_(std::less<uint32_t>(),
482 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
483 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200484 // Save RA (containing the return address) to mimic Quick.
485 AddAllocatedRegister(Location::RegisterLocation(RA));
486}
487
488#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100489// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
490#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700491#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200492
493void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
494 // Ensure that we fix up branches.
495 __ FinalizeCode();
496
497 // Adjust native pc offsets in stack maps.
498 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800499 uint32_t old_position =
500 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200501 uint32_t new_position = __ GetAdjustedPosition(old_position);
502 DCHECK_GE(new_position, old_position);
503 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
504 }
505
506 // Adjust pc offsets for the disassembly information.
507 if (disasm_info_ != nullptr) {
508 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
509 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
510 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
511 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
512 it.second.start = __ GetAdjustedPosition(it.second.start);
513 it.second.end = __ GetAdjustedPosition(it.second.end);
514 }
515 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
516 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
517 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
518 }
519 }
520
521 CodeGenerator::Finalize(allocator);
522}
523
524MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
525 return codegen_->GetAssembler();
526}
527
528void ParallelMoveResolverMIPS::EmitMove(size_t index) {
529 DCHECK_LT(index, moves_.size());
530 MoveOperands* move = moves_[index];
531 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
532}
533
534void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
535 DCHECK_LT(index, moves_.size());
536 MoveOperands* move = moves_[index];
537 Primitive::Type type = move->GetType();
538 Location loc1 = move->GetDestination();
539 Location loc2 = move->GetSource();
540
541 DCHECK(!loc1.IsConstant());
542 DCHECK(!loc2.IsConstant());
543
544 if (loc1.Equals(loc2)) {
545 return;
546 }
547
548 if (loc1.IsRegister() && loc2.IsRegister()) {
549 // Swap 2 GPRs.
550 Register r1 = loc1.AsRegister<Register>();
551 Register r2 = loc2.AsRegister<Register>();
552 __ Move(TMP, r2);
553 __ Move(r2, r1);
554 __ Move(r1, TMP);
555 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
556 FRegister f1 = loc1.AsFpuRegister<FRegister>();
557 FRegister f2 = loc2.AsFpuRegister<FRegister>();
558 if (type == Primitive::kPrimFloat) {
559 __ MovS(FTMP, f2);
560 __ MovS(f2, f1);
561 __ MovS(f1, FTMP);
562 } else {
563 DCHECK_EQ(type, Primitive::kPrimDouble);
564 __ MovD(FTMP, f2);
565 __ MovD(f2, f1);
566 __ MovD(f1, FTMP);
567 }
568 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
569 (loc1.IsFpuRegister() && loc2.IsRegister())) {
570 // Swap FPR and GPR.
571 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
572 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
573 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200574 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200575 __ Move(TMP, r2);
576 __ Mfc1(r2, f1);
577 __ Mtc1(TMP, f1);
578 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
579 // Swap 2 GPR register pairs.
580 Register r1 = loc1.AsRegisterPairLow<Register>();
581 Register r2 = loc2.AsRegisterPairLow<Register>();
582 __ Move(TMP, r2);
583 __ Move(r2, r1);
584 __ Move(r1, TMP);
585 r1 = loc1.AsRegisterPairHigh<Register>();
586 r2 = loc2.AsRegisterPairHigh<Register>();
587 __ Move(TMP, r2);
588 __ Move(r2, r1);
589 __ Move(r1, TMP);
590 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
591 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
592 // Swap FPR and GPR register pair.
593 DCHECK_EQ(type, Primitive::kPrimDouble);
594 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
595 : loc2.AsFpuRegister<FRegister>();
596 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
597 : loc2.AsRegisterPairLow<Register>();
598 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
599 : loc2.AsRegisterPairHigh<Register>();
600 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
601 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
602 // unpredictable and the following mfch1 will fail.
603 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800604 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200605 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800606 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200607 __ Move(r2_l, TMP);
608 __ Move(r2_h, AT);
609 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
610 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
611 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
612 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000613 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
614 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200615 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
616 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000617 __ 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>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200626 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000627 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
628 : loc2.GetHighStackIndex(kMipsWordSize);
629 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000630 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000631 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000632 __ Move(TMP, reg_h);
633 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
634 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200635 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
636 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
637 : loc2.AsFpuRegister<FRegister>();
638 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
639 if (type == Primitive::kPrimFloat) {
640 __ MovS(FTMP, reg);
641 __ LoadSFromOffset(reg, SP, offset);
642 __ StoreSToOffset(FTMP, SP, offset);
643 } else {
644 DCHECK_EQ(type, Primitive::kPrimDouble);
645 __ MovD(FTMP, reg);
646 __ LoadDFromOffset(reg, SP, offset);
647 __ StoreDToOffset(FTMP, SP, offset);
648 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200649 } else {
650 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
651 }
652}
653
654void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
655 __ Pop(static_cast<Register>(reg));
656}
657
658void ParallelMoveResolverMIPS::SpillScratch(int reg) {
659 __ Push(static_cast<Register>(reg));
660}
661
662void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
663 // Allocate a scratch register other than TMP, if available.
664 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
665 // automatically unspilled when the scratch scope object is destroyed).
666 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
667 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
668 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
669 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
670 __ LoadFromOffset(kLoadWord,
671 Register(ensure_scratch.GetRegister()),
672 SP,
673 index1 + stack_offset);
674 __ LoadFromOffset(kLoadWord,
675 TMP,
676 SP,
677 index2 + stack_offset);
678 __ StoreToOffset(kStoreWord,
679 Register(ensure_scratch.GetRegister()),
680 SP,
681 index2 + stack_offset);
682 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
683 }
684}
685
Alexey Frunze73296a72016-06-03 22:51:46 -0700686void CodeGeneratorMIPS::ComputeSpillMask() {
687 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
688 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
689 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
690 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
691 // registers, include the ZERO register to force alignment of FPU callee-saved registers
692 // within the stack frame.
693 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
694 core_spill_mask_ |= (1 << ZERO);
695 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700696}
697
698bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700699 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700700 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
701 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
702 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700703 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
704 // saved in an unused temporary register) and saving of RA and the current method pointer
705 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700706 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700707}
708
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200709static dwarf::Reg DWARFReg(Register reg) {
710 return dwarf::Reg::MipsCore(static_cast<int>(reg));
711}
712
713// TODO: mapping of floating-point registers to DWARF.
714
715void CodeGeneratorMIPS::GenerateFrameEntry() {
716 __ Bind(&frame_entry_label_);
717
718 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
719
720 if (do_overflow_check) {
721 __ LoadFromOffset(kLoadWord,
722 ZERO,
723 SP,
724 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
725 RecordPcInfo(nullptr, 0);
726 }
727
728 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700729 CHECK_EQ(fpu_spill_mask_, 0u);
730 CHECK_EQ(core_spill_mask_, 1u << RA);
731 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200732 return;
733 }
734
735 // Make sure the frame size isn't unreasonably large.
736 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
737 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
738 }
739
740 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200741
Alexey Frunze73296a72016-06-03 22:51:46 -0700742 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200743 __ IncreaseFrameSize(ofs);
744
Alexey Frunze73296a72016-06-03 22:51:46 -0700745 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
746 Register reg = static_cast<Register>(MostSignificantBit(mask));
747 mask ^= 1u << reg;
748 ofs -= kMipsWordSize;
749 // The ZERO register is only included for alignment.
750 if (reg != ZERO) {
751 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200752 __ cfi().RelOffset(DWARFReg(reg), ofs);
753 }
754 }
755
Alexey Frunze73296a72016-06-03 22:51:46 -0700756 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
757 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
758 mask ^= 1u << reg;
759 ofs -= kMipsDoublewordSize;
760 __ StoreDToOffset(reg, SP, ofs);
761 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200762 }
763
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100764 // Save the current method if we need it. Note that we do not
765 // do this in HCurrentMethod, as the instruction might have been removed
766 // in the SSA graph.
767 if (RequiresCurrentMethod()) {
768 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
769 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +0100770
771 if (GetGraph()->HasShouldDeoptimizeFlag()) {
772 // Initialize should deoptimize flag to 0.
773 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
774 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200775}
776
777void CodeGeneratorMIPS::GenerateFrameExit() {
778 __ cfi().RememberState();
779
780 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200781 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200782
Alexey Frunze73296a72016-06-03 22:51:46 -0700783 // For better instruction scheduling restore RA before other registers.
784 uint32_t ofs = GetFrameSize();
785 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
786 Register reg = static_cast<Register>(MostSignificantBit(mask));
787 mask ^= 1u << reg;
788 ofs -= kMipsWordSize;
789 // The ZERO register is only included for alignment.
790 if (reg != ZERO) {
791 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200792 __ cfi().Restore(DWARFReg(reg));
793 }
794 }
795
Alexey Frunze73296a72016-06-03 22:51:46 -0700796 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
797 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
798 mask ^= 1u << reg;
799 ofs -= kMipsDoublewordSize;
800 __ LoadDFromOffset(reg, SP, ofs);
801 // TODO: __ cfi().Restore(DWARFReg(reg));
802 }
803
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700804 size_t frame_size = GetFrameSize();
805 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
806 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
807 bool reordering = __ SetReorder(false);
808 if (exchange) {
809 __ Jr(RA);
810 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
811 } else {
812 __ DecreaseFrameSize(frame_size);
813 __ Jr(RA);
814 __ Nop(); // In delay slot.
815 }
816 __ SetReorder(reordering);
817 } else {
818 __ Jr(RA);
819 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200820 }
821
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200822 __ cfi().RestoreState();
823 __ cfi().DefCFAOffset(GetFrameSize());
824}
825
826void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
827 __ Bind(GetLabelOf(block));
828}
829
830void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
831 if (src.Equals(dst)) {
832 return;
833 }
834
835 if (src.IsConstant()) {
836 MoveConstant(dst, src.GetConstant());
837 } else {
838 if (Primitive::Is64BitType(dst_type)) {
839 Move64(dst, src);
840 } else {
841 Move32(dst, src);
842 }
843 }
844}
845
846void CodeGeneratorMIPS::Move32(Location destination, Location source) {
847 if (source.Equals(destination)) {
848 return;
849 }
850
851 if (destination.IsRegister()) {
852 if (source.IsRegister()) {
853 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
854 } else if (source.IsFpuRegister()) {
855 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
856 } else {
857 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
858 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
859 }
860 } else if (destination.IsFpuRegister()) {
861 if (source.IsRegister()) {
862 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
863 } else if (source.IsFpuRegister()) {
864 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
865 } else {
866 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
867 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
868 }
869 } else {
870 DCHECK(destination.IsStackSlot()) << destination;
871 if (source.IsRegister()) {
872 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
873 } else if (source.IsFpuRegister()) {
874 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
875 } else {
876 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
877 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
878 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
879 }
880 }
881}
882
883void CodeGeneratorMIPS::Move64(Location destination, Location source) {
884 if (source.Equals(destination)) {
885 return;
886 }
887
888 if (destination.IsRegisterPair()) {
889 if (source.IsRegisterPair()) {
890 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
891 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
892 } else if (source.IsFpuRegister()) {
893 Register dst_high = destination.AsRegisterPairHigh<Register>();
894 Register dst_low = destination.AsRegisterPairLow<Register>();
895 FRegister src = source.AsFpuRegister<FRegister>();
896 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800897 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200898 } else {
899 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
900 int32_t off = source.GetStackIndex();
901 Register r = destination.AsRegisterPairLow<Register>();
902 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
903 }
904 } else if (destination.IsFpuRegister()) {
905 if (source.IsRegisterPair()) {
906 FRegister dst = destination.AsFpuRegister<FRegister>();
907 Register src_high = source.AsRegisterPairHigh<Register>();
908 Register src_low = source.AsRegisterPairLow<Register>();
909 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800910 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200911 } else if (source.IsFpuRegister()) {
912 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
913 } else {
914 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
915 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
916 }
917 } else {
918 DCHECK(destination.IsDoubleStackSlot()) << destination;
919 int32_t off = destination.GetStackIndex();
920 if (source.IsRegisterPair()) {
921 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
922 } else if (source.IsFpuRegister()) {
923 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
924 } else {
925 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
926 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
927 __ StoreToOffset(kStoreWord, TMP, SP, off);
928 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
929 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
930 }
931 }
932}
933
934void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
935 if (c->IsIntConstant() || c->IsNullConstant()) {
936 // Move 32 bit constant.
937 int32_t value = GetInt32ValueOf(c);
938 if (destination.IsRegister()) {
939 Register dst = destination.AsRegister<Register>();
940 __ LoadConst32(dst, value);
941 } else {
942 DCHECK(destination.IsStackSlot())
943 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700944 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200945 }
946 } else if (c->IsLongConstant()) {
947 // Move 64 bit constant.
948 int64_t value = GetInt64ValueOf(c);
949 if (destination.IsRegisterPair()) {
950 Register r_h = destination.AsRegisterPairHigh<Register>();
951 Register r_l = destination.AsRegisterPairLow<Register>();
952 __ LoadConst64(r_h, r_l, value);
953 } else {
954 DCHECK(destination.IsDoubleStackSlot())
955 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700956 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200957 }
958 } else if (c->IsFloatConstant()) {
959 // Move 32 bit float constant.
960 int32_t value = GetInt32ValueOf(c);
961 if (destination.IsFpuRegister()) {
962 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
963 } else {
964 DCHECK(destination.IsStackSlot())
965 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700966 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200967 }
968 } else {
969 // Move 64 bit double constant.
970 DCHECK(c->IsDoubleConstant()) << c->DebugName();
971 int64_t value = GetInt64ValueOf(c);
972 if (destination.IsFpuRegister()) {
973 FRegister fd = destination.AsFpuRegister<FRegister>();
974 __ LoadDConst64(fd, value, TMP);
975 } else {
976 DCHECK(destination.IsDoubleStackSlot())
977 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700978 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200979 }
980 }
981}
982
983void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
984 DCHECK(destination.IsRegister());
985 Register dst = destination.AsRegister<Register>();
986 __ LoadConst32(dst, value);
987}
988
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200989void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
990 if (location.IsRegister()) {
991 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700992 } else if (location.IsRegisterPair()) {
993 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
994 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200995 } else {
996 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
997 }
998}
999
Vladimir Markoaad75c62016-10-03 08:46:48 +00001000template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1001inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1002 const ArenaDeque<PcRelativePatchInfo>& infos,
1003 ArenaVector<LinkerPatch>* linker_patches) {
1004 for (const PcRelativePatchInfo& info : infos) {
1005 const DexFile& dex_file = info.target_dex_file;
1006 size_t offset_or_index = info.offset_or_index;
1007 DCHECK(info.high_label.IsBound());
1008 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1009 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1010 // the assembler's base label used for PC-relative addressing.
1011 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1012 ? __ GetLabelLocation(&info.pc_rel_label)
1013 : __ GetPcRelBaseLabelLocation();
1014 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1015 }
1016}
1017
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001018void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1019 DCHECK(linker_patches->empty());
1020 size_t size =
Alexey Frunze06a46c42016-07-19 15:00:40 -07001021 pc_relative_dex_cache_patches_.size() +
1022 pc_relative_string_patches_.size() +
1023 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001024 type_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001025 boot_image_string_patches_.size() +
1026 boot_image_type_patches_.size() +
1027 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001028 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001029 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1030 linker_patches);
1031 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001032 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00001033 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1034 linker_patches);
1035 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001036 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1037 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001038 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1039 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001040 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001041 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1042 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001043 for (const auto& entry : boot_image_string_patches_) {
1044 const StringReference& target_string = entry.first;
1045 Literal* literal = entry.second;
1046 DCHECK(literal->GetLabel()->IsBound());
1047 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1048 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1049 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001050 target_string.string_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001051 }
1052 for (const auto& entry : boot_image_type_patches_) {
1053 const TypeReference& target_type = entry.first;
1054 Literal* literal = entry.second;
1055 DCHECK(literal->GetLabel()->IsBound());
1056 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1057 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1058 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001059 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001060 }
1061 for (const auto& entry : boot_image_address_patches_) {
1062 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1063 Literal* literal = entry.second;
1064 DCHECK(literal->GetLabel()->IsBound());
1065 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1066 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1067 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001068 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001069}
1070
1071CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001072 const DexFile& dex_file, dex::StringIndex string_index) {
1073 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001074}
1075
1076CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001077 const DexFile& dex_file, dex::TypeIndex type_index) {
1078 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001079}
1080
Vladimir Marko1998cd02017-01-13 13:02:58 +00001081CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
1082 const DexFile& dex_file, dex::TypeIndex type_index) {
1083 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1084}
1085
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001086CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1087 const DexFile& dex_file, uint32_t element_offset) {
1088 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1089}
1090
1091CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1092 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1093 patches->emplace_back(dex_file, offset_or_index);
1094 return &patches->back();
1095}
1096
Alexey Frunze06a46c42016-07-19 15:00:40 -07001097Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1098 return map->GetOrCreate(
1099 value,
1100 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1101}
1102
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001103Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1104 MethodToLiteralMap* map) {
1105 return map->GetOrCreate(
1106 target_method,
1107 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1108}
1109
Alexey Frunze06a46c42016-07-19 15:00:40 -07001110Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001111 dex::StringIndex string_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001112 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,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001118 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001119 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 Jakovljevice114da22016-12-26 14:21:43 +01001160void CodeGeneratorMIPS::MarkGCCard(Register object,
1161 Register value,
1162 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001163 MipsLabel done;
1164 Register card = AT;
1165 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001166 if (value_can_be_null) {
1167 __ Beqz(value, &done);
1168 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001169 __ LoadFromOffset(kLoadWord,
1170 card,
1171 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001172 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001173 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1174 __ Addu(temp, card, temp);
1175 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001176 if (value_can_be_null) {
1177 __ Bind(&done);
1178 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001179}
1180
David Brazdil58282f42016-01-14 12:45:10 +00001181void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001182 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1183 blocked_core_registers_[ZERO] = true;
1184 blocked_core_registers_[K0] = true;
1185 blocked_core_registers_[K1] = true;
1186 blocked_core_registers_[GP] = true;
1187 blocked_core_registers_[SP] = true;
1188 blocked_core_registers_[RA] = true;
1189
1190 // AT and TMP(T8) are used as temporary/scratch registers
1191 // (similar to how AT is used by MIPS assemblers).
1192 blocked_core_registers_[AT] = true;
1193 blocked_core_registers_[TMP] = true;
1194 blocked_fpu_registers_[FTMP] = true;
1195
1196 // Reserve suspend and thread registers.
1197 blocked_core_registers_[S0] = true;
1198 blocked_core_registers_[TR] = true;
1199
1200 // Reserve T9 for function calls
1201 blocked_core_registers_[T9] = true;
1202
1203 // Reserve odd-numbered FPU registers.
1204 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1205 blocked_fpu_registers_[i] = true;
1206 }
1207
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001208 if (GetGraph()->IsDebuggable()) {
1209 // Stubs do not save callee-save floating point registers. If the graph
1210 // is debuggable, we need to deal with these registers differently. For
1211 // now, just block them.
1212 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1213 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1214 }
1215 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001216}
1217
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001218size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1219 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1220 return kMipsWordSize;
1221}
1222
1223size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1224 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1225 return kMipsWordSize;
1226}
1227
1228size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1229 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1230 return kMipsDoublewordSize;
1231}
1232
1233size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1234 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1235 return kMipsDoublewordSize;
1236}
1237
1238void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001239 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001240}
1241
1242void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001243 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001244}
1245
Serban Constantinescufca16662016-07-14 09:21:59 +01001246constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1247
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001248void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1249 HInstruction* instruction,
1250 uint32_t dex_pc,
1251 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001252 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001253 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001254 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001255 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001256 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001257 // Reserve argument space on stack (for $a0-$a3) for
1258 // entrypoints that directly reference native implementations.
1259 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001260 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001261 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001262 } else {
1263 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001264 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001265 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001266 if (EntrypointRequiresStackMap(entrypoint)) {
1267 RecordPcInfo(instruction, dex_pc, slow_path);
1268 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001269}
1270
1271void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1272 Register class_reg) {
1273 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1274 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1275 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1276 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1277 __ Sync(0);
1278 __ Bind(slow_path->GetExitLabel());
1279}
1280
1281void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1282 __ Sync(0); // Only stype 0 is supported.
1283}
1284
1285void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1286 HBasicBlock* successor) {
1287 SuspendCheckSlowPathMIPS* slow_path =
1288 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1289 codegen_->AddSlowPath(slow_path);
1290
1291 __ LoadFromOffset(kLoadUnsignedHalfword,
1292 TMP,
1293 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001294 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001295 if (successor == nullptr) {
1296 __ Bnez(TMP, slow_path->GetEntryLabel());
1297 __ Bind(slow_path->GetReturnLabel());
1298 } else {
1299 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1300 __ B(slow_path->GetEntryLabel());
1301 // slow_path will return to GetLabelOf(successor).
1302 }
1303}
1304
1305InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1306 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001307 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001308 assembler_(codegen->GetAssembler()),
1309 codegen_(codegen) {}
1310
1311void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1312 DCHECK_EQ(instruction->InputCount(), 2U);
1313 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1314 Primitive::Type type = instruction->GetResultType();
1315 switch (type) {
1316 case Primitive::kPrimInt: {
1317 locations->SetInAt(0, Location::RequiresRegister());
1318 HInstruction* right = instruction->InputAt(1);
1319 bool can_use_imm = false;
1320 if (right->IsConstant()) {
1321 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1322 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1323 can_use_imm = IsUint<16>(imm);
1324 } else if (instruction->IsAdd()) {
1325 can_use_imm = IsInt<16>(imm);
1326 } else {
1327 DCHECK(instruction->IsSub());
1328 can_use_imm = IsInt<16>(-imm);
1329 }
1330 }
1331 if (can_use_imm)
1332 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1333 else
1334 locations->SetInAt(1, Location::RequiresRegister());
1335 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1336 break;
1337 }
1338
1339 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001340 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001341 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1342 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001343 break;
1344 }
1345
1346 case Primitive::kPrimFloat:
1347 case Primitive::kPrimDouble:
1348 DCHECK(instruction->IsAdd() || instruction->IsSub());
1349 locations->SetInAt(0, Location::RequiresFpuRegister());
1350 locations->SetInAt(1, Location::RequiresFpuRegister());
1351 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1352 break;
1353
1354 default:
1355 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1356 }
1357}
1358
1359void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1360 Primitive::Type type = instruction->GetType();
1361 LocationSummary* locations = instruction->GetLocations();
1362
1363 switch (type) {
1364 case Primitive::kPrimInt: {
1365 Register dst = locations->Out().AsRegister<Register>();
1366 Register lhs = locations->InAt(0).AsRegister<Register>();
1367 Location rhs_location = locations->InAt(1);
1368
1369 Register rhs_reg = ZERO;
1370 int32_t rhs_imm = 0;
1371 bool use_imm = rhs_location.IsConstant();
1372 if (use_imm) {
1373 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1374 } else {
1375 rhs_reg = rhs_location.AsRegister<Register>();
1376 }
1377
1378 if (instruction->IsAnd()) {
1379 if (use_imm)
1380 __ Andi(dst, lhs, rhs_imm);
1381 else
1382 __ And(dst, lhs, rhs_reg);
1383 } else if (instruction->IsOr()) {
1384 if (use_imm)
1385 __ Ori(dst, lhs, rhs_imm);
1386 else
1387 __ Or(dst, lhs, rhs_reg);
1388 } else if (instruction->IsXor()) {
1389 if (use_imm)
1390 __ Xori(dst, lhs, rhs_imm);
1391 else
1392 __ Xor(dst, lhs, rhs_reg);
1393 } else if (instruction->IsAdd()) {
1394 if (use_imm)
1395 __ Addiu(dst, lhs, rhs_imm);
1396 else
1397 __ Addu(dst, lhs, rhs_reg);
1398 } else {
1399 DCHECK(instruction->IsSub());
1400 if (use_imm)
1401 __ Addiu(dst, lhs, -rhs_imm);
1402 else
1403 __ Subu(dst, lhs, rhs_reg);
1404 }
1405 break;
1406 }
1407
1408 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001409 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1410 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1411 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1412 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001413 Location rhs_location = locations->InAt(1);
1414 bool use_imm = rhs_location.IsConstant();
1415 if (!use_imm) {
1416 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1417 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1418 if (instruction->IsAnd()) {
1419 __ And(dst_low, lhs_low, rhs_low);
1420 __ And(dst_high, lhs_high, rhs_high);
1421 } else if (instruction->IsOr()) {
1422 __ Or(dst_low, lhs_low, rhs_low);
1423 __ Or(dst_high, lhs_high, rhs_high);
1424 } else if (instruction->IsXor()) {
1425 __ Xor(dst_low, lhs_low, rhs_low);
1426 __ Xor(dst_high, lhs_high, rhs_high);
1427 } else if (instruction->IsAdd()) {
1428 if (lhs_low == rhs_low) {
1429 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1430 __ Slt(TMP, lhs_low, ZERO);
1431 __ Addu(dst_low, lhs_low, rhs_low);
1432 } else {
1433 __ Addu(dst_low, lhs_low, rhs_low);
1434 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1435 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1436 }
1437 __ Addu(dst_high, lhs_high, rhs_high);
1438 __ Addu(dst_high, dst_high, TMP);
1439 } else {
1440 DCHECK(instruction->IsSub());
1441 __ Sltu(TMP, lhs_low, rhs_low);
1442 __ Subu(dst_low, lhs_low, rhs_low);
1443 __ Subu(dst_high, lhs_high, rhs_high);
1444 __ Subu(dst_high, dst_high, TMP);
1445 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001446 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001447 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1448 if (instruction->IsOr()) {
1449 uint32_t low = Low32Bits(value);
1450 uint32_t high = High32Bits(value);
1451 if (IsUint<16>(low)) {
1452 if (dst_low != lhs_low || low != 0) {
1453 __ Ori(dst_low, lhs_low, low);
1454 }
1455 } else {
1456 __ LoadConst32(TMP, low);
1457 __ Or(dst_low, lhs_low, TMP);
1458 }
1459 if (IsUint<16>(high)) {
1460 if (dst_high != lhs_high || high != 0) {
1461 __ Ori(dst_high, lhs_high, high);
1462 }
1463 } else {
1464 if (high != low) {
1465 __ LoadConst32(TMP, high);
1466 }
1467 __ Or(dst_high, lhs_high, TMP);
1468 }
1469 } else if (instruction->IsXor()) {
1470 uint32_t low = Low32Bits(value);
1471 uint32_t high = High32Bits(value);
1472 if (IsUint<16>(low)) {
1473 if (dst_low != lhs_low || low != 0) {
1474 __ Xori(dst_low, lhs_low, low);
1475 }
1476 } else {
1477 __ LoadConst32(TMP, low);
1478 __ Xor(dst_low, lhs_low, TMP);
1479 }
1480 if (IsUint<16>(high)) {
1481 if (dst_high != lhs_high || high != 0) {
1482 __ Xori(dst_high, lhs_high, high);
1483 }
1484 } else {
1485 if (high != low) {
1486 __ LoadConst32(TMP, high);
1487 }
1488 __ Xor(dst_high, lhs_high, TMP);
1489 }
1490 } else if (instruction->IsAnd()) {
1491 uint32_t low = Low32Bits(value);
1492 uint32_t high = High32Bits(value);
1493 if (IsUint<16>(low)) {
1494 __ Andi(dst_low, lhs_low, low);
1495 } else if (low != 0xFFFFFFFF) {
1496 __ LoadConst32(TMP, low);
1497 __ And(dst_low, lhs_low, TMP);
1498 } else if (dst_low != lhs_low) {
1499 __ Move(dst_low, lhs_low);
1500 }
1501 if (IsUint<16>(high)) {
1502 __ Andi(dst_high, lhs_high, high);
1503 } else if (high != 0xFFFFFFFF) {
1504 if (high != low) {
1505 __ LoadConst32(TMP, high);
1506 }
1507 __ And(dst_high, lhs_high, TMP);
1508 } else if (dst_high != lhs_high) {
1509 __ Move(dst_high, lhs_high);
1510 }
1511 } else {
1512 if (instruction->IsSub()) {
1513 value = -value;
1514 } else {
1515 DCHECK(instruction->IsAdd());
1516 }
1517 int32_t low = Low32Bits(value);
1518 int32_t high = High32Bits(value);
1519 if (IsInt<16>(low)) {
1520 if (dst_low != lhs_low || low != 0) {
1521 __ Addiu(dst_low, lhs_low, low);
1522 }
1523 if (low != 0) {
1524 __ Sltiu(AT, dst_low, low);
1525 }
1526 } else {
1527 __ LoadConst32(TMP, low);
1528 __ Addu(dst_low, lhs_low, TMP);
1529 __ Sltu(AT, dst_low, TMP);
1530 }
1531 if (IsInt<16>(high)) {
1532 if (dst_high != lhs_high || high != 0) {
1533 __ Addiu(dst_high, lhs_high, high);
1534 }
1535 } else {
1536 if (high != low) {
1537 __ LoadConst32(TMP, high);
1538 }
1539 __ Addu(dst_high, lhs_high, TMP);
1540 }
1541 if (low != 0) {
1542 __ Addu(dst_high, dst_high, AT);
1543 }
1544 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001545 }
1546 break;
1547 }
1548
1549 case Primitive::kPrimFloat:
1550 case Primitive::kPrimDouble: {
1551 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1552 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1553 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1554 if (instruction->IsAdd()) {
1555 if (type == Primitive::kPrimFloat) {
1556 __ AddS(dst, lhs, rhs);
1557 } else {
1558 __ AddD(dst, lhs, rhs);
1559 }
1560 } else {
1561 DCHECK(instruction->IsSub());
1562 if (type == Primitive::kPrimFloat) {
1563 __ SubS(dst, lhs, rhs);
1564 } else {
1565 __ SubD(dst, lhs, rhs);
1566 }
1567 }
1568 break;
1569 }
1570
1571 default:
1572 LOG(FATAL) << "Unexpected binary operation type " << type;
1573 }
1574}
1575
1576void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001577 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001578
1579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1580 Primitive::Type type = instr->GetResultType();
1581 switch (type) {
1582 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001583 locations->SetInAt(0, Location::RequiresRegister());
1584 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1585 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1586 break;
1587 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001588 locations->SetInAt(0, Location::RequiresRegister());
1589 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1590 locations->SetOut(Location::RequiresRegister());
1591 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001592 default:
1593 LOG(FATAL) << "Unexpected shift type " << type;
1594 }
1595}
1596
1597static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1598
1599void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001600 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001601 LocationSummary* locations = instr->GetLocations();
1602 Primitive::Type type = instr->GetType();
1603
1604 Location rhs_location = locations->InAt(1);
1605 bool use_imm = rhs_location.IsConstant();
1606 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1607 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001608 const uint32_t shift_mask =
1609 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001610 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001611 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1612 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001613
1614 switch (type) {
1615 case Primitive::kPrimInt: {
1616 Register dst = locations->Out().AsRegister<Register>();
1617 Register lhs = locations->InAt(0).AsRegister<Register>();
1618 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001619 if (shift_value == 0) {
1620 if (dst != lhs) {
1621 __ Move(dst, lhs);
1622 }
1623 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001624 __ Sll(dst, lhs, shift_value);
1625 } else if (instr->IsShr()) {
1626 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001627 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001628 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001629 } else {
1630 if (has_ins_rotr) {
1631 __ Rotr(dst, lhs, shift_value);
1632 } else {
1633 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1634 __ Srl(dst, lhs, shift_value);
1635 __ Or(dst, dst, TMP);
1636 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001637 }
1638 } else {
1639 if (instr->IsShl()) {
1640 __ Sllv(dst, lhs, rhs_reg);
1641 } else if (instr->IsShr()) {
1642 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001643 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001644 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001645 } else {
1646 if (has_ins_rotr) {
1647 __ Rotrv(dst, lhs, rhs_reg);
1648 } else {
1649 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001650 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1651 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1652 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1653 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1654 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001655 __ Sllv(TMP, lhs, TMP);
1656 __ Srlv(dst, lhs, rhs_reg);
1657 __ Or(dst, dst, TMP);
1658 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001659 }
1660 }
1661 break;
1662 }
1663
1664 case Primitive::kPrimLong: {
1665 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1666 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1667 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1668 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1669 if (use_imm) {
1670 if (shift_value == 0) {
1671 codegen_->Move64(locations->Out(), locations->InAt(0));
1672 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001673 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001674 if (instr->IsShl()) {
1675 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1676 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1677 __ Sll(dst_low, lhs_low, shift_value);
1678 } else if (instr->IsShr()) {
1679 __ Srl(dst_low, lhs_low, shift_value);
1680 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1681 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001682 } else if (instr->IsUShr()) {
1683 __ Srl(dst_low, lhs_low, shift_value);
1684 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1685 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001686 } else {
1687 __ Srl(dst_low, lhs_low, shift_value);
1688 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1689 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001690 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001691 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001692 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001693 if (instr->IsShl()) {
1694 __ Sll(dst_low, lhs_low, shift_value);
1695 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1696 __ Sll(dst_high, lhs_high, shift_value);
1697 __ Or(dst_high, dst_high, TMP);
1698 } else if (instr->IsShr()) {
1699 __ Sra(dst_high, lhs_high, shift_value);
1700 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1701 __ Srl(dst_low, lhs_low, shift_value);
1702 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001703 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001704 __ Srl(dst_high, lhs_high, shift_value);
1705 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1706 __ Srl(dst_low, lhs_low, shift_value);
1707 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001708 } else {
1709 __ Srl(TMP, lhs_low, shift_value);
1710 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1711 __ Or(dst_low, dst_low, TMP);
1712 __ Srl(TMP, lhs_high, shift_value);
1713 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1714 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001715 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001716 }
1717 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001718 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001719 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001720 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001721 __ Move(dst_low, ZERO);
1722 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001723 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001724 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001725 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001726 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001727 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001728 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001729 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001730 // 64-bit rotation by 32 is just a swap.
1731 __ Move(dst_low, lhs_high);
1732 __ Move(dst_high, lhs_low);
1733 } else {
1734 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001735 __ Srl(dst_low, lhs_high, shift_value_high);
1736 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1737 __ Srl(dst_high, lhs_low, shift_value_high);
1738 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001739 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001740 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1741 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001742 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001743 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1744 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001745 __ Or(dst_high, dst_high, TMP);
1746 }
1747 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001748 }
1749 }
1750 } else {
1751 MipsLabel done;
1752 if (instr->IsShl()) {
1753 __ Sllv(dst_low, lhs_low, rhs_reg);
1754 __ Nor(AT, ZERO, rhs_reg);
1755 __ Srl(TMP, lhs_low, 1);
1756 __ Srlv(TMP, TMP, AT);
1757 __ Sllv(dst_high, lhs_high, rhs_reg);
1758 __ Or(dst_high, dst_high, TMP);
1759 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1760 __ Beqz(TMP, &done);
1761 __ Move(dst_high, dst_low);
1762 __ Move(dst_low, ZERO);
1763 } else if (instr->IsShr()) {
1764 __ Srav(dst_high, lhs_high, rhs_reg);
1765 __ Nor(AT, ZERO, rhs_reg);
1766 __ Sll(TMP, lhs_high, 1);
1767 __ Sllv(TMP, TMP, AT);
1768 __ Srlv(dst_low, lhs_low, rhs_reg);
1769 __ Or(dst_low, dst_low, TMP);
1770 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1771 __ Beqz(TMP, &done);
1772 __ Move(dst_low, dst_high);
1773 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001774 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001775 __ Srlv(dst_high, lhs_high, rhs_reg);
1776 __ Nor(AT, ZERO, rhs_reg);
1777 __ Sll(TMP, lhs_high, 1);
1778 __ Sllv(TMP, TMP, AT);
1779 __ Srlv(dst_low, lhs_low, rhs_reg);
1780 __ Or(dst_low, dst_low, TMP);
1781 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1782 __ Beqz(TMP, &done);
1783 __ Move(dst_low, dst_high);
1784 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001785 } else {
1786 __ Nor(AT, ZERO, rhs_reg);
1787 __ Srlv(TMP, lhs_low, rhs_reg);
1788 __ Sll(dst_low, lhs_high, 1);
1789 __ Sllv(dst_low, dst_low, AT);
1790 __ Or(dst_low, dst_low, TMP);
1791 __ Srlv(TMP, lhs_high, rhs_reg);
1792 __ Sll(dst_high, lhs_low, 1);
1793 __ Sllv(dst_high, dst_high, AT);
1794 __ Or(dst_high, dst_high, TMP);
1795 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1796 __ Beqz(TMP, &done);
1797 __ Move(TMP, dst_high);
1798 __ Move(dst_high, dst_low);
1799 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001800 }
1801 __ Bind(&done);
1802 }
1803 break;
1804 }
1805
1806 default:
1807 LOG(FATAL) << "Unexpected shift operation type " << type;
1808 }
1809}
1810
1811void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1812 HandleBinaryOp(instruction);
1813}
1814
1815void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1816 HandleBinaryOp(instruction);
1817}
1818
1819void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1820 HandleBinaryOp(instruction);
1821}
1822
1823void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1824 HandleBinaryOp(instruction);
1825}
1826
1827void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1828 LocationSummary* locations =
1829 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1830 locations->SetInAt(0, Location::RequiresRegister());
1831 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1832 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1833 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1834 } else {
1835 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1836 }
1837}
1838
Alexey Frunze2923db72016-08-20 01:55:47 -07001839auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1840 auto null_checker = [this, instruction]() {
1841 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1842 };
1843 return null_checker;
1844}
1845
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001846void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1847 LocationSummary* locations = instruction->GetLocations();
1848 Register obj = locations->InAt(0).AsRegister<Register>();
1849 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001850 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001851 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001852
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001853 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001854 switch (type) {
1855 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001856 Register out = locations->Out().AsRegister<Register>();
1857 if (index.IsConstant()) {
1858 size_t offset =
1859 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001860 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001861 } else {
1862 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001863 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001864 }
1865 break;
1866 }
1867
1868 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001869 Register out = locations->Out().AsRegister<Register>();
1870 if (index.IsConstant()) {
1871 size_t offset =
1872 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001873 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001874 } else {
1875 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001876 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001877 }
1878 break;
1879 }
1880
1881 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001882 Register out = locations->Out().AsRegister<Register>();
1883 if (index.IsConstant()) {
1884 size_t offset =
1885 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001886 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001887 } else {
1888 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1889 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001890 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001891 }
1892 break;
1893 }
1894
1895 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001896 Register out = locations->Out().AsRegister<Register>();
1897 if (index.IsConstant()) {
1898 size_t offset =
1899 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001900 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001901 } else {
1902 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1903 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001904 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001905 }
1906 break;
1907 }
1908
1909 case Primitive::kPrimInt:
1910 case Primitive::kPrimNot: {
1911 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001912 Register out = locations->Out().AsRegister<Register>();
1913 if (index.IsConstant()) {
1914 size_t offset =
1915 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001916 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001917 } else {
1918 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1919 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001920 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001921 }
1922 break;
1923 }
1924
1925 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001926 Register out = locations->Out().AsRegisterPairLow<Register>();
1927 if (index.IsConstant()) {
1928 size_t offset =
1929 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001930 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001931 } else {
1932 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1933 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001934 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001935 }
1936 break;
1937 }
1938
1939 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001940 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1941 if (index.IsConstant()) {
1942 size_t offset =
1943 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001944 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001945 } else {
1946 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1947 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001948 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001949 }
1950 break;
1951 }
1952
1953 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001954 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1955 if (index.IsConstant()) {
1956 size_t offset =
1957 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001958 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001959 } else {
1960 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1961 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001962 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001963 }
1964 break;
1965 }
1966
1967 case Primitive::kPrimVoid:
1968 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1969 UNREACHABLE();
1970 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001971}
1972
1973void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1974 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1975 locations->SetInAt(0, Location::RequiresRegister());
1976 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1977}
1978
1979void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1980 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001981 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001982 Register obj = locations->InAt(0).AsRegister<Register>();
1983 Register out = locations->Out().AsRegister<Register>();
1984 __ LoadFromOffset(kLoadWord, out, obj, offset);
1985 codegen_->MaybeRecordImplicitNullCheck(instruction);
1986}
1987
Alexey Frunzef58b2482016-09-02 22:14:06 -07001988Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1989 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1990 ? Location::ConstantLocation(instruction->AsConstant())
1991 : Location::RequiresRegister();
1992}
1993
1994Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1995 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1996 // We can store a non-zero float or double constant without first loading it into the FPU,
1997 // but we should only prefer this if the constant has a single use.
1998 if (instruction->IsConstant() &&
1999 (instruction->AsConstant()->IsZeroBitPattern() ||
2000 instruction->GetUses().HasExactlyOneElement())) {
2001 return Location::ConstantLocation(instruction->AsConstant());
2002 // Otherwise fall through and require an FPU register for the constant.
2003 }
2004 return Location::RequiresFpuRegister();
2005}
2006
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002007void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002008 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002009 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2010 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002011 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002012 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002013 InvokeRuntimeCallingConvention calling_convention;
2014 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2015 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2016 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2017 } else {
2018 locations->SetInAt(0, Location::RequiresRegister());
2019 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2020 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002021 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002022 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002023 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002024 }
2025 }
2026}
2027
2028void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2029 LocationSummary* locations = instruction->GetLocations();
2030 Register obj = locations->InAt(0).AsRegister<Register>();
2031 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002032 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002033 Primitive::Type value_type = instruction->GetComponentType();
2034 bool needs_runtime_call = locations->WillCall();
2035 bool needs_write_barrier =
2036 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002037 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002038 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002039
2040 switch (value_type) {
2041 case Primitive::kPrimBoolean:
2042 case Primitive::kPrimByte: {
2043 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002044 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002045 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002046 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002047 __ Addu(base_reg, obj, index.AsRegister<Register>());
2048 }
2049 if (value_location.IsConstant()) {
2050 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2051 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2052 } else {
2053 Register value = value_location.AsRegister<Register>();
2054 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002055 }
2056 break;
2057 }
2058
2059 case Primitive::kPrimShort:
2060 case Primitive::kPrimChar: {
2061 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002062 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002063 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002064 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002065 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2066 __ Addu(base_reg, obj, base_reg);
2067 }
2068 if (value_location.IsConstant()) {
2069 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2070 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2071 } else {
2072 Register value = value_location.AsRegister<Register>();
2073 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002074 }
2075 break;
2076 }
2077
2078 case Primitive::kPrimInt:
2079 case Primitive::kPrimNot: {
2080 if (!needs_runtime_call) {
2081 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002082 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002083 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002084 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002085 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2086 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002087 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002088 if (value_location.IsConstant()) {
2089 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2090 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2091 DCHECK(!needs_write_barrier);
2092 } else {
2093 Register value = value_location.AsRegister<Register>();
2094 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2095 if (needs_write_barrier) {
2096 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevice114da22016-12-26 14:21:43 +01002097 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunzef58b2482016-09-02 22:14:06 -07002098 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002099 }
2100 } else {
2101 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002102 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002103 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2104 }
2105 break;
2106 }
2107
2108 case Primitive::kPrimLong: {
2109 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002110 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002111 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002112 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002113 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2114 __ Addu(base_reg, obj, base_reg);
2115 }
2116 if (value_location.IsConstant()) {
2117 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2118 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2119 } else {
2120 Register value = value_location.AsRegisterPairLow<Register>();
2121 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002122 }
2123 break;
2124 }
2125
2126 case Primitive::kPrimFloat: {
2127 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002128 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002129 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002130 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002131 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2132 __ Addu(base_reg, obj, base_reg);
2133 }
2134 if (value_location.IsConstant()) {
2135 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2136 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2137 } else {
2138 FRegister value = value_location.AsFpuRegister<FRegister>();
2139 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002140 }
2141 break;
2142 }
2143
2144 case Primitive::kPrimDouble: {
2145 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002146 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002147 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002148 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002149 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2150 __ Addu(base_reg, obj, base_reg);
2151 }
2152 if (value_location.IsConstant()) {
2153 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2154 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2155 } else {
2156 FRegister value = value_location.AsFpuRegister<FRegister>();
2157 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002158 }
2159 break;
2160 }
2161
2162 case Primitive::kPrimVoid:
2163 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2164 UNREACHABLE();
2165 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002166}
2167
2168void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002169 RegisterSet caller_saves = RegisterSet::Empty();
2170 InvokeRuntimeCallingConvention calling_convention;
2171 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2172 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2173 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002174 locations->SetInAt(0, Location::RequiresRegister());
2175 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002176}
2177
2178void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2179 LocationSummary* locations = instruction->GetLocations();
2180 BoundsCheckSlowPathMIPS* slow_path =
2181 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2182 codegen_->AddSlowPath(slow_path);
2183
2184 Register index = locations->InAt(0).AsRegister<Register>();
2185 Register length = locations->InAt(1).AsRegister<Register>();
2186
2187 // length is limited by the maximum positive signed 32-bit integer.
2188 // Unsigned comparison of length and index checks for index < 0
2189 // and for length <= index simultaneously.
2190 __ Bgeu(index, length, slow_path->GetEntryLabel());
2191}
2192
2193void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2194 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2195 instruction,
2196 LocationSummary::kCallOnSlowPath);
2197 locations->SetInAt(0, Location::RequiresRegister());
2198 locations->SetInAt(1, Location::RequiresRegister());
2199 // Note that TypeCheckSlowPathMIPS uses this register too.
2200 locations->AddTemp(Location::RequiresRegister());
2201}
2202
2203void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2204 LocationSummary* locations = instruction->GetLocations();
2205 Register obj = locations->InAt(0).AsRegister<Register>();
2206 Register cls = locations->InAt(1).AsRegister<Register>();
2207 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2208
2209 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2210 codegen_->AddSlowPath(slow_path);
2211
2212 // TODO: avoid this check if we know obj is not null.
2213 __ Beqz(obj, slow_path->GetExitLabel());
2214 // Compare the class of `obj` with `cls`.
2215 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2216 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2217 __ Bind(slow_path->GetExitLabel());
2218}
2219
2220void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2221 LocationSummary* locations =
2222 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2223 locations->SetInAt(0, Location::RequiresRegister());
2224 if (check->HasUses()) {
2225 locations->SetOut(Location::SameAsFirstInput());
2226 }
2227}
2228
2229void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2230 // We assume the class is not null.
2231 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2232 check->GetLoadClass(),
2233 check,
2234 check->GetDexPc(),
2235 true);
2236 codegen_->AddSlowPath(slow_path);
2237 GenerateClassInitializationCheck(slow_path,
2238 check->GetLocations()->InAt(0).AsRegister<Register>());
2239}
2240
2241void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2242 Primitive::Type in_type = compare->InputAt(0)->GetType();
2243
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002244 LocationSummary* locations =
2245 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002246
2247 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002248 case Primitive::kPrimBoolean:
2249 case Primitive::kPrimByte:
2250 case Primitive::kPrimShort:
2251 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002252 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002253 locations->SetInAt(0, Location::RequiresRegister());
2254 locations->SetInAt(1, Location::RequiresRegister());
2255 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2256 break;
2257
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002258 case Primitive::kPrimLong:
2259 locations->SetInAt(0, Location::RequiresRegister());
2260 locations->SetInAt(1, Location::RequiresRegister());
2261 // Output overlaps because it is written before doing the low comparison.
2262 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2263 break;
2264
2265 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002266 case Primitive::kPrimDouble:
2267 locations->SetInAt(0, Location::RequiresFpuRegister());
2268 locations->SetInAt(1, Location::RequiresFpuRegister());
2269 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002270 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002271
2272 default:
2273 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2274 }
2275}
2276
2277void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2278 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002279 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002280 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002281 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002282
2283 // 0 if: left == right
2284 // 1 if: left > right
2285 // -1 if: left < right
2286 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002287 case Primitive::kPrimBoolean:
2288 case Primitive::kPrimByte:
2289 case Primitive::kPrimShort:
2290 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002291 case Primitive::kPrimInt: {
2292 Register lhs = locations->InAt(0).AsRegister<Register>();
2293 Register rhs = locations->InAt(1).AsRegister<Register>();
2294 __ Slt(TMP, lhs, rhs);
2295 __ Slt(res, rhs, lhs);
2296 __ Subu(res, res, TMP);
2297 break;
2298 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002299 case Primitive::kPrimLong: {
2300 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2302 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2303 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2304 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2305 // TODO: more efficient (direct) comparison with a constant.
2306 __ Slt(TMP, lhs_high, rhs_high);
2307 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2308 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2309 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2310 __ Sltu(TMP, lhs_low, rhs_low);
2311 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2312 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2313 __ Bind(&done);
2314 break;
2315 }
2316
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002317 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002318 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002319 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2320 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2321 MipsLabel done;
2322 if (isR6) {
2323 __ CmpEqS(FTMP, lhs, rhs);
2324 __ LoadConst32(res, 0);
2325 __ Bc1nez(FTMP, &done);
2326 if (gt_bias) {
2327 __ CmpLtS(FTMP, lhs, rhs);
2328 __ LoadConst32(res, -1);
2329 __ Bc1nez(FTMP, &done);
2330 __ LoadConst32(res, 1);
2331 } else {
2332 __ CmpLtS(FTMP, rhs, lhs);
2333 __ LoadConst32(res, 1);
2334 __ Bc1nez(FTMP, &done);
2335 __ LoadConst32(res, -1);
2336 }
2337 } else {
2338 if (gt_bias) {
2339 __ ColtS(0, lhs, rhs);
2340 __ LoadConst32(res, -1);
2341 __ Bc1t(0, &done);
2342 __ CeqS(0, lhs, rhs);
2343 __ LoadConst32(res, 1);
2344 __ Movt(res, ZERO, 0);
2345 } else {
2346 __ ColtS(0, rhs, lhs);
2347 __ LoadConst32(res, 1);
2348 __ Bc1t(0, &done);
2349 __ CeqS(0, lhs, rhs);
2350 __ LoadConst32(res, -1);
2351 __ Movt(res, ZERO, 0);
2352 }
2353 }
2354 __ Bind(&done);
2355 break;
2356 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002357 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002358 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002359 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2360 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2361 MipsLabel done;
2362 if (isR6) {
2363 __ CmpEqD(FTMP, lhs, rhs);
2364 __ LoadConst32(res, 0);
2365 __ Bc1nez(FTMP, &done);
2366 if (gt_bias) {
2367 __ CmpLtD(FTMP, lhs, rhs);
2368 __ LoadConst32(res, -1);
2369 __ Bc1nez(FTMP, &done);
2370 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002371 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002372 __ CmpLtD(FTMP, rhs, lhs);
2373 __ LoadConst32(res, 1);
2374 __ Bc1nez(FTMP, &done);
2375 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002376 }
2377 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002378 if (gt_bias) {
2379 __ ColtD(0, lhs, rhs);
2380 __ LoadConst32(res, -1);
2381 __ Bc1t(0, &done);
2382 __ CeqD(0, lhs, rhs);
2383 __ LoadConst32(res, 1);
2384 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002385 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002386 __ ColtD(0, rhs, lhs);
2387 __ LoadConst32(res, 1);
2388 __ Bc1t(0, &done);
2389 __ CeqD(0, lhs, rhs);
2390 __ LoadConst32(res, -1);
2391 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002392 }
2393 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002394 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002395 break;
2396 }
2397
2398 default:
2399 LOG(FATAL) << "Unimplemented compare type " << in_type;
2400 }
2401}
2402
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002403void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002404 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002405 switch (instruction->InputAt(0)->GetType()) {
2406 default:
2407 case Primitive::kPrimLong:
2408 locations->SetInAt(0, Location::RequiresRegister());
2409 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2410 break;
2411
2412 case Primitive::kPrimFloat:
2413 case Primitive::kPrimDouble:
2414 locations->SetInAt(0, Location::RequiresFpuRegister());
2415 locations->SetInAt(1, Location::RequiresFpuRegister());
2416 break;
2417 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002418 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002419 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2420 }
2421}
2422
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002423void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002424 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002425 return;
2426 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002427
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002428 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002429 LocationSummary* locations = instruction->GetLocations();
2430 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002431 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002433 switch (type) {
2434 default:
2435 // Integer case.
2436 GenerateIntCompare(instruction->GetCondition(), locations);
2437 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002438
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002439 case Primitive::kPrimLong:
2440 // TODO: don't use branches.
2441 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002442 break;
2443
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002444 case Primitive::kPrimFloat:
2445 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002446 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2447 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002448 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002449
2450 // Convert the branches into the result.
2451 MipsLabel done;
2452
2453 // False case: result = 0.
2454 __ LoadConst32(dst, 0);
2455 __ B(&done);
2456
2457 // True case: result = 1.
2458 __ Bind(&true_label);
2459 __ LoadConst32(dst, 1);
2460 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002461}
2462
Alexey Frunze7e99e052015-11-24 19:28:01 -08002463void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2464 DCHECK(instruction->IsDiv() || instruction->IsRem());
2465 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2466
2467 LocationSummary* locations = instruction->GetLocations();
2468 Location second = locations->InAt(1);
2469 DCHECK(second.IsConstant());
2470
2471 Register out = locations->Out().AsRegister<Register>();
2472 Register dividend = locations->InAt(0).AsRegister<Register>();
2473 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2474 DCHECK(imm == 1 || imm == -1);
2475
2476 if (instruction->IsRem()) {
2477 __ Move(out, ZERO);
2478 } else {
2479 if (imm == -1) {
2480 __ Subu(out, ZERO, dividend);
2481 } else if (out != dividend) {
2482 __ Move(out, dividend);
2483 }
2484 }
2485}
2486
2487void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2488 DCHECK(instruction->IsDiv() || instruction->IsRem());
2489 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2490
2491 LocationSummary* locations = instruction->GetLocations();
2492 Location second = locations->InAt(1);
2493 DCHECK(second.IsConstant());
2494
2495 Register out = locations->Out().AsRegister<Register>();
2496 Register dividend = locations->InAt(0).AsRegister<Register>();
2497 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002498 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002499 int ctz_imm = CTZ(abs_imm);
2500
2501 if (instruction->IsDiv()) {
2502 if (ctz_imm == 1) {
2503 // Fast path for division by +/-2, which is very common.
2504 __ Srl(TMP, dividend, 31);
2505 } else {
2506 __ Sra(TMP, dividend, 31);
2507 __ Srl(TMP, TMP, 32 - ctz_imm);
2508 }
2509 __ Addu(out, dividend, TMP);
2510 __ Sra(out, out, ctz_imm);
2511 if (imm < 0) {
2512 __ Subu(out, ZERO, out);
2513 }
2514 } else {
2515 if (ctz_imm == 1) {
2516 // Fast path for modulo +/-2, which is very common.
2517 __ Sra(TMP, dividend, 31);
2518 __ Subu(out, dividend, TMP);
2519 __ Andi(out, out, 1);
2520 __ Addu(out, out, TMP);
2521 } else {
2522 __ Sra(TMP, dividend, 31);
2523 __ Srl(TMP, TMP, 32 - ctz_imm);
2524 __ Addu(out, dividend, TMP);
2525 if (IsUint<16>(abs_imm - 1)) {
2526 __ Andi(out, out, abs_imm - 1);
2527 } else {
2528 __ Sll(out, out, 32 - ctz_imm);
2529 __ Srl(out, out, 32 - ctz_imm);
2530 }
2531 __ Subu(out, out, TMP);
2532 }
2533 }
2534}
2535
2536void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2537 DCHECK(instruction->IsDiv() || instruction->IsRem());
2538 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2539
2540 LocationSummary* locations = instruction->GetLocations();
2541 Location second = locations->InAt(1);
2542 DCHECK(second.IsConstant());
2543
2544 Register out = locations->Out().AsRegister<Register>();
2545 Register dividend = locations->InAt(0).AsRegister<Register>();
2546 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2547
2548 int64_t magic;
2549 int shift;
2550 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2551
2552 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2553
2554 __ LoadConst32(TMP, magic);
2555 if (isR6) {
2556 __ MuhR6(TMP, dividend, TMP);
2557 } else {
2558 __ MultR2(dividend, TMP);
2559 __ Mfhi(TMP);
2560 }
2561 if (imm > 0 && magic < 0) {
2562 __ Addu(TMP, TMP, dividend);
2563 } else if (imm < 0 && magic > 0) {
2564 __ Subu(TMP, TMP, dividend);
2565 }
2566
2567 if (shift != 0) {
2568 __ Sra(TMP, TMP, shift);
2569 }
2570
2571 if (instruction->IsDiv()) {
2572 __ Sra(out, TMP, 31);
2573 __ Subu(out, TMP, out);
2574 } else {
2575 __ Sra(AT, TMP, 31);
2576 __ Subu(AT, TMP, AT);
2577 __ LoadConst32(TMP, imm);
2578 if (isR6) {
2579 __ MulR6(TMP, AT, TMP);
2580 } else {
2581 __ MulR2(TMP, AT, TMP);
2582 }
2583 __ Subu(out, dividend, TMP);
2584 }
2585}
2586
2587void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2588 DCHECK(instruction->IsDiv() || instruction->IsRem());
2589 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2590
2591 LocationSummary* locations = instruction->GetLocations();
2592 Register out = locations->Out().AsRegister<Register>();
2593 Location second = locations->InAt(1);
2594
2595 if (second.IsConstant()) {
2596 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2597 if (imm == 0) {
2598 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2599 } else if (imm == 1 || imm == -1) {
2600 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002601 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002602 DivRemByPowerOfTwo(instruction);
2603 } else {
2604 DCHECK(imm <= -2 || imm >= 2);
2605 GenerateDivRemWithAnyConstant(instruction);
2606 }
2607 } else {
2608 Register dividend = locations->InAt(0).AsRegister<Register>();
2609 Register divisor = second.AsRegister<Register>();
2610 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2611 if (instruction->IsDiv()) {
2612 if (isR6) {
2613 __ DivR6(out, dividend, divisor);
2614 } else {
2615 __ DivR2(out, dividend, divisor);
2616 }
2617 } else {
2618 if (isR6) {
2619 __ ModR6(out, dividend, divisor);
2620 } else {
2621 __ ModR2(out, dividend, divisor);
2622 }
2623 }
2624 }
2625}
2626
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002627void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2628 Primitive::Type type = div->GetResultType();
2629 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002630 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002631 : LocationSummary::kNoCall;
2632
2633 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2634
2635 switch (type) {
2636 case Primitive::kPrimInt:
2637 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002638 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002639 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2640 break;
2641
2642 case Primitive::kPrimLong: {
2643 InvokeRuntimeCallingConvention calling_convention;
2644 locations->SetInAt(0, Location::RegisterPairLocation(
2645 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2646 locations->SetInAt(1, Location::RegisterPairLocation(
2647 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2648 locations->SetOut(calling_convention.GetReturnLocation(type));
2649 break;
2650 }
2651
2652 case Primitive::kPrimFloat:
2653 case Primitive::kPrimDouble:
2654 locations->SetInAt(0, Location::RequiresFpuRegister());
2655 locations->SetInAt(1, Location::RequiresFpuRegister());
2656 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2657 break;
2658
2659 default:
2660 LOG(FATAL) << "Unexpected div type " << type;
2661 }
2662}
2663
2664void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2665 Primitive::Type type = instruction->GetType();
2666 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002667
2668 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002669 case Primitive::kPrimInt:
2670 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002671 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002672 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002673 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002674 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2675 break;
2676 }
2677 case Primitive::kPrimFloat:
2678 case Primitive::kPrimDouble: {
2679 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2680 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2681 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2682 if (type == Primitive::kPrimFloat) {
2683 __ DivS(dst, lhs, rhs);
2684 } else {
2685 __ DivD(dst, lhs, rhs);
2686 }
2687 break;
2688 }
2689 default:
2690 LOG(FATAL) << "Unexpected div type " << type;
2691 }
2692}
2693
2694void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002695 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002696 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002697}
2698
2699void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2700 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2701 codegen_->AddSlowPath(slow_path);
2702 Location value = instruction->GetLocations()->InAt(0);
2703 Primitive::Type type = instruction->GetType();
2704
2705 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002706 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002707 case Primitive::kPrimByte:
2708 case Primitive::kPrimChar:
2709 case Primitive::kPrimShort:
2710 case Primitive::kPrimInt: {
2711 if (value.IsConstant()) {
2712 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2713 __ B(slow_path->GetEntryLabel());
2714 } else {
2715 // A division by a non-null constant is valid. We don't need to perform
2716 // any check, so simply fall through.
2717 }
2718 } else {
2719 DCHECK(value.IsRegister()) << value;
2720 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2721 }
2722 break;
2723 }
2724 case Primitive::kPrimLong: {
2725 if (value.IsConstant()) {
2726 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2727 __ B(slow_path->GetEntryLabel());
2728 } else {
2729 // A division by a non-null constant is valid. We don't need to perform
2730 // any check, so simply fall through.
2731 }
2732 } else {
2733 DCHECK(value.IsRegisterPair()) << value;
2734 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2735 __ Beqz(TMP, slow_path->GetEntryLabel());
2736 }
2737 break;
2738 }
2739 default:
2740 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2741 }
2742}
2743
2744void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2745 LocationSummary* locations =
2746 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2747 locations->SetOut(Location::ConstantLocation(constant));
2748}
2749
2750void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2751 // Will be generated at use site.
2752}
2753
2754void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2755 exit->SetLocations(nullptr);
2756}
2757
2758void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2759}
2760
2761void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2762 LocationSummary* locations =
2763 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2764 locations->SetOut(Location::ConstantLocation(constant));
2765}
2766
2767void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2768 // Will be generated at use site.
2769}
2770
2771void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2772 got->SetLocations(nullptr);
2773}
2774
2775void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2776 DCHECK(!successor->IsExitBlock());
2777 HBasicBlock* block = got->GetBlock();
2778 HInstruction* previous = got->GetPrevious();
2779 HLoopInformation* info = block->GetLoopInformation();
2780
2781 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2782 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2783 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2784 return;
2785 }
2786 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2787 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2788 }
2789 if (!codegen_->GoesToNextBlock(block, successor)) {
2790 __ B(codegen_->GetLabelOf(successor));
2791 }
2792}
2793
2794void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2795 HandleGoto(got, got->GetSuccessor());
2796}
2797
2798void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2799 try_boundary->SetLocations(nullptr);
2800}
2801
2802void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2803 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2804 if (!successor->IsExitBlock()) {
2805 HandleGoto(try_boundary, successor);
2806 }
2807}
2808
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002809void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2810 LocationSummary* locations) {
2811 Register dst = locations->Out().AsRegister<Register>();
2812 Register lhs = locations->InAt(0).AsRegister<Register>();
2813 Location rhs_location = locations->InAt(1);
2814 Register rhs_reg = ZERO;
2815 int64_t rhs_imm = 0;
2816 bool use_imm = rhs_location.IsConstant();
2817 if (use_imm) {
2818 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2819 } else {
2820 rhs_reg = rhs_location.AsRegister<Register>();
2821 }
2822
2823 switch (cond) {
2824 case kCondEQ:
2825 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002826 if (use_imm && IsInt<16>(-rhs_imm)) {
2827 if (rhs_imm == 0) {
2828 if (cond == kCondEQ) {
2829 __ Sltiu(dst, lhs, 1);
2830 } else {
2831 __ Sltu(dst, ZERO, lhs);
2832 }
2833 } else {
2834 __ Addiu(dst, lhs, -rhs_imm);
2835 if (cond == kCondEQ) {
2836 __ Sltiu(dst, dst, 1);
2837 } else {
2838 __ Sltu(dst, ZERO, dst);
2839 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002840 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002841 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002842 if (use_imm && IsUint<16>(rhs_imm)) {
2843 __ Xori(dst, lhs, rhs_imm);
2844 } else {
2845 if (use_imm) {
2846 rhs_reg = TMP;
2847 __ LoadConst32(rhs_reg, rhs_imm);
2848 }
2849 __ Xor(dst, lhs, rhs_reg);
2850 }
2851 if (cond == kCondEQ) {
2852 __ Sltiu(dst, dst, 1);
2853 } else {
2854 __ Sltu(dst, ZERO, dst);
2855 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002856 }
2857 break;
2858
2859 case kCondLT:
2860 case kCondGE:
2861 if (use_imm && IsInt<16>(rhs_imm)) {
2862 __ Slti(dst, lhs, rhs_imm);
2863 } else {
2864 if (use_imm) {
2865 rhs_reg = TMP;
2866 __ LoadConst32(rhs_reg, rhs_imm);
2867 }
2868 __ Slt(dst, lhs, rhs_reg);
2869 }
2870 if (cond == kCondGE) {
2871 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2872 // only the slt instruction but no sge.
2873 __ Xori(dst, dst, 1);
2874 }
2875 break;
2876
2877 case kCondLE:
2878 case kCondGT:
2879 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2880 // Simulate lhs <= rhs via lhs < rhs + 1.
2881 __ Slti(dst, lhs, rhs_imm + 1);
2882 if (cond == kCondGT) {
2883 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2884 // only the slti instruction but no sgti.
2885 __ Xori(dst, dst, 1);
2886 }
2887 } else {
2888 if (use_imm) {
2889 rhs_reg = TMP;
2890 __ LoadConst32(rhs_reg, rhs_imm);
2891 }
2892 __ Slt(dst, rhs_reg, lhs);
2893 if (cond == kCondLE) {
2894 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2895 // only the slt instruction but no sle.
2896 __ Xori(dst, dst, 1);
2897 }
2898 }
2899 break;
2900
2901 case kCondB:
2902 case kCondAE:
2903 if (use_imm && IsInt<16>(rhs_imm)) {
2904 // Sltiu sign-extends its 16-bit immediate operand before
2905 // the comparison and thus lets us compare directly with
2906 // unsigned values in the ranges [0, 0x7fff] and
2907 // [0xffff8000, 0xffffffff].
2908 __ Sltiu(dst, lhs, rhs_imm);
2909 } else {
2910 if (use_imm) {
2911 rhs_reg = TMP;
2912 __ LoadConst32(rhs_reg, rhs_imm);
2913 }
2914 __ Sltu(dst, lhs, rhs_reg);
2915 }
2916 if (cond == kCondAE) {
2917 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2918 // only the sltu instruction but no sgeu.
2919 __ Xori(dst, dst, 1);
2920 }
2921 break;
2922
2923 case kCondBE:
2924 case kCondA:
2925 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2926 // Simulate lhs <= rhs via lhs < rhs + 1.
2927 // Note that this only works if rhs + 1 does not overflow
2928 // to 0, hence the check above.
2929 // Sltiu sign-extends its 16-bit immediate operand before
2930 // the comparison and thus lets us compare directly with
2931 // unsigned values in the ranges [0, 0x7fff] and
2932 // [0xffff8000, 0xffffffff].
2933 __ Sltiu(dst, lhs, rhs_imm + 1);
2934 if (cond == kCondA) {
2935 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2936 // only the sltiu instruction but no sgtiu.
2937 __ Xori(dst, dst, 1);
2938 }
2939 } else {
2940 if (use_imm) {
2941 rhs_reg = TMP;
2942 __ LoadConst32(rhs_reg, rhs_imm);
2943 }
2944 __ Sltu(dst, rhs_reg, lhs);
2945 if (cond == kCondBE) {
2946 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2947 // only the sltu instruction but no sleu.
2948 __ Xori(dst, dst, 1);
2949 }
2950 }
2951 break;
2952 }
2953}
2954
Alexey Frunze674b9ee2016-09-20 14:54:15 -07002955bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
2956 LocationSummary* input_locations,
2957 Register dst) {
2958 Register lhs = input_locations->InAt(0).AsRegister<Register>();
2959 Location rhs_location = input_locations->InAt(1);
2960 Register rhs_reg = ZERO;
2961 int64_t rhs_imm = 0;
2962 bool use_imm = rhs_location.IsConstant();
2963 if (use_imm) {
2964 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2965 } else {
2966 rhs_reg = rhs_location.AsRegister<Register>();
2967 }
2968
2969 switch (cond) {
2970 case kCondEQ:
2971 case kCondNE:
2972 if (use_imm && IsInt<16>(-rhs_imm)) {
2973 __ Addiu(dst, lhs, -rhs_imm);
2974 } else if (use_imm && IsUint<16>(rhs_imm)) {
2975 __ Xori(dst, lhs, rhs_imm);
2976 } else {
2977 if (use_imm) {
2978 rhs_reg = TMP;
2979 __ LoadConst32(rhs_reg, rhs_imm);
2980 }
2981 __ Xor(dst, lhs, rhs_reg);
2982 }
2983 return (cond == kCondEQ);
2984
2985 case kCondLT:
2986 case kCondGE:
2987 if (use_imm && IsInt<16>(rhs_imm)) {
2988 __ Slti(dst, lhs, rhs_imm);
2989 } else {
2990 if (use_imm) {
2991 rhs_reg = TMP;
2992 __ LoadConst32(rhs_reg, rhs_imm);
2993 }
2994 __ Slt(dst, lhs, rhs_reg);
2995 }
2996 return (cond == kCondGE);
2997
2998 case kCondLE:
2999 case kCondGT:
3000 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3001 // Simulate lhs <= rhs via lhs < rhs + 1.
3002 __ Slti(dst, lhs, rhs_imm + 1);
3003 return (cond == kCondGT);
3004 } else {
3005 if (use_imm) {
3006 rhs_reg = TMP;
3007 __ LoadConst32(rhs_reg, rhs_imm);
3008 }
3009 __ Slt(dst, rhs_reg, lhs);
3010 return (cond == kCondLE);
3011 }
3012
3013 case kCondB:
3014 case kCondAE:
3015 if (use_imm && IsInt<16>(rhs_imm)) {
3016 // Sltiu sign-extends its 16-bit immediate operand before
3017 // the comparison and thus lets us compare directly with
3018 // unsigned values in the ranges [0, 0x7fff] and
3019 // [0xffff8000, 0xffffffff].
3020 __ Sltiu(dst, lhs, rhs_imm);
3021 } else {
3022 if (use_imm) {
3023 rhs_reg = TMP;
3024 __ LoadConst32(rhs_reg, rhs_imm);
3025 }
3026 __ Sltu(dst, lhs, rhs_reg);
3027 }
3028 return (cond == kCondAE);
3029
3030 case kCondBE:
3031 case kCondA:
3032 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3033 // Simulate lhs <= rhs via lhs < rhs + 1.
3034 // Note that this only works if rhs + 1 does not overflow
3035 // to 0, hence the check above.
3036 // Sltiu sign-extends its 16-bit immediate operand before
3037 // the comparison and thus lets us compare directly with
3038 // unsigned values in the ranges [0, 0x7fff] and
3039 // [0xffff8000, 0xffffffff].
3040 __ Sltiu(dst, lhs, rhs_imm + 1);
3041 return (cond == kCondA);
3042 } else {
3043 if (use_imm) {
3044 rhs_reg = TMP;
3045 __ LoadConst32(rhs_reg, rhs_imm);
3046 }
3047 __ Sltu(dst, rhs_reg, lhs);
3048 return (cond == kCondBE);
3049 }
3050 }
3051}
3052
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003053void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3054 LocationSummary* locations,
3055 MipsLabel* label) {
3056 Register lhs = locations->InAt(0).AsRegister<Register>();
3057 Location rhs_location = locations->InAt(1);
3058 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003059 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003060 bool use_imm = rhs_location.IsConstant();
3061 if (use_imm) {
3062 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3063 } else {
3064 rhs_reg = rhs_location.AsRegister<Register>();
3065 }
3066
3067 if (use_imm && rhs_imm == 0) {
3068 switch (cond) {
3069 case kCondEQ:
3070 case kCondBE: // <= 0 if zero
3071 __ Beqz(lhs, label);
3072 break;
3073 case kCondNE:
3074 case kCondA: // > 0 if non-zero
3075 __ Bnez(lhs, label);
3076 break;
3077 case kCondLT:
3078 __ Bltz(lhs, label);
3079 break;
3080 case kCondGE:
3081 __ Bgez(lhs, label);
3082 break;
3083 case kCondLE:
3084 __ Blez(lhs, label);
3085 break;
3086 case kCondGT:
3087 __ Bgtz(lhs, label);
3088 break;
3089 case kCondB: // always false
3090 break;
3091 case kCondAE: // always true
3092 __ B(label);
3093 break;
3094 }
3095 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003096 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3097 if (isR6 || !use_imm) {
3098 if (use_imm) {
3099 rhs_reg = TMP;
3100 __ LoadConst32(rhs_reg, rhs_imm);
3101 }
3102 switch (cond) {
3103 case kCondEQ:
3104 __ Beq(lhs, rhs_reg, label);
3105 break;
3106 case kCondNE:
3107 __ Bne(lhs, rhs_reg, label);
3108 break;
3109 case kCondLT:
3110 __ Blt(lhs, rhs_reg, label);
3111 break;
3112 case kCondGE:
3113 __ Bge(lhs, rhs_reg, label);
3114 break;
3115 case kCondLE:
3116 __ Bge(rhs_reg, lhs, label);
3117 break;
3118 case kCondGT:
3119 __ Blt(rhs_reg, lhs, label);
3120 break;
3121 case kCondB:
3122 __ Bltu(lhs, rhs_reg, label);
3123 break;
3124 case kCondAE:
3125 __ Bgeu(lhs, rhs_reg, label);
3126 break;
3127 case kCondBE:
3128 __ Bgeu(rhs_reg, lhs, label);
3129 break;
3130 case kCondA:
3131 __ Bltu(rhs_reg, lhs, label);
3132 break;
3133 }
3134 } else {
3135 // Special cases for more efficient comparison with constants on R2.
3136 switch (cond) {
3137 case kCondEQ:
3138 __ LoadConst32(TMP, rhs_imm);
3139 __ Beq(lhs, TMP, label);
3140 break;
3141 case kCondNE:
3142 __ LoadConst32(TMP, rhs_imm);
3143 __ Bne(lhs, TMP, label);
3144 break;
3145 case kCondLT:
3146 if (IsInt<16>(rhs_imm)) {
3147 __ Slti(TMP, lhs, rhs_imm);
3148 __ Bnez(TMP, label);
3149 } else {
3150 __ LoadConst32(TMP, rhs_imm);
3151 __ Blt(lhs, TMP, label);
3152 }
3153 break;
3154 case kCondGE:
3155 if (IsInt<16>(rhs_imm)) {
3156 __ Slti(TMP, lhs, rhs_imm);
3157 __ Beqz(TMP, label);
3158 } else {
3159 __ LoadConst32(TMP, rhs_imm);
3160 __ Bge(lhs, TMP, label);
3161 }
3162 break;
3163 case kCondLE:
3164 if (IsInt<16>(rhs_imm + 1)) {
3165 // Simulate lhs <= rhs via lhs < rhs + 1.
3166 __ Slti(TMP, lhs, rhs_imm + 1);
3167 __ Bnez(TMP, label);
3168 } else {
3169 __ LoadConst32(TMP, rhs_imm);
3170 __ Bge(TMP, lhs, label);
3171 }
3172 break;
3173 case kCondGT:
3174 if (IsInt<16>(rhs_imm + 1)) {
3175 // Simulate lhs > rhs via !(lhs < rhs + 1).
3176 __ Slti(TMP, lhs, rhs_imm + 1);
3177 __ Beqz(TMP, label);
3178 } else {
3179 __ LoadConst32(TMP, rhs_imm);
3180 __ Blt(TMP, lhs, label);
3181 }
3182 break;
3183 case kCondB:
3184 if (IsInt<16>(rhs_imm)) {
3185 __ Sltiu(TMP, lhs, rhs_imm);
3186 __ Bnez(TMP, label);
3187 } else {
3188 __ LoadConst32(TMP, rhs_imm);
3189 __ Bltu(lhs, TMP, label);
3190 }
3191 break;
3192 case kCondAE:
3193 if (IsInt<16>(rhs_imm)) {
3194 __ Sltiu(TMP, lhs, rhs_imm);
3195 __ Beqz(TMP, label);
3196 } else {
3197 __ LoadConst32(TMP, rhs_imm);
3198 __ Bgeu(lhs, TMP, label);
3199 }
3200 break;
3201 case kCondBE:
3202 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3203 // Simulate lhs <= rhs via lhs < rhs + 1.
3204 // Note that this only works if rhs + 1 does not overflow
3205 // to 0, hence the check above.
3206 __ Sltiu(TMP, lhs, rhs_imm + 1);
3207 __ Bnez(TMP, label);
3208 } else {
3209 __ LoadConst32(TMP, rhs_imm);
3210 __ Bgeu(TMP, lhs, label);
3211 }
3212 break;
3213 case kCondA:
3214 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3215 // Simulate lhs > rhs via !(lhs < rhs + 1).
3216 // Note that this only works if rhs + 1 does not overflow
3217 // to 0, hence the check above.
3218 __ Sltiu(TMP, lhs, rhs_imm + 1);
3219 __ Beqz(TMP, label);
3220 } else {
3221 __ LoadConst32(TMP, rhs_imm);
3222 __ Bltu(TMP, lhs, label);
3223 }
3224 break;
3225 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003226 }
3227 }
3228}
3229
3230void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3231 LocationSummary* locations,
3232 MipsLabel* label) {
3233 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3234 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3235 Location rhs_location = locations->InAt(1);
3236 Register rhs_high = ZERO;
3237 Register rhs_low = ZERO;
3238 int64_t imm = 0;
3239 uint32_t imm_high = 0;
3240 uint32_t imm_low = 0;
3241 bool use_imm = rhs_location.IsConstant();
3242 if (use_imm) {
3243 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3244 imm_high = High32Bits(imm);
3245 imm_low = Low32Bits(imm);
3246 } else {
3247 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3248 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3249 }
3250
3251 if (use_imm && imm == 0) {
3252 switch (cond) {
3253 case kCondEQ:
3254 case kCondBE: // <= 0 if zero
3255 __ Or(TMP, lhs_high, lhs_low);
3256 __ Beqz(TMP, label);
3257 break;
3258 case kCondNE:
3259 case kCondA: // > 0 if non-zero
3260 __ Or(TMP, lhs_high, lhs_low);
3261 __ Bnez(TMP, label);
3262 break;
3263 case kCondLT:
3264 __ Bltz(lhs_high, label);
3265 break;
3266 case kCondGE:
3267 __ Bgez(lhs_high, label);
3268 break;
3269 case kCondLE:
3270 __ Or(TMP, lhs_high, lhs_low);
3271 __ Sra(AT, lhs_high, 31);
3272 __ Bgeu(AT, TMP, label);
3273 break;
3274 case kCondGT:
3275 __ Or(TMP, lhs_high, lhs_low);
3276 __ Sra(AT, lhs_high, 31);
3277 __ Bltu(AT, TMP, label);
3278 break;
3279 case kCondB: // always false
3280 break;
3281 case kCondAE: // always true
3282 __ B(label);
3283 break;
3284 }
3285 } else if (use_imm) {
3286 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3287 switch (cond) {
3288 case kCondEQ:
3289 __ LoadConst32(TMP, imm_high);
3290 __ Xor(TMP, TMP, lhs_high);
3291 __ LoadConst32(AT, imm_low);
3292 __ Xor(AT, AT, lhs_low);
3293 __ Or(TMP, TMP, AT);
3294 __ Beqz(TMP, label);
3295 break;
3296 case kCondNE:
3297 __ LoadConst32(TMP, imm_high);
3298 __ Xor(TMP, TMP, lhs_high);
3299 __ LoadConst32(AT, imm_low);
3300 __ Xor(AT, AT, lhs_low);
3301 __ Or(TMP, TMP, AT);
3302 __ Bnez(TMP, label);
3303 break;
3304 case kCondLT:
3305 __ LoadConst32(TMP, imm_high);
3306 __ Blt(lhs_high, TMP, label);
3307 __ Slt(TMP, TMP, lhs_high);
3308 __ LoadConst32(AT, imm_low);
3309 __ Sltu(AT, lhs_low, AT);
3310 __ Blt(TMP, AT, label);
3311 break;
3312 case kCondGE:
3313 __ LoadConst32(TMP, imm_high);
3314 __ Blt(TMP, lhs_high, label);
3315 __ Slt(TMP, lhs_high, TMP);
3316 __ LoadConst32(AT, imm_low);
3317 __ Sltu(AT, lhs_low, AT);
3318 __ Or(TMP, TMP, AT);
3319 __ Beqz(TMP, label);
3320 break;
3321 case kCondLE:
3322 __ LoadConst32(TMP, imm_high);
3323 __ Blt(lhs_high, TMP, label);
3324 __ Slt(TMP, TMP, lhs_high);
3325 __ LoadConst32(AT, imm_low);
3326 __ Sltu(AT, AT, lhs_low);
3327 __ Or(TMP, TMP, AT);
3328 __ Beqz(TMP, label);
3329 break;
3330 case kCondGT:
3331 __ LoadConst32(TMP, imm_high);
3332 __ Blt(TMP, lhs_high, label);
3333 __ Slt(TMP, lhs_high, TMP);
3334 __ LoadConst32(AT, imm_low);
3335 __ Sltu(AT, AT, lhs_low);
3336 __ Blt(TMP, AT, label);
3337 break;
3338 case kCondB:
3339 __ LoadConst32(TMP, imm_high);
3340 __ Bltu(lhs_high, TMP, label);
3341 __ Sltu(TMP, TMP, lhs_high);
3342 __ LoadConst32(AT, imm_low);
3343 __ Sltu(AT, lhs_low, AT);
3344 __ Blt(TMP, AT, label);
3345 break;
3346 case kCondAE:
3347 __ LoadConst32(TMP, imm_high);
3348 __ Bltu(TMP, lhs_high, label);
3349 __ Sltu(TMP, lhs_high, TMP);
3350 __ LoadConst32(AT, imm_low);
3351 __ Sltu(AT, lhs_low, AT);
3352 __ Or(TMP, TMP, AT);
3353 __ Beqz(TMP, label);
3354 break;
3355 case kCondBE:
3356 __ LoadConst32(TMP, imm_high);
3357 __ Bltu(lhs_high, TMP, label);
3358 __ Sltu(TMP, TMP, lhs_high);
3359 __ LoadConst32(AT, imm_low);
3360 __ Sltu(AT, AT, lhs_low);
3361 __ Or(TMP, TMP, AT);
3362 __ Beqz(TMP, label);
3363 break;
3364 case kCondA:
3365 __ LoadConst32(TMP, imm_high);
3366 __ Bltu(TMP, lhs_high, label);
3367 __ Sltu(TMP, lhs_high, TMP);
3368 __ LoadConst32(AT, imm_low);
3369 __ Sltu(AT, AT, lhs_low);
3370 __ Blt(TMP, AT, label);
3371 break;
3372 }
3373 } else {
3374 switch (cond) {
3375 case kCondEQ:
3376 __ Xor(TMP, lhs_high, rhs_high);
3377 __ Xor(AT, lhs_low, rhs_low);
3378 __ Or(TMP, TMP, AT);
3379 __ Beqz(TMP, label);
3380 break;
3381 case kCondNE:
3382 __ Xor(TMP, lhs_high, rhs_high);
3383 __ Xor(AT, lhs_low, rhs_low);
3384 __ Or(TMP, TMP, AT);
3385 __ Bnez(TMP, label);
3386 break;
3387 case kCondLT:
3388 __ Blt(lhs_high, rhs_high, label);
3389 __ Slt(TMP, rhs_high, lhs_high);
3390 __ Sltu(AT, lhs_low, rhs_low);
3391 __ Blt(TMP, AT, label);
3392 break;
3393 case kCondGE:
3394 __ Blt(rhs_high, lhs_high, label);
3395 __ Slt(TMP, lhs_high, rhs_high);
3396 __ Sltu(AT, lhs_low, rhs_low);
3397 __ Or(TMP, TMP, AT);
3398 __ Beqz(TMP, label);
3399 break;
3400 case kCondLE:
3401 __ Blt(lhs_high, rhs_high, label);
3402 __ Slt(TMP, rhs_high, lhs_high);
3403 __ Sltu(AT, rhs_low, lhs_low);
3404 __ Or(TMP, TMP, AT);
3405 __ Beqz(TMP, label);
3406 break;
3407 case kCondGT:
3408 __ Blt(rhs_high, lhs_high, label);
3409 __ Slt(TMP, lhs_high, rhs_high);
3410 __ Sltu(AT, rhs_low, lhs_low);
3411 __ Blt(TMP, AT, label);
3412 break;
3413 case kCondB:
3414 __ Bltu(lhs_high, rhs_high, label);
3415 __ Sltu(TMP, rhs_high, lhs_high);
3416 __ Sltu(AT, lhs_low, rhs_low);
3417 __ Blt(TMP, AT, label);
3418 break;
3419 case kCondAE:
3420 __ Bltu(rhs_high, lhs_high, label);
3421 __ Sltu(TMP, lhs_high, rhs_high);
3422 __ Sltu(AT, lhs_low, rhs_low);
3423 __ Or(TMP, TMP, AT);
3424 __ Beqz(TMP, label);
3425 break;
3426 case kCondBE:
3427 __ Bltu(lhs_high, rhs_high, label);
3428 __ Sltu(TMP, rhs_high, lhs_high);
3429 __ Sltu(AT, rhs_low, lhs_low);
3430 __ Or(TMP, TMP, AT);
3431 __ Beqz(TMP, label);
3432 break;
3433 case kCondA:
3434 __ Bltu(rhs_high, lhs_high, label);
3435 __ Sltu(TMP, lhs_high, rhs_high);
3436 __ Sltu(AT, rhs_low, lhs_low);
3437 __ Blt(TMP, AT, label);
3438 break;
3439 }
3440 }
3441}
3442
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003443void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3444 bool gt_bias,
3445 Primitive::Type type,
3446 LocationSummary* locations) {
3447 Register dst = locations->Out().AsRegister<Register>();
3448 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3449 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3450 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3451 if (type == Primitive::kPrimFloat) {
3452 if (isR6) {
3453 switch (cond) {
3454 case kCondEQ:
3455 __ CmpEqS(FTMP, lhs, rhs);
3456 __ Mfc1(dst, FTMP);
3457 __ Andi(dst, dst, 1);
3458 break;
3459 case kCondNE:
3460 __ CmpEqS(FTMP, lhs, rhs);
3461 __ Mfc1(dst, FTMP);
3462 __ Addiu(dst, dst, 1);
3463 break;
3464 case kCondLT:
3465 if (gt_bias) {
3466 __ CmpLtS(FTMP, lhs, rhs);
3467 } else {
3468 __ CmpUltS(FTMP, lhs, rhs);
3469 }
3470 __ Mfc1(dst, FTMP);
3471 __ Andi(dst, dst, 1);
3472 break;
3473 case kCondLE:
3474 if (gt_bias) {
3475 __ CmpLeS(FTMP, lhs, rhs);
3476 } else {
3477 __ CmpUleS(FTMP, lhs, rhs);
3478 }
3479 __ Mfc1(dst, FTMP);
3480 __ Andi(dst, dst, 1);
3481 break;
3482 case kCondGT:
3483 if (gt_bias) {
3484 __ CmpUltS(FTMP, rhs, lhs);
3485 } else {
3486 __ CmpLtS(FTMP, rhs, lhs);
3487 }
3488 __ Mfc1(dst, FTMP);
3489 __ Andi(dst, dst, 1);
3490 break;
3491 case kCondGE:
3492 if (gt_bias) {
3493 __ CmpUleS(FTMP, rhs, lhs);
3494 } else {
3495 __ CmpLeS(FTMP, rhs, lhs);
3496 }
3497 __ Mfc1(dst, FTMP);
3498 __ Andi(dst, dst, 1);
3499 break;
3500 default:
3501 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3502 UNREACHABLE();
3503 }
3504 } else {
3505 switch (cond) {
3506 case kCondEQ:
3507 __ CeqS(0, lhs, rhs);
3508 __ LoadConst32(dst, 1);
3509 __ Movf(dst, ZERO, 0);
3510 break;
3511 case kCondNE:
3512 __ CeqS(0, lhs, rhs);
3513 __ LoadConst32(dst, 1);
3514 __ Movt(dst, ZERO, 0);
3515 break;
3516 case kCondLT:
3517 if (gt_bias) {
3518 __ ColtS(0, lhs, rhs);
3519 } else {
3520 __ CultS(0, lhs, rhs);
3521 }
3522 __ LoadConst32(dst, 1);
3523 __ Movf(dst, ZERO, 0);
3524 break;
3525 case kCondLE:
3526 if (gt_bias) {
3527 __ ColeS(0, lhs, rhs);
3528 } else {
3529 __ CuleS(0, lhs, rhs);
3530 }
3531 __ LoadConst32(dst, 1);
3532 __ Movf(dst, ZERO, 0);
3533 break;
3534 case kCondGT:
3535 if (gt_bias) {
3536 __ CultS(0, rhs, lhs);
3537 } else {
3538 __ ColtS(0, rhs, lhs);
3539 }
3540 __ LoadConst32(dst, 1);
3541 __ Movf(dst, ZERO, 0);
3542 break;
3543 case kCondGE:
3544 if (gt_bias) {
3545 __ CuleS(0, rhs, lhs);
3546 } else {
3547 __ ColeS(0, rhs, lhs);
3548 }
3549 __ LoadConst32(dst, 1);
3550 __ Movf(dst, ZERO, 0);
3551 break;
3552 default:
3553 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3554 UNREACHABLE();
3555 }
3556 }
3557 } else {
3558 DCHECK_EQ(type, Primitive::kPrimDouble);
3559 if (isR6) {
3560 switch (cond) {
3561 case kCondEQ:
3562 __ CmpEqD(FTMP, lhs, rhs);
3563 __ Mfc1(dst, FTMP);
3564 __ Andi(dst, dst, 1);
3565 break;
3566 case kCondNE:
3567 __ CmpEqD(FTMP, lhs, rhs);
3568 __ Mfc1(dst, FTMP);
3569 __ Addiu(dst, dst, 1);
3570 break;
3571 case kCondLT:
3572 if (gt_bias) {
3573 __ CmpLtD(FTMP, lhs, rhs);
3574 } else {
3575 __ CmpUltD(FTMP, lhs, rhs);
3576 }
3577 __ Mfc1(dst, FTMP);
3578 __ Andi(dst, dst, 1);
3579 break;
3580 case kCondLE:
3581 if (gt_bias) {
3582 __ CmpLeD(FTMP, lhs, rhs);
3583 } else {
3584 __ CmpUleD(FTMP, lhs, rhs);
3585 }
3586 __ Mfc1(dst, FTMP);
3587 __ Andi(dst, dst, 1);
3588 break;
3589 case kCondGT:
3590 if (gt_bias) {
3591 __ CmpUltD(FTMP, rhs, lhs);
3592 } else {
3593 __ CmpLtD(FTMP, rhs, lhs);
3594 }
3595 __ Mfc1(dst, FTMP);
3596 __ Andi(dst, dst, 1);
3597 break;
3598 case kCondGE:
3599 if (gt_bias) {
3600 __ CmpUleD(FTMP, rhs, lhs);
3601 } else {
3602 __ CmpLeD(FTMP, rhs, lhs);
3603 }
3604 __ Mfc1(dst, FTMP);
3605 __ Andi(dst, dst, 1);
3606 break;
3607 default:
3608 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3609 UNREACHABLE();
3610 }
3611 } else {
3612 switch (cond) {
3613 case kCondEQ:
3614 __ CeqD(0, lhs, rhs);
3615 __ LoadConst32(dst, 1);
3616 __ Movf(dst, ZERO, 0);
3617 break;
3618 case kCondNE:
3619 __ CeqD(0, lhs, rhs);
3620 __ LoadConst32(dst, 1);
3621 __ Movt(dst, ZERO, 0);
3622 break;
3623 case kCondLT:
3624 if (gt_bias) {
3625 __ ColtD(0, lhs, rhs);
3626 } else {
3627 __ CultD(0, lhs, rhs);
3628 }
3629 __ LoadConst32(dst, 1);
3630 __ Movf(dst, ZERO, 0);
3631 break;
3632 case kCondLE:
3633 if (gt_bias) {
3634 __ ColeD(0, lhs, rhs);
3635 } else {
3636 __ CuleD(0, lhs, rhs);
3637 }
3638 __ LoadConst32(dst, 1);
3639 __ Movf(dst, ZERO, 0);
3640 break;
3641 case kCondGT:
3642 if (gt_bias) {
3643 __ CultD(0, rhs, lhs);
3644 } else {
3645 __ ColtD(0, rhs, lhs);
3646 }
3647 __ LoadConst32(dst, 1);
3648 __ Movf(dst, ZERO, 0);
3649 break;
3650 case kCondGE:
3651 if (gt_bias) {
3652 __ CuleD(0, rhs, lhs);
3653 } else {
3654 __ ColeD(0, rhs, lhs);
3655 }
3656 __ LoadConst32(dst, 1);
3657 __ Movf(dst, ZERO, 0);
3658 break;
3659 default:
3660 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3661 UNREACHABLE();
3662 }
3663 }
3664 }
3665}
3666
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003667bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3668 bool gt_bias,
3669 Primitive::Type type,
3670 LocationSummary* input_locations,
3671 int cc) {
3672 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3673 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3674 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3675 if (type == Primitive::kPrimFloat) {
3676 switch (cond) {
3677 case kCondEQ:
3678 __ CeqS(cc, lhs, rhs);
3679 return false;
3680 case kCondNE:
3681 __ CeqS(cc, lhs, rhs);
3682 return true;
3683 case kCondLT:
3684 if (gt_bias) {
3685 __ ColtS(cc, lhs, rhs);
3686 } else {
3687 __ CultS(cc, lhs, rhs);
3688 }
3689 return false;
3690 case kCondLE:
3691 if (gt_bias) {
3692 __ ColeS(cc, lhs, rhs);
3693 } else {
3694 __ CuleS(cc, lhs, rhs);
3695 }
3696 return false;
3697 case kCondGT:
3698 if (gt_bias) {
3699 __ CultS(cc, rhs, lhs);
3700 } else {
3701 __ ColtS(cc, rhs, lhs);
3702 }
3703 return false;
3704 case kCondGE:
3705 if (gt_bias) {
3706 __ CuleS(cc, rhs, lhs);
3707 } else {
3708 __ ColeS(cc, rhs, lhs);
3709 }
3710 return false;
3711 default:
3712 LOG(FATAL) << "Unexpected non-floating-point condition";
3713 UNREACHABLE();
3714 }
3715 } else {
3716 DCHECK_EQ(type, Primitive::kPrimDouble);
3717 switch (cond) {
3718 case kCondEQ:
3719 __ CeqD(cc, lhs, rhs);
3720 return false;
3721 case kCondNE:
3722 __ CeqD(cc, lhs, rhs);
3723 return true;
3724 case kCondLT:
3725 if (gt_bias) {
3726 __ ColtD(cc, lhs, rhs);
3727 } else {
3728 __ CultD(cc, lhs, rhs);
3729 }
3730 return false;
3731 case kCondLE:
3732 if (gt_bias) {
3733 __ ColeD(cc, lhs, rhs);
3734 } else {
3735 __ CuleD(cc, lhs, rhs);
3736 }
3737 return false;
3738 case kCondGT:
3739 if (gt_bias) {
3740 __ CultD(cc, rhs, lhs);
3741 } else {
3742 __ ColtD(cc, rhs, lhs);
3743 }
3744 return false;
3745 case kCondGE:
3746 if (gt_bias) {
3747 __ CuleD(cc, rhs, lhs);
3748 } else {
3749 __ ColeD(cc, rhs, lhs);
3750 }
3751 return false;
3752 default:
3753 LOG(FATAL) << "Unexpected non-floating-point condition";
3754 UNREACHABLE();
3755 }
3756 }
3757}
3758
3759bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
3760 bool gt_bias,
3761 Primitive::Type type,
3762 LocationSummary* input_locations,
3763 FRegister dst) {
3764 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3765 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3766 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
3767 if (type == Primitive::kPrimFloat) {
3768 switch (cond) {
3769 case kCondEQ:
3770 __ CmpEqS(dst, lhs, rhs);
3771 return false;
3772 case kCondNE:
3773 __ CmpEqS(dst, lhs, rhs);
3774 return true;
3775 case kCondLT:
3776 if (gt_bias) {
3777 __ CmpLtS(dst, lhs, rhs);
3778 } else {
3779 __ CmpUltS(dst, lhs, rhs);
3780 }
3781 return false;
3782 case kCondLE:
3783 if (gt_bias) {
3784 __ CmpLeS(dst, lhs, rhs);
3785 } else {
3786 __ CmpUleS(dst, lhs, rhs);
3787 }
3788 return false;
3789 case kCondGT:
3790 if (gt_bias) {
3791 __ CmpUltS(dst, rhs, lhs);
3792 } else {
3793 __ CmpLtS(dst, rhs, lhs);
3794 }
3795 return false;
3796 case kCondGE:
3797 if (gt_bias) {
3798 __ CmpUleS(dst, rhs, lhs);
3799 } else {
3800 __ CmpLeS(dst, rhs, lhs);
3801 }
3802 return false;
3803 default:
3804 LOG(FATAL) << "Unexpected non-floating-point condition";
3805 UNREACHABLE();
3806 }
3807 } else {
3808 DCHECK_EQ(type, Primitive::kPrimDouble);
3809 switch (cond) {
3810 case kCondEQ:
3811 __ CmpEqD(dst, lhs, rhs);
3812 return false;
3813 case kCondNE:
3814 __ CmpEqD(dst, lhs, rhs);
3815 return true;
3816 case kCondLT:
3817 if (gt_bias) {
3818 __ CmpLtD(dst, lhs, rhs);
3819 } else {
3820 __ CmpUltD(dst, lhs, rhs);
3821 }
3822 return false;
3823 case kCondLE:
3824 if (gt_bias) {
3825 __ CmpLeD(dst, lhs, rhs);
3826 } else {
3827 __ CmpUleD(dst, lhs, rhs);
3828 }
3829 return false;
3830 case kCondGT:
3831 if (gt_bias) {
3832 __ CmpUltD(dst, rhs, lhs);
3833 } else {
3834 __ CmpLtD(dst, rhs, lhs);
3835 }
3836 return false;
3837 case kCondGE:
3838 if (gt_bias) {
3839 __ CmpUleD(dst, rhs, lhs);
3840 } else {
3841 __ CmpLeD(dst, rhs, lhs);
3842 }
3843 return false;
3844 default:
3845 LOG(FATAL) << "Unexpected non-floating-point condition";
3846 UNREACHABLE();
3847 }
3848 }
3849}
3850
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003851void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3852 bool gt_bias,
3853 Primitive::Type type,
3854 LocationSummary* locations,
3855 MipsLabel* label) {
3856 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3857 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3858 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3859 if (type == Primitive::kPrimFloat) {
3860 if (isR6) {
3861 switch (cond) {
3862 case kCondEQ:
3863 __ CmpEqS(FTMP, lhs, rhs);
3864 __ Bc1nez(FTMP, label);
3865 break;
3866 case kCondNE:
3867 __ CmpEqS(FTMP, lhs, rhs);
3868 __ Bc1eqz(FTMP, label);
3869 break;
3870 case kCondLT:
3871 if (gt_bias) {
3872 __ CmpLtS(FTMP, lhs, rhs);
3873 } else {
3874 __ CmpUltS(FTMP, lhs, rhs);
3875 }
3876 __ Bc1nez(FTMP, label);
3877 break;
3878 case kCondLE:
3879 if (gt_bias) {
3880 __ CmpLeS(FTMP, lhs, rhs);
3881 } else {
3882 __ CmpUleS(FTMP, lhs, rhs);
3883 }
3884 __ Bc1nez(FTMP, label);
3885 break;
3886 case kCondGT:
3887 if (gt_bias) {
3888 __ CmpUltS(FTMP, rhs, lhs);
3889 } else {
3890 __ CmpLtS(FTMP, rhs, lhs);
3891 }
3892 __ Bc1nez(FTMP, label);
3893 break;
3894 case kCondGE:
3895 if (gt_bias) {
3896 __ CmpUleS(FTMP, rhs, lhs);
3897 } else {
3898 __ CmpLeS(FTMP, rhs, lhs);
3899 }
3900 __ Bc1nez(FTMP, label);
3901 break;
3902 default:
3903 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003904 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003905 }
3906 } else {
3907 switch (cond) {
3908 case kCondEQ:
3909 __ CeqS(0, lhs, rhs);
3910 __ Bc1t(0, label);
3911 break;
3912 case kCondNE:
3913 __ CeqS(0, lhs, rhs);
3914 __ Bc1f(0, label);
3915 break;
3916 case kCondLT:
3917 if (gt_bias) {
3918 __ ColtS(0, lhs, rhs);
3919 } else {
3920 __ CultS(0, lhs, rhs);
3921 }
3922 __ Bc1t(0, label);
3923 break;
3924 case kCondLE:
3925 if (gt_bias) {
3926 __ ColeS(0, lhs, rhs);
3927 } else {
3928 __ CuleS(0, lhs, rhs);
3929 }
3930 __ Bc1t(0, label);
3931 break;
3932 case kCondGT:
3933 if (gt_bias) {
3934 __ CultS(0, rhs, lhs);
3935 } else {
3936 __ ColtS(0, rhs, lhs);
3937 }
3938 __ Bc1t(0, label);
3939 break;
3940 case kCondGE:
3941 if (gt_bias) {
3942 __ CuleS(0, rhs, lhs);
3943 } else {
3944 __ ColeS(0, rhs, lhs);
3945 }
3946 __ Bc1t(0, label);
3947 break;
3948 default:
3949 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003950 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003951 }
3952 }
3953 } else {
3954 DCHECK_EQ(type, Primitive::kPrimDouble);
3955 if (isR6) {
3956 switch (cond) {
3957 case kCondEQ:
3958 __ CmpEqD(FTMP, lhs, rhs);
3959 __ Bc1nez(FTMP, label);
3960 break;
3961 case kCondNE:
3962 __ CmpEqD(FTMP, lhs, rhs);
3963 __ Bc1eqz(FTMP, label);
3964 break;
3965 case kCondLT:
3966 if (gt_bias) {
3967 __ CmpLtD(FTMP, lhs, rhs);
3968 } else {
3969 __ CmpUltD(FTMP, lhs, rhs);
3970 }
3971 __ Bc1nez(FTMP, label);
3972 break;
3973 case kCondLE:
3974 if (gt_bias) {
3975 __ CmpLeD(FTMP, lhs, rhs);
3976 } else {
3977 __ CmpUleD(FTMP, lhs, rhs);
3978 }
3979 __ Bc1nez(FTMP, label);
3980 break;
3981 case kCondGT:
3982 if (gt_bias) {
3983 __ CmpUltD(FTMP, rhs, lhs);
3984 } else {
3985 __ CmpLtD(FTMP, rhs, lhs);
3986 }
3987 __ Bc1nez(FTMP, label);
3988 break;
3989 case kCondGE:
3990 if (gt_bias) {
3991 __ CmpUleD(FTMP, rhs, lhs);
3992 } else {
3993 __ CmpLeD(FTMP, rhs, lhs);
3994 }
3995 __ Bc1nez(FTMP, label);
3996 break;
3997 default:
3998 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003999 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004000 }
4001 } else {
4002 switch (cond) {
4003 case kCondEQ:
4004 __ CeqD(0, lhs, rhs);
4005 __ Bc1t(0, label);
4006 break;
4007 case kCondNE:
4008 __ CeqD(0, lhs, rhs);
4009 __ Bc1f(0, label);
4010 break;
4011 case kCondLT:
4012 if (gt_bias) {
4013 __ ColtD(0, lhs, rhs);
4014 } else {
4015 __ CultD(0, lhs, rhs);
4016 }
4017 __ Bc1t(0, label);
4018 break;
4019 case kCondLE:
4020 if (gt_bias) {
4021 __ ColeD(0, lhs, rhs);
4022 } else {
4023 __ CuleD(0, lhs, rhs);
4024 }
4025 __ Bc1t(0, label);
4026 break;
4027 case kCondGT:
4028 if (gt_bias) {
4029 __ CultD(0, rhs, lhs);
4030 } else {
4031 __ ColtD(0, rhs, lhs);
4032 }
4033 __ Bc1t(0, label);
4034 break;
4035 case kCondGE:
4036 if (gt_bias) {
4037 __ CuleD(0, rhs, lhs);
4038 } else {
4039 __ ColeD(0, rhs, lhs);
4040 }
4041 __ Bc1t(0, label);
4042 break;
4043 default:
4044 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004045 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004046 }
4047 }
4048 }
4049}
4050
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004051void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004052 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004053 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004054 MipsLabel* false_target) {
4055 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004056
David Brazdil0debae72015-11-12 18:37:00 +00004057 if (true_target == nullptr && false_target == nullptr) {
4058 // Nothing to do. The code always falls through.
4059 return;
4060 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004061 // Constant condition, statically compared against "true" (integer value 1).
4062 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004063 if (true_target != nullptr) {
4064 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004065 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004066 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004067 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004068 if (false_target != nullptr) {
4069 __ B(false_target);
4070 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004071 }
David Brazdil0debae72015-11-12 18:37:00 +00004072 return;
4073 }
4074
4075 // The following code generates these patterns:
4076 // (1) true_target == nullptr && false_target != nullptr
4077 // - opposite condition true => branch to false_target
4078 // (2) true_target != nullptr && false_target == nullptr
4079 // - condition true => branch to true_target
4080 // (3) true_target != nullptr && false_target != nullptr
4081 // - condition true => branch to true_target
4082 // - branch to false_target
4083 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004084 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004085 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004086 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004087 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004088 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4089 } else {
4090 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4091 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004092 } else {
4093 // The condition instruction has not been materialized, use its inputs as
4094 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004095 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004096 Primitive::Type type = condition->InputAt(0)->GetType();
4097 LocationSummary* locations = cond->GetLocations();
4098 IfCondition if_cond = condition->GetCondition();
4099 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004100
David Brazdil0debae72015-11-12 18:37:00 +00004101 if (true_target == nullptr) {
4102 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004103 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004104 }
4105
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004106 switch (type) {
4107 default:
4108 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4109 break;
4110 case Primitive::kPrimLong:
4111 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4112 break;
4113 case Primitive::kPrimFloat:
4114 case Primitive::kPrimDouble:
4115 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4116 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004117 }
4118 }
David Brazdil0debae72015-11-12 18:37:00 +00004119
4120 // If neither branch falls through (case 3), the conditional branch to `true_target`
4121 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4122 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004123 __ B(false_target);
4124 }
4125}
4126
4127void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4128 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004129 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004130 locations->SetInAt(0, Location::RequiresRegister());
4131 }
4132}
4133
4134void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004135 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4136 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4137 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4138 nullptr : codegen_->GetLabelOf(true_successor);
4139 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4140 nullptr : codegen_->GetLabelOf(false_successor);
4141 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004142}
4143
4144void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4145 LocationSummary* locations = new (GetGraph()->GetArena())
4146 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004147 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004148 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004149 locations->SetInAt(0, Location::RequiresRegister());
4150 }
4151}
4152
4153void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004154 SlowPathCodeMIPS* slow_path =
4155 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004156 GenerateTestAndBranch(deoptimize,
4157 /* condition_input_index */ 0,
4158 slow_path->GetEntryLabel(),
4159 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004160}
4161
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004162// This function returns true if a conditional move can be generated for HSelect.
4163// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4164// branches and regular moves.
4165//
4166// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4167//
4168// While determining feasibility of a conditional move and setting inputs/outputs
4169// are two distinct tasks, this function does both because they share quite a bit
4170// of common logic.
4171static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4172 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4173 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4174 HCondition* condition = cond->AsCondition();
4175
4176 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4177 Primitive::Type dst_type = select->GetType();
4178
4179 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4180 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4181 bool is_true_value_zero_constant =
4182 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4183 bool is_false_value_zero_constant =
4184 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4185
4186 bool can_move_conditionally = false;
4187 bool use_const_for_false_in = false;
4188 bool use_const_for_true_in = false;
4189
4190 if (!cond->IsConstant()) {
4191 switch (cond_type) {
4192 default:
4193 switch (dst_type) {
4194 default:
4195 // Moving int on int condition.
4196 if (is_r6) {
4197 if (is_true_value_zero_constant) {
4198 // seleqz out_reg, false_reg, cond_reg
4199 can_move_conditionally = true;
4200 use_const_for_true_in = true;
4201 } else if (is_false_value_zero_constant) {
4202 // selnez out_reg, true_reg, cond_reg
4203 can_move_conditionally = true;
4204 use_const_for_false_in = true;
4205 } else if (materialized) {
4206 // Not materializing unmaterialized int conditions
4207 // to keep the instruction count low.
4208 // selnez AT, true_reg, cond_reg
4209 // seleqz TMP, false_reg, cond_reg
4210 // or out_reg, AT, TMP
4211 can_move_conditionally = true;
4212 }
4213 } else {
4214 // movn out_reg, true_reg/ZERO, cond_reg
4215 can_move_conditionally = true;
4216 use_const_for_true_in = is_true_value_zero_constant;
4217 }
4218 break;
4219 case Primitive::kPrimLong:
4220 // Moving long on int condition.
4221 if (is_r6) {
4222 if (is_true_value_zero_constant) {
4223 // seleqz out_reg_lo, false_reg_lo, cond_reg
4224 // seleqz out_reg_hi, false_reg_hi, cond_reg
4225 can_move_conditionally = true;
4226 use_const_for_true_in = true;
4227 } else if (is_false_value_zero_constant) {
4228 // selnez out_reg_lo, true_reg_lo, cond_reg
4229 // selnez out_reg_hi, true_reg_hi, cond_reg
4230 can_move_conditionally = true;
4231 use_const_for_false_in = true;
4232 }
4233 // Other long conditional moves would generate 6+ instructions,
4234 // which is too many.
4235 } else {
4236 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4237 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4238 can_move_conditionally = true;
4239 use_const_for_true_in = is_true_value_zero_constant;
4240 }
4241 break;
4242 case Primitive::kPrimFloat:
4243 case Primitive::kPrimDouble:
4244 // Moving float/double on int condition.
4245 if (is_r6) {
4246 if (materialized) {
4247 // Not materializing unmaterialized int conditions
4248 // to keep the instruction count low.
4249 can_move_conditionally = true;
4250 if (is_true_value_zero_constant) {
4251 // sltu TMP, ZERO, cond_reg
4252 // mtc1 TMP, temp_cond_reg
4253 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4254 use_const_for_true_in = true;
4255 } else if (is_false_value_zero_constant) {
4256 // sltu TMP, ZERO, cond_reg
4257 // mtc1 TMP, temp_cond_reg
4258 // selnez.fmt out_reg, true_reg, temp_cond_reg
4259 use_const_for_false_in = true;
4260 } else {
4261 // sltu TMP, ZERO, cond_reg
4262 // mtc1 TMP, temp_cond_reg
4263 // sel.fmt temp_cond_reg, false_reg, true_reg
4264 // mov.fmt out_reg, temp_cond_reg
4265 }
4266 }
4267 } else {
4268 // movn.fmt out_reg, true_reg, cond_reg
4269 can_move_conditionally = true;
4270 }
4271 break;
4272 }
4273 break;
4274 case Primitive::kPrimLong:
4275 // We don't materialize long comparison now
4276 // and use conditional branches instead.
4277 break;
4278 case Primitive::kPrimFloat:
4279 case Primitive::kPrimDouble:
4280 switch (dst_type) {
4281 default:
4282 // Moving int on float/double condition.
4283 if (is_r6) {
4284 if (is_true_value_zero_constant) {
4285 // mfc1 TMP, temp_cond_reg
4286 // seleqz out_reg, false_reg, TMP
4287 can_move_conditionally = true;
4288 use_const_for_true_in = true;
4289 } else if (is_false_value_zero_constant) {
4290 // mfc1 TMP, temp_cond_reg
4291 // selnez out_reg, true_reg, TMP
4292 can_move_conditionally = true;
4293 use_const_for_false_in = true;
4294 } else {
4295 // mfc1 TMP, temp_cond_reg
4296 // selnez AT, true_reg, TMP
4297 // seleqz TMP, false_reg, TMP
4298 // or out_reg, AT, TMP
4299 can_move_conditionally = true;
4300 }
4301 } else {
4302 // movt out_reg, true_reg/ZERO, cc
4303 can_move_conditionally = true;
4304 use_const_for_true_in = is_true_value_zero_constant;
4305 }
4306 break;
4307 case Primitive::kPrimLong:
4308 // Moving long on float/double condition.
4309 if (is_r6) {
4310 if (is_true_value_zero_constant) {
4311 // mfc1 TMP, temp_cond_reg
4312 // seleqz out_reg_lo, false_reg_lo, TMP
4313 // seleqz out_reg_hi, false_reg_hi, TMP
4314 can_move_conditionally = true;
4315 use_const_for_true_in = true;
4316 } else if (is_false_value_zero_constant) {
4317 // mfc1 TMP, temp_cond_reg
4318 // selnez out_reg_lo, true_reg_lo, TMP
4319 // selnez out_reg_hi, true_reg_hi, TMP
4320 can_move_conditionally = true;
4321 use_const_for_false_in = true;
4322 }
4323 // Other long conditional moves would generate 6+ instructions,
4324 // which is too many.
4325 } else {
4326 // movt out_reg_lo, true_reg_lo/ZERO, cc
4327 // movt out_reg_hi, true_reg_hi/ZERO, cc
4328 can_move_conditionally = true;
4329 use_const_for_true_in = is_true_value_zero_constant;
4330 }
4331 break;
4332 case Primitive::kPrimFloat:
4333 case Primitive::kPrimDouble:
4334 // Moving float/double on float/double condition.
4335 if (is_r6) {
4336 can_move_conditionally = true;
4337 if (is_true_value_zero_constant) {
4338 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4339 use_const_for_true_in = true;
4340 } else if (is_false_value_zero_constant) {
4341 // selnez.fmt out_reg, true_reg, temp_cond_reg
4342 use_const_for_false_in = true;
4343 } else {
4344 // sel.fmt temp_cond_reg, false_reg, true_reg
4345 // mov.fmt out_reg, temp_cond_reg
4346 }
4347 } else {
4348 // movt.fmt out_reg, true_reg, cc
4349 can_move_conditionally = true;
4350 }
4351 break;
4352 }
4353 break;
4354 }
4355 }
4356
4357 if (can_move_conditionally) {
4358 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4359 } else {
4360 DCHECK(!use_const_for_false_in);
4361 DCHECK(!use_const_for_true_in);
4362 }
4363
4364 if (locations_to_set != nullptr) {
4365 if (use_const_for_false_in) {
4366 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4367 } else {
4368 locations_to_set->SetInAt(0,
4369 Primitive::IsFloatingPointType(dst_type)
4370 ? Location::RequiresFpuRegister()
4371 : Location::RequiresRegister());
4372 }
4373 if (use_const_for_true_in) {
4374 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4375 } else {
4376 locations_to_set->SetInAt(1,
4377 Primitive::IsFloatingPointType(dst_type)
4378 ? Location::RequiresFpuRegister()
4379 : Location::RequiresRegister());
4380 }
4381 if (materialized) {
4382 locations_to_set->SetInAt(2, Location::RequiresRegister());
4383 }
4384 // On R6 we don't require the output to be the same as the
4385 // first input for conditional moves unlike on R2.
4386 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4387 if (is_out_same_as_first_in) {
4388 locations_to_set->SetOut(Location::SameAsFirstInput());
4389 } else {
4390 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4391 ? Location::RequiresFpuRegister()
4392 : Location::RequiresRegister());
4393 }
4394 }
4395
4396 return can_move_conditionally;
4397}
4398
4399void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4400 LocationSummary* locations = select->GetLocations();
4401 Location dst = locations->Out();
4402 Location src = locations->InAt(1);
4403 Register src_reg = ZERO;
4404 Register src_reg_high = ZERO;
4405 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4406 Register cond_reg = TMP;
4407 int cond_cc = 0;
4408 Primitive::Type cond_type = Primitive::kPrimInt;
4409 bool cond_inverted = false;
4410 Primitive::Type dst_type = select->GetType();
4411
4412 if (IsBooleanValueOrMaterializedCondition(cond)) {
4413 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4414 } else {
4415 HCondition* condition = cond->AsCondition();
4416 LocationSummary* cond_locations = cond->GetLocations();
4417 IfCondition if_cond = condition->GetCondition();
4418 cond_type = condition->InputAt(0)->GetType();
4419 switch (cond_type) {
4420 default:
4421 DCHECK_NE(cond_type, Primitive::kPrimLong);
4422 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4423 break;
4424 case Primitive::kPrimFloat:
4425 case Primitive::kPrimDouble:
4426 cond_inverted = MaterializeFpCompareR2(if_cond,
4427 condition->IsGtBias(),
4428 cond_type,
4429 cond_locations,
4430 cond_cc);
4431 break;
4432 }
4433 }
4434
4435 DCHECK(dst.Equals(locations->InAt(0)));
4436 if (src.IsRegister()) {
4437 src_reg = src.AsRegister<Register>();
4438 } else if (src.IsRegisterPair()) {
4439 src_reg = src.AsRegisterPairLow<Register>();
4440 src_reg_high = src.AsRegisterPairHigh<Register>();
4441 } else if (src.IsConstant()) {
4442 DCHECK(src.GetConstant()->IsZeroBitPattern());
4443 }
4444
4445 switch (cond_type) {
4446 default:
4447 switch (dst_type) {
4448 default:
4449 if (cond_inverted) {
4450 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4451 } else {
4452 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4453 }
4454 break;
4455 case Primitive::kPrimLong:
4456 if (cond_inverted) {
4457 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4458 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4459 } else {
4460 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4461 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4462 }
4463 break;
4464 case Primitive::kPrimFloat:
4465 if (cond_inverted) {
4466 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4467 } else {
4468 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4469 }
4470 break;
4471 case Primitive::kPrimDouble:
4472 if (cond_inverted) {
4473 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4474 } else {
4475 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4476 }
4477 break;
4478 }
4479 break;
4480 case Primitive::kPrimLong:
4481 LOG(FATAL) << "Unreachable";
4482 UNREACHABLE();
4483 case Primitive::kPrimFloat:
4484 case Primitive::kPrimDouble:
4485 switch (dst_type) {
4486 default:
4487 if (cond_inverted) {
4488 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4489 } else {
4490 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4491 }
4492 break;
4493 case Primitive::kPrimLong:
4494 if (cond_inverted) {
4495 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4496 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4497 } else {
4498 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4499 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4500 }
4501 break;
4502 case Primitive::kPrimFloat:
4503 if (cond_inverted) {
4504 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4505 } else {
4506 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4507 }
4508 break;
4509 case Primitive::kPrimDouble:
4510 if (cond_inverted) {
4511 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4512 } else {
4513 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4514 }
4515 break;
4516 }
4517 break;
4518 }
4519}
4520
4521void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4522 LocationSummary* locations = select->GetLocations();
4523 Location dst = locations->Out();
4524 Location false_src = locations->InAt(0);
4525 Location true_src = locations->InAt(1);
4526 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4527 Register cond_reg = TMP;
4528 FRegister fcond_reg = FTMP;
4529 Primitive::Type cond_type = Primitive::kPrimInt;
4530 bool cond_inverted = false;
4531 Primitive::Type dst_type = select->GetType();
4532
4533 if (IsBooleanValueOrMaterializedCondition(cond)) {
4534 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4535 } else {
4536 HCondition* condition = cond->AsCondition();
4537 LocationSummary* cond_locations = cond->GetLocations();
4538 IfCondition if_cond = condition->GetCondition();
4539 cond_type = condition->InputAt(0)->GetType();
4540 switch (cond_type) {
4541 default:
4542 DCHECK_NE(cond_type, Primitive::kPrimLong);
4543 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4544 break;
4545 case Primitive::kPrimFloat:
4546 case Primitive::kPrimDouble:
4547 cond_inverted = MaterializeFpCompareR6(if_cond,
4548 condition->IsGtBias(),
4549 cond_type,
4550 cond_locations,
4551 fcond_reg);
4552 break;
4553 }
4554 }
4555
4556 if (true_src.IsConstant()) {
4557 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4558 }
4559 if (false_src.IsConstant()) {
4560 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4561 }
4562
4563 switch (dst_type) {
4564 default:
4565 if (Primitive::IsFloatingPointType(cond_type)) {
4566 __ Mfc1(cond_reg, fcond_reg);
4567 }
4568 if (true_src.IsConstant()) {
4569 if (cond_inverted) {
4570 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4571 } else {
4572 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4573 }
4574 } else if (false_src.IsConstant()) {
4575 if (cond_inverted) {
4576 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4577 } else {
4578 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4579 }
4580 } else {
4581 DCHECK_NE(cond_reg, AT);
4582 if (cond_inverted) {
4583 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4584 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4585 } else {
4586 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4587 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4588 }
4589 __ Or(dst.AsRegister<Register>(), AT, TMP);
4590 }
4591 break;
4592 case Primitive::kPrimLong: {
4593 if (Primitive::IsFloatingPointType(cond_type)) {
4594 __ Mfc1(cond_reg, fcond_reg);
4595 }
4596 Register dst_lo = dst.AsRegisterPairLow<Register>();
4597 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4598 if (true_src.IsConstant()) {
4599 Register src_lo = false_src.AsRegisterPairLow<Register>();
4600 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4601 if (cond_inverted) {
4602 __ Selnez(dst_lo, src_lo, cond_reg);
4603 __ Selnez(dst_hi, src_hi, cond_reg);
4604 } else {
4605 __ Seleqz(dst_lo, src_lo, cond_reg);
4606 __ Seleqz(dst_hi, src_hi, cond_reg);
4607 }
4608 } else {
4609 DCHECK(false_src.IsConstant());
4610 Register src_lo = true_src.AsRegisterPairLow<Register>();
4611 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4612 if (cond_inverted) {
4613 __ Seleqz(dst_lo, src_lo, cond_reg);
4614 __ Seleqz(dst_hi, src_hi, cond_reg);
4615 } else {
4616 __ Selnez(dst_lo, src_lo, cond_reg);
4617 __ Selnez(dst_hi, src_hi, cond_reg);
4618 }
4619 }
4620 break;
4621 }
4622 case Primitive::kPrimFloat: {
4623 if (!Primitive::IsFloatingPointType(cond_type)) {
4624 // sel*.fmt tests bit 0 of the condition register, account for that.
4625 __ Sltu(TMP, ZERO, cond_reg);
4626 __ Mtc1(TMP, fcond_reg);
4627 }
4628 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4629 if (true_src.IsConstant()) {
4630 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4631 if (cond_inverted) {
4632 __ SelnezS(dst_reg, src_reg, fcond_reg);
4633 } else {
4634 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4635 }
4636 } else if (false_src.IsConstant()) {
4637 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4638 if (cond_inverted) {
4639 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4640 } else {
4641 __ SelnezS(dst_reg, src_reg, fcond_reg);
4642 }
4643 } else {
4644 if (cond_inverted) {
4645 __ SelS(fcond_reg,
4646 true_src.AsFpuRegister<FRegister>(),
4647 false_src.AsFpuRegister<FRegister>());
4648 } else {
4649 __ SelS(fcond_reg,
4650 false_src.AsFpuRegister<FRegister>(),
4651 true_src.AsFpuRegister<FRegister>());
4652 }
4653 __ MovS(dst_reg, fcond_reg);
4654 }
4655 break;
4656 }
4657 case Primitive::kPrimDouble: {
4658 if (!Primitive::IsFloatingPointType(cond_type)) {
4659 // sel*.fmt tests bit 0 of the condition register, account for that.
4660 __ Sltu(TMP, ZERO, cond_reg);
4661 __ Mtc1(TMP, fcond_reg);
4662 }
4663 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4664 if (true_src.IsConstant()) {
4665 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4666 if (cond_inverted) {
4667 __ SelnezD(dst_reg, src_reg, fcond_reg);
4668 } else {
4669 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4670 }
4671 } else if (false_src.IsConstant()) {
4672 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4673 if (cond_inverted) {
4674 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4675 } else {
4676 __ SelnezD(dst_reg, src_reg, fcond_reg);
4677 }
4678 } else {
4679 if (cond_inverted) {
4680 __ SelD(fcond_reg,
4681 true_src.AsFpuRegister<FRegister>(),
4682 false_src.AsFpuRegister<FRegister>());
4683 } else {
4684 __ SelD(fcond_reg,
4685 false_src.AsFpuRegister<FRegister>(),
4686 true_src.AsFpuRegister<FRegister>());
4687 }
4688 __ MovD(dst_reg, fcond_reg);
4689 }
4690 break;
4691 }
4692 }
4693}
4694
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004695void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4696 LocationSummary* locations = new (GetGraph()->GetArena())
4697 LocationSummary(flag, LocationSummary::kNoCall);
4698 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004699}
4700
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004701void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4702 __ LoadFromOffset(kLoadWord,
4703 flag->GetLocations()->Out().AsRegister<Register>(),
4704 SP,
4705 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004706}
4707
David Brazdil74eb1b22015-12-14 11:44:01 +00004708void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4709 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004710 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004711}
4712
4713void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004714 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
4715 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
4716 if (is_r6) {
4717 GenConditionalMoveR6(select);
4718 } else {
4719 GenConditionalMoveR2(select);
4720 }
4721 } else {
4722 LocationSummary* locations = select->GetLocations();
4723 MipsLabel false_target;
4724 GenerateTestAndBranch(select,
4725 /* condition_input_index */ 2,
4726 /* true_target */ nullptr,
4727 &false_target);
4728 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4729 __ Bind(&false_target);
4730 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004731}
4732
David Srbecky0cf44932015-12-09 14:09:59 +00004733void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4734 new (GetGraph()->GetArena()) LocationSummary(info);
4735}
4736
David Srbeckyd28f4a02016-03-14 17:14:24 +00004737void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
4738 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004739}
4740
4741void CodeGeneratorMIPS::GenerateNop() {
4742 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004743}
4744
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004745void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4746 Primitive::Type field_type = field_info.GetFieldType();
4747 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4748 bool generate_volatile = field_info.IsVolatile() && is_wide;
4749 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004750 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004751
4752 locations->SetInAt(0, Location::RequiresRegister());
4753 if (generate_volatile) {
4754 InvokeRuntimeCallingConvention calling_convention;
4755 // need A0 to hold base + offset
4756 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4757 if (field_type == Primitive::kPrimLong) {
4758 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
4759 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004760 // Use Location::Any() to prevent situations when running out of available fp registers.
4761 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004762 // Need some temp core regs since FP results are returned in core registers
4763 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
4764 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
4765 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
4766 }
4767 } else {
4768 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4769 locations->SetOut(Location::RequiresFpuRegister());
4770 } else {
4771 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4772 }
4773 }
4774}
4775
4776void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
4777 const FieldInfo& field_info,
4778 uint32_t dex_pc) {
4779 Primitive::Type type = field_info.GetFieldType();
4780 LocationSummary* locations = instruction->GetLocations();
4781 Register obj = locations->InAt(0).AsRegister<Register>();
4782 LoadOperandType load_type = kLoadUnsignedByte;
4783 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004784 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004785 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004786
4787 switch (type) {
4788 case Primitive::kPrimBoolean:
4789 load_type = kLoadUnsignedByte;
4790 break;
4791 case Primitive::kPrimByte:
4792 load_type = kLoadSignedByte;
4793 break;
4794 case Primitive::kPrimShort:
4795 load_type = kLoadSignedHalfword;
4796 break;
4797 case Primitive::kPrimChar:
4798 load_type = kLoadUnsignedHalfword;
4799 break;
4800 case Primitive::kPrimInt:
4801 case Primitive::kPrimFloat:
4802 case Primitive::kPrimNot:
4803 load_type = kLoadWord;
4804 break;
4805 case Primitive::kPrimLong:
4806 case Primitive::kPrimDouble:
4807 load_type = kLoadDoubleword;
4808 break;
4809 case Primitive::kPrimVoid:
4810 LOG(FATAL) << "Unreachable type " << type;
4811 UNREACHABLE();
4812 }
4813
4814 if (is_volatile && load_type == kLoadDoubleword) {
4815 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004816 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004817 // Do implicit Null check
4818 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4819 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01004820 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004821 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
4822 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004823 // FP results are returned in core registers. Need to move them.
4824 Location out = locations->Out();
4825 if (out.IsFpuRegister()) {
4826 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4827 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4828 out.AsFpuRegister<FRegister>());
4829 } else {
4830 DCHECK(out.IsDoubleStackSlot());
4831 __ StoreToOffset(kStoreWord,
4832 locations->GetTemp(1).AsRegister<Register>(),
4833 SP,
4834 out.GetStackIndex());
4835 __ StoreToOffset(kStoreWord,
4836 locations->GetTemp(2).AsRegister<Register>(),
4837 SP,
4838 out.GetStackIndex() + 4);
4839 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004840 }
4841 } else {
4842 if (!Primitive::IsFloatingPointType(type)) {
4843 Register dst;
4844 if (type == Primitive::kPrimLong) {
4845 DCHECK(locations->Out().IsRegisterPair());
4846 dst = locations->Out().AsRegisterPairLow<Register>();
4847 } else {
4848 DCHECK(locations->Out().IsRegister());
4849 dst = locations->Out().AsRegister<Register>();
4850 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004851 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004852 } else {
4853 DCHECK(locations->Out().IsFpuRegister());
4854 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4855 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004856 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004857 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004858 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004859 }
4860 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004861 }
4862
4863 if (is_volatile) {
4864 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4865 }
4866}
4867
4868void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4869 Primitive::Type field_type = field_info.GetFieldType();
4870 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4871 bool generate_volatile = field_info.IsVolatile() && is_wide;
4872 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004873 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004874
4875 locations->SetInAt(0, Location::RequiresRegister());
4876 if (generate_volatile) {
4877 InvokeRuntimeCallingConvention calling_convention;
4878 // need A0 to hold base + offset
4879 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4880 if (field_type == Primitive::kPrimLong) {
4881 locations->SetInAt(1, Location::RegisterPairLocation(
4882 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4883 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004884 // Use Location::Any() to prevent situations when running out of available fp registers.
4885 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004886 // Pass FP parameters in core registers.
4887 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4888 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4889 }
4890 } else {
4891 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004892 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004893 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004894 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004895 }
4896 }
4897}
4898
4899void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
4900 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01004901 uint32_t dex_pc,
4902 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004903 Primitive::Type type = field_info.GetFieldType();
4904 LocationSummary* locations = instruction->GetLocations();
4905 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07004906 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004907 StoreOperandType store_type = kStoreByte;
4908 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004909 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004910 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004911
4912 switch (type) {
4913 case Primitive::kPrimBoolean:
4914 case Primitive::kPrimByte:
4915 store_type = kStoreByte;
4916 break;
4917 case Primitive::kPrimShort:
4918 case Primitive::kPrimChar:
4919 store_type = kStoreHalfword;
4920 break;
4921 case Primitive::kPrimInt:
4922 case Primitive::kPrimFloat:
4923 case Primitive::kPrimNot:
4924 store_type = kStoreWord;
4925 break;
4926 case Primitive::kPrimLong:
4927 case Primitive::kPrimDouble:
4928 store_type = kStoreDoubleword;
4929 break;
4930 case Primitive::kPrimVoid:
4931 LOG(FATAL) << "Unreachable type " << type;
4932 UNREACHABLE();
4933 }
4934
4935 if (is_volatile) {
4936 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4937 }
4938
4939 if (is_volatile && store_type == kStoreDoubleword) {
4940 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004941 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004942 // Do implicit Null check.
4943 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4944 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4945 if (type == Primitive::kPrimDouble) {
4946 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004947 if (value_location.IsFpuRegister()) {
4948 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4949 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004950 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004951 value_location.AsFpuRegister<FRegister>());
4952 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004953 __ LoadFromOffset(kLoadWord,
4954 locations->GetTemp(1).AsRegister<Register>(),
4955 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004956 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004957 __ LoadFromOffset(kLoadWord,
4958 locations->GetTemp(2).AsRegister<Register>(),
4959 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004960 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004961 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004962 DCHECK(value_location.IsConstant());
4963 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4964 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004965 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4966 locations->GetTemp(1).AsRegister<Register>(),
4967 value);
4968 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004969 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004970 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004971 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4972 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004973 if (value_location.IsConstant()) {
4974 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4975 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4976 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004977 Register src;
4978 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004979 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004980 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004981 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004982 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004983 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004984 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004985 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004986 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004987 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004988 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004989 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004990 }
4991 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004992 }
4993
4994 // TODO: memory barriers?
4995 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004996 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01004997 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004998 }
4999
5000 if (is_volatile) {
5001 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5002 }
5003}
5004
5005void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5006 HandleFieldGet(instruction, instruction->GetFieldInfo());
5007}
5008
5009void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5010 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5011}
5012
5013void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5014 HandleFieldSet(instruction, instruction->GetFieldInfo());
5015}
5016
5017void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01005018 HandleFieldSet(instruction,
5019 instruction->GetFieldInfo(),
5020 instruction->GetDexPc(),
5021 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005022}
5023
Alexey Frunze06a46c42016-07-19 15:00:40 -07005024void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
5025 HInstruction* instruction ATTRIBUTE_UNUSED,
5026 Location root,
5027 Register obj,
5028 uint32_t offset) {
5029 Register root_reg = root.AsRegister<Register>();
5030 if (kEmitCompilerReadBarrier) {
5031 UNIMPLEMENTED(FATAL) << "for read barrier";
5032 } else {
5033 // Plain GC root load with no read barrier.
5034 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5035 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5036 // Note that GC roots are not affected by heap poisoning, thus we
5037 // do not have to unpoison `root_reg` here.
5038 }
5039}
5040
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005041void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5042 LocationSummary::CallKind call_kind =
5043 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5044 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5045 locations->SetInAt(0, Location::RequiresRegister());
5046 locations->SetInAt(1, Location::RequiresRegister());
5047 // The output does overlap inputs.
5048 // Note that TypeCheckSlowPathMIPS uses this register too.
5049 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5050}
5051
5052void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5053 LocationSummary* locations = instruction->GetLocations();
5054 Register obj = locations->InAt(0).AsRegister<Register>();
5055 Register cls = locations->InAt(1).AsRegister<Register>();
5056 Register out = locations->Out().AsRegister<Register>();
5057
5058 MipsLabel done;
5059
5060 // Return 0 if `obj` is null.
5061 // TODO: Avoid this check if we know `obj` is not null.
5062 __ Move(out, ZERO);
5063 __ Beqz(obj, &done);
5064
5065 // Compare the class of `obj` with `cls`.
5066 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
5067 if (instruction->IsExactCheck()) {
5068 // Classes must be equal for the instanceof to succeed.
5069 __ Xor(out, out, cls);
5070 __ Sltiu(out, out, 1);
5071 } else {
5072 // If the classes are not equal, we go into a slow path.
5073 DCHECK(locations->OnlyCallsOnSlowPath());
5074 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5075 codegen_->AddSlowPath(slow_path);
5076 __ Bne(out, cls, slow_path->GetEntryLabel());
5077 __ LoadConst32(out, 1);
5078 __ Bind(slow_path->GetExitLabel());
5079 }
5080
5081 __ Bind(&done);
5082}
5083
5084void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5085 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5086 locations->SetOut(Location::ConstantLocation(constant));
5087}
5088
5089void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5090 // Will be generated at use site.
5091}
5092
5093void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5094 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5095 locations->SetOut(Location::ConstantLocation(constant));
5096}
5097
5098void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5099 // Will be generated at use site.
5100}
5101
5102void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5103 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5104 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5105}
5106
5107void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5108 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005109 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005110 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005111 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005112}
5113
5114void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5115 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5116 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005117 Location receiver = invoke->GetLocations()->InAt(0);
5118 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005119 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005120
5121 // Set the hidden argument.
5122 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5123 invoke->GetDexMethodIndex());
5124
5125 // temp = object->GetClass();
5126 if (receiver.IsStackSlot()) {
5127 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5128 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5129 } else {
5130 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5131 }
5132 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005133 __ LoadFromOffset(kLoadWord, temp, temp,
5134 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5135 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005136 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005137 // temp = temp->GetImtEntryAt(method_offset);
5138 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5139 // T9 = temp->GetEntryPoint();
5140 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5141 // T9();
5142 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005143 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005144 DCHECK(!codegen_->IsLeafMethod());
5145 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5146}
5147
5148void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005149 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5150 if (intrinsic.TryDispatch(invoke)) {
5151 return;
5152 }
5153
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005154 HandleInvoke(invoke);
5155}
5156
5157void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005158 // Explicit clinit checks triggered by static invokes must have been pruned by
5159 // art::PrepareForRegisterAllocation.
5160 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005161
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00005162 bool has_extra_input = invoke->HasPcRelativeDexCache();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005163
Chris Larsen701566a2015-10-27 15:29:13 -07005164 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5165 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005166 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5167 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5168 }
Chris Larsen701566a2015-10-27 15:29:13 -07005169 return;
5170 }
5171
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005172 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005173
5174 // Add the extra input register if either the dex cache array base register
5175 // or the PC-relative base register for accessing literals is needed.
5176 if (has_extra_input) {
5177 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5178 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005179}
5180
Orion Hodsonac141392017-01-13 11:53:47 +00005181void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5182 HandleInvoke(invoke);
5183}
5184
5185void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5186 codegen_->GenerateInvokePolymorphicCall(invoke);
5187}
5188
Chris Larsen701566a2015-10-27 15:29:13 -07005189static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005190 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005191 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5192 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005193 return true;
5194 }
5195 return false;
5196}
5197
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005198HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005199 HLoadString::LoadKind desired_string_load_kind) {
5200 if (kEmitCompilerReadBarrier) {
5201 UNIMPLEMENTED(FATAL) << "for read barrier";
5202 }
5203 // We disable PC-relative load when there is an irreducible loop, as the optimization
5204 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005205 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5206 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005207 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5208 bool fallback_load = has_irreducible_loops;
5209 switch (desired_string_load_kind) {
5210 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5211 DCHECK(!GetCompilerOptions().GetCompilePic());
5212 break;
5213 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5214 DCHECK(GetCompilerOptions().GetCompilePic());
5215 break;
5216 case HLoadString::LoadKind::kBootImageAddress:
5217 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005218 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005219 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005220 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005221 case HLoadString::LoadKind::kJitTableAddress:
5222 DCHECK(Runtime::Current()->UseJitCompilation());
5223 // TODO: implement.
5224 fallback_load = true;
5225 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005226 case HLoadString::LoadKind::kDexCacheViaMethod:
5227 fallback_load = false;
5228 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005229 }
5230 if (fallback_load) {
5231 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5232 }
5233 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005234}
5235
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005236HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5237 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005238 if (kEmitCompilerReadBarrier) {
5239 UNIMPLEMENTED(FATAL) << "for read barrier";
5240 }
5241 // We disable pc-relative load when there is an irreducible loop, as the optimization
5242 // is incompatible with it.
5243 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5244 bool fallback_load = has_irreducible_loops;
5245 switch (desired_class_load_kind) {
5246 case HLoadClass::LoadKind::kReferrersClass:
5247 fallback_load = false;
5248 break;
5249 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5250 DCHECK(!GetCompilerOptions().GetCompilePic());
5251 break;
5252 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5253 DCHECK(GetCompilerOptions().GetCompilePic());
5254 break;
5255 case HLoadClass::LoadKind::kBootImageAddress:
5256 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005257 case HLoadClass::LoadKind::kBssEntry:
5258 DCHECK(!Runtime::Current()->UseJitCompilation());
5259 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005260 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005261 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005262 fallback_load = true;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005263 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005264 case HLoadClass::LoadKind::kDexCacheViaMethod:
5265 fallback_load = false;
5266 break;
5267 }
5268 if (fallback_load) {
5269 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5270 }
5271 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005272}
5273
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005274Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5275 Register temp) {
5276 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5277 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5278 if (!invoke->GetLocations()->Intrinsified()) {
5279 return location.AsRegister<Register>();
5280 }
5281 // For intrinsics we allow any location, so it may be on the stack.
5282 if (!location.IsRegister()) {
5283 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5284 return temp;
5285 }
5286 // For register locations, check if the register was saved. If so, get it from the stack.
5287 // Note: There is a chance that the register was saved but not overwritten, so we could
5288 // save one load. However, since this is just an intrinsic slow path we prefer this
5289 // simple and more robust approach rather that trying to determine if that's the case.
5290 SlowPathCode* slow_path = GetCurrentSlowPath();
5291 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5292 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5293 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5294 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5295 return temp;
5296 }
5297 return location.AsRegister<Register>();
5298}
5299
Vladimir Markodc151b22015-10-15 18:02:30 +01005300HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5301 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005302 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005303 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
5304 // We disable PC-relative load when there is an irreducible loop, as the optimization
5305 // is incompatible with it.
5306 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5307 bool fallback_load = true;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005308 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005309 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005310 fallback_load = has_irreducible_loops;
5311 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005312 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005313 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005314 break;
5315 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005316 if (fallback_load) {
5317 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5318 dispatch_info.method_load_data = 0;
5319 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005320 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005321}
5322
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005323void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5324 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005325 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005326 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5327 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00005328 Register base_reg = invoke->HasPcRelativeDexCache()
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005329 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5330 : ZERO;
5331
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005332 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005333 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005334 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005335 uint32_t offset =
5336 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005337 __ LoadFromOffset(kLoadWord,
5338 temp.AsRegister<Register>(),
5339 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005340 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005341 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005342 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005343 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005344 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005345 break;
5346 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5347 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5348 break;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005349 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
5350 HMipsDexCacheArraysBase* base =
5351 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5352 int32_t offset =
5353 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5354 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5355 break;
5356 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005357 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005358 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005359 Register reg = temp.AsRegister<Register>();
5360 Register method_reg;
5361 if (current_method.IsRegister()) {
5362 method_reg = current_method.AsRegister<Register>();
5363 } else {
5364 // TODO: use the appropriate DCHECK() here if possible.
5365 // DCHECK(invoke->GetLocations()->Intrinsified());
5366 DCHECK(!current_method.IsValid());
5367 method_reg = reg;
5368 __ Lw(reg, SP, kCurrentMethodStackOffset);
5369 }
5370
5371 // temp = temp->dex_cache_resolved_methods_;
5372 __ LoadFromOffset(kLoadWord,
5373 reg,
5374 method_reg,
5375 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005376 // temp = temp[index_in_cache];
5377 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5378 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005379 __ LoadFromOffset(kLoadWord,
5380 reg,
5381 reg,
5382 CodeGenerator::GetCachePointerOffset(index_in_cache));
5383 break;
5384 }
5385 }
5386
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005387 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005388 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005389 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005390 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005391 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5392 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005393 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005394 T9,
5395 callee_method.AsRegister<Register>(),
5396 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005397 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005398 // T9()
5399 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005400 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005401 break;
5402 }
5403 DCHECK(!IsLeafMethod());
5404}
5405
5406void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005407 // Explicit clinit checks triggered by static invokes must have been pruned by
5408 // art::PrepareForRegisterAllocation.
5409 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005410
5411 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5412 return;
5413 }
5414
5415 LocationSummary* locations = invoke->GetLocations();
5416 codegen_->GenerateStaticOrDirectCall(invoke,
5417 locations->HasTemps()
5418 ? locations->GetTemp(0)
5419 : Location::NoLocation());
5420 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5421}
5422
Chris Larsen3acee732015-11-18 13:31:08 -08005423void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005424 // Use the calling convention instead of the location of the receiver, as
5425 // intrinsics may have put the receiver in a different register. In the intrinsics
5426 // slow path, the arguments have been moved to the right place, so here we are
5427 // guaranteed that the receiver is the first register of the calling convention.
5428 InvokeDexCallingConvention calling_convention;
5429 Register receiver = calling_convention.GetRegisterAt(0);
5430
Chris Larsen3acee732015-11-18 13:31:08 -08005431 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005432 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5433 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5434 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005435 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005436
5437 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005438 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005439 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005440 // temp = temp->GetMethodAt(method_offset);
5441 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5442 // T9 = temp->GetEntryPoint();
5443 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5444 // T9();
5445 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005446 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005447}
5448
5449void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5450 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5451 return;
5452 }
5453
5454 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005455 DCHECK(!codegen_->IsLeafMethod());
5456 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5457}
5458
5459void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005460 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5461 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005462 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005463 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005464 cls,
5465 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005466 Location::RegisterLocation(V0));
Alexey Frunze06a46c42016-07-19 15:00:40 -07005467 return;
5468 }
Vladimir Marko41559982017-01-06 14:04:23 +00005469 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005470
5471 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5472 ? LocationSummary::kCallOnSlowPath
5473 : LocationSummary::kNoCall;
5474 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005475 switch (load_kind) {
5476 // We need an extra register for PC-relative literals on R2.
5477 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005478 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005479 case HLoadClass::LoadKind::kBootImageAddress:
5480 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005481 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5482 break;
5483 }
5484 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005485 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005486 locations->SetInAt(0, Location::RequiresRegister());
5487 break;
5488 default:
5489 break;
5490 }
5491 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005492}
5493
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005494// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5495// move.
5496void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005497 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5498 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5499 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01005500 return;
5501 }
Vladimir Marko41559982017-01-06 14:04:23 +00005502 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01005503
Vladimir Marko41559982017-01-06 14:04:23 +00005504 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005505 Location out_loc = locations->Out();
5506 Register out = out_loc.AsRegister<Register>();
5507 Register base_or_current_method_reg;
5508 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5509 switch (load_kind) {
5510 // We need an extra register for PC-relative literals on R2.
5511 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005512 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005513 case HLoadClass::LoadKind::kBootImageAddress:
5514 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005515 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5516 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005517 case HLoadClass::LoadKind::kReferrersClass:
5518 case HLoadClass::LoadKind::kDexCacheViaMethod:
5519 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5520 break;
5521 default:
5522 base_or_current_method_reg = ZERO;
5523 break;
5524 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005525
Alexey Frunze06a46c42016-07-19 15:00:40 -07005526 bool generate_null_check = false;
5527 switch (load_kind) {
5528 case HLoadClass::LoadKind::kReferrersClass: {
5529 DCHECK(!cls->CanCallRuntime());
5530 DCHECK(!cls->MustGenerateClinitCheck());
5531 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5532 GenerateGcRootFieldLoad(cls,
5533 out_loc,
5534 base_or_current_method_reg,
5535 ArtMethod::DeclaringClassOffset().Int32Value());
5536 break;
5537 }
5538 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005539 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005540 __ LoadLiteral(out,
5541 base_or_current_method_reg,
5542 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5543 cls->GetTypeIndex()));
5544 break;
5545 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005546 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005547 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5548 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005549 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005550 break;
5551 }
5552 case HLoadClass::LoadKind::kBootImageAddress: {
5553 DCHECK(!kEmitCompilerReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005554 uint32_t address = dchecked_integral_cast<uint32_t>(
5555 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5556 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005557 __ LoadLiteral(out,
5558 base_or_current_method_reg,
5559 codegen_->DeduplicateBootImageAddressLiteral(address));
5560 break;
5561 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005562 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005563 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005564 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005565 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
5566 __ LoadFromOffset(kLoadWord, out, out, 0);
5567 generate_null_check = true;
5568 break;
5569 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005570 case HLoadClass::LoadKind::kJitTableAddress: {
5571 LOG(FATAL) << "Unimplemented";
Alexey Frunze06a46c42016-07-19 15:00:40 -07005572 break;
5573 }
Vladimir Marko41559982017-01-06 14:04:23 +00005574 case HLoadClass::LoadKind::kDexCacheViaMethod:
5575 LOG(FATAL) << "UNREACHABLE";
5576 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005577 }
5578
5579 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5580 DCHECK(cls->CanCallRuntime());
5581 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5582 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5583 codegen_->AddSlowPath(slow_path);
5584 if (generate_null_check) {
5585 __ Beqz(out, slow_path->GetEntryLabel());
5586 }
5587 if (cls->MustGenerateClinitCheck()) {
5588 GenerateClassInitializationCheck(slow_path, out);
5589 } else {
5590 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005591 }
5592 }
5593}
5594
5595static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005596 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005597}
5598
5599void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5600 LocationSummary* locations =
5601 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5602 locations->SetOut(Location::RequiresRegister());
5603}
5604
5605void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5606 Register out = load->GetLocations()->Out().AsRegister<Register>();
5607 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5608}
5609
5610void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5611 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5612}
5613
5614void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5615 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5616}
5617
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005618void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005619 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005620 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005621 HLoadString::LoadKind load_kind = load->GetLoadKind();
5622 switch (load_kind) {
5623 // We need an extra register for PC-relative literals on R2.
5624 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5625 case HLoadString::LoadKind::kBootImageAddress:
5626 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005627 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005628 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5629 break;
5630 }
5631 FALLTHROUGH_INTENDED;
5632 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005633 case HLoadString::LoadKind::kDexCacheViaMethod:
5634 locations->SetInAt(0, Location::RequiresRegister());
5635 break;
5636 default:
5637 break;
5638 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005639 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5640 InvokeRuntimeCallingConvention calling_convention;
5641 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5642 } else {
5643 locations->SetOut(Location::RequiresRegister());
5644 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005645}
5646
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005647// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5648// move.
5649void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005650 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005651 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005652 Location out_loc = locations->Out();
5653 Register out = out_loc.AsRegister<Register>();
5654 Register base_or_current_method_reg;
5655 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5656 switch (load_kind) {
5657 // We need an extra register for PC-relative literals on R2.
5658 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5659 case HLoadString::LoadKind::kBootImageAddress:
5660 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005661 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005662 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5663 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005664 default:
5665 base_or_current_method_reg = ZERO;
5666 break;
5667 }
5668
5669 switch (load_kind) {
5670 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005671 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005672 __ LoadLiteral(out,
5673 base_or_current_method_reg,
5674 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5675 load->GetStringIndex()));
5676 return; // No dex cache slow path.
5677 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005678 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005679 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005680 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005681 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005682 return; // No dex cache slow path.
5683 }
5684 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005685 uint32_t address = dchecked_integral_cast<uint32_t>(
5686 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5687 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005688 __ LoadLiteral(out,
5689 base_or_current_method_reg,
5690 codegen_->DeduplicateBootImageAddressLiteral(address));
5691 return; // No dex cache slow path.
5692 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005693 case HLoadString::LoadKind::kBssEntry: {
5694 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5695 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005696 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005697 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
5698 __ LoadFromOffset(kLoadWord, out, out, 0);
5699 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5700 codegen_->AddSlowPath(slow_path);
5701 __ Beqz(out, slow_path->GetEntryLabel());
5702 __ Bind(slow_path->GetExitLabel());
5703 return;
5704 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005705 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005706 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005707 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005708
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005709 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005710 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5711 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005712 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005713 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5714 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005715}
5716
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005717void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5718 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5719 locations->SetOut(Location::ConstantLocation(constant));
5720}
5721
5722void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5723 // Will be generated at use site.
5724}
5725
5726void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5727 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005728 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005729 InvokeRuntimeCallingConvention calling_convention;
5730 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5731}
5732
5733void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5734 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005735 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005736 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5737 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005738 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005739 }
5740 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5741}
5742
5743void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5744 LocationSummary* locations =
5745 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5746 switch (mul->GetResultType()) {
5747 case Primitive::kPrimInt:
5748 case Primitive::kPrimLong:
5749 locations->SetInAt(0, Location::RequiresRegister());
5750 locations->SetInAt(1, Location::RequiresRegister());
5751 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5752 break;
5753
5754 case Primitive::kPrimFloat:
5755 case Primitive::kPrimDouble:
5756 locations->SetInAt(0, Location::RequiresFpuRegister());
5757 locations->SetInAt(1, Location::RequiresFpuRegister());
5758 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5759 break;
5760
5761 default:
5762 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5763 }
5764}
5765
5766void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5767 Primitive::Type type = instruction->GetType();
5768 LocationSummary* locations = instruction->GetLocations();
5769 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5770
5771 switch (type) {
5772 case Primitive::kPrimInt: {
5773 Register dst = locations->Out().AsRegister<Register>();
5774 Register lhs = locations->InAt(0).AsRegister<Register>();
5775 Register rhs = locations->InAt(1).AsRegister<Register>();
5776
5777 if (isR6) {
5778 __ MulR6(dst, lhs, rhs);
5779 } else {
5780 __ MulR2(dst, lhs, rhs);
5781 }
5782 break;
5783 }
5784 case Primitive::kPrimLong: {
5785 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5786 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5787 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5788 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5789 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5790 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5791
5792 // Extra checks to protect caused by the existance of A1_A2.
5793 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5794 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5795 DCHECK_NE(dst_high, lhs_low);
5796 DCHECK_NE(dst_high, rhs_low);
5797
5798 // A_B * C_D
5799 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5800 // dst_lo: [ low(B*D) ]
5801 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5802
5803 if (isR6) {
5804 __ MulR6(TMP, lhs_high, rhs_low);
5805 __ MulR6(dst_high, lhs_low, rhs_high);
5806 __ Addu(dst_high, dst_high, TMP);
5807 __ MuhuR6(TMP, lhs_low, rhs_low);
5808 __ Addu(dst_high, dst_high, TMP);
5809 __ MulR6(dst_low, lhs_low, rhs_low);
5810 } else {
5811 __ MulR2(TMP, lhs_high, rhs_low);
5812 __ MulR2(dst_high, lhs_low, rhs_high);
5813 __ Addu(dst_high, dst_high, TMP);
5814 __ MultuR2(lhs_low, rhs_low);
5815 __ Mfhi(TMP);
5816 __ Addu(dst_high, dst_high, TMP);
5817 __ Mflo(dst_low);
5818 }
5819 break;
5820 }
5821 case Primitive::kPrimFloat:
5822 case Primitive::kPrimDouble: {
5823 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5824 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5825 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5826 if (type == Primitive::kPrimFloat) {
5827 __ MulS(dst, lhs, rhs);
5828 } else {
5829 __ MulD(dst, lhs, rhs);
5830 }
5831 break;
5832 }
5833 default:
5834 LOG(FATAL) << "Unexpected mul type " << type;
5835 }
5836}
5837
5838void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5839 LocationSummary* locations =
5840 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5841 switch (neg->GetResultType()) {
5842 case Primitive::kPrimInt:
5843 case Primitive::kPrimLong:
5844 locations->SetInAt(0, Location::RequiresRegister());
5845 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5846 break;
5847
5848 case Primitive::kPrimFloat:
5849 case Primitive::kPrimDouble:
5850 locations->SetInAt(0, Location::RequiresFpuRegister());
5851 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5852 break;
5853
5854 default:
5855 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5856 }
5857}
5858
5859void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5860 Primitive::Type type = instruction->GetType();
5861 LocationSummary* locations = instruction->GetLocations();
5862
5863 switch (type) {
5864 case Primitive::kPrimInt: {
5865 Register dst = locations->Out().AsRegister<Register>();
5866 Register src = locations->InAt(0).AsRegister<Register>();
5867 __ Subu(dst, ZERO, src);
5868 break;
5869 }
5870 case Primitive::kPrimLong: {
5871 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5872 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5873 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5874 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5875 __ Subu(dst_low, ZERO, src_low);
5876 __ Sltu(TMP, ZERO, dst_low);
5877 __ Subu(dst_high, ZERO, src_high);
5878 __ Subu(dst_high, dst_high, TMP);
5879 break;
5880 }
5881 case Primitive::kPrimFloat:
5882 case Primitive::kPrimDouble: {
5883 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5884 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5885 if (type == Primitive::kPrimFloat) {
5886 __ NegS(dst, src);
5887 } else {
5888 __ NegD(dst, src);
5889 }
5890 break;
5891 }
5892 default:
5893 LOG(FATAL) << "Unexpected neg type " << type;
5894 }
5895}
5896
5897void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5898 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005899 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005900 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005901 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005902 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5903 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005904}
5905
5906void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005907 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
5908 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005909}
5910
5911void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5912 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005913 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005914 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005915 if (instruction->IsStringAlloc()) {
5916 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5917 } else {
5918 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005919 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005920 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5921}
5922
5923void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005924 if (instruction->IsStringAlloc()) {
5925 // String is allocated through StringFactory. Call NewEmptyString entry point.
5926 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005927 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005928 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5929 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5930 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005931 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00005932 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5933 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005934 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005935 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005936 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005937}
5938
5939void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
5940 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5941 locations->SetInAt(0, Location::RequiresRegister());
5942 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5943}
5944
5945void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
5946 Primitive::Type type = instruction->GetType();
5947 LocationSummary* locations = instruction->GetLocations();
5948
5949 switch (type) {
5950 case Primitive::kPrimInt: {
5951 Register dst = locations->Out().AsRegister<Register>();
5952 Register src = locations->InAt(0).AsRegister<Register>();
5953 __ Nor(dst, src, ZERO);
5954 break;
5955 }
5956
5957 case Primitive::kPrimLong: {
5958 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5959 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5960 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5961 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5962 __ Nor(dst_high, src_high, ZERO);
5963 __ Nor(dst_low, src_low, ZERO);
5964 break;
5965 }
5966
5967 default:
5968 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5969 }
5970}
5971
5972void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5973 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5974 locations->SetInAt(0, Location::RequiresRegister());
5975 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5976}
5977
5978void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5979 LocationSummary* locations = instruction->GetLocations();
5980 __ Xori(locations->Out().AsRegister<Register>(),
5981 locations->InAt(0).AsRegister<Register>(),
5982 1);
5983}
5984
5985void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005986 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5987 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005988}
5989
Calin Juravle2ae48182016-03-16 14:05:09 +00005990void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
5991 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005992 return;
5993 }
5994 Location obj = instruction->GetLocations()->InAt(0);
5995
5996 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005997 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005998}
5999
Calin Juravle2ae48182016-03-16 14:05:09 +00006000void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006001 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006002 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006003
6004 Location obj = instruction->GetLocations()->InAt(0);
6005
6006 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6007}
6008
6009void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006010 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006011}
6012
6013void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6014 HandleBinaryOp(instruction);
6015}
6016
6017void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6018 HandleBinaryOp(instruction);
6019}
6020
6021void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6022 LOG(FATAL) << "Unreachable";
6023}
6024
6025void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6026 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6027}
6028
6029void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6030 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6031 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6032 if (location.IsStackSlot()) {
6033 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6034 } else if (location.IsDoubleStackSlot()) {
6035 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6036 }
6037 locations->SetOut(location);
6038}
6039
6040void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6041 ATTRIBUTE_UNUSED) {
6042 // Nothing to do, the parameter is already at its location.
6043}
6044
6045void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6046 LocationSummary* locations =
6047 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6048 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6049}
6050
6051void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6052 ATTRIBUTE_UNUSED) {
6053 // Nothing to do, the method is already at its location.
6054}
6055
6056void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006058 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006059 locations->SetInAt(i, Location::Any());
6060 }
6061 locations->SetOut(Location::Any());
6062}
6063
6064void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6065 LOG(FATAL) << "Unreachable";
6066}
6067
6068void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6069 Primitive::Type type = rem->GetResultType();
6070 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006071 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006072 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6073
6074 switch (type) {
6075 case Primitive::kPrimInt:
6076 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006077 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006078 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6079 break;
6080
6081 case Primitive::kPrimLong: {
6082 InvokeRuntimeCallingConvention calling_convention;
6083 locations->SetInAt(0, Location::RegisterPairLocation(
6084 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6085 locations->SetInAt(1, Location::RegisterPairLocation(
6086 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6087 locations->SetOut(calling_convention.GetReturnLocation(type));
6088 break;
6089 }
6090
6091 case Primitive::kPrimFloat:
6092 case Primitive::kPrimDouble: {
6093 InvokeRuntimeCallingConvention calling_convention;
6094 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6095 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6096 locations->SetOut(calling_convention.GetReturnLocation(type));
6097 break;
6098 }
6099
6100 default:
6101 LOG(FATAL) << "Unexpected rem type " << type;
6102 }
6103}
6104
6105void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6106 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006107
6108 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006109 case Primitive::kPrimInt:
6110 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006111 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006112 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006113 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006114 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6115 break;
6116 }
6117 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006118 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006119 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006120 break;
6121 }
6122 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006123 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006124 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006125 break;
6126 }
6127 default:
6128 LOG(FATAL) << "Unexpected rem type " << type;
6129 }
6130}
6131
6132void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6133 memory_barrier->SetLocations(nullptr);
6134}
6135
6136void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6137 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6138}
6139
6140void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6141 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6142 Primitive::Type return_type = ret->InputAt(0)->GetType();
6143 locations->SetInAt(0, MipsReturnLocation(return_type));
6144}
6145
6146void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6147 codegen_->GenerateFrameExit();
6148}
6149
6150void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6151 ret->SetLocations(nullptr);
6152}
6153
6154void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6155 codegen_->GenerateFrameExit();
6156}
6157
Alexey Frunze92d90602015-12-18 18:16:36 -08006158void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6159 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006160}
6161
Alexey Frunze92d90602015-12-18 18:16:36 -08006162void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6163 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006164}
6165
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006166void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6167 HandleShift(shl);
6168}
6169
6170void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6171 HandleShift(shl);
6172}
6173
6174void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6175 HandleShift(shr);
6176}
6177
6178void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6179 HandleShift(shr);
6180}
6181
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006182void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6183 HandleBinaryOp(instruction);
6184}
6185
6186void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6187 HandleBinaryOp(instruction);
6188}
6189
6190void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6191 HandleFieldGet(instruction, instruction->GetFieldInfo());
6192}
6193
6194void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6195 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6196}
6197
6198void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6199 HandleFieldSet(instruction, instruction->GetFieldInfo());
6200}
6201
6202void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006203 HandleFieldSet(instruction,
6204 instruction->GetFieldInfo(),
6205 instruction->GetDexPc(),
6206 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006207}
6208
6209void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6210 HUnresolvedInstanceFieldGet* instruction) {
6211 FieldAccessCallingConventionMIPS calling_convention;
6212 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6213 instruction->GetFieldType(),
6214 calling_convention);
6215}
6216
6217void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6218 HUnresolvedInstanceFieldGet* instruction) {
6219 FieldAccessCallingConventionMIPS calling_convention;
6220 codegen_->GenerateUnresolvedFieldAccess(instruction,
6221 instruction->GetFieldType(),
6222 instruction->GetFieldIndex(),
6223 instruction->GetDexPc(),
6224 calling_convention);
6225}
6226
6227void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6228 HUnresolvedInstanceFieldSet* instruction) {
6229 FieldAccessCallingConventionMIPS calling_convention;
6230 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6231 instruction->GetFieldType(),
6232 calling_convention);
6233}
6234
6235void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6236 HUnresolvedInstanceFieldSet* instruction) {
6237 FieldAccessCallingConventionMIPS calling_convention;
6238 codegen_->GenerateUnresolvedFieldAccess(instruction,
6239 instruction->GetFieldType(),
6240 instruction->GetFieldIndex(),
6241 instruction->GetDexPc(),
6242 calling_convention);
6243}
6244
6245void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6246 HUnresolvedStaticFieldGet* instruction) {
6247 FieldAccessCallingConventionMIPS calling_convention;
6248 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6249 instruction->GetFieldType(),
6250 calling_convention);
6251}
6252
6253void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6254 HUnresolvedStaticFieldGet* instruction) {
6255 FieldAccessCallingConventionMIPS calling_convention;
6256 codegen_->GenerateUnresolvedFieldAccess(instruction,
6257 instruction->GetFieldType(),
6258 instruction->GetFieldIndex(),
6259 instruction->GetDexPc(),
6260 calling_convention);
6261}
6262
6263void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6264 HUnresolvedStaticFieldSet* instruction) {
6265 FieldAccessCallingConventionMIPS calling_convention;
6266 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6267 instruction->GetFieldType(),
6268 calling_convention);
6269}
6270
6271void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6272 HUnresolvedStaticFieldSet* instruction) {
6273 FieldAccessCallingConventionMIPS calling_convention;
6274 codegen_->GenerateUnresolvedFieldAccess(instruction,
6275 instruction->GetFieldType(),
6276 instruction->GetFieldIndex(),
6277 instruction->GetDexPc(),
6278 calling_convention);
6279}
6280
6281void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006282 LocationSummary* locations =
6283 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006284 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006285}
6286
6287void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6288 HBasicBlock* block = instruction->GetBlock();
6289 if (block->GetLoopInformation() != nullptr) {
6290 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6291 // The back edge will generate the suspend check.
6292 return;
6293 }
6294 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6295 // The goto will generate the suspend check.
6296 return;
6297 }
6298 GenerateSuspendCheck(instruction, nullptr);
6299}
6300
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006301void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6302 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006303 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006304 InvokeRuntimeCallingConvention calling_convention;
6305 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6306}
6307
6308void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006309 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006310 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6311}
6312
6313void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6314 Primitive::Type input_type = conversion->GetInputType();
6315 Primitive::Type result_type = conversion->GetResultType();
6316 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006317 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006318
6319 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6320 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6321 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6322 }
6323
6324 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006325 if (!isR6 &&
6326 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6327 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006328 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006329 }
6330
6331 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6332
6333 if (call_kind == LocationSummary::kNoCall) {
6334 if (Primitive::IsFloatingPointType(input_type)) {
6335 locations->SetInAt(0, Location::RequiresFpuRegister());
6336 } else {
6337 locations->SetInAt(0, Location::RequiresRegister());
6338 }
6339
6340 if (Primitive::IsFloatingPointType(result_type)) {
6341 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6342 } else {
6343 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6344 }
6345 } else {
6346 InvokeRuntimeCallingConvention calling_convention;
6347
6348 if (Primitive::IsFloatingPointType(input_type)) {
6349 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6350 } else {
6351 DCHECK_EQ(input_type, Primitive::kPrimLong);
6352 locations->SetInAt(0, Location::RegisterPairLocation(
6353 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6354 }
6355
6356 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6357 }
6358}
6359
6360void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6361 LocationSummary* locations = conversion->GetLocations();
6362 Primitive::Type result_type = conversion->GetResultType();
6363 Primitive::Type input_type = conversion->GetInputType();
6364 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006365 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006366
6367 DCHECK_NE(input_type, result_type);
6368
6369 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6370 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6371 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6372 Register src = locations->InAt(0).AsRegister<Register>();
6373
Alexey Frunzea871ef12016-06-27 15:20:11 -07006374 if (dst_low != src) {
6375 __ Move(dst_low, src);
6376 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006377 __ Sra(dst_high, src, 31);
6378 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6379 Register dst = locations->Out().AsRegister<Register>();
6380 Register src = (input_type == Primitive::kPrimLong)
6381 ? locations->InAt(0).AsRegisterPairLow<Register>()
6382 : locations->InAt(0).AsRegister<Register>();
6383
6384 switch (result_type) {
6385 case Primitive::kPrimChar:
6386 __ Andi(dst, src, 0xFFFF);
6387 break;
6388 case Primitive::kPrimByte:
6389 if (has_sign_extension) {
6390 __ Seb(dst, src);
6391 } else {
6392 __ Sll(dst, src, 24);
6393 __ Sra(dst, dst, 24);
6394 }
6395 break;
6396 case Primitive::kPrimShort:
6397 if (has_sign_extension) {
6398 __ Seh(dst, src);
6399 } else {
6400 __ Sll(dst, src, 16);
6401 __ Sra(dst, dst, 16);
6402 }
6403 break;
6404 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006405 if (dst != src) {
6406 __ Move(dst, src);
6407 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006408 break;
6409
6410 default:
6411 LOG(FATAL) << "Unexpected type conversion from " << input_type
6412 << " to " << result_type;
6413 }
6414 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006415 if (input_type == Primitive::kPrimLong) {
6416 if (isR6) {
6417 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6418 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6419 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6420 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6421 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6422 __ Mtc1(src_low, FTMP);
6423 __ Mthc1(src_high, FTMP);
6424 if (result_type == Primitive::kPrimFloat) {
6425 __ Cvtsl(dst, FTMP);
6426 } else {
6427 __ Cvtdl(dst, FTMP);
6428 }
6429 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006430 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6431 : kQuickL2d;
6432 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006433 if (result_type == Primitive::kPrimFloat) {
6434 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6435 } else {
6436 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6437 }
6438 }
6439 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006440 Register src = locations->InAt(0).AsRegister<Register>();
6441 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6442 __ Mtc1(src, FTMP);
6443 if (result_type == Primitive::kPrimFloat) {
6444 __ Cvtsw(dst, FTMP);
6445 } else {
6446 __ Cvtdw(dst, FTMP);
6447 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006448 }
6449 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6450 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006451 if (result_type == Primitive::kPrimLong) {
6452 if (isR6) {
6453 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6454 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6455 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6456 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6457 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6458 MipsLabel truncate;
6459 MipsLabel done;
6460
6461 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6462 // value when the input is either a NaN or is outside of the range of the output type
6463 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6464 // the same result.
6465 //
6466 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6467 // value of the output type if the input is outside of the range after the truncation or
6468 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6469 // results. This matches the desired float/double-to-int/long conversion exactly.
6470 //
6471 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6472 //
6473 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6474 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6475 // even though it must be NAN2008=1 on R6.
6476 //
6477 // The code takes care of the different behaviors by first comparing the input to the
6478 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6479 // If the input is greater than or equal to the minimum, it procedes to the truncate
6480 // instruction, which will handle such an input the same way irrespective of NAN2008.
6481 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6482 // in order to return either zero or the minimum value.
6483 //
6484 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6485 // truncate instruction for MIPS64R6.
6486 if (input_type == Primitive::kPrimFloat) {
6487 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6488 __ LoadConst32(TMP, min_val);
6489 __ Mtc1(TMP, FTMP);
6490 __ CmpLeS(FTMP, FTMP, src);
6491 } else {
6492 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6493 __ LoadConst32(TMP, High32Bits(min_val));
6494 __ Mtc1(ZERO, FTMP);
6495 __ Mthc1(TMP, FTMP);
6496 __ CmpLeD(FTMP, FTMP, src);
6497 }
6498
6499 __ Bc1nez(FTMP, &truncate);
6500
6501 if (input_type == Primitive::kPrimFloat) {
6502 __ CmpEqS(FTMP, src, src);
6503 } else {
6504 __ CmpEqD(FTMP, src, src);
6505 }
6506 __ Move(dst_low, ZERO);
6507 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6508 __ Mfc1(TMP, FTMP);
6509 __ And(dst_high, dst_high, TMP);
6510
6511 __ B(&done);
6512
6513 __ Bind(&truncate);
6514
6515 if (input_type == Primitive::kPrimFloat) {
6516 __ TruncLS(FTMP, src);
6517 } else {
6518 __ TruncLD(FTMP, src);
6519 }
6520 __ Mfc1(dst_low, FTMP);
6521 __ Mfhc1(dst_high, FTMP);
6522
6523 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006524 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006525 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6526 : kQuickD2l;
6527 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006528 if (input_type == Primitive::kPrimFloat) {
6529 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6530 } else {
6531 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6532 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006533 }
6534 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006535 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6536 Register dst = locations->Out().AsRegister<Register>();
6537 MipsLabel truncate;
6538 MipsLabel done;
6539
6540 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6541 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6542 // even though it must be NAN2008=1 on R6.
6543 //
6544 // For details see the large comment above for the truncation of float/double to long on R6.
6545 //
6546 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6547 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006548 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006549 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6550 __ LoadConst32(TMP, min_val);
6551 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006552 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006553 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6554 __ LoadConst32(TMP, High32Bits(min_val));
6555 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006556 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006557 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006558
6559 if (isR6) {
6560 if (input_type == Primitive::kPrimFloat) {
6561 __ CmpLeS(FTMP, FTMP, src);
6562 } else {
6563 __ CmpLeD(FTMP, FTMP, src);
6564 }
6565 __ Bc1nez(FTMP, &truncate);
6566
6567 if (input_type == Primitive::kPrimFloat) {
6568 __ CmpEqS(FTMP, src, src);
6569 } else {
6570 __ CmpEqD(FTMP, src, src);
6571 }
6572 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6573 __ Mfc1(TMP, FTMP);
6574 __ And(dst, dst, TMP);
6575 } else {
6576 if (input_type == Primitive::kPrimFloat) {
6577 __ ColeS(0, FTMP, src);
6578 } else {
6579 __ ColeD(0, FTMP, src);
6580 }
6581 __ Bc1t(0, &truncate);
6582
6583 if (input_type == Primitive::kPrimFloat) {
6584 __ CeqS(0, src, src);
6585 } else {
6586 __ CeqD(0, src, src);
6587 }
6588 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6589 __ Movf(dst, ZERO, 0);
6590 }
6591
6592 __ B(&done);
6593
6594 __ Bind(&truncate);
6595
6596 if (input_type == Primitive::kPrimFloat) {
6597 __ TruncWS(FTMP, src);
6598 } else {
6599 __ TruncWD(FTMP, src);
6600 }
6601 __ Mfc1(dst, FTMP);
6602
6603 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006604 }
6605 } else if (Primitive::IsFloatingPointType(result_type) &&
6606 Primitive::IsFloatingPointType(input_type)) {
6607 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6608 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6609 if (result_type == Primitive::kPrimFloat) {
6610 __ Cvtsd(dst, src);
6611 } else {
6612 __ Cvtds(dst, src);
6613 }
6614 } else {
6615 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6616 << " to " << result_type;
6617 }
6618}
6619
6620void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6621 HandleShift(ushr);
6622}
6623
6624void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6625 HandleShift(ushr);
6626}
6627
6628void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6629 HandleBinaryOp(instruction);
6630}
6631
6632void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6633 HandleBinaryOp(instruction);
6634}
6635
6636void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6637 // Nothing to do, this should be removed during prepare for register allocator.
6638 LOG(FATAL) << "Unreachable";
6639}
6640
6641void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6642 // Nothing to do, this should be removed during prepare for register allocator.
6643 LOG(FATAL) << "Unreachable";
6644}
6645
6646void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006647 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006648}
6649
6650void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006651 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006652}
6653
6654void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006655 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006656}
6657
6658void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006659 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006660}
6661
6662void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006663 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006664}
6665
6666void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006667 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006668}
6669
6670void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006671 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006672}
6673
6674void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006675 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006676}
6677
6678void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006679 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006680}
6681
6682void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006683 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006684}
6685
6686void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006687 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006688}
6689
6690void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006691 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006692}
6693
6694void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006695 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006696}
6697
6698void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006699 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006700}
6701
6702void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006703 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006704}
6705
6706void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006707 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006708}
6709
6710void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006711 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006712}
6713
6714void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006715 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006716}
6717
6718void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006719 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006720}
6721
6722void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006723 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006724}
6725
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006726void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6727 LocationSummary* locations =
6728 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6729 locations->SetInAt(0, Location::RequiresRegister());
6730}
6731
Alexey Frunze96b66822016-09-10 02:32:44 -07006732void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6733 int32_t lower_bound,
6734 uint32_t num_entries,
6735 HBasicBlock* switch_block,
6736 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006737 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006738 Register temp_reg = TMP;
6739 __ Addiu32(temp_reg, value_reg, -lower_bound);
6740 // Jump to default if index is negative
6741 // Note: We don't check the case that index is positive while value < lower_bound, because in
6742 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6743 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6744
Alexey Frunze96b66822016-09-10 02:32:44 -07006745 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006746 // Jump to successors[0] if value == lower_bound.
6747 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6748 int32_t last_index = 0;
6749 for (; num_entries - last_index > 2; last_index += 2) {
6750 __ Addiu(temp_reg, temp_reg, -2);
6751 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6752 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6753 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6754 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6755 }
6756 if (num_entries - last_index == 2) {
6757 // The last missing case_value.
6758 __ Addiu(temp_reg, temp_reg, -1);
6759 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006760 }
6761
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006762 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006763 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006764 __ B(codegen_->GetLabelOf(default_block));
6765 }
6766}
6767
Alexey Frunze96b66822016-09-10 02:32:44 -07006768void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6769 Register constant_area,
6770 int32_t lower_bound,
6771 uint32_t num_entries,
6772 HBasicBlock* switch_block,
6773 HBasicBlock* default_block) {
6774 // Create a jump table.
6775 std::vector<MipsLabel*> labels(num_entries);
6776 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6777 for (uint32_t i = 0; i < num_entries; i++) {
6778 labels[i] = codegen_->GetLabelOf(successors[i]);
6779 }
6780 JumpTable* table = __ CreateJumpTable(std::move(labels));
6781
6782 // Is the value in range?
6783 __ Addiu32(TMP, value_reg, -lower_bound);
6784 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6785 __ Sltiu(AT, TMP, num_entries);
6786 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6787 } else {
6788 __ LoadConst32(AT, num_entries);
6789 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6790 }
6791
6792 // We are in the range of the table.
6793 // Load the target address from the jump table, indexing by the value.
6794 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6795 __ Sll(TMP, TMP, 2);
6796 __ Addu(TMP, TMP, AT);
6797 __ Lw(TMP, TMP, 0);
6798 // Compute the absolute target address by adding the table start address
6799 // (the table contains offsets to targets relative to its start).
6800 __ Addu(TMP, TMP, AT);
6801 // And jump.
6802 __ Jr(TMP);
6803 __ NopIfNoReordering();
6804}
6805
6806void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6807 int32_t lower_bound = switch_instr->GetStartValue();
6808 uint32_t num_entries = switch_instr->GetNumEntries();
6809 LocationSummary* locations = switch_instr->GetLocations();
6810 Register value_reg = locations->InAt(0).AsRegister<Register>();
6811 HBasicBlock* switch_block = switch_instr->GetBlock();
6812 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6813
6814 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6815 num_entries > kPackedSwitchJumpTableThreshold) {
6816 // R6 uses PC-relative addressing to access the jump table.
6817 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6818 // the jump table and it is implemented by changing HPackedSwitch to
6819 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6820 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6821 GenTableBasedPackedSwitch(value_reg,
6822 ZERO,
6823 lower_bound,
6824 num_entries,
6825 switch_block,
6826 default_block);
6827 } else {
6828 GenPackedSwitchWithCompares(value_reg,
6829 lower_bound,
6830 num_entries,
6831 switch_block,
6832 default_block);
6833 }
6834}
6835
6836void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6837 LocationSummary* locations =
6838 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6839 locations->SetInAt(0, Location::RequiresRegister());
6840 // Constant area pointer (HMipsComputeBaseMethodAddress).
6841 locations->SetInAt(1, Location::RequiresRegister());
6842}
6843
6844void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6845 int32_t lower_bound = switch_instr->GetStartValue();
6846 uint32_t num_entries = switch_instr->GetNumEntries();
6847 LocationSummary* locations = switch_instr->GetLocations();
6848 Register value_reg = locations->InAt(0).AsRegister<Register>();
6849 Register constant_area = locations->InAt(1).AsRegister<Register>();
6850 HBasicBlock* switch_block = switch_instr->GetBlock();
6851 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6852
6853 // This is an R2-only path. HPackedSwitch has been changed to
6854 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6855 // required to address the jump table relative to PC.
6856 GenTableBasedPackedSwitch(value_reg,
6857 constant_area,
6858 lower_bound,
6859 num_entries,
6860 switch_block,
6861 default_block);
6862}
6863
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006864void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6865 HMipsComputeBaseMethodAddress* insn) {
6866 LocationSummary* locations =
6867 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6868 locations->SetOut(Location::RequiresRegister());
6869}
6870
6871void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6872 HMipsComputeBaseMethodAddress* insn) {
6873 LocationSummary* locations = insn->GetLocations();
6874 Register reg = locations->Out().AsRegister<Register>();
6875
6876 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6877
6878 // Generate a dummy PC-relative call to obtain PC.
6879 __ Nal();
6880 // Grab the return address off RA.
6881 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006882 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006883
6884 // Remember this offset (the obtained PC value) for later use with constant area.
6885 __ BindPcRelBaseLabel();
6886}
6887
6888void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6889 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6890 locations->SetOut(Location::RequiresRegister());
6891}
6892
6893void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6894 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6895 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6896 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006897 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6898 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006899}
6900
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006901void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6902 // The trampoline uses the same calling convention as dex calling conventions,
6903 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6904 // the method_idx.
6905 HandleInvoke(invoke);
6906}
6907
6908void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6909 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6910}
6911
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006912void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6913 LocationSummary* locations =
6914 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6915 locations->SetInAt(0, Location::RequiresRegister());
6916 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006917}
6918
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006919void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6920 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006921 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006922 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006923 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006924 __ LoadFromOffset(kLoadWord,
6925 locations->Out().AsRegister<Register>(),
6926 locations->InAt(0).AsRegister<Register>(),
6927 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006928 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006929 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006930 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006931 __ LoadFromOffset(kLoadWord,
6932 locations->Out().AsRegister<Register>(),
6933 locations->InAt(0).AsRegister<Register>(),
6934 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006935 __ LoadFromOffset(kLoadWord,
6936 locations->Out().AsRegister<Register>(),
6937 locations->Out().AsRegister<Register>(),
6938 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006939 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006940}
6941
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006942#undef __
6943#undef QUICK_ENTRY_POINT
6944
6945} // namespace mips
6946} // namespace art