blob: 725e02bd9f160c489ac5c5f5de1ae79457096a00 [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 =
260 mips_codegen->NewPcRelativeTypePatch(cls_->GetDexFile(), type_index);
261 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)),
480 boot_image_address_patches_(std::less<uint32_t>(),
481 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
482 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200483 // Save RA (containing the return address) to mimic Quick.
484 AddAllocatedRegister(Location::RegisterLocation(RA));
485}
486
487#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100488// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
489#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700490#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200491
492void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
493 // Ensure that we fix up branches.
494 __ FinalizeCode();
495
496 // Adjust native pc offsets in stack maps.
497 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
498 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
499 uint32_t new_position = __ GetAdjustedPosition(old_position);
500 DCHECK_GE(new_position, old_position);
501 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
502 }
503
504 // Adjust pc offsets for the disassembly information.
505 if (disasm_info_ != nullptr) {
506 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
507 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
508 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
509 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
510 it.second.start = __ GetAdjustedPosition(it.second.start);
511 it.second.end = __ GetAdjustedPosition(it.second.end);
512 }
513 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
514 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
515 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
516 }
517 }
518
519 CodeGenerator::Finalize(allocator);
520}
521
522MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
523 return codegen_->GetAssembler();
524}
525
526void ParallelMoveResolverMIPS::EmitMove(size_t index) {
527 DCHECK_LT(index, moves_.size());
528 MoveOperands* move = moves_[index];
529 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
530}
531
532void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
533 DCHECK_LT(index, moves_.size());
534 MoveOperands* move = moves_[index];
535 Primitive::Type type = move->GetType();
536 Location loc1 = move->GetDestination();
537 Location loc2 = move->GetSource();
538
539 DCHECK(!loc1.IsConstant());
540 DCHECK(!loc2.IsConstant());
541
542 if (loc1.Equals(loc2)) {
543 return;
544 }
545
546 if (loc1.IsRegister() && loc2.IsRegister()) {
547 // Swap 2 GPRs.
548 Register r1 = loc1.AsRegister<Register>();
549 Register r2 = loc2.AsRegister<Register>();
550 __ Move(TMP, r2);
551 __ Move(r2, r1);
552 __ Move(r1, TMP);
553 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
554 FRegister f1 = loc1.AsFpuRegister<FRegister>();
555 FRegister f2 = loc2.AsFpuRegister<FRegister>();
556 if (type == Primitive::kPrimFloat) {
557 __ MovS(FTMP, f2);
558 __ MovS(f2, f1);
559 __ MovS(f1, FTMP);
560 } else {
561 DCHECK_EQ(type, Primitive::kPrimDouble);
562 __ MovD(FTMP, f2);
563 __ MovD(f2, f1);
564 __ MovD(f1, FTMP);
565 }
566 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
567 (loc1.IsFpuRegister() && loc2.IsRegister())) {
568 // Swap FPR and GPR.
569 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
570 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
571 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200572 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200573 __ Move(TMP, r2);
574 __ Mfc1(r2, f1);
575 __ Mtc1(TMP, f1);
576 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
577 // Swap 2 GPR register pairs.
578 Register r1 = loc1.AsRegisterPairLow<Register>();
579 Register r2 = loc2.AsRegisterPairLow<Register>();
580 __ Move(TMP, r2);
581 __ Move(r2, r1);
582 __ Move(r1, TMP);
583 r1 = loc1.AsRegisterPairHigh<Register>();
584 r2 = loc2.AsRegisterPairHigh<Register>();
585 __ Move(TMP, r2);
586 __ Move(r2, r1);
587 __ Move(r1, TMP);
588 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
589 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
590 // Swap FPR and GPR register pair.
591 DCHECK_EQ(type, Primitive::kPrimDouble);
592 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
593 : loc2.AsFpuRegister<FRegister>();
594 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
595 : loc2.AsRegisterPairLow<Register>();
596 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
597 : loc2.AsRegisterPairHigh<Register>();
598 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
599 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
600 // unpredictable and the following mfch1 will fail.
601 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800602 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200603 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800604 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200605 __ Move(r2_l, TMP);
606 __ Move(r2_h, AT);
607 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
608 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
609 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
610 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000611 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
612 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200613 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
614 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000615 __ Move(TMP, reg);
616 __ LoadFromOffset(kLoadWord, reg, SP, offset);
617 __ StoreToOffset(kStoreWord, TMP, SP, offset);
618 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
619 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
620 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
621 : loc2.AsRegisterPairLow<Register>();
622 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
623 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200624 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000625 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
626 : loc2.GetHighStackIndex(kMipsWordSize);
627 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000628 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000629 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000630 __ Move(TMP, reg_h);
631 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
632 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200633 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
634 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
635 : loc2.AsFpuRegister<FRegister>();
636 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
637 if (type == Primitive::kPrimFloat) {
638 __ MovS(FTMP, reg);
639 __ LoadSFromOffset(reg, SP, offset);
640 __ StoreSToOffset(FTMP, SP, offset);
641 } else {
642 DCHECK_EQ(type, Primitive::kPrimDouble);
643 __ MovD(FTMP, reg);
644 __ LoadDFromOffset(reg, SP, offset);
645 __ StoreDToOffset(FTMP, SP, offset);
646 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200647 } else {
648 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
649 }
650}
651
652void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
653 __ Pop(static_cast<Register>(reg));
654}
655
656void ParallelMoveResolverMIPS::SpillScratch(int reg) {
657 __ Push(static_cast<Register>(reg));
658}
659
660void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
661 // Allocate a scratch register other than TMP, if available.
662 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
663 // automatically unspilled when the scratch scope object is destroyed).
664 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
665 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
666 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
667 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
668 __ LoadFromOffset(kLoadWord,
669 Register(ensure_scratch.GetRegister()),
670 SP,
671 index1 + stack_offset);
672 __ LoadFromOffset(kLoadWord,
673 TMP,
674 SP,
675 index2 + stack_offset);
676 __ StoreToOffset(kStoreWord,
677 Register(ensure_scratch.GetRegister()),
678 SP,
679 index2 + stack_offset);
680 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
681 }
682}
683
Alexey Frunze73296a72016-06-03 22:51:46 -0700684void CodeGeneratorMIPS::ComputeSpillMask() {
685 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
686 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
687 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
688 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
689 // registers, include the ZERO register to force alignment of FPU callee-saved registers
690 // within the stack frame.
691 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
692 core_spill_mask_ |= (1 << ZERO);
693 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700694}
695
696bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700697 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700698 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
699 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
700 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700701 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
702 // saved in an unused temporary register) and saving of RA and the current method pointer
703 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700704 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700705}
706
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200707static dwarf::Reg DWARFReg(Register reg) {
708 return dwarf::Reg::MipsCore(static_cast<int>(reg));
709}
710
711// TODO: mapping of floating-point registers to DWARF.
712
713void CodeGeneratorMIPS::GenerateFrameEntry() {
714 __ Bind(&frame_entry_label_);
715
716 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
717
718 if (do_overflow_check) {
719 __ LoadFromOffset(kLoadWord,
720 ZERO,
721 SP,
722 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
723 RecordPcInfo(nullptr, 0);
724 }
725
726 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700727 CHECK_EQ(fpu_spill_mask_, 0u);
728 CHECK_EQ(core_spill_mask_, 1u << RA);
729 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200730 return;
731 }
732
733 // Make sure the frame size isn't unreasonably large.
734 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
735 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
736 }
737
738 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200739
Alexey Frunze73296a72016-06-03 22:51:46 -0700740 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200741 __ IncreaseFrameSize(ofs);
742
Alexey Frunze73296a72016-06-03 22:51:46 -0700743 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
744 Register reg = static_cast<Register>(MostSignificantBit(mask));
745 mask ^= 1u << reg;
746 ofs -= kMipsWordSize;
747 // The ZERO register is only included for alignment.
748 if (reg != ZERO) {
749 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200750 __ cfi().RelOffset(DWARFReg(reg), ofs);
751 }
752 }
753
Alexey Frunze73296a72016-06-03 22:51:46 -0700754 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
755 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
756 mask ^= 1u << reg;
757 ofs -= kMipsDoublewordSize;
758 __ StoreDToOffset(reg, SP, ofs);
759 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200760 }
761
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100762 // Save the current method if we need it. Note that we do not
763 // do this in HCurrentMethod, as the instruction might have been removed
764 // in the SSA graph.
765 if (RequiresCurrentMethod()) {
766 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
767 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +0100768
769 if (GetGraph()->HasShouldDeoptimizeFlag()) {
770 // Initialize should deoptimize flag to 0.
771 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
772 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200773}
774
775void CodeGeneratorMIPS::GenerateFrameExit() {
776 __ cfi().RememberState();
777
778 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200779 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200780
Alexey Frunze73296a72016-06-03 22:51:46 -0700781 // For better instruction scheduling restore RA before other registers.
782 uint32_t ofs = GetFrameSize();
783 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
784 Register reg = static_cast<Register>(MostSignificantBit(mask));
785 mask ^= 1u << reg;
786 ofs -= kMipsWordSize;
787 // The ZERO register is only included for alignment.
788 if (reg != ZERO) {
789 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200790 __ cfi().Restore(DWARFReg(reg));
791 }
792 }
793
Alexey Frunze73296a72016-06-03 22:51:46 -0700794 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
795 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
796 mask ^= 1u << reg;
797 ofs -= kMipsDoublewordSize;
798 __ LoadDFromOffset(reg, SP, ofs);
799 // TODO: __ cfi().Restore(DWARFReg(reg));
800 }
801
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700802 size_t frame_size = GetFrameSize();
803 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
804 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
805 bool reordering = __ SetReorder(false);
806 if (exchange) {
807 __ Jr(RA);
808 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
809 } else {
810 __ DecreaseFrameSize(frame_size);
811 __ Jr(RA);
812 __ Nop(); // In delay slot.
813 }
814 __ SetReorder(reordering);
815 } else {
816 __ Jr(RA);
817 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200818 }
819
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200820 __ cfi().RestoreState();
821 __ cfi().DefCFAOffset(GetFrameSize());
822}
823
824void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
825 __ Bind(GetLabelOf(block));
826}
827
828void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
829 if (src.Equals(dst)) {
830 return;
831 }
832
833 if (src.IsConstant()) {
834 MoveConstant(dst, src.GetConstant());
835 } else {
836 if (Primitive::Is64BitType(dst_type)) {
837 Move64(dst, src);
838 } else {
839 Move32(dst, src);
840 }
841 }
842}
843
844void CodeGeneratorMIPS::Move32(Location destination, Location source) {
845 if (source.Equals(destination)) {
846 return;
847 }
848
849 if (destination.IsRegister()) {
850 if (source.IsRegister()) {
851 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
852 } else if (source.IsFpuRegister()) {
853 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
854 } else {
855 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
856 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
857 }
858 } else if (destination.IsFpuRegister()) {
859 if (source.IsRegister()) {
860 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
861 } else if (source.IsFpuRegister()) {
862 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
863 } else {
864 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
865 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
866 }
867 } else {
868 DCHECK(destination.IsStackSlot()) << destination;
869 if (source.IsRegister()) {
870 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
871 } else if (source.IsFpuRegister()) {
872 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
873 } else {
874 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
875 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
876 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
877 }
878 }
879}
880
881void CodeGeneratorMIPS::Move64(Location destination, Location source) {
882 if (source.Equals(destination)) {
883 return;
884 }
885
886 if (destination.IsRegisterPair()) {
887 if (source.IsRegisterPair()) {
888 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
889 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
890 } else if (source.IsFpuRegister()) {
891 Register dst_high = destination.AsRegisterPairHigh<Register>();
892 Register dst_low = destination.AsRegisterPairLow<Register>();
893 FRegister src = source.AsFpuRegister<FRegister>();
894 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800895 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200896 } else {
897 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
898 int32_t off = source.GetStackIndex();
899 Register r = destination.AsRegisterPairLow<Register>();
900 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
901 }
902 } else if (destination.IsFpuRegister()) {
903 if (source.IsRegisterPair()) {
904 FRegister dst = destination.AsFpuRegister<FRegister>();
905 Register src_high = source.AsRegisterPairHigh<Register>();
906 Register src_low = source.AsRegisterPairLow<Register>();
907 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800908 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200909 } else if (source.IsFpuRegister()) {
910 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
911 } else {
912 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
913 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
914 }
915 } else {
916 DCHECK(destination.IsDoubleStackSlot()) << destination;
917 int32_t off = destination.GetStackIndex();
918 if (source.IsRegisterPair()) {
919 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
920 } else if (source.IsFpuRegister()) {
921 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
922 } else {
923 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
924 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
925 __ StoreToOffset(kStoreWord, TMP, SP, off);
926 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
927 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
928 }
929 }
930}
931
932void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
933 if (c->IsIntConstant() || c->IsNullConstant()) {
934 // Move 32 bit constant.
935 int32_t value = GetInt32ValueOf(c);
936 if (destination.IsRegister()) {
937 Register dst = destination.AsRegister<Register>();
938 __ LoadConst32(dst, value);
939 } else {
940 DCHECK(destination.IsStackSlot())
941 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700942 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200943 }
944 } else if (c->IsLongConstant()) {
945 // Move 64 bit constant.
946 int64_t value = GetInt64ValueOf(c);
947 if (destination.IsRegisterPair()) {
948 Register r_h = destination.AsRegisterPairHigh<Register>();
949 Register r_l = destination.AsRegisterPairLow<Register>();
950 __ LoadConst64(r_h, r_l, value);
951 } else {
952 DCHECK(destination.IsDoubleStackSlot())
953 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700954 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200955 }
956 } else if (c->IsFloatConstant()) {
957 // Move 32 bit float constant.
958 int32_t value = GetInt32ValueOf(c);
959 if (destination.IsFpuRegister()) {
960 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
961 } else {
962 DCHECK(destination.IsStackSlot())
963 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700964 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200965 }
966 } else {
967 // Move 64 bit double constant.
968 DCHECK(c->IsDoubleConstant()) << c->DebugName();
969 int64_t value = GetInt64ValueOf(c);
970 if (destination.IsFpuRegister()) {
971 FRegister fd = destination.AsFpuRegister<FRegister>();
972 __ LoadDConst64(fd, value, TMP);
973 } else {
974 DCHECK(destination.IsDoubleStackSlot())
975 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700976 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200977 }
978 }
979}
980
981void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
982 DCHECK(destination.IsRegister());
983 Register dst = destination.AsRegister<Register>();
984 __ LoadConst32(dst, value);
985}
986
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200987void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
988 if (location.IsRegister()) {
989 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700990 } else if (location.IsRegisterPair()) {
991 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
992 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200993 } else {
994 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
995 }
996}
997
Vladimir Markoaad75c62016-10-03 08:46:48 +0000998template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
999inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1000 const ArenaDeque<PcRelativePatchInfo>& infos,
1001 ArenaVector<LinkerPatch>* linker_patches) {
1002 for (const PcRelativePatchInfo& info : infos) {
1003 const DexFile& dex_file = info.target_dex_file;
1004 size_t offset_or_index = info.offset_or_index;
1005 DCHECK(info.high_label.IsBound());
1006 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1007 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1008 // the assembler's base label used for PC-relative addressing.
1009 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1010 ? __ GetLabelLocation(&info.pc_rel_label)
1011 : __ GetPcRelBaseLabelLocation();
1012 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1013 }
1014}
1015
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001016void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1017 DCHECK(linker_patches->empty());
1018 size_t size =
Alexey Frunze06a46c42016-07-19 15:00:40 -07001019 pc_relative_dex_cache_patches_.size() +
1020 pc_relative_string_patches_.size() +
1021 pc_relative_type_patches_.size() +
1022 boot_image_string_patches_.size() +
1023 boot_image_type_patches_.size() +
1024 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001025 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001026 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1027 linker_patches);
1028 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001029 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(pc_relative_type_patches_,
1030 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001031 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1032 linker_patches);
1033 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001034 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1035 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001036 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1037 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001038 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07001039 for (const auto& entry : boot_image_string_patches_) {
1040 const StringReference& target_string = entry.first;
1041 Literal* literal = entry.second;
1042 DCHECK(literal->GetLabel()->IsBound());
1043 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1044 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1045 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001046 target_string.string_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001047 }
1048 for (const auto& entry : boot_image_type_patches_) {
1049 const TypeReference& target_type = entry.first;
1050 Literal* literal = entry.second;
1051 DCHECK(literal->GetLabel()->IsBound());
1052 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1053 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1054 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001055 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001056 }
1057 for (const auto& entry : boot_image_address_patches_) {
1058 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1059 Literal* literal = entry.second;
1060 DCHECK(literal->GetLabel()->IsBound());
1061 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1062 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1063 }
1064}
1065
1066CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001067 const DexFile& dex_file, dex::StringIndex string_index) {
1068 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001069}
1070
1071CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001072 const DexFile& dex_file, dex::TypeIndex type_index) {
1073 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001074}
1075
1076CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1077 const DexFile& dex_file, uint32_t element_offset) {
1078 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1079}
1080
1081CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1082 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1083 patches->emplace_back(dex_file, offset_or_index);
1084 return &patches->back();
1085}
1086
Alexey Frunze06a46c42016-07-19 15:00:40 -07001087Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1088 return map->GetOrCreate(
1089 value,
1090 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1091}
1092
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001093Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1094 MethodToLiteralMap* map) {
1095 return map->GetOrCreate(
1096 target_method,
1097 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1098}
1099
Alexey Frunze06a46c42016-07-19 15:00:40 -07001100Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001101 dex::StringIndex string_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001102 return boot_image_string_patches_.GetOrCreate(
1103 StringReference(&dex_file, string_index),
1104 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1105}
1106
1107Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001108 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001109 return boot_image_type_patches_.GetOrCreate(
1110 TypeReference(&dex_file, type_index),
1111 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1112}
1113
1114Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1115 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1116 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1117 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1118}
1119
Vladimir Markoaad75c62016-10-03 08:46:48 +00001120void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholder(
1121 PcRelativePatchInfo* info, Register out, Register base) {
1122 bool reordering = __ SetReorder(false);
1123 if (GetInstructionSetFeatures().IsR6()) {
1124 DCHECK_EQ(base, ZERO);
1125 __ Bind(&info->high_label);
1126 __ Bind(&info->pc_rel_label);
1127 // Add a 32-bit offset to PC.
1128 __ Auipc(out, /* placeholder */ 0x1234);
1129 __ Addiu(out, out, /* placeholder */ 0x5678);
1130 } else {
1131 // If base is ZERO, emit NAL to obtain the actual base.
1132 if (base == ZERO) {
1133 // Generate a dummy PC-relative call to obtain PC.
1134 __ Nal();
1135 }
1136 __ Bind(&info->high_label);
1137 __ Lui(out, /* placeholder */ 0x1234);
1138 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1139 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1140 if (base == ZERO) {
1141 __ Bind(&info->pc_rel_label);
1142 }
1143 __ Ori(out, out, /* placeholder */ 0x5678);
1144 // Add a 32-bit offset to PC.
1145 __ Addu(out, out, (base == ZERO) ? RA : base);
1146 }
1147 __ SetReorder(reordering);
1148}
1149
Goran Jakovljevice114da22016-12-26 14:21:43 +01001150void CodeGeneratorMIPS::MarkGCCard(Register object,
1151 Register value,
1152 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001153 MipsLabel done;
1154 Register card = AT;
1155 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001156 if (value_can_be_null) {
1157 __ Beqz(value, &done);
1158 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001159 __ LoadFromOffset(kLoadWord,
1160 card,
1161 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001162 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001163 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1164 __ Addu(temp, card, temp);
1165 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001166 if (value_can_be_null) {
1167 __ Bind(&done);
1168 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001169}
1170
David Brazdil58282f42016-01-14 12:45:10 +00001171void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001172 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1173 blocked_core_registers_[ZERO] = true;
1174 blocked_core_registers_[K0] = true;
1175 blocked_core_registers_[K1] = true;
1176 blocked_core_registers_[GP] = true;
1177 blocked_core_registers_[SP] = true;
1178 blocked_core_registers_[RA] = true;
1179
1180 // AT and TMP(T8) are used as temporary/scratch registers
1181 // (similar to how AT is used by MIPS assemblers).
1182 blocked_core_registers_[AT] = true;
1183 blocked_core_registers_[TMP] = true;
1184 blocked_fpu_registers_[FTMP] = true;
1185
1186 // Reserve suspend and thread registers.
1187 blocked_core_registers_[S0] = true;
1188 blocked_core_registers_[TR] = true;
1189
1190 // Reserve T9 for function calls
1191 blocked_core_registers_[T9] = true;
1192
1193 // Reserve odd-numbered FPU registers.
1194 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1195 blocked_fpu_registers_[i] = true;
1196 }
1197
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001198 if (GetGraph()->IsDebuggable()) {
1199 // Stubs do not save callee-save floating point registers. If the graph
1200 // is debuggable, we need to deal with these registers differently. For
1201 // now, just block them.
1202 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1203 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1204 }
1205 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001206}
1207
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001208size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1209 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1210 return kMipsWordSize;
1211}
1212
1213size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1214 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1215 return kMipsWordSize;
1216}
1217
1218size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1219 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1220 return kMipsDoublewordSize;
1221}
1222
1223size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1224 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1225 return kMipsDoublewordSize;
1226}
1227
1228void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001229 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001230}
1231
1232void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001233 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001234}
1235
Serban Constantinescufca16662016-07-14 09:21:59 +01001236constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1237
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001238void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1239 HInstruction* instruction,
1240 uint32_t dex_pc,
1241 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001242 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001243 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001244 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001245 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001246 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001247 // Reserve argument space on stack (for $a0-$a3) for
1248 // entrypoints that directly reference native implementations.
1249 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001250 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001251 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001252 } else {
1253 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001254 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001255 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001256 if (EntrypointRequiresStackMap(entrypoint)) {
1257 RecordPcInfo(instruction, dex_pc, slow_path);
1258 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001259}
1260
1261void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1262 Register class_reg) {
1263 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1264 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1265 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1266 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1267 __ Sync(0);
1268 __ Bind(slow_path->GetExitLabel());
1269}
1270
1271void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1272 __ Sync(0); // Only stype 0 is supported.
1273}
1274
1275void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1276 HBasicBlock* successor) {
1277 SuspendCheckSlowPathMIPS* slow_path =
1278 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1279 codegen_->AddSlowPath(slow_path);
1280
1281 __ LoadFromOffset(kLoadUnsignedHalfword,
1282 TMP,
1283 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001284 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001285 if (successor == nullptr) {
1286 __ Bnez(TMP, slow_path->GetEntryLabel());
1287 __ Bind(slow_path->GetReturnLabel());
1288 } else {
1289 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1290 __ B(slow_path->GetEntryLabel());
1291 // slow_path will return to GetLabelOf(successor).
1292 }
1293}
1294
1295InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1296 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001297 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001298 assembler_(codegen->GetAssembler()),
1299 codegen_(codegen) {}
1300
1301void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1302 DCHECK_EQ(instruction->InputCount(), 2U);
1303 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1304 Primitive::Type type = instruction->GetResultType();
1305 switch (type) {
1306 case Primitive::kPrimInt: {
1307 locations->SetInAt(0, Location::RequiresRegister());
1308 HInstruction* right = instruction->InputAt(1);
1309 bool can_use_imm = false;
1310 if (right->IsConstant()) {
1311 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1312 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1313 can_use_imm = IsUint<16>(imm);
1314 } else if (instruction->IsAdd()) {
1315 can_use_imm = IsInt<16>(imm);
1316 } else {
1317 DCHECK(instruction->IsSub());
1318 can_use_imm = IsInt<16>(-imm);
1319 }
1320 }
1321 if (can_use_imm)
1322 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1323 else
1324 locations->SetInAt(1, Location::RequiresRegister());
1325 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1326 break;
1327 }
1328
1329 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001330 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001331 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1332 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001333 break;
1334 }
1335
1336 case Primitive::kPrimFloat:
1337 case Primitive::kPrimDouble:
1338 DCHECK(instruction->IsAdd() || instruction->IsSub());
1339 locations->SetInAt(0, Location::RequiresFpuRegister());
1340 locations->SetInAt(1, Location::RequiresFpuRegister());
1341 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1342 break;
1343
1344 default:
1345 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1346 }
1347}
1348
1349void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1350 Primitive::Type type = instruction->GetType();
1351 LocationSummary* locations = instruction->GetLocations();
1352
1353 switch (type) {
1354 case Primitive::kPrimInt: {
1355 Register dst = locations->Out().AsRegister<Register>();
1356 Register lhs = locations->InAt(0).AsRegister<Register>();
1357 Location rhs_location = locations->InAt(1);
1358
1359 Register rhs_reg = ZERO;
1360 int32_t rhs_imm = 0;
1361 bool use_imm = rhs_location.IsConstant();
1362 if (use_imm) {
1363 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1364 } else {
1365 rhs_reg = rhs_location.AsRegister<Register>();
1366 }
1367
1368 if (instruction->IsAnd()) {
1369 if (use_imm)
1370 __ Andi(dst, lhs, rhs_imm);
1371 else
1372 __ And(dst, lhs, rhs_reg);
1373 } else if (instruction->IsOr()) {
1374 if (use_imm)
1375 __ Ori(dst, lhs, rhs_imm);
1376 else
1377 __ Or(dst, lhs, rhs_reg);
1378 } else if (instruction->IsXor()) {
1379 if (use_imm)
1380 __ Xori(dst, lhs, rhs_imm);
1381 else
1382 __ Xor(dst, lhs, rhs_reg);
1383 } else if (instruction->IsAdd()) {
1384 if (use_imm)
1385 __ Addiu(dst, lhs, rhs_imm);
1386 else
1387 __ Addu(dst, lhs, rhs_reg);
1388 } else {
1389 DCHECK(instruction->IsSub());
1390 if (use_imm)
1391 __ Addiu(dst, lhs, -rhs_imm);
1392 else
1393 __ Subu(dst, lhs, rhs_reg);
1394 }
1395 break;
1396 }
1397
1398 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001399 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1400 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1401 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1402 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001403 Location rhs_location = locations->InAt(1);
1404 bool use_imm = rhs_location.IsConstant();
1405 if (!use_imm) {
1406 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1407 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1408 if (instruction->IsAnd()) {
1409 __ And(dst_low, lhs_low, rhs_low);
1410 __ And(dst_high, lhs_high, rhs_high);
1411 } else if (instruction->IsOr()) {
1412 __ Or(dst_low, lhs_low, rhs_low);
1413 __ Or(dst_high, lhs_high, rhs_high);
1414 } else if (instruction->IsXor()) {
1415 __ Xor(dst_low, lhs_low, rhs_low);
1416 __ Xor(dst_high, lhs_high, rhs_high);
1417 } else if (instruction->IsAdd()) {
1418 if (lhs_low == rhs_low) {
1419 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1420 __ Slt(TMP, lhs_low, ZERO);
1421 __ Addu(dst_low, lhs_low, rhs_low);
1422 } else {
1423 __ Addu(dst_low, lhs_low, rhs_low);
1424 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1425 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1426 }
1427 __ Addu(dst_high, lhs_high, rhs_high);
1428 __ Addu(dst_high, dst_high, TMP);
1429 } else {
1430 DCHECK(instruction->IsSub());
1431 __ Sltu(TMP, lhs_low, rhs_low);
1432 __ Subu(dst_low, lhs_low, rhs_low);
1433 __ Subu(dst_high, lhs_high, rhs_high);
1434 __ Subu(dst_high, dst_high, TMP);
1435 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001436 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001437 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1438 if (instruction->IsOr()) {
1439 uint32_t low = Low32Bits(value);
1440 uint32_t high = High32Bits(value);
1441 if (IsUint<16>(low)) {
1442 if (dst_low != lhs_low || low != 0) {
1443 __ Ori(dst_low, lhs_low, low);
1444 }
1445 } else {
1446 __ LoadConst32(TMP, low);
1447 __ Or(dst_low, lhs_low, TMP);
1448 }
1449 if (IsUint<16>(high)) {
1450 if (dst_high != lhs_high || high != 0) {
1451 __ Ori(dst_high, lhs_high, high);
1452 }
1453 } else {
1454 if (high != low) {
1455 __ LoadConst32(TMP, high);
1456 }
1457 __ Or(dst_high, lhs_high, TMP);
1458 }
1459 } else if (instruction->IsXor()) {
1460 uint32_t low = Low32Bits(value);
1461 uint32_t high = High32Bits(value);
1462 if (IsUint<16>(low)) {
1463 if (dst_low != lhs_low || low != 0) {
1464 __ Xori(dst_low, lhs_low, low);
1465 }
1466 } else {
1467 __ LoadConst32(TMP, low);
1468 __ Xor(dst_low, lhs_low, TMP);
1469 }
1470 if (IsUint<16>(high)) {
1471 if (dst_high != lhs_high || high != 0) {
1472 __ Xori(dst_high, lhs_high, high);
1473 }
1474 } else {
1475 if (high != low) {
1476 __ LoadConst32(TMP, high);
1477 }
1478 __ Xor(dst_high, lhs_high, TMP);
1479 }
1480 } else if (instruction->IsAnd()) {
1481 uint32_t low = Low32Bits(value);
1482 uint32_t high = High32Bits(value);
1483 if (IsUint<16>(low)) {
1484 __ Andi(dst_low, lhs_low, low);
1485 } else if (low != 0xFFFFFFFF) {
1486 __ LoadConst32(TMP, low);
1487 __ And(dst_low, lhs_low, TMP);
1488 } else if (dst_low != lhs_low) {
1489 __ Move(dst_low, lhs_low);
1490 }
1491 if (IsUint<16>(high)) {
1492 __ Andi(dst_high, lhs_high, high);
1493 } else if (high != 0xFFFFFFFF) {
1494 if (high != low) {
1495 __ LoadConst32(TMP, high);
1496 }
1497 __ And(dst_high, lhs_high, TMP);
1498 } else if (dst_high != lhs_high) {
1499 __ Move(dst_high, lhs_high);
1500 }
1501 } else {
1502 if (instruction->IsSub()) {
1503 value = -value;
1504 } else {
1505 DCHECK(instruction->IsAdd());
1506 }
1507 int32_t low = Low32Bits(value);
1508 int32_t high = High32Bits(value);
1509 if (IsInt<16>(low)) {
1510 if (dst_low != lhs_low || low != 0) {
1511 __ Addiu(dst_low, lhs_low, low);
1512 }
1513 if (low != 0) {
1514 __ Sltiu(AT, dst_low, low);
1515 }
1516 } else {
1517 __ LoadConst32(TMP, low);
1518 __ Addu(dst_low, lhs_low, TMP);
1519 __ Sltu(AT, dst_low, TMP);
1520 }
1521 if (IsInt<16>(high)) {
1522 if (dst_high != lhs_high || high != 0) {
1523 __ Addiu(dst_high, lhs_high, high);
1524 }
1525 } else {
1526 if (high != low) {
1527 __ LoadConst32(TMP, high);
1528 }
1529 __ Addu(dst_high, lhs_high, TMP);
1530 }
1531 if (low != 0) {
1532 __ Addu(dst_high, dst_high, AT);
1533 }
1534 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001535 }
1536 break;
1537 }
1538
1539 case Primitive::kPrimFloat:
1540 case Primitive::kPrimDouble: {
1541 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1542 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1543 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1544 if (instruction->IsAdd()) {
1545 if (type == Primitive::kPrimFloat) {
1546 __ AddS(dst, lhs, rhs);
1547 } else {
1548 __ AddD(dst, lhs, rhs);
1549 }
1550 } else {
1551 DCHECK(instruction->IsSub());
1552 if (type == Primitive::kPrimFloat) {
1553 __ SubS(dst, lhs, rhs);
1554 } else {
1555 __ SubD(dst, lhs, rhs);
1556 }
1557 }
1558 break;
1559 }
1560
1561 default:
1562 LOG(FATAL) << "Unexpected binary operation type " << type;
1563 }
1564}
1565
1566void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001567 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001568
1569 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1570 Primitive::Type type = instr->GetResultType();
1571 switch (type) {
1572 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001573 locations->SetInAt(0, Location::RequiresRegister());
1574 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1575 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1576 break;
1577 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001578 locations->SetInAt(0, Location::RequiresRegister());
1579 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1580 locations->SetOut(Location::RequiresRegister());
1581 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001582 default:
1583 LOG(FATAL) << "Unexpected shift type " << type;
1584 }
1585}
1586
1587static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1588
1589void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001590 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001591 LocationSummary* locations = instr->GetLocations();
1592 Primitive::Type type = instr->GetType();
1593
1594 Location rhs_location = locations->InAt(1);
1595 bool use_imm = rhs_location.IsConstant();
1596 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1597 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001598 const uint32_t shift_mask =
1599 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001600 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001601 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1602 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001603
1604 switch (type) {
1605 case Primitive::kPrimInt: {
1606 Register dst = locations->Out().AsRegister<Register>();
1607 Register lhs = locations->InAt(0).AsRegister<Register>();
1608 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001609 if (shift_value == 0) {
1610 if (dst != lhs) {
1611 __ Move(dst, lhs);
1612 }
1613 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001614 __ Sll(dst, lhs, shift_value);
1615 } else if (instr->IsShr()) {
1616 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001617 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001618 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001619 } else {
1620 if (has_ins_rotr) {
1621 __ Rotr(dst, lhs, shift_value);
1622 } else {
1623 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1624 __ Srl(dst, lhs, shift_value);
1625 __ Or(dst, dst, TMP);
1626 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001627 }
1628 } else {
1629 if (instr->IsShl()) {
1630 __ Sllv(dst, lhs, rhs_reg);
1631 } else if (instr->IsShr()) {
1632 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001633 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001634 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001635 } else {
1636 if (has_ins_rotr) {
1637 __ Rotrv(dst, lhs, rhs_reg);
1638 } else {
1639 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001640 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1641 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1642 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1643 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1644 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001645 __ Sllv(TMP, lhs, TMP);
1646 __ Srlv(dst, lhs, rhs_reg);
1647 __ Or(dst, dst, TMP);
1648 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001649 }
1650 }
1651 break;
1652 }
1653
1654 case Primitive::kPrimLong: {
1655 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1656 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1657 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1658 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1659 if (use_imm) {
1660 if (shift_value == 0) {
1661 codegen_->Move64(locations->Out(), locations->InAt(0));
1662 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001663 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001664 if (instr->IsShl()) {
1665 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1666 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1667 __ Sll(dst_low, lhs_low, shift_value);
1668 } else if (instr->IsShr()) {
1669 __ Srl(dst_low, lhs_low, shift_value);
1670 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1671 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001672 } else if (instr->IsUShr()) {
1673 __ Srl(dst_low, lhs_low, shift_value);
1674 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1675 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001676 } else {
1677 __ Srl(dst_low, lhs_low, shift_value);
1678 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1679 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001680 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001681 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001682 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001683 if (instr->IsShl()) {
1684 __ Sll(dst_low, lhs_low, shift_value);
1685 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1686 __ Sll(dst_high, lhs_high, shift_value);
1687 __ Or(dst_high, dst_high, TMP);
1688 } else if (instr->IsShr()) {
1689 __ Sra(dst_high, lhs_high, shift_value);
1690 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1691 __ Srl(dst_low, lhs_low, shift_value);
1692 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001693 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001694 __ Srl(dst_high, lhs_high, shift_value);
1695 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1696 __ Srl(dst_low, lhs_low, shift_value);
1697 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001698 } else {
1699 __ Srl(TMP, lhs_low, shift_value);
1700 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1701 __ Or(dst_low, dst_low, TMP);
1702 __ Srl(TMP, lhs_high, shift_value);
1703 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1704 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001705 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001706 }
1707 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001708 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001709 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001710 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001711 __ Move(dst_low, ZERO);
1712 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001713 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001714 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001715 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001716 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001717 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001718 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001719 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001720 // 64-bit rotation by 32 is just a swap.
1721 __ Move(dst_low, lhs_high);
1722 __ Move(dst_high, lhs_low);
1723 } else {
1724 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001725 __ Srl(dst_low, lhs_high, shift_value_high);
1726 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1727 __ Srl(dst_high, lhs_low, shift_value_high);
1728 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001729 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001730 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1731 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001732 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001733 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1734 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001735 __ Or(dst_high, dst_high, TMP);
1736 }
1737 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001738 }
1739 }
1740 } else {
1741 MipsLabel done;
1742 if (instr->IsShl()) {
1743 __ Sllv(dst_low, lhs_low, rhs_reg);
1744 __ Nor(AT, ZERO, rhs_reg);
1745 __ Srl(TMP, lhs_low, 1);
1746 __ Srlv(TMP, TMP, AT);
1747 __ Sllv(dst_high, lhs_high, rhs_reg);
1748 __ Or(dst_high, dst_high, TMP);
1749 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1750 __ Beqz(TMP, &done);
1751 __ Move(dst_high, dst_low);
1752 __ Move(dst_low, ZERO);
1753 } else if (instr->IsShr()) {
1754 __ Srav(dst_high, lhs_high, rhs_reg);
1755 __ Nor(AT, ZERO, rhs_reg);
1756 __ Sll(TMP, lhs_high, 1);
1757 __ Sllv(TMP, TMP, AT);
1758 __ Srlv(dst_low, lhs_low, rhs_reg);
1759 __ Or(dst_low, dst_low, TMP);
1760 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1761 __ Beqz(TMP, &done);
1762 __ Move(dst_low, dst_high);
1763 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001764 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001765 __ Srlv(dst_high, lhs_high, rhs_reg);
1766 __ Nor(AT, ZERO, rhs_reg);
1767 __ Sll(TMP, lhs_high, 1);
1768 __ Sllv(TMP, TMP, AT);
1769 __ Srlv(dst_low, lhs_low, rhs_reg);
1770 __ Or(dst_low, dst_low, TMP);
1771 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1772 __ Beqz(TMP, &done);
1773 __ Move(dst_low, dst_high);
1774 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001775 } else {
1776 __ Nor(AT, ZERO, rhs_reg);
1777 __ Srlv(TMP, lhs_low, rhs_reg);
1778 __ Sll(dst_low, lhs_high, 1);
1779 __ Sllv(dst_low, dst_low, AT);
1780 __ Or(dst_low, dst_low, TMP);
1781 __ Srlv(TMP, lhs_high, rhs_reg);
1782 __ Sll(dst_high, lhs_low, 1);
1783 __ Sllv(dst_high, dst_high, AT);
1784 __ Or(dst_high, dst_high, TMP);
1785 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1786 __ Beqz(TMP, &done);
1787 __ Move(TMP, dst_high);
1788 __ Move(dst_high, dst_low);
1789 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001790 }
1791 __ Bind(&done);
1792 }
1793 break;
1794 }
1795
1796 default:
1797 LOG(FATAL) << "Unexpected shift operation type " << type;
1798 }
1799}
1800
1801void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1802 HandleBinaryOp(instruction);
1803}
1804
1805void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1806 HandleBinaryOp(instruction);
1807}
1808
1809void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1810 HandleBinaryOp(instruction);
1811}
1812
1813void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1814 HandleBinaryOp(instruction);
1815}
1816
1817void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1818 LocationSummary* locations =
1819 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1820 locations->SetInAt(0, Location::RequiresRegister());
1821 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1822 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1823 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1824 } else {
1825 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1826 }
1827}
1828
Alexey Frunze2923db72016-08-20 01:55:47 -07001829auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1830 auto null_checker = [this, instruction]() {
1831 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1832 };
1833 return null_checker;
1834}
1835
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001836void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1837 LocationSummary* locations = instruction->GetLocations();
1838 Register obj = locations->InAt(0).AsRegister<Register>();
1839 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001840 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001841 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001842
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001843 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001844 switch (type) {
1845 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001846 Register out = locations->Out().AsRegister<Register>();
1847 if (index.IsConstant()) {
1848 size_t offset =
1849 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001850 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001851 } else {
1852 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001853 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001854 }
1855 break;
1856 }
1857
1858 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001859 Register out = locations->Out().AsRegister<Register>();
1860 if (index.IsConstant()) {
1861 size_t offset =
1862 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001863 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001864 } else {
1865 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001866 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001867 }
1868 break;
1869 }
1870
1871 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001872 Register out = locations->Out().AsRegister<Register>();
1873 if (index.IsConstant()) {
1874 size_t offset =
1875 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001876 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001877 } else {
1878 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1879 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001880 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001881 }
1882 break;
1883 }
1884
1885 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001886 Register out = locations->Out().AsRegister<Register>();
1887 if (index.IsConstant()) {
1888 size_t offset =
1889 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001890 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001891 } else {
1892 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1893 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001894 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001895 }
1896 break;
1897 }
1898
1899 case Primitive::kPrimInt:
1900 case Primitive::kPrimNot: {
1901 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001902 Register out = locations->Out().AsRegister<Register>();
1903 if (index.IsConstant()) {
1904 size_t offset =
1905 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001906 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001907 } else {
1908 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1909 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001910 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001911 }
1912 break;
1913 }
1914
1915 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001916 Register out = locations->Out().AsRegisterPairLow<Register>();
1917 if (index.IsConstant()) {
1918 size_t offset =
1919 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001920 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001921 } else {
1922 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1923 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001924 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001925 }
1926 break;
1927 }
1928
1929 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001930 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1931 if (index.IsConstant()) {
1932 size_t offset =
1933 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001934 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001935 } else {
1936 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1937 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001938 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001939 }
1940 break;
1941 }
1942
1943 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001944 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1945 if (index.IsConstant()) {
1946 size_t offset =
1947 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001948 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001949 } else {
1950 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1951 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001952 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001953 }
1954 break;
1955 }
1956
1957 case Primitive::kPrimVoid:
1958 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1959 UNREACHABLE();
1960 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001961}
1962
1963void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1964 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1965 locations->SetInAt(0, Location::RequiresRegister());
1966 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1967}
1968
1969void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1970 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001971 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001972 Register obj = locations->InAt(0).AsRegister<Register>();
1973 Register out = locations->Out().AsRegister<Register>();
1974 __ LoadFromOffset(kLoadWord, out, obj, offset);
1975 codegen_->MaybeRecordImplicitNullCheck(instruction);
1976}
1977
Alexey Frunzef58b2482016-09-02 22:14:06 -07001978Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1979 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1980 ? Location::ConstantLocation(instruction->AsConstant())
1981 : Location::RequiresRegister();
1982}
1983
1984Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1985 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1986 // We can store a non-zero float or double constant without first loading it into the FPU,
1987 // but we should only prefer this if the constant has a single use.
1988 if (instruction->IsConstant() &&
1989 (instruction->AsConstant()->IsZeroBitPattern() ||
1990 instruction->GetUses().HasExactlyOneElement())) {
1991 return Location::ConstantLocation(instruction->AsConstant());
1992 // Otherwise fall through and require an FPU register for the constant.
1993 }
1994 return Location::RequiresFpuRegister();
1995}
1996
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001997void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001998 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001999 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2000 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002001 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002002 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002003 InvokeRuntimeCallingConvention calling_convention;
2004 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2005 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2006 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2007 } else {
2008 locations->SetInAt(0, Location::RequiresRegister());
2009 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2010 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002011 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002012 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002013 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002014 }
2015 }
2016}
2017
2018void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2019 LocationSummary* locations = instruction->GetLocations();
2020 Register obj = locations->InAt(0).AsRegister<Register>();
2021 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002022 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002023 Primitive::Type value_type = instruction->GetComponentType();
2024 bool needs_runtime_call = locations->WillCall();
2025 bool needs_write_barrier =
2026 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002027 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002028 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002029
2030 switch (value_type) {
2031 case Primitive::kPrimBoolean:
2032 case Primitive::kPrimByte: {
2033 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002034 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002035 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002036 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002037 __ Addu(base_reg, obj, index.AsRegister<Register>());
2038 }
2039 if (value_location.IsConstant()) {
2040 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2041 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2042 } else {
2043 Register value = value_location.AsRegister<Register>();
2044 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002045 }
2046 break;
2047 }
2048
2049 case Primitive::kPrimShort:
2050 case Primitive::kPrimChar: {
2051 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002052 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002053 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002054 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002055 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2056 __ Addu(base_reg, obj, base_reg);
2057 }
2058 if (value_location.IsConstant()) {
2059 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2060 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2061 } else {
2062 Register value = value_location.AsRegister<Register>();
2063 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002064 }
2065 break;
2066 }
2067
2068 case Primitive::kPrimInt:
2069 case Primitive::kPrimNot: {
2070 if (!needs_runtime_call) {
2071 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002072 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002073 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002074 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002075 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2076 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002077 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002078 if (value_location.IsConstant()) {
2079 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2080 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2081 DCHECK(!needs_write_barrier);
2082 } else {
2083 Register value = value_location.AsRegister<Register>();
2084 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2085 if (needs_write_barrier) {
2086 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevice114da22016-12-26 14:21:43 +01002087 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunzef58b2482016-09-02 22:14:06 -07002088 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002089 }
2090 } else {
2091 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002092 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002093 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2094 }
2095 break;
2096 }
2097
2098 case Primitive::kPrimLong: {
2099 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002100 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002101 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002102 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002103 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2104 __ Addu(base_reg, obj, base_reg);
2105 }
2106 if (value_location.IsConstant()) {
2107 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2108 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2109 } else {
2110 Register value = value_location.AsRegisterPairLow<Register>();
2111 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002112 }
2113 break;
2114 }
2115
2116 case Primitive::kPrimFloat: {
2117 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002118 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002119 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002120 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002121 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2122 __ Addu(base_reg, obj, base_reg);
2123 }
2124 if (value_location.IsConstant()) {
2125 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2126 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2127 } else {
2128 FRegister value = value_location.AsFpuRegister<FRegister>();
2129 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002130 }
2131 break;
2132 }
2133
2134 case Primitive::kPrimDouble: {
2135 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002136 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002137 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002138 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002139 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2140 __ Addu(base_reg, obj, base_reg);
2141 }
2142 if (value_location.IsConstant()) {
2143 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2144 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2145 } else {
2146 FRegister value = value_location.AsFpuRegister<FRegister>();
2147 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002148 }
2149 break;
2150 }
2151
2152 case Primitive::kPrimVoid:
2153 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2154 UNREACHABLE();
2155 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002156}
2157
2158void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002159 RegisterSet caller_saves = RegisterSet::Empty();
2160 InvokeRuntimeCallingConvention calling_convention;
2161 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2162 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2163 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002164 locations->SetInAt(0, Location::RequiresRegister());
2165 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002166}
2167
2168void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2169 LocationSummary* locations = instruction->GetLocations();
2170 BoundsCheckSlowPathMIPS* slow_path =
2171 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2172 codegen_->AddSlowPath(slow_path);
2173
2174 Register index = locations->InAt(0).AsRegister<Register>();
2175 Register length = locations->InAt(1).AsRegister<Register>();
2176
2177 // length is limited by the maximum positive signed 32-bit integer.
2178 // Unsigned comparison of length and index checks for index < 0
2179 // and for length <= index simultaneously.
2180 __ Bgeu(index, length, slow_path->GetEntryLabel());
2181}
2182
2183void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2184 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2185 instruction,
2186 LocationSummary::kCallOnSlowPath);
2187 locations->SetInAt(0, Location::RequiresRegister());
2188 locations->SetInAt(1, Location::RequiresRegister());
2189 // Note that TypeCheckSlowPathMIPS uses this register too.
2190 locations->AddTemp(Location::RequiresRegister());
2191}
2192
2193void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2194 LocationSummary* locations = instruction->GetLocations();
2195 Register obj = locations->InAt(0).AsRegister<Register>();
2196 Register cls = locations->InAt(1).AsRegister<Register>();
2197 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2198
2199 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2200 codegen_->AddSlowPath(slow_path);
2201
2202 // TODO: avoid this check if we know obj is not null.
2203 __ Beqz(obj, slow_path->GetExitLabel());
2204 // Compare the class of `obj` with `cls`.
2205 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2206 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2207 __ Bind(slow_path->GetExitLabel());
2208}
2209
2210void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2211 LocationSummary* locations =
2212 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2213 locations->SetInAt(0, Location::RequiresRegister());
2214 if (check->HasUses()) {
2215 locations->SetOut(Location::SameAsFirstInput());
2216 }
2217}
2218
2219void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2220 // We assume the class is not null.
2221 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2222 check->GetLoadClass(),
2223 check,
2224 check->GetDexPc(),
2225 true);
2226 codegen_->AddSlowPath(slow_path);
2227 GenerateClassInitializationCheck(slow_path,
2228 check->GetLocations()->InAt(0).AsRegister<Register>());
2229}
2230
2231void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2232 Primitive::Type in_type = compare->InputAt(0)->GetType();
2233
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002234 LocationSummary* locations =
2235 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002236
2237 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002238 case Primitive::kPrimBoolean:
2239 case Primitive::kPrimByte:
2240 case Primitive::kPrimShort:
2241 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002242 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002243 locations->SetInAt(0, Location::RequiresRegister());
2244 locations->SetInAt(1, Location::RequiresRegister());
2245 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2246 break;
2247
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002248 case Primitive::kPrimLong:
2249 locations->SetInAt(0, Location::RequiresRegister());
2250 locations->SetInAt(1, Location::RequiresRegister());
2251 // Output overlaps because it is written before doing the low comparison.
2252 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2253 break;
2254
2255 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002256 case Primitive::kPrimDouble:
2257 locations->SetInAt(0, Location::RequiresFpuRegister());
2258 locations->SetInAt(1, Location::RequiresFpuRegister());
2259 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002261
2262 default:
2263 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2264 }
2265}
2266
2267void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2268 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002269 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002270 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002271 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002272
2273 // 0 if: left == right
2274 // 1 if: left > right
2275 // -1 if: left < right
2276 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002277 case Primitive::kPrimBoolean:
2278 case Primitive::kPrimByte:
2279 case Primitive::kPrimShort:
2280 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002281 case Primitive::kPrimInt: {
2282 Register lhs = locations->InAt(0).AsRegister<Register>();
2283 Register rhs = locations->InAt(1).AsRegister<Register>();
2284 __ Slt(TMP, lhs, rhs);
2285 __ Slt(res, rhs, lhs);
2286 __ Subu(res, res, TMP);
2287 break;
2288 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002289 case Primitive::kPrimLong: {
2290 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002291 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2292 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2293 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2294 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2295 // TODO: more efficient (direct) comparison with a constant.
2296 __ Slt(TMP, lhs_high, rhs_high);
2297 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2298 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2299 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2300 __ Sltu(TMP, lhs_low, rhs_low);
2301 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2302 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2303 __ Bind(&done);
2304 break;
2305 }
2306
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002307 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002308 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002309 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2310 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2311 MipsLabel done;
2312 if (isR6) {
2313 __ CmpEqS(FTMP, lhs, rhs);
2314 __ LoadConst32(res, 0);
2315 __ Bc1nez(FTMP, &done);
2316 if (gt_bias) {
2317 __ CmpLtS(FTMP, lhs, rhs);
2318 __ LoadConst32(res, -1);
2319 __ Bc1nez(FTMP, &done);
2320 __ LoadConst32(res, 1);
2321 } else {
2322 __ CmpLtS(FTMP, rhs, lhs);
2323 __ LoadConst32(res, 1);
2324 __ Bc1nez(FTMP, &done);
2325 __ LoadConst32(res, -1);
2326 }
2327 } else {
2328 if (gt_bias) {
2329 __ ColtS(0, lhs, rhs);
2330 __ LoadConst32(res, -1);
2331 __ Bc1t(0, &done);
2332 __ CeqS(0, lhs, rhs);
2333 __ LoadConst32(res, 1);
2334 __ Movt(res, ZERO, 0);
2335 } else {
2336 __ ColtS(0, rhs, lhs);
2337 __ LoadConst32(res, 1);
2338 __ Bc1t(0, &done);
2339 __ CeqS(0, lhs, rhs);
2340 __ LoadConst32(res, -1);
2341 __ Movt(res, ZERO, 0);
2342 }
2343 }
2344 __ Bind(&done);
2345 break;
2346 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002347 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002348 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002349 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2350 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2351 MipsLabel done;
2352 if (isR6) {
2353 __ CmpEqD(FTMP, lhs, rhs);
2354 __ LoadConst32(res, 0);
2355 __ Bc1nez(FTMP, &done);
2356 if (gt_bias) {
2357 __ CmpLtD(FTMP, lhs, rhs);
2358 __ LoadConst32(res, -1);
2359 __ Bc1nez(FTMP, &done);
2360 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002361 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002362 __ CmpLtD(FTMP, rhs, lhs);
2363 __ LoadConst32(res, 1);
2364 __ Bc1nez(FTMP, &done);
2365 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002366 }
2367 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002368 if (gt_bias) {
2369 __ ColtD(0, lhs, rhs);
2370 __ LoadConst32(res, -1);
2371 __ Bc1t(0, &done);
2372 __ CeqD(0, lhs, rhs);
2373 __ LoadConst32(res, 1);
2374 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002375 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002376 __ ColtD(0, rhs, lhs);
2377 __ LoadConst32(res, 1);
2378 __ Bc1t(0, &done);
2379 __ CeqD(0, lhs, rhs);
2380 __ LoadConst32(res, -1);
2381 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002382 }
2383 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002384 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002385 break;
2386 }
2387
2388 default:
2389 LOG(FATAL) << "Unimplemented compare type " << in_type;
2390 }
2391}
2392
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002393void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002394 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002395 switch (instruction->InputAt(0)->GetType()) {
2396 default:
2397 case Primitive::kPrimLong:
2398 locations->SetInAt(0, Location::RequiresRegister());
2399 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2400 break;
2401
2402 case Primitive::kPrimFloat:
2403 case Primitive::kPrimDouble:
2404 locations->SetInAt(0, Location::RequiresFpuRegister());
2405 locations->SetInAt(1, Location::RequiresFpuRegister());
2406 break;
2407 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002408 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002409 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2410 }
2411}
2412
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002413void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002414 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002415 return;
2416 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002417
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002418 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002419 LocationSummary* locations = instruction->GetLocations();
2420 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002421 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002422
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002423 switch (type) {
2424 default:
2425 // Integer case.
2426 GenerateIntCompare(instruction->GetCondition(), locations);
2427 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002428
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002429 case Primitive::kPrimLong:
2430 // TODO: don't use branches.
2431 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432 break;
2433
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002434 case Primitive::kPrimFloat:
2435 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002436 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2437 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002438 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002439
2440 // Convert the branches into the result.
2441 MipsLabel done;
2442
2443 // False case: result = 0.
2444 __ LoadConst32(dst, 0);
2445 __ B(&done);
2446
2447 // True case: result = 1.
2448 __ Bind(&true_label);
2449 __ LoadConst32(dst, 1);
2450 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002451}
2452
Alexey Frunze7e99e052015-11-24 19:28:01 -08002453void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2454 DCHECK(instruction->IsDiv() || instruction->IsRem());
2455 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2456
2457 LocationSummary* locations = instruction->GetLocations();
2458 Location second = locations->InAt(1);
2459 DCHECK(second.IsConstant());
2460
2461 Register out = locations->Out().AsRegister<Register>();
2462 Register dividend = locations->InAt(0).AsRegister<Register>();
2463 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2464 DCHECK(imm == 1 || imm == -1);
2465
2466 if (instruction->IsRem()) {
2467 __ Move(out, ZERO);
2468 } else {
2469 if (imm == -1) {
2470 __ Subu(out, ZERO, dividend);
2471 } else if (out != dividend) {
2472 __ Move(out, dividend);
2473 }
2474 }
2475}
2476
2477void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2478 DCHECK(instruction->IsDiv() || instruction->IsRem());
2479 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2480
2481 LocationSummary* locations = instruction->GetLocations();
2482 Location second = locations->InAt(1);
2483 DCHECK(second.IsConstant());
2484
2485 Register out = locations->Out().AsRegister<Register>();
2486 Register dividend = locations->InAt(0).AsRegister<Register>();
2487 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002488 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002489 int ctz_imm = CTZ(abs_imm);
2490
2491 if (instruction->IsDiv()) {
2492 if (ctz_imm == 1) {
2493 // Fast path for division by +/-2, which is very common.
2494 __ Srl(TMP, dividend, 31);
2495 } else {
2496 __ Sra(TMP, dividend, 31);
2497 __ Srl(TMP, TMP, 32 - ctz_imm);
2498 }
2499 __ Addu(out, dividend, TMP);
2500 __ Sra(out, out, ctz_imm);
2501 if (imm < 0) {
2502 __ Subu(out, ZERO, out);
2503 }
2504 } else {
2505 if (ctz_imm == 1) {
2506 // Fast path for modulo +/-2, which is very common.
2507 __ Sra(TMP, dividend, 31);
2508 __ Subu(out, dividend, TMP);
2509 __ Andi(out, out, 1);
2510 __ Addu(out, out, TMP);
2511 } else {
2512 __ Sra(TMP, dividend, 31);
2513 __ Srl(TMP, TMP, 32 - ctz_imm);
2514 __ Addu(out, dividend, TMP);
2515 if (IsUint<16>(abs_imm - 1)) {
2516 __ Andi(out, out, abs_imm - 1);
2517 } else {
2518 __ Sll(out, out, 32 - ctz_imm);
2519 __ Srl(out, out, 32 - ctz_imm);
2520 }
2521 __ Subu(out, out, TMP);
2522 }
2523 }
2524}
2525
2526void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2527 DCHECK(instruction->IsDiv() || instruction->IsRem());
2528 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2529
2530 LocationSummary* locations = instruction->GetLocations();
2531 Location second = locations->InAt(1);
2532 DCHECK(second.IsConstant());
2533
2534 Register out = locations->Out().AsRegister<Register>();
2535 Register dividend = locations->InAt(0).AsRegister<Register>();
2536 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2537
2538 int64_t magic;
2539 int shift;
2540 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2541
2542 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2543
2544 __ LoadConst32(TMP, magic);
2545 if (isR6) {
2546 __ MuhR6(TMP, dividend, TMP);
2547 } else {
2548 __ MultR2(dividend, TMP);
2549 __ Mfhi(TMP);
2550 }
2551 if (imm > 0 && magic < 0) {
2552 __ Addu(TMP, TMP, dividend);
2553 } else if (imm < 0 && magic > 0) {
2554 __ Subu(TMP, TMP, dividend);
2555 }
2556
2557 if (shift != 0) {
2558 __ Sra(TMP, TMP, shift);
2559 }
2560
2561 if (instruction->IsDiv()) {
2562 __ Sra(out, TMP, 31);
2563 __ Subu(out, TMP, out);
2564 } else {
2565 __ Sra(AT, TMP, 31);
2566 __ Subu(AT, TMP, AT);
2567 __ LoadConst32(TMP, imm);
2568 if (isR6) {
2569 __ MulR6(TMP, AT, TMP);
2570 } else {
2571 __ MulR2(TMP, AT, TMP);
2572 }
2573 __ Subu(out, dividend, TMP);
2574 }
2575}
2576
2577void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2578 DCHECK(instruction->IsDiv() || instruction->IsRem());
2579 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2580
2581 LocationSummary* locations = instruction->GetLocations();
2582 Register out = locations->Out().AsRegister<Register>();
2583 Location second = locations->InAt(1);
2584
2585 if (second.IsConstant()) {
2586 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2587 if (imm == 0) {
2588 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2589 } else if (imm == 1 || imm == -1) {
2590 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002591 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002592 DivRemByPowerOfTwo(instruction);
2593 } else {
2594 DCHECK(imm <= -2 || imm >= 2);
2595 GenerateDivRemWithAnyConstant(instruction);
2596 }
2597 } else {
2598 Register dividend = locations->InAt(0).AsRegister<Register>();
2599 Register divisor = second.AsRegister<Register>();
2600 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2601 if (instruction->IsDiv()) {
2602 if (isR6) {
2603 __ DivR6(out, dividend, divisor);
2604 } else {
2605 __ DivR2(out, dividend, divisor);
2606 }
2607 } else {
2608 if (isR6) {
2609 __ ModR6(out, dividend, divisor);
2610 } else {
2611 __ ModR2(out, dividend, divisor);
2612 }
2613 }
2614 }
2615}
2616
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002617void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2618 Primitive::Type type = div->GetResultType();
2619 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002620 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002621 : LocationSummary::kNoCall;
2622
2623 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2624
2625 switch (type) {
2626 case Primitive::kPrimInt:
2627 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002628 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002629 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2630 break;
2631
2632 case Primitive::kPrimLong: {
2633 InvokeRuntimeCallingConvention calling_convention;
2634 locations->SetInAt(0, Location::RegisterPairLocation(
2635 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2636 locations->SetInAt(1, Location::RegisterPairLocation(
2637 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2638 locations->SetOut(calling_convention.GetReturnLocation(type));
2639 break;
2640 }
2641
2642 case Primitive::kPrimFloat:
2643 case Primitive::kPrimDouble:
2644 locations->SetInAt(0, Location::RequiresFpuRegister());
2645 locations->SetInAt(1, Location::RequiresFpuRegister());
2646 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2647 break;
2648
2649 default:
2650 LOG(FATAL) << "Unexpected div type " << type;
2651 }
2652}
2653
2654void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2655 Primitive::Type type = instruction->GetType();
2656 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002657
2658 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002659 case Primitive::kPrimInt:
2660 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002661 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002662 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002663 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002664 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2665 break;
2666 }
2667 case Primitive::kPrimFloat:
2668 case Primitive::kPrimDouble: {
2669 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2670 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2671 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2672 if (type == Primitive::kPrimFloat) {
2673 __ DivS(dst, lhs, rhs);
2674 } else {
2675 __ DivD(dst, lhs, rhs);
2676 }
2677 break;
2678 }
2679 default:
2680 LOG(FATAL) << "Unexpected div type " << type;
2681 }
2682}
2683
2684void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002685 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002686 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002687}
2688
2689void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2690 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2691 codegen_->AddSlowPath(slow_path);
2692 Location value = instruction->GetLocations()->InAt(0);
2693 Primitive::Type type = instruction->GetType();
2694
2695 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002696 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002697 case Primitive::kPrimByte:
2698 case Primitive::kPrimChar:
2699 case Primitive::kPrimShort:
2700 case Primitive::kPrimInt: {
2701 if (value.IsConstant()) {
2702 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2703 __ B(slow_path->GetEntryLabel());
2704 } else {
2705 // A division by a non-null constant is valid. We don't need to perform
2706 // any check, so simply fall through.
2707 }
2708 } else {
2709 DCHECK(value.IsRegister()) << value;
2710 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2711 }
2712 break;
2713 }
2714 case Primitive::kPrimLong: {
2715 if (value.IsConstant()) {
2716 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2717 __ B(slow_path->GetEntryLabel());
2718 } else {
2719 // A division by a non-null constant is valid. We don't need to perform
2720 // any check, so simply fall through.
2721 }
2722 } else {
2723 DCHECK(value.IsRegisterPair()) << value;
2724 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2725 __ Beqz(TMP, slow_path->GetEntryLabel());
2726 }
2727 break;
2728 }
2729 default:
2730 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2731 }
2732}
2733
2734void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2735 LocationSummary* locations =
2736 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2737 locations->SetOut(Location::ConstantLocation(constant));
2738}
2739
2740void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2741 // Will be generated at use site.
2742}
2743
2744void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2745 exit->SetLocations(nullptr);
2746}
2747
2748void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2749}
2750
2751void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2752 LocationSummary* locations =
2753 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2754 locations->SetOut(Location::ConstantLocation(constant));
2755}
2756
2757void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2758 // Will be generated at use site.
2759}
2760
2761void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2762 got->SetLocations(nullptr);
2763}
2764
2765void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2766 DCHECK(!successor->IsExitBlock());
2767 HBasicBlock* block = got->GetBlock();
2768 HInstruction* previous = got->GetPrevious();
2769 HLoopInformation* info = block->GetLoopInformation();
2770
2771 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2772 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2773 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2774 return;
2775 }
2776 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2777 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2778 }
2779 if (!codegen_->GoesToNextBlock(block, successor)) {
2780 __ B(codegen_->GetLabelOf(successor));
2781 }
2782}
2783
2784void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2785 HandleGoto(got, got->GetSuccessor());
2786}
2787
2788void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2789 try_boundary->SetLocations(nullptr);
2790}
2791
2792void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2793 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2794 if (!successor->IsExitBlock()) {
2795 HandleGoto(try_boundary, successor);
2796 }
2797}
2798
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002799void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2800 LocationSummary* locations) {
2801 Register dst = locations->Out().AsRegister<Register>();
2802 Register lhs = locations->InAt(0).AsRegister<Register>();
2803 Location rhs_location = locations->InAt(1);
2804 Register rhs_reg = ZERO;
2805 int64_t rhs_imm = 0;
2806 bool use_imm = rhs_location.IsConstant();
2807 if (use_imm) {
2808 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2809 } else {
2810 rhs_reg = rhs_location.AsRegister<Register>();
2811 }
2812
2813 switch (cond) {
2814 case kCondEQ:
2815 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002816 if (use_imm && IsInt<16>(-rhs_imm)) {
2817 if (rhs_imm == 0) {
2818 if (cond == kCondEQ) {
2819 __ Sltiu(dst, lhs, 1);
2820 } else {
2821 __ Sltu(dst, ZERO, lhs);
2822 }
2823 } else {
2824 __ Addiu(dst, lhs, -rhs_imm);
2825 if (cond == kCondEQ) {
2826 __ Sltiu(dst, dst, 1);
2827 } else {
2828 __ Sltu(dst, ZERO, dst);
2829 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002830 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002831 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002832 if (use_imm && IsUint<16>(rhs_imm)) {
2833 __ Xori(dst, lhs, rhs_imm);
2834 } else {
2835 if (use_imm) {
2836 rhs_reg = TMP;
2837 __ LoadConst32(rhs_reg, rhs_imm);
2838 }
2839 __ Xor(dst, lhs, rhs_reg);
2840 }
2841 if (cond == kCondEQ) {
2842 __ Sltiu(dst, dst, 1);
2843 } else {
2844 __ Sltu(dst, ZERO, dst);
2845 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002846 }
2847 break;
2848
2849 case kCondLT:
2850 case kCondGE:
2851 if (use_imm && IsInt<16>(rhs_imm)) {
2852 __ Slti(dst, lhs, rhs_imm);
2853 } else {
2854 if (use_imm) {
2855 rhs_reg = TMP;
2856 __ LoadConst32(rhs_reg, rhs_imm);
2857 }
2858 __ Slt(dst, lhs, rhs_reg);
2859 }
2860 if (cond == kCondGE) {
2861 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2862 // only the slt instruction but no sge.
2863 __ Xori(dst, dst, 1);
2864 }
2865 break;
2866
2867 case kCondLE:
2868 case kCondGT:
2869 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2870 // Simulate lhs <= rhs via lhs < rhs + 1.
2871 __ Slti(dst, lhs, rhs_imm + 1);
2872 if (cond == kCondGT) {
2873 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2874 // only the slti instruction but no sgti.
2875 __ Xori(dst, dst, 1);
2876 }
2877 } else {
2878 if (use_imm) {
2879 rhs_reg = TMP;
2880 __ LoadConst32(rhs_reg, rhs_imm);
2881 }
2882 __ Slt(dst, rhs_reg, lhs);
2883 if (cond == kCondLE) {
2884 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2885 // only the slt instruction but no sle.
2886 __ Xori(dst, dst, 1);
2887 }
2888 }
2889 break;
2890
2891 case kCondB:
2892 case kCondAE:
2893 if (use_imm && IsInt<16>(rhs_imm)) {
2894 // Sltiu sign-extends its 16-bit immediate operand before
2895 // the comparison and thus lets us compare directly with
2896 // unsigned values in the ranges [0, 0x7fff] and
2897 // [0xffff8000, 0xffffffff].
2898 __ Sltiu(dst, lhs, rhs_imm);
2899 } else {
2900 if (use_imm) {
2901 rhs_reg = TMP;
2902 __ LoadConst32(rhs_reg, rhs_imm);
2903 }
2904 __ Sltu(dst, lhs, rhs_reg);
2905 }
2906 if (cond == kCondAE) {
2907 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2908 // only the sltu instruction but no sgeu.
2909 __ Xori(dst, dst, 1);
2910 }
2911 break;
2912
2913 case kCondBE:
2914 case kCondA:
2915 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2916 // Simulate lhs <= rhs via lhs < rhs + 1.
2917 // Note that this only works if rhs + 1 does not overflow
2918 // to 0, hence the check above.
2919 // Sltiu sign-extends its 16-bit immediate operand before
2920 // the comparison and thus lets us compare directly with
2921 // unsigned values in the ranges [0, 0x7fff] and
2922 // [0xffff8000, 0xffffffff].
2923 __ Sltiu(dst, lhs, rhs_imm + 1);
2924 if (cond == kCondA) {
2925 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2926 // only the sltiu instruction but no sgtiu.
2927 __ Xori(dst, dst, 1);
2928 }
2929 } else {
2930 if (use_imm) {
2931 rhs_reg = TMP;
2932 __ LoadConst32(rhs_reg, rhs_imm);
2933 }
2934 __ Sltu(dst, rhs_reg, lhs);
2935 if (cond == kCondBE) {
2936 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2937 // only the sltu instruction but no sleu.
2938 __ Xori(dst, dst, 1);
2939 }
2940 }
2941 break;
2942 }
2943}
2944
Alexey Frunze674b9ee2016-09-20 14:54:15 -07002945bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
2946 LocationSummary* input_locations,
2947 Register dst) {
2948 Register lhs = input_locations->InAt(0).AsRegister<Register>();
2949 Location rhs_location = input_locations->InAt(1);
2950 Register rhs_reg = ZERO;
2951 int64_t rhs_imm = 0;
2952 bool use_imm = rhs_location.IsConstant();
2953 if (use_imm) {
2954 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2955 } else {
2956 rhs_reg = rhs_location.AsRegister<Register>();
2957 }
2958
2959 switch (cond) {
2960 case kCondEQ:
2961 case kCondNE:
2962 if (use_imm && IsInt<16>(-rhs_imm)) {
2963 __ Addiu(dst, lhs, -rhs_imm);
2964 } else if (use_imm && IsUint<16>(rhs_imm)) {
2965 __ Xori(dst, lhs, rhs_imm);
2966 } else {
2967 if (use_imm) {
2968 rhs_reg = TMP;
2969 __ LoadConst32(rhs_reg, rhs_imm);
2970 }
2971 __ Xor(dst, lhs, rhs_reg);
2972 }
2973 return (cond == kCondEQ);
2974
2975 case kCondLT:
2976 case kCondGE:
2977 if (use_imm && IsInt<16>(rhs_imm)) {
2978 __ Slti(dst, lhs, rhs_imm);
2979 } else {
2980 if (use_imm) {
2981 rhs_reg = TMP;
2982 __ LoadConst32(rhs_reg, rhs_imm);
2983 }
2984 __ Slt(dst, lhs, rhs_reg);
2985 }
2986 return (cond == kCondGE);
2987
2988 case kCondLE:
2989 case kCondGT:
2990 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2991 // Simulate lhs <= rhs via lhs < rhs + 1.
2992 __ Slti(dst, lhs, rhs_imm + 1);
2993 return (cond == kCondGT);
2994 } else {
2995 if (use_imm) {
2996 rhs_reg = TMP;
2997 __ LoadConst32(rhs_reg, rhs_imm);
2998 }
2999 __ Slt(dst, rhs_reg, lhs);
3000 return (cond == kCondLE);
3001 }
3002
3003 case kCondB:
3004 case kCondAE:
3005 if (use_imm && IsInt<16>(rhs_imm)) {
3006 // Sltiu sign-extends its 16-bit immediate operand before
3007 // the comparison and thus lets us compare directly with
3008 // unsigned values in the ranges [0, 0x7fff] and
3009 // [0xffff8000, 0xffffffff].
3010 __ Sltiu(dst, lhs, rhs_imm);
3011 } else {
3012 if (use_imm) {
3013 rhs_reg = TMP;
3014 __ LoadConst32(rhs_reg, rhs_imm);
3015 }
3016 __ Sltu(dst, lhs, rhs_reg);
3017 }
3018 return (cond == kCondAE);
3019
3020 case kCondBE:
3021 case kCondA:
3022 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3023 // Simulate lhs <= rhs via lhs < rhs + 1.
3024 // Note that this only works if rhs + 1 does not overflow
3025 // to 0, hence the check above.
3026 // Sltiu sign-extends its 16-bit immediate operand before
3027 // the comparison and thus lets us compare directly with
3028 // unsigned values in the ranges [0, 0x7fff] and
3029 // [0xffff8000, 0xffffffff].
3030 __ Sltiu(dst, lhs, rhs_imm + 1);
3031 return (cond == kCondA);
3032 } else {
3033 if (use_imm) {
3034 rhs_reg = TMP;
3035 __ LoadConst32(rhs_reg, rhs_imm);
3036 }
3037 __ Sltu(dst, rhs_reg, lhs);
3038 return (cond == kCondBE);
3039 }
3040 }
3041}
3042
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003043void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3044 LocationSummary* locations,
3045 MipsLabel* label) {
3046 Register lhs = locations->InAt(0).AsRegister<Register>();
3047 Location rhs_location = locations->InAt(1);
3048 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003049 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003050 bool use_imm = rhs_location.IsConstant();
3051 if (use_imm) {
3052 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3053 } else {
3054 rhs_reg = rhs_location.AsRegister<Register>();
3055 }
3056
3057 if (use_imm && rhs_imm == 0) {
3058 switch (cond) {
3059 case kCondEQ:
3060 case kCondBE: // <= 0 if zero
3061 __ Beqz(lhs, label);
3062 break;
3063 case kCondNE:
3064 case kCondA: // > 0 if non-zero
3065 __ Bnez(lhs, label);
3066 break;
3067 case kCondLT:
3068 __ Bltz(lhs, label);
3069 break;
3070 case kCondGE:
3071 __ Bgez(lhs, label);
3072 break;
3073 case kCondLE:
3074 __ Blez(lhs, label);
3075 break;
3076 case kCondGT:
3077 __ Bgtz(lhs, label);
3078 break;
3079 case kCondB: // always false
3080 break;
3081 case kCondAE: // always true
3082 __ B(label);
3083 break;
3084 }
3085 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003086 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3087 if (isR6 || !use_imm) {
3088 if (use_imm) {
3089 rhs_reg = TMP;
3090 __ LoadConst32(rhs_reg, rhs_imm);
3091 }
3092 switch (cond) {
3093 case kCondEQ:
3094 __ Beq(lhs, rhs_reg, label);
3095 break;
3096 case kCondNE:
3097 __ Bne(lhs, rhs_reg, label);
3098 break;
3099 case kCondLT:
3100 __ Blt(lhs, rhs_reg, label);
3101 break;
3102 case kCondGE:
3103 __ Bge(lhs, rhs_reg, label);
3104 break;
3105 case kCondLE:
3106 __ Bge(rhs_reg, lhs, label);
3107 break;
3108 case kCondGT:
3109 __ Blt(rhs_reg, lhs, label);
3110 break;
3111 case kCondB:
3112 __ Bltu(lhs, rhs_reg, label);
3113 break;
3114 case kCondAE:
3115 __ Bgeu(lhs, rhs_reg, label);
3116 break;
3117 case kCondBE:
3118 __ Bgeu(rhs_reg, lhs, label);
3119 break;
3120 case kCondA:
3121 __ Bltu(rhs_reg, lhs, label);
3122 break;
3123 }
3124 } else {
3125 // Special cases for more efficient comparison with constants on R2.
3126 switch (cond) {
3127 case kCondEQ:
3128 __ LoadConst32(TMP, rhs_imm);
3129 __ Beq(lhs, TMP, label);
3130 break;
3131 case kCondNE:
3132 __ LoadConst32(TMP, rhs_imm);
3133 __ Bne(lhs, TMP, label);
3134 break;
3135 case kCondLT:
3136 if (IsInt<16>(rhs_imm)) {
3137 __ Slti(TMP, lhs, rhs_imm);
3138 __ Bnez(TMP, label);
3139 } else {
3140 __ LoadConst32(TMP, rhs_imm);
3141 __ Blt(lhs, TMP, label);
3142 }
3143 break;
3144 case kCondGE:
3145 if (IsInt<16>(rhs_imm)) {
3146 __ Slti(TMP, lhs, rhs_imm);
3147 __ Beqz(TMP, label);
3148 } else {
3149 __ LoadConst32(TMP, rhs_imm);
3150 __ Bge(lhs, TMP, label);
3151 }
3152 break;
3153 case kCondLE:
3154 if (IsInt<16>(rhs_imm + 1)) {
3155 // Simulate lhs <= rhs via lhs < rhs + 1.
3156 __ Slti(TMP, lhs, rhs_imm + 1);
3157 __ Bnez(TMP, label);
3158 } else {
3159 __ LoadConst32(TMP, rhs_imm);
3160 __ Bge(TMP, lhs, label);
3161 }
3162 break;
3163 case kCondGT:
3164 if (IsInt<16>(rhs_imm + 1)) {
3165 // Simulate lhs > rhs via !(lhs < rhs + 1).
3166 __ Slti(TMP, lhs, rhs_imm + 1);
3167 __ Beqz(TMP, label);
3168 } else {
3169 __ LoadConst32(TMP, rhs_imm);
3170 __ Blt(TMP, lhs, label);
3171 }
3172 break;
3173 case kCondB:
3174 if (IsInt<16>(rhs_imm)) {
3175 __ Sltiu(TMP, lhs, rhs_imm);
3176 __ Bnez(TMP, label);
3177 } else {
3178 __ LoadConst32(TMP, rhs_imm);
3179 __ Bltu(lhs, TMP, label);
3180 }
3181 break;
3182 case kCondAE:
3183 if (IsInt<16>(rhs_imm)) {
3184 __ Sltiu(TMP, lhs, rhs_imm);
3185 __ Beqz(TMP, label);
3186 } else {
3187 __ LoadConst32(TMP, rhs_imm);
3188 __ Bgeu(lhs, TMP, label);
3189 }
3190 break;
3191 case kCondBE:
3192 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3193 // Simulate lhs <= rhs via lhs < rhs + 1.
3194 // Note that this only works if rhs + 1 does not overflow
3195 // to 0, hence the check above.
3196 __ Sltiu(TMP, lhs, rhs_imm + 1);
3197 __ Bnez(TMP, label);
3198 } else {
3199 __ LoadConst32(TMP, rhs_imm);
3200 __ Bgeu(TMP, lhs, label);
3201 }
3202 break;
3203 case kCondA:
3204 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3205 // Simulate lhs > rhs via !(lhs < rhs + 1).
3206 // Note that this only works if rhs + 1 does not overflow
3207 // to 0, hence the check above.
3208 __ Sltiu(TMP, lhs, rhs_imm + 1);
3209 __ Beqz(TMP, label);
3210 } else {
3211 __ LoadConst32(TMP, rhs_imm);
3212 __ Bltu(TMP, lhs, label);
3213 }
3214 break;
3215 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003216 }
3217 }
3218}
3219
3220void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3221 LocationSummary* locations,
3222 MipsLabel* label) {
3223 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3224 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3225 Location rhs_location = locations->InAt(1);
3226 Register rhs_high = ZERO;
3227 Register rhs_low = ZERO;
3228 int64_t imm = 0;
3229 uint32_t imm_high = 0;
3230 uint32_t imm_low = 0;
3231 bool use_imm = rhs_location.IsConstant();
3232 if (use_imm) {
3233 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3234 imm_high = High32Bits(imm);
3235 imm_low = Low32Bits(imm);
3236 } else {
3237 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3238 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3239 }
3240
3241 if (use_imm && imm == 0) {
3242 switch (cond) {
3243 case kCondEQ:
3244 case kCondBE: // <= 0 if zero
3245 __ Or(TMP, lhs_high, lhs_low);
3246 __ Beqz(TMP, label);
3247 break;
3248 case kCondNE:
3249 case kCondA: // > 0 if non-zero
3250 __ Or(TMP, lhs_high, lhs_low);
3251 __ Bnez(TMP, label);
3252 break;
3253 case kCondLT:
3254 __ Bltz(lhs_high, label);
3255 break;
3256 case kCondGE:
3257 __ Bgez(lhs_high, label);
3258 break;
3259 case kCondLE:
3260 __ Or(TMP, lhs_high, lhs_low);
3261 __ Sra(AT, lhs_high, 31);
3262 __ Bgeu(AT, TMP, label);
3263 break;
3264 case kCondGT:
3265 __ Or(TMP, lhs_high, lhs_low);
3266 __ Sra(AT, lhs_high, 31);
3267 __ Bltu(AT, TMP, label);
3268 break;
3269 case kCondB: // always false
3270 break;
3271 case kCondAE: // always true
3272 __ B(label);
3273 break;
3274 }
3275 } else if (use_imm) {
3276 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3277 switch (cond) {
3278 case kCondEQ:
3279 __ LoadConst32(TMP, imm_high);
3280 __ Xor(TMP, TMP, lhs_high);
3281 __ LoadConst32(AT, imm_low);
3282 __ Xor(AT, AT, lhs_low);
3283 __ Or(TMP, TMP, AT);
3284 __ Beqz(TMP, label);
3285 break;
3286 case kCondNE:
3287 __ LoadConst32(TMP, imm_high);
3288 __ Xor(TMP, TMP, lhs_high);
3289 __ LoadConst32(AT, imm_low);
3290 __ Xor(AT, AT, lhs_low);
3291 __ Or(TMP, TMP, AT);
3292 __ Bnez(TMP, label);
3293 break;
3294 case kCondLT:
3295 __ LoadConst32(TMP, imm_high);
3296 __ Blt(lhs_high, TMP, label);
3297 __ Slt(TMP, TMP, lhs_high);
3298 __ LoadConst32(AT, imm_low);
3299 __ Sltu(AT, lhs_low, AT);
3300 __ Blt(TMP, AT, label);
3301 break;
3302 case kCondGE:
3303 __ LoadConst32(TMP, imm_high);
3304 __ Blt(TMP, lhs_high, label);
3305 __ Slt(TMP, lhs_high, TMP);
3306 __ LoadConst32(AT, imm_low);
3307 __ Sltu(AT, lhs_low, AT);
3308 __ Or(TMP, TMP, AT);
3309 __ Beqz(TMP, label);
3310 break;
3311 case kCondLE:
3312 __ LoadConst32(TMP, imm_high);
3313 __ Blt(lhs_high, TMP, label);
3314 __ Slt(TMP, TMP, lhs_high);
3315 __ LoadConst32(AT, imm_low);
3316 __ Sltu(AT, AT, lhs_low);
3317 __ Or(TMP, TMP, AT);
3318 __ Beqz(TMP, label);
3319 break;
3320 case kCondGT:
3321 __ LoadConst32(TMP, imm_high);
3322 __ Blt(TMP, lhs_high, label);
3323 __ Slt(TMP, lhs_high, TMP);
3324 __ LoadConst32(AT, imm_low);
3325 __ Sltu(AT, AT, lhs_low);
3326 __ Blt(TMP, AT, label);
3327 break;
3328 case kCondB:
3329 __ LoadConst32(TMP, imm_high);
3330 __ Bltu(lhs_high, TMP, label);
3331 __ Sltu(TMP, TMP, lhs_high);
3332 __ LoadConst32(AT, imm_low);
3333 __ Sltu(AT, lhs_low, AT);
3334 __ Blt(TMP, AT, label);
3335 break;
3336 case kCondAE:
3337 __ LoadConst32(TMP, imm_high);
3338 __ Bltu(TMP, lhs_high, label);
3339 __ Sltu(TMP, lhs_high, TMP);
3340 __ LoadConst32(AT, imm_low);
3341 __ Sltu(AT, lhs_low, AT);
3342 __ Or(TMP, TMP, AT);
3343 __ Beqz(TMP, label);
3344 break;
3345 case kCondBE:
3346 __ LoadConst32(TMP, imm_high);
3347 __ Bltu(lhs_high, TMP, label);
3348 __ Sltu(TMP, TMP, lhs_high);
3349 __ LoadConst32(AT, imm_low);
3350 __ Sltu(AT, AT, lhs_low);
3351 __ Or(TMP, TMP, AT);
3352 __ Beqz(TMP, label);
3353 break;
3354 case kCondA:
3355 __ LoadConst32(TMP, imm_high);
3356 __ Bltu(TMP, lhs_high, label);
3357 __ Sltu(TMP, lhs_high, TMP);
3358 __ LoadConst32(AT, imm_low);
3359 __ Sltu(AT, AT, lhs_low);
3360 __ Blt(TMP, AT, label);
3361 break;
3362 }
3363 } else {
3364 switch (cond) {
3365 case kCondEQ:
3366 __ Xor(TMP, lhs_high, rhs_high);
3367 __ Xor(AT, lhs_low, rhs_low);
3368 __ Or(TMP, TMP, AT);
3369 __ Beqz(TMP, label);
3370 break;
3371 case kCondNE:
3372 __ Xor(TMP, lhs_high, rhs_high);
3373 __ Xor(AT, lhs_low, rhs_low);
3374 __ Or(TMP, TMP, AT);
3375 __ Bnez(TMP, label);
3376 break;
3377 case kCondLT:
3378 __ Blt(lhs_high, rhs_high, label);
3379 __ Slt(TMP, rhs_high, lhs_high);
3380 __ Sltu(AT, lhs_low, rhs_low);
3381 __ Blt(TMP, AT, label);
3382 break;
3383 case kCondGE:
3384 __ Blt(rhs_high, lhs_high, label);
3385 __ Slt(TMP, lhs_high, rhs_high);
3386 __ Sltu(AT, lhs_low, rhs_low);
3387 __ Or(TMP, TMP, AT);
3388 __ Beqz(TMP, label);
3389 break;
3390 case kCondLE:
3391 __ Blt(lhs_high, rhs_high, label);
3392 __ Slt(TMP, rhs_high, lhs_high);
3393 __ Sltu(AT, rhs_low, lhs_low);
3394 __ Or(TMP, TMP, AT);
3395 __ Beqz(TMP, label);
3396 break;
3397 case kCondGT:
3398 __ Blt(rhs_high, lhs_high, label);
3399 __ Slt(TMP, lhs_high, rhs_high);
3400 __ Sltu(AT, rhs_low, lhs_low);
3401 __ Blt(TMP, AT, label);
3402 break;
3403 case kCondB:
3404 __ Bltu(lhs_high, rhs_high, label);
3405 __ Sltu(TMP, rhs_high, lhs_high);
3406 __ Sltu(AT, lhs_low, rhs_low);
3407 __ Blt(TMP, AT, label);
3408 break;
3409 case kCondAE:
3410 __ Bltu(rhs_high, lhs_high, label);
3411 __ Sltu(TMP, lhs_high, rhs_high);
3412 __ Sltu(AT, lhs_low, rhs_low);
3413 __ Or(TMP, TMP, AT);
3414 __ Beqz(TMP, label);
3415 break;
3416 case kCondBE:
3417 __ Bltu(lhs_high, rhs_high, label);
3418 __ Sltu(TMP, rhs_high, lhs_high);
3419 __ Sltu(AT, rhs_low, lhs_low);
3420 __ Or(TMP, TMP, AT);
3421 __ Beqz(TMP, label);
3422 break;
3423 case kCondA:
3424 __ Bltu(rhs_high, lhs_high, label);
3425 __ Sltu(TMP, lhs_high, rhs_high);
3426 __ Sltu(AT, rhs_low, lhs_low);
3427 __ Blt(TMP, AT, label);
3428 break;
3429 }
3430 }
3431}
3432
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003433void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3434 bool gt_bias,
3435 Primitive::Type type,
3436 LocationSummary* locations) {
3437 Register dst = locations->Out().AsRegister<Register>();
3438 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3439 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3440 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3441 if (type == Primitive::kPrimFloat) {
3442 if (isR6) {
3443 switch (cond) {
3444 case kCondEQ:
3445 __ CmpEqS(FTMP, lhs, rhs);
3446 __ Mfc1(dst, FTMP);
3447 __ Andi(dst, dst, 1);
3448 break;
3449 case kCondNE:
3450 __ CmpEqS(FTMP, lhs, rhs);
3451 __ Mfc1(dst, FTMP);
3452 __ Addiu(dst, dst, 1);
3453 break;
3454 case kCondLT:
3455 if (gt_bias) {
3456 __ CmpLtS(FTMP, lhs, rhs);
3457 } else {
3458 __ CmpUltS(FTMP, lhs, rhs);
3459 }
3460 __ Mfc1(dst, FTMP);
3461 __ Andi(dst, dst, 1);
3462 break;
3463 case kCondLE:
3464 if (gt_bias) {
3465 __ CmpLeS(FTMP, lhs, rhs);
3466 } else {
3467 __ CmpUleS(FTMP, lhs, rhs);
3468 }
3469 __ Mfc1(dst, FTMP);
3470 __ Andi(dst, dst, 1);
3471 break;
3472 case kCondGT:
3473 if (gt_bias) {
3474 __ CmpUltS(FTMP, rhs, lhs);
3475 } else {
3476 __ CmpLtS(FTMP, rhs, lhs);
3477 }
3478 __ Mfc1(dst, FTMP);
3479 __ Andi(dst, dst, 1);
3480 break;
3481 case kCondGE:
3482 if (gt_bias) {
3483 __ CmpUleS(FTMP, rhs, lhs);
3484 } else {
3485 __ CmpLeS(FTMP, rhs, lhs);
3486 }
3487 __ Mfc1(dst, FTMP);
3488 __ Andi(dst, dst, 1);
3489 break;
3490 default:
3491 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3492 UNREACHABLE();
3493 }
3494 } else {
3495 switch (cond) {
3496 case kCondEQ:
3497 __ CeqS(0, lhs, rhs);
3498 __ LoadConst32(dst, 1);
3499 __ Movf(dst, ZERO, 0);
3500 break;
3501 case kCondNE:
3502 __ CeqS(0, lhs, rhs);
3503 __ LoadConst32(dst, 1);
3504 __ Movt(dst, ZERO, 0);
3505 break;
3506 case kCondLT:
3507 if (gt_bias) {
3508 __ ColtS(0, lhs, rhs);
3509 } else {
3510 __ CultS(0, lhs, rhs);
3511 }
3512 __ LoadConst32(dst, 1);
3513 __ Movf(dst, ZERO, 0);
3514 break;
3515 case kCondLE:
3516 if (gt_bias) {
3517 __ ColeS(0, lhs, rhs);
3518 } else {
3519 __ CuleS(0, lhs, rhs);
3520 }
3521 __ LoadConst32(dst, 1);
3522 __ Movf(dst, ZERO, 0);
3523 break;
3524 case kCondGT:
3525 if (gt_bias) {
3526 __ CultS(0, rhs, lhs);
3527 } else {
3528 __ ColtS(0, rhs, lhs);
3529 }
3530 __ LoadConst32(dst, 1);
3531 __ Movf(dst, ZERO, 0);
3532 break;
3533 case kCondGE:
3534 if (gt_bias) {
3535 __ CuleS(0, rhs, lhs);
3536 } else {
3537 __ ColeS(0, rhs, lhs);
3538 }
3539 __ LoadConst32(dst, 1);
3540 __ Movf(dst, ZERO, 0);
3541 break;
3542 default:
3543 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3544 UNREACHABLE();
3545 }
3546 }
3547 } else {
3548 DCHECK_EQ(type, Primitive::kPrimDouble);
3549 if (isR6) {
3550 switch (cond) {
3551 case kCondEQ:
3552 __ CmpEqD(FTMP, lhs, rhs);
3553 __ Mfc1(dst, FTMP);
3554 __ Andi(dst, dst, 1);
3555 break;
3556 case kCondNE:
3557 __ CmpEqD(FTMP, lhs, rhs);
3558 __ Mfc1(dst, FTMP);
3559 __ Addiu(dst, dst, 1);
3560 break;
3561 case kCondLT:
3562 if (gt_bias) {
3563 __ CmpLtD(FTMP, lhs, rhs);
3564 } else {
3565 __ CmpUltD(FTMP, lhs, rhs);
3566 }
3567 __ Mfc1(dst, FTMP);
3568 __ Andi(dst, dst, 1);
3569 break;
3570 case kCondLE:
3571 if (gt_bias) {
3572 __ CmpLeD(FTMP, lhs, rhs);
3573 } else {
3574 __ CmpUleD(FTMP, lhs, rhs);
3575 }
3576 __ Mfc1(dst, FTMP);
3577 __ Andi(dst, dst, 1);
3578 break;
3579 case kCondGT:
3580 if (gt_bias) {
3581 __ CmpUltD(FTMP, rhs, lhs);
3582 } else {
3583 __ CmpLtD(FTMP, rhs, lhs);
3584 }
3585 __ Mfc1(dst, FTMP);
3586 __ Andi(dst, dst, 1);
3587 break;
3588 case kCondGE:
3589 if (gt_bias) {
3590 __ CmpUleD(FTMP, rhs, lhs);
3591 } else {
3592 __ CmpLeD(FTMP, rhs, lhs);
3593 }
3594 __ Mfc1(dst, FTMP);
3595 __ Andi(dst, dst, 1);
3596 break;
3597 default:
3598 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3599 UNREACHABLE();
3600 }
3601 } else {
3602 switch (cond) {
3603 case kCondEQ:
3604 __ CeqD(0, lhs, rhs);
3605 __ LoadConst32(dst, 1);
3606 __ Movf(dst, ZERO, 0);
3607 break;
3608 case kCondNE:
3609 __ CeqD(0, lhs, rhs);
3610 __ LoadConst32(dst, 1);
3611 __ Movt(dst, ZERO, 0);
3612 break;
3613 case kCondLT:
3614 if (gt_bias) {
3615 __ ColtD(0, lhs, rhs);
3616 } else {
3617 __ CultD(0, lhs, rhs);
3618 }
3619 __ LoadConst32(dst, 1);
3620 __ Movf(dst, ZERO, 0);
3621 break;
3622 case kCondLE:
3623 if (gt_bias) {
3624 __ ColeD(0, lhs, rhs);
3625 } else {
3626 __ CuleD(0, lhs, rhs);
3627 }
3628 __ LoadConst32(dst, 1);
3629 __ Movf(dst, ZERO, 0);
3630 break;
3631 case kCondGT:
3632 if (gt_bias) {
3633 __ CultD(0, rhs, lhs);
3634 } else {
3635 __ ColtD(0, rhs, lhs);
3636 }
3637 __ LoadConst32(dst, 1);
3638 __ Movf(dst, ZERO, 0);
3639 break;
3640 case kCondGE:
3641 if (gt_bias) {
3642 __ CuleD(0, rhs, lhs);
3643 } else {
3644 __ ColeD(0, rhs, lhs);
3645 }
3646 __ LoadConst32(dst, 1);
3647 __ Movf(dst, ZERO, 0);
3648 break;
3649 default:
3650 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3651 UNREACHABLE();
3652 }
3653 }
3654 }
3655}
3656
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003657bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3658 bool gt_bias,
3659 Primitive::Type type,
3660 LocationSummary* input_locations,
3661 int cc) {
3662 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3663 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3664 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3665 if (type == Primitive::kPrimFloat) {
3666 switch (cond) {
3667 case kCondEQ:
3668 __ CeqS(cc, lhs, rhs);
3669 return false;
3670 case kCondNE:
3671 __ CeqS(cc, lhs, rhs);
3672 return true;
3673 case kCondLT:
3674 if (gt_bias) {
3675 __ ColtS(cc, lhs, rhs);
3676 } else {
3677 __ CultS(cc, lhs, rhs);
3678 }
3679 return false;
3680 case kCondLE:
3681 if (gt_bias) {
3682 __ ColeS(cc, lhs, rhs);
3683 } else {
3684 __ CuleS(cc, lhs, rhs);
3685 }
3686 return false;
3687 case kCondGT:
3688 if (gt_bias) {
3689 __ CultS(cc, rhs, lhs);
3690 } else {
3691 __ ColtS(cc, rhs, lhs);
3692 }
3693 return false;
3694 case kCondGE:
3695 if (gt_bias) {
3696 __ CuleS(cc, rhs, lhs);
3697 } else {
3698 __ ColeS(cc, rhs, lhs);
3699 }
3700 return false;
3701 default:
3702 LOG(FATAL) << "Unexpected non-floating-point condition";
3703 UNREACHABLE();
3704 }
3705 } else {
3706 DCHECK_EQ(type, Primitive::kPrimDouble);
3707 switch (cond) {
3708 case kCondEQ:
3709 __ CeqD(cc, lhs, rhs);
3710 return false;
3711 case kCondNE:
3712 __ CeqD(cc, lhs, rhs);
3713 return true;
3714 case kCondLT:
3715 if (gt_bias) {
3716 __ ColtD(cc, lhs, rhs);
3717 } else {
3718 __ CultD(cc, lhs, rhs);
3719 }
3720 return false;
3721 case kCondLE:
3722 if (gt_bias) {
3723 __ ColeD(cc, lhs, rhs);
3724 } else {
3725 __ CuleD(cc, lhs, rhs);
3726 }
3727 return false;
3728 case kCondGT:
3729 if (gt_bias) {
3730 __ CultD(cc, rhs, lhs);
3731 } else {
3732 __ ColtD(cc, rhs, lhs);
3733 }
3734 return false;
3735 case kCondGE:
3736 if (gt_bias) {
3737 __ CuleD(cc, rhs, lhs);
3738 } else {
3739 __ ColeD(cc, rhs, lhs);
3740 }
3741 return false;
3742 default:
3743 LOG(FATAL) << "Unexpected non-floating-point condition";
3744 UNREACHABLE();
3745 }
3746 }
3747}
3748
3749bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
3750 bool gt_bias,
3751 Primitive::Type type,
3752 LocationSummary* input_locations,
3753 FRegister dst) {
3754 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3755 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3756 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
3757 if (type == Primitive::kPrimFloat) {
3758 switch (cond) {
3759 case kCondEQ:
3760 __ CmpEqS(dst, lhs, rhs);
3761 return false;
3762 case kCondNE:
3763 __ CmpEqS(dst, lhs, rhs);
3764 return true;
3765 case kCondLT:
3766 if (gt_bias) {
3767 __ CmpLtS(dst, lhs, rhs);
3768 } else {
3769 __ CmpUltS(dst, lhs, rhs);
3770 }
3771 return false;
3772 case kCondLE:
3773 if (gt_bias) {
3774 __ CmpLeS(dst, lhs, rhs);
3775 } else {
3776 __ CmpUleS(dst, lhs, rhs);
3777 }
3778 return false;
3779 case kCondGT:
3780 if (gt_bias) {
3781 __ CmpUltS(dst, rhs, lhs);
3782 } else {
3783 __ CmpLtS(dst, rhs, lhs);
3784 }
3785 return false;
3786 case kCondGE:
3787 if (gt_bias) {
3788 __ CmpUleS(dst, rhs, lhs);
3789 } else {
3790 __ CmpLeS(dst, rhs, lhs);
3791 }
3792 return false;
3793 default:
3794 LOG(FATAL) << "Unexpected non-floating-point condition";
3795 UNREACHABLE();
3796 }
3797 } else {
3798 DCHECK_EQ(type, Primitive::kPrimDouble);
3799 switch (cond) {
3800 case kCondEQ:
3801 __ CmpEqD(dst, lhs, rhs);
3802 return false;
3803 case kCondNE:
3804 __ CmpEqD(dst, lhs, rhs);
3805 return true;
3806 case kCondLT:
3807 if (gt_bias) {
3808 __ CmpLtD(dst, lhs, rhs);
3809 } else {
3810 __ CmpUltD(dst, lhs, rhs);
3811 }
3812 return false;
3813 case kCondLE:
3814 if (gt_bias) {
3815 __ CmpLeD(dst, lhs, rhs);
3816 } else {
3817 __ CmpUleD(dst, lhs, rhs);
3818 }
3819 return false;
3820 case kCondGT:
3821 if (gt_bias) {
3822 __ CmpUltD(dst, rhs, lhs);
3823 } else {
3824 __ CmpLtD(dst, rhs, lhs);
3825 }
3826 return false;
3827 case kCondGE:
3828 if (gt_bias) {
3829 __ CmpUleD(dst, rhs, lhs);
3830 } else {
3831 __ CmpLeD(dst, rhs, lhs);
3832 }
3833 return false;
3834 default:
3835 LOG(FATAL) << "Unexpected non-floating-point condition";
3836 UNREACHABLE();
3837 }
3838 }
3839}
3840
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003841void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3842 bool gt_bias,
3843 Primitive::Type type,
3844 LocationSummary* locations,
3845 MipsLabel* label) {
3846 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3847 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3848 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3849 if (type == Primitive::kPrimFloat) {
3850 if (isR6) {
3851 switch (cond) {
3852 case kCondEQ:
3853 __ CmpEqS(FTMP, lhs, rhs);
3854 __ Bc1nez(FTMP, label);
3855 break;
3856 case kCondNE:
3857 __ CmpEqS(FTMP, lhs, rhs);
3858 __ Bc1eqz(FTMP, label);
3859 break;
3860 case kCondLT:
3861 if (gt_bias) {
3862 __ CmpLtS(FTMP, lhs, rhs);
3863 } else {
3864 __ CmpUltS(FTMP, lhs, rhs);
3865 }
3866 __ Bc1nez(FTMP, label);
3867 break;
3868 case kCondLE:
3869 if (gt_bias) {
3870 __ CmpLeS(FTMP, lhs, rhs);
3871 } else {
3872 __ CmpUleS(FTMP, lhs, rhs);
3873 }
3874 __ Bc1nez(FTMP, label);
3875 break;
3876 case kCondGT:
3877 if (gt_bias) {
3878 __ CmpUltS(FTMP, rhs, lhs);
3879 } else {
3880 __ CmpLtS(FTMP, rhs, lhs);
3881 }
3882 __ Bc1nez(FTMP, label);
3883 break;
3884 case kCondGE:
3885 if (gt_bias) {
3886 __ CmpUleS(FTMP, rhs, lhs);
3887 } else {
3888 __ CmpLeS(FTMP, rhs, lhs);
3889 }
3890 __ Bc1nez(FTMP, label);
3891 break;
3892 default:
3893 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003894 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003895 }
3896 } else {
3897 switch (cond) {
3898 case kCondEQ:
3899 __ CeqS(0, lhs, rhs);
3900 __ Bc1t(0, label);
3901 break;
3902 case kCondNE:
3903 __ CeqS(0, lhs, rhs);
3904 __ Bc1f(0, label);
3905 break;
3906 case kCondLT:
3907 if (gt_bias) {
3908 __ ColtS(0, lhs, rhs);
3909 } else {
3910 __ CultS(0, lhs, rhs);
3911 }
3912 __ Bc1t(0, label);
3913 break;
3914 case kCondLE:
3915 if (gt_bias) {
3916 __ ColeS(0, lhs, rhs);
3917 } else {
3918 __ CuleS(0, lhs, rhs);
3919 }
3920 __ Bc1t(0, label);
3921 break;
3922 case kCondGT:
3923 if (gt_bias) {
3924 __ CultS(0, rhs, lhs);
3925 } else {
3926 __ ColtS(0, rhs, lhs);
3927 }
3928 __ Bc1t(0, label);
3929 break;
3930 case kCondGE:
3931 if (gt_bias) {
3932 __ CuleS(0, rhs, lhs);
3933 } else {
3934 __ ColeS(0, rhs, lhs);
3935 }
3936 __ Bc1t(0, label);
3937 break;
3938 default:
3939 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003940 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003941 }
3942 }
3943 } else {
3944 DCHECK_EQ(type, Primitive::kPrimDouble);
3945 if (isR6) {
3946 switch (cond) {
3947 case kCondEQ:
3948 __ CmpEqD(FTMP, lhs, rhs);
3949 __ Bc1nez(FTMP, label);
3950 break;
3951 case kCondNE:
3952 __ CmpEqD(FTMP, lhs, rhs);
3953 __ Bc1eqz(FTMP, label);
3954 break;
3955 case kCondLT:
3956 if (gt_bias) {
3957 __ CmpLtD(FTMP, lhs, rhs);
3958 } else {
3959 __ CmpUltD(FTMP, lhs, rhs);
3960 }
3961 __ Bc1nez(FTMP, label);
3962 break;
3963 case kCondLE:
3964 if (gt_bias) {
3965 __ CmpLeD(FTMP, lhs, rhs);
3966 } else {
3967 __ CmpUleD(FTMP, lhs, rhs);
3968 }
3969 __ Bc1nez(FTMP, label);
3970 break;
3971 case kCondGT:
3972 if (gt_bias) {
3973 __ CmpUltD(FTMP, rhs, lhs);
3974 } else {
3975 __ CmpLtD(FTMP, rhs, lhs);
3976 }
3977 __ Bc1nez(FTMP, label);
3978 break;
3979 case kCondGE:
3980 if (gt_bias) {
3981 __ CmpUleD(FTMP, rhs, lhs);
3982 } else {
3983 __ CmpLeD(FTMP, rhs, lhs);
3984 }
3985 __ Bc1nez(FTMP, label);
3986 break;
3987 default:
3988 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003989 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003990 }
3991 } else {
3992 switch (cond) {
3993 case kCondEQ:
3994 __ CeqD(0, lhs, rhs);
3995 __ Bc1t(0, label);
3996 break;
3997 case kCondNE:
3998 __ CeqD(0, lhs, rhs);
3999 __ Bc1f(0, label);
4000 break;
4001 case kCondLT:
4002 if (gt_bias) {
4003 __ ColtD(0, lhs, rhs);
4004 } else {
4005 __ CultD(0, lhs, rhs);
4006 }
4007 __ Bc1t(0, label);
4008 break;
4009 case kCondLE:
4010 if (gt_bias) {
4011 __ ColeD(0, lhs, rhs);
4012 } else {
4013 __ CuleD(0, lhs, rhs);
4014 }
4015 __ Bc1t(0, label);
4016 break;
4017 case kCondGT:
4018 if (gt_bias) {
4019 __ CultD(0, rhs, lhs);
4020 } else {
4021 __ ColtD(0, rhs, lhs);
4022 }
4023 __ Bc1t(0, label);
4024 break;
4025 case kCondGE:
4026 if (gt_bias) {
4027 __ CuleD(0, rhs, lhs);
4028 } else {
4029 __ ColeD(0, rhs, lhs);
4030 }
4031 __ Bc1t(0, label);
4032 break;
4033 default:
4034 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004035 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004036 }
4037 }
4038 }
4039}
4040
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004041void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004042 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004043 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004044 MipsLabel* false_target) {
4045 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004046
David Brazdil0debae72015-11-12 18:37:00 +00004047 if (true_target == nullptr && false_target == nullptr) {
4048 // Nothing to do. The code always falls through.
4049 return;
4050 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004051 // Constant condition, statically compared against "true" (integer value 1).
4052 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004053 if (true_target != nullptr) {
4054 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004055 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004056 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004057 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004058 if (false_target != nullptr) {
4059 __ B(false_target);
4060 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004061 }
David Brazdil0debae72015-11-12 18:37:00 +00004062 return;
4063 }
4064
4065 // The following code generates these patterns:
4066 // (1) true_target == nullptr && false_target != nullptr
4067 // - opposite condition true => branch to false_target
4068 // (2) true_target != nullptr && false_target == nullptr
4069 // - condition true => branch to true_target
4070 // (3) true_target != nullptr && false_target != nullptr
4071 // - condition true => branch to true_target
4072 // - branch to false_target
4073 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004074 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004075 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004076 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004077 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004078 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4079 } else {
4080 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4081 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004082 } else {
4083 // The condition instruction has not been materialized, use its inputs as
4084 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004085 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004086 Primitive::Type type = condition->InputAt(0)->GetType();
4087 LocationSummary* locations = cond->GetLocations();
4088 IfCondition if_cond = condition->GetCondition();
4089 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004090
David Brazdil0debae72015-11-12 18:37:00 +00004091 if (true_target == nullptr) {
4092 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004093 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004094 }
4095
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004096 switch (type) {
4097 default:
4098 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4099 break;
4100 case Primitive::kPrimLong:
4101 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4102 break;
4103 case Primitive::kPrimFloat:
4104 case Primitive::kPrimDouble:
4105 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4106 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004107 }
4108 }
David Brazdil0debae72015-11-12 18:37:00 +00004109
4110 // If neither branch falls through (case 3), the conditional branch to `true_target`
4111 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4112 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004113 __ B(false_target);
4114 }
4115}
4116
4117void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4118 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004119 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004120 locations->SetInAt(0, Location::RequiresRegister());
4121 }
4122}
4123
4124void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004125 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4126 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4127 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4128 nullptr : codegen_->GetLabelOf(true_successor);
4129 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4130 nullptr : codegen_->GetLabelOf(false_successor);
4131 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004132}
4133
4134void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4135 LocationSummary* locations = new (GetGraph()->GetArena())
4136 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004137 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004138 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004139 locations->SetInAt(0, Location::RequiresRegister());
4140 }
4141}
4142
4143void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004144 SlowPathCodeMIPS* slow_path =
4145 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004146 GenerateTestAndBranch(deoptimize,
4147 /* condition_input_index */ 0,
4148 slow_path->GetEntryLabel(),
4149 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004150}
4151
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004152// This function returns true if a conditional move can be generated for HSelect.
4153// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4154// branches and regular moves.
4155//
4156// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4157//
4158// While determining feasibility of a conditional move and setting inputs/outputs
4159// are two distinct tasks, this function does both because they share quite a bit
4160// of common logic.
4161static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4162 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4163 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4164 HCondition* condition = cond->AsCondition();
4165
4166 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4167 Primitive::Type dst_type = select->GetType();
4168
4169 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4170 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4171 bool is_true_value_zero_constant =
4172 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4173 bool is_false_value_zero_constant =
4174 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4175
4176 bool can_move_conditionally = false;
4177 bool use_const_for_false_in = false;
4178 bool use_const_for_true_in = false;
4179
4180 if (!cond->IsConstant()) {
4181 switch (cond_type) {
4182 default:
4183 switch (dst_type) {
4184 default:
4185 // Moving int on int condition.
4186 if (is_r6) {
4187 if (is_true_value_zero_constant) {
4188 // seleqz out_reg, false_reg, cond_reg
4189 can_move_conditionally = true;
4190 use_const_for_true_in = true;
4191 } else if (is_false_value_zero_constant) {
4192 // selnez out_reg, true_reg, cond_reg
4193 can_move_conditionally = true;
4194 use_const_for_false_in = true;
4195 } else if (materialized) {
4196 // Not materializing unmaterialized int conditions
4197 // to keep the instruction count low.
4198 // selnez AT, true_reg, cond_reg
4199 // seleqz TMP, false_reg, cond_reg
4200 // or out_reg, AT, TMP
4201 can_move_conditionally = true;
4202 }
4203 } else {
4204 // movn out_reg, true_reg/ZERO, cond_reg
4205 can_move_conditionally = true;
4206 use_const_for_true_in = is_true_value_zero_constant;
4207 }
4208 break;
4209 case Primitive::kPrimLong:
4210 // Moving long on int condition.
4211 if (is_r6) {
4212 if (is_true_value_zero_constant) {
4213 // seleqz out_reg_lo, false_reg_lo, cond_reg
4214 // seleqz out_reg_hi, false_reg_hi, cond_reg
4215 can_move_conditionally = true;
4216 use_const_for_true_in = true;
4217 } else if (is_false_value_zero_constant) {
4218 // selnez out_reg_lo, true_reg_lo, cond_reg
4219 // selnez out_reg_hi, true_reg_hi, cond_reg
4220 can_move_conditionally = true;
4221 use_const_for_false_in = true;
4222 }
4223 // Other long conditional moves would generate 6+ instructions,
4224 // which is too many.
4225 } else {
4226 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4227 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4228 can_move_conditionally = true;
4229 use_const_for_true_in = is_true_value_zero_constant;
4230 }
4231 break;
4232 case Primitive::kPrimFloat:
4233 case Primitive::kPrimDouble:
4234 // Moving float/double on int condition.
4235 if (is_r6) {
4236 if (materialized) {
4237 // Not materializing unmaterialized int conditions
4238 // to keep the instruction count low.
4239 can_move_conditionally = true;
4240 if (is_true_value_zero_constant) {
4241 // sltu TMP, ZERO, cond_reg
4242 // mtc1 TMP, temp_cond_reg
4243 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4244 use_const_for_true_in = true;
4245 } else if (is_false_value_zero_constant) {
4246 // sltu TMP, ZERO, cond_reg
4247 // mtc1 TMP, temp_cond_reg
4248 // selnez.fmt out_reg, true_reg, temp_cond_reg
4249 use_const_for_false_in = true;
4250 } else {
4251 // sltu TMP, ZERO, cond_reg
4252 // mtc1 TMP, temp_cond_reg
4253 // sel.fmt temp_cond_reg, false_reg, true_reg
4254 // mov.fmt out_reg, temp_cond_reg
4255 }
4256 }
4257 } else {
4258 // movn.fmt out_reg, true_reg, cond_reg
4259 can_move_conditionally = true;
4260 }
4261 break;
4262 }
4263 break;
4264 case Primitive::kPrimLong:
4265 // We don't materialize long comparison now
4266 // and use conditional branches instead.
4267 break;
4268 case Primitive::kPrimFloat:
4269 case Primitive::kPrimDouble:
4270 switch (dst_type) {
4271 default:
4272 // Moving int on float/double condition.
4273 if (is_r6) {
4274 if (is_true_value_zero_constant) {
4275 // mfc1 TMP, temp_cond_reg
4276 // seleqz out_reg, false_reg, TMP
4277 can_move_conditionally = true;
4278 use_const_for_true_in = true;
4279 } else if (is_false_value_zero_constant) {
4280 // mfc1 TMP, temp_cond_reg
4281 // selnez out_reg, true_reg, TMP
4282 can_move_conditionally = true;
4283 use_const_for_false_in = true;
4284 } else {
4285 // mfc1 TMP, temp_cond_reg
4286 // selnez AT, true_reg, TMP
4287 // seleqz TMP, false_reg, TMP
4288 // or out_reg, AT, TMP
4289 can_move_conditionally = true;
4290 }
4291 } else {
4292 // movt out_reg, true_reg/ZERO, cc
4293 can_move_conditionally = true;
4294 use_const_for_true_in = is_true_value_zero_constant;
4295 }
4296 break;
4297 case Primitive::kPrimLong:
4298 // Moving long on float/double condition.
4299 if (is_r6) {
4300 if (is_true_value_zero_constant) {
4301 // mfc1 TMP, temp_cond_reg
4302 // seleqz out_reg_lo, false_reg_lo, TMP
4303 // seleqz out_reg_hi, false_reg_hi, TMP
4304 can_move_conditionally = true;
4305 use_const_for_true_in = true;
4306 } else if (is_false_value_zero_constant) {
4307 // mfc1 TMP, temp_cond_reg
4308 // selnez out_reg_lo, true_reg_lo, TMP
4309 // selnez out_reg_hi, true_reg_hi, TMP
4310 can_move_conditionally = true;
4311 use_const_for_false_in = true;
4312 }
4313 // Other long conditional moves would generate 6+ instructions,
4314 // which is too many.
4315 } else {
4316 // movt out_reg_lo, true_reg_lo/ZERO, cc
4317 // movt out_reg_hi, true_reg_hi/ZERO, cc
4318 can_move_conditionally = true;
4319 use_const_for_true_in = is_true_value_zero_constant;
4320 }
4321 break;
4322 case Primitive::kPrimFloat:
4323 case Primitive::kPrimDouble:
4324 // Moving float/double on float/double condition.
4325 if (is_r6) {
4326 can_move_conditionally = true;
4327 if (is_true_value_zero_constant) {
4328 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4329 use_const_for_true_in = true;
4330 } else if (is_false_value_zero_constant) {
4331 // selnez.fmt out_reg, true_reg, temp_cond_reg
4332 use_const_for_false_in = true;
4333 } else {
4334 // sel.fmt temp_cond_reg, false_reg, true_reg
4335 // mov.fmt out_reg, temp_cond_reg
4336 }
4337 } else {
4338 // movt.fmt out_reg, true_reg, cc
4339 can_move_conditionally = true;
4340 }
4341 break;
4342 }
4343 break;
4344 }
4345 }
4346
4347 if (can_move_conditionally) {
4348 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4349 } else {
4350 DCHECK(!use_const_for_false_in);
4351 DCHECK(!use_const_for_true_in);
4352 }
4353
4354 if (locations_to_set != nullptr) {
4355 if (use_const_for_false_in) {
4356 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4357 } else {
4358 locations_to_set->SetInAt(0,
4359 Primitive::IsFloatingPointType(dst_type)
4360 ? Location::RequiresFpuRegister()
4361 : Location::RequiresRegister());
4362 }
4363 if (use_const_for_true_in) {
4364 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4365 } else {
4366 locations_to_set->SetInAt(1,
4367 Primitive::IsFloatingPointType(dst_type)
4368 ? Location::RequiresFpuRegister()
4369 : Location::RequiresRegister());
4370 }
4371 if (materialized) {
4372 locations_to_set->SetInAt(2, Location::RequiresRegister());
4373 }
4374 // On R6 we don't require the output to be the same as the
4375 // first input for conditional moves unlike on R2.
4376 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4377 if (is_out_same_as_first_in) {
4378 locations_to_set->SetOut(Location::SameAsFirstInput());
4379 } else {
4380 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4381 ? Location::RequiresFpuRegister()
4382 : Location::RequiresRegister());
4383 }
4384 }
4385
4386 return can_move_conditionally;
4387}
4388
4389void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4390 LocationSummary* locations = select->GetLocations();
4391 Location dst = locations->Out();
4392 Location src = locations->InAt(1);
4393 Register src_reg = ZERO;
4394 Register src_reg_high = ZERO;
4395 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4396 Register cond_reg = TMP;
4397 int cond_cc = 0;
4398 Primitive::Type cond_type = Primitive::kPrimInt;
4399 bool cond_inverted = false;
4400 Primitive::Type dst_type = select->GetType();
4401
4402 if (IsBooleanValueOrMaterializedCondition(cond)) {
4403 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4404 } else {
4405 HCondition* condition = cond->AsCondition();
4406 LocationSummary* cond_locations = cond->GetLocations();
4407 IfCondition if_cond = condition->GetCondition();
4408 cond_type = condition->InputAt(0)->GetType();
4409 switch (cond_type) {
4410 default:
4411 DCHECK_NE(cond_type, Primitive::kPrimLong);
4412 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4413 break;
4414 case Primitive::kPrimFloat:
4415 case Primitive::kPrimDouble:
4416 cond_inverted = MaterializeFpCompareR2(if_cond,
4417 condition->IsGtBias(),
4418 cond_type,
4419 cond_locations,
4420 cond_cc);
4421 break;
4422 }
4423 }
4424
4425 DCHECK(dst.Equals(locations->InAt(0)));
4426 if (src.IsRegister()) {
4427 src_reg = src.AsRegister<Register>();
4428 } else if (src.IsRegisterPair()) {
4429 src_reg = src.AsRegisterPairLow<Register>();
4430 src_reg_high = src.AsRegisterPairHigh<Register>();
4431 } else if (src.IsConstant()) {
4432 DCHECK(src.GetConstant()->IsZeroBitPattern());
4433 }
4434
4435 switch (cond_type) {
4436 default:
4437 switch (dst_type) {
4438 default:
4439 if (cond_inverted) {
4440 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4441 } else {
4442 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4443 }
4444 break;
4445 case Primitive::kPrimLong:
4446 if (cond_inverted) {
4447 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4448 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4449 } else {
4450 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4451 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4452 }
4453 break;
4454 case Primitive::kPrimFloat:
4455 if (cond_inverted) {
4456 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4457 } else {
4458 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4459 }
4460 break;
4461 case Primitive::kPrimDouble:
4462 if (cond_inverted) {
4463 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4464 } else {
4465 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4466 }
4467 break;
4468 }
4469 break;
4470 case Primitive::kPrimLong:
4471 LOG(FATAL) << "Unreachable";
4472 UNREACHABLE();
4473 case Primitive::kPrimFloat:
4474 case Primitive::kPrimDouble:
4475 switch (dst_type) {
4476 default:
4477 if (cond_inverted) {
4478 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4479 } else {
4480 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4481 }
4482 break;
4483 case Primitive::kPrimLong:
4484 if (cond_inverted) {
4485 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4486 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4487 } else {
4488 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4489 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4490 }
4491 break;
4492 case Primitive::kPrimFloat:
4493 if (cond_inverted) {
4494 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4495 } else {
4496 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4497 }
4498 break;
4499 case Primitive::kPrimDouble:
4500 if (cond_inverted) {
4501 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4502 } else {
4503 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4504 }
4505 break;
4506 }
4507 break;
4508 }
4509}
4510
4511void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4512 LocationSummary* locations = select->GetLocations();
4513 Location dst = locations->Out();
4514 Location false_src = locations->InAt(0);
4515 Location true_src = locations->InAt(1);
4516 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4517 Register cond_reg = TMP;
4518 FRegister fcond_reg = FTMP;
4519 Primitive::Type cond_type = Primitive::kPrimInt;
4520 bool cond_inverted = false;
4521 Primitive::Type dst_type = select->GetType();
4522
4523 if (IsBooleanValueOrMaterializedCondition(cond)) {
4524 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4525 } else {
4526 HCondition* condition = cond->AsCondition();
4527 LocationSummary* cond_locations = cond->GetLocations();
4528 IfCondition if_cond = condition->GetCondition();
4529 cond_type = condition->InputAt(0)->GetType();
4530 switch (cond_type) {
4531 default:
4532 DCHECK_NE(cond_type, Primitive::kPrimLong);
4533 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4534 break;
4535 case Primitive::kPrimFloat:
4536 case Primitive::kPrimDouble:
4537 cond_inverted = MaterializeFpCompareR6(if_cond,
4538 condition->IsGtBias(),
4539 cond_type,
4540 cond_locations,
4541 fcond_reg);
4542 break;
4543 }
4544 }
4545
4546 if (true_src.IsConstant()) {
4547 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4548 }
4549 if (false_src.IsConstant()) {
4550 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4551 }
4552
4553 switch (dst_type) {
4554 default:
4555 if (Primitive::IsFloatingPointType(cond_type)) {
4556 __ Mfc1(cond_reg, fcond_reg);
4557 }
4558 if (true_src.IsConstant()) {
4559 if (cond_inverted) {
4560 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4561 } else {
4562 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4563 }
4564 } else if (false_src.IsConstant()) {
4565 if (cond_inverted) {
4566 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4567 } else {
4568 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4569 }
4570 } else {
4571 DCHECK_NE(cond_reg, AT);
4572 if (cond_inverted) {
4573 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4574 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4575 } else {
4576 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4577 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4578 }
4579 __ Or(dst.AsRegister<Register>(), AT, TMP);
4580 }
4581 break;
4582 case Primitive::kPrimLong: {
4583 if (Primitive::IsFloatingPointType(cond_type)) {
4584 __ Mfc1(cond_reg, fcond_reg);
4585 }
4586 Register dst_lo = dst.AsRegisterPairLow<Register>();
4587 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4588 if (true_src.IsConstant()) {
4589 Register src_lo = false_src.AsRegisterPairLow<Register>();
4590 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4591 if (cond_inverted) {
4592 __ Selnez(dst_lo, src_lo, cond_reg);
4593 __ Selnez(dst_hi, src_hi, cond_reg);
4594 } else {
4595 __ Seleqz(dst_lo, src_lo, cond_reg);
4596 __ Seleqz(dst_hi, src_hi, cond_reg);
4597 }
4598 } else {
4599 DCHECK(false_src.IsConstant());
4600 Register src_lo = true_src.AsRegisterPairLow<Register>();
4601 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4602 if (cond_inverted) {
4603 __ Seleqz(dst_lo, src_lo, cond_reg);
4604 __ Seleqz(dst_hi, src_hi, cond_reg);
4605 } else {
4606 __ Selnez(dst_lo, src_lo, cond_reg);
4607 __ Selnez(dst_hi, src_hi, cond_reg);
4608 }
4609 }
4610 break;
4611 }
4612 case Primitive::kPrimFloat: {
4613 if (!Primitive::IsFloatingPointType(cond_type)) {
4614 // sel*.fmt tests bit 0 of the condition register, account for that.
4615 __ Sltu(TMP, ZERO, cond_reg);
4616 __ Mtc1(TMP, fcond_reg);
4617 }
4618 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4619 if (true_src.IsConstant()) {
4620 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4621 if (cond_inverted) {
4622 __ SelnezS(dst_reg, src_reg, fcond_reg);
4623 } else {
4624 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4625 }
4626 } else if (false_src.IsConstant()) {
4627 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4628 if (cond_inverted) {
4629 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4630 } else {
4631 __ SelnezS(dst_reg, src_reg, fcond_reg);
4632 }
4633 } else {
4634 if (cond_inverted) {
4635 __ SelS(fcond_reg,
4636 true_src.AsFpuRegister<FRegister>(),
4637 false_src.AsFpuRegister<FRegister>());
4638 } else {
4639 __ SelS(fcond_reg,
4640 false_src.AsFpuRegister<FRegister>(),
4641 true_src.AsFpuRegister<FRegister>());
4642 }
4643 __ MovS(dst_reg, fcond_reg);
4644 }
4645 break;
4646 }
4647 case Primitive::kPrimDouble: {
4648 if (!Primitive::IsFloatingPointType(cond_type)) {
4649 // sel*.fmt tests bit 0 of the condition register, account for that.
4650 __ Sltu(TMP, ZERO, cond_reg);
4651 __ Mtc1(TMP, fcond_reg);
4652 }
4653 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4654 if (true_src.IsConstant()) {
4655 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4656 if (cond_inverted) {
4657 __ SelnezD(dst_reg, src_reg, fcond_reg);
4658 } else {
4659 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4660 }
4661 } else if (false_src.IsConstant()) {
4662 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4663 if (cond_inverted) {
4664 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4665 } else {
4666 __ SelnezD(dst_reg, src_reg, fcond_reg);
4667 }
4668 } else {
4669 if (cond_inverted) {
4670 __ SelD(fcond_reg,
4671 true_src.AsFpuRegister<FRegister>(),
4672 false_src.AsFpuRegister<FRegister>());
4673 } else {
4674 __ SelD(fcond_reg,
4675 false_src.AsFpuRegister<FRegister>(),
4676 true_src.AsFpuRegister<FRegister>());
4677 }
4678 __ MovD(dst_reg, fcond_reg);
4679 }
4680 break;
4681 }
4682 }
4683}
4684
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004685void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4686 LocationSummary* locations = new (GetGraph()->GetArena())
4687 LocationSummary(flag, LocationSummary::kNoCall);
4688 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004689}
4690
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004691void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4692 __ LoadFromOffset(kLoadWord,
4693 flag->GetLocations()->Out().AsRegister<Register>(),
4694 SP,
4695 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004696}
4697
David Brazdil74eb1b22015-12-14 11:44:01 +00004698void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4699 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004700 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004701}
4702
4703void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004704 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
4705 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
4706 if (is_r6) {
4707 GenConditionalMoveR6(select);
4708 } else {
4709 GenConditionalMoveR2(select);
4710 }
4711 } else {
4712 LocationSummary* locations = select->GetLocations();
4713 MipsLabel false_target;
4714 GenerateTestAndBranch(select,
4715 /* condition_input_index */ 2,
4716 /* true_target */ nullptr,
4717 &false_target);
4718 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4719 __ Bind(&false_target);
4720 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004721}
4722
David Srbecky0cf44932015-12-09 14:09:59 +00004723void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4724 new (GetGraph()->GetArena()) LocationSummary(info);
4725}
4726
David Srbeckyd28f4a02016-03-14 17:14:24 +00004727void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
4728 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004729}
4730
4731void CodeGeneratorMIPS::GenerateNop() {
4732 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004733}
4734
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004735void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4736 Primitive::Type field_type = field_info.GetFieldType();
4737 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4738 bool generate_volatile = field_info.IsVolatile() && is_wide;
4739 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004740 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004741
4742 locations->SetInAt(0, Location::RequiresRegister());
4743 if (generate_volatile) {
4744 InvokeRuntimeCallingConvention calling_convention;
4745 // need A0 to hold base + offset
4746 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4747 if (field_type == Primitive::kPrimLong) {
4748 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
4749 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004750 // Use Location::Any() to prevent situations when running out of available fp registers.
4751 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004752 // Need some temp core regs since FP results are returned in core registers
4753 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
4754 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
4755 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
4756 }
4757 } else {
4758 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4759 locations->SetOut(Location::RequiresFpuRegister());
4760 } else {
4761 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4762 }
4763 }
4764}
4765
4766void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
4767 const FieldInfo& field_info,
4768 uint32_t dex_pc) {
4769 Primitive::Type type = field_info.GetFieldType();
4770 LocationSummary* locations = instruction->GetLocations();
4771 Register obj = locations->InAt(0).AsRegister<Register>();
4772 LoadOperandType load_type = kLoadUnsignedByte;
4773 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004774 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004775 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004776
4777 switch (type) {
4778 case Primitive::kPrimBoolean:
4779 load_type = kLoadUnsignedByte;
4780 break;
4781 case Primitive::kPrimByte:
4782 load_type = kLoadSignedByte;
4783 break;
4784 case Primitive::kPrimShort:
4785 load_type = kLoadSignedHalfword;
4786 break;
4787 case Primitive::kPrimChar:
4788 load_type = kLoadUnsignedHalfword;
4789 break;
4790 case Primitive::kPrimInt:
4791 case Primitive::kPrimFloat:
4792 case Primitive::kPrimNot:
4793 load_type = kLoadWord;
4794 break;
4795 case Primitive::kPrimLong:
4796 case Primitive::kPrimDouble:
4797 load_type = kLoadDoubleword;
4798 break;
4799 case Primitive::kPrimVoid:
4800 LOG(FATAL) << "Unreachable type " << type;
4801 UNREACHABLE();
4802 }
4803
4804 if (is_volatile && load_type == kLoadDoubleword) {
4805 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004806 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004807 // Do implicit Null check
4808 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4809 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01004810 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004811 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
4812 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004813 // FP results are returned in core registers. Need to move them.
4814 Location out = locations->Out();
4815 if (out.IsFpuRegister()) {
4816 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4817 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4818 out.AsFpuRegister<FRegister>());
4819 } else {
4820 DCHECK(out.IsDoubleStackSlot());
4821 __ StoreToOffset(kStoreWord,
4822 locations->GetTemp(1).AsRegister<Register>(),
4823 SP,
4824 out.GetStackIndex());
4825 __ StoreToOffset(kStoreWord,
4826 locations->GetTemp(2).AsRegister<Register>(),
4827 SP,
4828 out.GetStackIndex() + 4);
4829 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004830 }
4831 } else {
4832 if (!Primitive::IsFloatingPointType(type)) {
4833 Register dst;
4834 if (type == Primitive::kPrimLong) {
4835 DCHECK(locations->Out().IsRegisterPair());
4836 dst = locations->Out().AsRegisterPairLow<Register>();
4837 } else {
4838 DCHECK(locations->Out().IsRegister());
4839 dst = locations->Out().AsRegister<Register>();
4840 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004841 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004842 } else {
4843 DCHECK(locations->Out().IsFpuRegister());
4844 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4845 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004846 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004847 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004848 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004849 }
4850 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004851 }
4852
4853 if (is_volatile) {
4854 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4855 }
4856}
4857
4858void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4859 Primitive::Type field_type = field_info.GetFieldType();
4860 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4861 bool generate_volatile = field_info.IsVolatile() && is_wide;
4862 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004863 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004864
4865 locations->SetInAt(0, Location::RequiresRegister());
4866 if (generate_volatile) {
4867 InvokeRuntimeCallingConvention calling_convention;
4868 // need A0 to hold base + offset
4869 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4870 if (field_type == Primitive::kPrimLong) {
4871 locations->SetInAt(1, Location::RegisterPairLocation(
4872 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4873 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004874 // Use Location::Any() to prevent situations when running out of available fp registers.
4875 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004876 // Pass FP parameters in core registers.
4877 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4878 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4879 }
4880 } else {
4881 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004882 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004883 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004884 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004885 }
4886 }
4887}
4888
4889void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
4890 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01004891 uint32_t dex_pc,
4892 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004893 Primitive::Type type = field_info.GetFieldType();
4894 LocationSummary* locations = instruction->GetLocations();
4895 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07004896 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004897 StoreOperandType store_type = kStoreByte;
4898 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004899 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004900 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004901
4902 switch (type) {
4903 case Primitive::kPrimBoolean:
4904 case Primitive::kPrimByte:
4905 store_type = kStoreByte;
4906 break;
4907 case Primitive::kPrimShort:
4908 case Primitive::kPrimChar:
4909 store_type = kStoreHalfword;
4910 break;
4911 case Primitive::kPrimInt:
4912 case Primitive::kPrimFloat:
4913 case Primitive::kPrimNot:
4914 store_type = kStoreWord;
4915 break;
4916 case Primitive::kPrimLong:
4917 case Primitive::kPrimDouble:
4918 store_type = kStoreDoubleword;
4919 break;
4920 case Primitive::kPrimVoid:
4921 LOG(FATAL) << "Unreachable type " << type;
4922 UNREACHABLE();
4923 }
4924
4925 if (is_volatile) {
4926 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4927 }
4928
4929 if (is_volatile && store_type == kStoreDoubleword) {
4930 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004931 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004932 // Do implicit Null check.
4933 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4934 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4935 if (type == Primitive::kPrimDouble) {
4936 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004937 if (value_location.IsFpuRegister()) {
4938 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4939 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004940 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004941 value_location.AsFpuRegister<FRegister>());
4942 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004943 __ LoadFromOffset(kLoadWord,
4944 locations->GetTemp(1).AsRegister<Register>(),
4945 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004946 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004947 __ LoadFromOffset(kLoadWord,
4948 locations->GetTemp(2).AsRegister<Register>(),
4949 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004950 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004951 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004952 DCHECK(value_location.IsConstant());
4953 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4954 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004955 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4956 locations->GetTemp(1).AsRegister<Register>(),
4957 value);
4958 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004959 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004960 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004961 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4962 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004963 if (value_location.IsConstant()) {
4964 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4965 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4966 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004967 Register src;
4968 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004969 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004970 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004971 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004972 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004973 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004974 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004975 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004976 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004977 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004978 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004979 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004980 }
4981 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004982 }
4983
4984 // TODO: memory barriers?
4985 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004986 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01004987 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004988 }
4989
4990 if (is_volatile) {
4991 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4992 }
4993}
4994
4995void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4996 HandleFieldGet(instruction, instruction->GetFieldInfo());
4997}
4998
4999void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5000 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5001}
5002
5003void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5004 HandleFieldSet(instruction, instruction->GetFieldInfo());
5005}
5006
5007void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01005008 HandleFieldSet(instruction,
5009 instruction->GetFieldInfo(),
5010 instruction->GetDexPc(),
5011 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005012}
5013
Alexey Frunze06a46c42016-07-19 15:00:40 -07005014void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
5015 HInstruction* instruction ATTRIBUTE_UNUSED,
5016 Location root,
5017 Register obj,
5018 uint32_t offset) {
5019 Register root_reg = root.AsRegister<Register>();
5020 if (kEmitCompilerReadBarrier) {
5021 UNIMPLEMENTED(FATAL) << "for read barrier";
5022 } else {
5023 // Plain GC root load with no read barrier.
5024 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5025 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5026 // Note that GC roots are not affected by heap poisoning, thus we
5027 // do not have to unpoison `root_reg` here.
5028 }
5029}
5030
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005031void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5032 LocationSummary::CallKind call_kind =
5033 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5034 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5035 locations->SetInAt(0, Location::RequiresRegister());
5036 locations->SetInAt(1, Location::RequiresRegister());
5037 // The output does overlap inputs.
5038 // Note that TypeCheckSlowPathMIPS uses this register too.
5039 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5040}
5041
5042void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5043 LocationSummary* locations = instruction->GetLocations();
5044 Register obj = locations->InAt(0).AsRegister<Register>();
5045 Register cls = locations->InAt(1).AsRegister<Register>();
5046 Register out = locations->Out().AsRegister<Register>();
5047
5048 MipsLabel done;
5049
5050 // Return 0 if `obj` is null.
5051 // TODO: Avoid this check if we know `obj` is not null.
5052 __ Move(out, ZERO);
5053 __ Beqz(obj, &done);
5054
5055 // Compare the class of `obj` with `cls`.
5056 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
5057 if (instruction->IsExactCheck()) {
5058 // Classes must be equal for the instanceof to succeed.
5059 __ Xor(out, out, cls);
5060 __ Sltiu(out, out, 1);
5061 } else {
5062 // If the classes are not equal, we go into a slow path.
5063 DCHECK(locations->OnlyCallsOnSlowPath());
5064 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5065 codegen_->AddSlowPath(slow_path);
5066 __ Bne(out, cls, slow_path->GetEntryLabel());
5067 __ LoadConst32(out, 1);
5068 __ Bind(slow_path->GetExitLabel());
5069 }
5070
5071 __ Bind(&done);
5072}
5073
5074void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5075 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5076 locations->SetOut(Location::ConstantLocation(constant));
5077}
5078
5079void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5080 // Will be generated at use site.
5081}
5082
5083void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5084 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5085 locations->SetOut(Location::ConstantLocation(constant));
5086}
5087
5088void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5089 // Will be generated at use site.
5090}
5091
5092void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5093 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5094 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5095}
5096
5097void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5098 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005099 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005100 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005101 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005102}
5103
5104void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5105 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5106 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005107 Location receiver = invoke->GetLocations()->InAt(0);
5108 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005109 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005110
5111 // Set the hidden argument.
5112 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5113 invoke->GetDexMethodIndex());
5114
5115 // temp = object->GetClass();
5116 if (receiver.IsStackSlot()) {
5117 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5118 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5119 } else {
5120 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5121 }
5122 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005123 __ LoadFromOffset(kLoadWord, temp, temp,
5124 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5125 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005126 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005127 // temp = temp->GetImtEntryAt(method_offset);
5128 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5129 // T9 = temp->GetEntryPoint();
5130 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5131 // T9();
5132 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005133 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005134 DCHECK(!codegen_->IsLeafMethod());
5135 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5136}
5137
5138void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005139 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5140 if (intrinsic.TryDispatch(invoke)) {
5141 return;
5142 }
5143
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005144 HandleInvoke(invoke);
5145}
5146
5147void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005148 // Explicit clinit checks triggered by static invokes must have been pruned by
5149 // art::PrepareForRegisterAllocation.
5150 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005151
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00005152 bool has_extra_input = invoke->HasPcRelativeDexCache();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005153
Chris Larsen701566a2015-10-27 15:29:13 -07005154 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5155 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005156 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5157 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5158 }
Chris Larsen701566a2015-10-27 15:29:13 -07005159 return;
5160 }
5161
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005162 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005163
5164 // Add the extra input register if either the dex cache array base register
5165 // or the PC-relative base register for accessing literals is needed.
5166 if (has_extra_input) {
5167 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5168 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005169}
5170
Orion Hodsonac141392017-01-13 11:53:47 +00005171void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5172 HandleInvoke(invoke);
5173}
5174
5175void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5176 codegen_->GenerateInvokePolymorphicCall(invoke);
5177}
5178
Chris Larsen701566a2015-10-27 15:29:13 -07005179static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005180 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005181 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5182 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005183 return true;
5184 }
5185 return false;
5186}
5187
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005188HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005189 HLoadString::LoadKind desired_string_load_kind) {
5190 if (kEmitCompilerReadBarrier) {
5191 UNIMPLEMENTED(FATAL) << "for read barrier";
5192 }
5193 // We disable PC-relative load when there is an irreducible loop, as the optimization
5194 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005195 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5196 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005197 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5198 bool fallback_load = has_irreducible_loops;
5199 switch (desired_string_load_kind) {
5200 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5201 DCHECK(!GetCompilerOptions().GetCompilePic());
5202 break;
5203 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5204 DCHECK(GetCompilerOptions().GetCompilePic());
5205 break;
5206 case HLoadString::LoadKind::kBootImageAddress:
5207 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005208 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005209 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005210 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005211 case HLoadString::LoadKind::kJitTableAddress:
5212 DCHECK(Runtime::Current()->UseJitCompilation());
5213 // TODO: implement.
5214 fallback_load = true;
5215 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005216 case HLoadString::LoadKind::kDexCacheViaMethod:
5217 fallback_load = false;
5218 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005219 }
5220 if (fallback_load) {
5221 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5222 }
5223 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005224}
5225
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005226HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5227 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005228 if (kEmitCompilerReadBarrier) {
5229 UNIMPLEMENTED(FATAL) << "for read barrier";
5230 }
5231 // We disable pc-relative load when there is an irreducible loop, as the optimization
5232 // is incompatible with it.
5233 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5234 bool fallback_load = has_irreducible_loops;
5235 switch (desired_class_load_kind) {
5236 case HLoadClass::LoadKind::kReferrersClass:
5237 fallback_load = false;
5238 break;
5239 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5240 DCHECK(!GetCompilerOptions().GetCompilePic());
5241 break;
5242 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5243 DCHECK(GetCompilerOptions().GetCompilePic());
5244 break;
5245 case HLoadClass::LoadKind::kBootImageAddress:
5246 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005247 case HLoadClass::LoadKind::kBssEntry:
5248 DCHECK(!Runtime::Current()->UseJitCompilation());
5249 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005250 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005251 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005252 fallback_load = true;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005253 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005254 case HLoadClass::LoadKind::kDexCacheViaMethod:
5255 fallback_load = false;
5256 break;
5257 }
5258 if (fallback_load) {
5259 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5260 }
5261 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005262}
5263
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005264Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5265 Register temp) {
5266 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5267 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5268 if (!invoke->GetLocations()->Intrinsified()) {
5269 return location.AsRegister<Register>();
5270 }
5271 // For intrinsics we allow any location, so it may be on the stack.
5272 if (!location.IsRegister()) {
5273 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5274 return temp;
5275 }
5276 // For register locations, check if the register was saved. If so, get it from the stack.
5277 // Note: There is a chance that the register was saved but not overwritten, so we could
5278 // save one load. However, since this is just an intrinsic slow path we prefer this
5279 // simple and more robust approach rather that trying to determine if that's the case.
5280 SlowPathCode* slow_path = GetCurrentSlowPath();
5281 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5282 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5283 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5284 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5285 return temp;
5286 }
5287 return location.AsRegister<Register>();
5288}
5289
Vladimir Markodc151b22015-10-15 18:02:30 +01005290HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5291 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005292 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005293 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
5294 // We disable PC-relative load when there is an irreducible loop, as the optimization
5295 // is incompatible with it.
5296 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5297 bool fallback_load = true;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005298 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005299 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005300 fallback_load = has_irreducible_loops;
5301 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005302 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005303 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005304 break;
5305 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005306 if (fallback_load) {
5307 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5308 dispatch_info.method_load_data = 0;
5309 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005310 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005311}
5312
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005313void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5314 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005315 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005316 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5317 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00005318 Register base_reg = invoke->HasPcRelativeDexCache()
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005319 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5320 : ZERO;
5321
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005322 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005323 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005324 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005325 uint32_t offset =
5326 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005327 __ LoadFromOffset(kLoadWord,
5328 temp.AsRegister<Register>(),
5329 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005330 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005331 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005332 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005333 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005334 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005335 break;
5336 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5337 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5338 break;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005339 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
5340 HMipsDexCacheArraysBase* base =
5341 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5342 int32_t offset =
5343 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5344 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5345 break;
5346 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005347 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005348 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005349 Register reg = temp.AsRegister<Register>();
5350 Register method_reg;
5351 if (current_method.IsRegister()) {
5352 method_reg = current_method.AsRegister<Register>();
5353 } else {
5354 // TODO: use the appropriate DCHECK() here if possible.
5355 // DCHECK(invoke->GetLocations()->Intrinsified());
5356 DCHECK(!current_method.IsValid());
5357 method_reg = reg;
5358 __ Lw(reg, SP, kCurrentMethodStackOffset);
5359 }
5360
5361 // temp = temp->dex_cache_resolved_methods_;
5362 __ LoadFromOffset(kLoadWord,
5363 reg,
5364 method_reg,
5365 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005366 // temp = temp[index_in_cache];
5367 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5368 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005369 __ LoadFromOffset(kLoadWord,
5370 reg,
5371 reg,
5372 CodeGenerator::GetCachePointerOffset(index_in_cache));
5373 break;
5374 }
5375 }
5376
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005377 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005378 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005379 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005380 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005381 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5382 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005383 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005384 T9,
5385 callee_method.AsRegister<Register>(),
5386 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005387 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005388 // T9()
5389 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005390 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005391 break;
5392 }
5393 DCHECK(!IsLeafMethod());
5394}
5395
5396void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005397 // Explicit clinit checks triggered by static invokes must have been pruned by
5398 // art::PrepareForRegisterAllocation.
5399 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005400
5401 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5402 return;
5403 }
5404
5405 LocationSummary* locations = invoke->GetLocations();
5406 codegen_->GenerateStaticOrDirectCall(invoke,
5407 locations->HasTemps()
5408 ? locations->GetTemp(0)
5409 : Location::NoLocation());
5410 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5411}
5412
Chris Larsen3acee732015-11-18 13:31:08 -08005413void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005414 // Use the calling convention instead of the location of the receiver, as
5415 // intrinsics may have put the receiver in a different register. In the intrinsics
5416 // slow path, the arguments have been moved to the right place, so here we are
5417 // guaranteed that the receiver is the first register of the calling convention.
5418 InvokeDexCallingConvention calling_convention;
5419 Register receiver = calling_convention.GetRegisterAt(0);
5420
Chris Larsen3acee732015-11-18 13:31:08 -08005421 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005422 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5423 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5424 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005425 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005426
5427 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005428 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005429 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005430 // temp = temp->GetMethodAt(method_offset);
5431 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5432 // T9 = temp->GetEntryPoint();
5433 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5434 // T9();
5435 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005436 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005437}
5438
5439void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5440 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5441 return;
5442 }
5443
5444 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005445 DCHECK(!codegen_->IsLeafMethod());
5446 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5447}
5448
5449void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005450 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5451 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005452 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005453 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005454 cls,
5455 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005456 Location::RegisterLocation(V0));
Alexey Frunze06a46c42016-07-19 15:00:40 -07005457 return;
5458 }
Vladimir Marko41559982017-01-06 14:04:23 +00005459 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005460
5461 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5462 ? LocationSummary::kCallOnSlowPath
5463 : LocationSummary::kNoCall;
5464 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005465 switch (load_kind) {
5466 // We need an extra register for PC-relative literals on R2.
5467 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005468 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005469 case HLoadClass::LoadKind::kBootImageAddress:
5470 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005471 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5472 break;
5473 }
5474 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005475 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005476 locations->SetInAt(0, Location::RequiresRegister());
5477 break;
5478 default:
5479 break;
5480 }
5481 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005482}
5483
5484void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005485 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5486 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5487 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01005488 return;
5489 }
Vladimir Marko41559982017-01-06 14:04:23 +00005490 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01005491
Vladimir Marko41559982017-01-06 14:04:23 +00005492 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005493 Location out_loc = locations->Out();
5494 Register out = out_loc.AsRegister<Register>();
5495 Register base_or_current_method_reg;
5496 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5497 switch (load_kind) {
5498 // We need an extra register for PC-relative literals on R2.
5499 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005500 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005501 case HLoadClass::LoadKind::kBootImageAddress:
5502 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005503 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5504 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005505 case HLoadClass::LoadKind::kReferrersClass:
5506 case HLoadClass::LoadKind::kDexCacheViaMethod:
5507 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5508 break;
5509 default:
5510 base_or_current_method_reg = ZERO;
5511 break;
5512 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005513
Alexey Frunze06a46c42016-07-19 15:00:40 -07005514 bool generate_null_check = false;
5515 switch (load_kind) {
5516 case HLoadClass::LoadKind::kReferrersClass: {
5517 DCHECK(!cls->CanCallRuntime());
5518 DCHECK(!cls->MustGenerateClinitCheck());
5519 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5520 GenerateGcRootFieldLoad(cls,
5521 out_loc,
5522 base_or_current_method_reg,
5523 ArtMethod::DeclaringClassOffset().Int32Value());
5524 break;
5525 }
5526 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005527 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005528 __ LoadLiteral(out,
5529 base_or_current_method_reg,
5530 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5531 cls->GetTypeIndex()));
5532 break;
5533 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005534 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005535 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5536 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005537 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005538 break;
5539 }
5540 case HLoadClass::LoadKind::kBootImageAddress: {
5541 DCHECK(!kEmitCompilerReadBarrier);
5542 DCHECK_NE(cls->GetAddress(), 0u);
5543 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5544 __ LoadLiteral(out,
5545 base_or_current_method_reg,
5546 codegen_->DeduplicateBootImageAddressLiteral(address));
5547 break;
5548 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005549 case HLoadClass::LoadKind::kBssEntry: {
5550 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5551 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5552 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5553 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
5554 __ LoadFromOffset(kLoadWord, out, out, 0);
5555 generate_null_check = true;
5556 break;
5557 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005558 case HLoadClass::LoadKind::kJitTableAddress: {
5559 LOG(FATAL) << "Unimplemented";
Alexey Frunze06a46c42016-07-19 15:00:40 -07005560 break;
5561 }
Vladimir Marko41559982017-01-06 14:04:23 +00005562 case HLoadClass::LoadKind::kDexCacheViaMethod:
5563 LOG(FATAL) << "UNREACHABLE";
5564 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005565 }
5566
5567 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5568 DCHECK(cls->CanCallRuntime());
5569 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5570 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5571 codegen_->AddSlowPath(slow_path);
5572 if (generate_null_check) {
5573 __ Beqz(out, slow_path->GetEntryLabel());
5574 }
5575 if (cls->MustGenerateClinitCheck()) {
5576 GenerateClassInitializationCheck(slow_path, out);
5577 } else {
5578 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005579 }
5580 }
5581}
5582
5583static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005584 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005585}
5586
5587void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5588 LocationSummary* locations =
5589 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5590 locations->SetOut(Location::RequiresRegister());
5591}
5592
5593void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5594 Register out = load->GetLocations()->Out().AsRegister<Register>();
5595 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5596}
5597
5598void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5599 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5600}
5601
5602void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5603 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5604}
5605
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005606void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005607 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005609 HLoadString::LoadKind load_kind = load->GetLoadKind();
5610 switch (load_kind) {
5611 // We need an extra register for PC-relative literals on R2.
5612 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5613 case HLoadString::LoadKind::kBootImageAddress:
5614 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005615 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005616 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5617 break;
5618 }
5619 FALLTHROUGH_INTENDED;
5620 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005621 case HLoadString::LoadKind::kDexCacheViaMethod:
5622 locations->SetInAt(0, Location::RequiresRegister());
5623 break;
5624 default:
5625 break;
5626 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005627 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5628 InvokeRuntimeCallingConvention calling_convention;
5629 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5630 } else {
5631 locations->SetOut(Location::RequiresRegister());
5632 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005633}
5634
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005635// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5636// move.
5637void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005638 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005639 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005640 Location out_loc = locations->Out();
5641 Register out = out_loc.AsRegister<Register>();
5642 Register base_or_current_method_reg;
5643 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5644 switch (load_kind) {
5645 // We need an extra register for PC-relative literals on R2.
5646 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5647 case HLoadString::LoadKind::kBootImageAddress:
5648 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005649 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005650 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5651 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005652 default:
5653 base_or_current_method_reg = ZERO;
5654 break;
5655 }
5656
5657 switch (load_kind) {
5658 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005659 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005660 __ LoadLiteral(out,
5661 base_or_current_method_reg,
5662 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5663 load->GetStringIndex()));
5664 return; // No dex cache slow path.
5665 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005666 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005667 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005668 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005669 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005670 return; // No dex cache slow path.
5671 }
5672 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005673 uint32_t address = dchecked_integral_cast<uint32_t>(
5674 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5675 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005676 __ LoadLiteral(out,
5677 base_or_current_method_reg,
5678 codegen_->DeduplicateBootImageAddressLiteral(address));
5679 return; // No dex cache slow path.
5680 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005681 case HLoadString::LoadKind::kBssEntry: {
5682 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5683 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005684 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005685 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
5686 __ LoadFromOffset(kLoadWord, out, out, 0);
5687 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5688 codegen_->AddSlowPath(slow_path);
5689 __ Beqz(out, slow_path->GetEntryLabel());
5690 __ Bind(slow_path->GetExitLabel());
5691 return;
5692 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005693 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005694 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005695 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005696
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005697 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005698 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5699 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005700 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005701 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5702 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005703}
5704
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005705void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5706 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5707 locations->SetOut(Location::ConstantLocation(constant));
5708}
5709
5710void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5711 // Will be generated at use site.
5712}
5713
5714void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5715 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005716 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005717 InvokeRuntimeCallingConvention calling_convention;
5718 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5719}
5720
5721void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5722 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005723 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005724 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5725 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005726 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005727 }
5728 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5729}
5730
5731void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5732 LocationSummary* locations =
5733 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5734 switch (mul->GetResultType()) {
5735 case Primitive::kPrimInt:
5736 case Primitive::kPrimLong:
5737 locations->SetInAt(0, Location::RequiresRegister());
5738 locations->SetInAt(1, Location::RequiresRegister());
5739 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5740 break;
5741
5742 case Primitive::kPrimFloat:
5743 case Primitive::kPrimDouble:
5744 locations->SetInAt(0, Location::RequiresFpuRegister());
5745 locations->SetInAt(1, Location::RequiresFpuRegister());
5746 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5747 break;
5748
5749 default:
5750 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5751 }
5752}
5753
5754void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5755 Primitive::Type type = instruction->GetType();
5756 LocationSummary* locations = instruction->GetLocations();
5757 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5758
5759 switch (type) {
5760 case Primitive::kPrimInt: {
5761 Register dst = locations->Out().AsRegister<Register>();
5762 Register lhs = locations->InAt(0).AsRegister<Register>();
5763 Register rhs = locations->InAt(1).AsRegister<Register>();
5764
5765 if (isR6) {
5766 __ MulR6(dst, lhs, rhs);
5767 } else {
5768 __ MulR2(dst, lhs, rhs);
5769 }
5770 break;
5771 }
5772 case Primitive::kPrimLong: {
5773 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5774 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5775 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5776 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5777 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5778 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5779
5780 // Extra checks to protect caused by the existance of A1_A2.
5781 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5782 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5783 DCHECK_NE(dst_high, lhs_low);
5784 DCHECK_NE(dst_high, rhs_low);
5785
5786 // A_B * C_D
5787 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5788 // dst_lo: [ low(B*D) ]
5789 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5790
5791 if (isR6) {
5792 __ MulR6(TMP, lhs_high, rhs_low);
5793 __ MulR6(dst_high, lhs_low, rhs_high);
5794 __ Addu(dst_high, dst_high, TMP);
5795 __ MuhuR6(TMP, lhs_low, rhs_low);
5796 __ Addu(dst_high, dst_high, TMP);
5797 __ MulR6(dst_low, lhs_low, rhs_low);
5798 } else {
5799 __ MulR2(TMP, lhs_high, rhs_low);
5800 __ MulR2(dst_high, lhs_low, rhs_high);
5801 __ Addu(dst_high, dst_high, TMP);
5802 __ MultuR2(lhs_low, rhs_low);
5803 __ Mfhi(TMP);
5804 __ Addu(dst_high, dst_high, TMP);
5805 __ Mflo(dst_low);
5806 }
5807 break;
5808 }
5809 case Primitive::kPrimFloat:
5810 case Primitive::kPrimDouble: {
5811 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5812 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5813 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5814 if (type == Primitive::kPrimFloat) {
5815 __ MulS(dst, lhs, rhs);
5816 } else {
5817 __ MulD(dst, lhs, rhs);
5818 }
5819 break;
5820 }
5821 default:
5822 LOG(FATAL) << "Unexpected mul type " << type;
5823 }
5824}
5825
5826void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5827 LocationSummary* locations =
5828 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5829 switch (neg->GetResultType()) {
5830 case Primitive::kPrimInt:
5831 case Primitive::kPrimLong:
5832 locations->SetInAt(0, Location::RequiresRegister());
5833 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5834 break;
5835
5836 case Primitive::kPrimFloat:
5837 case Primitive::kPrimDouble:
5838 locations->SetInAt(0, Location::RequiresFpuRegister());
5839 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5840 break;
5841
5842 default:
5843 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5844 }
5845}
5846
5847void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5848 Primitive::Type type = instruction->GetType();
5849 LocationSummary* locations = instruction->GetLocations();
5850
5851 switch (type) {
5852 case Primitive::kPrimInt: {
5853 Register dst = locations->Out().AsRegister<Register>();
5854 Register src = locations->InAt(0).AsRegister<Register>();
5855 __ Subu(dst, ZERO, src);
5856 break;
5857 }
5858 case Primitive::kPrimLong: {
5859 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5860 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5861 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5862 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5863 __ Subu(dst_low, ZERO, src_low);
5864 __ Sltu(TMP, ZERO, dst_low);
5865 __ Subu(dst_high, ZERO, src_high);
5866 __ Subu(dst_high, dst_high, TMP);
5867 break;
5868 }
5869 case Primitive::kPrimFloat:
5870 case Primitive::kPrimDouble: {
5871 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5872 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5873 if (type == Primitive::kPrimFloat) {
5874 __ NegS(dst, src);
5875 } else {
5876 __ NegD(dst, src);
5877 }
5878 break;
5879 }
5880 default:
5881 LOG(FATAL) << "Unexpected neg type " << type;
5882 }
5883}
5884
5885void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5886 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005887 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005888 InvokeRuntimeCallingConvention calling_convention;
5889 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5890 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5891 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5892 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5893}
5894
5895void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5896 InvokeRuntimeCallingConvention calling_convention;
5897 Register current_method_register = calling_convention.GetRegisterAt(2);
5898 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5899 // Move an uint16_t value to a register.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005900 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex().index_);
Serban Constantinescufca16662016-07-14 09:21:59 +01005901 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005902 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5903 void*, uint32_t, int32_t, ArtMethod*>();
5904}
5905
5906void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5907 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005908 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005909 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005910 if (instruction->IsStringAlloc()) {
5911 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5912 } else {
5913 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005914 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005915 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5916}
5917
5918void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005919 if (instruction->IsStringAlloc()) {
5920 // String is allocated through StringFactory. Call NewEmptyString entry point.
5921 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005922 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005923 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5924 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5925 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005926 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00005927 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5928 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005929 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005930 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005931 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005932}
5933
5934void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
5935 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5936 locations->SetInAt(0, Location::RequiresRegister());
5937 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5938}
5939
5940void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
5941 Primitive::Type type = instruction->GetType();
5942 LocationSummary* locations = instruction->GetLocations();
5943
5944 switch (type) {
5945 case Primitive::kPrimInt: {
5946 Register dst = locations->Out().AsRegister<Register>();
5947 Register src = locations->InAt(0).AsRegister<Register>();
5948 __ Nor(dst, src, ZERO);
5949 break;
5950 }
5951
5952 case Primitive::kPrimLong: {
5953 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5954 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5955 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5956 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5957 __ Nor(dst_high, src_high, ZERO);
5958 __ Nor(dst_low, src_low, ZERO);
5959 break;
5960 }
5961
5962 default:
5963 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5964 }
5965}
5966
5967void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5968 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5969 locations->SetInAt(0, Location::RequiresRegister());
5970 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5971}
5972
5973void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5974 LocationSummary* locations = instruction->GetLocations();
5975 __ Xori(locations->Out().AsRegister<Register>(),
5976 locations->InAt(0).AsRegister<Register>(),
5977 1);
5978}
5979
5980void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005981 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5982 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005983}
5984
Calin Juravle2ae48182016-03-16 14:05:09 +00005985void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
5986 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005987 return;
5988 }
5989 Location obj = instruction->GetLocations()->InAt(0);
5990
5991 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005992 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005993}
5994
Calin Juravle2ae48182016-03-16 14:05:09 +00005995void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005996 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005997 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005998
5999 Location obj = instruction->GetLocations()->InAt(0);
6000
6001 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6002}
6003
6004void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006005 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006006}
6007
6008void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6009 HandleBinaryOp(instruction);
6010}
6011
6012void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6013 HandleBinaryOp(instruction);
6014}
6015
6016void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6017 LOG(FATAL) << "Unreachable";
6018}
6019
6020void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6021 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6022}
6023
6024void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6025 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6026 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6027 if (location.IsStackSlot()) {
6028 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6029 } else if (location.IsDoubleStackSlot()) {
6030 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6031 }
6032 locations->SetOut(location);
6033}
6034
6035void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6036 ATTRIBUTE_UNUSED) {
6037 // Nothing to do, the parameter is already at its location.
6038}
6039
6040void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6041 LocationSummary* locations =
6042 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6043 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6044}
6045
6046void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6047 ATTRIBUTE_UNUSED) {
6048 // Nothing to do, the method is already at its location.
6049}
6050
6051void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6052 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006053 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006054 locations->SetInAt(i, Location::Any());
6055 }
6056 locations->SetOut(Location::Any());
6057}
6058
6059void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6060 LOG(FATAL) << "Unreachable";
6061}
6062
6063void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6064 Primitive::Type type = rem->GetResultType();
6065 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006066 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006067 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6068
6069 switch (type) {
6070 case Primitive::kPrimInt:
6071 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006072 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6074 break;
6075
6076 case Primitive::kPrimLong: {
6077 InvokeRuntimeCallingConvention calling_convention;
6078 locations->SetInAt(0, Location::RegisterPairLocation(
6079 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6080 locations->SetInAt(1, Location::RegisterPairLocation(
6081 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6082 locations->SetOut(calling_convention.GetReturnLocation(type));
6083 break;
6084 }
6085
6086 case Primitive::kPrimFloat:
6087 case Primitive::kPrimDouble: {
6088 InvokeRuntimeCallingConvention calling_convention;
6089 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6090 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6091 locations->SetOut(calling_convention.GetReturnLocation(type));
6092 break;
6093 }
6094
6095 default:
6096 LOG(FATAL) << "Unexpected rem type " << type;
6097 }
6098}
6099
6100void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6101 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006102
6103 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006104 case Primitive::kPrimInt:
6105 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006106 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006107 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006108 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006109 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6110 break;
6111 }
6112 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006113 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006114 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006115 break;
6116 }
6117 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006118 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006119 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006120 break;
6121 }
6122 default:
6123 LOG(FATAL) << "Unexpected rem type " << type;
6124 }
6125}
6126
6127void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6128 memory_barrier->SetLocations(nullptr);
6129}
6130
6131void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6132 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6133}
6134
6135void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6136 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6137 Primitive::Type return_type = ret->InputAt(0)->GetType();
6138 locations->SetInAt(0, MipsReturnLocation(return_type));
6139}
6140
6141void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6142 codegen_->GenerateFrameExit();
6143}
6144
6145void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6146 ret->SetLocations(nullptr);
6147}
6148
6149void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6150 codegen_->GenerateFrameExit();
6151}
6152
Alexey Frunze92d90602015-12-18 18:16:36 -08006153void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6154 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006155}
6156
Alexey Frunze92d90602015-12-18 18:16:36 -08006157void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6158 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006159}
6160
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006161void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6162 HandleShift(shl);
6163}
6164
6165void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6166 HandleShift(shl);
6167}
6168
6169void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6170 HandleShift(shr);
6171}
6172
6173void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6174 HandleShift(shr);
6175}
6176
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006177void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6178 HandleBinaryOp(instruction);
6179}
6180
6181void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6182 HandleBinaryOp(instruction);
6183}
6184
6185void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6186 HandleFieldGet(instruction, instruction->GetFieldInfo());
6187}
6188
6189void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6190 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6191}
6192
6193void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6194 HandleFieldSet(instruction, instruction->GetFieldInfo());
6195}
6196
6197void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006198 HandleFieldSet(instruction,
6199 instruction->GetFieldInfo(),
6200 instruction->GetDexPc(),
6201 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006202}
6203
6204void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6205 HUnresolvedInstanceFieldGet* instruction) {
6206 FieldAccessCallingConventionMIPS calling_convention;
6207 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6208 instruction->GetFieldType(),
6209 calling_convention);
6210}
6211
6212void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6213 HUnresolvedInstanceFieldGet* instruction) {
6214 FieldAccessCallingConventionMIPS calling_convention;
6215 codegen_->GenerateUnresolvedFieldAccess(instruction,
6216 instruction->GetFieldType(),
6217 instruction->GetFieldIndex(),
6218 instruction->GetDexPc(),
6219 calling_convention);
6220}
6221
6222void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6223 HUnresolvedInstanceFieldSet* instruction) {
6224 FieldAccessCallingConventionMIPS calling_convention;
6225 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6226 instruction->GetFieldType(),
6227 calling_convention);
6228}
6229
6230void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6231 HUnresolvedInstanceFieldSet* instruction) {
6232 FieldAccessCallingConventionMIPS calling_convention;
6233 codegen_->GenerateUnresolvedFieldAccess(instruction,
6234 instruction->GetFieldType(),
6235 instruction->GetFieldIndex(),
6236 instruction->GetDexPc(),
6237 calling_convention);
6238}
6239
6240void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6241 HUnresolvedStaticFieldGet* instruction) {
6242 FieldAccessCallingConventionMIPS calling_convention;
6243 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6244 instruction->GetFieldType(),
6245 calling_convention);
6246}
6247
6248void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6249 HUnresolvedStaticFieldGet* instruction) {
6250 FieldAccessCallingConventionMIPS calling_convention;
6251 codegen_->GenerateUnresolvedFieldAccess(instruction,
6252 instruction->GetFieldType(),
6253 instruction->GetFieldIndex(),
6254 instruction->GetDexPc(),
6255 calling_convention);
6256}
6257
6258void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6259 HUnresolvedStaticFieldSet* instruction) {
6260 FieldAccessCallingConventionMIPS calling_convention;
6261 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6262 instruction->GetFieldType(),
6263 calling_convention);
6264}
6265
6266void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6267 HUnresolvedStaticFieldSet* instruction) {
6268 FieldAccessCallingConventionMIPS calling_convention;
6269 codegen_->GenerateUnresolvedFieldAccess(instruction,
6270 instruction->GetFieldType(),
6271 instruction->GetFieldIndex(),
6272 instruction->GetDexPc(),
6273 calling_convention);
6274}
6275
6276void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006277 LocationSummary* locations =
6278 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006279 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006280}
6281
6282void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6283 HBasicBlock* block = instruction->GetBlock();
6284 if (block->GetLoopInformation() != nullptr) {
6285 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6286 // The back edge will generate the suspend check.
6287 return;
6288 }
6289 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6290 // The goto will generate the suspend check.
6291 return;
6292 }
6293 GenerateSuspendCheck(instruction, nullptr);
6294}
6295
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006296void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6297 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006298 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006299 InvokeRuntimeCallingConvention calling_convention;
6300 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6301}
6302
6303void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006304 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006305 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6306}
6307
6308void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6309 Primitive::Type input_type = conversion->GetInputType();
6310 Primitive::Type result_type = conversion->GetResultType();
6311 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006312 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006313
6314 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6315 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6316 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6317 }
6318
6319 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006320 if (!isR6 &&
6321 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6322 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006323 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006324 }
6325
6326 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6327
6328 if (call_kind == LocationSummary::kNoCall) {
6329 if (Primitive::IsFloatingPointType(input_type)) {
6330 locations->SetInAt(0, Location::RequiresFpuRegister());
6331 } else {
6332 locations->SetInAt(0, Location::RequiresRegister());
6333 }
6334
6335 if (Primitive::IsFloatingPointType(result_type)) {
6336 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6337 } else {
6338 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6339 }
6340 } else {
6341 InvokeRuntimeCallingConvention calling_convention;
6342
6343 if (Primitive::IsFloatingPointType(input_type)) {
6344 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6345 } else {
6346 DCHECK_EQ(input_type, Primitive::kPrimLong);
6347 locations->SetInAt(0, Location::RegisterPairLocation(
6348 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6349 }
6350
6351 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6352 }
6353}
6354
6355void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6356 LocationSummary* locations = conversion->GetLocations();
6357 Primitive::Type result_type = conversion->GetResultType();
6358 Primitive::Type input_type = conversion->GetInputType();
6359 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006360 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006361
6362 DCHECK_NE(input_type, result_type);
6363
6364 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6365 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6366 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6367 Register src = locations->InAt(0).AsRegister<Register>();
6368
Alexey Frunzea871ef12016-06-27 15:20:11 -07006369 if (dst_low != src) {
6370 __ Move(dst_low, src);
6371 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006372 __ Sra(dst_high, src, 31);
6373 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6374 Register dst = locations->Out().AsRegister<Register>();
6375 Register src = (input_type == Primitive::kPrimLong)
6376 ? locations->InAt(0).AsRegisterPairLow<Register>()
6377 : locations->InAt(0).AsRegister<Register>();
6378
6379 switch (result_type) {
6380 case Primitive::kPrimChar:
6381 __ Andi(dst, src, 0xFFFF);
6382 break;
6383 case Primitive::kPrimByte:
6384 if (has_sign_extension) {
6385 __ Seb(dst, src);
6386 } else {
6387 __ Sll(dst, src, 24);
6388 __ Sra(dst, dst, 24);
6389 }
6390 break;
6391 case Primitive::kPrimShort:
6392 if (has_sign_extension) {
6393 __ Seh(dst, src);
6394 } else {
6395 __ Sll(dst, src, 16);
6396 __ Sra(dst, dst, 16);
6397 }
6398 break;
6399 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006400 if (dst != src) {
6401 __ Move(dst, src);
6402 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006403 break;
6404
6405 default:
6406 LOG(FATAL) << "Unexpected type conversion from " << input_type
6407 << " to " << result_type;
6408 }
6409 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006410 if (input_type == Primitive::kPrimLong) {
6411 if (isR6) {
6412 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6413 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6414 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6415 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6416 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6417 __ Mtc1(src_low, FTMP);
6418 __ Mthc1(src_high, FTMP);
6419 if (result_type == Primitive::kPrimFloat) {
6420 __ Cvtsl(dst, FTMP);
6421 } else {
6422 __ Cvtdl(dst, FTMP);
6423 }
6424 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006425 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6426 : kQuickL2d;
6427 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006428 if (result_type == Primitive::kPrimFloat) {
6429 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6430 } else {
6431 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6432 }
6433 }
6434 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006435 Register src = locations->InAt(0).AsRegister<Register>();
6436 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6437 __ Mtc1(src, FTMP);
6438 if (result_type == Primitive::kPrimFloat) {
6439 __ Cvtsw(dst, FTMP);
6440 } else {
6441 __ Cvtdw(dst, FTMP);
6442 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006443 }
6444 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6445 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006446 if (result_type == Primitive::kPrimLong) {
6447 if (isR6) {
6448 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6449 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6450 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6451 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6452 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6453 MipsLabel truncate;
6454 MipsLabel done;
6455
6456 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6457 // value when the input is either a NaN or is outside of the range of the output type
6458 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6459 // the same result.
6460 //
6461 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6462 // value of the output type if the input is outside of the range after the truncation or
6463 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6464 // results. This matches the desired float/double-to-int/long conversion exactly.
6465 //
6466 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6467 //
6468 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6469 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6470 // even though it must be NAN2008=1 on R6.
6471 //
6472 // The code takes care of the different behaviors by first comparing the input to the
6473 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6474 // If the input is greater than or equal to the minimum, it procedes to the truncate
6475 // instruction, which will handle such an input the same way irrespective of NAN2008.
6476 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6477 // in order to return either zero or the minimum value.
6478 //
6479 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6480 // truncate instruction for MIPS64R6.
6481 if (input_type == Primitive::kPrimFloat) {
6482 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6483 __ LoadConst32(TMP, min_val);
6484 __ Mtc1(TMP, FTMP);
6485 __ CmpLeS(FTMP, FTMP, src);
6486 } else {
6487 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6488 __ LoadConst32(TMP, High32Bits(min_val));
6489 __ Mtc1(ZERO, FTMP);
6490 __ Mthc1(TMP, FTMP);
6491 __ CmpLeD(FTMP, FTMP, src);
6492 }
6493
6494 __ Bc1nez(FTMP, &truncate);
6495
6496 if (input_type == Primitive::kPrimFloat) {
6497 __ CmpEqS(FTMP, src, src);
6498 } else {
6499 __ CmpEqD(FTMP, src, src);
6500 }
6501 __ Move(dst_low, ZERO);
6502 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6503 __ Mfc1(TMP, FTMP);
6504 __ And(dst_high, dst_high, TMP);
6505
6506 __ B(&done);
6507
6508 __ Bind(&truncate);
6509
6510 if (input_type == Primitive::kPrimFloat) {
6511 __ TruncLS(FTMP, src);
6512 } else {
6513 __ TruncLD(FTMP, src);
6514 }
6515 __ Mfc1(dst_low, FTMP);
6516 __ Mfhc1(dst_high, FTMP);
6517
6518 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006519 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006520 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6521 : kQuickD2l;
6522 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006523 if (input_type == Primitive::kPrimFloat) {
6524 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6525 } else {
6526 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6527 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006528 }
6529 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006530 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6531 Register dst = locations->Out().AsRegister<Register>();
6532 MipsLabel truncate;
6533 MipsLabel done;
6534
6535 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6536 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6537 // even though it must be NAN2008=1 on R6.
6538 //
6539 // For details see the large comment above for the truncation of float/double to long on R6.
6540 //
6541 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6542 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006543 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006544 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6545 __ LoadConst32(TMP, min_val);
6546 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006547 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006548 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6549 __ LoadConst32(TMP, High32Bits(min_val));
6550 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006551 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006552 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006553
6554 if (isR6) {
6555 if (input_type == Primitive::kPrimFloat) {
6556 __ CmpLeS(FTMP, FTMP, src);
6557 } else {
6558 __ CmpLeD(FTMP, FTMP, src);
6559 }
6560 __ Bc1nez(FTMP, &truncate);
6561
6562 if (input_type == Primitive::kPrimFloat) {
6563 __ CmpEqS(FTMP, src, src);
6564 } else {
6565 __ CmpEqD(FTMP, src, src);
6566 }
6567 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6568 __ Mfc1(TMP, FTMP);
6569 __ And(dst, dst, TMP);
6570 } else {
6571 if (input_type == Primitive::kPrimFloat) {
6572 __ ColeS(0, FTMP, src);
6573 } else {
6574 __ ColeD(0, FTMP, src);
6575 }
6576 __ Bc1t(0, &truncate);
6577
6578 if (input_type == Primitive::kPrimFloat) {
6579 __ CeqS(0, src, src);
6580 } else {
6581 __ CeqD(0, src, src);
6582 }
6583 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6584 __ Movf(dst, ZERO, 0);
6585 }
6586
6587 __ B(&done);
6588
6589 __ Bind(&truncate);
6590
6591 if (input_type == Primitive::kPrimFloat) {
6592 __ TruncWS(FTMP, src);
6593 } else {
6594 __ TruncWD(FTMP, src);
6595 }
6596 __ Mfc1(dst, FTMP);
6597
6598 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006599 }
6600 } else if (Primitive::IsFloatingPointType(result_type) &&
6601 Primitive::IsFloatingPointType(input_type)) {
6602 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6603 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6604 if (result_type == Primitive::kPrimFloat) {
6605 __ Cvtsd(dst, src);
6606 } else {
6607 __ Cvtds(dst, src);
6608 }
6609 } else {
6610 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6611 << " to " << result_type;
6612 }
6613}
6614
6615void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6616 HandleShift(ushr);
6617}
6618
6619void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6620 HandleShift(ushr);
6621}
6622
6623void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6624 HandleBinaryOp(instruction);
6625}
6626
6627void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6628 HandleBinaryOp(instruction);
6629}
6630
6631void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6632 // Nothing to do, this should be removed during prepare for register allocator.
6633 LOG(FATAL) << "Unreachable";
6634}
6635
6636void InstructionCodeGeneratorMIPS::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 LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006642 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006643}
6644
6645void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006646 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006647}
6648
6649void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006650 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006651}
6652
6653void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006654 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006655}
6656
6657void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006658 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006659}
6660
6661void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006662 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006663}
6664
6665void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006666 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006667}
6668
6669void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006670 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006671}
6672
6673void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006674 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006675}
6676
6677void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006678 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006679}
6680
6681void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006682 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006683}
6684
6685void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006686 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006687}
6688
6689void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006690 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006691}
6692
6693void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006694 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006695}
6696
6697void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006698 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006699}
6700
6701void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006702 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006703}
6704
6705void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006706 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006707}
6708
6709void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006710 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006711}
6712
6713void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006714 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006715}
6716
6717void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006718 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006719}
6720
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006721void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6722 LocationSummary* locations =
6723 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6724 locations->SetInAt(0, Location::RequiresRegister());
6725}
6726
Alexey Frunze96b66822016-09-10 02:32:44 -07006727void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6728 int32_t lower_bound,
6729 uint32_t num_entries,
6730 HBasicBlock* switch_block,
6731 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006732 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006733 Register temp_reg = TMP;
6734 __ Addiu32(temp_reg, value_reg, -lower_bound);
6735 // Jump to default if index is negative
6736 // Note: We don't check the case that index is positive while value < lower_bound, because in
6737 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6738 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6739
Alexey Frunze96b66822016-09-10 02:32:44 -07006740 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006741 // Jump to successors[0] if value == lower_bound.
6742 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6743 int32_t last_index = 0;
6744 for (; num_entries - last_index > 2; last_index += 2) {
6745 __ Addiu(temp_reg, temp_reg, -2);
6746 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6747 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6748 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6749 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6750 }
6751 if (num_entries - last_index == 2) {
6752 // The last missing case_value.
6753 __ Addiu(temp_reg, temp_reg, -1);
6754 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006755 }
6756
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006757 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006758 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006759 __ B(codegen_->GetLabelOf(default_block));
6760 }
6761}
6762
Alexey Frunze96b66822016-09-10 02:32:44 -07006763void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6764 Register constant_area,
6765 int32_t lower_bound,
6766 uint32_t num_entries,
6767 HBasicBlock* switch_block,
6768 HBasicBlock* default_block) {
6769 // Create a jump table.
6770 std::vector<MipsLabel*> labels(num_entries);
6771 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6772 for (uint32_t i = 0; i < num_entries; i++) {
6773 labels[i] = codegen_->GetLabelOf(successors[i]);
6774 }
6775 JumpTable* table = __ CreateJumpTable(std::move(labels));
6776
6777 // Is the value in range?
6778 __ Addiu32(TMP, value_reg, -lower_bound);
6779 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6780 __ Sltiu(AT, TMP, num_entries);
6781 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6782 } else {
6783 __ LoadConst32(AT, num_entries);
6784 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6785 }
6786
6787 // We are in the range of the table.
6788 // Load the target address from the jump table, indexing by the value.
6789 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6790 __ Sll(TMP, TMP, 2);
6791 __ Addu(TMP, TMP, AT);
6792 __ Lw(TMP, TMP, 0);
6793 // Compute the absolute target address by adding the table start address
6794 // (the table contains offsets to targets relative to its start).
6795 __ Addu(TMP, TMP, AT);
6796 // And jump.
6797 __ Jr(TMP);
6798 __ NopIfNoReordering();
6799}
6800
6801void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6802 int32_t lower_bound = switch_instr->GetStartValue();
6803 uint32_t num_entries = switch_instr->GetNumEntries();
6804 LocationSummary* locations = switch_instr->GetLocations();
6805 Register value_reg = locations->InAt(0).AsRegister<Register>();
6806 HBasicBlock* switch_block = switch_instr->GetBlock();
6807 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6808
6809 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6810 num_entries > kPackedSwitchJumpTableThreshold) {
6811 // R6 uses PC-relative addressing to access the jump table.
6812 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6813 // the jump table and it is implemented by changing HPackedSwitch to
6814 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6815 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6816 GenTableBasedPackedSwitch(value_reg,
6817 ZERO,
6818 lower_bound,
6819 num_entries,
6820 switch_block,
6821 default_block);
6822 } else {
6823 GenPackedSwitchWithCompares(value_reg,
6824 lower_bound,
6825 num_entries,
6826 switch_block,
6827 default_block);
6828 }
6829}
6830
6831void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6832 LocationSummary* locations =
6833 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6834 locations->SetInAt(0, Location::RequiresRegister());
6835 // Constant area pointer (HMipsComputeBaseMethodAddress).
6836 locations->SetInAt(1, Location::RequiresRegister());
6837}
6838
6839void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6840 int32_t lower_bound = switch_instr->GetStartValue();
6841 uint32_t num_entries = switch_instr->GetNumEntries();
6842 LocationSummary* locations = switch_instr->GetLocations();
6843 Register value_reg = locations->InAt(0).AsRegister<Register>();
6844 Register constant_area = locations->InAt(1).AsRegister<Register>();
6845 HBasicBlock* switch_block = switch_instr->GetBlock();
6846 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6847
6848 // This is an R2-only path. HPackedSwitch has been changed to
6849 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6850 // required to address the jump table relative to PC.
6851 GenTableBasedPackedSwitch(value_reg,
6852 constant_area,
6853 lower_bound,
6854 num_entries,
6855 switch_block,
6856 default_block);
6857}
6858
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006859void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6860 HMipsComputeBaseMethodAddress* insn) {
6861 LocationSummary* locations =
6862 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6863 locations->SetOut(Location::RequiresRegister());
6864}
6865
6866void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6867 HMipsComputeBaseMethodAddress* insn) {
6868 LocationSummary* locations = insn->GetLocations();
6869 Register reg = locations->Out().AsRegister<Register>();
6870
6871 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6872
6873 // Generate a dummy PC-relative call to obtain PC.
6874 __ Nal();
6875 // Grab the return address off RA.
6876 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006877 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006878
6879 // Remember this offset (the obtained PC value) for later use with constant area.
6880 __ BindPcRelBaseLabel();
6881}
6882
6883void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6884 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6885 locations->SetOut(Location::RequiresRegister());
6886}
6887
6888void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6889 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6890 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6891 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006892 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6893 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006894}
6895
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006896void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6897 // The trampoline uses the same calling convention as dex calling conventions,
6898 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6899 // the method_idx.
6900 HandleInvoke(invoke);
6901}
6902
6903void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6904 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6905}
6906
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006907void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6908 LocationSummary* locations =
6909 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6910 locations->SetInAt(0, Location::RequiresRegister());
6911 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006912}
6913
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006914void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6915 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006916 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006917 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006918 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006919 __ LoadFromOffset(kLoadWord,
6920 locations->Out().AsRegister<Register>(),
6921 locations->InAt(0).AsRegister<Register>(),
6922 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006923 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006924 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006925 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006926 __ LoadFromOffset(kLoadWord,
6927 locations->Out().AsRegister<Register>(),
6928 locations->InAt(0).AsRegister<Register>(),
6929 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006930 __ LoadFromOffset(kLoadWord,
6931 locations->Out().AsRegister<Register>(),
6932 locations->Out().AsRegister<Register>(),
6933 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006934 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006935}
6936
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006937#undef __
6938#undef QUICK_ENTRY_POINT
6939
6940} // namespace mips
6941} // namespace art